Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
spinslider.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2017 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 
19 #include <cmath>
20 #include <QLabel>
21 #include <QSlider>
22 #include <QGridLayout>
23 #include <QLocale>
24 #include <QDebug>
25 #include <QStyle>
26 #include "spinslider.h"
27 
28 
29 SpinSlider::SpinSlider( QString text, GROWTH_TYPE type, VALUE_TYPE dataType, qreal min, qreal max, QWidget* parent ) : QWidget( parent )
30 {
31  if ( type == LOG )
32  {
33  // important! dividing by zero is not acceptable.
34  Q_ASSERT_X( min > 0.f , "SpinSlider" , "Real type value must larger than 0!!" );
35  }
36 
37  mValue = 1.0;
38  mGrowthType = type;
39  mValueType = dataType;
40  mMin = min;
41  mMax = max;
42 
43  QLabel* label = new QLabel(text+": ");
44  label->setFont( QFont( "Helvetica", 10 ) );
45 
46  mSlider = new QSlider(Qt::Horizontal, this);
47  mSlider->setMinimum( 0 );
48  mSlider->setMaximum( 100 );
49  mSlider->setMaximumWidth( 500 );
50 
51  QGridLayout* layout = new QGridLayout();
52  layout->setMargin( 2 );
53  layout->setSpacing( 2 );
54 
55  layout->addWidget( label, 0, 0, 1, 1 );
56  layout->addWidget( mSlider, 1, 0, 1, 2 );
57 
58  setLayout( layout );
59  setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
60 
61  connect( mSlider, &QSlider::valueChanged, this, &SpinSlider::onSliderValueChanged );
62 }
63 
64 void SpinSlider::changeValue(qreal value)
65 {
66  mValue = value;
67  emit valueChanged( value );
68 }
69 
70 void SpinSlider::onSliderValueChanged( int v )
71 {
72  //qDebug() << "Value changed!! " << v;
73  qreal value2 = 0.0;
74  if ( mGrowthType == LINEAR )
75  {
76  value2 = mMin + v * ( mMax - mMin ) / mSlider->maximum();
77  }
78  else if ( mGrowthType == LOG )
79  {
80  value2 = mMin * std::exp( v * std::log( mMax / mMin ) / mSlider->maximum() );
81  }
82  else if ( mGrowthType == EXPONENT ) {
83  value2 = mMin + std::pow( v, mExp ) * ( mMax - mMin ) / std::pow( mSlider->maximum(), mExp );
84  }
85  changeValue( value2 );
86 }
87 
88 void SpinSlider::setValue( qreal v )
89 {
90  //qDebug() << "setValue!!" << v;
91  int value2 = 0;
92  if ( mGrowthType == LINEAR )
93  {
94  value2 = std::round( mSlider->maximum() * ( v - mMin ) / ( mMax - mMin ) );
95  }
96  else if ( mGrowthType == LOG )
97  {
98  value2 = std::round( std::log( v / mMin ) * mSlider->maximum() / std::log( mMax / mMin ) );
99  }
100  else if ( mGrowthType == EXPONENT )
101  {
102  value2 = std::round( std::pow( ( v - mMin ) * std::pow( mSlider->maximum(), mExp ) / ( mMax - mMin ), 1 / mExp ));
103  }
104  //qDebug() << "Position! " << value2;
105 
106  changeValue( v );
107  mSlider->setSliderPosition( value2 );
108 }
109 
110 void SpinSlider::setPixelPos(qreal min, qreal max, int val, int space, bool upsideDown)
111 {
112  mSlider->setSliderPosition(QStyle::sliderValueFromPosition(min, max, val,space, upsideDown));
113 }
114 
115 void SpinSlider::setExponent( const qreal exp )
116 {
117  mExp = exp;
118 }