All Classes Namespaces Functions Variables Enumerations Properties Pages
spinslider.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program 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.
15 
16 */
17 
18 #include "spinslider.h"
19 
20 #include <cmath>
21 #include <QLabel>
22 #include <QSlider>
23 #include <QGridLayout>
24 #include <QLocale>
25 #include <QDebug>
26 #include <QStyle>
27 
28 
29 SpinSlider::SpinSlider(QWidget* parent) : QWidget(parent)
30 {}
31 
32 void SpinSlider::init(QString text, GROWTH_TYPE type, VALUE_TYPE dataType, qreal min, qreal max)
33 {
34  if (type == LOG)
35  {
36  // important! dividing by zero is not acceptable.
37  Q_ASSERT_X(min > 0.0, "SpinSlider", "Real type value must larger than 0!!");
38  }
39 
40  mValue = 1.0;
41  mGrowthType = type;
42  mValueType = dataType;
43  mMin = min;
44  mMax = max;
45 
46  mLabel = new QLabel(text + ": ");
47 
48  mSlider = new QSlider(Qt::Horizontal, this);
49  mSlider->setMinimum(0);
50  mSlider->setMaximum(100);
51  mSlider->setMaximumWidth(500);
52 
54  layout->setMargin(2);
55  layout->setSpacing(2);
56 
57  layout->addWidget(mLabel, 0, 0, 1, 1);
58  layout->addWidget(mSlider, 1, 0, 1, 2);
59 
60  setLayout(layout);
62 
63  connect(mSlider, &QSlider::valueChanged, this, &SpinSlider::onSliderValueChanged);
64 }
65 
66 void SpinSlider::changeValue(qreal value)
67 {
68  mValue = value;
69  emit valueChanged(value);
70 }
71 
72 void SpinSlider::onSliderValueChanged(int v)
73 {
74  qreal value2 = 0.0;
75  if (mGrowthType == LINEAR)
76  {
77  value2 = mMin + v * (mMax - mMin) / mSlider->maximum();
78  }
79  else if (mGrowthType == LOG)
80  {
81  value2 = mMin * std::exp(v * std::log(mMax / mMin) / mSlider->maximum());
82  }
83  else if (mGrowthType == EXPONENT) {
84  value2 = mMin + std::pow(v, mExp) * (mMax - mMin) / std::pow(mSlider->maximum(), mExp);
85  }
86  changeValue(value2);
87 }
88 
89 void SpinSlider::setLabel(QString newText)
90 {
91  mLabel->setText(newText);
92 }
93 
94 void SpinSlider::setValue(qreal v)
95 {
96  int value2 = 0;
97  if (mGrowthType == LINEAR)
98  {
99  value2 =qRound((mSlider->maximum() * (v - mMin) / (mMax - mMin)));
100  }
101  else if (mGrowthType == LOG)
102  {
103  value2 = qRound(std::log(v / mMin) * mSlider->maximum() / std::log(mMax / mMin));
104  }
105  else if (mGrowthType == EXPONENT)
106  {
107  value2 = qRound(std::pow((v - mMin) * std::pow(mSlider->maximum(), mExp) / (mMax - mMin), 1 / mExp));
108  }
109 
110  changeValue(v);
111  mSlider->setSliderPosition(value2);
112 }
113 
114 void SpinSlider::setPixelPos(qreal min, qreal max, int val, int space, bool upsideDown)
115 {
116  mSlider->setSliderPosition(QStyle::sliderValueFromPosition(static_cast<int>(min),
117  static_cast<int>(max), val, space, upsideDown));
118 }
119 
120 void SpinSlider::setExponent(const qreal exp)
121 {
122  mExp = exp;
123 }
QLayout * layout() const const
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void setSpacing(int spacing)
void setLayout(QLayout *layout)
void setSliderPosition(int)
void setText(const QString &)
void setMinimum(int)
void setSizePolicy(QSizePolicy)
int sliderValueFromPosition(int min, int max, int position, int span, bool upsideDown)
void setMaximumWidth(int maxw)
void valueChanged(int value)
Horizontal
void setMaximum(int)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setMargin(int margin)