src/colorSelector.cc

changeset 946
9cbd658b63f9
parent 878
945e44575b3e
child 947
edc8fc0f37f2
equal deleted inserted replaced
945:c310073e4f22 946:9cbd658b63f9
30 #include "colors.h" 30 #include "colors.h"
31 #include "configuration.h" 31 #include "configuration.h"
32 #include "miscallenous.h" 32 #include "miscallenous.h"
33 #include "ui_colorsel.h" 33 #include "ui_colorsel.h"
34 34
35 static const int g_numColumns = 16; 35 enum { NUM_COLUMNS = 16 };
36 36
37 EXTERN_CFGENTRY (String, MainColor) 37 EXTERN_CFGENTRY (String, MainColor)
38 EXTERN_CFGENTRY (Float, MainColorAlpha) 38 EXTERN_CFGENTRY (Float, MainColorAlpha)
39 39
40 // ============================================================================= 40 // =============================================================================
48 setSelection (defaultvalue); 48 setSelection (defaultvalue);
49 49
50 QGridLayout* layout = new QGridLayout (this); 50 QGridLayout* layout = new QGridLayout (this);
51 51
52 // Spawn color selector buttons 52 // Spawn color selector buttons
53 for (int i = 0; i < CountLDConfigColors(); ++i) 53 for (LDColor ldcolor; ldcolor.isLDConfigColor(); ++ldcolor)
54 { 54 {
55 LDColor ldcolor = LDColor::fromIndex (i);
56 QPushButton* button = new QPushButton (this); 55 QPushButton* button = new QPushButton (this);
57 button->setMinimumSize (QSize (32, 32)); 56 button->setMinimumSize (QSize (32, 32));
58 button->setMaximumSize (button->minimumSize()); 57 button->setMaximumSize (button->minimumSize());
59 58
60 if (ldcolor != null) 59 if (ldcolor.isValid ())
61 { 60 {
62 QString colorname;
63 QColor color (ldcolor.faceColor()); 61 QColor color (ldcolor.faceColor());
64 62
65 if (i == MainColorIndex) 63 if (ldcolor == MainColor)
66 { 64 {
67 color = QColor (cfg::MainColor); 65 color = QColor (cfg::MainColor);
68 color.setAlphaF (cfg::MainColorAlpha); 66 color.setAlphaF (cfg::MainColorAlpha);
69 } 67 }
70 68
73 button->setStyleSheet (format ("background-color: rgba(%1, %2, %3, %4); color: %5", 71 button->setStyleSheet (format ("background-color: rgba(%1, %2, %3, %4); color: %5",
74 color.red(), color.green(), color.blue(), color.alpha(), color2name)); 72 color.red(), color.green(), color.blue(), color.alpha(), color2name));
75 button->setCheckable (true); 73 button->setCheckable (true);
76 button->setText (QString::number (ldcolor.index())); 74 button->setText (QString::number (ldcolor.index()));
77 button->setToolTip (format ("%1: %2", ldcolor.index(), ldcolor.name())); 75 button->setToolTip (format ("%1: %2", ldcolor.index(), ldcolor.name()));
78 m_buttons[i] = button; 76 m_buttons[ldcolor.index()] = button;
79 m_buttonsReversed[button] = i; 77 m_buttonsReversed[button] = ldcolor.index();
80 connect (button, SIGNAL (clicked(bool)), this, SLOT (colorButtonClicked())); 78 connect (button, SIGNAL (clicked(bool)), this, SLOT (colorButtonClicked()));
81 79
82 if (ldcolor == selection()) 80 if (ldcolor == selection())
83 button->setChecked (true); 81 button->setChecked (true);
84 } 82 }
85 else 83 else
86 { 84 {
87 button->setEnabled (false); 85 button->setEnabled (false);
88 } 86 }
89 87
90 layout->addWidget (button, i / g_numColumns, i % g_numColumns); 88 layout->addWidget (button, ldcolor.index() / NUM_COLUMNS, ldcolor.index() % NUM_COLUMNS);
91 } 89 }
92 90
93 QWidget* widget = new QWidget(); 91 QWidget* widget = new QWidget();
94 widget->setLayout (layout); 92 widget->setLayout (layout);
95 ui->definedColors->setWidget (widget); 93 ui->definedColors->setWidget (widget);
120 QPushButton* button = qobject_cast<QPushButton*> (sender()); 118 QPushButton* button = qobject_cast<QPushButton*> (sender());
121 auto it = m_buttonsReversed.find (button); 119 auto it = m_buttonsReversed.find (button);
122 LDColor color; 120 LDColor color;
123 121
124 if (Q_UNLIKELY (button == null or it == m_buttonsReversed.end() 122 if (Q_UNLIKELY (button == null or it == m_buttonsReversed.end()
125 or (color = LDColor::fromIndex (*it)) == null)) 123 or not (color = *it).isValid()))
126 { 124 {
127 print ("colorButtonClicked() called with invalid sender"); 125 print ("colorButtonClicked() called with invalid sender");
128 return; 126 return;
129 } 127 }
130 128
131 if (selection() != null) 129 if (selection().isValid())
132 { 130 {
133 auto it2 = m_buttons.find (selection().index()); 131 auto button = m_buttons.find (selection().index());
134 132
135 if (it2 != m_buttons.end()) 133 if (button != m_buttons.end())
136 (*it2)->setChecked (false); 134 (*button)->setChecked (false);
137 } 135 }
138 136
139 setSelection (color); 137 setSelection (color);
140 button->setChecked (true); 138 button->setChecked (true);
141 drawColorInfo(); 139 drawColorInfo();
143 141
144 // ============================================================================= 142 // =============================================================================
145 // 143 //
146 void ColorSelector::drawColorInfo() 144 void ColorSelector::drawColorInfo()
147 { 145 {
148 if (selection() == null) 146 if (not selection().isValid())
149 { 147 {
150 ui->colorLabel->setText ("---"); 148 ui->colorLabel->setText ("---");
151 ui->iconLabel->setPixmap (QPixmap()); 149 ui->iconLabel->setPixmap (QPixmap());
152 ui->transparentDirectColor->setChecked (false); 150 ui->transparentDirectColor->setChecked (false);
153 return; 151 return;
166 #endif 164 #endif
167 } 165 }
168 166
169 // ============================================================================= 167 // =============================================================================
170 // 168 //
171 void ColorSelector::selectDirectColor (QColor col) 169 void ColorSelector::selectDirectColor (QColor color)
172 { 170 {
173 int32 idx = (ui->transparentDirectColor->isChecked() ? 0x03000000 : 0x02000000); 171 qint32 colorIndex = (ui->transparentDirectColor->isChecked() ? 0x03000000 : 0x02000000);
174 idx |= (col.red() << 16) | (col.green() << 8) | (col.blue()); 172 colorIndex |= (color.red() << 16) | (color.green() << 8) | (color.blue());
175 setSelection (LDColor::fromIndex (idx)); 173 setSelection (colorIndex);
176 drawColorInfo(); 174 drawColorInfo();
177 } 175 }
178 176
179 // ============================================================================= 177 // =============================================================================
180 // 178 //
181 void ColorSelector::chooseDirectColor() 179 void ColorSelector::chooseDirectColor()
182 { 180 {
183 QColor defcolor = selection() != null ? selection().faceColor() : Qt::white; 181 QColor defcolor = selection() != -1 ? selection().faceColor() : Qt::white;
184 QColor newcolor = QColorDialog::getColor (defcolor); 182 QColor newcolor = QColorDialog::getColor (defcolor);
185 183
186 if (not newcolor.isValid()) 184 if (not newcolor.isValid())
187 return; // canceled 185 return; // canceled
188 186
191 189
192 // ============================================================================= 190 // =============================================================================
193 // 191 //
194 void ColorSelector::transparentCheckboxClicked() 192 void ColorSelector::transparentCheckboxClicked()
195 { 193 {
196 if (selection() == null or not selection().isDirect()) 194 if (selection().isDirect())
197 return; 195 selectDirectColor (selection().faceColor());
198
199 selectDirectColor (selection().faceColor());
200 } 196 }
201 197
202 // ============================================================================= 198 // =============================================================================
203 // 199 //
204 bool ColorSelector::selectColor (LDColor& val, LDColor defval, QWidget* parent) 200 bool ColorSelector::selectColor (LDColor& val, LDColor defval, QWidget* parent)
205 { 201 {
206 ColorSelector dlg (defval, parent); 202 ColorSelector dlg (defval, parent);
207 203
208 if (dlg.exec() and dlg.selection() != null) 204 if (dlg.exec() and dlg.selection().isValid())
209 { 205 {
210 val = dlg.selection(); 206 val = dlg.selection();
211 return true; 207 return true;
212 } 208 }
213 209

mercurial