31 #include <QInputDialog>
32 #include <QMessageBox>
33 #include <QMouseEvent>
36 #include <QPushButton>
39 : QToolButton( parent )
40 , mColorRampDialogTitle( dialogTitle.isEmpty() ? tr(
"Select Color Ramp" ) : dialogTitle )
42 setAcceptDrops(
true );
43 setMinimumSize( QSize( 24, 16 ) );
44 connect(
this, &QPushButton::clicked,
this, &QgsColorRampButton::buttonClicked );
47 mMenu =
new QMenu(
this );
48 connect( mMenu, &QMenu::aboutToShow,
this, &QgsColorRampButton::prepareMenu );
51 mAllRampsMenu =
new QMenu( mMenu );
52 mAllRampsMenu->setTitle( tr(
"All Color Ramps" ) );
54 setPopupMode( QToolButton::MenuButtonPopup );
65 return QSize( 120,
static_cast<int>( std::max(
Qgis::UI_SCALE_FACTOR * fontMetrics().height( ), 22.0 ) ) );
68 return QSize( 120,
static_cast<int>( std::max(
Qgis::UI_SCALE_FACTOR * fontMetrics().height( ) * 1.4, 28.0 ) ) );
72 void QgsColorRampButton::showColorRampDialog()
75 bool panelMode = panel && panel->
dockMode();
77 std::unique_ptr< QgsColorRamp > currentRamp(
colorRamp() );
83 if ( currentRamp->type() == QLatin1String(
"gradient" ) )
87 dlg.setWindowTitle( mColorRampDialogTitle );
93 else if ( currentRamp->type() == QLatin1String(
"random" ) )
112 else if ( currentRamp->type() == QLatin1String(
"preset" ) )
131 else if ( currentRamp->type() == QLatin1String(
"colorbrewer" ) )
150 else if ( currentRamp->type() == QLatin1String(
"cpt-city" ) )
156 if ( dlg.saveAsGradientRamp() )
170 if ( !mDefaultColorRamp )
185 if ( e->type() == QEvent::ToolTip )
192 return QToolButton::event( e );
197 if ( e->button() == Qt::RightButton )
199 QToolButton::showMenu();
202 QToolButton::mousePressEvent( e );
205 QPixmap QgsColorRampButton::createMenuIcon(
QgsColorRamp *colorramp )
213 void QgsColorRampButton::buttonClicked()
217 showColorRampDialog();
221 QToolButton::showMenu();
225 void QgsColorRampButton::prepareMenu()
229 QAction *invertAction =
new QAction( tr(
"Invert Color Ramp" ),
this );
231 mMenu->addAction( invertAction );
232 connect( invertAction, &QAction::triggered,
this, &QgsColorRampButton::invertColorRamp );
236 QAction *nullAction =
new QAction( tr(
"Clear Current Ramp" ),
this );
237 mMenu->addAction( nullAction );
241 mMenu->addSeparator();
244 if ( mDefaultColorRamp )
246 QAction *defaultColorRampAction =
new QAction( tr(
"Default Color Ramp" ),
this );
247 defaultColorRampAction->setIcon( createMenuIcon( mDefaultColorRamp.get() ) );
248 mMenu->addAction( defaultColorRampAction );
252 if ( mShowRandomColorRamp )
254 QAction *randomColorRampAction =
new QAction( tr(
"Random Color Ramp" ),
this );
255 randomColorRampAction->setCheckable(
true );
257 mMenu->addAction( randomColorRampAction );
262 QAction *shuffleRandomColorRampAction =
new QAction( tr(
"Shuffle Random Colors" ),
this );
263 mMenu->addAction( shuffleRandomColorRampAction );
268 mMenu->addSeparator();
273 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
275 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( *it ) );
277 if ( !mShowGradientOnly || ( ramp->type() == QLatin1String(
"gradient" ) || ramp->type() == QLatin1String(
"cpt-city" ) ) )
280 QAction *ra =
new QAction( *it,
this );
282 connect( ra, &QAction::triggered,
this, &QgsColorRampButton::loadColorRamp );
283 mMenu->addAction( ra );
287 mMenu->addSeparator();
289 mAllRampsMenu->clear();
290 mMenu->addMenu( mAllRampsMenu );
293 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
295 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( *it ) );
297 if ( !mShowGradientOnly || ( ramp->type() == QLatin1String(
"gradient" ) || ramp->type() == QLatin1String(
"cpt-city" ) ) )
300 QAction *ra =
new QAction( *it,
this );
302 connect( ra, &QAction::triggered,
this, &QgsColorRampButton::loadColorRamp );
303 mAllRampsMenu->addAction( ra );
307 mMenu->addSeparator();
309 QAction *newColorRampAction =
new QAction( tr(
"Create New Color Ramp…" ),
this );
310 connect( newColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::createColorRamp );
311 mMenu->addAction( newColorRampAction );
313 QAction *editColorRampAction =
new QAction( tr(
"Edit Color Ramp…" ),
this );
315 connect( editColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::showColorRampDialog );
316 mMenu->addAction( editColorRampAction );
318 QAction *saveColorRampAction =
new QAction( tr(
"Save Color Ramp…" ),
this );
320 connect( saveColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::saveColorRamp );
321 mMenu->addAction( saveColorRampAction );
324 void QgsColorRampButton::loadColorRamp()
326 QAction *selectedItem = qobject_cast<QAction *>( sender() );
329 QString name = selectedItem->iconText();
335 void QgsColorRampButton::createColorRamp()
337 QStringList rampTypes;
341 if ( mShowGradientOnly )
343 rampTypes << tr(
"Gradient" ) << tr(
"Catalog: cpt-city" );
347 rampTypes << tr(
"Gradient" ) << tr(
"Color presets" ) << tr(
"Random" ) << tr(
"Catalog: cpt-city" );
348 rampTypes << tr(
"Catalog: ColorBrewer" );
350 rampType = QInputDialog::getItem(
this, tr(
"Color ramp type" ),
351 tr(
"Please select color ramp type:" ), rampTypes, 0,
false, &ok );
353 if ( !ok || rampType.isEmpty() )
357 if ( rampType == tr(
"Gradient" ) )
361 else if ( rampType == tr(
"Random" ) )
365 else if ( rampType == tr(
"Catalog: ColorBrewer" ) )
369 else if ( rampType == tr(
"Color presets" ) )
373 else if ( rampType == tr(
"Catalog: cpt-city" ) )
386 showColorRampDialog();
389 void QgsColorRampButton::saveColorRamp()
392 if ( !saveDlg.exec() || saveDlg.name().isEmpty() )
398 if ( mStyle->
symbolNames().contains( saveDlg.name() ) )
400 int res = QMessageBox::warning(
this, tr(
"Save Color Ramp" ),
401 tr(
"Color ramp with name '%1' already exists. Overwrite?" )
402 .arg( saveDlg.name() ),
403 QMessageBox::Yes | QMessageBox::No );
404 if ( res != QMessageBox::Yes )
411 QStringList colorRampTags = saveDlg.tags().split(
',' );
416 mStyle->
saveColorRamp( saveDlg.name(), ramp, saveDlg.isFavorite(), colorRampTags );
421 void QgsColorRampButton::invertColorRamp()
423 mColorRamp->invert();
430 if ( e->type() == QEvent::EnabledChange )
434 QToolButton::changeEvent( e );
437 #if 0 // causes too many cyclical updates, but may be needed on some platforms
438 void QgsColorRampButton::paintEvent( QPaintEvent *e )
440 QToolButton::paintEvent( e );
442 if ( !mBackgroundSet )
452 QToolButton::showEvent( e );
457 QToolButton::resizeEvent(
event );
465 if ( colorramp == mColorRamp.get() )
468 mColorRamp.reset( colorramp ? colorramp->
clone() :
nullptr );
479 if ( !name.isEmpty() )
481 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
503 backgroundColorRamp = mColorRamp.get();
506 QSize currentIconSize;
510 if ( !mIconSize.isValid() )
513 QStyleOptionToolButton opt;
514 initStyleOption( &opt );
515 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
519 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
521 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
524 currentIconSize = mIconSize;
530 currentIconSize = QSize( width() - 10, height() - 6 );
532 currentIconSize = QSize( width() - 10, height() - 12 );
536 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
545 pm = QPixmap( currentIconSize );
546 pm.fill( Qt::transparent );
549 QPen pen = ( palette().buttonText().color() );
551 painter.begin( &pm );
552 painter.setPen( pen );
553 painter.drawText( QRect( 0, 0, currentIconSize.width(), currentIconSize.height() ), Qt::AlignCenter, QStringLiteral(
"Random colors" ) );
559 if ( backgroundColorRamp )
565 setIconSize( currentIconSize );
571 return mColorRamp ? mColorRamp->clone() :
nullptr;
576 mColorRampDialogTitle = title;
581 return mColorRampDialogTitle;
586 setMenu(
showMenu ? mMenu :
nullptr );
587 setPopupMode(
showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
595 return menu() ? true :
false;
600 mDefaultColorRamp.reset( colorramp ? colorramp->
clone() :
nullptr );
605 return mDefaultColorRamp ? mDefaultColorRamp->clone() :
nullptr;
628 void QgsColorRampButton::rampWidgetUpdated()
631 if ( limitedRampWidget )
638 if ( colorBrewerRampWidget )
645 if ( presetRampWidget )