src/dialogs/colorselector.cpp

changeset 1173
6cd85b28f43b
parent 1072
9ce9496427f2
child 1174
91696a2e022c
equal deleted inserted replaced
1172:3defab8cfd93 1173:6cd85b28f43b
64 button->setStyleSheet (format ("background-color: rgba(%1, %2, %3, %4); color: %5", 64 button->setStyleSheet (format ("background-color: rgba(%1, %2, %3, %4); color: %5",
65 color.red(), color.green(), color.blue(), color.alpha(), color2name)); 65 color.red(), color.green(), color.blue(), color.alpha(), color2name));
66 button->setCheckable (true); 66 button->setCheckable (true);
67 button->setText (QString::number (ldcolor.index())); 67 button->setText (QString::number (ldcolor.index()));
68 button->setToolTip (format ("%1: %2", ldcolor.index(), ldcolor.name())); 68 button->setToolTip (format ("%1: %2", ldcolor.index(), ldcolor.name()));
69 m_buttons[ldcolor.index()] = button; 69 m_buttons[ldcolor] = button;
70 m_buttonsReversed[button] = ldcolor.index(); 70 m_buttonsReversed[button] = ldcolor;
71 connect (button, SIGNAL (clicked(bool)), this, SLOT (colorButtonClicked())); 71 connect (button, SIGNAL (clicked(bool)), this, SLOT (colorButtonClicked()));
72 72
73 if (ldcolor == selection()) 73 if (ldcolor == selection())
74 button->setChecked (true); 74 button->setChecked (true);
75 } 75 }
102 delete &ui; 102 delete &ui;
103 } 103 }
104 104
105 void ColorSelector::colorButtonClicked() 105 void ColorSelector::colorButtonClicked()
106 { 106 {
107 QPushButton* button = qobject_cast<QPushButton*> (sender()); 107 QPushButton* button = qobject_cast<QPushButton*>(sender());
108 auto it = m_buttonsReversed.find (button); 108 LDColor color = m_buttonsReversed.value(button, LDColor::nullColor);
109 LDColor color;
110 109
111 if (Q_UNLIKELY (button == nullptr or it == m_buttonsReversed.end() 110 if (color.isValid())
112 or not (color = *it).isValid()))
113 { 111 {
114 print ("colorButtonClicked() called with invalid sender"); 112 // Uncheck the button we previously had pressed.
115 return; 113 if (m_selection.isValid())
114 {
115 QPushButton* button = m_buttons.value(m_selection);
116
117 if (button)
118 button->setChecked(false);
119 }
120
121 // Select the new color.
122 m_selection = color;
123 button->setChecked (true);
124 drawColorInfo();
116 } 125 }
117
118 if (selection().isValid())
119 {
120 auto button = m_buttons.find (selection().index());
121
122 if (button != m_buttons.end())
123 (*button)->setChecked (false);
124 }
125
126 m_selection = color;
127 button->setChecked (true);
128 drawColorInfo();
129 } 126 }
130 127
131 void ColorSelector::drawColorInfo() 128 void ColorSelector::drawColorInfo()
132 { 129 {
133 if (not selection().isValid()) 130 if (not selection().isValid())

mercurial