]> git.sesse.net Git - kdenlive/blob - src/KoSliderCombo.cpp
Fix label
[kdenlive] / src / KoSliderCombo.cpp
1 /* This file is part of the KDE project
2    Copyright (c) 2007 Casper Boemann <cbr@boemann.dk>
3    Copyright (c) 2010 Jean-Baptiste Mardelle <jb@kdenlive.org>
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9
10    This library 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 GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public License
16    along with this library; see the file COPYING.LIB.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19 */
20 #include "KoSliderCombo.h"
21
22 #include <QTimer>
23 #include <QApplication>
24 #include <QSize>
25 #include <QSlider>
26 #include <QStyle>
27 #include <QStylePainter>
28 #include <QStyleOptionSlider>
29 #include <QLineEdit>
30 #include <QValidator>
31 #include <QHBoxLayout>
32 #include <QFrame>
33 #include <QMenu>
34 #include <QMouseEvent>
35 #include <QDoubleSpinBox>
36 #include <QDesktopWidget>
37
38 #include <kglobal.h>
39 #include <klocale.h>
40 #include <kdebug.h>
41
42 class KoSliderComboContainer : public QMenu
43 {
44 public:
45     KoSliderComboContainer(KoSliderCombo *parent) : QMenu(parent), m_parent(parent) {}
46
47 protected:
48     virtual void mousePressEvent(QMouseEvent *e);
49 private:
50     KoSliderCombo *m_parent;
51 };
52
53 void KoSliderComboContainer::mousePressEvent(QMouseEvent *e)
54 {
55     QStyleOptionComboBox opt;
56     opt.init(m_parent);
57     opt.subControls = QStyle::SC_All;
58     opt.activeSubControls = QStyle::SC_ComboBoxArrow;
59     QStyle::SubControl sc = style()->hitTestComplexControl(QStyle::CC_ComboBox, &opt,
60                             m_parent->mapFromGlobal(e->globalPos()),
61                             m_parent);
62     if (sc == QStyle::SC_ComboBoxArrow)
63         setAttribute(Qt::WA_NoMouseReplay);
64     QMenu::mousePressEvent(e);
65 }
66
67 class KoSliderCombo::KoSliderComboPrivate
68 {
69 public:
70     KoSliderCombo *thePublic;
71     QValidator *m_validator;
72     QTimer m_timer;
73     KoSliderComboContainer *container;
74     QSlider *slider;
75     QStyle::StateFlag arrowState;
76     qreal minimum;
77     qreal maximum;
78     int decimals;
79     bool firstShowOfSlider;
80
81     void showPopup();
82     void hidePopup();
83
84     void sliderValueChanged(int value);
85     void sliderReleased();
86     void lineEditFinished();
87 };
88
89 KoSliderCombo::KoSliderCombo(QWidget *parent)
90         : QComboBox(parent)
91         , d(new KoSliderComboPrivate())
92 {
93     d->thePublic = this;
94     d->minimum = 0.0;
95     d->maximum = 100.0;
96     d->decimals = 2;
97     d->container = new KoSliderComboContainer(this);
98     d->container->setAttribute(Qt::WA_WindowPropagation);
99     QStyleOptionComboBox opt;
100     opt.init(this);
101 //    d->container->setFrameStyle(style()->styleHint(QStyle::SH_ComboBox_PopupFrameStyle, &opt, this));
102
103     d->slider = new QSlider(Qt::Horizontal);
104     d->slider->setMinimum(0);
105     d->slider->setMaximum(256);
106     d->slider->setPageStep(10);
107     d->slider->setValue(0);
108     // When set to true, causes flicker on Qt 4.6. Any reason to keep it?
109     d->firstShowOfSlider = false; //true;
110
111     QHBoxLayout * l = new QHBoxLayout();
112     l->setMargin(2);
113     l->setSpacing(2);
114     l->addWidget(d->slider);
115     d->container->setLayout(l);
116     d->container->resize(200, 30);
117
118     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
119
120     setEditable(true);
121     setEditText(KGlobal::locale()->formatNumber(0, d->decimals));
122
123     connect(d->slider, SIGNAL(valueChanged(int)), SLOT(sliderValueChanged(int)));
124     connect(d->slider, SIGNAL(sliderReleased()), SLOT(sliderReleased()));
125     connect(lineEdit(), SIGNAL(editingFinished()), SLOT(lineEditFinished()));
126 }
127
128 KoSliderCombo::~KoSliderCombo()
129 {
130     delete d;
131 }
132
133 QSize KoSliderCombo::sizeHint() const
134 {
135     return minimumSizeHint();
136 }
137
138 QSize KoSliderCombo::minimumSizeHint() const
139 {
140     QSize sh;
141
142     const QFontMetrics &fm = fontMetrics();
143
144     sh.setWidth(5 * fm.width(QLatin1Char('8')));
145     sh.setHeight(qMax(fm.lineSpacing(), 14) + 2);
146
147     // add style and strut values
148     QStyleOptionComboBox opt;
149     opt.init(this);
150     opt.subControls = QStyle::SC_All;
151     opt.editable = true;
152     sh = style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sh, this);
153
154     return sh.expandedTo(QApplication::globalStrut());
155 }
156
157 void KoSliderCombo::KoSliderComboPrivate::showPopup()
158 {
159     if (firstShowOfSlider) {
160         container->show(); //show container a bit early so the slider can be layout'ed
161         firstShowOfSlider = false;
162     }
163
164     QStyleOptionSlider opt;
165     opt.init(slider);
166     opt.maximum = 256;
167     opt.sliderPosition = opt.sliderValue = slider->value();
168     int hdlPos = thePublic->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle).center().x();
169
170     QStyleOptionComboBox optThis;
171     optThis.init(thePublic);
172     optThis.subControls = QStyle::SC_All;
173     optThis.editable = true;
174     int arrowPos = thePublic->style()->subControlRect(QStyle::CC_ComboBox, &optThis, QStyle::SC_ComboBoxArrow).center().x();
175
176     QSize popSize = container->size();
177     QRect popupRect(thePublic->mapToGlobal(QPoint(arrowPos - hdlPos - slider->x(), thePublic->size().height())), popSize);
178
179     // Make sure the popup is not drawn outside the screen area
180     QRect screenRect = QApplication::desktop()->availableGeometry(thePublic);
181     if (popupRect.right() > screenRect.right())
182         popupRect.translate(screenRect.right() - popupRect.right(), 0);
183     if (popupRect.left() < screenRect.left())
184         popupRect.translate(screenRect.left() - popupRect.left(), 0);
185     if (popupRect.bottom() > screenRect.bottom())
186         popupRect.translate(0, -(thePublic->height() + container->height()));
187
188     container->setGeometry(popupRect);
189     container->raise();
190     container->show();
191     slider->setFocus();
192 }
193
194 void KoSliderCombo::KoSliderComboPrivate::hidePopup()
195 {
196     container->hide();
197 }
198
199 void KoSliderCombo::hideEvent(QHideEvent *)
200 {
201     d->hidePopup();
202 }
203
204 void KoSliderCombo::changeEvent(QEvent *e)
205 {
206     switch (e->type()) {
207     case QEvent::EnabledChange:
208         if (!isEnabled())
209             d->hidePopup();
210         break;
211     case QEvent::PaletteChange:
212         d->container->setPalette(palette());
213         break;
214     default:
215         break;
216     }
217     QComboBox::changeEvent(e);
218 }
219
220 void KoSliderCombo::paintEvent(QPaintEvent *)
221 {
222     QStylePainter gc(this);
223
224     gc.setPen(palette().color(QPalette::Text));
225
226     QStyleOptionComboBox opt;
227     opt.init(this);
228     opt.subControls = QStyle::SC_All;
229     opt.editable = true;
230     gc.drawComplexControl(QStyle::CC_ComboBox, opt);
231     gc.drawControl(QStyle::CE_ComboBoxLabel, opt);
232 }
233
234 void KoSliderCombo::mousePressEvent(QMouseEvent *e)
235 {
236     QStyleOptionComboBox opt;
237     opt.init(this);
238     opt.subControls = QStyle::SC_All;
239     opt.editable = true;
240     QStyle::SubControl sc = style()->hitTestComplexControl(QStyle::CC_ComboBox, &opt, e->pos(),
241                             this);
242     if (sc == QStyle::SC_ComboBoxArrow && !d->container->isVisible()) {
243         d->showPopup();
244     } else
245         QComboBox::mousePressEvent(e);
246 }
247
248 void KoSliderCombo::keyPressEvent(QKeyEvent *e)
249 {
250     if (e->key() == Qt::Key_Up) setValue(value() + d->slider->singleStep() *(maximum() - minimum()) / 256 + 0.5);
251     else if (e->key() == Qt::Key_Down) setValue(value() - d->slider->singleStep() *(maximum() - minimum()) / 256 - 0.5);
252     else QComboBox::keyPressEvent(e);
253 }
254
255 void KoSliderCombo::wheelEvent(QWheelEvent *e)
256 {
257     if (e->delta() > 0) setValue(value() + d->slider->singleStep() *(maximum() - minimum()) / 256 + 0.5);
258     else setValue(value() - d->slider->singleStep() *(maximum() - minimum()) / 256 - 0.5);
259 }
260
261 void KoSliderCombo::KoSliderComboPrivate::lineEditFinished()
262 {
263     qreal value = thePublic->currentText().toDouble();
264     slider->blockSignals(true);
265     slider->setValue(int((value - minimum) * 256 / (maximum - minimum) + 0.5));
266     slider->blockSignals(false);
267     emit thePublic->valueChanged(value, true);
268 }
269
270 void KoSliderCombo::KoSliderComboPrivate::sliderValueChanged(int slidervalue)
271 {
272     thePublic->setEditText(KGlobal::locale()->formatNumber(minimum + (maximum - minimum)*slidervalue / 256, decimals));
273
274     qreal value = thePublic->currentText().toDouble();
275     emit thePublic->valueChanged(value, false);
276 }
277
278 void KoSliderCombo::KoSliderComboPrivate::sliderReleased()
279 {
280     qreal value = thePublic->currentText().toDouble();
281     emit thePublic->valueChanged(value, true);
282 }
283
284 qreal KoSliderCombo::maximum() const
285 {
286     return d->maximum;
287 }
288
289 qreal KoSliderCombo::minimum() const
290 {
291     return d->minimum;
292 }
293
294 qreal KoSliderCombo::decimals() const
295 {
296     return d->decimals;
297 }
298
299 qreal KoSliderCombo::value() const
300 {
301     return currentText().toDouble();
302 }
303
304 void KoSliderCombo::setDecimals(int dec)
305 {
306     d->decimals = dec;
307     if (dec == 0) lineEdit()->setValidator(new QIntValidator(this));
308     else lineEdit()->setValidator(new QDoubleValidator(this));
309 }
310
311 void KoSliderCombo::setMinimum(qreal min)
312 {
313     d->minimum = min;
314 }
315
316 void KoSliderCombo::setMaximum(qreal max)
317 {
318     d->maximum = max;
319 }
320
321 void KoSliderCombo::setValue(qreal value)
322 {
323     if (value < d->minimum)
324         value = d->minimum;
325     if (value > d->maximum)
326         value = d->maximum;
327     setEditText(KGlobal::locale()->formatNumber(value, d->decimals));
328     d->slider->blockSignals(true);
329     d->slider->setValue(int((value - d->minimum) * 256 / (d->maximum - d->minimum) + 0.5));
330     d->slider->blockSignals(false);
331     emit valueChanged(value, true);
332 }
333
334 #include <KoSliderCombo.moc>