]> git.sesse.net Git - kdenlive/blob - src/KoSliderCombo.cpp
Re-use KOffice widget in the titler to gain space (spin box with popup slider)
[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 public:
69     KoSliderCombo *thePublic;
70     QValidator *m_validator;
71     QTimer m_timer;
72     KoSliderComboContainer *container;
73     QSlider *slider;
74     QStyle::StateFlag arrowState;
75     qreal minimum;
76     qreal maximum;
77     int decimals;
78     bool firstShowOfSlider;
79
80     void showPopup();
81     void hidePopup();
82
83     void sliderValueChanged(int value);
84     void sliderReleased();
85     void lineEditFinished();
86 };
87
88 KoSliderCombo::KoSliderCombo(QWidget *parent)
89    : QComboBox(parent)
90     ,d(new KoSliderComboPrivate())
91 {
92     d->thePublic = this;
93     d->minimum = 0.0;
94     d->maximum = 100.0;
95     d->decimals = 2;
96     d->container = new KoSliderComboContainer(this);
97     d->container->setAttribute(Qt::WA_WindowPropagation);
98     QStyleOptionComboBox opt;
99     opt.init(this);
100 //    d->container->setFrameStyle(style()->styleHint(QStyle::SH_ComboBox_PopupFrameStyle, &opt, this));
101
102     d->slider = new QSlider(Qt::Horizontal);
103     d->slider->setMinimum(0);
104     d->slider->setMaximum(256);
105     d->slider->setPageStep(10);
106     d->slider->setValue(0);
107     // When set to true, causes flicker on Qt 4.6. Any reason to keep it?
108     d->firstShowOfSlider = false; //true;
109
110     QHBoxLayout * l = new QHBoxLayout();
111     l->setMargin(2);
112     l->setSpacing(2);
113     l->addWidget(d->slider);
114     d->container->setLayout(l);
115     d->container->resize(200, 30);
116
117     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
118
119     setEditable(true);
120     setEditText(KGlobal::locale()->formatNumber(0, d->decimals));
121
122     connect(d->slider, SIGNAL(valueChanged(int)), SLOT(sliderValueChanged(int)));
123     connect(d->slider, SIGNAL(sliderReleased()), SLOT(sliderReleased()));
124     connect(lineEdit(), SIGNAL(editingFinished()), SLOT(lineEditFinished()));
125 }
126
127 KoSliderCombo::~KoSliderCombo()
128 {
129     delete d;
130 }
131
132 QSize KoSliderCombo::sizeHint() const
133 {
134     return minimumSizeHint();
135 }
136
137 QSize KoSliderCombo::minimumSizeHint() const
138 {
139     QSize sh;
140
141     const QFontMetrics &fm = fontMetrics();
142
143     sh.setWidth(5 * fm.width(QLatin1Char('8')));
144     sh.setHeight(qMax(fm.lineSpacing(), 14) + 2);
145
146     // add style and strut values
147     QStyleOptionComboBox opt;
148     opt.init(this);
149     opt.subControls = QStyle::SC_All;
150     opt.editable = true;
151     sh = style()->sizeFromContents(QStyle::CT_ComboBox, &opt, sh, this);
152
153     return sh.expandedTo(QApplication::globalStrut());
154 }
155
156 void KoSliderCombo::KoSliderComboPrivate::showPopup()
157 {
158     if(firstShowOfSlider) {
159         container->show(); //show container a bit early so the slider can be layout'ed
160         firstShowOfSlider = false;
161     }
162
163     QStyleOptionSlider opt;
164     opt.init(slider);
165     opt.maximum=256;
166     opt.sliderPosition = opt.sliderValue = slider->value();
167     int hdlPos = thePublic->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle).center().x();
168
169     QStyleOptionComboBox optThis;
170     optThis.init(thePublic);
171     optThis.subControls = QStyle::SC_All;
172     optThis.editable = true;
173     int arrowPos = thePublic->style()->subControlRect(QStyle::CC_ComboBox, &optThis, QStyle::SC_ComboBoxArrow).center().x();
174
175     QSize popSize = container->size();
176     QRect popupRect(thePublic->mapToGlobal(QPoint(arrowPos - hdlPos - slider->x(), thePublic->size().height())), popSize);
177
178     // Make sure the popup is not drawn outside the screen area
179     QRect screenRect = QApplication::desktop()->availableGeometry(container);
180     if (popupRect.right() > screenRect.right())
181         popupRect.translate(screenRect.right() - popupRect.right(), 0);
182     if (popupRect.left() < screenRect.left())
183         popupRect.translate(screenRect.left() - popupRect.left(), 0);
184     if (popupRect.bottom() > screenRect.bottom())
185         popupRect.translate(0, -(thePublic->height() + container->height()));
186
187     container->setGeometry(popupRect);
188     container->raise();
189     container->show();
190     slider->setFocus();
191 }
192
193 void KoSliderCombo::KoSliderComboPrivate::hidePopup()
194 {
195     container->hide();
196 }
197
198 void KoSliderCombo::hideEvent(QHideEvent *)
199 {
200     d->hidePopup();
201 }
202
203 void KoSliderCombo::changeEvent(QEvent *e)
204 {
205     switch (e->type())
206     {
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     {
244         d->showPopup();
245     }
246     else
247         QComboBox::mousePressEvent(e);
248 }
249
250 void KoSliderCombo::keyPressEvent(QKeyEvent *e)
251 {
252     if (e->key() == Qt::Key_Up) setValue(value() + d->slider->singleStep() * maximum() / 256 + 0.5);
253     else if (e->key() == Qt::Key_Down) setValue(value() - d->slider->singleStep() * maximum() / 256 - 0.5);
254     else QComboBox::keyPressEvent(e);
255 }
256
257 void KoSliderCombo::wheelEvent(QWheelEvent *e)
258 {
259     if (e->delta() > 0) setValue(value() + d->slider->singleStep() * maximum() / 256 + 0.5);
260     else setValue(value() - d->slider->singleStep() * maximum() / 256 - 0.5);
261 }
262
263 void KoSliderCombo::KoSliderComboPrivate::lineEditFinished()
264 {
265     qreal value = thePublic->currentText().toDouble();
266     slider->blockSignals(true);
267     slider->setValue(int((value - minimum) * 256 / maximum + 0.5));
268     slider->blockSignals(false);
269     emit thePublic->valueChanged(value, true);
270 }
271
272 void KoSliderCombo::KoSliderComboPrivate::sliderValueChanged(int slidervalue)
273 {
274     thePublic->setEditText(KGlobal::locale()->formatNumber(minimum + maximum*slidervalue/256, decimals));
275
276     qreal value = thePublic->currentText().toDouble();
277     emit thePublic->valueChanged(value, false);
278 }
279
280 void KoSliderCombo::KoSliderComboPrivate::sliderReleased()
281 {
282     qreal value = thePublic->currentText().toDouble();
283     emit thePublic->valueChanged(value, true);
284 }
285
286 qreal KoSliderCombo::maximum() const
287 {
288     return d->maximum;
289 }
290
291 qreal KoSliderCombo::minimum() const
292 {
293     return d->minimum;
294 }
295
296 qreal KoSliderCombo::decimals() const
297 {
298     return d->decimals;
299 }
300
301 qreal KoSliderCombo::value() const
302 {
303     return currentText().toDouble();
304 }
305
306 void KoSliderCombo::setDecimals(int dec)
307 {
308     d->decimals = dec;
309     if (dec == 0) lineEdit()->setValidator(new QIntValidator(this));
310     else lineEdit()->setValidator(new QDoubleValidator(this));
311 }
312
313 void KoSliderCombo::setMinimum(qreal min)
314 {
315     d->minimum = min;
316 }
317
318 void KoSliderCombo::setMaximum(qreal max)
319 {
320     d->maximum = max;
321 }
322
323 void KoSliderCombo::setValue(qreal value)
324 {
325     if(value < d->minimum)
326         value = d->minimum;
327     if(value > d->maximum)
328         value = d->maximum;
329     setEditText(KGlobal::locale()->formatNumber(value, d->decimals));
330     d->slider->blockSignals(true);
331     d->slider->setValue(int((value - d->minimum) * 256 / d->maximum + 0.5));
332     d->slider->blockSignals(false);
333     emit valueChanged(value, true);
334 }
335
336 #include <KoSliderCombo.moc>