QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
QgsPresetColorRampDialog::QgsPresetColorRampDialog
QgsPresetColorRampDialog(const QgsPresetSchemeColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsPresetColorRampDialog.
Definition: qgspresetcolorrampdialog.cpp:109
QgsPresetColorRampDialog::buttonBox
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
Definition: qgspresetcolorrampdialog.cpp:127
QgsCompoundColorWidget::setAllowOpacity
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color dialog.
Definition: qgscompoundcolorwidget.cpp:303
QgsPresetColorRampWidget::ramp
QgsPresetSchemeColorRamp ramp
Definition: qgspresetcolorrampdialog.h:37
QgsPresetSchemeColorRamp
A scheme based color ramp consisting of a list of predefined colors.
Definition: qgscolorrampimpl.h:543
qgssymbollayerutils.h
QgsPanelWidget::openPanel
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
Definition: qgspanelwidget.cpp:84
QgsColorSchemeList::showImportColorsDialog
void showImportColorsDialog()
Displays a file picker dialog allowing users to import colors into the list from a file.
Definition: qgscolorschemelist.cpp:141
QgsColorSchemeList::copyColors
void copyColors()
Copies colors from the list to the clipboard.
Definition: qgscolorschemelist.cpp:118
QgsPresetColorRampWidget::setRamp
void setRamp(const QgsPresetSchemeColorRamp &ramp)
Sets the color ramp to show in the dialog.
Definition: qgspresetcolorrampdialog.cpp:50
QgsPresetColorRampWidget
A widget which allows users to modify the properties of a QgsPresetSchemeColorRamp.
Definition: qgspresetcolorrampdialog.h:34
QgsPanelWidget::dockMode
bool dockMode()
Returns the dock mode state.
Definition: qgspanelwidget.h:93
QgsPresetColorRampWidget::QgsPresetColorRampWidget
QgsPresetColorRampWidget(const QgsPresetSchemeColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsPresetColorRampWidget.
Definition: qgspresetcolorrampdialog.cpp:25
QgsCompoundColorWidget
A custom QGIS widget for selecting a color, including options for selecting colors via hue wheel,...
Definition: qgscompoundcolorwidget.h:33
QgsCompoundColorWidget::LayoutVertical
@ LayoutVertical
Use a narrower, vertically stacked layout.
Definition: qgscompoundcolorwidget.h:44
QgsPresetColorRampWidget::changed
void changed()
Emitted when the dialog settings change.
QgsCompoundColorWidget::currentColorChanged
void currentColorChanged(const QColor &color)
Emitted when the dialog's color changes.
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:29
QgsColorSchemeList::pasteColors
void pasteColors()
Pastes colors from clipboard to the list.
Definition: qgscolorschemelist.cpp:100
QgsPanelWidget::panelAccepted
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
QgsSymbolLayerUtils::colorRampPreviewPixmap
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.
Definition: qgssymbollayerutils.cpp:1026
QgsColorSchemeList::removeSelection
void removeSelection()
Removes any selected colors from the list.
Definition: qgscolorschemelist.cpp:75
QgsPresetColorRampDialog::changed
void changed()
Emitted when the dialog settings change.
QgsColorSchemeList::showExportColorsDialog
void showExportColorsDialog()
Displays a file picker dialog allowing users to export colors from the list into a file.
Definition: qgscolorschemelist.cpp:170
QgsPanelWidget::setPanelTitle
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Definition: qgspanelwidget.h:44
qgscolordialog.h
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
QgsRecentColorScheme::lastUsedColor
static QColor lastUsedColor()
Returns the most recently used color.
Definition: qgscolorscheme.cpp:104
qgspresetcolorrampdialog.h
QgsColorDialog::getColor
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), bool allowOpacity=false)
Returns a color selection from a color dialog.
Definition: qgscolordialog.cpp:81
QgsPresetColorRampDialog::ramp
QgsPresetSchemeColorRamp ramp
Definition: qgspresetcolorrampdialog.h:89
QgsSymbolLayerUtils::colorToName
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
Definition: qgssymbollayerutils.cpp:3416