QGIS API Documentation  2.99.0-Master (9f5e33a)
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  , mAllowOpacity( true )
38 {
39  setupUi( this );
40 
41  QgsSettings settings;
42  restoreGeometry( settings.value( QStringLiteral( "Windows/ColorDialog/geometry" ) ).toByteArray() );
43 
44  if ( mPreviousColor.isValid() )
45  {
46  QPushButton *resetButton = new QPushButton( tr( "Reset" ) );
47  mButtonBox->addButton( resetButton, QDialogButtonBox::ResetRole );
48  }
49 
50  if ( color.isValid() )
51  {
52  mColorWidget->setColor( color );
53  mColorWidget->setPreviousColor( color );
54  }
55 
56  mColorWidget->setAllowOpacity( true );
57 
59  connect( this, &QDialog::rejected, this, &QgsColorDialog::discardColor );
60 }
61 
62 QColor QgsColorDialog::color() const
63 {
64  return mColorWidget->color();
65 }
66 
67 void QgsColorDialog::setTitle( const QString &title )
68 {
69  setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title );
70 }
71 
72 void QgsColorDialog::setAllowOpacity( const bool allowOpacity )
73 {
74  mAllowOpacity = allowOpacity;
75  mColorWidget->setAllowOpacity( allowOpacity );
76 }
77 
78 QColor QgsColorDialog::getLiveColor( const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent, const QString &title, const bool allowOpacity )
79 {
80  QColor returnColor( initialColor );
81 
82  QgsSettings settings;
83 
84  //using native color dialogs?
85  bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
86  if ( useNative )
87  {
88  QColorDialog *liveDialog = new QColorDialog( initialColor, parent );
89  liveDialog->setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title );
90  liveDialog->setOptions( allowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
91 
92  connect( liveDialog, SIGNAL( currentColorChanged( const QColor & ) ),
93  updateObject, updateSlot );
94 
95  if ( liveDialog->exec() )
96  {
97  returnColor = liveDialog->currentColor();
98  }
99  delete liveDialog;
100  }
101  else
102  {
103  QgsColorDialog *liveDialog = new QgsColorDialog( parent, 0, initialColor );
104  liveDialog->setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title );
105  if ( !allowOpacity )
106  {
107  liveDialog->setAllowOpacity( false );
108  }
109 
110  connect( liveDialog, SIGNAL( currentColorChanged( const QColor & ) ),
111  updateObject, updateSlot );
112 
113  if ( liveDialog->exec() )
114  {
115  returnColor = liveDialog->color();
116  }
117  delete liveDialog;
118  }
119 
120  return returnColor;
121 }
122 
123 QColor QgsColorDialog::getColor( const QColor &initialColor, QWidget *parent, const QString &title, const bool allowOpacity )
124 {
125  QString dialogTitle = title.isEmpty() ? tr( "Select Color" ) : title;
126 
127  QgsSettings settings;
128  //using native color dialogs?
129  bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
130  if ( useNative )
131  {
132  return QColorDialog::getColor( initialColor, parent, dialogTitle, allowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
133  }
134  else
135  {
136  QgsColorDialog *dialog = new QgsColorDialog( parent, 0, initialColor );
137  dialog->setWindowTitle( dialogTitle );
138  dialog->setAllowOpacity( allowOpacity );
139 
140  QColor result;
141  if ( dialog->exec() )
142  {
143  result = dialog->color();
144  }
145 
146  if ( !parent )
147  {
148  delete dialog;
149  }
150  return result;
151  }
152 }
153 
154 void QgsColorDialog::on_mButtonBox_accepted()
155 {
156  saveSettings();
157  accept();
158 }
159 
160 void QgsColorDialog::on_mButtonBox_rejected()
161 {
162  saveSettings();
163  reject();
164 }
165 
166 void QgsColorDialog::on_mButtonBox_clicked( QAbstractButton *button )
167 {
168  if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ResetRole && mPreviousColor.isValid() )
169  {
170  setColor( mPreviousColor );
171  }
172 }
173 
174 void QgsColorDialog::discardColor()
175 {
176  mColorWidget->setDiscarded( true );
177 }
178 
179 void QgsColorDialog::saveSettings()
180 {
181  QgsSettings settings;
182  settings.setValue( QStringLiteral( "Windows/ColorDialog/geometry" ), saveGeometry() );
183 }
184 
185 void QgsColorDialog::setColor( const QColor &color )
186 {
187  if ( !color.isValid() )
188  {
189  return;
190  }
191 
192  QColor fixedColor = QColor( color );
193  if ( !mAllowOpacity )
194  {
195  //alpha disallowed, so don't permit transparent colors
196  fixedColor.setAlpha( 255 );
197  }
198 
199  mColorWidget->setColor( fixedColor );
200  emit currentColorChanged( fixedColor );
201 }
202 
203 void QgsColorDialog::closeEvent( QCloseEvent *e )
204 {
205  saveSettings();
206  QDialog::closeEvent( e );
207 }
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:54
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.
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.