src/dialogs/colorselector.cpp

changeset 1217
314e12e23c3a
parent 1215
77a0270352a3
child 1222
34def2630300
equal deleted inserted replaced
1216:12f9ea615cbc 1217:314e12e23c3a
29 #include "colorselector.h" 29 #include "colorselector.h"
30 #include "ui_colorselector.h" 30 #include "ui_colorselector.h"
31 31
32 enum { NUM_COLUMNS = 16 }; 32 enum { NUM_COLUMNS = 16 };
33 33
34 ColorSelector::ColorSelector (QWidget* parent, LDColor defaultvalue) : 34 ColorSelector::ColorSelector(QWidget* parent, LDColor defaultvalue) :
35 QDialog (parent), 35 QDialog(parent),
36 HierarchyElement (parent), 36 HierarchyElement(parent),
37 ui (*new Ui_ColorSelUi), 37 ui(*new Ui_ColorSelUi),
38 m_selection (defaultvalue) 38 m_selection(defaultvalue)
39 { 39 {
40 m_firstResize = true; 40 m_firstResize = true;
41 ui.setupUi (this); 41 ui.setupUi(this);
42 42
43 QGridLayout* layout = new QGridLayout (this); 43 QGridLayout* layout = new QGridLayout(this);
44 44
45 // Spawn color selector buttons 45 // Spawn color selector buttons
46 for (LDColor ldcolor; ldcolor.isLDConfigColor(); ++ldcolor) 46 for (LDColor ldcolor; ldcolor.isLDConfigColor(); ++ldcolor)
47 { 47 {
48 QPushButton* button = new QPushButton (this); 48 QPushButton* button = new QPushButton(this);
49 button->setMinimumSize (QSize (32, 32)); 49 button->setMinimumSize(QSize(32, 32));
50 button->setMaximumSize (button->minimumSize()); 50 button->setMaximumSize(button->minimumSize());
51 51
52 if (ldcolor.isValid ()) 52 if (ldcolor.isValid())
53 { 53 {
54 QColor color (ldcolor.faceColor()); 54 QColor color(ldcolor.faceColor());
55 55
56 if (ldcolor == MainColor) 56 if (ldcolor == MainColor)
57 { 57 {
58 color = QColor (config.mainColor()); 58 color = QColor(config.mainColor());
59 color.setAlphaF (config.mainColorAlpha()); 59 color.setAlphaF(config.mainColorAlpha());
60 } 60 }
61 61
62 QString color2name (luma (color) < 80 ? "white" : "black"); 62 QString color2name(luma(color) < 80 ? "white" : "black");
63 button->setAutoFillBackground (true); 63 button->setAutoFillBackground(true);
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.index()] = button;
70 m_buttonsReversed[button] = ldcolor.index(); 70 m_buttonsReversed[button] = ldcolor.index();
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 }
76 else 76 else
77 { 77 {
78 button->setEnabled (false); 78 button->setEnabled(false);
79 } 79 }
80 80
81 layout->addWidget (button, ldcolor.index() / NUM_COLUMNS, ldcolor.index() % NUM_COLUMNS); 81 layout->addWidget(button, ldcolor.index() / NUM_COLUMNS, ldcolor.index() % NUM_COLUMNS);
82 } 82 }
83 83
84 QWidget* widget = new QWidget(); 84 QWidget* widget = new QWidget();
85 widget->setLayout (layout); 85 widget->setLayout(layout);
86 ui.definedColors->setWidget (widget); 86 ui.definedColors->setWidget(widget);
87 connect (ui.directColor, SIGNAL (clicked (bool)), this, SLOT (chooseDirectColor())); 87 connect(ui.directColor, SIGNAL(clicked(bool)), this, SLOT(chooseDirectColor()));
88 88
89 ui.definedColors->setMinimumWidth (ui.definedColors->widget()->width() + 16); 89 ui.definedColors->setMinimumWidth(ui.definedColors->widget()->width() + 16);
90 90
91 #ifdef TRANSPARENT_DIRECT_COLORS 91 #ifdef TRANSPARENT_DIRECT_COLORS
92 connect (ui.transparentDirectColor, SIGNAL (clicked (bool)), this, SLOT (transparentCheckboxClicked())); 92 connect(ui.transparentDirectColor, SIGNAL(clicked(bool)), this, SLOT(transparentCheckboxClicked()));
93 #else 93 #else
94 ui.transparentDirectColor->hide(); 94 ui.transparentDirectColor->hide();
95 #endif 95 #endif
96 96
97 drawColorInfo(); 97 drawColorInfo();
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 auto it = m_buttonsReversed.find(button);
109 LDColor color; 109 LDColor color;
110 110
111 if (Q_UNLIKELY (button == nullptr or it == m_buttonsReversed.end() 111 if (Q_UNLIKELY(button == nullptr or it == m_buttonsReversed.end()
112 or not (color = *it).isValid())) 112 or not(color = *it).isValid()))
113 { 113 {
114 print ("colorButtonClicked() called with invalid sender"); 114 print("colorButtonClicked() called with invalid sender");
115 return; 115 return;
116 } 116 }
117 117
118 if (selection().isValid()) 118 if (selection().isValid())
119 { 119 {
120 auto button = m_buttons.find (selection().index()); 120 auto button = m_buttons.find(selection().index());
121 121
122 if (button != m_buttons.end()) 122 if (button != m_buttons.end())
123 (*button)->setChecked (false); 123 (*button)->setChecked(false);
124 } 124 }
125 125
126 m_selection = color; 126 m_selection = color;
127 button->setChecked (true); 127 button->setChecked(true);
128 drawColorInfo(); 128 drawColorInfo();
129 } 129 }
130 130
131 void ColorSelector::drawColorInfo() 131 void ColorSelector::drawColorInfo()
132 { 132 {
133 if (not selection().isValid()) 133 if (not selection().isValid())
134 { 134 {
135 ui.colorLabel->setText ("---"); 135 ui.colorLabel->setText("---");
136 ui.iconLabel->setPixmap (QPixmap()); 136 ui.iconLabel->setPixmap(QPixmap());
137 ui.transparentDirectColor->setChecked (false); 137 ui.transparentDirectColor->setChecked(false);
138 return; 138 return;
139 } 139 }
140 140
141 ui.colorLabel->setText (format ("%1 - %2", selection().indexString(), 141 ui.colorLabel->setText(format("%1 - %2", selection().indexString(),
142 (selection().isDirect() ? "<direct color>" : selection().name()))); 142 (selection().isDirect() ? "<direct color>" : selection().name())));
143 ui.iconLabel->setPixmap (guiUtilities()->makeColorIcon (selection(), 16).pixmap (16, 16)); 143 ui.iconLabel->setPixmap(guiUtilities()->makeColorIcon(selection(), 16).pixmap(16, 16));
144 144
145 #ifdef TRANSPARENT_DIRECT_COLORS 145 #ifdef TRANSPARENT_DIRECT_COLORS
146 ui.transparentDirectColor->setEnabled (selection().isDirect()); 146 ui.transparentDirectColor->setEnabled(selection().isDirect());
147 ui.transparentDirectColor->setChecked (selection().isDirect() and selection().faceColor().alphaF() < 1.0); 147 ui.transparentDirectColor->setChecked(selection().isDirect() and selection().faceColor().alphaF() < 1.0);
148 #else 148 #else
149 ui.transparentDirectColor->setChecked (false); 149 ui.transparentDirectColor->setChecked(false);
150 ui.transparentDirectColor->setEnabled (false); 150 ui.transparentDirectColor->setEnabled(false);
151 #endif 151 #endif
152 } 152 }
153 153
154 void ColorSelector::selectDirectColor (QColor color) 154 void ColorSelector::selectDirectColor(QColor color)
155 { 155 {
156 qint32 colorIndex = (ui.transparentDirectColor->isChecked() ? 0x03000000 : 0x02000000); 156 qint32 colorIndex = (ui.transparentDirectColor->isChecked() ? 0x03000000 : 0x02000000);
157 colorIndex |= (color.red() << 16) | (color.green() << 8) | (color.blue()); 157 colorIndex |= (color.red() << 16) |(color.green() << 8) |(color.blue());
158 m_selection = colorIndex; 158 m_selection = colorIndex;
159 drawColorInfo(); 159 drawColorInfo();
160 } 160 }
161 161
162 void ColorSelector::chooseDirectColor() 162 void ColorSelector::chooseDirectColor()
163 { 163 {
164 QColor defcolor = selection() != -1 ? selection().faceColor() : Qt::white; 164 QColor defcolor = selection() != -1 ? selection().faceColor() : Qt::white;
165 QColor newcolor = QColorDialog::getColor (defcolor); 165 QColor newcolor = QColorDialog::getColor(defcolor);
166 166
167 if (not newcolor.isValid()) 167 if (not newcolor.isValid())
168 return; // canceled 168 return; // canceled
169 169
170 selectDirectColor (newcolor); 170 selectDirectColor(newcolor);
171 } 171 }
172 172
173 void ColorSelector::transparentCheckboxClicked() 173 void ColorSelector::transparentCheckboxClicked()
174 { 174 {
175 if (selection().isDirect()) 175 if (selection().isDirect())
176 selectDirectColor (selection().faceColor()); 176 selectDirectColor(selection().faceColor());
177 } 177 }
178 178
179 bool ColorSelector::selectColor (QWidget* parent, LDColor& val, LDColor defaultvalue) 179 bool ColorSelector::selectColor(QWidget* parent, LDColor& val, LDColor defaultvalue)
180 { 180 {
181 ColorSelector dlg (parent, defaultvalue); 181 ColorSelector dlg(parent, defaultvalue);
182 182
183 if (dlg.exec() and dlg.selection().isValid()) 183 if (dlg.exec() and dlg.selection().isValid())
184 { 184 {
185 val = dlg.selection(); 185 val = dlg.selection();
186 return true; 186 return true;

mercurial