Skip to content

Commit

Permalink
Add a set to null ('clear') to color buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 24, 2016
1 parent 2fddc00 commit 58e1c7f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
29 changes: 29 additions & 0 deletions python/gui/qgscolorbuttonv2.sip
Expand Up @@ -151,6 +151,28 @@ class QgsColorButtonV2 : QToolButton
*/
void setNoColorString( const QString& noColorString );

/** Sets whether a set to null (clear) option is shown in the button's drop down menu.
* @param showNull set to true to show a null option
* @note added in QGIS 2.16
* @see showNull()
* @see isNull()
*/
void setShowNull( bool showNull );

/** Returns whether the set to null (clear) option is shown in the button's drop down menu.
* @note added in QGIS 2.16
* @see setShowNull()
* @see isNull()
*/
bool showNull() const;

/** Returns true if the current color is null.
* @note added in QGIS 2.16
* @see setShowNull()
* @see showNull()
*/
bool isNull() const;

/** Returns the string used for the "no color" option in the button's drop down menu.
* @returns string used for the "no color" menu option
* @see setNoColorString
Expand Down Expand Up @@ -237,6 +259,13 @@ class QgsColorButtonV2 : QToolButton
*/
void setToDefaultColor();

/** Sets color to null.
* @see setToDefaultColor()
* @see setToNoColor()
* @note added in QGIS 2.16
*/
void setToNull();

signals:

/** Is emitted whenever a new color is set for the button. The color is always valid.
Expand Down
1 change: 1 addition & 0 deletions src/gui/editorwidgets/qgscolorwidgetwrapper.cpp
Expand Up @@ -64,6 +64,7 @@ void QgsColorWidgetWrapper::initWidget( QWidget* editor )
mColorButton = editor->findChild<QgsColorButtonV2*>();
}

mColorButton->setShowNull( true );
connect( mColorButton, SIGNAL( colorChanged( QColor ) ), this, SLOT( valueChanged() ) );
}

Expand Down
29 changes: 29 additions & 0 deletions src/gui/qgscolorbuttonv2.cpp
Expand Up @@ -49,6 +49,7 @@ QgsColorButtonV2::QgsColorButtonV2( QWidget *parent, const QString& cdt, QgsColo
, mColorSet( false )
, mShowNoColorOption( false )
, mNoColorString( tr( "No color" ) )
, mShowNull( false )
, mPickingColor( false )
, mMenu( nullptr )

Expand Down Expand Up @@ -155,6 +156,11 @@ void QgsColorButtonV2::setToDefaultColor()
setColor( mDefaultColor );
}

void QgsColorButtonV2::setToNull()
{
setColor( QColor() );
}

void QgsColorButtonV2::setToNoColor()
{
if ( mAllowAlpha )
Expand Down Expand Up @@ -391,6 +397,14 @@ void QgsColorButtonV2::prepareMenu()
//menu is opened, otherwise color schemes like the recent color scheme grid are meaningless
mMenu->clear();

if ( mShowNull )
{
QAction* nullAction = new QAction( tr( "Clear color" ), this );
nullAction->setIcon( createMenuIcon( Qt::transparent, false ) );
mMenu->addAction( nullAction );
connect( nullAction, SIGNAL( triggered() ), this, SLOT( setToNull() ) );
}

//show default color option if set
if ( mDefaultColor.isValid() )
{
Expand Down Expand Up @@ -664,3 +678,18 @@ void QgsColorButtonV2::setDefaultColor( const QColor& color )
mDefaultColor = color;
}

void QgsColorButtonV2::setShowNull( bool showNull )
{
mShowNull = showNull;
}

bool QgsColorButtonV2::showNull() const
{
return mShowNull;
}

bool QgsColorButtonV2::isNull() const
{
return !mColor.isValid();
}

32 changes: 32 additions & 0 deletions src/gui/qgscolorbuttonv2.h
Expand Up @@ -184,6 +184,28 @@ class GUI_EXPORT QgsColorButtonV2 : public QToolButton
*/
void setNoColorString( const QString& noColorString ) { mNoColorString = noColorString; }

/** Sets whether a set to null (clear) option is shown in the button's drop down menu.
* @param showNull set to true to show a null option
* @note added in QGIS 2.16
* @see showNull()
* @see isNull()
*/
void setShowNull( bool showNull );

/** Returns whether the set to null (clear) option is shown in the button's drop down menu.
* @note added in QGIS 2.16
* @see setShowNull()
* @see isNull()
*/
bool showNull() const;

/** Returns true if the current color is null.
* @note added in QGIS 2.16
* @see setShowNull()
* @see showNull()
*/
bool isNull() const;

/** Returns the string used for the "no color" option in the button's drop down menu.
* @returns string used for the "no color" menu option
* @see setNoColorString
Expand Down Expand Up @@ -261,15 +283,24 @@ class GUI_EXPORT QgsColorButtonV2 : public QToolButton
/** Sets color to a totally transparent color.
* @note If the color button is not set to show an alpha channel in the color
* dialog (see setColorDialogOptions) then the color will not be changed.
* @see setToNull()
*/
void setToNoColor();

/** Sets color to the button's default color, if set.
* @see setDefaultColor
* @see defaultColor
* @see setToNull()
*/
void setToDefaultColor();

/** Sets color to null.
* @see setToDefaultColor()
* @see setToNoColor()
* @note added in QGIS 2.16
*/
void setToNull();

signals:

/** Is emitted whenever a new color is set for the button. The color is always valid.
Expand Down Expand Up @@ -346,6 +377,7 @@ class GUI_EXPORT QgsColorButtonV2 : public QToolButton

bool mShowNoColorOption;
QString mNoColorString;
bool mShowNull;

QPoint mDragStartPosition;
bool mPickingColor;
Expand Down

0 comments on commit 58e1c7f

Please sign in to comment.