leocad/qt/lc_qpropertiesdialog.cpp

148 lines
4 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
#include "lc_qpropertiesdialog.h"
#include "ui_lc_qpropertiesdialog.h"
#include "lc_qutils.h"
2013-08-09 06:57:18 +02:00
#include "lc_colors.h"
#include "lc_application.h"
#include "pieceinf.h"
2019-12-22 21:42:02 +01:00
class lcPartsTableWidgetItem : public QTableWidgetItem
{
public:
explicit lcPartsTableWidgetItem(const QString& Text, int Type = QTableWidgetItem::Type)
2019-12-22 21:42:02 +01:00
: QTableWidgetItem(Text, Type)
{
mLast = false;
}
bool operator<(const QTableWidgetItem& Other) const override
2019-12-22 21:42:02 +01:00
{
if (mLast)
return false;
if (((const lcPartsTableWidgetItem&)Other).mLast)
return true;
2020-12-24 03:07:12 +01:00
if (column() > 0)
{
int Count = text().toInt();
int OtherCount = Other.text().toInt();
return Count < OtherCount;
}
2019-12-22 21:42:02 +01:00
return QTableWidgetItem::operator<(Other);
}
bool mLast;
};
2020-12-24 03:07:12 +01:00
lcQPropertiesDialog::lcQPropertiesDialog(QWidget* Parent, lcPropertiesDialogOptions* Options)
: QDialog(Parent), mOptions(Options), ui(new Ui::lcQPropertiesDialog)
2013-08-09 06:57:18 +02:00
{
ui->setupUi(this);
2020-12-24 03:07:12 +01:00
setWindowTitle(tr("%1 Properties").arg(mOptions->Properties.mFileName));
2014-12-31 18:05:23 +01:00
2020-12-24 03:07:12 +01:00
ui->descriptionEdit->setText(mOptions->Properties.mDescription);
ui->authorEdit->setText(mOptions->Properties.mAuthor);
ui->commentsEdit->setText(mOptions->Properties.mComments);
2013-08-09 06:57:18 +02:00
2020-12-24 03:07:12 +01:00
const lcPartsList& PartsList = mOptions->PartsList;
QStringList horizontalLabels;
2013-08-09 06:57:18 +02:00
2016-11-15 21:12:47 +01:00
QVector<bool> ColorsUsed(gNumUserColors);
2013-08-09 06:57:18 +02:00
for (const auto& PartIt : PartsList)
for (const auto& ColorIt : PartIt.second)
ColorsUsed[ColorIt.first] = true;
2013-08-19 03:01:06 +02:00
2016-11-15 21:12:47 +01:00
QVector<int> ColorColumns(gNumUserColors);
int NumColors = 0;
2013-08-19 03:01:06 +02:00
horizontalLabels.append(tr("Part"));
for (int colorIdx = 0; colorIdx < gNumUserColors; colorIdx++)
{
2016-11-15 21:12:47 +01:00
if (ColorsUsed[colorIdx])
2013-08-19 03:01:06 +02:00
{
2016-11-15 21:12:47 +01:00
ColorColumns[colorIdx] = NumColors++;
2013-08-19 03:01:06 +02:00
horizontalLabels.append(gColorList[colorIdx].Name);
2013-08-09 06:57:18 +02:00
}
}
horizontalLabels.append(tr("Total"));
2013-08-09 06:57:18 +02:00
QTableWidget *table = ui->partsTable;
2016-11-15 21:12:47 +01:00
table->setColumnCount(NumColors + 2);
2019-05-18 20:33:27 +02:00
table->setRowCount((int)PartsList.size() + 1);
2013-08-09 06:57:18 +02:00
table->setHorizontalHeaderLabels(horizontalLabels);
2019-03-11 00:23:32 +01:00
table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
2013-08-19 03:01:06 +02:00
2019-05-18 20:33:27 +02:00
std::vector<int> InfoTotals(PartsList.size());
std::vector<int> ColorTotals(NumColors);
2019-05-28 01:22:49 +02:00
int Row = 0, Total = 0;
for (const auto& PartIt : PartsList)
2013-08-09 06:57:18 +02:00
{
2019-12-22 21:42:02 +01:00
table->setItem(Row, 0, new lcPartsTableWidgetItem(PartIt.first->m_strDescription));
for (const auto& ColorIt : PartIt.second)
{
int ColorIndex = ColorIt.first;
int Count = ColorIt.second;
2019-12-22 21:42:02 +01:00
lcPartsTableWidgetItem* Item = new lcPartsTableWidgetItem(QString::number(Count));
Item->setTextAlignment(Qt::AlignCenter);
table->setItem(Row, ColorColumns[ColorIndex] + 1, Item);
InfoTotals[Row] += Count;
ColorTotals[ColorColumns[ColorIndex]] += Count;
Total += Count;
}
2019-12-22 21:42:02 +01:00
for (int Column = 0; Column <= NumColors; Column++)
if (!table->item(Row, Column))
table->setItem(Row, Column, new lcPartsTableWidgetItem(QString()));
Row++;
}
2013-08-09 06:57:18 +02:00
2019-12-22 21:42:02 +01:00
lcPartsTableWidgetItem* Item = new lcPartsTableWidgetItem(tr("Total"));
Item->mLast = true;
table->setItem((int)InfoTotals.size(), 0, Item);
2019-06-03 22:23:04 +02:00
for (Row = 0; Row < (int)InfoTotals.size(); Row++)
{
2019-12-22 21:42:02 +01:00
lcPartsTableWidgetItem *item = new lcPartsTableWidgetItem(QString::number(InfoTotals[Row]));
2013-08-19 03:01:06 +02:00
item->setTextAlignment(Qt::AlignCenter);
table->setItem(Row, NumColors + 1, item);
2013-08-09 06:57:18 +02:00
}
2016-11-15 21:12:47 +01:00
for (int colorIdx = 0; colorIdx < NumColors; colorIdx++)
{
2019-12-22 21:42:02 +01:00
lcPartsTableWidgetItem *item = new lcPartsTableWidgetItem(QString::number(ColorTotals[colorIdx]));
item->mLast = true;
item->setTextAlignment(Qt::AlignCenter);
2019-05-18 20:33:27 +02:00
table->setItem((int)InfoTotals.size(), colorIdx + 1, item);
}
2019-12-22 21:42:02 +01:00
lcPartsTableWidgetItem *item = new lcPartsTableWidgetItem(QString::number(Total));
item->mLast = true;
item->setTextAlignment(Qt::AlignCenter);
2019-05-18 20:33:27 +02:00
table->setItem((int)InfoTotals.size(), NumColors + 1, item);
2013-08-09 06:57:18 +02:00
}
lcQPropertiesDialog::~lcQPropertiesDialog()
{
delete ui;
}
void lcQPropertiesDialog::accept()
{
2020-12-24 03:07:12 +01:00
mOptions->Properties.mDescription = ui->descriptionEdit->text();
mOptions->Properties.mAuthor = ui->authorEdit->text();
mOptions->Properties.mComments = ui->commentsEdit->toPlainText();
2013-08-09 06:57:18 +02:00
QDialog::accept();
}