1 /*
2  *  kis_double_widget.cc - part of Krita
3  *
4  *  Copyright (c) 1999 Carsten Pfeiffer <pfeiffer@kde.org>
5  *  Copyright (c) 2004 Adrian Page <adrian@pagenet.plus.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #include "widgets/kis_double_widget.h"
23 
24 #include <QLayout>
25 #include <QSlider>
26 #include <QHBoxLayout>
27 #include <QDoubleSpinBox>
28 
29 #include "kis_double_parse_spin_box.h"
30 
KisDoubleWidget(QWidget * parent,const char * name)31 KisDoubleWidget::KisDoubleWidget(QWidget* parent, const char* name)
32         : QWidget(parent)
33 {
34     setObjectName(name);
35     init(0, 1);
36 }
37 
KisDoubleWidget(double min,double max,QWidget * parent,const char * name)38 KisDoubleWidget::KisDoubleWidget(double min, double max, QWidget* parent, const char* name)
39         : QWidget(parent)
40 {
41     setObjectName(name);
42     init(min, max);
43 }
44 
~KisDoubleWidget()45 KisDoubleWidget::~KisDoubleWidget()
46 {
47 }
48 
init(double min,double max)49 void KisDoubleWidget::init(double min, double max)
50 {
51     m_spinBox = new KisDoubleParseSpinBox(this);
52     m_spinBox->setMinimum(min);
53     m_spinBox->setMaximum(max);
54     m_spinBox->setSingleStep(0.05);
55     m_spinBox->setValue(0);
56     m_spinBox->setObjectName("spinbox");
57     connect(m_spinBox, SIGNAL(valueChanged(double)), this, SLOT(setSliderValue(double)));
58 
59     m_slider = new QSlider(Qt::Horizontal, this);
60     m_slider->setMinimum(static_cast<int>(min * 100 + 0.5));
61     m_slider->setMaximum(static_cast<int>(max * 100 + 0.5));
62     m_slider->setPageStep(1);
63     m_slider->setValue(0);
64     m_slider->setObjectName("slider");
65 
66     connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int)));
67     connect(m_slider, SIGNAL(sliderPressed()), SIGNAL(sliderPressed()));
68     connect(m_slider, SIGNAL(sliderReleased()), SIGNAL(sliderReleased()));
69 
70     m_layout = new QHBoxLayout(this);
71     m_layout->setObjectName("hbox layout");
72     m_layout->setMargin(0);
73     m_layout->setSpacing(0);
74 
75     m_layout->addWidget(m_slider);
76     m_layout->addSpacing(5);
77     m_layout->addWidget(m_spinBox);
78     m_layout->addItem(new QSpacerItem(5, 1, QSizePolicy::Expanding, QSizePolicy::Minimum));
79 }
80 
value() const81 double KisDoubleWidget::value() const
82 {
83     return m_spinBox->value();
84 }
85 
setValue(double value)86 void KisDoubleWidget::setValue(double value)
87 {
88     int intValue;
89 
90     if (value < 0) {
91         intValue = static_cast<int>(value * 100 - 0.5);
92     } else {
93         intValue = static_cast<int>(value * 100 + 0.5);
94     }
95     m_slider->setValue(intValue);
96 }
97 
setRange(double min,double max)98 void KisDoubleWidget::setRange(double min, double max)
99 {
100     m_spinBox->setRange(min, max);
101     m_slider->setRange(static_cast<int>(min * 100 + 0.5), static_cast<int>(max * 100 + 0.5));
102 }
103 
setTickPosition(QSlider::TickPosition tickPosition)104 void KisDoubleWidget::setTickPosition(QSlider::TickPosition tickPosition)
105 {
106     m_slider->setTickPosition(tickPosition);
107 }
108 
setTickInterval(double value)109 void KisDoubleWidget::setTickInterval(double value)
110 {
111     m_slider->setTickInterval(static_cast<int>(value * 100 + 0.5));
112 }
113 
tickInterval() const114 double KisDoubleWidget::tickInterval() const
115 {
116     return m_slider->tickInterval() / 100.0;
117 }
118 
setSliderValue(double value)119 void KisDoubleWidget::setSliderValue(double value)
120 {
121     int intValue;
122 
123     if (value < 0) {
124         intValue = static_cast<int>(value * 100 - 0.5);
125     } else {
126         intValue = static_cast<int>(value * 100 + 0.5);
127     }
128     m_slider->setValue(intValue);
129     emit valueChanged(value);
130 }
131 
sliderValueChanged(int value)132 void KisDoubleWidget::sliderValueChanged(int value)
133 {
134     m_spinBox->setValue(value / 100.0);
135 }
136 
setPrecision(int precision)137 void KisDoubleWidget::setPrecision(int precision)
138 {
139     m_spinBox->setDecimals(precision);
140 }
141 
setSingleStep(double step)142 void KisDoubleWidget::setSingleStep(double step)
143 {
144     m_spinBox->setSingleStep(step);
145     m_slider->setSingleStep(static_cast<int>(step * 100));
146 }
147 
setPageStep(double step)148 void KisDoubleWidget::setPageStep(double step)
149 {
150     m_slider->setPageStep(static_cast<int>(step * 100));
151 }
152 
setTracking(bool tracking)153 void KisDoubleWidget::setTracking(bool tracking)
154 {
155     m_slider->setTracking(tracking);
156 }
157 
hasTracking() const158 bool KisDoubleWidget::hasTracking() const
159 {
160     return m_slider->hasTracking();
161 }
162 
163 
164