JKQtExtras
a library of useful Qt widgets and tools
jkqtedoubleedit.h
1 /*
2  Copyright (c) 2008-2020 Jan W. Krieger (<jan@jkrieger.de>)
3 
4  This software is free software: you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License (LGPL) as published by
6  the Free Software Foundation, either version 2.1 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 Lesser General Public License (LGPL) for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License (LGPL)
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef JKTEDOUBLEEDIT_H
19 #define JKTEDOUBLEEDIT_H
20 
21 #include "jkqtextras_imexport.h"
22 #include <QLineEdit>
23 #include <QRegExpValidator>
24 #include <QRegExp>
25 #include <QToolButton>
26 #include <cmath>
27 #include <QList>
28 #include <QAction>
29 #include <QPair>
30 
31 /*! \brief this QLineEdit descendent implements a validating edit field that allows to enter floating point numbers
32  with a possibly defined range
33  \ingroup JKQtExtrasWidgetsEdits
34 
35  This widget supports this set of features:
36  - This widget accepts input in standard and scientific notation.
37  - The widget can optionally check (and correct) the edit range. Checking can be switched for
38  the upper and lower bound indepently!
39  - The maximal number of decimals may be set using setDecimals(). \note Note that trailing 0s are deleted from the string.
40  - This widget accepts comma and point as decimal separator and transforms them to the systems decimal separator,
41  set in the current locale.
42  - This widget colors the background of the widget if the number you entered exceeds the range.
43  In that case the edit corrects the value when it looses its focus. The error background color may be set
44  by setErrorBackgroundColor().
45  - It is also possible to extend the default context menu with new QAction's by calling addContextmenuAction(),
46  deleteContextmenuAction() and clearContextmenuActions().
47  - This widget may also be used to catch key events. You will have to register each event using addKeyEvent().
48  If a key event matches, the keyEvent() signal will be emitted.
49  .
50 
51  Screenshot:
52  \image html JKQTEDoubleEdit.png
53 
54  Basic usage is simple:
55  \snippet jkqtextras_test/testmainwindow.cpp Example: JKQTEDoubleEdit
56 
57  */
58 class JKQTEXTRAS_LIB_EXPORT JKQTEDoubleEdit : public QLineEdit {
59  Q_OBJECT
60  Q_PROPERTY(int decimals READ decimals WRITE setDecimals USER true )
61  Q_PROPERTY(double value READ value WRITE setValue USER true )
62  Q_PROPERTY(double increment READ increment WRITE setIncrement USER true )
63  Q_PROPERTY(double maximum READ maximum WRITE setMaximum USER true )
64  Q_PROPERTY(double minimum READ minimum WRITE setMinimum USER true )
65  Q_PROPERTY(bool checkMaximum READ checkMaximum WRITE setCheckMaximum USER true )
66  Q_PROPERTY(bool checkMinimum READ checkMinimum WRITE setCheckMinimum USER true )
67  Q_PROPERTY(bool showUpDown READ showUpDown WRITE setShowUpDown USER true )
68  Q_PROPERTY(bool integerWidget READ isIntegerWidget WRITE setIntegerWidget USER true )
69  Q_PROPERTY(QColor errorBackgroundColor READ errorBackgroundColor WRITE setErrorBackgroundColor USER true )
70  Q_PROPERTY(QColor noIntegerBackgroundColor READ noIntegerBackgroundColor WRITE setNoIntegerBackgroundColor USER true )
71  Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor USER true )
72  public:
73  /** \brief class constructor */
74  JKQTEDoubleEdit(QWidget* parent);
75  /** \brief class destructor */
76  virtual ~JKQTEDoubleEdit();
77  /** \brief return the value entered in this widget */
78  double value() const;
79  /** \brief add a new QAction to the default context menu (append after a seprator) */
80  void addContextmenuAction(QAction* menuAction);
81  /** \brief remove a given QAction from the default context menu (added with addContextmenuAction() ) */
82  void deleteContextmenuAction(QAction* menuAction);
83  /** \brief remove all additional context menu QAction s */
85  /** \brief add a new Key Event */
86  void addKeyEvent(int key, Qt::KeyboardModifiers modifiers);
87  /** \brief maximum of the value range */
88  double maximum() const;
89  /** \brief minimum of the value range */
90  double minimum() const;
91  /** \brief returns whether the range maximum is checked */
92  bool checkMaximum() const;
93  /** \brief returns whether the range minimum is checked */
94  bool checkMinimum() const;
95  /** \brief returns the number of displayed decimals */
96  int decimals() const;
97  /** \brief background color used when an entry error occured (unparseable string) */
98  QColor errorBackgroundColor() const;
99  /** \brief color for the widget's background */
100  QColor backgroundColor() const;
101  /** \brief background color used when an setIntegerWidget(true), but the entered number is no integer */
102  QColor noIntegerBackgroundColor() const;
103  /** \brief increment, when using the buttons or up/down keys */
104  double increment() const;
105  /** \brief are the up/down buttons shown? */
106  bool showUpDown() const;
107  /** \brief is integer edit mode en-/disable (only integer numbers are allowed, i.e. no decimals!) */
108  bool isIntegerWidget() const;
109 
110  public slots:
111  /** \brief background color used when an setIntegerWidget(true), but the entered number is no integer */
112  void setNoIntegerBackgroundColor(const QColor &noIntegerBackgroundColor);
113  /** \brief background color used when an entry error occured (unparseable string) */
114  void setErrorBackgroundColor(const QColor& color);
115  /** \brief color for the widget's background */
116  void setBackgroundColor(const QColor& color);
117  /** \brief sets whether the range maximum is checked */
118  void setCheckMaximum(bool check);
119  /** \brief sets whether the range minimum is checked */
120  void setCheckMinimum(bool check);
121  /** \brief sets which bounds of the range to checked */
122  void setCheckBounds(bool min, bool max);
123  /** \brief en-/disable integer edit mode (only integer numbers are allowed, i.e. no decimals!) */
124  void setIntegerWidget(bool intWidget);
125  /** \brief set the value entered in this widget */
126  void setValue(double value);
127  /** \brief maximum of the value range, also calls setCheckMaximum(true) */
128  void setMaximum(double value);
129  /** \brief minimum of the value range, also calls setCheckMinimum(true) */
130  void setMinimum(double value);
131  /** \brief set the value range, also calls setCheckMaximum(true) and setCheckMinimum(true) */
132  void setRange(double min, double max);
133  /** \brief set the number of displayed decimals */
134  void setDecimals(int decimals);
135  /** \brief increase value by increment() */
136  void stepUp();
137  /** \brief decrease value by increment() */
138  void stepDown();
139  /** \brief increment, when using the buttons or up/down keys */
140  void setIncrement(double inc);
141  /** \brief sets the single step increment (equivalent to setIncrement(inc) ) */
142  void setSingleStep(double inc);
143  /** \brief sets whether up/down buttons are shown */
144  void setShowUpDown(bool showUpDown);
145  signals:
146  /** \brief emited whenever a entered value changed */
147  void valueChanged(double value);
148  /** \brief emitted when the widget looses focus */
149  void focusOut(double value);
150  /** \brief emitted when a key event matches */
151  void keyEventMatches(int key, Qt::KeyboardModifiers modifiers);
152  protected:
153  /** \brief used to store the key events */
154  QList<QPair<int, Qt::KeyboardModifiers> > m_keyEvents;
155  /** \brief list of additional actions for context menu */
156  QList<QAction*> m_contextmenuActions;
157  /** \brief regular expression used to match floating point numbers */
158  QRegExp m_regexp;
159  /** \brief RegExp validator for this widget */
160  QRegExpValidator* m_validator;
161  /** \brief hidden setValidator() method */
162  void setValidator(const QValidator* validator);
163  /** \brief error color */
164  QColor m_errorColor;
165  QColor m_background;
167  double m_maximum;
168  double m_minimum;
169  double m_increment;
174  bool m_Integer;
176  QToolButton* m_btnUp;
177  QToolButton* m_btnDown;
178  double m_current;
179  virtual void contextMenuEvent(QContextMenuEvent* event);
180  protected slots:
181  void updateWidget(const QString & text);
182  virtual void focusOutEvent ( QFocusEvent * event ) ;
183  virtual void keyPressEvent ( QKeyEvent * event ) ;
184  virtual void resizeEvent ( QResizeEvent * event ) ;
185  virtual void wheelEvent(QWheelEvent *event);
186 };
187 
188 #endif // JKTEDOUBLEEDIT_H
JKQTEDoubleEdit::m_current
double m_current
Definition: jkqtedoubleedit.h:178
JKQTEDoubleEdit::value
double value() const
return the value entered in this widget
JKQTEDoubleEdit::increment
double increment() const
increment, when using the buttons or up/down keys
JKQTEDoubleEdit::deleteContextmenuAction
void deleteContextmenuAction(QAction *menuAction)
remove a given QAction from the default context menu (added with addContextmenuAction() )
JKQTEDoubleEdit::setCheckMaximum
void setCheckMaximum(bool check)
sets whether the range maximum is checked
JKQTEDoubleEdit::setDecimals
void setDecimals(int decimals)
set the number of displayed decimals
JKQTEDoubleEdit::setValidator
void setValidator(const QValidator *validator)
hidden setValidator() method
JKQTEDoubleEdit::m_errorColor
QColor m_errorColor
error color
Definition: jkqtedoubleedit.h:164
JKQTEDoubleEdit::stepDown
void stepDown()
decrease value by increment()
JKQTEDoubleEdit::setCheckBounds
void setCheckBounds(bool min, bool max)
sets which bounds of the range to checked
JKQTEDoubleEdit::focusOutEvent
virtual void focusOutEvent(QFocusEvent *event)
JKQTEDoubleEdit::updateWidget
void updateWidget(const QString &text)
JKQTEDoubleEdit::setMinimum
void setMinimum(double value)
minimum of the value range, also calls setCheckMinimum(true)
JKQTEDoubleEdit::m_background
QColor m_background
Definition: jkqtedoubleedit.h:165
JKQTEDoubleEdit::decimals
int decimals() const
returns the number of displayed decimals
JKQTEDoubleEdit::keyEventMatches
void keyEventMatches(int key, Qt::KeyboardModifiers modifiers)
emitted when a key event matches
JKQTEDoubleEdit::~JKQTEDoubleEdit
virtual ~JKQTEDoubleEdit()
class destructor
JKQTEDoubleEdit::m_increment
double m_increment
Definition: jkqtedoubleedit.h:169
JKQTEDoubleEdit::minimum
double minimum() const
minimum of the value range
JKQTEDoubleEdit::setCheckMinimum
void setCheckMinimum(bool check)
sets whether the range minimum is checked
JKQTEDoubleEdit::addContextmenuAction
void addContextmenuAction(QAction *menuAction)
add a new QAction to the default context menu (append after a seprator)
JKQTEDoubleEdit::stepUp
void stepUp()
increase value by increment()
JKQTEDoubleEdit::JKDoubleEdit_BASIC_REGEXP
QString JKDoubleEdit_BASIC_REGEXP
Definition: jkqtedoubleedit.h:173
JKQTEXTRAS_LIB_EXPORT
#define JKQTEXTRAS_LIB_EXPORT
Definition: jkqtextras_imexport.h:95
JKQTEDoubleEdit::setSingleStep
void setSingleStep(double inc)
sets the single step increment (equivalent to setIncrement(inc) )
JKQTEDoubleEdit::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
JKQTEDoubleEdit::maximum
double maximum() const
maximum of the value range
JKQTEDoubleEdit::setRange
void setRange(double min, double max)
set the value range, also calls setCheckMaximum(true) and setCheckMinimum(true)
JKQTEDoubleEdit::m_maximum
double m_maximum
Definition: jkqtedoubleedit.h:167
JKQTEDoubleEdit::m_btnDown
QToolButton * m_btnDown
Definition: jkqtedoubleedit.h:177
JKQTEDoubleEdit::errorBackgroundColor
QColor errorBackgroundColor() const
background color used when an entry error occured (unparseable string)
JKQTEDoubleEdit::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *event)
JKQTEDoubleEdit::m_keyEvents
QList< QPair< int, Qt::KeyboardModifiers > > m_keyEvents
used to store the key events
Definition: jkqtedoubleedit.h:154
JKQTEDoubleEdit::clearContextmenuActions
void clearContextmenuActions()
remove all additional context menu QAction s
JKQTEDoubleEdit::m_validator
QRegExpValidator * m_validator
RegExp validator for this widget.
Definition: jkqtedoubleedit.h:160
JKQTEDoubleEdit::m_minimum
double m_minimum
Definition: jkqtedoubleedit.h:168
JKQTEDoubleEdit::backgroundColor
QColor backgroundColor() const
color for the widget's background
JKQTEDoubleEdit::checkMinimum
bool checkMinimum() const
returns whether the range minimum is checked
JKQTEDoubleEdit::m_btnUp
QToolButton * m_btnUp
Definition: jkqtedoubleedit.h:176
JKQTEDoubleEdit::addKeyEvent
void addKeyEvent(int key, Qt::KeyboardModifiers modifiers)
add a new Key Event
JKQTEDoubleEdit::m_regexp
QRegExp m_regexp
regular expression used to match floating point numbers
Definition: jkqtedoubleedit.h:158
JKQTEDoubleEdit::setIncrement
void setIncrement(double inc)
increment, when using the buttons or up/down keys
JKQTEDoubleEdit::valueChanged
void valueChanged(double value)
emited whenever a entered value changed
JKQTEDoubleEdit::m_noIntegerBackgroundColor
QColor m_noIntegerBackgroundColor
Definition: jkqtedoubleedit.h:166
JKQTEDoubleEdit::setIntegerWidget
void setIntegerWidget(bool intWidget)
en-/disable integer edit mode (only integer numbers are allowed, i.e. no decimals!...
JKQTEDoubleEdit::m_decimals
int m_decimals
Definition: jkqtedoubleedit.h:172
JKQTEDoubleEdit::keyPressEvent
virtual void keyPressEvent(QKeyEvent *event)
JKQTEDoubleEdit::setErrorBackgroundColor
void setErrorBackgroundColor(const QColor &color)
background color used when an entry error occured (unparseable string)
JKQTEDoubleEdit::noIntegerBackgroundColor
QColor noIntegerBackgroundColor() const
background color used when an setIntegerWidget(true), but the entered number is no integer
JKQTEDoubleEdit::checkMaximum
bool checkMaximum() const
returns whether the range maximum is checked
JKQTEDoubleEdit::m_Integer
bool m_Integer
Definition: jkqtedoubleedit.h:174
JKQTEDoubleEdit::setMaximum
void setMaximum(double value)
maximum of the value range, also calls setCheckMaximum(true)
JKQTEDoubleEdit::setNoIntegerBackgroundColor
void setNoIntegerBackgroundColor(const QColor &noIntegerBackgroundColor)
background color used when an setIntegerWidget(true), but the entered number is no integer
JKQTEDoubleEdit::setBackgroundColor
void setBackgroundColor(const QColor &color)
color for the widget's background
JKQTEDoubleEdit::m_showUpDown
bool m_showUpDown
Definition: jkqtedoubleedit.h:175
JKQTEDoubleEdit::focusOut
void focusOut(double value)
emitted when the widget looses focus
JKQTEDoubleEdit::JKQTEDoubleEdit
JKQTEDoubleEdit(QWidget *parent)
class constructor
JKQTEDoubleEdit::showUpDown
bool showUpDown() const
are the up/down buttons shown?
JKQTEDoubleEdit::wheelEvent
virtual void wheelEvent(QWheelEvent *event)
JKQTEDoubleEdit::setValue
void setValue(double value)
set the value entered in this widget
JKQTEDoubleEdit::m_checkMaximum
bool m_checkMaximum
Definition: jkqtedoubleedit.h:170
JKQTEDoubleEdit::setShowUpDown
void setShowUpDown(bool showUpDown)
sets whether up/down buttons are shown
JKQTEDoubleEdit
this QLineEdit descendent implements a validating edit field that allows to enter floating point numb...
Definition: jkqtedoubleedit.h:58
JKQTEDoubleEdit::m_checkMinimum
bool m_checkMinimum
Definition: jkqtedoubleedit.h:171
JKQTEDoubleEdit::isIntegerWidget
bool isIntegerWidget() const
is integer edit mode en-/disable (only integer numbers are allowed, i.e. no decimals!...
JKQTEDoubleEdit::m_contextmenuActions
QList< QAction * > m_contextmenuActions
list of additional actions for context menu
Definition: jkqtedoubleedit.h:156