29#include "moc_qgscolorrampbutton.cpp"
32#include <QInputDialog>
40 : QToolButton( parent )
41 , mColorRampDialogTitle( dialogTitle.isEmpty() ? tr(
"Select Color Ramp" ) : dialogTitle )
43 setAcceptDrops(
true );
44 setMinimumSize( QSize( 24, 16 ) );
45 connect(
this, &QPushButton::clicked,
this, &QgsColorRampButton::buttonClicked );
48 mMenu =
new QMenu(
this );
49 connect( mMenu, &QMenu::aboutToShow,
this, &QgsColorRampButton::prepareMenu );
52 mAllRampsMenu =
new QMenu( mMenu );
53 mAllRampsMenu->setTitle( tr(
"All Color Ramps" ) );
55 setPopupMode( QToolButton::MenuButtonPopup );
66 return QSize( 120,
static_cast<int>( std::max(
Qgis::UI_SCALE_FACTOR * fontMetrics().height( ), 22.0 ) ) );
69 return QSize( 120,
static_cast<int>( std::max(
Qgis::UI_SCALE_FACTOR * fontMetrics().height( ) * 1.4, 28.0 ) ) );
73void QgsColorRampButton::showColorRampDialog()
76 const bool panelMode = panel && panel->
dockMode();
78 std::unique_ptr< QgsColorRamp > currentRamp(
colorRamp() );
88 dlg.setWindowTitle( mColorRampDialogTitle );
157 if ( dlg.saveAsGradientRamp() )
171 if ( !mDefaultColorRamp )
186 if ( e->type() == QEvent::ToolTip )
193 return QToolButton::event( e );
198 if ( e->button() == Qt::RightButton )
200 QToolButton::showMenu();
203 QToolButton::mousePressEvent( e );
206QPixmap QgsColorRampButton::createMenuIcon(
QgsColorRamp *colorramp )
214void QgsColorRampButton::buttonClicked()
218 showColorRampDialog();
222 QToolButton::showMenu();
226void QgsColorRampButton::prepareMenu()
230 QAction *invertAction =
new QAction( tr(
"Invert Color Ramp" ),
this );
232 mMenu->addAction( invertAction );
233 connect( invertAction, &QAction::triggered,
this, &QgsColorRampButton::invertColorRamp );
237 QAction *nullAction =
new QAction( tr(
"Clear Current Ramp" ),
this );
238 nullAction->setEnabled( !
isNull() );
239 mMenu->addAction( nullAction );
243 mMenu->addSeparator();
246 if ( mDefaultColorRamp )
248 QAction *defaultColorRampAction =
new QAction( tr(
"Default Color Ramp" ),
this );
249 defaultColorRampAction->setIcon( createMenuIcon( mDefaultColorRamp.get() ) );
250 mMenu->addAction( defaultColorRampAction );
254 if ( mShowRandomColorRamp )
256 QAction *randomColorRampAction =
new QAction( tr(
"Random Color Ramp" ),
this );
257 randomColorRampAction->setCheckable(
true );
259 mMenu->addAction( randomColorRampAction );
264 QAction *shuffleRandomColorRampAction =
new QAction( tr(
"Shuffle Random Colors" ),
this );
265 mMenu->addAction( shuffleRandomColorRampAction );
270 mMenu->addSeparator();
275 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
277 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( *it ) );
282 QAction *ra =
new QAction( *it,
this );
284 connect( ra, &QAction::triggered,
this, &QgsColorRampButton::loadColorRamp );
285 mMenu->addAction( ra );
289 mMenu->addSeparator();
291 mAllRampsMenu->clear();
292 mMenu->addMenu( mAllRampsMenu );
295 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
297 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( *it ) );
302 QAction *ra =
new QAction( *it,
this );
304 connect( ra, &QAction::triggered,
this, &QgsColorRampButton::loadColorRamp );
305 mAllRampsMenu->addAction( ra );
309 mMenu->addSeparator();
311 QAction *newColorRampAction =
new QAction( tr(
"Create New Color Ramp…" ),
this );
312 connect( newColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::createColorRamp );
313 mMenu->addAction( newColorRampAction );
315 QAction *editColorRampAction =
new QAction( tr(
"Edit Color Ramp…" ),
this );
317 connect( editColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::showColorRampDialog );
318 mMenu->addAction( editColorRampAction );
320 QAction *saveColorRampAction =
new QAction( tr(
"Save Color Ramp…" ),
this );
322 connect( saveColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::saveColorRamp );
323 mMenu->addAction( saveColorRampAction );
326void QgsColorRampButton::loadColorRamp()
328 QAction *selectedItem = qobject_cast<QAction *>( sender() );
331 const QString name = selectedItem->iconText();
337void QgsColorRampButton::createColorRamp()
343 QStringList rampTypeNames;
344 rampTypeNames.reserve( rampTypes.size() );
345 if ( mShowGradientOnly )
347 rampTypes.erase( std::remove_if( rampTypes.begin(), rampTypes.end(), [](
const QPair< QString, QString > &type )
349 return type.first != QgsGradientColorRamp::typeString() && type.first != QgsCptCityColorRamp::typeString();
350 } ), rampTypes.end() );
353 for (
const QPair< QString, QString > &type : rampTypes )
354 rampTypeNames << type.second;
356 const QString selectedRampTypeName = QInputDialog::getItem(
this, tr(
"Color ramp type" ),
357 tr(
"Please select color ramp type:" ), rampTypeNames, 0,
false, &ok );
359 if ( !ok || selectedRampTypeName.isEmpty() )
362 const QString rampType = rampTypes.value( rampTypeNames.indexOf( selectedRampTypeName ) ).first;
394 showColorRampDialog();
397void QgsColorRampButton::saveColorRamp()
400 if ( !saveDlg.exec() || saveDlg.name().isEmpty() )
405 QgsStyle *destinationStyle = saveDlg.destinationStyle();
408 if ( destinationStyle->
symbolNames().contains( saveDlg.name() ) )
410 const int res = QMessageBox::warning(
this, tr(
"Save Color Ramp" ),
411 tr(
"Color ramp with name '%1' already exists. Overwrite?" )
412 .arg( saveDlg.name() ),
413 QMessageBox::Yes | QMessageBox::No );
414 if ( res != QMessageBox::Yes )
421 const QStringList colorRampTags = saveDlg.tags().split(
',' );
426 destinationStyle->
saveColorRamp( saveDlg.name(), ramp, saveDlg.isFavorite(), colorRampTags );
431void QgsColorRampButton::invertColorRamp()
433 mColorRamp->invert();
440 if ( e->type() == QEvent::EnabledChange )
444 QToolButton::changeEvent( e );
448void QgsColorRampButton::paintEvent( QPaintEvent *e )
450 QToolButton::paintEvent( e );
452 if ( !mBackgroundSet )
462 QToolButton::showEvent( e );
467 QToolButton::resizeEvent(
event );
475 if ( colorramp == mColorRamp.get() )
478 mColorRamp.reset( colorramp ? colorramp->
clone() : nullptr );
489 if ( !name.isEmpty() )
491 const std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
513 backgroundColorRamp = mColorRamp.get();
516 QSize currentIconSize;
520 if ( !mIconSize.isValid() )
523 QStyleOptionToolButton opt;
524 initStyleOption( &opt );
525 const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
529 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
531 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
534 currentIconSize = mIconSize;
540 currentIconSize = QSize( width() - 10, height() - 6 );
542 currentIconSize = QSize( width() - 10, height() - 12 );
546 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
555 pm = QPixmap( currentIconSize );
556 pm.fill( Qt::transparent );
559 const QPen pen = ( palette().buttonText().color() );
561 painter.begin( &pm );
562 painter.setPen( pen );
563 painter.drawText( QRect( 0, 0, currentIconSize.width(), currentIconSize.height() ), Qt::AlignCenter, QStringLiteral(
"Random colors" ) );
569 if ( backgroundColorRamp )
575 setIconSize( currentIconSize );
581 return mColorRamp ? mColorRamp->clone() :
nullptr;
586 mColorRampDialogTitle = title;
591 return mColorRampDialogTitle;
596 setMenu(
showMenu ? mMenu : nullptr );
597 setPopupMode(
showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
605 return menu() ? true :
false;
610 mDefaultColorRamp.reset( colorramp ? colorramp->
clone() : nullptr );
615 return mDefaultColorRamp ? mDefaultColorRamp->clone() :
nullptr;
638void QgsColorRampButton::rampWidgetUpdated()
641 if ( limitedRampWidget )
648 if ( colorBrewerRampWidget )
655 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.
A color ramp from the CPT City collection.
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.
QgsColorRamp * colorRamp(const QString &name) const
Returns a new copy of the specified color ramp.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
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,...
#define QgsDebugError(str)