31 #include <QInputDialog> 32 #include <QMessageBox> 33 #include <QMouseEvent> 36 #include <QPushButton> 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 );
63 delete mDefaultColorRamp;
70 return QSize( 120, static_cast<int>( std::max(
Qgis::UI_SCALE_FACTOR * fontMetrics().height( ), 22.0 ) ) );
73 return QSize( 120, static_cast<int>( std::max(
Qgis::UI_SCALE_FACTOR * fontMetrics().height( ) * 1.4, 28.0 ) ) );
77 void QgsColorRampButton::showColorRampDialog()
80 bool panelMode = panel && panel->
dockMode();
82 std::unique_ptr< QgsColorRamp > currentRamp(
colorRamp() );
88 if ( currentRamp->type() == QLatin1String(
"gradient" ) )
92 dlg.setWindowTitle( mColorRampDialogTitle );
98 else if ( currentRamp->type() == QLatin1String(
"random" ) )
117 else if ( currentRamp->type() == QLatin1String(
"preset" ) )
136 else if ( currentRamp->type() == QLatin1String(
"colorbrewer" ) )
155 else if ( currentRamp->type() == QLatin1String(
"cpt-city" ) )
175 if ( !mDefaultColorRamp )
190 if ( e->type() == QEvent::ToolTip )
197 return QToolButton::event( e );
202 if ( e->button() == Qt::RightButton )
204 QToolButton::showMenu();
207 QToolButton::mousePressEvent( e );
210 QPixmap QgsColorRampButton::createMenuIcon(
QgsColorRamp *colorramp )
217 void QgsColorRampButton::buttonClicked()
221 showColorRampDialog();
225 QToolButton::showMenu();
229 void QgsColorRampButton::prepareMenu()
233 QAction *invertAction =
new QAction( tr(
"Invert Color Ramp" ),
this );
235 mMenu->addAction( invertAction );
236 connect( invertAction, &QAction::triggered,
this, &QgsColorRampButton::invertColorRamp );
240 QAction *nullAction =
new QAction( tr(
"Clear Current Ramp" ),
this );
241 mMenu->addAction( nullAction );
245 mMenu->addSeparator();
248 if ( mDefaultColorRamp )
250 QAction *defaultColorRampAction =
new QAction( tr(
"Default Color Ramp" ),
this );
251 defaultColorRampAction->setIcon( createMenuIcon( mDefaultColorRamp ) );
252 mMenu->addAction( defaultColorRampAction );
256 if ( mShowRandomColorRamp )
258 QAction *randomColorRampAction =
new QAction( tr(
"Random Color Ramp" ),
this );
259 randomColorRampAction->setCheckable(
true );
261 mMenu->addAction( randomColorRampAction );
264 if (
isRandomColorRamp() || dynamic_cast<QgsLimitedRandomColorRamp *>( mColorRamp ) )
266 QAction *shuffleRandomColorRampAction =
new QAction( tr(
"Shuffle Random Colors" ),
this );
267 mMenu->addAction( shuffleRandomColorRampAction );
272 mMenu->addSeparator();
276 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
278 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( *it ) );
280 if ( !mShowGradientOnly || ( ramp->type() == QLatin1String(
"gradient" ) || ramp->type() == QLatin1String(
"cpt-city" ) ) )
283 QAction *ra =
new QAction( *it,
this );
285 connect( ra, &QAction::triggered,
this, &QgsColorRampButton::loadColorRamp );
286 mMenu->addAction( ra );
290 mMenu->addSeparator();
292 mAllRampsMenu->clear();
293 mMenu->addMenu( mAllRampsMenu );
296 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
298 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( *it ) );
300 if ( !mShowGradientOnly || ( ramp->type() == QLatin1String(
"gradient" ) || ramp->type() == QLatin1String(
"cpt-city" ) ) )
303 QAction *ra =
new QAction( *it,
this );
305 connect( ra, &QAction::triggered,
this, &QgsColorRampButton::loadColorRamp );
306 mAllRampsMenu->addAction( ra );
310 mMenu->addSeparator();
312 QAction *newColorRampAction =
new QAction( tr(
"Create New Color Ramp…" ),
this );
313 connect( newColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::createColorRamp );
314 mMenu->addAction( newColorRampAction );
316 QAction *editColorRampAction =
new QAction( tr(
"Edit Color Ramp…" ),
this );
318 connect( editColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::showColorRampDialog );
319 mMenu->addAction( editColorRampAction );
321 QAction *saveColorRampAction =
new QAction( tr(
"Save Color Ramp…" ),
this );
323 connect( saveColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::saveColorRamp );
324 mMenu->addAction( saveColorRampAction );
327 void QgsColorRampButton::loadColorRamp()
329 QAction *selectedItem = qobject_cast<QAction *>( sender() );
332 QString name = selectedItem->iconText();
338 void QgsColorRampButton::createColorRamp()
340 QStringList rampTypes;
344 if ( mShowGradientOnly )
346 rampTypes << tr(
"Gradient" ) << tr(
"Catalog: cpt-city" );
350 rampTypes << tr(
"Gradient" ) << tr(
"Color presets" ) << tr(
"Random" ) << tr(
"Catalog: cpt-city" );
351 rampTypes << tr(
"Catalog: ColorBrewer" );
353 rampType = QInputDialog::getItem(
this, tr(
"Color ramp type" ),
354 tr(
"Please select color ramp type:" ), rampTypes, 0,
false, &ok );
356 if ( !ok || rampType.isEmpty() )
360 if ( rampType == tr(
"Gradient" ) )
364 else if ( rampType == tr(
"Random" ) )
368 else if ( rampType == tr(
"Catalog: ColorBrewer" ) )
372 else if ( rampType == tr(
"Color presets" ) )
376 else if ( rampType == tr(
"Catalog: cpt-city" ) )
389 showColorRampDialog();
392 void QgsColorRampButton::saveColorRamp()
395 if ( !saveDlg.exec() || saveDlg.
name().isEmpty() )
403 int res = QMessageBox::warning(
this, tr(
"Save Color Ramp" ),
404 tr(
"Color ramp with name '%1' already exists. Overwrite?" )
405 .arg( saveDlg.
name() ),
406 QMessageBox::Yes | QMessageBox::No );
407 if ( res != QMessageBox::Yes )
414 QStringList colorRampTags = saveDlg.
tags().split(
',' );
424 void QgsColorRampButton::invertColorRamp()
433 if ( e->type() == QEvent::EnabledChange )
437 QToolButton::changeEvent( e );
440 #if 0 // causes too many cyclical updates, but may be needed on some platforms 441 void QgsColorRampButton::paintEvent( QPaintEvent *e )
443 QToolButton::paintEvent( e );
445 if ( !mBackgroundSet )
455 QToolButton::showEvent( e );
460 QToolButton::resizeEvent( event );
469 mColorRamp = colorramp->
clone();
472 if ( ( oldColorRamp != mColorRamp ) || !mColorRampSet )
481 mColorRampSet =
true;
486 if ( !name.isEmpty() )
488 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
510 backgroundColorRamp = mColorRamp;
513 QSize currentIconSize;
517 if ( !mIconSize.isValid() )
520 QStyleOptionToolButton opt;
521 initStyleOption( &opt );
522 QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton,
526 mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
528 mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
531 currentIconSize = mIconSize;
537 currentIconSize = QSize( width() - 10, height() - 6 );
539 currentIconSize = QSize( width() - 10, height() - 12 );
543 if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
552 pm = QPixmap( currentIconSize );
553 pm.fill( Qt::transparent );
556 QPen pen = ( QApplication::palette().buttonText().color() );
558 painter.begin( &pm );
559 painter.setPen( pen );
560 painter.drawText( QRect( 0, 0, currentIconSize.width(), currentIconSize.height() ), Qt::AlignCenter, QStringLiteral(
"Random colors" ) );
566 if ( backgroundColorRamp )
572 setIconSize( currentIconSize );
578 return mColorRamp ? mColorRamp->
clone() :
nullptr;
583 mColorRampDialogTitle = title;
588 return mColorRampDialogTitle;
593 setMenu( showMenu ? mMenu :
nullptr );
594 setPopupMode( showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
602 delete mDefaultColorRamp;
603 mDefaultColorRamp = colorramp->
clone();
626 void QgsColorRampButton::rampWidgetUpdated()
629 if ( limitedRampWidget )
636 if ( colorBrewerRampWidget )
643 if ( presetRampWidget )
QString tags() const
returns the text value of the tags element
bool addColorRamp(const QString &name, QgsColorRamp *colorRamp, bool update=false)
Adds a color ramp to the style.
a dialog for setting properties of a newly saved style.
static const double UI_SCALE_FACTOR
UI scaling factor.
QgsColorBrewerColorRamp * clone() const override
Creates a clone of the color ramp.
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
QString name() const
returns the text value of the name element
Abstract base class for color ramps.
bool saveColorRamp(const QString &name, QgsColorRamp *ramp, bool favorite, const QStringList &tags)
Adds the colorramp to the DB.
A dialog which allows users to modify the properties of a QgsColorBrewerColorRamp.
QgsColorRamp * colorRamp(const QString &name) const
Returns a new copy of the specified color ramp.
virtual void invert()
Inverts the ordering of the color ramp.
bool removeColorRamp(const QString &name)
Removes color ramp from style (and delete it)
A dialog which allows users to modify the properties of a QgsGradientColorRamp.
A dialog which allows users to modify the properties of a QgsPresetSchemeColorRamp.
static QgsStyle * defaultStyle()
Returns default application-wide style.
A dialog which allows users to modify the properties of a QgsLimitedRandomColorRamp.
QgsGradientColorRamp * clone() const override
Creates a clone of the color ramp.
Constrained random color ramp, which returns random colors based on preset parameters.
QStringList symbolNames()
Returns a list of names of symbols.
QgsGradientColorRamp ramp
QgsPresetSchemeColorRamp * clone() const override
Creates a clone of the color ramp.
static QIcon colorRampPreviewIcon(QgsColorRamp *ramp, QSize size, int padding=0)
Returns an icon preview for a color ramp.
A scheme based color ramp consisting of a list of predefined colors.
Totally random color ramp.
QgsGradientColorRamp * cloneGradientRamp() const
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0)
Returns a pixmap preview for a color ramp.
QStringList colorRampNames()
Returns a list of names of color ramps.
QgsLimitedRandomColorRamp * clone() const override
Creates a clone of the color ramp.
QgsCptCityColorRamp * clone() const override
Creates a clone of the color ramp.
QgsPresetSchemeColorRamp ramp
Color ramp utilising "Color Brewer" preset color schemes.
bool isFavorite() const
returns whether the favorite element is checked
A dialog which allows users to modify the properties of a QgsCptCityColorRamp.
QgsColorBrewerColorRamp ramp
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
QgsLimitedRandomColorRamp ramp
QStringList symbolsOfFavorite(StyleEntity type) const
Returns the symbol names which are flagged as favorite.
bool saveAsGradientRamp() const
Returns true if the ramp should be converted to a QgsGradientColorRamp.