QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Public Slots | Signals | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Properties | Private Slots | Private Member Functions | Private Attributes | List of all members
QgsColorButtonV2 Class Reference

A cross platform button subclass for selecting colors. More...

#include <qgscolorbuttonv2.h>

Inheritance diagram for QgsColorButtonV2:
Inheritance graph
[legend]
Collaboration diagram for QgsColorButtonV2:
Collaboration graph
[legend]

Public Slots

void setColor (const QColor &color)
 Sets the current color for the button. More...
 
void setButtonBackground (const QColor color=QColor())
 Sets the background pixmap for the button based upon color and transparency. More...
 
void copyColor ()
 Copies the current color to the clipboard. More...
 
void pasteColor ()
 Pastes a color from the clipboard to the color button. More...
 
void activatePicker ()
 Activates the color picker tool, which allows for sampling a color from anywhere on the screen. More...
 
void setToNoColor ()
 Sets color to a totally transparent color. More...
 
void setToDefaultColor ()
 Sets color to the button's default color, if set. More...
 

Signals

void colorChanged (const QColor &color)
 Is emitted whenever a new color is set for the button. More...
 

Public Member Functions

 QgsColorButtonV2 (QWidget *parent=0, QString cdt="", QColorDialog::ColorDialogOptions cdo=0, QgsColorSchemeRegistry *registry=0)
 Construct a new color button. More...
 
virtual ~QgsColorButtonV2 ()
 
virtual QSize sizeHint () const
 
QColor color () const
 Return the currently selected color. More...
 
