Skip to content

Commit 58e1c7f

Browse files
committed
Add a set to null ('clear') to color buttons
1 parent 2fddc00 commit 58e1c7f

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

python/gui/qgscolorbuttonv2.sip

+29
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,28 @@ class QgsColorButtonV2 : QToolButton
151151
*/
152152
void setNoColorString( const QString& noColorString );
153153

154+
/** Sets whether a set to null (clear) option is shown in the button's drop down menu.
155+
* @param showNull set to true to show a null option
156+
* @note added in QGIS 2.16
157+
* @see showNull()
158+
* @see isNull()
159+
*/
160+
void setShowNull( bool showNull );
161+
162+
/** Returns whether the set to null (clear) option is shown in the button's drop down menu.
163+
* @note added in QGIS 2.16
164+
* @see setShowNull()
165+
* @see isNull()
166+
*/
167+
bool showNull() const;
168+
169+
/** Returns true if the current color is null.
170+
* @note added in QGIS 2.16
171+
* @see setShowNull()
172+
* @see showNull()
173+
*/
174+
bool isNull() const;
175+
154176
/** Returns the string used for the "no color" option in the button's drop down menu.
155177
* @returns string used for the "no color" menu option
156178
* @see setNoColorString
@@ -237,6 +259,13 @@ class QgsColorButtonV2 : QToolButton
237259
*/
238260
void setToDefaultColor();
239261

262+
/** Sets color to null.
263+
* @see setToDefaultColor()
264+
* @see setToNoColor()
265+
* @note added in QGIS 2.16
266+
*/
267+
void setToNull();
268+
240269
signals:
241270

242271
/** Is emitted whenever a new color is set for the button. The color is always valid.

src/gui/editorwidgets/qgscolorwidgetwrapper.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void QgsColorWidgetWrapper::initWidget( QWidget* editor )
6464
mColorButton = editor->findChild<QgsColorButtonV2*>();
6565
}
6666

67+
mColorButton->setShowNull( true );
6768
connect( mColorButton, SIGNAL( colorChanged( QColor ) ), this, SLOT( valueChanged() ) );
6869
}
6970

src/gui/qgscolorbuttonv2.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ QgsColorButtonV2::QgsColorButtonV2( QWidget *parent, const QString& cdt, QgsColo
4949
, mColorSet( false )
5050
, mShowNoColorOption( false )
5151
, mNoColorString( tr( "No color" ) )
52+
, mShowNull( false )
5253
, mPickingColor( false )
5354
, mMenu( nullptr )
5455

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

159+
void QgsColorButtonV2::setToNull()
160+
{
161+
setColor( QColor() );
162+
}
163+
158164
void QgsColorButtonV2::setToNoColor()
159165
{
160166
if ( mAllowAlpha )
@@ -391,6 +397,14 @@ void QgsColorButtonV2::prepareMenu()
391397
//menu is opened, otherwise color schemes like the recent color scheme grid are meaningless
392398
mMenu->clear();
393399

400+
if ( mShowNull )
401+
{
402+
QAction* nullAction = new QAction( tr( "Clear color" ), this );
403+
nullAction->setIcon( createMenuIcon( Qt::transparent, false ) );
404+
mMenu->addAction( nullAction );
405+
connect( nullAction, SIGNAL( triggered() ), this, SLOT( setToNull() ) );
406+
}
407+
394408
//show default color option if set
395409
if ( mDefaultColor.isValid() )
396410
{
@@ -664,3 +678,18 @@ void QgsColorButtonV2::setDefaultColor( const QColor& color )
664678
mDefaultColor = color;
665679
}
666680

681+
void QgsColorButtonV2::setShowNull( bool showNull )
682+
{
683+
mShowNull = showNull;
684+
}
685+
686+
bool QgsColorButtonV2::showNull() const
687+
{
688+
return mShowNull;
689+
}
690+
691+
bool QgsColorButtonV2::isNull() const
692+
{
693+
return !mColor.isValid();
694+
}
695+

src/gui/qgscolorbuttonv2.h

+32
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,28 @@ class GUI_EXPORT QgsColorButtonV2 : public QToolButton
184184
*/
185185
void setNoColorString( const QString& noColorString ) { mNoColorString = noColorString; }
186186

187+
/** Sets whether a set to null (clear) option is shown in the button's drop down menu.
188+
* @param showNull set to true to show a null option
189+
* @note added in QGIS 2.16
190+
* @see showNull()
191+
* @see isNull()
192+
*/
193+
void setShowNull( bool showNull );
194+
195+
/** Returns whether the set to null (clear) option is shown in the button's drop down menu.
196+
* @note added in QGIS 2.16
197+
* @see setShowNull()
198+
* @see isNull()
199+
*/
200+
bool showNull() const;
201+
202+
/** Returns true if the current color is null.
203+
* @note added in QGIS 2.16
204+
* @see setShowNull()
205+
* @see showNull()
206+
*/
207+
bool isNull() const;
208+
187209
/** Returns the string used for the "no color" option in the button's drop down menu.
188210
* @returns string used for the "no color" menu option
189211
* @see setNoColorString
@@ -261,15 +283,24 @@ class GUI_EXPORT QgsColorButtonV2 : public QToolButton
261283
/** Sets color to a totally transparent color.
262284
* @note If the color button is not set to show an alpha channel in the color
263285
* dialog (see setColorDialogOptions) then the color will not be changed.
286+
* @see setToNull()
264287
*/
265288
void setToNoColor();
266289

267290
/** Sets color to the button's default color, if set.
268291
* @see setDefaultColor
269292
* @see defaultColor
293+
* @see setToNull()
270294
*/
271295
void setToDefaultColor();
272296

297+
/** Sets color to null.
298+
* @see setToDefaultColor()
299+
* @see setToNoColor()
300+
* @note added in QGIS 2.16
301+
*/
302+
void setToNull();
303+
273304
signals:
274305

275306
/** 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
346377

347378
bool mShowNoColorOption;
348379
QString mNoColorString;
380+
bool mShowNull;
349381

350382
QPoint mDragStartPosition;
351383
bool mPickingColor;

0 commit comments

Comments
 (0)