28 #include <QTemporaryFile> 29 #include <QMouseEvent> 33 #include <QDesktopWidget> 35 #include <QStyleOptionToolButton> 36 #include <QWidgetAction> 38 #include <QGridLayout> 39 #include <QPushButton> 44 , mColorDialogTitle( cdt.isEmpty() ? tr(
"Select Color" ) : cdt )
46 , mDefaultColor(
QColor() )
47 , mAllowAlpha( false )
48 , mAcceptLiveUpdates( true )
50 , mShowNoColorOption( false )
51 , mNoColorString( tr(
"No color" ) )
53 , mPickingColor( false )
62 connect(
this, SIGNAL(
clicked() ),
this, SLOT( buttonClicked() ) );
65 mMenu =
new QMenu(
this );
66 connect( mMenu, SIGNAL( aboutToShow() ),
this, SLOT( prepareMenu() ) );
79 return QSize( 120, 22 );
81 return QSize( 120, 28 );
89 if ( transpBkgrd.
isNull() )
95 void QgsColorButtonV2::showColorDialog()
110 connect( colorWidget, SIGNAL( currentColorChanged(
QColor ) ),
this, SLOT( setValidTemporaryColor(
QColor ) ) );
112 panel->openPanel( colorWidget );
120 bool useNative = settings.
value(
"/qgis/native_color_dialogs",
false ).
toBool();
125 if ( mAcceptLiveUpdates && settings.
value(
"/qgis/live_color_dialogs",
false ).
toBool() )
128 color(),
this, SLOT( setValidColor(
const QColor& ) ),
129 this->
parentWidget(), mColorDialogTitle, mAllowAlpha ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
139 if ( mAcceptLiveUpdates && settings.
value(
"/qgis/live_color_dialogs",
false ).
toBool() )
142 color(),
this, SLOT( setValidColor(
const QColor& ) ),
148 dialog.
setTitle( mColorDialogTitle );
153 newColor = dialog.
color();
160 setValidColor( newColor );
161 addRecentColor( newColor );
170 if ( !mDefaultColor.
isValid() )
185 if ( e->
type() == QEvent::ToolTip )
193 "HSV: %3,%4,%5" ).
arg( name )
195 .
arg( hue ).
arg( saturation ).
arg( value );
220 if ( e->
button() == Qt::RightButton )
225 else if ( e->
button() == Qt::LeftButton )
227 mDragStartPosition = e->
pos();
232 bool QgsColorButtonV2::colorFromMimeData(
const QMimeData * mimeData,
QColor& resultColor )
234 bool hasAlpha =
false;
244 else if ( !hasAlpha )
249 resultColor = mimeColor;
262 if ( e->
buttons() & Qt::LeftButton )
295 drag->
exec( Qt::CopyAction );
312 void QgsColorButtonV2::stopPicking(
QPointF eventPos,
bool sampleColor )
318 mPickingColor =
false;
330 setColor( snappedImage.pixel( 0, 0 ) );
331 addRecentColor( mColor );
336 if ( !mPickingColor )
351 if ( colorFromMimeData( e->
mimeData(), mimeColor ) )
372 if ( colorFromMimeData( e->
mimeData(), mimeColor ) )
377 addRecentColor( mimeColor );
381 void QgsColorButtonV2::setValidColor(
const QColor& newColor )
386 addRecentColor( newColor );
390 void QgsColorButtonV2::setValidTemporaryColor(
const QColor& newColor )
402 addRecentColor( colorWidget->color() );
403 colorWidget->deleteLater();
407 QPixmap QgsColorButtonV2::createMenuIcon(
const QColor &
color,
const bool showChecks )
411 pixmap.
fill( Qt::transparent );
435 void QgsColorButtonV2::buttonClicked()
437 switch ( mBehaviour )
448 void QgsColorButtonV2::prepareMenu()
459 nullAction->
setIcon( createMenuIcon( Qt::transparent,
false ) );
468 defaultColorAction->
setIcon( createMenuIcon( mDefaultColor ) );
473 if ( mShowNoColorOption && mAllowAlpha )
476 noColorAction->
setIcon( createMenuIcon( Qt::transparent,
false ) );
490 if ( mColorSchemeRegistry )
495 for ( ; it != schemeList.
end(); ++it )
517 pasteColorAction->
setIcon( createMenuIcon( clipColor ) );
536 connect( chooseColorAction, SIGNAL(
triggered() ),
this, SLOT( showColorDialog() ) );
541 if ( e->
type() == QEvent::EnabledChange )
548 #if 0 // causes too many cyclical updates, but may be needed on some platforms 553 if ( !mBackgroundSet )
580 if ( oldColor != mColor || ( mColor ==
QColor( Qt::black ) && !mColorSet ) )
593 void QgsColorButtonV2::addRecentColor(
const QColor& color )
604 backgroundColor = mColor;
607 QSize currentIconSize;
625 currentIconSize = mIconSize;
637 if ( !currentIconSize.
isValid() || currentIconSize.
width() <= 0 || currentIconSize.
height() <= 0 )
644 pixmap.
fill( Qt::transparent );
646 if ( backgroundColor.
isValid() )
651 p.setRenderHint( QPainter::Antialiasing );
652 p.setPen( Qt::NoPen );
653 if ( mAllowAlpha && backgroundColor.
alpha() < 255 )
657 p.setBrush( checkBrush );
658 p.drawRoundedRect(
rect, 3, 3 );
662 p.setBrush( backgroundColor );
663 p.drawRoundedRect(
rect, 3, 3 );
684 addRecentColor( clipColor );
695 mPickingColor =
true;
710 mColorDialogTitle = title;
715 return mColorDialogTitle;
720 setMenu( showMenu ? mMenu :
nullptr );
721 setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
734 mDefaultColor =
color;
A color swatch grid which can be embedded into a menu.
Registry of color schemes.
const QMimeData * mimeData() const
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
QColor color() const
Returns the current color for the dialog.
void fill(const QColor &color)
void setMimeData(QMimeData *data)
A custom QGIS dialog for selecting a color.
static QString encodeColor(const QColor &color)
void setPixmap(const QPixmap &pixmap)
void setIcon(const QIcon &icon)
static QColor colorFromMimeData(const QMimeData *data, bool &hasAlpha)
Attempts to parse mime data as a color.
QPixmap grabWindow(WId window, int x, int y, int width, int height)
virtual QRect subControlRect(ComplexControl control, const QStyleOptionComplex *option, SubControl subControl, const QWidget *widget) const=0
void acceptProposedAction()
QString tr(const char *sourceText, const char *disambiguation, int n)
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
uint pixel(int screen) const
Qt::DropAction exec(QFlags< Qt::DropAction > supportedActions)
void setAllowAlpha(const bool allowAlpha)
Sets whether alpha modification (transparency) is permitted for the color dialog. ...
void drawRect(const QRectF &rectangle)
const char * name() const
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
const QPoint & globalPos() const
void setPen(const QColor &color)
void setTitle(const QString &title)
Sets the title for the color dialog.
void setBrush(const QBrush &brush)
void setMimeData(QMimeData *src, Mode mode)
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
QColor getColor(const QColor &initial, QWidget *parent, const QString &title, QFlags< QColorDialog::ColorDialogOption > options)
static QgsColorSchemeRegistry * instance()
Returns the global instance pointer, creating the object on the first call.
const char * sampler_cursor[]
QVariant value(const QString &key, const QVariant &defaultValue) const
QDesktopWidget * desktop()
virtual void setColor(const QColor &color, const bool emitSignals=false) override
const QPoint & pos() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool begin(QPaintDevice *device)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
static QColor getLiveColor(const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent=nullptr, const QString &title="", const QColorDialog::ColorDialogOptions &options=nullptr)
Return a color selection from a QColorDialog, with live updating of interim selections.
static QColor getLiveColor(const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent=nullptr, const QString &title=QString(), const bool allowAlpha=true)
Return a color selection from a color dialog, with live updating of interim selections.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.