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