25 #include <QTemporaryFile> 26 #include <QMouseEvent> 30 #include <QDesktopWidget> 39 if ( len == 0 || len > MAX_PATH )
42 .arg( path ).arg( len ).arg( GetLastError() ) );
69 , mColorDialogTitle( cdt.isEmpty() ?
tr(
"Select Color" ) : cdt )
71 , mColorDialogOptions( cdo )
72 , mAcceptLiveUpdates( true )
75 , mPickingColor( false )
78 connect(
this, SIGNAL(
clicked() ),
this, SLOT( onButtonClicked() ) );
91 if ( transpBkgrd.
isNull() )
97 void QgsColorButton::onButtonClicked()
103 bool useNative = settings.
value(
"/qgis/native_color_dialogs",
false ).
toBool();
107 if ( mAcceptLiveUpdates && settings.
value(
"/qgis/live_color_dialogs",
false ).
toBool() )
110 color(),
this, SLOT( setValidColor(
const QColor& ) ),
111 this->
parentWidget(), mColorDialogTitle, mColorDialogOptions );
121 if ( mAcceptLiveUpdates && settings.
value(
"/qgis/live_color_dialogs",
false ).
toBool() )
124 color(),
this, SLOT( setValidColor(
const QColor& ) ),
125 this->
parentWidget(), mColorDialogTitle, mColorDialogOptions & QColorDialog::ShowAlphaChannel );
130 dialog.
setTitle( mColorDialogTitle );
131 dialog.
setAllowAlpha( mColorDialogOptions & QColorDialog::ShowAlphaChannel );
135 newColor = dialog.
color();
142 setValidColor( newColor );
158 if ( e->
button() == Qt::RightButton )
160 showContextMenu( e );
163 else if ( e->
button() == Qt::LeftButton )
165 mDragStartPosition = e->
pos();
170 QMimeData * QgsColorButton::createColorMimeData()
const 178 bool QgsColorButton::colorFromMimeData(
const QMimeData * mimeData,
QColor& resultColor )
184 if ( !( mColorDialogOptions & QColorDialog::ShowAlphaChannel ) )
189 resultColor = mimeColor;
194 bool hasAlpha =
false;
198 if ( !( mColorDialogOptions & QColorDialog::ShowAlphaChannel ) )
203 else if ( !hasAlpha )
208 resultColor = textColor;
221 if ( e->
buttons() & Qt::LeftButton )
235 if ( !( e->
buttons() & Qt::LeftButton ) )
251 QImage colorImage( 50, 50, QImage::Format_RGB32 );
253 imagePainter.
begin( &colorImage );
257 QColor pixmapColor = mColor;
266 drag->
exec( Qt::CopyAction );
283 void QgsColorButton::stopPicking(
QPointF eventPos,
bool sampleColor )
288 mPickingColor =
false;
300 setColor( snappedImage.pixel( 0, 0 ) );
305 if ( !mPickingColor )
320 if ( colorFromMimeData( e->
mimeData(), mimeColor ) )
330 if ( colorFromMimeData( e->
mimeData(), mimeColor ) )
339 QMenu colorContextMenu;
342 colorContextMenu.
addAction( copyColorAction );
345 colorContextMenu.
addAction( pasteColorAction );
352 colorContextMenu.
addAction( pickColorAction );
362 if ( selectedAction == copyColorAction )
367 else if ( selectedAction == pasteColorAction )
373 else if ( selectedAction == pickColorAction )
379 mPickingColor =
true;
381 delete pickColorAction;
384 delete copyColorAction;
385 delete pasteColorAction;
388 void QgsColorButton::setValidColor(
const QColor& newColor )
398 if ( e->
type() == QEvent::EnabledChange )
405 #if 0 // causes too many cyclical updates, but may be needed on some platforms 410 if ( !mBackgroundSet )
433 if ( oldColor != mColor || ( mColor ==
QColor( Qt::black ) && !mColorSet ) )
469 roundRect.
moveTo( chamfer, inset );
470 roundRect.
lineTo( iconW - chamfer, inset );
471 roundRect.
lineTo( iconW - inset, chamfer );
472 roundRect.
lineTo( iconW - inset, iconH - chamfer );
473 roundRect.
lineTo( iconW - chamfer, iconH - inset );
474 roundRect.
lineTo( chamfer, iconH - inset );
475 roundRect.
lineTo( inset, iconH - chamfer );
476 roundRect.
lineTo( inset, chamfer );
484 if ( color.
alpha() < 255 )
502 bool useAlpha = ( mColorDialogOptions & QColorDialog::ShowAlphaChannel );
509 QString bkgrd =
QString(
" background-color: rgba(%1,%2,%3,%4);" )
511 .arg( color.
green() )
513 .arg( useAlpha ? color.
alpha() : 255 );
515 if ( useAlpha && color.
alpha() < 255 )
522 p.setRenderHint( QPainter::Antialiasing );
523 p.setPen( Qt::NoPen );
524 p.setBrush( mColor );
528 if ( mTempPNG.
open() )
539 bgFileName = fullPath( bgFileName );
541 bkgrd =
QString(
" background-image: url(%1);" ).
arg( bgFileName );
547 " background-position: top left;" 548 " background-origin: content;" 549 " background-clip: content;" 554 " border-width: 1px;" 555 " border-color: rgb(%3,%3,%3);" 556 " border-radius: 3px;} " 557 "QgsColorButton:pressed{" 559 " background-position: top left;" 560 " background-origin: content;" 561 " background-clip: content;" 565 " border-style: inset;" 566 " border-width: 2px;" 567 " border-color: rgb(128,128,128);" 568 " border-radius: 4px;} " )
583 mColorDialogOptions = cdo;
588 return mColorDialogOptions;
593 mColorDialogTitle = cdt;
598 return mColorDialogTitle;
void setClipPath(const QPainterPath &path, Qt::ClipOperation operation)
const QMimeData * mimeData() const
void fillRect(const QRectF &rectangle, const QBrush &brush)
QColor color() const
Returns the current color for the dialog.
void setRenderHint(RenderHint hint, bool on)
void fill(const QColor &color)
void setMimeData(QMimeData *data)
A custom QGIS dialog for selecting a color.
void setColorData(const QVariant &color)
void setPixmap(const QPixmap &pixmap)
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
void moveTo(const QPointF &point)
void drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position)
QPixmap grabWindow(WId window, int x, int y, int width, int height)
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
QString fromUtf8(const char *str, int size)
bool save(const QString &fileName, const char *format, int quality) const
void setPen(const QColor &color)
void lineTo(const QPointF &endPoint)
typedef ColorDialogOptions
void setAutoRemove(bool b)
const char * constData() const
void setTitle(const QString &title)
Sets the title for the color dialog.
void setBrush(const QBrush &brush)
void setText(const QString &text)
void setMimeData(QMimeData *src, Mode mode)
QVariant colorData() const
QColor getColor(const QColor &initial, QWidget *parent, const QString &title, QFlags< QColorDialog::ColorDialogOption > options)
const char * sampler_cursor[]
QVariant value(const QString &key, const QVariant &defaultValue) const
static QColor parseColorWithAlpha(const QString &colorStr, bool &containsAlpha, bool strictEval=false)
Attempts to parse a string as a color using a variety of common formats, including hex codes...
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)
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.
QByteArray toUtf8() const