src/widgets/colorselectdialog.cpp

changeset 205
1a4342d80de7
parent 178
a23024fc98e0
child 264
76a025db4948
--- a/src/widgets/colorselectdialog.cpp	Wed Jun 08 19:33:00 2022 +0300
+++ b/src/widgets/colorselectdialog.cpp	Wed Jun 08 20:41:21 2022 +0300
@@ -5,7 +5,7 @@
 #include "ui_colorselectdialog.h"
 #include "uiutilities.h"
 
-ColorSelectDialog::ColorSelectDialog(const ldraw::ColorTable& colorTable, QWidget *parent) :
+ColorSelectDialog::ColorSelectDialog(const ColorTable& colorTable, QWidget *parent) :
 	QDialog{parent},
 	ui{*new Ui_ColorSelectDialog},
 	colorTable{colorTable}
@@ -39,12 +39,13 @@
 		iterator != std::end(this->colorTable);
 		++iterator
 	) {
-		const qint32 index = iterator->first;
-		const ldraw::ColorDefinition& colordef = iterator->second;
+		const qint32 index = iterator->first.index;
+		const ColorDefinition& colordef = iterator->second;
 		QPushButton* const button = new QPushButton{QString::number(index), this};
 		button->setMinimumSize({40, 40});
 		button->setToolTip(colordef.displayName);
-		uiutilities::colorizeWidget(button, ldraw::colorFace({index}, colorTable));
+		const QColor face = colorFace({index}, colorTable).value_or(QColor{});
+		uiutilities::colorizeWidget(button, face);
 		button->setProperty("_colorIndex", index);
 		button->setCheckable(true);
 		connect(button, &QAbstractButton::clicked, this, &ColorSelectDialog::handleButtonClick);
@@ -90,8 +91,10 @@
 
 void ColorSelectDialog::updateSelectedColorTexts()
 {
-	this->ui.selectedColorName->setText(ldraw::colorDisplayName(this->selectedColor, this->colorTable));
-	uiutilities::colorizeWidget(this->ui.selectedColorName, ldraw::colorFace(this->selectedColor, colorTable));
+	const QString displayName = colorDisplayName(this->selectedColor, this->colorTable).value_or(QStringLiteral("???"));
+	this->ui.selectedColorName->setText(displayName);
+	const QColor face = colorFace(this->selectedColor, colorTable).value_or(QColor{});
+	uiutilities::colorizeWidget(this->ui.selectedColorName, face);
 	this->ui.colorIndex->setValue(this->selectedColor.index);
 	for (QPushButton* button : this->buttons)
 	{
@@ -117,10 +120,13 @@
 void ColorSelectDialog::chooseDirectColor()
 {
 	QColorDialog dialog;
-	dialog.setCurrentColor(ldraw::colorFace(this->selectedColor, this->colorTable));
+	const std::optional<QColor> face = colorFace(this->selectedColor, this->colorTable);
+	if (face.has_value()) {
+		dialog.setCurrentColor(*face);
+	}
 	if (dialog.exec())
 	{
-		this->setCurrentColor(ldraw::directColor(dialog.selectedColor()));
+		this->setCurrentColor(directColor(dialog.selectedColor()));
 	}
 }
 
@@ -133,12 +139,12 @@
 	}
 	else
 	{
-		const ldraw::ColorDefinition& colordef = this->colorTable[color];
-		return colordef.displayName.contains(filterText, Qt::CaseInsensitive);
+		const std::optional<QString> name = colorDisplayName(color, colorTable);
+		return name.value_or(QString{}).contains(filterText, Qt::CaseInsensitive);
 	}
 }
 
-void ColorSelectDialog::setCurrentColor(ldraw::Color color)
+void ColorSelectDialog::setCurrentColor(ColorIndex color)
 {
 	this->selectedColor = color;
 	this->updateSelectedColorTexts();

mercurial