CFEL - ASG Software Suite  2.5.0
CASS
minmax_control.cpp
Go to the documentation of this file.
1 // Copyright (C) 2013 Lutz Foucar
2 
3 /**
4  * @file minmax_control.cpp contains a control over min and max values
5  *
6  * @author Lutz Foucar
7  */
8 
9 #include <cmath>
10 
11 #include <QtCore/QSettings>
12 #include <QtCore/QDebug>
13 
14 #if QT_VERSION >= 0x050000
15 #include <QtWidgets/QHBoxLayout>
16 #include <QtWidgets/QCheckBox>
17 #include <QtWidgets/QLineEdit>
18 #include <QtWidgets/QLabel>
19 #include <QtWidgets/QToolButton>
20 #include <QtWidgets/QToolBar>
21 #else
22 #include <QtGui/QHBoxLayout>
23 #include <QtGui/QCheckBox>
24 #include <QtGui/QLineEdit>
25 #include <QtGui/QLabel>
26 #include <QtGui/QToolButton>
27 #include <QtGui/QToolBar>
28 #endif
29 #include <QtGui/QDoubleValidator>
30 
31 #include "minmax_control.h"
32 
33 using namespace jocassview;
34 
36  : QWidget(parent)
37 {
38  // set the title of this widget
39  setWindowTitle(title);
40 
41  // the object to load the settings from
42  QSettings settings;
43  settings.beginGroup(windowTitle());
44 
45  // generate a vertical layout that will hold the controls
47  layout->setContentsMargins(0,0,0,0);
48 
49  // create a button that toggles between log and linear scale
50  _log = new QToolButton(this);
51  _log->setIcon(QIcon(":images/log.png"));
52  _log->setCheckable(true);
53  _log->setChecked(settings.value("LogScale",false).toBool());
54  _log->setToolTip(title + tr(": Plot the axis in logrithmic scale"));
55  _log->setToolButtonStyle(parent->toolButtonStyle());
56  _log->setIconSize(parent->iconSize());
57  _log->setAutoRaise(true);
58  connect(_log,SIGNAL(toggled(bool)),this,SLOT(on_changed()));
59  layout->addWidget(_log);
60 
61  // generate the checkbox to enable manual control
62  _auto = new QToolButton(this);
63  _auto->setIcon(QIcon(":images/autoscale.png"));
64  _auto->setCheckable(true);
65  _auto->setChecked(settings.value("AutoScale",true).toBool());
66  _auto->setToolTip(title + tr(": Toggle manual setting the minimum and maximum value of the scale"));
67  _auto->setToolButtonStyle(parent->toolButtonStyle());
68  _auto->setIconSize(parent->iconSize());
69  _auto->setAutoRaise(true);
70  connect(_auto,SIGNAL(toggled(bool)),this,SLOT(on_changed()));
71  layout->addWidget(_auto);
72 
73  // generate a validator to ensure that only numbers are entered in the input
74  QDoubleValidator *doubleValidator(new QDoubleValidator(-2e12,2e12,6,this));
75 
76  // generate the min input
77 // QLabel *minlabel(new QLabel(tr("Min"),this));
78 // layout->addWidget(minlabel);
79  _mininput = new QLineEdit(this);
80  _mininput->setValidator(doubleValidator);
81  _mininput->setText(settings.value("MinValue","0").toString());
82  _mininput->setMaximumWidth(120);
83  _mininput->setToolTip(title + tr(": Minimum axis value"));
84  connect(_mininput,SIGNAL(textChanged(QString)),this,SLOT(on_changed()));
85  layout->addWidget(_mininput);
86 
87  // generate the min input
88 // QLabel *maxlabel(new QLabel(tr("Max"),this));
89 // layout->addWidget(maxlabel);
90  _maxinput = new QLineEdit(this);
91  _maxinput->setValidator(doubleValidator);
92  _maxinput->setText(settings.value("MaxValue","1").toString());
93  _maxinput->setMaximumWidth(120);
94  _maxinput->setToolTip(title + tr(": Maximum axis value"));
95  connect(_maxinput,SIGNAL(textChanged(QString)),this,SLOT(on_changed()));
96  layout->addWidget(_maxinput);
97 
98  settings.endGroup();
99 
100  // set up the control
101  on_changed();
102 
103  // set the layout of this widget
104  setLayout(layout);
105 }
106 
108 {
109  if(max() <= min() || (log() && (!std::isfinite(std::log10(min())) ||
110  !std::isfinite(std::log10(max())))))
111  {
112  _mininput->setStyleSheet("QLineEdit {color: blue; background-color: #FF0000}");
113  _maxinput->setStyleSheet("QLineEdit {color: blue; background-color: #FF0000}");
114  }
115  else
116  {
117  _mininput->setStyleSheet("QLineEdit {color: black; background-color: #FFFFFF}");
118  _maxinput->setStyleSheet("QLineEdit {color: black; background-color: #FFFFFF}");
119  }
120 
121 
122  // save the states
123  QSettings settings;
124  settings.beginGroup(windowTitle());
125  settings.setValue("LogScale",log());
126  settings.setValue("AutoScale",autoscale());
127  settings.setValue("MinValue",min());
128  settings.setValue("MaxValue",max());
129 
130  settings.endGroup();
131 
132  // tell others that something has changed
133  emit controls_changed();
134 }
135 
137 {
138  return _auto->isChecked();
139 }
140 
141 bool MinMaxControl::log() const
142 {
143  return _log->isChecked();
144 }
145 
146 double MinMaxControl::min() const
147 {
148  return _mininput->text().toDouble();
149 }
150 
151 double MinMaxControl::max() const
152 {
153  return _maxinput->text().toDouble();
154 }
QLineEdit * _mininput
the minimum value input
void on_changed()
react on when one of the inputs changed
QToolButton * _auto
select manual input
bool log() const
return wether log is enabled
setValue(const QString &key, const QVariant &value)
MinMaxControl(QString title, QToolBar *parent)
constructor
setLayout(QLayout *layout)
bool autoscale() const
return whether the plot should be autoscaled
QToolButton * _log
select manual input
void controls_changed()
signal is emitted when one of the controls have changed
value(const QString &key, const QVariant &defaultValue=QVariant()
double max() const
retieve the maximum value
contains a control over min and max values
QLineEdit * _maxinput
the maximum value input
double min() const
retrieve the minimum value
setValidator(const QValidator *v)
beginGroup(const QString &prefix)