void setColorDialogOptions (const QColorDialog::ColorDialogOptions cdo)
 Set options for the color chooser dialog (e.g. More...
 
QColorDialog::ColorDialogOptions colorDialogOptions () const
 Returns the options for the color chooser dialog. More...
 
void setColorDialogTitle (const QString title)
 Set the title for the color chooser dialog window. More...
 
QString colorDialogTitle () const
 Returns the title for the color chooser dialog window. More...
 
bool acceptLiveUpdates () const
 Returns whether the button accepts live updates from QColorDialog. More...
 
void setAcceptLiveUpdates (const bool accept)
 Sets whether the button accepts live updates from QColorDialog. More...
 
void setDefaultColor (const QColor color)
 Sets the default color for the button, which is shown in the button's drop down menu for the "default color" option. More...
 
QColor defaultColor () const
 Returns the default color for the button, which is shown in the button's drop down menu for the "default color" option. More...
 
void setShowNoColor (const bool showNoColorOption)
 Sets whether the "no color" option should be shown in the button's drop down menu. More...
 
bool showNoColor () const
 Returns whether the "no color" option is shown in the button's drop down menu. More...
 
void setNoColorString (const QString noColorString)
 Sets the string to use for the "no color" option in the button's drop down menu. More...
 
QString noColorString () const
 Returns the string used for the "no color" option in the button's drop down menu. More...
 
void setContext (const QString context)
 Sets the context string for the color button. More...
 
QString context () const
 Returns the context string for the color button. More...
 
void setColorSchemeRegistry (QgsColorSchemeRegistry *registry)
 Sets the color scheme registry for the button, which controls the color swatch grids that are shown in the button's drop down menu. More...
 
QgsColorSchemeRegistrycolorSchemeRegistry ()
 Returns the color scheme registry for the button, which controls the color swatch grids that are shown in the button's drop down menu. More...
 

Protected Member Functions

void changeEvent (QEvent *e)
 
void showEvent (QShowEvent *e)
 
void mousePressEvent (QMouseEvent *e)
 Reimplemented to detect right mouse button clicks on the color button and allow dragging colors. More...
 
void mouseMoveEvent (QMouseEvent *e)
 Reimplemented to allow dragging colors from button. More...
 
void mouseReleaseEvent (QMouseEvent *e)
 Reimplemented to allow color picking. More...
 
void keyPressEvent (QKeyEvent *e)
 Reimplemented to allow cancelling color pick via keypress, and sample via space bar press. More...
 
void dragEnterEvent (QDragEnterEvent *e)
 Reimplemented to accept dragged colors. More...
 
void dragLeaveEvent (QDragLeaveEvent *e)
 Reimplemented to reset button appearance after drag leave. More...
 
void dropEvent (QDropEvent *e)
 Reimplemented to accept dropped colors. More...
 

Static Protected Member Functions

static const QPixmap & transparentBackground ()
 Returns a checkboard pattern pixmap for use as a background to transparent colors. More...
 

Properties

QString colorDialogTitle
 
bool acceptLiveUpdates
 
QColor color
 
QColorDialog::ColorDialogOptions colorDialogOptions
 

Private Slots

void showColorDialog ()
 
void setValidColor (const QColor &newColor)
 Sets color for button, if valid. More...
 
void prepareMenu ()
 Creates the drop down menu entries. More...
 

Private Member Functions

QMimeData * createColorMimeData () const
 Creates mime data from the current color. More...
 
bool colorFromMimeData (const QMimeData *mimeData, QColor &resultColor)
 Attempts to parse mimeData as a color, either via the mime data's color data or by parsing a textual representation of a color. More...
 
void stopPicking (QPointF eventPos, bool sampleColor=true)
 Ends a color picking operation. More...
 
void addRecentColor (const QColor color)
 Adds a color to the recent colors list. More...
 
QPixmap createDragIcon (const QColor color)
 Create an icon for dragging colors. More...
 
QPixmap createMenuIcon (const QColor color, const bool showChecks=true)
 Create a color icon for display in the drop down menu. More...
 

Private Attributes

QString mColorDialogTitle
 
QColor mColor
 
QgsColorSchemeRegistrymColorSchemeRegistry
 
QColor mDefaultColor
 
QString mContext
 
QColorDialog::ColorDialogOptions mColorDialogOptions
 
bool mAcceptLiveUpdates
 
bool mColorSet
 
bool mShowNoColorOption
 
QString mNoColorString
 
QPoint mDragStartPosition
 
bool mPickingColor
 
QMenu * mMenu
 
QSize mIconSize
 

Detailed Description

A cross platform button subclass for selecting colors.

Will open a color chooser dialog when clicked. Offers live updates to button from color chooser dialog. An attached drop down menu allows for copying and pasting colors, picking colors from the screen, and selecting colors from color swatch grids.

Note
Added in version 2.5

Definition at line 34 of file qgscolorbuttonv2.h.

Constructor & Destructor Documentation

QgsColorButtonV2::QgsColorButtonV2 ( QWidget *  parent = 0,
QString  cdt = "",
QColorDialog::ColorDialogOptions  cdo = 0,
QgsColorSchemeRegistry registry = 0 
)

Construct a new color button.

Parameters
parentThe parent QWidget for the dialog
cdtThe title to show in the color chooser dialog
cdoOptions for the color chooser dialog
registrya color scheme registry for color swatch grids to show in the drop down menu. If not specified, the button will use the global color scheme registry

Definition at line 40 of file qgscolorbuttonv2.cpp.

References QgsColorSchemeRegistry::instance(), mColorSchemeRegistry, mMenu, prepareMenu(), and showColorDialog().

QgsColorButtonV2::~QgsColorButtonV2 ( )
virtual

Definition at line 68 of file qgscolorbuttonv2.cpp.

Member Function Documentation

bool QgsColorButtonV2::acceptLiveUpdates ( ) const
inline

Returns whether the button accepts live updates from QColorDialog.

Returns
true if the button will be accepted immediately when the dialog's color changes
See also
setAcceptLiveUpdates

Definition at line 93 of file qgscolorbuttonv2.h.

void QgsColorButtonV2::activatePicker ( )
slot

Activates the color picker tool, which allows for sampling a color from anywhere on the screen.

Definition at line 587 of file qgscolorbuttonv2.cpp.

References mPickingColor, and sampler_cursor.

Referenced by prepareMenu().

void QgsColorButtonV2::addRecentColor ( const QColor  color)
private

Adds a color to the recent colors list.

Parameters
colorto add to recent colors list

Definition at line 497 of file qgscolorbuttonv2.cpp.

References color().

Referenced by dropEvent(), pasteColor(), setValidColor(), and stopPicking().

void QgsColorButtonV2::changeEvent ( QEvent *  e)
protected

Definition at line 446 of file qgscolorbuttonv2.cpp.

References setButtonBackground().

QColor QgsColorButtonV2::color ( ) const

Return the currently selected color.

Returns
currently selected color
See also
setColor

Referenced by addRecentColor(), createDragIcon(), setButtonBackground(), setColor(), setDefaultColor(), and showColorDialog().

void QgsColorButtonV2::colorChanged ( const QColor &  color)
signal

Is emitted whenever a new color is set for the button.

The color is always valid. In case the new color is the same no signal is emitted, to avoid infinite loops.

Parameters
colorNew color

Referenced by prepareMenu(), and setColor().

QColorDialog::ColorDialogOptions QgsColorButtonV2::colorDialogOptions ( ) const

Returns the options for the color chooser dialog.

Returns
Options for the color chooser dialog
See also
setColorDialogOptions
QString QgsColorButtonV2::colorDialogTitle ( ) const

Returns the title for the color chooser dialog window.

Returns
title for the color chooser dialog
See also
setColorDialogTitle
bool QgsColorButtonV2::colorFromMimeData ( const QMimeData *  mimeData,
QColor &  resultColor 
)
private

Attempts to parse mimeData as a color, either via the mime data's color data or by parsing a textual representation of a color.

Returns
true if mime data could be intrepreted as a color
Parameters
mimeDatamime data
resultColorQColor to store evaluated color
See also
createColorMimeData

Definition at line 156 of file qgscolorbuttonv2.cpp.

References mColor, mColorDialogOptions, and QgsSymbolLayerV2Utils::parseColorWithAlpha().

Referenced by dragEnterEvent(), dropEvent(), pasteColor(), and prepareMenu().

QgsColorSchemeRegistry* QgsColorButtonV2::colorSchemeRegistry ( )
inline

Returns the color scheme registry for the button, which controls the color swatch grids that are shown in the button's drop down menu.

Returns
color scheme registry for the button. If returned value is 0 then all color swatch grids are hidden from the button's drop down menu.
See also
setColorSchemeRegistry

Definition at line 186 of file qgscolorbuttonv2.h.

QString QgsColorButtonV2::context ( ) const
inline

Returns the context string for the color button.

The context string is passed to all color swatch grids shown in the button's drop down menu, to allow them to customise their display colors based on the context.

Returns
context string for the color button's color swatch grids
See also
setContext

Definition at line 170 of file qgscolorbuttonv2.h.

void QgsColorButtonV2::copyColor ( )
slot

Copies the current color to the clipboard.

See also
pasteColor

Definition at line 570 of file qgscolorbuttonv2.cpp.

References createColorMimeData().

Referenced by prepareMenu().

QMimeData * QgsColorButtonV2::createColorMimeData ( ) const
private

Creates mime data from the current color.

Sets both the mime data's color data, and the mime data's text with the color's hex code.

See also
colorFromMimeData

Definition at line 146 of file qgscolorbuttonv2.cpp.

References mColor.

Referenced by copyColor(), and mouseMoveEvent().

QPixmap QgsColorButtonV2::createDragIcon ( const QColor  color)
private

Create an icon for dragging colors.

Parameters
colorfor icon

Definition at line 236 of file qgscolorbuttonv2.cpp.

References color().

Referenced by mouseMoveEvent().

QPixmap QgsColorButtonV2::createMenuIcon ( const QColor  color,
const bool  showChecks = true 
)
private

Create a color icon for display in the drop down menu.

Parameters
colorfor icon
showChecksset to true to display a checkboard pattern behind transparent colors

Definition at line 346 of file qgscolorbuttonv2.cpp.

References transparentBackground().

Referenced by prepareMenu().

QColor QgsColorButtonV2::defaultColor ( ) const
inline

Returns the default color for the button, which is shown in the button's drop down menu for the "default color" option.

Returns
default color for the button. Returns an invalid QColor if the default color option is disabled.
See also
setDefaultColor

Definition at line 116 of file qgscolorbuttonv2.h.

void QgsColorButtonV2::dragEnterEvent ( QDragEnterEvent *  e)
protected

Reimplemented to accept dragged colors.

Definition at line 303 of file qgscolorbuttonv2.cpp.

References colorFromMimeData(), and setButtonBackground().

void QgsColorButtonV2::dragLeaveEvent ( QDragLeaveEvent *  e)
protected

Reimplemented to reset button appearance after drag leave.

Definition at line 317 of file qgscolorbuttonv2.cpp.

References mColor, and setButtonBackground().

void QgsColorButtonV2::dropEvent ( QDropEvent *  e)
protected

Reimplemented to accept dropped colors.

Definition at line 324 of file qgscolorbuttonv2.cpp.

References addRecentColor(), colorFromMimeData(), and setColor().

void QgsColorButtonV2::keyPressEvent ( QKeyEvent *  e)
protected

Reimplemented to allow cancelling color pick via keypress, and sample via space bar press.

Definition at line 290 of file qgscolorbuttonv2.cpp.

References mPickingColor, and stopPicking().

void QgsColorButtonV2::mouseMoveEvent ( QMouseEvent *  e)
protected

Reimplemented to allow dragging colors from button.

Definition at line 194 of file qgscolorbuttonv2.cpp.

References createColorMimeData(), createDragIcon(), mColor, mDragStartPosition, mPickingColor, and setButtonBackground().

void QgsColorButtonV2::mousePressEvent ( QMouseEvent *  e)
protected

Reimplemented to detect right mouse button clicks on the color button and allow dragging colors.

Definition at line 130 of file qgscolorbuttonv2.cpp.

References mDragStartPosition, and mPickingColor.

void QgsColorButtonV2::mouseReleaseEvent ( QMouseEvent *  e)
protected

Reimplemented to allow color picking.

Definition at line 255 of file qgscolorbuttonv2.cpp.

References mPickingColor, and stopPicking().

QString QgsColorButtonV2::noColorString ( ) const
inline

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 also
setNoColorString
showNoColor
Note
The "no color" option is only shown if the color button is set to show an alpha channel in the color dialog (see setColorDialogOptions)

Definition at line 154 of file qgscolorbuttonv2.h.

void QgsColorButtonV2::pasteColor ( )
slot

Pastes a color from the clipboard to the color button.

If clipboard does not contain a valid color or string representation of a color, then no change is applied.

See also
copyColor

Definition at line 576 of file qgscolorbuttonv2.cpp.

References addRecentColor(), colorFromMimeData(), and setColor().

Referenced by prepareMenu().

void QgsColorButtonV2::prepareMenu ( )
privateslot
void QgsColorButtonV2::setAcceptLiveUpdates ( const bool  accept)
inline

Sets whether the button accepts live updates from QColorDialog.

Live updates may cause changes that are not undoable on QColorDialog cancel.

Parameters
acceptset to true to enable live updates
See also
acceptLiveUpdates

Definition at line 100 of file qgscolorbuttonv2.h.

void QgsColorButtonV2::setButtonBackground ( const QColor  color = QColor())
slot

Sets the background pixmap for the button based upon color and transparency.

Call directly to update background after adding/removing QColorDialog::ShowAlphaChannel option but the color has not changed, i.e. setColor() wouldn't update button and you want the button to retain the set color's alpha component regardless

Parameters
colorColor for button background. If no color is specified, the button's current color will be used

Definition at line 518 of file qgscolorbuttonv2.cpp.

References color(), mColor, mColorDialogOptions, mIconSize, and transparentBackground().

Referenced by changeEvent(), dragEnterEvent(), dragLeaveEvent(), mouseMoveEvent(), setColor(), and showEvent().

void QgsColorButtonV2::setColor ( const QColor &  color)
slot

Sets the current color for the button.

Will emit a colorChanged signal if the color is different to the previous color.

Parameters
colornew color for the button
See also
color

Definition at line 473 of file qgscolorbuttonv2.cpp.

References color(), colorChanged(), mColor, mColorSet, and setButtonBackground().

Referenced by dropEvent(), pasteColor(), setToDefaultColor(), setToNoColor(), setValidColor(), and stopPicking().

void QgsColorButtonV2::setColorDialogOptions ( const QColorDialog::ColorDialogOptions  cdo)

Set options for the color chooser dialog (e.g.

whether alpha channel is shown).

Parameters
cdoOptions for the color chooser dialog. For instance, to allow the user to choose an alpha value in the dialog, call setColorDialogOptions( QColorDialog::ShowAlphaChannel )
See also
colorDialogOptions

Definition at line 602 of file qgscolorbuttonv2.cpp.

References mColorDialogOptions.

void QgsColorButtonV2::setColorDialogTitle ( const QString  title)

Set the title for the color chooser dialog window.

Parameters
titleTitle for the color chooser dialog
See also
colorDialogTitle

Definition at line 612 of file qgscolorbuttonv2.cpp.

References mColorDialogTitle.

void QgsColorButtonV2::setColorSchemeRegistry ( QgsColorSchemeRegistry registry)
inline

Sets the color scheme registry for the button, which controls the color swatch grids that are shown in the button's drop down menu.

Parameters
registrycolor scheme registry for the button. Set to 0 to hide all color swatch grids from the button's drop down menu.
See also
colorSchemeRegistry

Definition at line 178 of file qgscolorbuttonv2.h.

void QgsColorButtonV2::setContext ( const QString  context)
inline

Sets the context string for the color button.

The context string is passed to all color swatch grids shown in the button's drop down menu, to allow them to customise their display colors based on the context.

Parameters
contextcontext string for the color button's color swatch grids
See also
context

Definition at line 162 of file qgscolorbuttonv2.h.

void QgsColorButtonV2::setDefaultColor ( const QColor  color)

Sets the default color for the button, which is shown in the button's drop down menu for the "default color" option.

Parameters
colordefault color for the button. Set to an invalid QColor to disable the default color option.
See also
defaultColor

Definition at line 622 of file qgscolorbuttonv2.cpp.

References color(), and mDefaultColor.

void QgsColorButtonV2::setNoColorString ( const QString  noColorString)
inline

Sets the string to use for the "no color" option in the button's drop down menu.

Parameters
noColorStringstring to use for the "no color" menu option
See also
noColorString
setShowNoColor
Note
The "no color" option is only shown if the color button is set to show an alpha channel in the color dialog (see setColorDialogOptions)

Definition at line 145 of file qgscolorbuttonv2.h.

void QgsColorButtonV2::setShowNoColor ( const bool  showNoColorOption)
inline

Sets whether the "no color" option should be shown in the button's drop down menu.

If selected, the "no color" option sets the color button's color to a totally transparent color.

Parameters
showNoColorOptionset to true to show the no color option. This is disabled by default.
See also
showNoColor
setNoColorString
Note
The "no color" option is only shown if the color button is set to show an alpha channel in the color dialog (see setColorDialogOptions)

Definition at line 126 of file qgscolorbuttonv2.h.

void QgsColorButtonV2::setToDefaultColor ( )
slot

Sets color to the button's default color, if set.

See also
setDefaultColor
defaultColor

Definition at line 112 of file qgscolorbuttonv2.cpp.

References mDefaultColor, and setColor().

Referenced by prepareMenu().

void QgsColorButtonV2::setToNoColor ( )
slot

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.

Definition at line 122 of file qgscolorbuttonv2.cpp.

References mColorDialogOptions, and setColor().

Referenced by prepareMenu().

void QgsColorButtonV2::setValidColor ( const QColor &  newColor)
privateslot

Sets color for button, if valid.

Definition at line 337 of file qgscolorbuttonv2.cpp.

References addRecentColor(), and setColor().

Referenced by prepareMenu(), and showColorDialog().

void QgsColorButtonV2::showColorDialog ( )
privateslot
void QgsColorButtonV2::showEvent ( QShowEvent *  e)
protected

Definition at line 467 of file qgscolorbuttonv2.cpp.

References setButtonBackground().

bool QgsColorButtonV2::showNoColor ( ) const
inline

Returns whether the "no color" option is shown in the button's drop down menu.

If selected, the "no color" option sets the color button's color to a totally transparent color.

Returns
true if the no color option is shown.
See also
setShowNoColor
noColorString
Note
The "no color" option is only shown if the color button is set to show an alpha channel in the color dialog (see setColorDialogOptions)

Definition at line 136 of file qgscolorbuttonv2.h.

QSize QgsColorButtonV2::sizeHint ( ) const
virtual

Definition at line 72 of file qgscolorbuttonv2.cpp.

void QgsColorButtonV2::stopPicking ( QPointF  eventPos,
bool  sampleColor = true 
)
private

Ends a color picking operation.

Parameters
eventPosglobal position of pixel to sample color from
sampleColorset to true to actually sample the color, false to just cancel the color picking operation

Definition at line 268 of file qgscolorbuttonv2.cpp.

References addRecentColor(), mColor, mPickingColor, and setColor().

Referenced by keyPressEvent(), and mouseReleaseEvent().

const QPixmap & QgsColorButtonV2::transparentBackground ( )
staticprotected

Returns a checkboard pattern pixmap for use as a background to transparent colors.

Definition at line 82 of file qgscolorbuttonv2.cpp.

References QgsApplication::getThemePixmap().

Referenced by createMenuIcon(), and setButtonBackground().

Member Data Documentation

bool QgsColorButtonV2::mAcceptLiveUpdates
private

Definition at line 294 of file qgscolorbuttonv2.h.

Referenced by showColorDialog().

QColor QgsColorButtonV2::mColor
private
QColorDialog::ColorDialogOptions QgsColorButtonV2::mColorDialogOptions
private
QString QgsColorButtonV2::mColorDialogTitle
private

Definition at line 286 of file qgscolorbuttonv2.h.

Referenced by setColorDialogTitle(), and showColorDialog().

QgsColorSchemeRegistry* QgsColorButtonV2::mColorSchemeRegistry
private

Definition at line 289 of file qgscolorbuttonv2.h.

Referenced by prepareMenu(), and QgsColorButtonV2().

bool QgsColorButtonV2::mColorSet
private

Definition at line 295 of file qgscolorbuttonv2.h.

Referenced by setColor().

QString QgsColorButtonV2::mContext
private

Definition at line 292 of file qgscolorbuttonv2.h.

Referenced by prepareMenu().

QColor QgsColorButtonV2::mDefaultColor
private

Definition at line 291 of file qgscolorbuttonv2.h.

Referenced by prepareMenu(), setDefaultColor(), and setToDefaultColor().

QPoint QgsColorButtonV2::mDragStartPosition
private

Definition at line 300 of file qgscolorbuttonv2.h.

Referenced by mouseMoveEvent(), and mousePressEvent().

QSize QgsColorButtonV2::mIconSize
private

Definition at line 305 of file qgscolorbuttonv2.h.

Referenced by setButtonBackground().

QMenu* QgsColorButtonV2::mMenu
private

Definition at line 303 of file qgscolorbuttonv2.h.

Referenced by prepareMenu(), and QgsColorButtonV2().

QString QgsColorButtonV2::mNoColorString
private

Definition at line 298 of file qgscolorbuttonv2.h.

Referenced by prepareMenu().

bool QgsColorButtonV2::mPickingColor
private
bool QgsColorButtonV2::mShowNoColorOption
private

Definition at line 297 of file qgscolorbuttonv2.h.

Referenced by prepareMenu().

Property Documentation

bool QgsColorButtonV2::acceptLiveUpdates
readwrite

Definition at line 38 of file qgscolorbuttonv2.h.

QColor QgsColorButtonV2::color
readwrite

Definition at line 39 of file qgscolorbuttonv2.h.

QColorDialog::ColorDialogOptions QgsColorButtonV2::colorDialogOptions
readwrite

Definition at line 41 of file qgscolorbuttonv2.h.

QString QgsColorButtonV2::colorDialogTitle
readwrite

Definition at line 37 of file qgscolorbuttonv2.h.


The documentation for this class was generated from the following files: