Skip to content

Commit

Permalink
add support for NULL values in range widget (new Qgs(Double)SpinBox c…
Browse files Browse the repository at this point in the history
…lasses to show the clear button)
  • Loading branch information
3nids committed Sep 25, 2014
1 parent acb4c04 commit 4c39896
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 45 deletions.
6 changes: 6 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -68,6 +68,7 @@ editorwidgets/qgsdatetimeedit.cpp
editorwidgets/qgsdatetimeeditfactory.cpp
editorwidgets/qgsdatetimeeditconfig.cpp
editorwidgets/qgsdatetimeeditwrapper.cpp
editorwidgets/qgsdoublespinbox.cpp
editorwidgets/qgsdummyconfigdlg.cpp
editorwidgets/qgsenumerationwidgetwrapper.cpp
editorwidgets/qgsenumerationwidgetfactory.cpp
Expand All @@ -81,6 +82,7 @@ editorwidgets/qgsphotowidgetfactory.cpp
editorwidgets/qgsrangeconfigdlg.cpp
editorwidgets/qgsrangewidgetwrapper.cpp
editorwidgets/qgsrangewidgetfactory.cpp
editorwidgets/qgsspinbox.cpp
editorwidgets/qgsrelationwidgetwrapper.cpp
editorwidgets/qgsrelationreferenceconfigdlg.cpp
editorwidgets/qgsrelationreferencefactory.cpp
Expand Down Expand Up @@ -294,6 +296,7 @@ editorwidgets/qgscolorwidgetwrapper.h
editorwidgets/qgsdatetimeedit.h
editorwidgets/qgsdatetimeeditconfig.h
editorwidgets/qgsdatetimeeditwrapper.h
editorwidgets/qgsdoublespinbox.h
editorwidgets/qgsdummyconfigdlg.h
editorwidgets/qgsenumerationwidgetwrapper.h
editorwidgets/qgsfilenamewidgetwrapper.h
Expand All @@ -306,6 +309,7 @@ editorwidgets/qgsrelationreferenceconfigdlg.h
editorwidgets/qgsrelationreferencewidget.h
editorwidgets/qgsrelationreferencewidgetwrapper.h
editorwidgets/qgsrelationwidgetwrapper.h
editorwidgets/qgsspinbox.h
editorwidgets/qgstexteditconfigdlg.h
editorwidgets/qgstexteditwrapper.h
editorwidgets/qgsuniquevaluesconfigdlg.h
Expand Down Expand Up @@ -524,6 +528,7 @@ editorwidgets/qgsdatetimeedit.h
editorwidgets/qgsdatetimeeditfactory.h
editorwidgets/qgsdatetimeeditconfig.h
editorwidgets/qgsdatetimeeditwrapper.h
editorwidgets/qgsdoublespinbox.h
editorwidgets/qgsdummyconfigdlg.h
editorwidgets/qgsenumerationwidgetwrapper.h
editorwidgets/qgsenumerationwidgetfactory.h
Expand All @@ -537,6 +542,7 @@ editorwidgets/qgsphotowidgetfactory.h
editorwidgets/qgsrangeconfigdlg.h
editorwidgets/qgsrangewidgetwrapper.h
editorwidgets/qgsrangewidgetfactory.h
editorwidgets/qgsspinbox.h
editorwidgets/qgsrelationreferenceconfigdlg.h
editorwidgets/qgsrelationreferencefactory.h
editorwidgets/qgsrelationreferencewidget.h
Expand Down
82 changes: 82 additions & 0 deletions src/gui/editorwidgets/qgsdoublespinbox.cpp
@@ -0,0 +1,82 @@
/***************************************************************************
qgsdoublespinbox.cpp
--------------------------------------
Date : 09.2014
Copyright : (C) 2014 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QLineEdit>
#include <QMouseEvent>
#include <QSettings>
#include <QStyle>
#include <QToolButton>

#include "qgsdoublespinbox.h"

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

QgsDoubleSpinBox::QgsDoubleSpinBox( QWidget *parent )
: QDoubleSpinBox( parent )
, mShowClearButton( true )
{
mClearButton = new QToolButton( this );
mClearButton->setIcon( QgsApplication::getThemeIcon( "/mIconClear.svg" ) );
mClearButton->setCursor( Qt::ArrowCursor );
mClearButton->setStyleSheet( "position: absolute; border: none; padding: 0px;" );
connect( mClearButton, SIGNAL( clicked() ), this, SLOT( clear() ) );

setStyleSheet( QString( "padding-right: %1px;" ).arg( mClearButton->sizeHint().width() + 18 + frameWidth() + 1 ) );

QSize msz = minimumSizeHint();
setMinimumSize( qMax( msz.width(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ),
qMax( msz.height(), mClearButton->sizeHint().height() + frameWidth() * 2 + 2 ) );

connect( this, SIGNAL( valueChanged( double ) ), this, SLOT( changed( double ) ) );
}

void QgsDoubleSpinBox::setShowClearButton( bool showClearButton )
{
mShowClearButton = showClearButton;
mClearButton->setVisible( mShowClearButton && isEnabled() && value() != minimum() );
}

void QgsDoubleSpinBox::changeEvent( QEvent *event )
{
QDoubleSpinBox::changeEvent( event );
mClearButton->setVisible( mShowClearButton && isEnabled() && value() != minimum() );
}

void QgsDoubleSpinBox::changed( const double& value )
{
mClearButton->setVisible( mShowClearButton && isEnabled() && value != minimum() );
}

void QgsDoubleSpinBox::clear()
{
setValue( minimum() );
}

int QgsDoubleSpinBox::frameWidth() const
{
return style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
}

void QgsDoubleSpinBox::resizeEvent( QResizeEvent * event )
{
QDoubleSpinBox::resizeEvent( event );

QSize sz = mClearButton->sizeHint();

mClearButton->move( rect().right() - frameWidth() - 18 - sz.width() ,
( rect().bottom() + 1 - sz.height() ) / 2 );

}
57 changes: 57 additions & 0 deletions src/gui/editorwidgets/qgsdoublespinbox.h
@@ -0,0 +1,57 @@
/***************************************************************************
qgsdoublespinbox.h
--------------------------------------
Date : 09.2014
Copyright : (C) 2014 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSDOUBLESPPINBOX_H
#define QGSDOUBLESPPINBOX_H

#include <QDoubleSpinBox>
#include <QToolButton>

/**
* @brief The QgsSpinBox is a spin box with a clear button that will set the value to the minimum. This minum can then be handled by a special value text.
*/
class GUI_EXPORT QgsDoubleSpinBox : public QDoubleSpinBox
{
Q_OBJECT
Q_PROPERTY( bool showClearButton READ showClearButton WRITE setShowClearButton )

public:
explicit QgsDoubleSpinBox( QWidget *parent = 0 );

//! determines if the widget will show a clear button
//! @note the clear button will set the widget to its minimum value
void setShowClearButton( bool showClearButton );
bool showClearButton() const {return mShowClearButton;}

//! Set the current value to the minimum
virtual void clear();

protected:
virtual void resizeEvent( QResizeEvent* event );
virtual void changeEvent( QEvent* event );

private slots:
void changed( const double &value );

private:
int spinButtonWidth() const;
int frameWidth() const;

bool mShowClearButton;

QToolButton* mClearButton;
};

