QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#include "moc_qgspresetcolorrampdialog.cpp"
18
19#include "qgssymbollayerutils.h"
20#include "qgscolordialog.h"
21#include "qgshelp.h"
22
23#include <QFileDialog>
24#include <QAbstractButton>
25#include <QDialogButtonBox>
26#include <QMessageBox>
27
29 : QgsPanelWidget( parent )
30 , mRamp( ramp )
31{
32 setupUi( this );
33 connect( mButtonAddColor, &QToolButton::clicked, this, &QgsPresetColorRampWidget::mButtonAddColor_clicked );
34 mTreeColors->setScheme( &mRamp );
35
36 connect( mButtonCopyColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::copyColors );
37 connect( mButtonRemoveColor, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::removeSelection );
38 connect( mButtonPasteColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::pasteColors );
39 connect( mButtonImportColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::showImportColorsDialog );
40 connect( mButtonExportColors, &QAbstractButton::clicked, mTreeColors, &QgsColorSchemeList::showExportColorsDialog );
41
42 connect( mTreeColors->model(), &QAbstractItemModel::dataChanged, this, &QgsPresetColorRampWidget::schemeChanged );
43 connect( mTreeColors->model(), &QAbstractItemModel::rowsRemoved, this, &QgsPresetColorRampWidget::schemeChanged );
44
45 updatePreview();
46}
47
52
54{
55 mRamp = ramp;
56 mTreeColors->setScheme( &mRamp );
57 updatePreview();
58 emit changed();
59}
60
61void QgsPresetColorRampWidget::updatePreview()
62{
63 const QSize size( 300, 40 );
64 lblPreview->setPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size ) );
65}
66
67void QgsPresetColorRampWidget::setColors()
68{
69 updatePreview();
70 emit changed();
71}
72
73void QgsPresetColorRampWidget::mButtonAddColor_clicked()
74{
75 if ( dockMode() )
76 {
78
80 colorWidget->setPanelTitle( tr( "Select Color" ) );
81 colorWidget->setAllowOpacity( true );
82 connect( colorWidget, &QgsCompoundColorWidget::currentColorChanged, this, &QgsPresetColorRampWidget::newColorChanged );
83 openPanel( colorWidget );
84 }
85 else
86 {
87 const QColor newColor = QgsColorDialog::getColor( QColor(), this->parentWidget(), tr( "Select Color" ), true );
88 if ( !newColor.isValid() )
89 {
90 return;
91 }
92 activateWindow();
93
94 mTreeColors->addColor( newColor, QgsSymbolLayerUtils::colorToName( newColor ) );
95 }
96}
97
98void QgsPresetColorRampWidget::schemeChanged()
99{
100 mTreeColors->saveColorsToScheme();
101 updatePreview();
102 emit changed();
103}
104
105void QgsPresetColorRampWidget::newColorChanged( const QColor &color )
106{
107 const int row = mTreeColors->model()->rowCount() - 1;
108 const QModelIndex colorIndex = mTreeColors->model()->index( row, 0 );
109 mTreeColors->model()->setData( colorIndex, color );
110}
111
113 : QDialog( parent )
114{
115 QVBoxLayout *vLayout = new QVBoxLayout();
116 mWidget = new QgsPresetColorRampWidget( ramp );
117 connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
118
119 vLayout->addWidget( mWidget );
120 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
121 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
122 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
123 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsPresetColorRampDialog::showHelp );
124 vLayout->addWidget( mButtonBox );
125 setLayout( vLayout );
126 setWindowTitle( tr( "Color Presets Ramp" ) );
128}
129
130QDialogButtonBox *QgsPresetColorRampDialog::buttonBox() const
131{
132 return mButtonBox;
133}
134
135void QgsPresetColorRampDialog::showHelp()
136{
137 QgsHelp::openHelp( QStringLiteral( "style_library/style_manager.html#setting-a-color-ramp" ) );
138}
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:39
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.