mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-07 00:03:52 -05:00
Add options for color buttons, allowing them to be used in a swatch
type mode.
This commit is contained in:
parent
7f3a5149c5
commit
e79ff32f47
@ -15,6 +15,14 @@ class QgsColorButtonV2: QToolButton
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
/*! Specifies the behaviour when the button is clicked
|
||||||
|
*/
|
||||||
|
enum Behaviour
|
||||||
|
{
|
||||||
|
ShowDialog, /*!< show a color picker dialog when clicked */
|
||||||
|
SignalOnly /*!< emit colorClicked signal only, no dialog */
|
||||||
|
};
|
||||||
|
|
||||||
/**Construct a new color button.
|
/**Construct a new color button.
|
||||||
* @param parent The parent QWidget for the dialog
|
* @param parent The parent QWidget for the dialog
|
||||||
* @param cdt The title to show in the color chooser dialog
|
* @param cdt The title to show in the color chooser dialog
|
||||||
@ -70,6 +78,32 @@ class QgsColorButtonV2: QToolButton
|
|||||||
*/
|
*/
|
||||||
void setAcceptLiveUpdates( const bool accept );
|
void setAcceptLiveUpdates( const bool accept );
|
||||||
|
|
||||||
|
/**Sets whether the drop down menu should be shown for the button. The default behaviour is to
|
||||||
|
* show the menu.
|
||||||
|
* @param showMenu set to false to hide the drop down menu
|
||||||
|
* @see showMenu
|
||||||
|
*/
|
||||||
|
void setShowMenu( const bool showMenu );
|
||||||
|
|
||||||
|
/**Returns whether the drop down menu is shown for the button.
|
||||||
|
* @returns true if drop down menu is shown
|
||||||
|
* @see setShowMenu
|
||||||
|
*/
|
||||||
|
bool showMenu() const;
|
||||||
|
|
||||||
|
/**Sets the behaviour for when the button is clicked. The default behaviour is to show
|
||||||
|
* a color picker dialog.
|
||||||
|
* @param behaviour behaviour when button is clicked
|
||||||
|
* @see behaviour
|
||||||
|
*/
|
||||||
|
void setBehaviour( const Behaviour behaviour );
|
||||||
|
|
||||||
|
/**Returns the behaviour for when the button is clicked.
|
||||||
|
* @returns behaviour when button is clicked
|
||||||
|
* @see setBehaviour
|
||||||
|
*/
|
||||||
|
Behaviour behaviour() const;
|
||||||
|
|
||||||
/**Sets the default color for the button, which is shown in the button's drop down menu for the
|
/**Sets the default color for the button, which is shown in the button's drop down menu for the
|
||||||
* "default color" option.
|
* "default color" option.
|
||||||
* @param color default color for the button. Set to an invalid QColor to disable the default color
|
* @param color default color for the button. Set to an invalid QColor to disable the default color
|
||||||
@ -209,10 +243,18 @@ class QgsColorButtonV2: QToolButton
|
|||||||
*/
|
*/
|
||||||
void colorChanged( const QColor &color );
|
void colorChanged( const QColor &color );
|
||||||
|
|
||||||
|
/**Emitted when the button is clicked, if the button's behaviour is set to SignalOnly
|
||||||
|
* @param color button color
|
||||||
|
* @see setBehaviour
|
||||||
|
* @see behaviour
|
||||||
|
*/
|
||||||
|
void colorClicked( const QColor color );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void changeEvent( QEvent* e );
|
void changeEvent( QEvent* e );
|
||||||
void showEvent( QShowEvent* e );
|
void showEvent( QShowEvent* e );
|
||||||
|
void resizeEvent( QResizeEvent *event );
|
||||||
|
|
||||||
/**Returns a checkboard pattern pixmap for use as a background to transparent colors
|
/**Returns a checkboard pattern pixmap for use as a background to transparent colors
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -40,6 +40,7 @@
|
|||||||
|
|
||||||
QgsColorButtonV2::QgsColorButtonV2( QWidget *parent, QString cdt, QColorDialog::ColorDialogOptions cdo, QgsColorSchemeRegistry* registry )
|
QgsColorButtonV2::QgsColorButtonV2( QWidget *parent, QString cdt, QColorDialog::ColorDialogOptions cdo, QgsColorSchemeRegistry* registry )
|
||||||
: QToolButton( parent )
|
: QToolButton( parent )
|
||||||
|
, mBehaviour( QgsColorButtonV2::ShowDialog )
|
||||||
, mColorDialogTitle( cdt.isEmpty() ? tr( "Select Color" ) : cdt )
|
, mColorDialogTitle( cdt.isEmpty() ? tr( "Select Color" ) : cdt )
|
||||||
, mColor( Qt::black )
|
, mColor( Qt::black )
|
||||||
, mDefaultColor( QColor() ) //default to invalid color
|
, mDefaultColor( QColor() ) //default to invalid color
|
||||||
@ -57,7 +58,7 @@ QgsColorButtonV2::QgsColorButtonV2( QWidget *parent, QString cdt, QColorDialog::
|
|||||||
|
|
||||||
setAcceptDrops( true );
|
setAcceptDrops( true );
|
||||||
setMinimumSize( QSize( 24, 16 ) );
|
setMinimumSize( QSize( 24, 16 ) );
|
||||||
connect( this, SIGNAL( clicked() ), this, SLOT( showColorDialog() ) );
|
connect( this, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
|
||||||
|
|
||||||
//setup dropdown menu
|
//setup dropdown menu
|
||||||
mMenu = new QMenu( this );
|
mMenu = new QMenu( this );
|
||||||
@ -330,6 +331,19 @@ QPixmap QgsColorButtonV2::createMenuIcon( const QColor color, const bool showChe
|
|||||||
return pixmap;
|
return pixmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QgsColorButtonV2::buttonClicked()
|
||||||
|
{
|
||||||
|
switch ( mBehaviour )
|
||||||
|
{
|
||||||
|
case ShowDialog:
|
||||||
|
showColorDialog();
|
||||||
|
return;
|
||||||
|
case SignalOnly:
|
||||||
|
emit colorClicked( mColor );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void QgsColorButtonV2::prepareMenu()
|
void QgsColorButtonV2::prepareMenu()
|
||||||
{
|
{
|
||||||
//we need to tear down and rebuild this menu every time it is shown. Otherwise the space allocated to any
|
//we need to tear down and rebuild this menu every time it is shown. Otherwise the space allocated to any
|
||||||
@ -429,6 +443,14 @@ void QgsColorButtonV2::showEvent( QShowEvent* e )
|
|||||||
QToolButton::showEvent( e );
|
QToolButton::showEvent( e );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QgsColorButtonV2::resizeEvent( QResizeEvent *event )
|
||||||
|
{
|
||||||
|
QToolButton::resizeEvent( event );
|
||||||
|
//recalculate icon size and redraw icon
|
||||||
|
mIconSize = QSize();
|
||||||
|
setButtonBackground( mColor );
|
||||||
|
}
|
||||||
|
|
||||||
void QgsColorButtonV2::setColor( const QColor &color )
|
void QgsColorButtonV2::setColor( const QColor &color )
|
||||||
{
|
{
|
||||||
if ( !color.isValid() )
|
if ( !color.isValid() )
|
||||||
@ -455,6 +477,11 @@ void QgsColorButtonV2::setColor( const QColor &color )
|
|||||||
|
|
||||||
void QgsColorButtonV2::addRecentColor( const QColor color )
|
void QgsColorButtonV2::addRecentColor( const QColor color )
|
||||||
{
|
{
|
||||||
|
if ( !color.isValid() )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//strip alpha from color
|
//strip alpha from color
|
||||||
QColor opaqueColor = color;
|
QColor opaqueColor = color;
|
||||||
opaqueColor.setAlpha( 255 );
|
opaqueColor.setAlpha( 255 );
|
||||||
@ -485,26 +512,41 @@ void QgsColorButtonV2::setButtonBackground( const QColor color )
|
|||||||
|
|
||||||
bool useAlpha = ( mColorDialogOptions & QColorDialog::ShowAlphaChannel );
|
bool useAlpha = ( mColorDialogOptions & QColorDialog::ShowAlphaChannel );
|
||||||
|
|
||||||
|
QSize currentIconSize;
|
||||||
//icon size is button size with a small margin
|
//icon size is button size with a small margin
|
||||||
if ( !mIconSize.isValid() )
|
if ( menu() )
|
||||||
{
|
{
|
||||||
//calculate size of push button part of widget (ie, without the menu dropdown button part)
|
if ( !mIconSize.isValid() )
|
||||||
QStyleOptionToolButton opt;
|
{
|
||||||
initStyleOption( &opt );
|
//calculate size of push button part of widget (ie, without the menu dropdown button part)
|
||||||
QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
|
QStyleOptionToolButton opt;
|
||||||
this );
|
initStyleOption( &opt );
|
||||||
//make sure height of icon looks good under different platforms
|
QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
|
||||||
|
this );
|
||||||
|
//make sure height of icon looks good under different platforms
|
||||||
#ifdef Q_WS_WIN
|
#ifdef Q_WS_WIN
|
||||||
mIconSize = QSize( buttonSize.width() - 10, height() - 14 );
|
mIconSize = QSize( buttonSize.width() - 10, height() - 14 );
|
||||||
#else
|
#else
|
||||||
mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
|
mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
currentIconSize = mIconSize;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//no menu
|
||||||
|
#ifdef Q_WS_WIN
|
||||||
|
currentIconSize = QSize( width() - 10, height() - 14 );
|
||||||
|
#else
|
||||||
|
currentIconSize = QSize( width() - 10, height() - 12 );
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//create an icon pixmap
|
//create an icon pixmap
|
||||||
QPixmap pixmap( mIconSize );
|
QPixmap pixmap( currentIconSize );
|
||||||
pixmap.fill( Qt::transparent );
|
pixmap.fill( Qt::transparent );
|
||||||
|
|
||||||
QRect rect( 0, 0, mIconSize.width(), mIconSize.height() );
|
QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
|
||||||
QPainter p;
|
QPainter p;
|
||||||
p.begin( &pixmap );
|
p.begin( &pixmap );
|
||||||
p.setRenderHint( QPainter::Antialiasing );
|
p.setRenderHint( QPainter::Antialiasing );
|
||||||
@ -522,7 +564,7 @@ void QgsColorButtonV2::setButtonBackground( const QColor color )
|
|||||||
p.drawRoundedRect( rect, 3, 3 );
|
p.drawRoundedRect( rect, 3, 3 );
|
||||||
p.end();
|
p.end();
|
||||||
|
|
||||||
setIconSize( mIconSize );
|
setIconSize( currentIconSize );
|
||||||
setIcon( pixmap );
|
setIcon( pixmap );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -578,6 +620,20 @@ QString QgsColorButtonV2::colorDialogTitle() const
|
|||||||
return mColorDialogTitle;
|
return mColorDialogTitle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QgsColorButtonV2::setShowMenu( const bool showMenu )
|
||||||
|
{
|
||||||
|
setMenu( showMenu ? mMenu : 0 );
|
||||||
|
setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
|
||||||
|
//force recalculation of icon size
|
||||||
|
mIconSize = QSize();
|
||||||
|
setButtonBackground( mColor );
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsColorButtonV2::setBehaviour( const QgsColorButtonV2::Behaviour behaviour )
|
||||||
|
{
|
||||||
|
mBehaviour = behaviour;
|
||||||
|
}
|
||||||
|
|
||||||
void QgsColorButtonV2::setDefaultColor( const QColor color )
|
void QgsColorButtonV2::setDefaultColor( const QColor color )
|
||||||
{
|
{
|
||||||
mDefaultColor = color;
|
mDefaultColor = color;
|
||||||
|
|||||||
@ -42,6 +42,14 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
/*! Specifies the behaviour when the button is clicked
|
||||||
|
*/
|
||||||
|
enum Behaviour
|
||||||
|
{
|
||||||
|
ShowDialog = 0, /*!< show a color picker dialog when clicked */
|
||||||
|
SignalOnly /*!< emit colorClicked signal only, no dialog */
|
||||||
|
};
|
||||||
|
|
||||||
/**Construct a new color button.
|
/**Construct a new color button.
|
||||||
* @param parent The parent QWidget for the dialog
|
* @param parent The parent QWidget for the dialog
|
||||||
* @param cdt The title to show in the color chooser dialog
|
* @param cdt The title to show in the color chooser dialog
|
||||||
@ -99,6 +107,32 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
|
|||||||
*/
|
*/
|
||||||
void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
|
void setAcceptLiveUpdates( const bool accept ) { mAcceptLiveUpdates = accept; }
|
||||||
|
|
||||||
|
/**Sets whether the drop down menu should be shown for the button. The default behaviour is to
|
||||||
|
* show the menu.
|
||||||
|
* @param showMenu set to false to hide the drop down menu
|
||||||
|
* @see showMenu
|
||||||
|
*/
|
||||||
|
void setShowMenu( const bool showMenu );
|
||||||
|
|
||||||
|
/**Returns whether the drop down menu is shown for the button.
|
||||||
|
* @returns true if drop down menu is shown
|
||||||
|
* @see setShowMenu
|
||||||
|
*/
|
||||||
|
bool showMenu() const { return menu() ? true : false; }
|
||||||
|
|
||||||
|
/**Sets the behaviour for when the button is clicked. The default behaviour is to show
|
||||||
|
* a color picker dialog.
|
||||||
|
* @param behaviour behaviour when button is clicked
|
||||||
|
* @see behaviour
|
||||||
|
*/
|
||||||
|
void setBehaviour( const Behaviour behaviour );
|
||||||
|
|
||||||
|
/**Returns the behaviour for when the button is clicked.
|
||||||
|
* @returns behaviour when button is clicked
|
||||||
|
* @see setBehaviour
|
||||||
|
*/
|
||||||
|
Behaviour behaviour() const { return mBehaviour; }
|
||||||
|
|
||||||
/**Sets the default color for the button, which is shown in the button's drop down menu for the
|
/**Sets the default color for the button, which is shown in the button's drop down menu for the
|
||||||
* "default color" option.
|
* "default color" option.
|
||||||
* @param color default color for the button. Set to an invalid QColor to disable the default color
|
* @param color default color for the button. Set to an invalid QColor to disable the default color
|
||||||
@ -238,10 +272,18 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
|
|||||||
*/
|
*/
|
||||||
void colorChanged( const QColor &color );
|
void colorChanged( const QColor &color );
|
||||||
|
|
||||||
|
/**Emitted when the button is clicked, if the button's behaviour is set to SignalOnly
|
||||||
|
* @param color button color
|
||||||
|
* @see setBehaviour
|
||||||
|
* @see behaviour
|
||||||
|
*/
|
||||||
|
void colorClicked( const QColor color );
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void changeEvent( QEvent* e );
|
void changeEvent( QEvent* e );
|
||||||
void showEvent( QShowEvent* e );
|
void showEvent( QShowEvent* e );
|
||||||
|
void resizeEvent( QResizeEvent *event );
|
||||||
|
|
||||||
/**Returns a checkboard pattern pixmap for use as a background to transparent colors
|
/**Returns a checkboard pattern pixmap for use as a background to transparent colors
|
||||||
*/
|
*/
|
||||||
@ -283,6 +325,8 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
|
|||||||
void dropEvent( QDropEvent *e );
|
void dropEvent( QDropEvent *e );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
Behaviour mBehaviour;
|
||||||
QString mColorDialogTitle;
|
QString mColorDialogTitle;
|
||||||
QColor mColor;
|
QColor mColor;
|
||||||
|
|
||||||
@ -334,6 +378,8 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
|
|||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
|
void buttonClicked();
|
||||||
|
|
||||||
void showColorDialog();
|
void showColorDialog();
|
||||||
|
|
||||||
/**Sets color for button, if valid.
|
/**Sets color for button, if valid.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user