QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsspinbox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsspinbox.cpp
3  --------------------------------------
4  Date : 09.2014
5  Copyright : (C) 2014 Denis Rouzaud
6  Email : denis.rouzaud@gmail.com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include <QLineEdit>
17 #include <QMouseEvent>
18 #include <QSettings>
19 #include <QStyle>
20 #include <QToolButton>
21 
22 #include "qgsspinbox.h"
23 #include "qgsexpression.h"
24 #include "qgsapplication.h"
25 #include "qgslogger.h"
26 
28  : QSpinBox( parent )
29  , mShowClearButton( true )
30  , mClearValueMode( MinimumValue )
31  , mCustomClearValue( 0 )
32  , mExpressionsEnabled( true )
33 {
34  mClearButton = new QToolButton( this );
35  mClearButton->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
36  mClearButton->setCursor( Qt::ArrowCursor );
37  mClearButton->setStyleSheet( "position: absolute; border: none; padding: 0px;" );
38  connect( mClearButton, SIGNAL( clicked() ), this, SLOT( clear() ) );
39 
40  setStyleSheet( QString( "padding-right: %1px;" ).arg( mClearButton->sizeHint().width() + 18 + frameWidth() + 1 ) );
41 
42  QSize msz = minimumSizeHint();
43  setMinimumSize( qMax( msz.width(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ),
44  qMax( msz.height(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ) );
45 
46  connect( this, SIGNAL( valueChanged( int ) ), this, SLOT( changed( int ) ) );
47 }
48 
49 void QgsSpinBox::setShowClearButton( const bool showClearButton )
50 {
51  mShowClearButton = showClearButton;
52  mClearButton->setVisible( shouldShowClearForValue( value() ) );
53 }
54 
55 void QgsSpinBox::setExpressionsEnabled( const bool enabled )
56 {
57  mExpressionsEnabled = enabled;
58 }
59 
61 {
62  QSpinBox::changeEvent( event );
63  mClearButton->setVisible( shouldShowClearForValue( value() ) );
64 }
65 
67 {
68  mClearButton->setVisible( shouldShowClearForValue( value() ) );
69  QSpinBox::paintEvent( event );
70 }
71 
72 void QgsSpinBox::changed( const int& value )
73 {
74  mClearButton->setVisible( shouldShowClearForValue( value ) );
75 }
76 
78 {
79  setValue( clearValue() );
80 }
81 
82 void QgsSpinBox::setClearValue( int customValue, QString specialValueText )
83 {
84  mClearValueMode = CustomValue;
85  mCustomClearValue = customValue;
86 
87  if ( !specialValueText.isEmpty() )
88  {
89  int v = value();
90  clear();
91  setSpecialValueText( specialValueText );
92  setValue( v );
93  }
94 }
95 
97 {
98  mClearValueMode = mode;
99  mCustomClearValue = 0;
100 
101  if ( !specialValueText.isEmpty() )
102  {
103  int v = value();
104  clear();
105  setSpecialValueText( specialValueText );
106  setValue( v );
107  }
108 }
109 
111 {
112  if ( mClearValueMode == MinimumValue )
113  return minimum() ;
114  else if ( mClearValueMode == MaximumValue )
115  return maximum();
116  else
117  return mCustomClearValue;
118 }
119 
120 int QgsSpinBox::valueFromText( const QString &text ) const
121 {
122  if ( !mExpressionsEnabled )
123  {
124  return QSpinBox::valueFromText( text );
125  }
126 
127  QString trimmedText = stripped( text );
128  if ( trimmedText.isEmpty() )
129  {
130  return mShowClearButton ? clearValue() : value();
131  }
132 
133  return qRound( QgsExpression::evaluateToDouble( trimmedText, value() ) );
134 }
135 
136 QValidator::State QgsSpinBox::validate( QString &input, int &pos ) const
137 {
138  if ( !mExpressionsEnabled )
139  {
140  QValidator::State r = QSpinBox::validate( input, pos );
141  return r;
142  }
143 
144  return QValidator::Acceptable;
145 }
146 
147 int QgsSpinBox::frameWidth() const
148 {
149  return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
150 }
151 
152 bool QgsSpinBox::shouldShowClearForValue( const int value ) const
153 {
154  if ( !mShowClearButton || !isEnabled() )
155  {
156  return false;
157  }
158  return value != clearValue();
159 }
160 
161 QString QgsSpinBox::stripped( const QString &originalText ) const
162 {
163  //adapted from QAbstractSpinBoxPrivate::stripped
164  //trims whitespace, prefix and suffix from spin box text
165  QString text = originalText;
166  if ( specialValueText().size() == 0 || text != specialValueText() )
167  {
168  int from = 0;
169  int size = text.size();
170  bool changed = false;
171  if ( prefix().size() && text.startsWith( prefix() ) )
172  {
173  from += prefix().size();
174  size -= from;
175  changed = true;
176  }
177  if ( suffix().size() && text.endsWith( suffix() ) )
178  {
179  size -= suffix().size();
180  changed = true;
181  }
182  if ( changed )
183  text = text.mid( from, size );
184  }
185 
186  text = text.trimmed();
187 
188  return text;
189 }
190 
192 {
193  QSpinBox::resizeEvent( event );
194 
195  QSize sz = mClearButton->sizeHint();
196 
197  mClearButton->move( rect().right() - frameWidth() - 18 - sz.width(),
198  ( rect().bottom() + 1 - sz.height() ) / 2 );
199 
200 }
void setClearValueMode(ClearValueMode mode, QString clearValueText=QString())
setClearValueMode defines if the clear value should be the minimum or maximum values of the widget or...
Definition: qgsspinbox.cpp:96
void setStyleSheet(const QString &styleSheet)
int minimum() const
int width() const
void setCursor(const QCursor &)
virtual QSize sizeHint() const
virtual QSize minimumSizeHint() const
QString suffix() const
void setExpressionsEnabled(const bool enabled)
Sets if the widget will allow entry of simple expressions, which are evaluated and then discarded...
Definition: qgsspinbox.cpp:55
QStyle * style() const
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const =0
QString prefix() const
int size() const
virtual void setVisible(bool visible)
virtual QValidator::State validate(QString &input, int &pos) const override
Definition: qgsspinbox.cpp:136
virtual void clear() override
Set the current value to the value defined by the clear value.
Definition: qgsspinbox.cpp:77
virtual void resizeEvent(QResizeEvent *event) override
Definition: qgsspinbox.cpp:191
void setIcon(const QIcon &icon)
QString text() const
void valueChanged(int i)
QSize size() const
void setMinimumSize(const QSize &)
void setShowClearButton(const bool showClearButton)
determines if the widget will show a clear button
Definition: qgsspinbox.cpp:49
bool isEmpty() const
QString trimmed() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
void move(int x, int y)
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QRect rect() const
virtual void paintEvent(QPaintEvent *event) override
Definition: qgsspinbox.cpp:66
int maximum() const
virtual int valueFromText(const QString &text) const
virtual QValidator::State validate(QString &text, int &pos) const
virtual int valueFromText(const QString &text) const override
Definition: qgsspinbox.cpp:120
QString mid(int position, int n) const
virtual void paintEvent(QPaintEvent *event)
virtual void resizeEvent(QResizeEvent *event)
virtual void changeEvent(QEvent *event)
int value() const
int height() const
int clearValue() const
returns the value used when clear() is called.
Definition: qgsspinbox.cpp:110
int bottom() const
void setSpecialValueText(const QString &txt)
virtual void changeEvent(QEvent *event) override
Definition: qgsspinbox.cpp:60
void setClearValue(int customValue, QString clearValueText=QString())
setClearValue defines the clear value for the widget and will automatically set the clear value mode ...
Definition: qgsspinbox.cpp:82
bool showClearButton() const
Definition: qgsspinbox.h:45
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QgsSpinBox(QWidget *parent=0)
Definition: qgsspinbox.cpp:27
static double evaluateToDouble(const QString &text, const double fallbackValue)
Attempts to evaluate a text string as an expression to a resultant double value.