24 #include <QTemporaryFile>
25 #include <QMouseEvent>
32 QString QgsColorButton::fullPath(
const QString &path )
35 int len = GetLongPathName( path.toUtf8().constData(), buf, MAX_PATH );
37 if ( len == 0 || len > MAX_PATH )
39 QgsDebugMsg( QString(
"GetLongPathName('%1') failed with %2: %3" )
40 .arg( path ).arg( len ).arg( GetLastError() ) );
44 QString res = QString::fromUtf8( buf );
66 : QPushButton( parent )
67 , mColorDialogTitle( cdt.isEmpty() ?
tr(
"Select Color" ) : cdt )
69 , mColorDialogOptions( cdo )
70 , mAcceptLiveUpdates( true )
74 setAcceptDrops(
true );
88 if ( transpBkgrd.isNull() )
99 if (
mAcceptLiveUpdates && settings.value(
"/qgis/live_color_dialogs",
false ).toBool() )
117 if ( e->button() == Qt::RightButton )
122 else if ( e->button() == Qt::LeftButton )
126 QPushButton::mousePressEvent( e );
131 QMimeData *mimeData =
new QMimeData;
132 mimeData->setColorData( QVariant(
mColor ) );
133 mimeData->setText(
mColor.name() );
140 QColor mimeColor = mimeData->colorData().value<QColor>();
141 if ( mimeColor.isValid() )
146 mimeColor.setAlpha( 255 );
148 resultColor = mimeColor;
153 bool hasAlpha =
false;
155 if ( textColor.isValid() )
160 textColor.setAlpha( 255 );
162 else if ( !hasAlpha )
165 textColor.setAlpha(
mColor.alpha() );
167 resultColor = textColor;
177 if ( !( e->buttons() & Qt::LeftButton ) )
179 QPushButton::mouseMoveEvent( e );
183 if (( e->pos() -
mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
185 QPushButton::mouseMoveEvent( e );
189 QDrag *drag =
new QDrag(
this );
193 QImage colorImage( 50, 50, QImage::Format_RGB32 );
194 QPainter imagePainter;
195 imagePainter.begin( &colorImage );
197 imagePainter.fillRect( QRect( 0, 0, 50, 50 ), QBrush( QColor( 200, 200, 200 ) ) );
199 QColor pixmapColor =
mColor;
200 pixmapColor.setAlpha( 255 );
201 imagePainter.setBrush( QBrush( pixmapColor ) );
202 imagePainter.setPen( QPen( Qt::white ) );
203 imagePainter.drawRect( QRect( 1, 1, 47, 47 ) );
206 drag->setPixmap( QPixmap::fromImage( colorImage ) );
208 drag->exec( Qt::CopyAction );
218 e->acceptProposedAction();
228 e->acceptProposedAction();
235 QMenu colorContextMenu;
237 QAction* copyColorAction =
new QAction(
tr(
"Copy color" ), 0 );
238 colorContextMenu.addAction( copyColorAction );
239 QAction* pasteColorAction =
new QAction(
tr(
"Paste color" ), 0 );
240 pasteColorAction->setEnabled(
false );
241 colorContextMenu.addSeparator();
242 colorContextMenu.addAction( pasteColorAction );
247 pasteColorAction->setEnabled(
true );
250 QAction* selectedAction = colorContextMenu.exec( event->globalPos( ) );
251 if ( selectedAction == copyColorAction )
256 else if ( selectedAction == pasteColorAction )
262 delete copyColorAction;
263 delete pasteColorAction;
268 if ( newColor.isValid() )
276 if ( e->type() == QEvent::EnabledChange )
280 QPushButton::changeEvent( e );
283 #if 0 // causes too many cyclical updates, but may be needed on some platforms
284 void QgsColorButton::paintEvent( QPaintEvent* e )
286 QPushButton::paintEvent( e );
288 if ( !mBackgroundSet )
298 QPushButton::showEvent( e );
303 if ( !color.isValid() )
326 if ( !text().isEmpty() )
332 pixmap = QPixmap( iconSize() );
333 pixmap.fill( QColor( 0, 0, 0, 0 ) );
335 int iconW = iconSize().width();
336 int iconH = iconSize().height();
337 QRect rect( 0, 0, iconW, iconH );
340 QPainterPath roundRect;
343 roundRect.moveTo( chamfer, inset );
344 roundRect.lineTo( iconW - chamfer, inset );
345 roundRect.lineTo( iconW - inset, chamfer );
346 roundRect.lineTo( iconW - inset, iconH - chamfer );
347 roundRect.lineTo( iconW - chamfer, iconH - inset );
348 roundRect.lineTo( chamfer, iconH - inset );
349 roundRect.lineTo( inset, iconH - chamfer );
350 roundRect.lineTo( inset, chamfer );
351 roundRect.closeSubpath();
355 p.setRenderHint( QPainter::Antialiasing );
356 p.setClipPath( roundRect );
357 p.setPen( Qt::NoPen );
358 if (
mColor.alpha() < 255 )
367 setIcon( QIcon( pixmap ) );
379 QString margin = QString(
"%1px %2px %3px %4px" ).arg( 0 ).arg( 0 ).arg( 0 ).arg( 0 );
383 QString bkgrd = QString(
" background-color: rgba(%1,%2,%3,%4);" )
387 .arg( useAlpha ?
mColor.alpha() : 255 );
389 if ( useAlpha &&
mColor.alpha() < 255 )
392 QRect rect( 0, 0, pixmap.width(), pixmap.height() );
396 p.setRenderHint( QPainter::Antialiasing );
397 p.setPen( Qt::NoPen );
405 pixmap.save(
mTempPNG.fileName(),
"PNG" );
409 QString bgFileName =
mTempPNG.fileName();
413 bgFileName = fullPath( bgFileName );
415 bkgrd = QString(
" background-image: url(%1);" ).arg( bgFileName );
419 setStyleSheet( QString(
"QgsColorButton{"
421 " background-position: top left;"
422 " background-origin: content;"
423 " background-clip: content;"
428 " border-width: 1px;"
429 " border-color: rgb(%3,%3,%3);"
430 " border-radius: 3px;} "
431 "QgsColorButton:pressed{"
433 " background-position: top left;"
434 " background-origin: content;"
435 " background-clip: content;"
439 " border-style: inset;"
440 " border-width: 2px;"
441 " border-color: rgb(128,128,128);"
442 " border-radius: 4px;} " )
445 .arg( isEnabled() ?
"128" :
"110" )
446 .arg( isEnabled() ?
"outset" :
"dotted" ) );
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
static QColor parseColorWithAlpha(const QString colorStr, bool &containsAlpha)
Attempts to parse a string as a color using a variety of common formats, including hex codes...
static QColor getLiveColor(const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent=0, const QString &title="", QColorDialog::ColorDialogOptions options=0)
Return a color selection from a QColorDialog, with live updating of interim selections.