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