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