QGIS API Documentation  3.6.0-Noosa (5873452)
qgscolordialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolordialog.cpp - color selection dialog
3 
4  ---------------------
5  begin : March 19, 2013
6  copyright : (C) 2013 by Larry Shaffer
7  email : larrys at dakcarto dot com
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgscolordialog.h"
18 #include "qgscolorscheme.h"
19 #include "qgscolorschemeregistry.h"
20 #include "qgssymbollayerutils.h"
21 #include "qgsapplication.h"
22 #include "qgssettings.h"
23 
24 #include <QPushButton>
25 #include <QMenu>
26 #include <QToolButton>
27 #include <QFileDialog>
28 #include <QMessageBox>
29 #include <QDesktopWidget>
30 #include <QMouseEvent>
31 #include <QInputDialog>
32 
33 QgsColorDialog::QgsColorDialog( QWidget *parent, Qt::WindowFlags fl, const QColor &color )
34  : QDialog( parent, fl )
35  , mPreviousColor( color )
36 {
37  setupUi( this );
38  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsColorDialog::mButtonBox_accepted );
39  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsColorDialog::mButtonBox_rejected );
40  connect( mButtonBox, &QDialogButtonBox::clicked, this, &QgsColorDialog::mButtonBox_clicked );
41 
42  QgsSettings settings;
43  restoreGeometry( settings.value( QStringLiteral( "Windows/ColorDialog/geometry" ) ).toByteArray() );
44 
45  if ( mPreviousColor.isValid() )
46  {
47  QPushButton *resetButton = new QPushButton( tr( "Reset" ) );
48  mButtonBox->addButton( resetButton, QDialogButtonBox::ResetRole );
49  }
50 
51  if ( color.isValid() )
52  {
53  mColorWidget->setColor( color );
54  mColorWidget->setPreviousColor( color );
55  }
56 
57  mColorWidget->setAllowOpacity( true );
58 
60  connect( this, &QDialog::rejected, this, &QgsColorDialog::discardColor );
61  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsColorDialog::showHelp );
62 }
63 
64 QColor QgsColorDialog::color() const
65 {
66  return mColorWidget->color();
67 }
68 
69 void QgsColorDialog::setTitle( const QString &title )
70 {
71  setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title );
72 }
73 
74 void QgsColorDialog::setAllowOpacity( const bool allowOpacity )
75 {
76  mAllowOpacity = allowOpacity;
77  mColorWidget->setAllowOpacity( allowOpacity );
78 }
79 
80 QColor QgsColorDialog::getColor( const QColor &initialColor, QWidget *parent, const QString &title, const bool allowOpacity )
81 {
82  QString dialogTitle = title.isEmpty() ? tr( "Select Color" ) : title;
83 
84  QgsSettings settings;
85  //using native color dialogs?
86  bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
87  if ( useNative )
88  {
89  return QColorDialog::getColor( initialColor, parent, dialogTitle, allowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
90  }
91  else
92  {
93  QgsColorDialog *dialog = new QgsColorDialog( parent, nullptr, initialColor );
94  dialog->setWindowTitle( dialogTitle );
95  dialog->setAllowOpacity( allowOpacity );
96 
97  QColor result;
98  if ( dialog->exec() )
99  {
100  result = dialog->color();
101  }
102 
103  if ( !parent )
104  {
105  delete dialog;
106  }
107  return result;
108  }
109 }
110 
111 void QgsColorDialog::mButtonBox_accepted()
112 {
113  saveSettings();
114  accept();
115 }
116 
117 void QgsColorDialog::mButtonBox_rejected()
118 {
119  saveSettings();
120  reject();
121 }
122 
123 void QgsColorDialog::mButtonBox_clicked( QAbstractButton *button )
124 {
125  if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ResetRole && mPreviousColor.isValid() )
126  {
127  setColor( mPreviousColor );
128  }
129 }
130 
131 void QgsColorDialog::discardColor()
132 {
133  mColorWidget->setDiscarded( true );
134 }
135 
136 void QgsColorDialog::saveSettings()
137 {
138  QgsSettings settings;
139  settings.setValue( QStringLiteral( "Windows/ColorDialog/geometry" ), saveGeometry() );
140 }
141 
142 void QgsColorDialog::setColor( const QColor &color )
143 {
144  if ( !color.isValid() )
145  {
146  return;
147  }
148 
149  QColor fixedColor = QColor( color );
150  if ( !mAllowOpacity )
151  {
152  //alpha disallowed, so don't permit transparent colors
153  fixedColor.setAlpha( 255 );
154  }
155 
156  mColorWidget->setColor( fixedColor );
157  emit currentColorChanged( fixedColor );
158 }
159 
160 void QgsColorDialog::closeEvent( QCloseEvent *e )
161 {
162  saveSettings();
163  QDialog::closeEvent( e );
164 }
165 
166 void QgsColorDialog::showHelp()
167 {
168  QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#color-selector" ) );
169 }
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), bool allowOpacity=false)
Returns a color selection from a color dialog.
void currentColorChanged(const QColor &color)
Emitted when the dialog&#39;s color changes.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color dialog.
void saveGeometry(QWidget *widget, const QString &keyName)
Save the wigget geometry into settings.
bool restoreGeometry(QWidget *widget, const QString &keyName)
Restore the wigget geometry from settings.
void setTitle(const QString &title)
Sets the title for the color dialog.
void setColor(const QColor &color)
Sets the current color for the dialog.
void closeEvent(QCloseEvent *e) override
QColor color() const
Returns the current color for the dialog.
A custom QGIS dialog for selecting a color.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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:36
QgsColorDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags, const QColor &color=QColor())
Create a new color picker dialog.