QGIS API Documentation  3.2.0-Bonn (bc43194)
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  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  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  int row = mTreeColors->model()->rowCount() - 1;
105  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  vLayout->addWidget( mWidget );
115  QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
116  connect( bbox, &QDialogButtonBox::accepted, this, &QDialog::accept );
117  connect( bbox, &QDialogButtonBox::rejected, this, &QDialog::reject );
118  connect( bbox, &QDialogButtonBox::helpRequested, this, &QgsPresetColorRampDialog::showHelp );
119  vLayout->addWidget( bbox );
120  setLayout( vLayout );
121  setWindowTitle( tr( "Color Presets Ramp" ) );
123 }
124 
125 void QgsPresetColorRampDialog::showHelp()
126 {
127  QgsHelp::openHelp( QStringLiteral( "working_with_vector/style_library.html#color-ramp" ) );
128 }
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), bool allowOpacity=false)
Returns a color selection from a color dialog.
bool dockMode()
Returns the dock mode state.
void removeSelection()
Removes any selected colors from the list.
void setRamp(const QgsPresetSchemeColorRamp &ramp)
Sets the color ramp to show in the dialog.
QgsPresetColorRampWidget(const QgsPresetSchemeColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsPresetColorRampWidget.
A widget which allows users to modify the properties of a QgsPresetSchemeColorRamp.
void changed()
Emitted when the dialog settings change.
Base class for any widget that can be shown as a inline panel.
void pasteColors()
Pastes colors from clipboard to the list.
void showExportColorsDialog()
Displays a file picker dialog allowing users to export colors from the list into a file...
void changed()
Emitted when the dialog settings change.
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...
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
QgsPresetColorRampDialog(const QgsPresetSchemeColorRamp &ramp, QWidget *parent=nullptr)
Constructor for QgsPresetColorRampDialog.
A scheme based color ramp consisting of a list of predefined colors.
Definition: qgscolorramp.h:471
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0)
Returns a pixmap preview for a color ramp.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color dialog.
void copyColors()
Copies colors from the list to the clipboard.
void currentColorChanged(const QColor &color)
Emitted when the dialog&#39;s color changes.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:35
QgsPresetSchemeColorRamp ramp() const
Returns a color ramp representing the current settings from the dialog.
static QColor lastUsedColor()
Returns the most recently used color.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Use a narrower, vertically stacked layout.