QGIS API Documentation  3.25.0-Master (6b426f5f8a)
qgspresetcolorrampdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspresetcolorrampdialog.cpp
3  ----------------------------
4  begin : September 2016
5  copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgssymbollayerutils.h"
19 #include "qgscolordialog.h"
20 #include <QFileDialog>
21 #include <QAbstractButton>
22 #include <QDialogButtonBox>
23 #include <QMessageBox>
24 
26  : QgsPanelWidget( parent )
27  , mRamp( ramp )
28 {
29  setupUi( this );
30  connect( mButtonAddColor, &QToolButton::clicked, this, &QgsPresetColorRampWidget::mButtonAddColor_clicked );
31  mTreeColors->setScheme( &mRamp );
32 
33  connect( mButtonCopyColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::copyColors );
34  connect( mButtonRemoveColor, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::removeSelection );
35  connect( mButtonPasteColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::pasteColors );
36  connect( mButtonImportColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::showImportColorsDialog );
37  connect( mButtonExportColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::showExportColorsDialog );
38 
39  connect( mTreeColors->model(), &QAbstractItemModel::dataChanged, this, &QgsPresetColorRampWidget::schemeChanged );
40  connect( mTreeColors->model(), &QAbstractItemModel::rowsRemoved, this, &QgsPresetColorRampWidget::schemeChanged );
41 
42  updatePreview();
43 }
44 
46 {
47  return mRamp;
48 }
49 
51 {
52  mRamp = ramp;
53  mTreeColors->setScheme( &mRamp );
54  updatePreview();
55  emit changed();
56 }
57 
58 void QgsPresetColorRampWidget::updatePreview()
59 {
60  const QSize size( 300, 40 );
61  lblPreview->setPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size ) );
62 }
63 
64 void QgsPresetColorRampWidget::setColors()
65 {
66  updatePreview();
67  emit changed();
68 }
69 
70 void QgsPresetColorRampWidget::mButtonAddColor_clicked()
71 {
72  if ( dockMode() )
73  {
75 
77  colorWidget->setPanelTitle( tr( "Select Color" ) );
78  colorWidget->setAllowOpacity( true );
79  connect( colorWidget, &QgsCompoundColorWidget::currentColorChanged, this, &QgsPresetColorRampWidget::newColorChanged );
80  openPanel( colorWidget );
81  }
82  else
83  {
84  const QColor newColor = QgsColorDialog::getColor( QColor(), this->parentWidget(), tr( "Select Color" ), true );
85  if ( !newColor.isValid() )
86  {
87  return;
88  }
89  activateWindow();
90 
91  mTreeColors->addColor( newColor, QgsSymbolLayerUtils::colorToName( newColor ) );
92  }
93 }
94 
95 void QgsPresetColorRampWidget::schemeChanged()
96 {
97  mTreeColors->saveColorsToScheme();
98  updatePreview();
99  emit changed();
100 }
101 
102 void QgsPresetColorRampWidget::newColorChanged( const QColor &color )
103 {
104  const int row = mTreeColors->model()->rowCount() - 1;
105  const QModelIndex colorIndex = mTreeColors->model()->index( row, 0 );
106  mTreeColors->model()->setData( colorIndex, color );
107 }
108 
110  : QDialog( parent )
111 {
112  QVBoxLayout *vLayout = new QVBoxLayout();
113  mWidget = new QgsPresetColorRampWidget( ramp );
114  connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
115 
116  vLayout->addWidget( mWidget );
117  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
118  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
119  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
120  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsPresetColorRampDialog::showHelp );
121  vLayout->addWidget( mButtonBox );
122  setLayout( vLayout );
123  setWindowTitle( tr( "Color Presets Ramp" ) );
125 }
126 
127 QDialogButtonBox *QgsPresetColorRampDialog::buttonBox() const
128 {
129  return mButtonBox;
130 }
131 
132 void QgsPresetColorRampDialog::showHelp()
133 {
134  QgsHelp::openHelp( QStringLiteral( "style_library/style_manager.html#setting-a-color-ramp" ) );
135 }
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), bool allowOpacity=false)
Returns a color selection from a color dialog.
void pasteColors()
Pastes colors from clipboard to the list.
void removeSelection()
Removes any selected colors from the list.
void copyColors()
Copies colors from the list to the clipboard.
void showExportColorsDialog()
Displays a file picker dialog allowing users to export colors from the list into a file.
void showImportColorsDialog()
Displays a file picker dialog allowing users to import colors into the list from a file.
A custom QGIS widget for selecting a color, including options for selecting colors via hue wheel,...
@ LayoutVertical
Use a narrower, vertically stacked layout.
void currentColorChanged(const QColor &color)
Emitted when the dialog's color changes.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color dialog.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
Base class for any widget that can be shown as a inline panel.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
bool dockMode()
Returns the dock mode state.
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
QgsPresetSchemeColorRamp ramp
void changed()
Emitted when the dialog settings change.
QgsPresetColorRampDialog(const QgsPresetSchemeColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsPresetColorRampDialog.
A widget which allows users to modify the properties of a QgsPresetSchemeColorRamp.
void setRamp(const QgsPresetSchemeColorRamp &ramp)
Sets the color ramp to show in the dialog.
QgsPresetSchemeColorRamp ramp
QgsPresetColorRampWidget(const QgsPresetSchemeColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsPresetColorRampWidget.
void changed()
Emitted when the dialog settings change.
A scheme based color ramp consisting of a list of predefined colors.
static QColor lastUsedColor()
Returns the most recently used color.
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.
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.