QGIS API Documentation  3.13.0-Master (740be229cb)
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  connect( mColorWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
46 
47  if ( mPreviousColor.isValid() )
48  {
49  QPushButton *resetButton = new QPushButton( tr( "Reset" ) );
50  mButtonBox->addButton( resetButton, QDialogButtonBox::ResetRole );
51  }
52 
53  if ( color.isValid() )
54  {
55  mColorWidget->setColor( color );
56  mColorWidget->setPreviousColor( color );
57  }
58 
59  mColorWidget->setAllowOpacity( true );
60 
62  connect( this, &QDialog::rejected, this, &QgsColorDialog::discardColor );
63  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsColorDialog::showHelp );
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::setAllowOpacity( const bool allowOpacity )
77 {
78  mAllowOpacity = allowOpacity;
79  mColorWidget->setAllowOpacity( allowOpacity );
80 }
81 
82 QColor QgsColorDialog::getColor( const QColor &initialColor, QWidget *parent, const QString &title, const bool allowOpacity )
83 {
84  QString dialogTitle = title.isEmpty() ? tr( "Select Color" ) : title;
85 
86  QgsSettings settings;
87  //using native color dialogs?
88  bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
89  if ( useNative )
90  {
91  return QColorDialog::getColor( initialColor, parent, dialogTitle, allowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
92  }
93  else
94  {
95  QgsColorDialog *dialog = new QgsColorDialog( parent, nullptr, initialColor );
96  dialog->setWindowTitle( dialogTitle );
97  dialog->setAllowOpacity( allowOpacity );
98 
99  QColor result;
100  if ( dialog->exec() )
101  {
102  result = dialog->color();
103  }
104 
105  if ( !parent )
106  {
107  delete dialog;
108  }
109  return result;
110  }
111 }
112 
113 void QgsColorDialog::mButtonBox_accepted()
114 {
115  accept();
116 }
117 
118 void QgsColorDialog::mButtonBox_rejected()
119 {
120  reject();
121 }
122 
123 void QgsColorDialog::mButtonBox_clicked( QAbstractButton *button )
124 {
125  if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ResetRole && mPreviousColor.isValid() )
126  {
127  setColor( mPreviousColor );
128  }
129 }
130 
131 void QgsColorDialog::discardColor()
132 {
133  mColorWidget->setDiscarded( true );
134 }
135 
136 void QgsColorDialog::setColor( const QColor &color )
137 {
138  if ( !color.isValid() )
139  {
140  return;
141  }
142 
143  QColor fixedColor = QColor( color );
144  if ( !mAllowOpacity )
145  {
146  //alpha disallowed, so don't permit transparent colors
147  fixedColor.setAlpha( 255 );
148  }
149 
150  mColorWidget->setColor( fixedColor );
151  emit currentColorChanged( fixedColor );
152 }
153 
154 void QgsColorDialog::closeEvent( QCloseEvent *e )
155 {
156  QDialog::closeEvent( e );
157 }
158 
159 void QgsColorDialog::showHelp()
160 {
161  QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#color-selector" ) );
162 }
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:61
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 panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
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:133
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.