1 /*
2  *  Copyright (c) 2016 Dmitry Kazakov <dimula73@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "kis_uniform_paintop_property_widget.h"
20 
21 #include <QVBoxLayout>
22 #include <QCheckBox>
23 #include <QComboBox>
24 
25 #include "kis_slider_spin_box.h"
26 #include "kis_acyclic_signal_connector.h"
27 #include "kis_slider_based_paintop_property.h"
28 #include "kis_combo_based_paintop_property.h"
29 #include "kis_debug.h"
30 
31 /****************************************************************/
32 /*      KisUniformPaintOpPropertyWidget                         */
33 /****************************************************************/
34 
35 struct KisUniformPaintOpPropertyWidget::Private
36 {
PrivateKisUniformPaintOpPropertyWidget::Private37     Private(KisUniformPaintOpPropertySP _property)
38         : property(_property) {}
39 
40     typedef KisUniformPaintOpProperty::Type Type;
41     KisUniformPaintOpPropertySP property;
42 };
43 
KisUniformPaintOpPropertyWidget(KisUniformPaintOpPropertySP property,QWidget * parent)44 KisUniformPaintOpPropertyWidget::KisUniformPaintOpPropertyWidget(KisUniformPaintOpPropertySP property, QWidget *parent)
45     : QWidget(parent),
46       m_d(new Private(property))
47 {
48     KisAcyclicSignalConnector *conn = new KisAcyclicSignalConnector(this);
49     conn->connectForwardVariant(property.data(), SIGNAL(valueChanged(QVariant)),
50                                 this, SLOT(setValue(QVariant)));
51 
52     conn->connectBackwardVariant(this, SIGNAL(valueChanged(QVariant)),
53                                  property.data(), SLOT(setValue(QVariant)));
54 }
55 
~KisUniformPaintOpPropertyWidget()56 KisUniformPaintOpPropertyWidget::~KisUniformPaintOpPropertyWidget()
57 {
58 }
59 
property() const60 KisUniformPaintOpPropertySP KisUniformPaintOpPropertyWidget::property() const
61 {
62     return m_d->property;
63 }
64 
slotThemeChanged(QPalette pal)65 void KisUniformPaintOpPropertyWidget::slotThemeChanged(QPalette pal)
66 {
67     for(int i=0; i<this->children().size(); i++) {
68         QWidget *w = qobject_cast<QWidget*>(this->children().at(i));
69         if (w) {
70             w->setPalette(pal);
71         }
72     }
73 }
74 
75 /****************************************************************/
76 /*      KisUniformPaintOpPropertyIntSlider                      */
77 /****************************************************************/
78 
KisUniformPaintOpPropertyIntSlider(KisUniformPaintOpPropertySP property,QWidget * parent)79 KisUniformPaintOpPropertyIntSlider::KisUniformPaintOpPropertyIntSlider(KisUniformPaintOpPropertySP property, QWidget *parent)
80     : KisUniformPaintOpPropertyWidget(property, parent)
81 {
82     const QString prefix = QString("%1: ").arg(property->name());
83     QVBoxLayout *layout = new QVBoxLayout(this);
84 
85     KisIntSliderBasedPaintOpProperty *sliderProperty =
86         dynamic_cast<KisIntSliderBasedPaintOpProperty*>(property.data());
87     KIS_ASSERT_RECOVER_RETURN(sliderProperty);
88 
89     m_slider = new KisSliderSpinBox(this);
90     m_slider->setBlockUpdateSignalOnDrag(true);
91     m_slider->setRange(sliderProperty->min(), sliderProperty->max());
92     m_slider->setSingleStep(sliderProperty->singleStep());
93     m_slider->setPageStep(sliderProperty->pageStep());
94     m_slider->setPrefix(prefix);
95     m_slider->setSuffix(sliderProperty->suffix());
96     m_slider->setExponentRatio(sliderProperty->exponentRatio());
97 
98     m_slider->setValue(sliderProperty->value().toInt());
99     connect(m_slider, SIGNAL(valueChanged(int)), SLOT(slotSliderChanged(int)));
100 
101     layout->addWidget(m_slider);
102     setLayout(layout);
103 }
104 
setValue(const QVariant & value)105 void KisUniformPaintOpPropertyIntSlider::setValue(const QVariant &value)
106 {
107     m_slider->setValue(value.toInt());
108 }
109 
slotSliderChanged(int value)110 void KisUniformPaintOpPropertyIntSlider::slotSliderChanged(int value)
111 {
112     emit valueChanged(value);
113 }
114 
115 /****************************************************************/
116 /*      KisUniformPaintOpPropertyDoubleSlider                   */
117 /****************************************************************/
118 
KisUniformPaintOpPropertyDoubleSlider(KisUniformPaintOpPropertySP property,QWidget * parent)119 KisUniformPaintOpPropertyDoubleSlider::KisUniformPaintOpPropertyDoubleSlider(KisUniformPaintOpPropertySP property, QWidget *parent)
120     : KisUniformPaintOpPropertyWidget(property, parent)
121 {
122     const QString prefix = QString("%1: ").arg(property->name());
123     QVBoxLayout *layout = new QVBoxLayout(this);
124 
125     KisDoubleSliderBasedPaintOpProperty *sliderProperty =
126         dynamic_cast<KisDoubleSliderBasedPaintOpProperty*>(property.data());
127     KIS_ASSERT_RECOVER_RETURN(sliderProperty);
128 
129     m_slider = new KisDoubleSliderSpinBox(this);
130     m_slider->setBlockUpdateSignalOnDrag(true);
131     m_slider->setRange(sliderProperty->min(), sliderProperty->max(), sliderProperty->decimals());
132     m_slider->setSingleStep(sliderProperty->singleStep());
133     m_slider->setPrefix(prefix);
134     m_slider->setSuffix(sliderProperty->suffix());
135     m_slider->setExponentRatio(sliderProperty->exponentRatio());
136 
137     m_slider->setValue(sliderProperty->value().toReal());
138     connect(m_slider, SIGNAL(valueChanged(qreal)), SLOT(slotSliderChanged(qreal)));
139 
140     layout->addWidget(m_slider);
141     setLayout(layout);
142 }
143 
setValue(const QVariant & value)144 void KisUniformPaintOpPropertyDoubleSlider::setValue(const QVariant &value)
145 {
146     m_slider->setValue(value.toReal());
147 }
148 
slotSliderChanged(qreal value)149 void KisUniformPaintOpPropertyDoubleSlider::slotSliderChanged(qreal value)
150 {
151     emit valueChanged(value);
152 }
153 
154 /****************************************************************/
155 /*      KisUniformPaintOpPropertyCheckBox                       */
156 /****************************************************************/
157 
KisUniformPaintOpPropertyCheckBox(KisUniformPaintOpPropertySP property,QWidget * parent)158 KisUniformPaintOpPropertyCheckBox::KisUniformPaintOpPropertyCheckBox(KisUniformPaintOpPropertySP property, QWidget *parent)
159     : KisUniformPaintOpPropertyWidget(property, parent)
160 {
161     QVBoxLayout *layout = new QVBoxLayout(this);
162 
163     m_checkBox = new QCheckBox(property->name(), this);
164     m_checkBox->setChecked(property->value().toBool());
165     connect(m_checkBox, SIGNAL(toggled(bool)), SLOT(slotCheckBoxChanged(bool)));
166 
167     layout->addWidget(m_checkBox);
168     setLayout(layout);
169 }
170 
setValue(const QVariant & value)171 void KisUniformPaintOpPropertyCheckBox::setValue(const QVariant &value)
172 {
173     m_checkBox->setChecked(value.toBool());
174 }
175 
slotCheckBoxChanged(bool value)176 void KisUniformPaintOpPropertyCheckBox::slotCheckBoxChanged(bool value)
177 {
178     emit valueChanged(value);
179 }
180 
181 /****************************************************************/
182 /*      KisUniformPaintOpPropertyComboBox                       */
183 /****************************************************************/
184 
KisUniformPaintOpPropertyComboBox(KisUniformPaintOpPropertySP property,QWidget * parent)185 KisUniformPaintOpPropertyComboBox::KisUniformPaintOpPropertyComboBox(KisUniformPaintOpPropertySP property, QWidget *parent)
186     : KisUniformPaintOpPropertyWidget(property, parent)
187 {
188     QVBoxLayout *layout = new QVBoxLayout(this);
189 
190     KisComboBasedPaintOpProperty *comboProperty =
191         dynamic_cast<KisComboBasedPaintOpProperty*>(property.data());
192     KIS_ASSERT_RECOVER_RETURN(comboProperty);
193 
194     const QList<QString> items = comboProperty->items();
195     const QList<QIcon> icons = comboProperty->icons();
196 
197     m_comboBox = new QComboBox(this);
198 
199     KIS_SAFE_ASSERT_RECOVER_RETURN(icons.isEmpty() ||
200                                    items.size() == icons.size());
201 
202     if (!icons.isEmpty()) {
203         auto itemIt = items.constBegin();
204         auto iconIt = icons.constBegin();
205 
206         while (itemIt != items.constEnd() &&
207                iconIt != icons.constEnd()) {
208 
209             m_comboBox->addItem(*iconIt, *itemIt);
210 
211             ++itemIt;
212             ++iconIt;
213         }
214     } else {
215         Q_FOREACH (const QString &item, items) {
216             m_comboBox->addItem(item);
217         }
218     }
219 
220     m_comboBox->setCurrentIndex(property->value().toInt());
221     connect(m_comboBox, SIGNAL(currentIndexChanged(int)), SLOT(slotComboBoxChanged(int)));
222 
223     layout->addWidget(m_comboBox);
224     setLayout(layout);
225 }
226 
setValue(const QVariant & value)227 void KisUniformPaintOpPropertyComboBox::setValue(const QVariant &value)
228 {
229     m_comboBox->setCurrentIndex(value.toInt());
230 }
231 
slotComboBoxChanged(int value)232 void KisUniformPaintOpPropertyComboBox::slotComboBoxChanged(int value)
233 {
234     emit valueChanged(value);
235 }
236