QGIS API Documentation  2.99.0-Master (19b062c)
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::getLiveColor( const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent, const QString &title, const bool allowOpacity )
81 {
82  QColor returnColor( initialColor );
83 
84  QgsSettings settings;
85 
86  //using native color dialogs?
87  bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
88  if ( useNative )
89  {
90  QColorDialog *liveDialog = new QColorDialog( initialColor, parent );
91  liveDialog->setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title );
92  liveDialog->setOptions( allowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
93 
94  connect( liveDialog, SIGNAL( currentColorChanged( const QColor & ) ),
95  updateObject, updateSlot );
96 
97  if ( liveDialog->exec() )
98  {
99  returnColor = liveDialog->currentColor();
100  }
101  delete liveDialog;
102  }
103  else
104  {
105  QgsColorDialog *liveDialog = new QgsColorDialog( parent, 0, initialColor );
106  liveDialog->setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title );
107  if ( !allowOpacity )
108  {
109  liveDialog->setAllowOpacity( false );
110  }
111 
112  connect( liveDialog, SIGNAL( currentColorChanged( const QColor & ) ),
113  updateObject, updateSlot );
114 
115  if ( liveDialog->exec() )
116  {
117  returnColor = liveDialog->color();
118  }
119  delete liveDialog;
120  }
121 
122  return returnColor;
123 }
124 
125 QColor QgsColorDialog::getColor( const QColor &initialColor, QWidget *parent, const QString &title, const bool allowOpacity )
126 {
127  QString dialogTitle = title.isEmpty() ? tr( "Select Color" ) : title;
128 
129  QgsSettings settings;
130  //using native color dialogs?
131  bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
132  if ( useNative )
133  {
134  return QColorDialog::getColor( initialColor, parent, dialogTitle, allowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
135  }
136  else
137  {
138  QgsColorDialog *dialog = new QgsColorDialog( parent, 0, initialColor );
139  dialog->setWindowTitle( dialogTitle );
140  dialog->setAllowOpacity( allowOpacity );
141 
142  QColor result;
143  if ( dialog->exec() )
144  {
145  result = dialog->color();
146  }
147 
148  if ( !parent )
149  {
150  delete dialog;
151  }
152  return result;
153  }
154 }
155 
156 void QgsColorDialog::mButtonBox_accepted()
157 {
158  saveSettings();
159  accept();
160 }
161 
162 void QgsColorDialog::mButtonBox_rejected()
163 {
164  saveSettings();
165  reject();
166 }
167 
168 void QgsColorDialog::mButtonBox_clicked( QAbstractButton *button )
169 {
170  if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ResetRole && mPreviousColor.isValid() )
171  {
172  setColor( mPreviousColor );
173  }
174 }
175 
176 void QgsColorDialog::discardColor()
177 {
178  mColorWidget->setDiscarded( true );
179 }
180 
181 void QgsColorDialog::saveSettings()
182 {
183  QgsSettings settings;
184  settings.setValue( QStringLiteral( "Windows/ColorDialog/geometry" ), saveGeometry() );
185 }
186 
187 void QgsColorDialog::setColor( const QColor &color )
188 {
189  if ( !color.isValid() )
190  {
191  return;
192  }
193 
194  QColor fixedColor = QColor( color );
195  if ( !mAllowOpacity )
196  {
197  //alpha disallowed, so don't permit transparent colors
198  fixedColor.setAlpha( 255 );
199  }
200 
201  mColorWidget->setColor( fixedColor );
202  emit currentColorChanged( fixedColor );
203 }
204 
205 void QgsColorDialog::closeEvent( QCloseEvent *e )
206 {
207  saveSettings();
208  QDialog::closeEvent( e );
209 }
210 
211 void QgsColorDialog::showHelp()
212 {
213  QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#color-selector" ) );
214 }
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.