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