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" ) )
52 , mPickingColor( false )
61 connect(
this, SIGNAL(
clicked() ),
this, SLOT( buttonClicked() ) );
64 mMenu =
new QMenu(
this );
65 connect( mMenu, SIGNAL( aboutToShow() ),
this, SLOT( prepareMenu() ) );
78 return QSize( 120, 22 );
80 return QSize( 120, 28 );
88 if ( transpBkgrd.
isNull() )
94 void QgsColorButtonV2::showColorDialog()
100 bool useNative = settings.
value(
"/qgis/native_color_dialogs",
false ).
toBool();
105 if ( mAcceptLiveUpdates && settings.
value(
"/qgis/live_color_dialogs",
false ).
toBool() )
108 color(),
this, SLOT( setValidColor(
const QColor& ) ),
109 this->
parentWidget(), mColorDialogTitle, mAllowAlpha ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
119 if ( mAcceptLiveUpdates && settings.
value(
"/qgis/live_color_dialogs",
false ).
toBool() )
122 color(),
this, SLOT( setValidColor(
const QColor& ) ),
128 dialog.
setTitle( mColorDialogTitle );
133 newColor = dialog.
color();
140 setValidColor( newColor );
141 addRecentColor( newColor );
150 if ( !mDefaultColor.
isValid() )
175 if ( e->
button() == Qt::RightButton )
180 else if ( e->
button() == Qt::LeftButton )
182 mDragStartPosition = e->
pos();
187 bool QgsColorButtonV2::colorFromMimeData(
const QMimeData * mimeData,
QColor& resultColor )
189 bool hasAlpha =
false;
199 else if ( !hasAlpha )
204 resultColor = mimeColor;
217 if ( e->
buttons() & Qt::LeftButton )
250 drag->
exec( Qt::CopyAction );
267 void QgsColorButtonV2::stopPicking(
QPointF eventPos,
bool sampleColor )
273 mPickingColor =
false;
285 setColor( snappedImage.pixel( 0, 0 ) );
286 addRecentColor( mColor );
291 if ( !mPickingColor )
306 if ( colorFromMimeData( e->
mimeData(), mimeColor ) )
327 if ( colorFromMimeData( e->
mimeData(), mimeColor ) )
332 addRecentColor( mimeColor );
336 void QgsColorButtonV2::setValidColor(
const QColor& newColor )
341 addRecentColor( newColor );
345 QPixmap QgsColorButtonV2::createMenuIcon(
const QColor &
color,
const bool showChecks )
349 pixmap.
fill( Qt::transparent );
373 void QgsColorButtonV2::buttonClicked()
375 switch ( mBehaviour )
386 void QgsColorButtonV2::prepareMenu()
398 defaultColorAction->
setIcon( createMenuIcon( mDefaultColor ) );
403 if ( mShowNoColorOption && mAllowAlpha )
406 noColorAction->
setIcon( createMenuIcon( Qt::transparent,
false ) );
411 if ( mColorSchemeRegistry )
416 for ( ; it != schemeList.
end(); ++it )
438 pasteColorAction->
setIcon( createMenuIcon( clipColor ) );
457 connect( chooseColorAction, SIGNAL(
triggered() ),
this, SLOT( showColorDialog() ) );
462 if ( e->
type() == QEvent::EnabledChange )
469 #if 0 // causes too many cyclical updates, but may be needed on some platforms 474 if ( !mBackgroundSet )
501 if ( oldColor != mColor || ( mColor ==
QColor( Qt::black ) && !mColorSet ) )
514 void QgsColorButtonV2::addRecentColor(
const QColor& color )
525 backgroundColor = mColor;
528 QSize currentIconSize;
546 currentIconSize = mIconSize;
558 if ( !currentIconSize.
isValid() || currentIconSize.
width() <= 0 || currentIconSize.
height() <= 0 )
565 pixmap.
fill( Qt::transparent );
567 if ( backgroundColor.
isValid() )
572 p.setRenderHint( QPainter::Antialiasing );
573 p.setPen( Qt::NoPen );
574 if ( mAllowAlpha && backgroundColor.
alpha() < 255 )
578 p.setBrush( checkBrush );
579 p.drawRoundedRect(
rect, 3, 3 );
583 p.setBrush( backgroundColor );
584 p.drawRoundedRect(
rect, 3, 3 );
605 addRecentColor( clipColor );
616 mPickingColor =
true;
631 mColorDialogTitle = title;
636 return mColorDialogTitle;
641 setMenu( showMenu ? mMenu :
nullptr );
642 setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
655 mDefaultColor =
color;
Registry of color schemes.
const QMimeData * mimeData() const
static QMimeData * colorToMimeData(const QColor &color)
Creates mime data from a color.
void fill(const QColor &color)
void setMimeData(QMimeData *data)
A custom QGIS dialog for selecting a 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 QPoint & globalPos() const
QColor color() const
Returns the current color for the dialog.
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()
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)
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
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.