]> git.sesse.net Git - kdenlive/blob - src/doubleparameterwidget.cpp
Add "drag value" widget: Numbers can be edited by dragging horizontal on the widget...
[kdenlive] / src / doubleparameterwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
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                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "doubleparameterwidget.h"
22 #include "dragvalue.h"
23
24 #include <QGridLayout>
25 #include <QLabel>
26 #include <QSlider>
27 #include <QSpinBox>
28 #include <QToolButton>
29
30 #include <KIcon>
31 #include <KLocalizedString>
32
33
34 DoubleParameterWidget::DoubleParameterWidget(const QString &name, int value, int min, int max, int defaultValue, const QString &comment, const QString suffix, QWidget *parent) :
35         QWidget(parent),
36         m_default(defaultValue)
37 {
38     QGridLayout *layout = new QGridLayout(this);
39     layout->setContentsMargins(0, 0, 0, 0);
40     layout->setSpacing(0);
41
42     m_name = new QLabel(name, this);
43     layout->addWidget(m_name, 0, 0);
44
45     m_slider = new QSlider(Qt::Horizontal, this);
46     m_slider->setRange(min, max);
47     //m_slider->setPageStep((max - min) / 10);
48     layout->addWidget(m_slider, 0, 1);
49
50     m_dragVal = new DragValue(this);
51     m_dragVal->setRange(min, max);
52     m_dragVal->setPrecision(0);
53     layout->addWidget(m_dragVal, 0, 2);
54
55     m_spinBox = new QSpinBox(this);
56     m_spinBox->setRange(min, max);
57     m_spinBox->setKeyboardTracking(false);
58     if (!suffix.isEmpty())
59         m_spinBox->setSuffix(suffix);
60     //layout->addWidget(m_spinBox, 0, 2);
61     m_spinBox->setHidden(true);
62
63     QToolButton *reset = new QToolButton(this);
64     reset->setAutoRaise(true);
65     reset->setIcon(KIcon("edit-undo"));
66     reset->setToolTip(i18n("Reset to default value"));
67     layout->addWidget(reset, 0, 3);
68
69     m_commentLabel = new QLabel(comment, this);
70     m_commentLabel->setWordWrap(true);
71     m_commentLabel->setTextFormat(Qt::RichText);
72     m_commentLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
73     m_commentLabel->setFrameShape(QFrame::StyledPanel);
74     m_commentLabel->setFrameShadow(QFrame::Raised);
75     m_commentLabel->setHidden(true);
76     layout->addWidget(m_commentLabel, 1, 0, 1, -1);
77
78     connect(m_slider,  SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
79     connect(m_dragVal, SIGNAL(valueChanged(qreal, bool)), this, SLOT(slotSetValue(qreal, bool)));
80     connect(m_spinBox, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
81     connect(reset,     SIGNAL(clicked(bool)),     this, SLOT(slotReset()));
82
83     //m_spinBox->setValue(value);
84     m_dragVal->setValue(value);
85 }
86
87 void DoubleParameterWidget::setValue(int value)
88 {
89     m_slider->blockSignals(true);
90     m_spinBox->blockSignals(true);
91     m_dragVal->blockSignals(true);
92
93     m_slider->setValue(value);
94     m_spinBox->setValue(value);
95     m_dragVal->setValue(value);
96
97     m_slider->blockSignals(false);
98     m_spinBox->blockSignals(false);
99     m_dragVal->blockSignals(false);
100
101     emit valueChanged(value);
102 }
103
104 void DoubleParameterWidget::slotSetValue(qreal value, bool final)
105 {
106     if (final)
107         setValue((int)value);
108 }
109
110 int DoubleParameterWidget::getValue()
111 {
112     return m_spinBox->value();
113 }
114
115 void DoubleParameterWidget::setName(const QString& name)
116 {
117     m_name->setText(name);
118 }
119
120 void DoubleParameterWidget::slotReset()
121 {
122     setValue(m_default);
123 }
124
125 void DoubleParameterWidget::slotShowComment( bool show)
126 {
127     if (m_commentLabel->text() != QString()) {
128         m_commentLabel->setVisible(show);
129         if (show)
130             layout()->setContentsMargins(0, 0, 0, 15);
131         else
132             layout()->setContentsMargins(0, 0, 0, 0);
133     }
134 }
135
136 #include "doubleparameterwidget.moc"