QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 <QMouseEvent>
31 #include <QInputDialog>
32 
33 QgsColorDialog::QgsColorDialog( QWidget *parent, Qt::WindowFlags fl, const QColor &color )
34  : QDialog( parent, fl )
35  , mPreviousColor( color )
36 {
37  setupUi( this );
39 
40  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsColorDialog::mButtonBox_accepted );
41  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsColorDialog::mButtonBox_rejected );
42  connect( mButtonBox, &QDialogButtonBox::clicked, this, &QgsColorDialog::mButtonBox_clicked );
43 
44  connect( mColorWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
45 
46  if ( mPreviousColor.isValid() )
47  {
48  QPushButton *resetButton = new QPushButton( tr( "Reset" ) );
49  mButtonBox->addButton( resetButton, QDialogButtonBox::ResetRole );
50  }
51 
52  if ( color.isValid() )
53  {
54  mColorWidget->setColor( color );
55  mColorWidget->setPreviousColor( color );
56  }
57 
58  mColorWidget->setAllowOpacity( true );
59 
61  connect( this, &QDialog::rejected, this, &QgsColorDialog::discardColor );
62  connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsColorDialog::showHelp );
63 }
64 
65 QColor QgsColorDialog::color() const
66 {
67  return mColorWidget->color();
68 }
69 
70 void QgsColorDialog::setTitle( const QString &title )
71 {
72  setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title );
73 }
74 
75 void QgsColorDialog::setAllowOpacity( const bool allowOpacity )
76 {
77  mAllowOpacity = allowOpacity;
78  mColorWidget->setAllowOpacity( allowOpacity );
79 }
80 
81 QColor QgsColorDialog::getColor( const QColor &initialColor, QWidget *parent, const QString &title, const bool allowOpacity )
82 {
83  const QString dialogTitle = title.isEmpty() ? tr( "Select Color" ) : title;
84 
85  const QgsSettings settings;
86  //using native color dialogs?
87  const bool useNative = settings.value( QStringLiteral( "qgis/native_color_dialogs" ), false ).toBool();
88  if ( useNative )
89  {
90  return QColorDialog::getColor( initialColor, parent, dialogTitle, allowOpacity ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
91  }
92  else
93  {
94  QgsColorDialog *dialog = new QgsColorDialog( parent, Qt::WindowFlags(), initialColor );
95  dialog->setWindowTitle( dialogTitle );
96  dialog->setAllowOpacity( allowOpacity );
97 
98  QColor result;
99  if ( dialog->exec() )
100  {
101  result = dialog->color();
102  }
103 
104  if ( !parent )
105  {
106  delete dialog;
107  }
108  return result;
109  }
110 }
111 
112 void QgsColorDialog::mButtonBox_accepted()
113 {
114  accept();
115 }
116 
117 void QgsColorDialog::mButtonBox_rejected()
118 {
119  reject();
120 }
121 
122 void QgsColorDialog::mButtonBox_clicked( QAbstractButton *button )
123 {
124  if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ResetRole && mPreviousColor.isValid() )
125  {
126  setColor( mPreviousColor );
127  }
128 }
129 
130 void QgsColorDialog::discardColor()
131 {
132  mColorWidget->setDiscarded( true );
133 }
134 
135 void QgsColorDialog::setColor( const QColor &color )
136 {
137  if ( !color.isValid() )
138  {
139  return;
140  }
141 
142  QColor fixedColor = QColor( color );
143  if ( !mAllowOpacity )
144  {
145  //alpha disallowed, so don't permit transparent colors
146  fixedColor.setAlpha( 255 );
147  }
148 
149  mColorWidget->setColor( fixedColor );
150  emit currentColorChanged( fixedColor );
151 }
152 
153 void QgsColorDialog::closeEvent( QCloseEvent *e )
154 {
155  QDialog::closeEvent( e );
156 }
157 
158 void QgsColorDialog::showHelp()
159 {
160  QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#color-selector" ) );
161 }
A custom QGIS dialog for selecting a color.
QColor color() const
Returns the current color for the dialog.
void setColor(const QColor &color)
Sets the current color for the dialog.
void currentColorChanged(const QColor &color)
Emitted when the dialog's color changes.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color dialog.
void closeEvent(QCloseEvent *e) override
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), bool allowOpacity=false)
Returns a color selection from a color dialog.
QgsColorDialog(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags, const QColor &color=QColor())
Create a new color picker dialog.
void setTitle(const QString &title)
Sets the title for the color dialog.
void currentColorChanged(const QColor &color)
Emitted when the dialog'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:174
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.