31#include <QInputDialog>
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 ) ) );
72void QgsColorRampButton::showColorRampDialog()
75 const 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 );
205QPixmap QgsColorRampButton::createMenuIcon(
QgsColorRamp *colorramp )
213void QgsColorRampButton::buttonClicked()
217 showColorRampDialog();
221 QToolButton::showMenu();
225void 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 nullAction->setEnabled( !
isNull() );
238 mMenu->addAction( nullAction );
242 mMenu->addSeparator();
245 if ( mDefaultColorRamp )
247 QAction *defaultColorRampAction =
new QAction( tr(
"Default Color Ramp" ),
this );
248 defaultColorRampAction->setIcon( createMenuIcon( mDefaultColorRamp.get() ) );
249 mMenu->addAction( defaultColorRampAction );
253 if ( mShowRandomColorRamp )
255 QAction *randomColorRampAction =
new QAction( tr(
"Random Color Ramp" ),
this );
256 randomColorRampAction->setCheckable(
true );
258 mMenu->addAction( randomColorRampAction );
263 QAction *shuffleRandomColorRampAction =
new QAction( tr(
"Shuffle Random Colors" ),
this );
264 mMenu->addAction( shuffleRandomColorRampAction );
269 mMenu->addSeparator();
274 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
276 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( *it ) );
281 QAction *ra =
new QAction( *it,
this );
283 connect( ra, &QAction::triggered,
this, &QgsColorRampButton::loadColorRamp );
284 mMenu->addAction( ra );
288 mMenu->addSeparator();
290 mAllRampsMenu->clear();
291 mMenu->addMenu( mAllRampsMenu );
294 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
296 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( *it ) );
301 QAction *ra =
new QAction( *it,
this );
303 connect( ra, &QAction::triggered,
this, &QgsColorRampButton::loadColorRamp );
304 mAllRampsMenu->addAction( ra );
308 mMenu->addSeparator();
310 QAction *newColorRampAction =
new QAction( tr(
"Create New Color Ramp…" ),
this );
311 connect( newColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::createColorRamp );
312 mMenu->addAction( newColorRampAction );
314 QAction *editColorRampAction =
new QAction( tr(
"Edit Color Ramp…" ),
this );
316 connect( editColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::showColorRampDialog );
317 mMenu->addAction( editColorRampAction );
319 QAction *saveColorRampAction =
new QAction( tr(
"Save Color Ramp…" ),
this );
321 connect( saveColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::saveColorRamp );
322 mMenu->addAction( saveColorRampAction );
325void QgsColorRampButton::loadColorRamp()
327 QAction *selectedItem = qobject_cast<QAction *>( sender() );
330 const QString name = selectedItem->iconText();
336void QgsColorRampButton::createColorRamp()
342 QStringList rampTypeNames;
343 rampTypeNames.reserve( rampTypes.size() );
344 if ( mShowGradientOnly )
346 rampTypes.erase( std::remove_if( rampTypes.begin(), rampTypes.end(), [](
const QPair< QString, QString > &type )
348 return type.first != QgsGradientColorRamp::typeString() && type.first != QgsCptCityColorRamp::typeString();
349 } ), rampTypes.end() );
352 for (
const QPair< QString, QString > &type : rampTypes )
353 rampTypeNames << type.second;
355 const QString selectedRampTypeName = QInputDialog::getItem(
this, tr(
"Color ramp type" ),
356 tr(
"Please select color ramp type:" ), rampTypeNames, 0,
false, &ok );
358 if ( !ok || selectedRampTypeName.isEmpty() )
361 const QString rampType = rampTypes.value( rampTypeNames.indexOf( selectedRampTypeName ) ).first;
393 showColorRampDialog();
396void QgsColorRampButton::saveColorRamp()
399 if ( !saveDlg.exec() || saveDlg.name().isEmpty() )
404 QgsStyle *destinationStyle = saveDlg.destinationStyle();
407 if ( destinationStyle->
symbolNames().contains( saveDlg.name() ) )
409 const int res = QMessageBox::warning(
this, tr(
"Save Color Ramp" ),
410 tr(
"Color ramp with name '%1' already exists. Overwrite?" )
411 .arg( saveDlg.name() ),
412 QMessageBox::Yes | QMessageBox::No );
413 if ( res != QMessageBox::Yes )
420 const QStringList colorRampTags = saveDlg.tags().split(
',' );
425 destinationStyle->
saveColorRamp( saveDlg.name(), ramp, saveDlg.isFavorite(), colorRampTags );
430void QgsColorRampButton::invertColorRamp()
432 mColorRamp->invert();
439 if ( e->type() == QEvent::EnabledChange )
443 QToolButton::changeEvent( e );
447void QgsColorRampButton::paintEvent( QPaintEvent *e )
449 QToolButton::paintEvent( e );
451 if ( !mBackgroundSet )
461 QToolButton::showEvent( e );
466 QToolButton::resizeEvent(
event );
474 if ( colorramp == mColorRamp.get() )
477 mColorRamp.reset( colorramp ? colorramp->
clone() :
nullptr );
488 if ( !name.isEmpty() )
490 const std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
512 backgroundColorRamp = mColorRamp.get();
515 QSize currentIconSize;
519 if ( !mIconSize.isValid() )
522 QStyleOptionToolButton opt;
523 initStyleOption( &opt );
524 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
528 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
530 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
533 currentIconSize = mIconSize;
539 currentIconSize = QSize( width() - 10, height() - 6 );
541 currentIconSize = QSize( width() - 10, height() - 12 );
545 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
554 pm = QPixmap( currentIconSize );
555 pm.fill( Qt::transparent );
558 const QPen pen = ( palette().buttonText().color() );
560 painter.begin( &pm );
561 painter.setPen( pen );
562 painter.drawText( QRect( 0, 0, currentIconSize.width(), currentIconSize.height() ), Qt::AlignCenter, QStringLiteral(
"Random colors" ) );
568 if ( backgroundColorRamp )
574 setIconSize( currentIconSize );
580 return mColorRamp ? mColorRamp->clone() :
nullptr;
585 mColorRampDialogTitle = title;
590 return mColorRampDialogTitle;
595 setMenu(
showMenu ? mMenu :
nullptr );
596 setPopupMode(
showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
604 return menu() ? true :
false;
609 mDefaultColorRamp.reset( colorramp ? colorramp->
clone() :
nullptr );
614 return mDefaultColorRamp ? mDefaultColorRamp->clone() :
nullptr;
637void QgsColorRampButton::rampWidgetUpdated()
640 if ( limitedRampWidget )
647 if ( colorBrewerRampWidget )
654 if ( presetRampWidget )
static const double UI_SCALE_FACTOR
UI scaling factor.
A dialog which allows users to modify the properties of a QgsColorBrewerColorRamp.
Color ramp utilising "Color Brewer" preset color schemes.
QgsColorBrewerColorRamp * clone() const override
Creates a clone of the color ramp.
static QString typeString()
Returns the string identifier for QgsColorBrewerColorRamp.
Abstract base class for color ramps.
static QList< QPair< QString, QString > > rampTypes()
Returns a list of available ramp types, where the first value in each item is the QgsColorRamp::type(...
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
A dialog which allows users to modify the properties of a QgsCptCityColorRamp.
static QString typeString()
Returns the string identifier for QgsCptCityColorRamp.
A dialog which allows users to modify the properties of a QgsGradientColorRamp.
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
static QString typeString()
Returns the string identifier for QgsGradientColorRamp.
A dialog which allows users to modify the properties of a QgsLimitedRandomColorRamp.
Constrained random color ramp, which returns random colors based on preset parameters.
static QString typeString()
Returns the string identifier for QgsLimitedRandomColorRamp.
QgsLimitedRandomColorRamp * clone() const override
Creates a clone of the color ramp.
A dialog which allows users to modify the properties of a QgsPresetSchemeColorRamp.
A scheme based color ramp consisting of a list of predefined colors.
static QString typeString()
Returns the string identifier for QgsPresetSchemeColorRamp.
QgsPresetSchemeColorRamp * clone() const override
Creates a clone of the color ramp.
Totally random color ramp.
a dialog for setting properties of a newly saved style.
bool saveColorRamp(const QString &name, QgsColorRamp *ramp, bool favorite, const QStringList &tags)
Adds the colorramp to the database.
bool addColorRamp(const QString &name, QgsColorRamp *colorRamp, bool update=false)
Adds a color ramp to the style.
@ ColorrampEntity
Color ramps.
QStringList colorRampNames() const
Returns a list of names of color ramps.
static QgsStyle * defaultStyle()
Returns default application-wide style.
QgsColorRamp * colorRamp(const QString &name) const
Returns a new copy of the specified color ramp.
QStringList symbolsOfFavorite(StyleEntity type) const
Returns the symbol names which are flagged as favorite.
bool removeColorRamp(const QString &name)
Removes color ramp from style (and delete it)
QStringList symbolNames() const
Returns a list of names of symbols.
static QIcon colorRampPreviewIcon(QgsColorRamp *ramp, QSize size, int padding=0)
Returns an icon preview for a color ramp.
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0, Qt::Orientation direction=Qt::Horizontal, bool flipDirection=false, bool drawTransparentBackground=true)
Returns a pixmap preview for a color ramp.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...