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