Skip to content

Commit

Permalink
Also evaluate expressions entered in QgsSpinBox
Browse files Browse the repository at this point in the history
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.
16 changes: 16 additions & 0 deletions python/gui/editorwidgets/qgsspinbox.sip
Expand Up @@ -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();
Expand All @@ -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 );
Expand Down
17 changes: 14 additions & 3 deletions src/app/composer/qgscomposeritemwidget.cpp
Expand Up @@ -115,6 +115,7 @@ QgsComposerItemWidget::QgsComposerItemWidget( QWidget* parent, QgsComposerItem*
, mFreezeYPosSpin( false )
, mFreezeWidthSpin( false )
, mFreezeHeightSpin( false )
, mFreezePageSpin( false )
{

setupUi( this );
Expand All @@ -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();
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/app/composer/qgscomposeritemwidget.h
Expand Up @@ -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 );
Expand All @@ -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 );
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgsdoublespinbox.cpp
Expand Up @@ -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() );
Expand Down
65 changes: 64 additions & 1 deletion src/gui/editorwidgets/qgsspinbox.cpp
Expand Up @@ -20,7 +20,7 @@
#include <QToolButton>

#include "qgsspinbox.h"

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

Expand All @@ -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" ) );
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down
19 changes: 19 additions & 0 deletions src/gui/editorwidgets/qgsspinbox.h
Expand Up @@ -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();

Expand All @@ -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 );
Expand All @@ -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
12 changes: 10 additions & 2 deletions src/ui/qgscomposerattributetablewidgetbase.ui
Expand Up @@ -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">
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down
18 changes: 16 additions & 2 deletions src/ui/qgscomposeritemwidgetbase.ui
Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
7 changes: 6 additions & 1 deletion src/ui/qgscomposerlegendwidgetbase.ui
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 697ef51

Please sign in to comment.