#endif // QGSDOUBLESPPINBOX_H
11 changes: 11 additions & 0 deletions src/gui/editorwidgets/qgsrangeconfigdlg.cpp
Expand Up @@ -66,6 +66,8 @@ QgsRangeConfigDlg::QgsRangeConfigDlg( QgsVectorLayer* vl, int fieldIdx, QWidget
}

valuesLabel->setText( text );

connect( rangeWidget, SIGNAL( currentIndexChanged( int ) ), this, SLOT( rangeWidgetChanged( int ) ) );
}

QgsEditorWidgetConfig QgsRangeConfigDlg::config()
Expand All @@ -92,6 +94,7 @@ QgsEditorWidgetConfig QgsRangeConfigDlg::config()
}

cfg.insert( "Style", rangeWidget->itemData( rangeWidget->currentIndex() ).toString() );
cfg.insert( "AllowNull", allowNullCheckBox->isChecked() );

if ( suffixLineEdit->text() != "" )
{
Expand All @@ -114,4 +117,12 @@ void QgsRangeConfigDlg::setConfig( const QgsEditorWidgetConfig& config )
rangeWidget->setCurrentIndex( rangeWidget->findData( config.value( "Style", "SpinBox" ) ) );

suffixLineEdit->setText( config.value( "Suffix" ).toString() );

allowNullCheckBox->setChecked( config.value( "AllowNull", true ).toBool() );
}

void QgsRangeConfigDlg::rangeWidgetChanged( int index )
{
QString style = rangeWidget->itemData( index ).toString();
allowNullCheckBox->setEnabled( style == "SpinBox" );
}
3 changes: 3 additions & 0 deletions src/gui/editorwidgets/qgsrangeconfigdlg.h
Expand Up @@ -27,6 +27,9 @@ class GUI_EXPORT QgsRangeConfigDlg : public QgsEditorConfigWidget, private Ui::Q
explicit QgsRangeConfigDlg( QgsVectorLayer* vl, int fieldIdx, QWidget* parent );
virtual QgsEditorWidgetConfig config();
virtual void setConfig( const QgsEditorWidgetConfig& config );

protected slots:
void rangeWidgetChanged( int index );
};

#endif // QGSRANGECONFIGDLG_H
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsrangewidgetfactory.cpp
Expand Up @@ -44,6 +44,7 @@ QgsEditorWidgetConfig QgsRangeWidgetFactory::readConfig( const QDomElement& conf
cfg.insert( "Min", configElement.attribute( "Min" ).toInt() );
cfg.insert( "Max", configElement.attribute( "Max" ).toInt() );
cfg.insert( "Step", configElement.attribute( "Step" ).toInt() );
cfg.insert( "AllowNull", configElement.attribute( "AllowNull" ) == "1" );

if ( configElement.hasAttribute( "Suffix" ) )
{
Expand All @@ -63,6 +64,7 @@ void QgsRangeWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QD
configElement.setAttribute( "Min", config["Min"].toInt() );
configElement.setAttribute( "Max", config["Max"].toInt() );
configElement.setAttribute( "Step", config["Step"].toInt() );
configElement.setAttribute( "AllowNull", config["AllowNull"].toBool() );
if ( config.contains( "Suffix" ) )
{
configElement.setAttribute( "Suffix", config["Suffix"].toString() );
Expand Down

0 comments on commit 4c39896

Please sign in to comment.