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 )
218 void QgsColorRampButton::buttonClicked()
222 showColorRampDialog();
226 QToolButton::showMenu();
230 void QgsColorRampButton::prepareMenu()
234 QAction *invertAction =
new QAction( tr(
"Invert Color Ramp" ),
this );
236 mMenu->addAction( invertAction );
237 connect( invertAction, &QAction::triggered,
this, &QgsColorRampButton::invertColorRamp );
241 QAction *nullAction =
new QAction( tr(
"Clear Current Ramp" ),
this );
242 mMenu->addAction( nullAction );
246 mMenu->addSeparator();
249 if ( mDefaultColorRamp )
251 QAction *defaultColorRampAction =
new QAction( tr(
"Default Color Ramp" ),
this );
252 defaultColorRampAction->setIcon( createMenuIcon( mDefaultColorRamp ) );
253 mMenu->addAction( defaultColorRampAction );
257 if ( mShowRandomColorRamp )
259 QAction *randomColorRampAction =
new QAction( tr(
"Random Color Ramp" ),
this );
260 randomColorRampAction->setCheckable(
true );
262 mMenu->addAction( randomColorRampAction );
265 if (
isRandomColorRamp() || dynamic_cast<QgsLimitedRandomColorRamp *>( mColorRamp ) )
267 QAction *shuffleRandomColorRampAction =
new QAction( tr(
"Shuffle Random Colors" ),
this );
268 mMenu->addAction( shuffleRandomColorRampAction );
273 mMenu->addSeparator();
278 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
280 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( *it ) );
282 if ( !mShowGradientOnly || ( ramp->type() == QLatin1String(
"gradient" ) || ramp->type() == QLatin1String(
"cpt-city" ) ) )
285 QAction *ra =
new QAction( *it,
this );
287 connect( ra, &QAction::triggered,
this, &QgsColorRampButton::loadColorRamp );
288 mMenu->addAction( ra );
292 mMenu->addSeparator();
294 mAllRampsMenu->clear();
295 mMenu->addMenu( mAllRampsMenu );
298 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
300 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( *it ) );
302 if ( !mShowGradientOnly || ( ramp->type() == QLatin1String(
"gradient" ) || ramp->type() == QLatin1String(
"cpt-city" ) ) )
305 QAction *ra =
new QAction( *it,
this );
307 connect( ra, &QAction::triggered,
this, &QgsColorRampButton::loadColorRamp );
308 mAllRampsMenu->addAction( ra );
312 mMenu->addSeparator();
314 QAction *newColorRampAction =
new QAction( tr(
"Create New Color Ramp…" ),
this );
315 connect( newColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::createColorRamp );
316 mMenu->addAction( newColorRampAction );
318 QAction *editColorRampAction =
new QAction( tr(
"Edit Color Ramp…" ),
this );
320 connect( editColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::showColorRampDialog );
321 mMenu->addAction( editColorRampAction );
323 QAction *saveColorRampAction =
new QAction( tr(
"Save Color Ramp…" ),
this );
325 connect( saveColorRampAction, &QAction::triggered,
this, &QgsColorRampButton::saveColorRamp );
326 mMenu->addAction( saveColorRampAction );
329 void QgsColorRampButton::loadColorRamp()
331 QAction *selectedItem = qobject_cast<QAction *>( sender() );
334 QString name = selectedItem->iconText();
340 void QgsColorRampButton::createColorRamp()
342 QStringList rampTypes;
346 if ( mShowGradientOnly )
348 rampTypes << tr(
"Gradient" ) << tr(
"Catalog: cpt-city" );
352 rampTypes << tr(
"Gradient" ) << tr(
"Color presets" ) << tr(
"Random" ) << tr(
"Catalog: cpt-city" );
353 rampTypes << tr(
"Catalog: ColorBrewer" );
355 rampType = QInputDialog::getItem(
this, tr(
"Color ramp type" ),
356 tr(
"Please select color ramp type:" ), rampTypes, 0,
false, &ok );
358 if ( !ok || rampType.isEmpty() )
362 if ( rampType == tr(
"Gradient" ) )
366 else if ( rampType == tr(
"Random" ) )
370 else if ( rampType == tr(
"Catalog: ColorBrewer" ) )
374 else if ( rampType == tr(
"Color presets" ) )
378 else if ( rampType == tr(
"Catalog: cpt-city" ) )
391 showColorRampDialog();
394 void QgsColorRampButton::saveColorRamp()
397 if ( !saveDlg.exec() || saveDlg.
name().isEmpty() )
405 int res = QMessageBox::warning(
this, tr(
"Save Color Ramp" ),
406 tr(
"Color ramp with name '%1' already exists. Overwrite?" )
407 .arg( saveDlg.
name() ),
408 QMessageBox::Yes | QMessageBox::No );
409 if ( res != QMessageBox::Yes )
416 QStringList colorRampTags = saveDlg.
tags().split(
',' );
426 void QgsColorRampButton::invertColorRamp()
435 if ( e->type() == QEvent::EnabledChange )
439 QToolButton::changeEvent( e );
442 #if 0 // causes too many cyclical updates, but may be needed on some platforms 443 void QgsColorRampButton::paintEvent( QPaintEvent *e )
445 QToolButton::paintEvent( e );
447 if ( !mBackgroundSet )
457 QToolButton::showEvent( e );
462 QToolButton::resizeEvent( event );
471 mColorRamp = colorramp->
clone();
474 if ( ( oldColorRamp != mColorRamp ) || !mColorRampSet )
483 mColorRampSet =
true;
488 if ( !name.isEmpty() )
490 std::unique_ptr< QgsColorRamp > ramp( mStyle->
colorRamp( name ) );
512 backgroundColorRamp = mColorRamp;
515 QSize currentIconSize;
519 if ( !mIconSize.isValid() )
522 QStyleOptionToolButton opt;
523 initStyleOption( &opt );
524 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 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 delete mDefaultColorRamp;
605 mDefaultColorRamp = colorramp->
clone();
628 void QgsColorRampButton::rampWidgetUpdated()
631 if ( limitedRampWidget )
638 if ( colorBrewerRampWidget )
645 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.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly...
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.