28 #include <QTemporaryFile>
29 #include <QMouseEvent>
33 #include <QDesktopWidget>
35 #include <QStyleOptionToolButton>
36 #include <QWidgetAction>
38 #include <QGridLayout>
39 #include <QPushButton>
42 : QToolButton( parent )
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 )
59 setAcceptDrops(
true );
60 setMinimumSize( QSize( 24, 16 ) );
61 connect(
this, SIGNAL( clicked() ),
this, SLOT( buttonClicked() ) );
64 mMenu =
new QMenu(
this );
65 connect( mMenu, SIGNAL( aboutToShow() ),
this, SLOT( prepareMenu() ) );
67 setPopupMode( QToolButton::MenuButtonPopup );
78 return QSize( 120, 22 );
80 return QSize( 120, 28 );
86 static QPixmap transpBkgrd;
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 );
113 newColor = QColorDialog::getColor(
color(), 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& ) ),
123 this->parentWidget(), mColorDialogTitle, mAllowAlpha );
128 dialog.setTitle( mColorDialogTitle );
129 dialog.setAllowAlpha( mAllowAlpha );
133 newColor = dialog.color();
138 if ( newColor.isValid() )
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;
192 if ( mimeColor.isValid() )
197 mimeColor.setAlpha( 255 );
199 else if ( !hasAlpha )
202 mimeColor.setAlpha( mColor.alpha() );
204 resultColor = mimeColor;
217 if ( e->buttons() & Qt::LeftButton )
221 QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), e->globalPos().x(), e->globalPos().y(), 1, 1 );
222 QImage snappedImage = snappedPixmap.toImage();
223 QColor hoverColor = snappedImage.pixel( 0, 0 );
232 if ( !( e->buttons() & Qt::LeftButton ) || !mColor.isValid() )
239 if (( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
247 QDrag *drag =
new QDrag(
this );
250 drag->exec( Qt::CopyAction );
259 stopPicking( e->globalPos() );
267 void QgsColorButtonV2::stopPicking( QPointF eventPos,
bool sampleColor )
273 mPickingColor =
false;
282 QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), eventPos.x(), eventPos.y(), 1, 1 );
283 QImage snappedImage = snappedPixmap.toImage();
285 setColor( snappedImage.pixel( 0, 0 ) );
286 addRecentColor( mColor );
291 if ( !mPickingColor )
299 stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
306 if ( colorFromMimeData( e->mimeData(), mimeColor ) )
311 e->acceptProposedAction();
327 if ( colorFromMimeData( e->mimeData(), mimeColor ) )
330 e->acceptProposedAction();
332 addRecentColor( mimeColor );
336 void QgsColorButtonV2::setValidColor(
const QColor& newColor )
338 if ( newColor.isValid() )
341 addRecentColor( newColor );
345 QPixmap QgsColorButtonV2::createMenuIcon(
const QColor &color,
const bool showChecks )
348 QPixmap pixmap( 16, 16 );
349 pixmap.fill( Qt::transparent );
358 p.setPen( Qt::NoPen );
359 p.setBrush( checkBrush );
360 p.drawRect( 0, 0, 15, 15 );
364 p.setBrush( QBrush( color ) );
367 p.setPen( QColor( 197, 197, 197 ) );
368 p.drawRect( 0, 0, 15, 15 );
373 void QgsColorButtonV2::buttonClicked()
375 switch ( mBehaviour )
386 void QgsColorButtonV2::prepareMenu()
395 if ( mDefaultColor.isValid() )
397 QAction* defaultColorAction =
new QAction(
tr(
"Default color" ),
this );
398 defaultColorAction->setIcon( createMenuIcon( mDefaultColor ) );
399 mMenu->addAction( defaultColorAction );
400 connect( defaultColorAction, SIGNAL( triggered() ),
this, SLOT(
setToDefaultColor() ) );
403 if ( mShowNoColorOption && mAllowAlpha )
405 QAction* noColorAction =
new QAction( mNoColorString,
this );
406 noColorAction->setIcon( createMenuIcon( Qt::transparent,
false ) );
407 mMenu->addAction( noColorAction );
408 connect( noColorAction, SIGNAL( triggered() ),
this, SLOT(
setToNoColor() ) );
411 if ( mColorSchemeRegistry )
415 QList< QgsColorScheme* >::iterator it = schemeList.begin();
416 for ( ; it != schemeList.end(); ++it )
420 mMenu->addAction( colorAction );
421 connect( colorAction, SIGNAL(
colorChanged(
const QColor& ) ),
this, SLOT( setValidColor(
const QColor& ) ) );
422 connect( colorAction, SIGNAL(
colorChanged(
const QColor& ) ),
this, SLOT( addRecentColor(
const QColor& ) ) );
426 mMenu->addSeparator();
428 QAction* copyColorAction =
new QAction(
tr(
"Copy color" ),
this );
429 mMenu->addAction( copyColorAction );
430 connect( copyColorAction, SIGNAL( triggered() ),
this, SLOT(
copyColor() ) );
432 QAction* pasteColorAction =
new QAction(
tr(
"Paste color" ),
this );
436 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
438 pasteColorAction->setIcon( createMenuIcon( clipColor ) );
442 pasteColorAction->setEnabled(
false );
444 mMenu->addAction( pasteColorAction );
445 connect( pasteColorAction, SIGNAL( triggered() ),
this, SLOT(
pasteColor() ) );
451 QAction* pickColorAction =
new QAction(
tr(
"Pick color" ),
this );
452 mMenu->addAction( pickColorAction );
453 connect( pickColorAction, SIGNAL( triggered() ),
this, SLOT(
activatePicker() ) );
455 QAction* chooseColorAction =
new QAction(
tr(
"Choose color..." ),
this );
456 mMenu->addAction( chooseColorAction );
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
470 void QgsColorButtonV2::paintEvent( QPaintEvent* e )
472 QToolButton::paintEvent( e );
474 if ( !mBackgroundSet )
497 QColor oldColor = mColor;
501 if ( oldColor != mColor || ( mColor == QColor( Qt::black ) && !mColorSet ) )
514 void QgsColorButtonV2::addRecentColor(
const QColor& color )
516 if ( !color.isValid() )
522 QColor opaqueColor =
color;
523 opaqueColor.setAlpha( 255 );
526 QList< QVariant > recentColorVariants = settings.value( QString(
"/colors/recent" ) ).toList();
529 for (
int colorIdx = recentColorVariants.length() - 1; colorIdx >= 0; --colorIdx )
531 if (( recentColorVariants.at( colorIdx ).value<QColor>() ).name() == opaqueColor.name() )
533 recentColorVariants.removeAt( colorIdx );
538 QVariant colorVariant = QVariant( opaqueColor );
539 recentColorVariants.prepend( colorVariant );
542 while ( recentColorVariants.count() > 20 )
544 recentColorVariants.pop_back();
547 settings.setValue( QString(
"/colors/recent" ), recentColorVariants );
552 QColor backgroundColor =
color;
554 if ( !color.isValid() )
556 backgroundColor = mColor;
559 QSize currentIconSize;
563 if ( !mIconSize.isValid() )
566 QStyleOptionToolButton opt;
567 initStyleOption( &opt );
568 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
572 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
574 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
577 currentIconSize = mIconSize;
583 currentIconSize = QSize( width() - 10, height() - 6 );
585 currentIconSize = QSize( width() - 10, height() - 12 );
589 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
595 QPixmap pixmap( currentIconSize );
596 pixmap.fill( Qt::transparent );
598 if ( backgroundColor.isValid() )
600 QRect rect( 0, 0, currentIconSize.width(), currentIconSize.height() );
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 );
619 setIconSize( currentIconSize );
632 if ( colorFromMimeData( QApplication::clipboard()->mimeData(), clipColor ) )
636 addRecentColor( clipColor );
643 QPixmap samplerPixmap = QPixmap((
const char ** )
sampler_cursor );
644 setCursor( QCursor( samplerPixmap, 0, 0 ) );
647 mPickingColor =
true;
662 mColorDialogTitle = title;
667 return mColorDialogTitle;
672 setMenu( showMenu ? mMenu : 0 );
673 setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
686 mDefaultColor =
color;