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