Debian Codegraph @

plugins/dockers/widegamutcolorselector/WGShadeSelector.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 "WGShadeSelector.h"
8
9 #include "WGConfig.h"
10 #include "WGShadeSlider.h"
11
12 #include <QVBoxLayout>
13 #include <QMouseEvent>
14
15 WGShadeSelector::WGShadeSelector(WGSelectorDisplayConfigSP displayConfig, KisVisualColorModelSP colorModel, QWidget *parent)
16 : WGSelectorWidgetBase(displayConfig, parent)
17 , m_model(colorModel)
18 {
19 QVBoxLayout* l = new QVBoxLayout(this);
20 l->setSpacing(1);
21 l->setContentsMargins(0, 0, 0, 0);
22 setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
23
24 connectToModel();
25 }
26
27 void WGShadeSelector::setModel(KisVisualColorModelSP colorModel)
28 {
29 if (m_model) {
30 m_model->disconnect(this);
31 disconnect(m_model.data());
32 }
33 m_model = colorModel;
34 for (WGShadeSlider *slider : std::as_const(m_sliders)) {
35 slider->setModel(m_model);
36 }
37 connectToModel();
38 if (m_model->colorModel() != KisVisualColorModel::None) {
39 slotChannelValuesChanged(m_model->channelValues());
40 }
41 }
42
43 void WGShadeSelector::updateSettings()
44 {
45 WGConfig::Accessor cfg;
46 m_lineHeight = cfg.get(WGConfig::shadeSelectorLineHeight);
47 QVector<WGConfig::ShadeLine> config = cfg.shadeSelectorLines();
48
49 while (config.size() > m_sliders.size()) {
50 WGShadeSlider *line = new WGShadeSlider(displayConfiguration(), this, m_model);
51 m_sliders.append(line);
52 layout()->addWidget(m_sliders.last());
53 connect(line, SIGNAL(sigChannelValuesChanged(QVector4D)), SLOT(slotSliderValuesChanged(QVector4D)));
54 connect(line, SIGNAL(sigInteraction(bool)), SLOT(slotSliderInteraction(bool)));
55 }
56 while (config.size() < m_sliders.size()) {
57 layout()->removeWidget(m_sliders.last());
58 delete m_sliders.takeLast();
59 }
60
61 for (int i=0; i < config.size(); i++) {
62 m_sliders[i]->setGradient(config[i].gradient, config[i].offset);
63 m_sliders[i]->setDisplayMode(config[i].patchCount < 0, config[i].patchCount);
64 m_sliders[i]->setFixedHeight(m_lineHeight);
65 }
66 m_resetOnExternalUpdate = cfg.get(WGConfig::shadeSelectorUpdateOnExternalChanges);
67 m_resetOnInteractions = cfg.get(WGConfig::shadeSelectorUpdateOnInteractionEnd);
68 m_resetOnRightClick = cfg.get(WGConfig::shadeSelectorUpdateOnRightClick);
69
70 if (m_model->colorModel() != KisVisualColorModel::None) {
71 slotReset();
72 slotChannelValuesChanged(m_model->channelValues());
73 }
74 }
75
76 void WGShadeSelector::mousePressEvent(QMouseEvent *event)
77 {
78 if (m_resetOnRightClick && event->button() == Qt::RightButton) {
79 for (WGShadeSlider* slider : std::as_const(m_sliders)) {
80 slider->slotSetChannelValues(m_model->channelValues());
81 }
82 }
83 }
84
85 void WGShadeSelector::connectToModel()
86 {
87 connect(m_model.data(), SIGNAL(sigColorModelChanged()), SLOT(slotReset()));
88 connect(m_model.data(), SIGNAL(sigColorSpaceChanged()), SLOT(slotReset()));
89 connect(m_model.data(), SIGNAL(sigChannelValuesChanged(QVector4D,quint32)),
90 SLOT(slotChannelValuesChanged(QVector4D)));
91 connect(this, SIGNAL(sigChannelValuesChanged(QVector4D)),
92 m_model.data(), SLOT(slotSetChannelValues(QVector4D)));
93 }
94
95 void WGShadeSelector::slotChannelValuesChanged(const QVector4D &values)
96 {
97 if (m_allowUpdates && (m_resetOnExternalUpdate || !m_initialized)) {
98 for (WGShadeSlider* slider : std::as_const(m_sliders)) {
99 slider->slotSetChannelValues(values);
100 }
101 m_initialized = true;
102 }
103 }
104
105 void WGShadeSelector::slotSliderValuesChanged(const QVector4D &values)
106 {
107 m_allowUpdates = false;
108 Q_EMIT sigChannelValuesChanged(values);
109 m_allowUpdates = true;
110 }
111
112 void WGShadeSelector::slotSliderInteraction(bool active)
113 {
114 if (active) {
115 const WGShadeSlider* activeLine = qobject_cast<WGShadeSlider*>(sender());
116 for (WGShadeSlider* slider : std::as_const(m_sliders)) {
117 if (slider != activeLine) {
118 slider->resetHandle();
119 }
120 }
121 Q_EMIT sigColorInteraction(active);
122 if (activeLine) {
123 // the line may have different channel values at any position than the last used one
124 m_allowUpdates = false;
125 Q_EMIT sigChannelValuesChanged(activeLine->channelValues());
126 m_allowUpdates = true;
127 }
128 }
129 else {
130 // reset slider base values if configured for automatic reset
131 if (m_resetOnInteractions) {
132 for (WGShadeSlider* slider : std::as_const(m_sliders)) {
133 slider->slotSetChannelValues(m_model->channelValues());
134 }
135 }
136 Q_EMIT sigColorInteraction(active);
137 }
138 }
139
140 void WGShadeSelector::slotReset()
141 {
142 m_initialized = false;
143 }