Skip to content
Permalink
Browse files
Also evaluate expressions entered in QgsSpinBox
and flip all composer spin boxes to QgsSpinBox (refs #10544)
  • Loading branch information
nyalldawson committed Dec 5, 2014
1 parent dfe7980 commit 697ef51
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 24 deletions.
@@ -18,6 +18,19 @@ class QgsSpinBox : QSpinBox
//! @note the clear button will set the widget to its minimum value
void setShowClearButton( const bool showClearButton );
bool showClearButton() const;

/**Sets if the widget will allow entry of simple expressions, which are
* evaluated and then discarded.
* @param enabled set to true to allow expression entry
* @note added in QGIS 2.7
*/
void setExpressionsEnabled( const bool enabled );
/**Returns whether the widget will allow entry of simple expressions, which are
* evaluated and then discarded.
* @returns true if spin box allows expression entry
* @note added in QGIS 2.7
*/
bool expressionsEnabled() const;

//! Set the current value to the value defined by the clear value.
virtual void clear();
@@ -38,6 +51,9 @@ class QgsSpinBox : QSpinBox
//! returns the value used when clear() is called.
int clearValue() const;

virtual int valueFromText( const QString & text ) const;
virtual QValidator::State validate( QString & input, int & pos ) const;

protected:
virtual void resizeEvent( QResizeEvent* event );
virtual void changeEvent( QEvent* event );
@@ -115,6 +115,7 @@ QgsComposerItemWidget::QgsComposerItemWidget( QWidget* parent, QgsComposerItem*
, mFreezeYPosSpin( false )
, mFreezeWidthSpin( false )
, mFreezeHeightSpin( false )
, mFreezePageSpin( false )
{

setupUi( this );
@@ -138,7 +139,6 @@ QgsComposerItemWidget::QgsComposerItemWidget( QWidget* parent, QgsComposerItem*
connect( mItem, SIGNAL( itemChanged() ), this, SLOT( setValuesForGuiNonPositionElements() ) );

connect( mTransparencySlider, SIGNAL( valueChanged( int ) ), mTransparencySpnBx, SLOT( setValue( int ) ) );
connect( mTransparencySpnBx, SIGNAL( valueChanged( int ) ), mTransparencySlider, SLOT( setValue( int ) ) );

//connect atlas signals to data defined buttons
QgsAtlasComposition* atlas = atlasComposition();
@@ -464,7 +464,8 @@ void QgsComposerItemWidget::setValuesForGuiPositionElements()
mWidthSpin->setValue( mItem->rect().width() );
if ( !mFreezeHeightSpin )
mHeightSpin->setValue( mItem->rect().height() );
mPageSpinBox->setValue( mItem->page() );
if ( !mFreezePageSpin )
mPageSpinBox->setValue( mItem->page() );

mXPosSpin->blockSignals( false );
mYPosSpin->blockSignals( false );
@@ -640,8 +641,11 @@ void QgsComposerItemWidget::on_mBlendModeCombo_currentIndexChanged( int index )
}
}

void QgsComposerItemWidget::on_mTransparencySlider_valueChanged( int value )
void QgsComposerItemWidget::on_mTransparencySpnBx_valueChanged( int value )
{
mTransparencySlider->blockSignals( true );
mTransparencySlider->setValue( value );
mTransparencySlider->blockSignals( false );
if ( mItem )
{
mItem->beginCommand( tr( "Item transparency changed" ), QgsComposerMergeCommand::ItemTransparency );
@@ -661,6 +665,13 @@ void QgsComposerItemWidget::on_mItemIdLineEdit_editingFinished()
}
}

void QgsComposerItemWidget::on_mPageSpinBox_valueChanged( int )
{
mFreezePageSpin = true;
changeItemPosition();
mFreezePageSpin = false;
}

void QgsComposerItemWidget::on_mXPosSpin_valueChanged( double )
{
mFreezeXPosSpin = true;
@@ -90,7 +90,7 @@ class QgsComposerItemWidget: public QgsComposerItemBaseWidget, private Ui::QgsCo
void on_mItemIdLineEdit_editingFinished();

//adjust coordinates in line edits
void on_mPageSpinBox_valueChanged( int ) { changeItemPosition(); }
void on_mPageSpinBox_valueChanged( int );
void on_mXPosSpin_valueChanged( double );
void on_mYPosSpin_valueChanged( double );
void on_mWidthSpin_valueChanged( double );
@@ -107,7 +107,7 @@ class QgsComposerItemWidget: public QgsComposerItemBaseWidget, private Ui::QgsCo
void on_mLowerRightCheckBox_stateChanged( int state );

void on_mBlendModeCombo_currentIndexChanged( int index );
void on_mTransparencySlider_valueChanged( int value );
void on_mTransparencySpnBx_valueChanged( int value );

void on_mItemRotationSpinBox_valueChanged( double val );
void on_mExcludeFromPrintsCheckBox_toggled( bool checked );
@@ -134,6 +134,7 @@ class QgsComposerItemWidget: public QgsComposerItemBaseWidget, private Ui::QgsCo
bool mFreezeYPosSpin;
bool mFreezeWidthSpin;
bool mFreezeHeightSpin;
bool mFreezePageSpin;

// void changeItemTransparency( int value );
void changeItemPosition();
@@ -151,7 +151,7 @@ double QgsDoubleSpinBox::valueFromText( const QString &text ) const
QString trimmedText = stripped( text );
if ( trimmedText.isEmpty() )
{
return clearValue();
return mShowClearButton ? clearValue() : value();
}

return QgsExpression::evaluateToDouble( trimmedText, value() );
@@ -20,7 +20,7 @@
#include <QToolButton>

#include "qgsspinbox.h"

#include "qgsexpression.h"
#include "qgsapplication.h"
#include "qgslogger.h"

@@ -29,6 +29,7 @@ QgsSpinBox::QgsSpinBox( QWidget *parent )
, mShowClearButton( true )
, mClearValueMode( MinimumValue )
, mCustomClearValue( 0 )
, mExpressionsEnabled( true )
{
mClearButton = new QToolButton( this );
mClearButton->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
@@ -51,6 +52,11 @@ void QgsSpinBox::setShowClearButton( const bool showClearButton )
mClearButton->setVisible( shouldShowClearForValue( value() ) );
}

void QgsSpinBox::setExpressionsEnabled( const bool enabled )
{
mExpressionsEnabled = enabled;
}

void QgsSpinBox::changeEvent( QEvent *event )
{
QSpinBox::changeEvent( event );
@@ -105,6 +111,33 @@ int QgsSpinBox::clearValue() const
return mCustomClearValue;
}

int QgsSpinBox::valueFromText( const QString &text ) const
{
if ( !mExpressionsEnabled )
{
return QSpinBox::valueFromText( text );
}

QString trimmedText = stripped( text );
if ( trimmedText.isEmpty() )
{
return mShowClearButton ? clearValue() : value();
}

return qRound( QgsExpression::evaluateToDouble( trimmedText, value() ) );
}

QValidator::State QgsSpinBox::validate( QString &input, int &pos ) const
{
if ( !mExpressionsEnabled )
{
QValidator::State r = QSpinBox::validate( input, pos );
return r;
}

return QValidator::Acceptable;
}

int QgsSpinBox::frameWidth() const
{
return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
@@ -119,6 +152,36 @@ bool QgsSpinBox::shouldShowClearForValue( const int value ) const
return value != clearValue();
}

QString QgsSpinBox::stripped( const QString &originalText ) const
{
//adapted from QAbstractSpinBoxPrivate::stripped
//trims whitespace, prefix and suffix from spin box text
QString text = originalText;
if ( specialValueText().size() == 0 || text != specialValueText() )
{
int from = 0;
int size = text.size();
bool changed = false;
if ( prefix().size() && text.startsWith( prefix() ) )
{
from += prefix().size();
size -= from;
changed = true;
}
if ( suffix().size() && text.endsWith( suffix() ) )
{
size -= suffix().size();
changed = true;
}
if ( changed )
text = text.mid( from, size );
}

text = text.trimmed();

return text;
}

void QgsSpinBox::resizeEvent( QResizeEvent * event )
{
QSpinBox::resizeEvent( event );
@@ -43,6 +43,19 @@ class GUI_EXPORT QgsSpinBox : public QSpinBox
void setShowClearButton( const bool showClearButton );
bool showClearButton() const {return mShowClearButton;}

/**Sets if the widget will allow entry of simple expressions, which are
* evaluated and then discarded.
* @param enabled set to true to allow expression entry
* @note added in QGIS 2.7
*/
void setExpressionsEnabled( const bool enabled );
/**Returns whether the widget will allow entry of simple expressions, which are
* evaluated and then discarded.
* @returns true if spin box allows expression entry
* @note added in QGIS 2.7
*/
bool expressionsEnabled() const {return mExpressionsEnabled;}

//! Set the current value to the value defined by the clear value.
virtual void clear();

@@ -62,6 +75,9 @@ class GUI_EXPORT QgsSpinBox : public QSpinBox
//! returns the value used when clear() is called.
int clearValue() const;

virtual int valueFromText( const QString & text ) const;
virtual QValidator::State validate( QString & input, int & pos ) const;

protected:
virtual void resizeEvent( QResizeEvent* event );
virtual void changeEvent( QEvent* event );
@@ -77,7 +93,10 @@ class GUI_EXPORT QgsSpinBox : public QSpinBox
ClearValueMode mClearValueMode;
int mCustomClearValue;

bool mExpressionsEnabled;

QToolButton* mClearButton;
QString stripped( const QString &originalText ) const;
};

#endif // QGSSPPINBOX_H
@@ -47,7 +47,7 @@
<x>0</x>
<y>0</y>
<width>392</width>
<height>1204</height>
<height>1202</height>
</rect>
</property>
<layout class="QVBoxLayout" name="mainLayout">
@@ -152,10 +152,13 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="mMaximumRowsSpinBox">
<widget class="QgsSpinBox" name="mMaximumRowsSpinBox">
<property name="maximum">
<number>99999</number>
</property>
<property name="showClearButton">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
@@ -727,6 +730,11 @@
<extends>QComboBox</extends>
<header>qgsmaplayercombobox.h</header>
</customwidget>
<customwidget>
<class>QgsSpinBox</class>
<extends>QSpinBox</extends>
<header>qgsspinbox.h</header>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBoxBasic</class>
<extends>QGroupBox</extends>
@@ -98,10 +98,13 @@
</widget>
</item>
<item row="0" column="1" colspan="3">
<widget class="QSpinBox" name="mPageSpinBox">
<widget class="QgsSpinBox" name="mPageSpinBox">
<property name="minimum">
<number>1</number>
</property>
<property name="showClearButton">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="1" colspan="2">
@@ -646,7 +649,13 @@
</widget>
</item>
<item>
<widget class="QSpinBox" name="mTransparencySpnBx">
<widget class="QgsSpinBox" name="mTransparencySpnBx">
<property name="minimumSize">
<size>
<width>80</width>
<height>25</height>
</size>
</property>
<property name="maximum">
<number>100</number>
</property>
@@ -717,6 +726,11 @@
<extends>QDoubleSpinBox</extends>
<header>qgsdoublespinbox.h</header>
</customwidget>
<customwidget>
<class>QgsSpinBox</class>
<extends>QSpinBox</extends>
<header>qgsspinbox.h</header>
</customwidget>
<customwidget>
<class>QgsPenJoinStyleComboBox</class>
<extends>QComboBox</extends>
@@ -522,7 +522,7 @@
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="mColumnCountSpinBox">
<widget class="QgsSpinBox" name="mColumnCountSpinBox">
<property name="prefix">
<string/>
</property>
@@ -892,6 +892,11 @@
<extends>QDoubleSpinBox</extends>
<header>qgsdoublespinbox.h</header>
</customwidget>
<customwidget>
<class>QgsSpinBox</class>
<extends>QSpinBox</extends>
<header>qgsspinbox.h</header>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBoxBasic</class>
<extends>QGroupBox</extends>

0 comments on commit 697ef51

Please sign in to comment.