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() );
87 dlg.setWindowTitle( mColorRampDialogTitle );
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 ) );
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 ) );
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()
341 QStringList rampTypeNames;
342 rampTypeNames.reserve( rampTypes.size() );
343 if ( mShowGradientOnly )
345 rampTypes.erase( std::remove_if( rampTypes.begin(), rampTypes.end(), [](
const QPair< QString, QString > &type )
347 return type.first != QgsGradientColorRamp::typeString() && type.first != QgsCptCityColorRamp::typeString();
348 } ), rampTypes.end() );
351 for (
const QPair< QString, QString > &type : rampTypes )
352 rampTypeNames << type.second;
354 const QString selectedRampTypeName = QInputDialog::getItem(
this, tr(
"Color ramp type" ),
355 tr(
"Please select color ramp type:" ), rampTypeNames, 0,
false, &ok );
357 if ( !ok || selectedRampTypeName.isEmpty() )
360 const QString rampType = rampTypes.value( rampTypeNames.indexOf( selectedRampTypeName ) ).first;
392 showColorRampDialog();
395 void QgsColorRampButton::saveColorRamp()
398 if ( !saveDlg.exec() || saveDlg.name().isEmpty() )
404 if ( mStyle->
symbolNames().contains( saveDlg.name() ) )
406 int res = QMessageBox::warning(
this, tr(
"Save Color Ramp" ),
407 tr(
"Color ramp with name '%1' already exists. Overwrite?" )
408 .arg( saveDlg.name() ),
409 QMessageBox::Yes | QMessageBox::No );
410 if ( res != QMessageBox::Yes )
417 QStringList colorRampTags = saveDlg.tags().split(
',' );
422 mStyle->
saveColorRamp( saveDlg.name(), ramp, saveDlg.isFavorite(), colorRampTags );
427 void QgsColorRampButton::invertColorRamp()
429 mColorRamp->invert();
436 if ( e->type() == QEvent::EnabledChange )
440 QToolButton::changeEvent( e );
443 #if 0 // causes too many cyclical updates, but may be needed on some platforms
444 void QgsColorRampButton::paintEvent( QPaintEvent *e )
446 QToolButton::paintEvent( e );
448 if ( !mBackgroundSet )
458 QToolButton::showEvent( e );
463 QToolButton::resizeEvent(
event );
471 if ( colorramp == mColorRamp.get() )
474 mColorRamp.reset( colorramp ? colorramp->
clone() :
nullptr );
485 if ( !name.isEmpty() )
487 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
509 backgroundColorRamp = mColorRamp.get();
512 QSize currentIconSize;
516 if ( !mIconSize.isValid() )
519 QStyleOptionToolButton opt;
520 initStyleOption( &opt );
521 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
525 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
527 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
530 currentIconSize = mIconSize;
536 currentIconSize = QSize( width() - 10, height() - 6 );
538 currentIconSize = QSize( width() - 10, height() - 12 );
542 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
551 pm = QPixmap( currentIconSize );
552 pm.fill( Qt::transparent );
555 QPen pen = ( palette().buttonText().color() );
557 painter.begin( &pm );
558 painter.setPen( pen );
559 painter.drawText( QRect( 0, 0, currentIconSize.width(), currentIconSize.height() ), Qt::AlignCenter, QStringLiteral(
"Random colors" ) );
565 if ( backgroundColorRamp )
571 setIconSize( currentIconSize );
577 return mColorRamp ? mColorRamp->clone() :
nullptr;
582 mColorRampDialogTitle = title;
587 return mColorRampDialogTitle;
592 setMenu(
showMenu ? mMenu :
nullptr );
593 setPopupMode(
showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
601 return menu() ? true :
false;
606 mDefaultColorRamp.reset( colorramp ? colorramp->
clone() :
nullptr );
611 return mDefaultColorRamp ? mDefaultColorRamp->clone() :
nullptr;
634 void QgsColorRampButton::rampWidgetUpdated()
637 if ( limitedRampWidget )
644 if ( colorBrewerRampWidget )
651 if ( presetRampWidget )