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()
 
  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 ) {
 
  347                       return type.first != QgsGradientColorRamp::typeString() && type.first != QgsCptCityColorRamp::typeString();
 
  352  for ( 
const QPair<QString, QString> &type : rampTypes )
 
  353    rampTypeNames << type.second;
 
  355  const QString selectedRampTypeName = QInputDialog::getItem( 
this, tr( 
"Color ramp type" ), 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();
 
  395void QgsColorRampButton::saveColorRamp()
 
  398  if ( !saveDlg.exec() || saveDlg.name().isEmpty() )
 
  403  QgsStyle *destinationStyle = saveDlg.destinationStyle();
 
  406  if ( destinationStyle->
symbolNames().contains( saveDlg.name() ) )
 
  408    const int res = QMessageBox::warning( 
this, tr( 
"Save Color Ramp" ), tr( 
"Color ramp with name '%1' already exists. Overwrite?" ).arg( saveDlg.name() ), QMessageBox::Yes | QMessageBox::No );
 
  409    if ( res != QMessageBox::Yes )
 
  416  const QStringList colorRampTags = saveDlg.tags().split( 
',' );
 
  421  destinationStyle->
saveColorRamp( saveDlg.name(), ramp, saveDlg.isFavorite(), colorRampTags );
 
  426void QgsColorRampButton::invertColorRamp()
 
  428  mColorRamp->invert();
 
  435  if ( e->type() == QEvent::EnabledChange )
 
  439  QToolButton::changeEvent( e );
 
 
  443void QgsColorRampButton::paintEvent( QPaintEvent *e )
 
  445  QToolButton::paintEvent( e );
 
  447  if ( !mBackgroundSet )
 
  457  QToolButton::showEvent( e );
 
 
  462  QToolButton::resizeEvent( 
event );
 
 
  470  if ( colorramp == mColorRamp.get() )
 
  473  mColorRamp.reset( colorramp ? colorramp->
clone() : nullptr );
 
 
  484  if ( !name.isEmpty() )
 
  486    const std::unique_ptr<QgsColorRamp> ramp( mStyle->
colorRamp( name ) );
 
 
  508    backgroundColorRamp = mColorRamp.get();
 
  511  QSize currentIconSize;
 
  515    if ( !mIconSize.isValid() )
 
  518      QStyleOptionToolButton opt;
 
  519      initStyleOption( &opt );
 
  520      const QRect buttonSize = QApplication::style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButton, 
this );
 
  523      mIconSize = QSize( buttonSize.width() - 10, height() - 6 );
 
  525      mIconSize = QSize( buttonSize.width() - 10, height() - 12 );
 
  528    currentIconSize = mIconSize;
 
  534    currentIconSize = QSize( width() - 10, height() - 6 );
 
  536    currentIconSize = QSize( width() - 10, height() - 12 );
 
  540  if ( !currentIconSize.isValid() || currentIconSize.width() <= 0 || currentIconSize.height() <= 0 )
 
  549    pm = QPixmap( currentIconSize );
 
  550    pm.fill( Qt::transparent );
 
  553    const QPen pen = ( palette().buttonText().color() );
 
  555    painter.begin( &pm );
 
  556    painter.setPen( pen );
 
  557    painter.drawText( QRect( 0, 0, currentIconSize.width(), currentIconSize.height() ), Qt::AlignCenter, QStringLiteral( 
"Random colors" ) );
 
  563    if ( backgroundColorRamp )
 
  569  setIconSize( currentIconSize );
 
 
  575  return mColorRamp ? mColorRamp->clone() : 
nullptr;
 
 
  580  mColorRampDialogTitle = title;
 
 
  585  return mColorRampDialogTitle;
 
 
  590  setMenu( 
showMenu ? mMenu : nullptr );
 
  591  setPopupMode( 
showMenu ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
 
 
  599  return menu() ? true : 
false;
 
 
  604  mDefaultColorRamp.reset( colorramp ? colorramp->
clone() : nullptr );
 
 
  609  return mDefaultColorRamp ? mDefaultColorRamp->clone() : 
nullptr;
 
 
  632void QgsColorRampButton::rampWidgetUpdated()
 
  635  if ( limitedRampWidget )
 
  642  if ( colorBrewerRampWidget )
 
  649  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.
 
A color ramp consisting of random colors, constrained within component ranges.
 
A dialog for setting properties of a newly saved style.
 
A database of saved style entities, including symbols, color ramps, text formats and others.
 
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.
 
bool saveColorRamp(const QString &name, const QgsColorRamp *ramp, bool favorite, const QStringList &tags)
Adds the colorramp to the database.
 
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)