All Classes Namespaces Functions Variables Enumerations Properties Pages
colorinspector.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2012-2020 Matthew Chiawen Chang
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 */
16 #include "colorinspector.h"
17 #include "ui_colorinspector.h"
18 
19 #include <QSettings>
20 #include <QDebug>
21 
22 #include "colorslider.h"
23 #include "pencildef.h"
24 #include "editor.h"
25 #include "colormanager.h"
26 #include "util.h"
27 
28 
29 ColorInspector::ColorInspector(QWidget *parent) :
30  BaseDockWidget(parent)
31 {
32  QWidget* innerWidget = new QWidget;
33  setWindowTitle(tr("Color Inspector", "Window title of color inspector"));
34 
35  ui = new Ui::ColorInspector;
36  ui->setupUi(innerWidget);
37  setWidget(innerWidget);
38 }
39 
40 ColorInspector::~ColorInspector()
41 {
42  delete ui;
43 }
44 
45 void ColorInspector::initUI()
46 {
47  mCurrentColor = editor()->color()->frontColor();
48 
49  QSettings settings(PENCIL2D, PENCIL2D);
50  isRgbColors = settings.value("isRgb").toBool();
51 
52  if (isRgbColors) {
53  ui->colorSpecTabWidget->setCurrentWidget(ui->rgbTab);
54  } else {
55  ui->colorSpecTabWidget->setCurrentWidget(ui->hsvTab);
56  }
57  onColorSpecChanged();
58 
59  ui->redSlider->init(ColorSlider::ColorSpecType::RGB, ColorSlider::ColorType::RED, mCurrentColor, 0.0, 255.0);
60  ui->greenSlider->init(ColorSlider::ColorSpecType::RGB, ColorSlider::ColorType::GREEN, mCurrentColor, 0.0, 255.0);
61  ui->blueSlider->init(ColorSlider::ColorSpecType::RGB, ColorSlider::ColorType::BLUE, mCurrentColor, 0.0, 255.0);
62  ui->rgbAlphaSlider->init(ColorSlider::ColorSpecType::RGB, ColorSlider::ColorType::ALPHA, mCurrentColor, 0.0, 255.0);
63 
64  ui->hueSlider->init(ColorSlider::ColorSpecType::HSV, ColorSlider::ColorType::HUE, mCurrentColor, 0.0, 359.0);
65  ui->saturationSlider->init(ColorSlider::ColorSpecType::HSV, ColorSlider::ColorType::SAT, mCurrentColor, 0.0, 255.0);
66  ui->valueSlider->init(ColorSlider::ColorSpecType::HSV, ColorSlider::ColorType::VAL, mCurrentColor, 0.0, 255.0);
67  ui->hsvAlphaSlider->init(ColorSlider::ColorSpecType::HSV, ColorSlider::ColorType::ALPHA, mCurrentColor, 0.0, 255.0);
68 
69  QPalette p1 = ui->colorWrapper->palette();
70  p1.setBrush(QPalette::Background, QBrush(QImage(":/background/checkerboard.png")));
71  ui->colorWrapper->setPalette(p1);
72 
73  QPalette p2 = ui->color->palette();
74  p2.setColor(QPalette::Background, mCurrentColor);
75  ui->color->setPalette(p2);
76 
77  connect(ui->colorSpecTabWidget, &QTabWidget::currentChanged, this, &ColorInspector::onColorSpecChanged);
78 
79  auto onColorChangedSlider = static_cast<void(ColorInspector::*)(const QColor&)>(&ColorInspector::onColorChanged);
80  connect(ui->redSlider, &ColorSlider::valueChanged, this, onColorChangedSlider);
81  connect(ui->greenSlider, &ColorSlider::valueChanged, this, onColorChangedSlider);
82  connect(ui->blueSlider, &ColorSlider::valueChanged, this, onColorChangedSlider);
83  connect(ui->rgbAlphaSlider, &ColorSlider::valueChanged, this, onColorChangedSlider);
84  connect(ui->hueSlider, &ColorSlider::valueChanged, this, onColorChangedSlider);
85  connect(ui->saturationSlider, &ColorSlider::valueChanged, this, onColorChangedSlider);
86  connect(ui->valueSlider, &ColorSlider::valueChanged, this, onColorChangedSlider);
87  connect(ui->hsvAlphaSlider, &ColorSlider::valueChanged, this, onColorChangedSlider);
88 
89  auto spinBoxChanged = static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged);
90  auto onColorChangedSpinBox = static_cast<void(ColorInspector::*)()>(&ColorInspector::onColorChanged);
91  connect(ui->redSpinBox, spinBoxChanged, this, onColorChangedSpinBox);
92  clearFocusOnFinished(ui->redSpinBox);
93  connect(ui->greenSpinBox, spinBoxChanged, this, onColorChangedSpinBox);
94  clearFocusOnFinished(ui->greenSpinBox);
95  connect(ui->blueSpinBox, spinBoxChanged, this, onColorChangedSpinBox);
96  clearFocusOnFinished(ui->blueSpinBox);
97  connect(ui->rgbAlphaSpinBox, spinBoxChanged, this, onColorChangedSpinBox);
98  clearFocusOnFinished(ui->rgbAlphaSpinBox);
99  connect(ui->hueSpinBox, spinBoxChanged, this, onColorChangedSpinBox);
100  clearFocusOnFinished(ui->hueSpinBox);
101  connect(ui->saturationSpinBox, spinBoxChanged, this, onColorChangedSpinBox);
102  clearFocusOnFinished(ui->saturationSpinBox);
103  connect(ui->valueSpinBox, spinBoxChanged, this, onColorChangedSpinBox);
104  clearFocusOnFinished(ui->valueSpinBox);
105  connect(ui->hsvAlphaSpinBox, spinBoxChanged, this, onColorChangedSpinBox);
106  clearFocusOnFinished(ui->hsvAlphaSpinBox);
107 
108  connect(editor(), &Editor::objectLoaded, this, &ColorInspector::updateUI);
109 }
110 
111 void ColorInspector::updateUI()
112 {
113  QColor newColor = editor()->color()->frontColor();
114  setColor(newColor);
115 }
116 
117 void ColorInspector::setColor(QColor newColor)
118 {
119  // compare under the same color spec
120  newColor = (isRgbColors) ? newColor.toRgb() : newColor.toHsv();
121 
122  if (newColor == mCurrentColor)
123  {
124  return;
125  }
126 
127  mCurrentColor = newColor;
128 
129  updateControls();
130 }
131 
132 QColor ColorInspector::color()
133 {
134  return mCurrentColor;
135 }
136 
137 void ColorInspector::updateControls()
138 {
139  // this is a UI update function, never emit any signals
140  // grab the color from color manager, and then update itself, that's it.
141 
142  QSignalBlocker b1(ui->redSlider);
143  QSignalBlocker b2(ui->greenSlider);
144  QSignalBlocker b3(ui->blueSlider);
145  QSignalBlocker b4(ui->rgbAlphaSlider);
146 
147  ui->redSlider->setRgb(mCurrentColor);
148  ui->greenSlider->setRgb(mCurrentColor);
149  ui->blueSlider->setRgb(mCurrentColor);
150  ui->rgbAlphaSlider->setRgb(mCurrentColor);
151 
152  QSignalBlocker b5(ui->redSpinBox);
153  QSignalBlocker b6(ui->greenSpinBox);
154  QSignalBlocker b7(ui->blueSpinBox);
155  QSignalBlocker b8(ui->rgbAlphaSpinBox);
156 
157  ui->redSpinBox->setValue(mCurrentColor.red());
158  ui->greenSpinBox->setValue(mCurrentColor.green());
159  ui->blueSpinBox->setValue(mCurrentColor.blue());
160  ui->rgbAlphaSpinBox->setValue(mCurrentColor.alpha());
161 
162  QSignalBlocker b9(ui->hueSlider);
163  QSignalBlocker b10(ui->saturationSlider);
164  QSignalBlocker b11(ui->valueSlider);
165  QSignalBlocker b12(ui->hsvAlphaSlider);
166 
167  ui->hueSlider->setHsv(mCurrentColor);
168  ui->saturationSlider->setHsv(mCurrentColor);
169  ui->valueSlider->setHsv(mCurrentColor);
170  ui->hsvAlphaSlider->setHsv(mCurrentColor);
171 
172  QSignalBlocker b13(ui->hueSpinBox);
173  QSignalBlocker b14(ui->saturationSpinBox);
174  QSignalBlocker b15(ui->valueSpinBox);
175  QSignalBlocker b16(ui->hsvAlphaSpinBox);
176 
177  ui->hueSpinBox->setValue(mCurrentColor.hue());
178  ui->saturationSpinBox->setValue(qRound(mCurrentColor.saturation() / 2.55));
179  ui->valueSpinBox->setValue(qRound(mCurrentColor.value() / 2.55));
180  ui->hsvAlphaSpinBox->setValue(qRound(mCurrentColor.alpha() / 2.55));
181 
182  QPalette p = ui->color->palette();
183  p.setColor(QPalette::Background, mCurrentColor);
184  ui->color->setPalette(p);
185 
186  update();
187 }
188 
189 void ColorInspector::onColorSpecChanged()
190 {
191  // assume hsv if not checked
192  isRgbColors = ui->colorSpecTabWidget->currentWidget() == ui->rgbTab;
193 
194  QSettings settings(PENCIL2D, PENCIL2D);
195  settings.setValue("isRgb", isRgbColors);
196 
197  if (isRgbColors)
198  {
199  mCurrentColor = mCurrentColor.toRgb();
200  }
201  else
202  {
203  mCurrentColor = mCurrentColor.toHsv();
204  }
205 
206  updateControls();
207 
208  emit colorSpecChanged(isRgbColors);
209 }
210 
211 void ColorInspector::onColorChanged()
212 {
213  if (isRgbColors) {
214  mCurrentColor.setRgb(
215  ui->redSpinBox->value(),
216  ui->greenSpinBox->value(),
217  ui->blueSpinBox->value(),
218  ui->rgbAlphaSpinBox->value());
219  } else {
220  mCurrentColor.setHsv(
221  ui->hueSpinBox->value(),
222  static_cast<int>(ui->saturationSpinBox->value() * 2.55),
223  static_cast<int>(ui->valueSpinBox->value() * 2.55),
224  static_cast<int>(ui->hsvAlphaSpinBox->value() * 2.55));
225  }
226 
227  updateControls();
228 
229  emit colorChanged(mCurrentColor);
230 }
231 
232 void ColorInspector::onColorChanged(const QColor& color)
233 {
234  mCurrentColor = color;
235 
236  updateControls();
237 
238  emit colorChanged(color);
239 }
void setBrush(QPalette::ColorRole role, const QBrush &brush)
void setColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QColor &color)
int value() const const
void setRgb(int r, int g, int b, int a)
int hue() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
void update()
QColor toHsv() const const
void valueChanged(int i)
QColor toRgb() const const
int red() const const
void currentChanged(int index)
int alpha() const const
int green() const const
QColor frontColor(bool useIndexedColor=true)
frontColor
void setWidget(QWidget *widget)
int blue() const const
void setWindowTitle(const QString &)
QWidget(QWidget *parent, Qt::WindowFlags f)
void setHsv(int h, int s, int v, int a)
int saturation() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)