COPASI API  4.16.103
scrollbar.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2006 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 // Copyright 1997 Josef Wilgen
16 // Copyright 2002 Uwe Rathmann
17 //
18 // This file is published under the Qwt License, Version 1.0.
19 // You should have received a copy of this licence in the file
20 // QwtLicense.
21 //
22 // Modifications made to the original are
23 #include <QtGui/QStyle>
24 #if QT_VERSION >= 0x040000
25 #include <QtGui/QStyleOption>
26 #endif
27 #include "scrollbar.h"
28 
29 #include <cmath>
30 
31 ScrollBar::ScrollBar(QWidget * parent):
32  QScrollBar(parent),
33  mLogScale(false)
34 {
35  init();
36 }
37 
38 ScrollBar::ScrollBar(Qt::Orientation o,
39  QWidget *parent):
40  QScrollBar(o, parent),
41  mLogScale(false)
42 {
43  init();
44 }
45 
46 ScrollBar::ScrollBar(double minBase, double maxBase, bool logscale,
47  Qt::Orientation o, QWidget *parent):
48  QScrollBar(o, parent),
49  mLogScale(logscale)
50 {
51  init();
52  setBase(minBase, maxBase);
53  moveSlider(minBase, maxBase);
54 }
55 
57 {
58  d_inverted = orientation() == Qt::Vertical;
59  d_baseTicks = 1000000;
60  d_minBase = 0.0;
61  d_maxBase = 1.0;
63 
64  connect(this, SIGNAL(sliderMoved(int)), SLOT(catchSliderMoved(int)));
65  connect(this, SIGNAL(valueChanged(int)), SLOT(catchValueChanged(int)));
66 }
67 
68 void ScrollBar::setInverted(bool inverted)
69 {
70  if (d_inverted != inverted)
71  {
72  d_inverted = inverted;
74  }
75 }
76 
78 {
79  return d_inverted;
80 }
81 
82 void ScrollBar::setBase(double min, double max)
83 {
84  if (mLogScale)
85  {
86  min = log(min);
87  max = log(max);
88  }
89 
90  if (min != d_minBase || max != d_maxBase)
91  {
92  d_minBase = min;
93  d_maxBase = max;
94 
96  }
97 }
98 
99 void ScrollBar::moveSlider(double min, double max)
100 {
101  if (mLogScale)
102  {
103  min = log(min);
104  max = log(max);
105  }
106 
107  int sliderTicks;
108  sliderTicks = qRound((max - min) /
110 
111  // setRange initiates a valueChanged of the scrollbars
112  // in some situations. So we block
113  // and unblock the signals.
114 
115  blockSignals(true);
116 
117  setRange(sliderTicks / 2, d_baseTicks - sliderTicks / 2);
118  int steps = sliderTicks / 200;
119 
120  if (steps <= 0)
121  steps = 1;
122 
123 #if QT_VERSION < 0x040000
124  setSteps(steps, sliderTicks);
125 #else
126  setSingleStep(steps);
127  setPageStep(sliderTicks);
128 #endif
129 
130  int tick;
131  tick = mapToTick(min + (max - min) / 2);
132 
133  if (isInverted())
134  tick = d_baseTicks - tick;
135 
136 #if QT_VERSION < 0x040000
137  directSetValue(tick);
138  rangeChange();
139 #else
140  setSliderPosition(tick);
141 #endif
142  blockSignals(false);
143 }
144 
146 {
147  if (mLogScale)
148  return exp(d_minBase);
149  else
150  return d_minBase;
151 }
152 
154 {
155  if (mLogScale)
156  return exp(d_maxBase);
157  else
158  return d_maxBase;
159 }
160 
161 void ScrollBar::sliderRange(int value, double &min, double &max) const
162 {
163  if (isInverted())
164  value = d_baseTicks - value;
165 
166  const int visibleTicks = pageStep();
167 
168  min = mapFromTick(value - visibleTicks / 2);
169  max = mapFromTick(value + visibleTicks / 2);
170 
171  if (mLogScale)
172  {
173  min = exp(min);
174  max = exp(max);
175  }
176 }
177 
179 {
180  double min, dummy;
181  sliderRange(value(), min, dummy);
182 
183  return min;
184 }
185 
187 {
188  double max, dummy;
189  sliderRange(value(), dummy, max);
190 
191  return max;
192 }
193 
194 int ScrollBar::mapToTick(double v) const
195 {
196  return (int)((v - d_minBase) / (d_maxBase - d_minBase) * d_baseTicks);
197 }
198 
199 double ScrollBar::mapFromTick(int tick) const
200 {
201  return d_minBase + (d_maxBase - d_minBase) * tick / d_baseTicks;
202 }
203 
205 {
206  double min, max;
207  sliderRange(value, min, max);
208  emit valueChanged(orientation(), min, max);
209 }
210 
212 {
213  double min, max;
214  sliderRange(value, min, max);
215  emit sliderMoved(orientation(), min, max);
216 }
217 
218 int ScrollBar::extent() const
219 {
220 #if QT_VERSION < 0x040000
221  return style().pixelMetric(QStyle::PM_ScrollBarExtent, this);
222 #else
223  QStyleOptionSlider opt;
224  opt.init(this);
225  opt.subControls = QStyle::SC_None;
226  opt.activeSubControls = QStyle::SC_None;
227  opt.orientation = orientation();
228  opt.minimum = minimum();
229  opt.maximum = maximum();
230  opt.sliderPosition = sliderPosition();
231  opt.sliderValue = value();
232  opt.singleStep = singleStep();
233  opt.pageStep = pageStep();
234  opt.upsideDown = invertedAppearance();
235 
236  if (orientation() == Qt::Horizontal)
237  opt.state |= QStyle::State_Horizontal;
238 
239  return style()->pixelMetric(QStyle::PM_ScrollBarExtent, &opt, this);
240 #endif
241 }
242 
244 {
245  mLogScale = l;
246 }
247 
249 {
250  return mLogScale;
251 }
double maxBaseValue() const
Definition: scrollbar.cpp:153
int extent() const
Definition: scrollbar.cpp:218
double maxSliderValue() const
Definition: scrollbar.cpp:186
void init()
Definition: scrollbar.cpp:56
void sliderRange(int value, double &min, double &max) const
Definition: scrollbar.cpp:161
double mapFromTick(int) const
Definition: scrollbar.cpp:199
int d_baseTicks
Definition: scrollbar.h:75
bool mLogScale
Definition: scrollbar.h:76
bool d_inverted
Definition: scrollbar.h:72
void setInverted(bool)
Definition: scrollbar.cpp:68
void sliderMoved(Qt::Orientation, double, double)
double minSliderValue() const
Definition: scrollbar.cpp:178
bool isLogScale() const
Definition: scrollbar.cpp:248
void catchValueChanged(int value)
Definition: scrollbar.cpp:204
bool isInverted() const
Definition: scrollbar.cpp:77
void setLogScale(bool l)
Definition: scrollbar.cpp:243
void catchSliderMoved(int value)
Definition: scrollbar.cpp:211
virtual void moveSlider(double min, double max)
Definition: scrollbar.cpp:99
int mapToTick(double) const
Definition: scrollbar.cpp:194
ScrollBar(QWidget *parent=NULL)
Definition: scrollbar.cpp:31
double d_minBase
Definition: scrollbar.h:73
double d_maxBase
Definition: scrollbar.h:74
void valueChanged(Qt::Orientation, double, double)
virtual void setBase(double min, double max)
Definition: scrollbar.cpp:82
#define min(a, b)
Definition: f2c.h:175
double minBaseValue() const
Definition: scrollbar.cpp:145
#define max(a, b)
Definition: f2c.h:176