QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 #include "qgsgui.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 {
38  setupUi( this );
40 
41  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsColorDialog::mButtonBox_accepted );
42  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsColorDialog::mButtonBox_rejected );
43  connect( mButtonBox, &QDialogButtonBox::clicked, this, &QgsColorDialog::mButtonBox_clicked );
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::getColor( const QColor &initialColor, QWidget *parent, const QString &title, const bool allowOpacity )
81 {
82  QString dialogTitle = title.isEmpty() ? tr( "Select Color" ) : title;
83 
84  QgsSettings settings;
85  //using native color dialogs?
86  bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
87  if ( useNative )
88  {
89  return QColorDialog::getColor( initialColor, parent, dialogTitle, allowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
90  }
91  else
92  {
93  QgsColorDialog *dialog = new QgsColorDialog( parent, nullptr, initialColor );
94  dialog->setWindowTitle( dialogTitle );
95  dialog->setAllowOpacity( allowOpacity );
96 
97  QColor result;
98  if ( dialog->exec() )
99  {
100  result = dialog->color();
101  }
102 
103  if ( !parent )
104  {
105  delete dialog;
106  }
107  return result;
108  }
109 }
110 
111 void QgsColorDialog::mButtonBox_accepted()
112 {
113  accept();
114 }
115 
116 void QgsColorDialog::mButtonBox_rejected()
117 {
118  reject();
119 }
120 
121 void QgsColorDialog::mButtonBox_clicked( QAbstractButton *button )
122 {
123  if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ResetRole && mPreviousColor.isValid() )
124  {
125  setColor( mPreviousColor );
126  }
127 }
128 
129 void QgsColorDialog::discardColor()
130 {
131  mColorWidget->setDiscarded( true );
132 }
133 
134 void QgsColorDialog::setColor( const QColor &color )
135 {
136  if ( !color.isValid() )
137  {
138  return;
139  }
140 
141  QColor fixedColor = QColor( color );
142  if ( !mAllowOpacity )
143  {
144  //alpha disallowed, so don't permit transparent colors
145  fixedColor.setAlpha( 255 );
146  }
147 
148  mColorWidget->setColor( fixedColor );
149  emit currentColorChanged( fixedColor );
150 }
151 
152 void QgsColorDialog::closeEvent( QCloseEvent *e )
153 {
154  QDialog::closeEvent( e );
155 }
156 
157 void QgsColorDialog::showHelp()
158 {
159  QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#color-selector" ) );
160 }
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), bool allowOpacity=false)
Returns a color selection from a color dialog.
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:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity 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
QColor color() const
Returns the current color for the dialog.
A custom QGIS dialog for selecting a color.
void currentColorChanged(const QColor &color)
Emitted when the dialog&#39;s color changes.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:104
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
QgsColorDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags, const QColor &color=QColor())
Create a new color picker dialog.