Skip to content
Permalink
Browse files

Add a set to null ('clear') to color buttons

  • Loading branch information
nyalldawson committed May 24, 2016
1 parent 2fddc00 commit 58e1c7fc431dc37ffe4d7bc29f3c700201860733
@@ -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
@@ -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.
@@ -64,6 +64,7 @@ void QgsColorWidgetWrapper::initWidget( QWidget* editor )
mColorButton = editor->findChild<QgsColorButtonV2*>();
}

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

@@ -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 )

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

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

void QgsColorButtonV2::setToNoColor()
{
if ( mAllowAlpha )
@@ -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() )
{
@@ -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();
}

@@ -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
@@ -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.
@@ -346,6 +377,7 @@ class GUI_EXPORT QgsColorButtonV2 : public QToolButton

bool mShowNoColorOption;
QString mNoColorString;
bool mShowNull;

QPoint mDragStartPosition;
bool mPickingColor;

0 comments on commit 58e1c7f

Please sign in to comment.
You can’t perform that action at this time.