1 /***************************************************************************
2 * Copyright (C) 2011 by Till Theato (root@ttill.de) *
3 * Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This file is part of Kdenlive (www.kdenlive.org). *
6 * Kdenlive is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 2 of the License, or *
9 * (at your option) any later version. *
11 * Kdenlive is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with Kdenlive. If not, see <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
20 #include "dragvalue.h"
21 #include "kdenlivesettings.h"
25 #include <QHBoxLayout>
26 #include <QToolButton>
28 #include <QDoubleValidator>
29 #include <QIntValidator>
30 #include <QMouseEvent>
31 #include <QFocusEvent>
32 #include <QWheelEvent>
33 #include <QApplication>
42 #include <KLocalizedString>
43 #include <KGlobalSettings>
46 DragValue::DragValue(const QString &label, double defaultValue, int decimals, double min, double max, int id, const QString suffix, bool showSlider, QWidget* parent) :
51 m_default(defaultValue),
56 if (showSlider) setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
57 else setSizePolicy(QSizePolicy::Maximum, QSizePolicy::MinimumExpanding);
58 setFocusPolicy(Qt::StrongFocus);
59 setContextMenuPolicy(Qt::CustomContextMenu);
61 QHBoxLayout *l = new QHBoxLayout;
63 l->setContentsMargins(0, 0, 0, 0);
64 m_label = new CustomLabel(label, showSlider, m_maximum - m_minimum, this);
65 l->addWidget(m_label);
67 m_label->setMaximum(max - min);
69 m_intEdit = new QSpinBox(this);
70 m_intEdit->setObjectName("dragBox");
71 if (!suffix.isEmpty()) m_intEdit->setSuffix(suffix);
72 m_intEdit->setKeyboardTracking(false);
73 m_intEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
74 m_intEdit->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
75 m_intEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
76 m_intEdit->setRange((int) m_minimum, (int)m_maximum);
77 l->addWidget(m_intEdit);
78 connect(m_intEdit, SIGNAL(valueChanged(int)), this, SLOT(slotSetValue(int)));
79 connect(m_intEdit, SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
82 m_doubleEdit = new QDoubleSpinBox(this);
83 m_doubleEdit->setDecimals(decimals);
84 m_doubleEdit->setObjectName("dragBox");
85 if (!suffix.isEmpty()) m_doubleEdit->setSuffix(suffix);
86 m_doubleEdit->setKeyboardTracking(false);
87 m_doubleEdit->setButtonSymbols(QAbstractSpinBox::NoButtons);
88 m_doubleEdit->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
89 m_doubleEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
90 m_doubleEdit->setRange(m_minimum, m_maximum);
92 if (m_maximum - m_minimum > 10000) factor = 1000;
94 m_doubleEdit->setSingleStep((m_maximum - m_minimum) / factor);
95 l->addWidget(m_doubleEdit);
96 connect(m_doubleEdit, SIGNAL(valueChanged(double)), this, SLOT(slotSetValue(double)));
97 connect(m_doubleEdit, SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
100 connect(m_label, SIGNAL(valueChanged(double,bool)), this, SLOT(setValueFromProgress(double,bool)));
101 connect(m_label, SIGNAL(resetValue()), this, SLOT(slotReset()));
103 if (m_intEdit) m_label->setMaximumHeight(m_intEdit->sizeHint().height());
104 else m_label->setMaximumHeight(m_doubleEdit->sizeHint().height());
106 m_menu = new QMenu(this);
108 m_scale = new KSelectAction(i18n("Scaling"), this);
109 m_scale->addAction(i18n("Normal scale"));
110 m_scale->addAction(i18n("Pixel scale"));
111 m_scale->addAction(i18n("Nonlinear scale"));
112 m_scale->setCurrentItem(KdenliveSettings::dragvalue_mode());
113 m_menu->addAction(m_scale);
115 m_directUpdate = new QAction(i18n("Direct update"), this);
116 m_directUpdate->setCheckable(true);
117 m_directUpdate->setChecked(KdenliveSettings::dragvalue_directupdate());
118 m_menu->addAction(m_directUpdate);
120 QAction *reset = new QAction(KIcon("edit-undo"), i18n("Reset value"), this);
121 connect(reset, SIGNAL(triggered()), this, SLOT(slotReset()));
122 m_menu->addAction(reset);
125 QAction *timeline = new QAction(KIcon("go-jump"), i18n("Show %1 in timeline", label), this);
126 connect(timeline, SIGNAL(triggered()), this, SLOT(slotSetInTimeline()));
127 connect(m_label, SIGNAL(setInTimeline()), this, SLOT(slotSetInTimeline()));
128 m_menu->addAction(timeline);
131 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(slotShowContextMenu(const QPoint&)));
132 connect(m_scale, SIGNAL(triggered(int)), this, SLOT(slotSetScaleMode(int)));
133 connect(m_directUpdate, SIGNAL(triggered(bool)), this, SLOT(slotSetDirectUpdate(bool)));
137 DragValue::~DragValue()
139 if (m_intEdit) delete m_intEdit;
140 if (m_doubleEdit) delete m_doubleEdit;
143 delete m_directUpdate;
146 int DragValue::spinSize()
148 if (m_intEdit) return m_intEdit->sizeHint().width();
149 else return m_doubleEdit->sizeHint().width();
152 void DragValue::setSpinSize(int width)
154 if (m_intEdit) m_intEdit->setMinimumWidth(width);
155 else m_doubleEdit->setMinimumWidth(width);
158 void DragValue::slotSetInTimeline()
160 emit inTimeline(m_id);
163 int DragValue::precision() const
168 qreal DragValue::maximum() const
173 qreal DragValue::minimum() const
178 qreal DragValue::value() const
180 if (m_intEdit) return m_intEdit->value();
181 else return m_doubleEdit->value();
184 void DragValue::setMaximum(qreal max)
187 if (m_intEdit) m_intEdit->setRange(m_minimum, m_maximum);
188 else m_doubleEdit->setRange(m_minimum, m_maximum);
191 void DragValue::setMinimum(qreal min)
194 if (m_intEdit) m_intEdit->setRange(m_minimum, m_maximum);
195 else m_doubleEdit->setRange(m_minimum, m_maximum);
198 void DragValue::setRange(qreal min, qreal max)
202 if (m_intEdit) m_intEdit->setRange(m_minimum, m_maximum);
203 else m_doubleEdit->setRange(m_minimum, m_maximum);
206 void DragValue::setPrecision(int /*precision*/)
208 //TODO: Not implemented, in case we need double value, we should replace the KIntSpinBox with KDoubleNumInput...
209 /*m_precision = precision;
211 m_edit->setValidator(new QIntValidator(m_minimum, m_maximum, this));
213 m_edit->setValidator(new QDoubleValidator(m_minimum, m_maximum, precision, this));*/
216 void DragValue::setStep(qreal step)
219 m_intEdit->setSingleStep(step);
221 m_doubleEdit->setSingleStep(step);
224 void DragValue::slotReset()
227 m_intEdit->blockSignals(true);
228 m_intEdit->setValue(m_default);
229 m_intEdit->blockSignals(false);
230 emit valueChanged((int) m_default, true);
233 m_doubleEdit->blockSignals(true);
234 m_doubleEdit->setValue(m_default);
235 m_doubleEdit->blockSignals(false);
236 emit valueChanged(m_default, true);
238 m_label->setProgressValue((m_default - m_minimum) / (m_maximum - m_minimum) * m_label->maximum());
241 void DragValue::slotSetValue(int value)
243 setValue(value, KdenliveSettings::dragvalue_directupdate());
246 void DragValue::slotSetValue(double value)
248 setValue(value, KdenliveSettings::dragvalue_directupdate());
251 void DragValue::setValueFromProgress(double value, bool final)
253 value = m_minimum + value * (m_maximum - m_minimum) / m_label->maximum();
254 if (m_decimals == 0) setValue(qRound(value), final);
255 else setValue(value, final);
258 void DragValue::setValue(double value, bool final)
260 value = qBound(m_minimum, value, m_maximum);
262 m_intEdit->blockSignals(true);
263 m_intEdit->setValue((int) value);
264 m_intEdit->blockSignals(false);
265 emit valueChanged((int) value, final);
268 m_doubleEdit->blockSignals(true);
269 m_doubleEdit->setValue(value);
270 m_doubleEdit->blockSignals(false);
271 emit valueChanged(value, final);
274 m_label->setProgressValue((value - m_minimum) / (m_maximum - m_minimum) * m_label->maximum());
277 void DragValue::focusInEvent(QFocusEvent* e)
279 if (e->reason() == Qt::TabFocusReason || e->reason() == Qt::BacktabFocusReason) {
280 if (m_intEdit) m_intEdit->setFocus(e->reason());
281 else m_doubleEdit->setFocus(e->reason());
283 QWidget::focusInEvent(e);
287 void DragValue::slotEditingFinished()
290 int value = m_intEdit->value();
291 m_intEdit->blockSignals(true);
292 m_intEdit->clearFocus();
293 m_intEdit->blockSignals(false);
294 if (!KdenliveSettings::dragvalue_directupdate()) emit valueChanged(value, true);
297 double value = m_doubleEdit->value();
298 m_doubleEdit->blockSignals(true);
299 m_doubleEdit->clearFocus();
300 m_doubleEdit->blockSignals(false);
301 if (!KdenliveSettings::dragvalue_directupdate()) emit valueChanged(value, true);
305 void DragValue::slotShowContextMenu(const QPoint& pos)
307 // values might have been changed by another object of this class
308 m_scale->setCurrentItem(KdenliveSettings::dragvalue_mode());
309 m_directUpdate->setChecked(KdenliveSettings::dragvalue_directupdate());
310 m_menu->exec(mapToGlobal(pos));
313 void DragValue::slotSetScaleMode(int mode)
315 KdenliveSettings::setDragvalue_mode(mode);
318 void DragValue::slotSetDirectUpdate(bool directUpdate)
320 KdenliveSettings::setDragvalue_directupdate(directUpdate);
323 void DragValue::setInTimelineProperty(bool intimeline)
325 if (m_label->property("inTimeline").toBool() == intimeline) return;
326 m_label->setProperty("inTimeline", intimeline);
327 style()->unpolish(m_label);
328 style()->polish(m_label);
331 m_intEdit->setProperty("inTimeline", intimeline);
332 style()->unpolish(m_intEdit);
333 style()->polish(m_intEdit);
337 m_doubleEdit->setProperty("inTimeline", intimeline);
338 style()->unpolish(m_doubleEdit);
339 style()->polish(m_doubleEdit);
340 m_doubleEdit->update();
345 CustomLabel::CustomLabel(const QString &label, bool showSlider, int range, QWidget* parent) :
346 QProgressBar(parent),
348 m_showSlider(showSlider),
350 //m_precision(pow(10, precision)),
352 setFont(KGlobalSettings::toolBarFont());
353 setFormat(" " + label);
354 setFocusPolicy(Qt::ClickFocus);
355 setCursor(Qt::PointingHandCursor);
356 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
357 if (showSlider) setRange(0, 1000);
361 const QFontMetrics &fm = fontMetrics();
362 sh.setWidth(fm.width(" " + label + " "));
363 setMaximumWidth(sh.width());
364 setObjectName("dragOnly");
369 void CustomLabel::mousePressEvent(QMouseEvent* e)
371 if (e->button() == Qt::LeftButton) {
372 m_dragStartPosition = m_dragLastPosition = e->pos();
375 else if (e->button() == Qt::MidButton) {
377 m_dragStartPosition = QPoint(-1, -1);
379 else QWidget::mousePressEvent(e);
382 void CustomLabel::mouseMoveEvent(QMouseEvent* e)
384 if (m_dragStartPosition != QPoint(-1, -1)) {
385 if (!m_dragMode && (e->pos() - m_dragStartPosition).manhattanLength() >= QApplication::startDragDistance()) {
387 m_dragLastPosition = e->pos();
392 if (KdenliveSettings::dragvalue_mode() > 0 || !m_showSlider) {
393 int diff = e->x() - m_dragLastPosition.x();
395 if (e->modifiers() == Qt::ControlModifier)
397 else if (e->modifiers() == Qt::ShiftModifier)
399 if (KdenliveSettings::dragvalue_mode() == 2)
400 diff = (diff > 0 ? 1 : -1) * pow(diff, 2);
402 double nv = value() + diff * m_step;
403 if (nv != value()) setNewValue(nv, KdenliveSettings::dragvalue_directupdate());
406 double nv = minimum() + ((double) maximum() - minimum()) / width() * e->pos().x();
407 if (nv != value()) setNewValue(nv, KdenliveSettings::dragvalue_directupdate());
409 m_dragLastPosition = e->pos();
413 else QWidget::mouseMoveEvent(e);
416 void CustomLabel::mouseReleaseEvent(QMouseEvent* e)
418 if (e->button() == Qt::MidButton) {
422 if (e->modifiers() == Qt::ControlModifier) {
423 emit setInTimeline();
428 setNewValue(value(), true);
429 m_dragLastPosition = m_dragStartPosition;
432 else if (m_showSlider) {
433 setNewValue((double) maximum() * e->pos().x() / width(), true);
434 m_dragLastPosition = m_dragStartPosition;
440 void CustomLabel::wheelEvent(QWheelEvent* e)
442 if (e->delta() > 0) {
443 if (e->modifiers() == Qt::ControlModifier) slotValueInc(10);
444 else if (e->modifiers() == Qt::AltModifier) slotValueInc(0.1);
448 if (e->modifiers() == Qt::ControlModifier) slotValueDec(10);
449 else if (e->modifiers() == Qt::AltModifier) slotValueDec(0.1);
455 void CustomLabel::slotValueInc(double factor)
457 setNewValue(value() + m_step * factor, true);
460 void CustomLabel::slotValueDec(double factor)
462 setNewValue(value() - m_step * factor, true);
465 void CustomLabel::setProgressValue(double value)
467 setValue(qRound(value));
470 void CustomLabel::setNewValue(double value, bool update)
472 setValue(qRound(value));
473 emit valueChanged(qRound(value), update);
476 void CustomLabel::setStep(double step)
481 #include "dragvalue.moc"