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() )
177 if ( e->
button() == Qt::RightButton )
182 else if ( e->
button() == Qt::LeftButton )
184 mDragStartPosition = e->
pos();
189 bool QgsColorButtonV2::colorFromMimeData(
const QMimeData * mimeData,
QColor& resultColor )
191 bool hasAlpha =
false;
201 else if ( !hasAlpha )
206 resultColor = mimeColor;
219 if ( e->
buttons() & Qt::LeftButton )
252 drag->
exec( Qt::CopyAction );
269 void QgsColorButtonV2::stopPicking(
QPointF eventPos,
bool sampleColor )
275 mPickingColor =
false;
287 setColor( snappedImage.pixel( 0, 0 ) );
288 addRecentColor( mColor );
293 if ( !mPickingColor )
308 if ( colorFromMimeData( e->
mimeData(), mimeColor ) )
329 if ( colorFromMimeData( e->
mimeData(), mimeColor ) )
334 addRecentColor( mimeColor );
338 void QgsColorButtonV2::setValidColor(
const QColor& newColor )
343 addRecentColor( newColor );
347 QPixmap QgsColorButtonV2::createMenuIcon(
const QColor &
color,
const bool showChecks )
351 pixmap.
fill( Qt::transparent );
375 void QgsColorButtonV2::buttonClicked()
377 switch ( mBehaviour )
388 void QgsColorButtonV2::prepareMenu()
400 defaultColorAction->
setIcon( createMenuIcon( mDefaultColor ) );
405 if ( mShowNoColorOption && mAllowAlpha )
408 noColorAction->
setIcon( createMenuIcon( Qt::transparent,
false ) );
413 if ( mColorSchemeRegistry )
418 for ( ; it != schemeList.
end(); ++it )
440 pasteColorAction->
setIcon( createMenuIcon( clipColor ) );
459 connect( chooseColorAction, SIGNAL(
triggered() ),
this, SLOT( showColorDialog() ) );
464 if ( e->
type() == QEvent::EnabledChange )
471 #if 0 // causes too many cyclical updates, but may be needed on some platforms 476 if ( !mBackgroundSet )
503 if ( oldColor != mColor || ( mColor ==
QColor( Qt::black ) && !mColorSet ) )
516 void QgsColorButtonV2::addRecentColor(
const QColor& color )
527 backgroundColor = mColor;
530 QSize currentIconSize;
548 currentIconSize = mIconSize;
560 if ( !currentIconSize.
isValid() || currentIconSize.
width() <= 0 || currentIconSize.
height() <= 0 )
567 pixmap.
fill( Qt::transparent );
569 if ( backgroundColor.
isValid() )
574 p.setRenderHint( QPainter::Antialiasing );
575 p.setPen( Qt::NoPen );
576 if ( mAllowAlpha && backgroundColor.
alpha() < 255 )
580 p.setBrush( checkBrush );
581 p.drawRoundedRect(
rect, 3, 3 );
585 p.setBrush( backgroundColor );
586 p.drawRoundedRect(
rect, 3, 3 );
607 addRecentColor( clipColor );
618 mPickingColor =
true;
633 mColorDialogTitle = title;
638 return mColorDialogTitle;
643 setMenu( showMenu ? mMenu :
nullptr );
644 setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
657 mDefaultColor =
color;
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.
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)
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()
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)
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.