QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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 "qgscolordialog.h"
19#include "qgshelp.h"
20#include "qgssymbollayerutils.h"
21
22#include <QAbstractButton>
23#include <QDialogButtonBox>
24#include <QFileDialog>
25#include <QMessageBox>
26#include <QString>
27
28#include "moc_qgspresetcolorrampdialog.cpp"
29
30using namespace Qt::StringLiterals;
31
33 : QgsPanelWidget( parent )
34 , mRamp( ramp )
35{
36 setupUi( this );
37 connect( mButtonAddColor, &QToolButton::clicked, this, &QgsPresetColorRampWidget::mButtonAddColor_clicked );
38 mTreeColors->setScheme( &mRamp );
39
40 connect( mButtonCopyColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::copyColors );
41 connect( mButtonRemoveColor, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::removeSelection );
42 connect( mButtonPasteColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::pasteColors );
43 connect( mButtonImportColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::showImportColorsDialog );
44 connect( mButtonExportColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::showExportColorsDialog );
45
46 connect( mTreeColors->model(), &QAbstractItemModel::dataChanged, this, &QgsPresetColorRampWidget::schemeChanged );
47 connect( mTreeColors->model(), &QAbstractItemModel::rowsRemoved, this, &QgsPresetColorRampWidget::schemeChanged );
48
49 updatePreview();
50}
51
56
58{
59 mRamp = ramp;
60 mTreeColors->setScheme( &mRamp );
61 updatePreview();
62 emit changed();
63}
64
65void QgsPresetColorRampWidget::updatePreview()
66{
67 const QSize size( 300, 40 );
68 lblPreview->setPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size ) );
69}
70
71void QgsPresetColorRampWidget::setColors()
72{
73 updatePreview();
74 emit changed();
75}
76
77void QgsPresetColorRampWidget::mButtonAddColor_clicked()
78{
79 if ( dockMode() )
80 {
82
83 QgsCompoundColorWidget *colorWidget = new QgsCompoundColorWidget( this, QgsRecentColorScheme::lastUsedColor(), QgsCompoundColorWidget::LayoutVertical );
84 colorWidget->setPanelTitle( tr( "Select Color" ) );
85 colorWidget->setAllowOpacity( true );
86 connect( colorWidget, &QgsCompoundColorWidget::currentColorChanged, this, &QgsPresetColorRampWidget::newColorChanged );
87 openPanel( colorWidget );
88 }
89 else
90 {
91 const QColor newColor = QgsColorDialog::getColor( QColor(), this->parentWidget(), tr( "Select Color" ), true );
92 if ( !newColor.isValid() )
93 {
94 return;
95 }
96 activateWindow();
97
98 mTreeColors->addColor( newColor, QgsSymbolLayerUtils::colorToName( newColor ) );
99 }
100}
101
102void QgsPresetColorRampWidget::schemeChanged()
103{
104 mTreeColors->saveColorsToScheme();
105 updatePreview();
106 emit changed();
107}
108
109void QgsPresetColorRampWidget::newColorChanged( const QColor &color )
110{
111 const int row = mTreeColors->model()->rowCount() - 1;
112 const QModelIndex colorIndex = mTreeColors->model()->index( row, 0 );
113 mTreeColors->model()->setData( colorIndex, color );
114}
115
117 : QDialog( parent )
118{
119 QVBoxLayout *vLayout = new QVBoxLayout();
120 mWidget = new QgsPresetColorRampWidget( ramp );
121 connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
122
123 vLayout->addWidget( mWidget );
124 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
125 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
126 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
127 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsPresetColorRampDialog::showHelp );
128 vLayout->addWidget( mButtonBox );
129 setLayout( vLayout );
130 setWindowTitle( tr( "Color Presets Ramp" ) );
132}
133
134QDialogButtonBox *QgsPresetColorRampDialog::buttonBox() const
135{
136 return mButtonBox;
137}
138
139void QgsPresetColorRampDialog::showHelp()
140{
141 QgsHelp::openHelp( u"style_library/style_manager.html#setting-a-color-ramp"_s );
142}
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.
@ 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:41
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
bool dockMode() const
Returns the dock mode state.
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
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.