Library access cleanup.

This commit is contained in:
Leonardo 2016-11-15 12:12:47 -08:00
parent ed37143a6b
commit 13c42645f1

View file

@ -4,7 +4,6 @@
#include "lc_qutils.h" #include "lc_qutils.h"
#include "lc_basewindow.h" #include "lc_basewindow.h"
#include "lc_colors.h" #include "lc_colors.h"
#include "lc_library.h"
#include "lc_application.h" #include "lc_application.h"
#include "pieceinf.h" #include "pieceinf.h"
@ -55,39 +54,34 @@ lcQPropertiesDialog::lcQPropertiesDialog(QWidget *parent, void *data) :
pix.fill(QColor(options->Properties.mAmbientColor[0] * 255, options->Properties.mAmbientColor[1] * 255, options->Properties.mAmbientColor[2] * 255)); pix.fill(QColor(options->Properties.mAmbientColor[0] * 255, options->Properties.mAmbientColor[1] * 255, options->Properties.mAmbientColor[2] * 255));
ui->ambientColorButton->setIcon(pix); ui->ambientColorButton->setIcon(pix);
lcPiecesLibrary *library = lcGetPiecesLibrary();
lcArray<lcPartsListEntry>& partsUsed = options->PartsList; lcArray<lcPartsListEntry>& partsUsed = options->PartsList;
QStringList horizontalLabels, partNames; QStringList horizontalLabels, partNames;
bool *colorsUsed = new bool[gNumUserColors]; QVector<bool> ColorsUsed(gNumUserColors);
memset(colorsUsed, 0, sizeof(bool) * gNumUserColors); QMap<PieceInfo*, int> InfoRows;
int *infoRows = new int[library->mPieces.GetSize()], numInfos = 0; for (int PartIdx = 0; PartIdx < partsUsed.GetSize(); PartIdx++)
memset(infoRows, 0, sizeof(int) * library->mPieces.GetSize());
for (int partIdx = 0; partIdx < partsUsed.GetSize(); partIdx++)
{ {
colorsUsed[partsUsed[partIdx].ColorIndex] = true; ColorsUsed[partsUsed[PartIdx].ColorIndex] = true;
PieceInfo* Info = partsUsed[PartIdx].Info;
int infoIndex = library->mPieces.FindIndex(partsUsed[partIdx].Info); if (!InfoRows.value(Info))
if (!infoRows[infoIndex])
{ {
infoRows[infoIndex] = ++numInfos; InfoRows[Info] = InfoRows.size();
partNames.append(partsUsed[partIdx].Info->m_strDescription); partNames.append(Info->m_strDescription);
} }
} }
int *colorColumns = new int[gNumUserColors]; QVector<int> ColorColumns(gNumUserColors);
memset(colorColumns, 0, sizeof(int) * gNumUserColors); int NumColors = 0;
int numColors = 0;
horizontalLabels.append(tr("Part")); horizontalLabels.append(tr("Part"));
for (int colorIdx = 0; colorIdx < gNumUserColors; colorIdx++) for (int colorIdx = 0; colorIdx < gNumUserColors; colorIdx++)
{ {
if (colorsUsed[colorIdx]) if (ColorsUsed[colorIdx])
{ {
colorColumns[colorIdx] = numColors++; ColorColumns[colorIdx] = NumColors++;
horizontalLabels.append(gColorList[colorIdx].Name); horizontalLabels.append(gColorList[colorIdx].Name);
} }
} }
@ -95,8 +89,8 @@ lcQPropertiesDialog::lcQPropertiesDialog(QWidget *parent, void *data) :
horizontalLabels.append(tr("Total")); horizontalLabels.append(tr("Total"));
QTableWidget *table = ui->partsTable; QTableWidget *table = ui->partsTable;
table->setColumnCount(numColors + 2); table->setColumnCount(NumColors + 2);
table->setRowCount(numInfos + 1); table->setRowCount(InfoRows.size() + 1);
table->setHorizontalHeaderLabels(horizontalLabels); table->setHorizontalHeaderLabels(horizontalLabels);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
@ -110,49 +104,45 @@ lcQPropertiesDialog::lcQPropertiesDialog(QWidget *parent, void *data) :
table->setItem(partNames.size(), 0, new QTableWidgetItem(tr("Total"))); table->setItem(partNames.size(), 0, new QTableWidgetItem(tr("Total")));
QVector<int> infoTotals; QVector<int> InfoTotals;
QVector<int> colorTotals; QVector<int> ColorTotals;
int total = 0; int total = 0;
infoTotals.resize(numInfos); InfoTotals.resize(InfoRows.size());
colorTotals.resize(numColors); ColorTotals.resize(NumColors);
for (int partIdx = 0; partIdx < partsUsed.GetSize(); partIdx++) for (int partIdx = 0; partIdx < partsUsed.GetSize(); partIdx++)
{ {
int colorIndex = partsUsed[partIdx].ColorIndex; int colorIndex = partsUsed[partIdx].ColorIndex;
int infoIndex = library->mPieces.FindIndex(partsUsed[partIdx].Info); PieceInfo* info = partsUsed[partIdx].Info;
int count = partsUsed[partIdx].Count; int count = partsUsed[partIdx].Count;
QTableWidgetItem *item = new QTableWidgetItem(QString::number(count)); QTableWidgetItem *item = new QTableWidgetItem(QString::number(count));
item->setTextAlignment(Qt::AlignCenter); item->setTextAlignment(Qt::AlignCenter);
table->setItem(infoRows[infoIndex] - 1, colorColumns[colorIndex] + 1, item); table->setItem(InfoRows[info] - 1, ColorColumns[colorIndex] + 1, item);
infoTotals[infoRows[infoIndex] - 1] += count; InfoTotals[InfoRows[info] - 1] += count;
colorTotals[colorColumns[colorIndex]] += count; ColorTotals[ColorColumns[colorIndex]] += count;
total += count; total += count;
} }
for (int infoIdx = 0; infoIdx < numInfos; infoIdx++) for (int infoIdx = 0; infoIdx < InfoRows.size(); infoIdx++)
{ {
QTableWidgetItem *item = new QTableWidgetItem(QString::number(infoTotals[infoIdx])); QTableWidgetItem *item = new QTableWidgetItem(QString::number(InfoTotals[infoIdx]));
item->setTextAlignment(Qt::AlignCenter); item->setTextAlignment(Qt::AlignCenter);
table->setItem(infoIdx, numColors + 1, item); table->setItem(infoIdx, NumColors + 1, item);
} }
for (int colorIdx = 0; colorIdx < numColors; colorIdx++) for (int colorIdx = 0; colorIdx < NumColors; colorIdx++)
{ {
QTableWidgetItem *item = new QTableWidgetItem(QString::number(colorTotals[colorIdx])); QTableWidgetItem *item = new QTableWidgetItem(QString::number(ColorTotals[colorIdx]));
item->setTextAlignment(Qt::AlignCenter); item->setTextAlignment(Qt::AlignCenter);
table->setItem(numInfos, colorIdx + 1, item); table->setItem(InfoRows.size(), colorIdx + 1, item);
} }
QTableWidgetItem *item = new QTableWidgetItem(QString::number(total)); QTableWidgetItem *item = new QTableWidgetItem(QString::number(total));
item->setTextAlignment(Qt::AlignCenter); item->setTextAlignment(Qt::AlignCenter);
table->setItem(numInfos, numColors + 1, item); table->setItem(InfoRows.size(), NumColors + 1, item);
delete[] colorColumns;
delete[] colorsUsed;
delete[] infoRows;
} }
lcQPropertiesDialog::~lcQPropertiesDialog() lcQPropertiesDialog::~lcQPropertiesDialog()