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 );
129 dialog.setAllowAlpha( mAllowAlpha );
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 )
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 )
529 for (
int colorIdx = recentColorVariants.
length() - 1; colorIdx >= 0; --colorIdx )
531 if (( recentColorVariants.
at( colorIdx ).value<
QColor>() ).name() == opaqueColor.
name() )
533 recentColorVariants.
removeAt( colorIdx );
539 recentColorVariants.
prepend( colorVariant );
542 while ( recentColorVariants.
count() > 20 )
556 backgroundColor = mColor;
559 QSize currentIconSize;
577 currentIconSize = mIconSize;
589 if ( !currentIconSize.
isValid() || currentIconSize.
width() <= 0 || currentIconSize.
height() <= 0 )
596 pixmap.
fill( Qt::transparent );
598 if ( backgroundColor.
isValid() )
603 p.setRenderHint( QPainter::Antialiasing );
604 p.setPen( Qt::NoPen );
605 if ( mAllowAlpha && backgroundColor.
alpha() < 255 )
609 p.setBrush( checkBrush );
610 p.drawRoundedRect(
rect, 3, 3 );
614 p.setBrush( backgroundColor );
615 p.drawRoundedRect(
rect, 3, 3 );
636 addRecentColor( clipColor );
647 mPickingColor =
true;
662 mColorDialogTitle = title;
667 return mColorDialogTitle;
672 setMenu( showMenu ? mMenu : 0 );
673 setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
686 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.
QList< QVariant > toList() const
void setPixmap(const QPixmap &pixmap)
void setIcon(const QIcon &icon)
const T & at(int i) const
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 setValue(const QString &key, const QVariant &value)
void drawRect(const QRectF &rectangle)
int count(const T &value) const
const QPoint & globalPos() const
void setPen(const QColor &color)
void setBrush(const QBrush &brush)
void setMimeData(QMimeData *src, Mode mode)
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()
static QColor getLiveColor(const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent=0, const QString &title="", const QColorDialog::ColorDialogOptions &options=0)
Return a color selection from a QColorDialog, with live updating of interim selections.
void prepend(const T &value)
static QColor getLiveColor(const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent=0, const QString &title=QString(), const bool allowAlpha=true)
Return a color selection from a color dialog, with live updating of interim selections.
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.
void setBaseColor(const QColor &baseColor)
Sets the base color for the color grid.