QGIS API Documentation  2.99.0-Master (ae4d26a)
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 "qgscursors.h"
22 #include "qgsapplication.h"
23 #include "qgssettings.h"
24 
25 #include <QPushButton>
26 #include <QMenu>
27 #include <QToolButton>
28 #include <QFileDialog>
29 #include <QMessageBox>
30 #include <QDesktopWidget>
31 #include <QMouseEvent>
32 #include <QInputDialog>
33 
34 QgsColorDialog::QgsColorDialog( QWidget *parent, Qt::WindowFlags fl, const QColor &color )
35  : QDialog( parent, fl )
36  , mPreviousColor( color )
37 {
38  setupUi( this );
39  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsColorDialog::mButtonBox_accepted );
40  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsColorDialog::mButtonBox_rejected );
41  connect( mButtonBox, &QDialogButtonBox::clicked, this, &QgsColorDialog::mButtonBox_clicked );
42 
43  QgsSettings settings;
44  restoreGeometry( settings.value( QStringLiteral( "Windows/ColorDialog/geometry" ) ).toByteArray() );
45 
46  if ( mPreviousColor.isValid() )
47  {
48  QPushButton *resetButton = new QPushButton( tr( "Reset" ) );
49  mButtonBox->addButton( resetButton, QDialogButtonBox::ResetRole );
50  }
51 
52  if ( color.isValid() )
53  {
54  mColorWidget->setColor( color );
55  mColorWidget->setPreviousColor( color );
56  }
57 
58  mColorWidget->setAllowOpacity( true );
59 
61  connect( this, &QDialog::rejected, this, &QgsColorDialog::discardColor );
62  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsColorDialog::showHelp );
63 }
64 
65 QColor QgsColorDialog::color() const
66 {
67  return mColorWidget->color();
68 }
69 
70 void QgsColorDialog::setTitle( const QString &title )
71 {
72  setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title );
73 }
74 
75 void QgsColorDialog::setAllowOpacity( const bool allowOpacity )
76 {
77  mAllowOpacity = allowOpacity;
78  mColorWidget->setAllowOpacity( allowOpacity );
79 }
80 
81 QColor QgsColorDialog::getLiveColor( const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent, const QString &title, const bool allowOpacity )
82 {
83  QColor returnColor( initialColor );
84 
85  QgsSettings settings;
86 
87  //using native color dialogs?
88  bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
89  if ( useNative )
90  {
91  QColorDialog *liveDialog = new QColorDialog( initialColor, parent );
92  liveDialog->setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title );
93  liveDialog->setOptions( allowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
94 
95  connect( liveDialog, SIGNAL( currentColorChanged( const QColor & ) ),
96  updateObject, updateSlot );
97 
98  if ( liveDialog->exec() )
99  {
100  returnColor = liveDialog->currentColor();
101  }
102  delete liveDialog;
103  }
104  else
105  {
106  QgsColorDialog *liveDialog = new QgsColorDialog( parent, 0, initialColor );
107  liveDialog->setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title );
108  if ( !allowOpacity )
109  {
110  liveDialog->setAllowOpacity( false );
111  }
112 
113  connect( liveDialog, SIGNAL( currentColorChanged( const QColor & ) ),
114  updateObject, updateSlot );
115 
116  if ( liveDialog->exec() )
117  {
118  returnColor = liveDialog->color();
119  }
120  delete liveDialog;
121  }
122 
123  return returnColor;
124 }
125 
126 QColor QgsColorDialog::getColor( const QColor &initialColor, QWidget *parent, const QString &title, const bool allowOpacity )
127 {
128  QString dialogTitle = title.isEmpty() ? tr( "Select Color" ) : title;
129 
130  QgsSettings settings;
131  //using native color dialogs?
132  bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
133  if ( useNative )
134  {
135  return QColorDialog::getColor( initialColor, parent, dialogTitle, allowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
136  }
137  else
138  {
139  QgsColorDialog *dialog = new QgsColorDialog( parent, 0, initialColor );
140  dialog->setWindowTitle( dialogTitle );
141  dialog->setAllowOpacity( allowOpacity );
142 
143  QColor result;
144  if ( dialog->exec() )
145  {
146  result = dialog->color();
147  }
148 
149  if ( !parent )
150  {
151  delete dialog;
152  }
153  return result;
154  }
155 }
156 
157 void QgsColorDialog::mButtonBox_accepted()
158 {
159  saveSettings();
160  accept();
161 }
162 
163 void QgsColorDialog::mButtonBox_rejected()
164 {
165  saveSettings();
166  reject();
167 }
168 
169 void QgsColorDialog::mButtonBox_clicked( QAbstractButton *button )
170 {
171  if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ResetRole && mPreviousColor.isValid() )
172  {
173  setColor( mPreviousColor );
174  }
175 }
176 
177 void QgsColorDialog::discardColor()
178 {
179  mColorWidget->setDiscarded( true );
180 }
181 
182 void QgsColorDialog::saveSettings()
183 {
184  QgsSettings settings;
185  settings.setValue( QStringLiteral( "Windows/ColorDialog/geometry" ), saveGeometry() );
186 }
187 
188 void QgsColorDialog::setColor( const QColor &color )
189 {
190  if ( !color.isValid() )
191  {
192  return;
193  }
194 
195  QColor fixedColor = QColor( color );
196  if ( !mAllowOpacity )
197  {
198  //alpha disallowed, so don't permit transparent colors
199  fixedColor.setAlpha( 255 );
200  }
201 
202  mColorWidget->setColor( fixedColor );
203  emit currentColorChanged( fixedColor );
204 }
205 
206 void QgsColorDialog::closeEvent( QCloseEvent *e )
207 {
208  saveSettings();
209  QDialog::closeEvent( e );
210 }
211 
212 void QgsColorDialog::showHelp()
213 {
214  QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#color-selector" ) );
215 }
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:55
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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.
void setAllowOpacity(const bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color dialog.
A custom QGIS dialog for selecting a color.
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), const bool allowOpacity=false)
Return a color selection from a color dialog.
void currentColorChanged(const QColor &color)
Emitted when the dialog&#39;s color changes.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:34
QgsColorDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags, const QColor &color=QColor())
Create a new color picker dialog.
static QColor getLiveColor(const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent=nullptr, const QString &title=QString(), const bool allowOpacity=true)
Return a color selection from a color dialog, with live updating of interim selections.