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