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