Debian Codegraph @

plugins/dockers/widegamutcolorselector/WGColorSelectorSettings.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2020 Mathias Wein <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7 #include "WGColorSelectorSettings.h"
8 #include "ui_WdgWGSelectorSettings.h"
9
10 #include "WGConfigSelectorTypes.h"
11 #include "WGSelectorConfigGrid.h"
12 #include "WGColorSelectorDock.h"
13 #include "WGShadeLineEditor.h"
14
15 #include <QApplication>
16 #include <QButtonGroup>
17 #include <QVBoxLayout>
18 #include <QScreen>
19 #include <QDialogButtonBox>
20 #include <QPushButton>
21 #include <QToolButton>
22
23 WGColorSelectorSettings::WGColorSelectorSettings(QWidget *parent)
24 : KisPreferenceSet(parent)
25 , m_ui(new Ui::WGConfigWidget)
26 , m_shadeLineGroup(new QButtonGroup(this))
27 {
28 m_ui->setupUi(this);
29 m_selectorConfigGrid = new WGSelectorConfigGrid;
30 m_selectorConfigGrid->setConfigurations(WGSelectorConfigGrid::hueBasedConfigurations());
31 m_ui->btnSelectorShape->setPopupWidget(m_selectorConfigGrid);
32 connect(m_selectorConfigGrid, SIGNAL(sigConfigSelected(KisColorSelectorConfiguration)),
33 SLOT(slotSetSelectorConfiguration(KisColorSelectorConfiguration)));
34 connect(m_selectorConfigGrid, SIGNAL(sigConfigSelected(KisColorSelectorConfiguration)),
35 m_ui->btnSelectorShape, SLOT(hidePopupWidget()));
36 connect(m_ui->cmbColorModel, SIGNAL(currentIndexChanged(int)), SLOT(slotSetColorModel(int)));
37 connect(m_ui->sbShadeLineCount, SIGNAL(valueChanged(int)), SLOT(slotSetShadeLineCount(int)));
38 m_favoriteConfigGrid = new WGSelectorConfigGrid(0, true);
39 m_favoriteConfigGrid->setConfigurations(WGSelectorConfigGrid::hueBasedConfigurations());
40 m_ui->btnFavoriteSelectors->setPopupWidget(m_favoriteConfigGrid);
41
42 m_shadeLineEditor = new WGShadeLineEditor(this);
43 m_shadeLineEditor->hide();
44 connect(m_shadeLineEditor, SIGNAL(sigEditorClosed(int)), SLOT(slotLineEdited(int)));
45
46 slotColorSpaceSourceChanged(m_ui->cmbSelectionColorSpace->currentIndex());
47 connect(m_ui->cmbSelectionColorSpace, SIGNAL(currentIndexChanged(int)), SLOT(slotColorSpaceSourceChanged(int)));
48
49 m_shadeLineGroup->setExclusive(false);
50 slotSetShadeLineCount(m_ui->sbShadeLineCount->value());
51 connect(m_shadeLineGroup, SIGNAL(idClicked(int)), SLOT(slotShowLineEditor(int)));
52 }
53
54 WGColorSelectorSettings::~WGColorSelectorSettings()
55 {
56 }
57
58 QString WGColorSelectorSettings::id()
59 {
60 return stringID();
61 }
62
63 QString WGColorSelectorSettings::name()
64 {
65 return QString(i18n("Wide Gamut Selector"));
66 }
67
68 QString WGColorSelectorSettings::header()
69 {
70 return QString(i18n("Wide Gamut Color Selector Settings"));
71 }
72
73 QIcon WGColorSelectorSettings::icon()
74 {
75 return QIcon();
76 }
77
78 QString WGColorSelectorSettings::stringID()
79 {
80 return QString("WideGamutColorSelector");
81 }
82
83 void WGColorSelectorSettings::savePreferences() const
84 {
85 WGConfig::Accessor cfg(false);
86 cfg.set(WGConfig::selectorRenderMode,
87 static_cast<KisVisualColorSelector::RenderMode>(m_ui->cmbSelectorRenderingMode->currentIndex()));
88 cfg.set(WGConfig::rgbColorModel, static_cast<KisVisualColorModel::ColorModel>(
89 m_ui->cmbColorModel->currentIndex() + KisVisualColorModel::HSV));
90 cfg.setColorSelectorConfiguration(m_selectorConfigGrid->currentConfiguration());
91 // General
92 cfg.set(WGConfig::quickSettingsEnabled, m_ui->grpQuickSettingsMenu->isChecked());
93 cfg.setFavoriteConfigurations(m_favoriteConfigGrid->selectedConfigurations());
94 cfg.set(WGConfig::colorSpaceSource,
95 static_cast<WGColorSelectorDock::ColorSpaceSource>(m_ui->cmbSelectionColorSpace->currentIndex()));
96 cfg.setCustomSelectionColorSpace(m_ui->wdgColorspace->currentColorSpace());
97 cfg.set(WGConfig::proofToPaintingColors, m_ui->chkProofColors->isChecked());
98 // Pop-ups
99 cfg.set(WGConfig::popupSize, m_ui->sbPopupSize->value());
100 cfg.set(WGConfig::popupPatches.orientation, m_ui->rbPopupHorizontal->isChecked() ? Qt::Horizontal : Qt::Vertical);
101 cfg.set(WGConfig::popupPatches.patchSize, QSize(m_ui->sbPatchWidth->value(), m_ui->sbPatchHeight->value()));
102 cfg.set(WGConfig::popupPatches.maxCount, m_ui->sbPopupMaxPatches->value());
103 cfg.set(WGConfig::popupPatches.rows, m_ui->sbPopupPatchesRows->value());
104 cfg.set(WGConfig::popupPatches.scrolling, static_cast<WGConfig::Scrolling>(m_ui->cbPopupScrolling->currentIndex()));
105 // Shade Selector
106 cfg.set(WGConfig::minimalShadeSelectorEnabled, m_ui->grpMinimalShadeSelector->isChecked());
107 cfg.set(WGConfig::shadeSelectorUpdateOnExternalChanges, m_ui->chkShadeSelUpdateExternal->isChecked());
108 cfg.set(WGConfig::shadeSelectorUpdateOnInteractionEnd, m_ui->chkShadeSelUpdateInteraction->isChecked());
109 cfg.set(WGConfig::shadeSelectorUpdateOnRightClick, m_ui->chkShadeSelUpdateOnRightClick->isChecked());
110 // we don't discard shade line configurations right away so we need to trim here
111 QVector<WGConfig::ShadeLine> lineConfig = m_shadeLineConfig;
112 lineConfig.resize(m_ui->sbShadeLineCount->value());
113 cfg.setShadeSelectorLines(lineConfig);
114 cfg.set(WGConfig::shadeSelectorLineHeight, m_ui->sbShadeLineHeight->value());
115 // Color History
116 cfg.set(WGConfig::colorHistoryEnabled, m_ui->historyGroupBox->isChecked());
117 cfg.set(WGConfig::colorHistory.orientation, m_ui->rbHistoryHorizontal->isChecked() ? Qt::Horizontal : Qt::Vertical);
118 cfg.set(WGConfig::colorHistory.patchSize, { m_ui->sbHistoryPatchWidth->value(), m_ui->sbHistoryPatchHeight->value() });
119 cfg.set(WGConfig::colorHistory.maxCount, m_ui->sbHistoryMaxPatches->value());
120 cfg.set(WGConfig::colorHistory.rows, m_ui->sbHistoryRows->value());
121 cfg.set(WGConfig::colorHistory.scrolling, static_cast<WGConfig::Scrolling>(m_ui->cbScrolling->currentIndex()));
122 cfg.set(WGConfig::colorHistoryShowClearButton, m_ui->ckHistoryClearButton->isChecked());
123 // Common Colors (Colors from Image)
124 cfg.set(WGConfig::commonColorsEnabled, m_ui->commonColorsGroupBox->isChecked());
125 cfg.set(WGConfig::commonColors.orientation, m_ui->rbCommonColorsHorizontal->isChecked() ? Qt::Horizontal : Qt::Vertical);
126 cfg.set(WGConfig::commonColors.patchSize, { m_ui->sbCommonColorsPatchWidth->value(),
127 m_ui->sbCommonColorsPatchHeight->value() });
128 cfg.set(WGConfig::commonColors.maxCount, m_ui->sbCommonColorsPatches->value());
129 cfg.set(WGConfig::commonColors.rows, m_ui->sbCommonColorsRows->value());
130 cfg.set(WGConfig::commonColors.scrolling, static_cast<WGConfig::Scrolling>(m_ui->cbCommonColorsScrolling->currentIndex()));
131 cfg.set(WGConfig::commonColorsAutoUpdate, m_ui->ckCCAutoUpdate->isChecked());
132
133 WGConfig::notifier()->notifyConfigChanged();
134 WGConfig::notifier()->notifySelectorConfigChanged();
135 }
136
137 void WGColorSelectorSettings::loadPreferences()
138 {
139 loadPreferencesImpl(false);
140 }
141
142 void WGColorSelectorSettings::loadDefaultPreferences()
143 {
144 loadPreferencesImpl(true);
145 }
146
147 void WGColorSelectorSettings::loadPreferencesImpl(bool defaults)
148 {
149 WGConfig::Accessor cfg;
150 m_ui->cmbSelectorRenderingMode->setCurrentIndex(cfg.get(WGConfig::selectorRenderMode, defaults));
151 m_ui->cmbColorModel->setCurrentIndex(cfg.get(WGConfig::rgbColorModel, defaults) - KisVisualColorModel::HSV);
152 KisColorSelectorConfiguration selectorCfg = defaults ? cfg.defaultColorSelectorConfiguration
153 : cfg.colorSelectorConfiguration();
154 m_selectorConfigGrid->setChecked(selectorCfg);
155 m_ui->btnSelectorShape->setIcon(m_selectorConfigGrid->generateIcon(selectorCfg, devicePixelRatioF()));
156 // General
157 m_ui->grpQuickSettingsMenu->setChecked(cfg.get(WGConfig::quickSettingsEnabled, defaults));
158 QVector<KisColorSelectorConfiguration> favoriteConfigs = cfg.favoriteConfigurations(defaults);
159 for (const KisColorSelectorConfiguration &fav : std::as_const(favoriteConfigs)) {
160 m_favoriteConfigGrid->setChecked(fav);
161 }
162 m_ui->cmbSelectionColorSpace->setCurrentIndex(cfg.get(WGConfig::colorSpaceSource, defaults));
163 m_ui->wdgColorspace->setCurrentColorSpace(cfg.customSelectionColorSpace(defaults));
164 m_ui->chkProofColors->setChecked(cfg.get(WGConfig::proofToPaintingColors, defaults));
165 // Pop-ups
166 m_ui->sbPopupSize->setValue(cfg.get(WGConfig::popupSize, defaults));
167 Qt::Orientation patchOrientation = cfg.get(WGConfig::popupPatches.orientation, defaults);
168 if (patchOrientation == Qt::Horizontal) {
169 m_ui->rbPopupHorizontal->setChecked(true);
170 } else {
171 m_ui->rbPopupVertical->setChecked(true);
172 }
173 QSize colorPatchSize = cfg.get(WGConfig::popupPatches.patchSize, defaults);
174 m_ui->sbPatchWidth->setValue(colorPatchSize.width());
175 m_ui->sbPatchHeight->setValue(colorPatchSize.height());
176 m_ui->sbPopupMaxPatches->setValue(cfg.get(WGConfig::popupPatches.maxCount, defaults));
177 m_ui->sbPopupPatchesRows->setValue(cfg.get(WGConfig::popupPatches.rows, defaults));
178 m_ui->cbPopupScrolling->setCurrentIndex(static_cast<int>(cfg.get(WGConfig::popupPatches.scrolling, defaults)));
179 // Shade Selector
180 m_ui->grpMinimalShadeSelector->setChecked(cfg.get(WGConfig::minimalShadeSelectorEnabled, defaults));
181 m_ui->chkShadeSelUpdateExternal->setChecked(cfg.get(WGConfig::shadeSelectorUpdateOnExternalChanges, defaults));
182 m_ui->chkShadeSelUpdateInteraction->setChecked(cfg.get(WGConfig::shadeSelectorUpdateOnInteractionEnd, defaults));
183 m_ui->chkShadeSelUpdateOnRightClick->setChecked(cfg.get(WGConfig::shadeSelectorUpdateOnRightClick, defaults));
184 m_shadeLineConfig = cfg.shadeSelectorLines(defaults);
185 // update shade lines we will re-use, the rest is handled by slotSetShadeLineCount()
186 for (int i = 0, end = qMin(m_shadeLineButtons.size(), m_shadeLineConfig.size()); i < end; i++) {
187 m_shadeLineButtons.at(i)->setIcon(m_shadeLineEditor->generateIcon(m_shadeLineConfig.at(i)));
188 }
189 m_ui->sbShadeLineCount->setValue(m_shadeLineConfig.size());
190 m_ui->sbShadeLineHeight->setValue(cfg.get(WGConfig::shadeSelectorLineHeight, defaults));
191 // Color History
192 m_ui->historyGroupBox->setChecked(cfg.get(WGConfig::colorHistoryEnabled, defaults));
193 patchOrientation = cfg.get(WGConfig::colorHistory.orientation, defaults);
194 if (patchOrientation == Qt::Horizontal) {
195 m_ui->rbHistoryHorizontal->setChecked(true);
196 } else {
197 m_ui->rbHistoryVertical->setChecked(true);
198 }
199 colorPatchSize = cfg.get(WGConfig::colorHistory.patchSize, defaults);
200 m_ui->sbHistoryPatchWidth->setValue(colorPatchSize.width());
201 m_ui->sbHistoryPatchHeight->setValue(colorPatchSize.height());
202 m_ui->sbHistoryMaxPatches->setValue(cfg.get(WGConfig::colorHistory.maxCount));
203 m_ui->sbHistoryRows->setValue(cfg.get(WGConfig::colorHistory.rows, defaults));
204 m_ui->cbScrolling->setCurrentIndex(static_cast<int>(cfg.get(WGConfig::colorHistory.scrolling, defaults)));
205 m_ui->ckHistoryClearButton->setChecked(cfg.get(WGConfig::colorHistoryShowClearButton, defaults));
206 // Common Colors (Colors from Image)
207 m_ui->commonColorsGroupBox->setChecked(cfg.get(WGConfig::commonColorsEnabled, defaults));
208 patchOrientation = cfg.get(WGConfig::commonColors.orientation, defaults);
209 if (patchOrientation == Qt::Horizontal) {
210 m_ui->rbCommonColorsHorizontal->setChecked(true);
211 } else {
212 m_ui->rbCommonColorsVertical->setChecked(true);
213 }
214 colorPatchSize = cfg.get(WGConfig::commonColors.patchSize, defaults);
215 m_ui->sbCommonColorsPatchWidth->setValue(colorPatchSize.width());
216 m_ui->sbCommonColorsPatchHeight->setValue(colorPatchSize.height());
217 m_ui->sbCommonColorsPatches->setValue(cfg.get(WGConfig::commonColors.maxCount, defaults));
218 m_ui->sbCommonColorsRows->setValue(cfg.get(WGConfig::commonColors.rows, defaults));
219 m_ui->cbCommonColorsScrolling->setCurrentIndex(static_cast<int>(cfg.get(WGConfig::commonColors.scrolling, defaults)));
220 m_ui->ckCCAutoUpdate->setChecked(cfg.get(WGConfig::commonColorsAutoUpdate, defaults));
221 }
222
223 void WGColorSelectorSettings::slotSetSelectorConfiguration(const KisColorSelectorConfiguration &cfg)
224 {
225 Q_UNUSED(cfg);
226 m_ui->btnSelectorShape->setIcon(m_selectorConfigGrid->currentIcon());
227 }
228
229 void WGColorSelectorSettings::slotSetColorModel(int index)
230 {
231 KisVisualColorModel::ColorModel model;
232 switch (index) {
233 case 0:
234 default:
235 model = KisVisualColorModel::HSV;
236 break;
237 case 1:
238 model = KisVisualColorModel::HSL;
239 break;
240 case 2:
241 model = KisVisualColorModel::HSI;
242 break;
243 case 3:
244 model = KisVisualColorModel::HSY;
245 }
246 m_selectorConfigGrid->setColorModel(model);
247 m_ui->btnSelectorShape->setIcon(m_selectorConfigGrid->currentIcon());
248 }
249
250 void WGColorSelectorSettings::slotColorSpaceSourceChanged(int index)
251 {
252 WGColorSelectorDock::ColorSpaceSource csSource = static_cast<WGColorSelectorDock::ColorSpaceSource>(index);
253 m_ui->wdgColorspace->setEnabled(csSource == WGColorSelectorDock::FixedColorSpace);
254 }
255
256 void WGColorSelectorSettings::slotSetShadeLineCount(int count)
257 {
258 if (m_shadeLineConfig.size() < count) {
259 m_shadeLineConfig.resize(count);
260 }
261 while (m_shadeLineButtons.size() < count) {
262 QToolButton *lineButton = new QToolButton(this);
263 lineButton->setIconSize(QSize(128, 10));
264 // TODO: update icon
265 lineButton->setIcon(m_shadeLineEditor->generateIcon(m_shadeLineConfig.at(m_shadeLineButtons.size())));
266 m_shadeLineGroup->addButton(lineButton, m_shadeLineButtons.size());
267 m_shadeLineButtons.append(lineButton);
268 m_ui->shadeLineLayout->addWidget(lineButton);
269 }
270 while (m_shadeLineButtons.size() > count)
271 {
272 m_ui->shadeLineLayout->removeWidget(m_shadeLineButtons.last());
273 delete m_shadeLineButtons.last();
274 m_shadeLineButtons.removeLast();
275 }
276 }
277
278 void WGColorSelectorSettings::slotShowLineEditor(int lineNum)
279 {
280 KIS_SAFE_ASSERT_RECOVER_RETURN(lineNum >= 0 && lineNum < m_shadeLineConfig.size());
281
282 m_shadeLineEditor->setConfiguration(m_shadeLineConfig[lineNum], lineNum);
283 m_shadeLineEditor->show();
284
285 QWidget *btn = m_shadeLineButtons.at(lineNum);
286 QRect fitRect = kisGrowRect(btn->screen()->availableGeometry(), -10);
287 QRect popupRect = m_shadeLineEditor->rect();
288 popupRect.moveTo(btn->mapToGlobal(QPoint()));
289 popupRect = kisEnsureInRect(popupRect, fitRect);
290 m_shadeLineEditor->move(popupRect.topLeft());
291 }
292
293 void WGColorSelectorSettings::slotLineEdited(int lineNum)
294 {
295 KIS_SAFE_ASSERT_RECOVER_RETURN(lineNum >= 0 && lineNum < m_shadeLineConfig.size());
296
297 m_shadeLineConfig[lineNum] = m_shadeLineEditor->configuration();
298 m_shadeLineButtons[lineNum]->setIcon(m_shadeLineEditor->generateIcon(m_shadeLineConfig.at(lineNum)));
299 }
300
301
302 WGColorSelectorSettingsDialog::WGColorSelectorSettingsDialog(QWidget *parent)
303 : QDialog(parent)
304 , m_widget(new WGColorSelectorSettings(this))
305 {
306 QLayout* layout = new QVBoxLayout(this);
307 layout->addWidget(m_widget);
308
309 m_widget->loadPreferences();
310
311 QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel|QDialogButtonBox::RestoreDefaults,
312 Qt::Horizontal,
313 this);
314 layout->addWidget(buttonBox);
315
316 connect(buttonBox, SIGNAL(accepted()), m_widget, SLOT(savePreferences()));
317 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
318 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
319 connect(buttonBox->button(QDialogButtonBox::RestoreDefaults),
320 SIGNAL(clicked()), m_widget, SLOT(loadDefaultPreferences()));
321 }