src/dialogs/colorselector.cpp

changeset 1173
6cd85b28f43b
parent 1072
9ce9496427f2
child 1174
91696a2e022c
--- a/src/dialogs/colorselector.cpp	Thu Feb 23 23:36:59 2017 +0200
+++ b/src/dialogs/colorselector.cpp	Sat Feb 25 14:30:10 2017 +0200
@@ -66,8 +66,8 @@
 			button->setCheckable (true);
 			button->setText (QString::number (ldcolor.index()));
 			button->setToolTip (format ("%1: %2", ldcolor.index(), ldcolor.name()));
-			m_buttons[ldcolor.index()] = button;
-			m_buttonsReversed[button] = ldcolor.index();
+			m_buttons[ldcolor] = button;
+			m_buttonsReversed[button] = ldcolor;
 			connect (button, SIGNAL (clicked(bool)), this, SLOT (colorButtonClicked()));
 
 			if (ldcolor == selection())
@@ -104,28 +104,25 @@
 
 void ColorSelector::colorButtonClicked()
 {
-	QPushButton* button = qobject_cast<QPushButton*> (sender());
-	auto it = m_buttonsReversed.find (button);
-	LDColor color;
+	QPushButton* button = qobject_cast<QPushButton*>(sender());
+	LDColor color = m_buttonsReversed.value(button, LDColor::nullColor);
 
-	if (Q_UNLIKELY (button == nullptr or it == m_buttonsReversed.end()
-		or not (color = *it).isValid()))
+	if (color.isValid())
 	{
-		print ("colorButtonClicked() called with invalid sender");
-		return;
-	}
+		// Uncheck the button we previously had pressed.
+		if (m_selection.isValid())
+		{
+			QPushButton* button = m_buttons.value(m_selection);
 
-	if (selection().isValid())
-	{
-		auto button = m_buttons.find (selection().index());
+			if (button)
+				button->setChecked(false);
+		}
 
-		if (button != m_buttons.end())
-			(*button)->setChecked (false);
+		// Select the new color.
+		m_selection = color;
+		button->setChecked (true);
+		drawColorInfo();
 	}
-
-	m_selection = color;
-	button->setChecked (true);
-	drawColorInfo();
 }
 
 void ColorSelector::drawColorInfo()

mercurial