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