Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtgradientdialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6#include "ui_qtgradientdialog.h"
7
8#include <QtWidgets/qpushbutton.h>
9
11
13{
14 Q_OBJECT
15 QtGradientDialog *q_ptr = nullptr;
17public:
19
21};
22
23void QtGradientDialogPrivate::slotAboutToShowDetails(bool details, int extensionWidthHint)
24{
25 if (details) {
26 q_ptr->resize(q_ptr->size() + QSize(extensionWidthHint, 0));
27 } else {
28 q_ptr->setMinimumSize(1, 1);
29 q_ptr->resize(q_ptr->size() - QSize(extensionWidthHint, 0));
30 q_ptr->setMinimumSize(0, 0);
31 }
32}
33
34/*!
35 \class QtGradientDialog
36
37 \brief The QtGradientDialog class provides a dialog for specifying gradients.
38
39 The gradient dialog's function is to allow users to edit gradients.
40 For example, you might use this in a drawing program to allow the user to set the brush gradient.
41
42 \table
43 \row
44 \li \inlineimage qtgradientdialog.png
45 \li \inlineimage qtgradientdialogextension.png
46 \header
47 \li Details extension hidden
48 \li Details extension visible
49 \endtable
50
51 Starting from the top of the dialog there are several buttons:
52
53 \image qtgradientdialogtopbuttons.png
54
55 The first three buttons allow for changing a type of the gradient (QGradient::Type), while the second three allow for
56 changing spread of the gradient (QGradient::Spread). The last button shows or hides the details extension of the dialog.
57 Conceptually the default view with hidden details provides the full functional control over gradient editing.
58 The additional extension with details allows to set gradient's parameters more precisely. The visibility
59 of extension can be controlled by detailsVisible property. Moreover, if you don't want the user to
60 switch on or off the visibility of extension you can set the detailsButtonVisible property to false.
61
62 Below top buttons there is an area where edited gradient is interactively previewed.
63 In addition the user can edit gradient type's specific parameters directly in this area by dragging
64 appropriate handles.
65
66 \table
67 \row
68 \li \inlineimage qtgradientdialoglineareditor.png
69 \li \inlineimage qtgradientdialogradialeditor.png
70 \li \inlineimage qtgradientdialogconicaleditor.png
71 \header
72 \li Editing linear type
73 \li Editing radial type
74 \li Editing conical type
75 \row
76 \li The user can change the start and final point positions by dragging the circular handles.
77 \li The user can change the center and focal point positions by dragging the circular handles
78 and can change the gradient's radius by dragging horizontal or vertical line.
79 \li The user can change the center point by dragging the circular handle
80 and can change the gradient's angle by dragging the big wheel.
81 \endtable
82
83 In the middle of the dialog there is an area where the user can edit gradient stops.
84
85 \table
86 \row
87 \li \inlineimage qtgradientdialogstops.png
88 \li \inlineimage qtgradientdialogstopszoomed.png
89 \endtable
90
91 The top part of this area contains stop handles, and bottom part shows the preview of gradient stops path.
92 In order to create a new gradient stop double click inside the view over the desired position.
93 If you double click on existing stop handle in the top part of the view, clicked handle will be duplicated
94 (duplicate will contain the same color).
95 The stop can be activated by clicking on its handle. You can activate previous or next stop by pressing
96 left or right key respectively. To jump to the first or last stop press home or end key respectively.
97 The gradient stops editor supports multiselection.
98 Clicking a handle holding the shift modifier key down will select a range of stops between
99 the active stop and clicked one. Clicking a handle holding control modifier key down will remove from or
100 add to selection the clicked stop depending if it was or wasn't already selected respectively.
101 Multiselection can also be created using rubberband (by pressing the left mouse button outside
102 of any handle and dragging).
103 Sometimes it's hard to select a stop because its handle can be partially covered by other handle.
104 In that case the user can zoom in the view by spinning mouse wheel.
105 The selected stop handles can be moved by drag & drop. In order to remove selected stops press delete key.
106 For convenience context menu is provided with the following actions:
107
108 \list
109 \li New Stop - creates a new gradient stop
110 \li Delete - removes the active and all selected stops
111 \li Flip All - mirrors all stops
112 \li Select All - selects all stops
113 \li Zoom In - zooms in
114 \li Zoom Out - zooms out
115 \li Zoom All - goes back to original 100% zoom
116 \endlist
117
118 The bottom part of the QtGradientDialog contains a set of widgets allowing to control the color of
119 the active and selected stops.
120
121 \table
122 \row
123 \li \inlineimage qtgradientdialogcolorhsv.png
124 \li \inlineimage qtgradientdialogcolorrgb.png
125 \endtable
126
127
128 The color button shows the color of the active gradient stop. It also allows for choosing
129 a color from standard color dialog and applying it to the
130 active stop and all selected stops. It's also possible to drag a color directly from the color button
131 and to drop it in gradient stops editor at desired position (it will create new stop with dragged color)
132 or at desired stop handle (it will change the color of that handle).
133
134 To the right of color button there is a set of 2 radio buttons which allows to switch between
135 HVS and RGB color spec.
136
137 Finally there are 4 color sliders working either in HSVA (hue saturation value alpha) or
138 RGBA (red green blue alpha) mode, depending on which radio button is chosen. The radio buttons
139 can be controlled programatically by spec() and setSpec() methods. The sliders show the
140 color of the active stop. By double clicking inside color slider you can set directly the desired color.
141 Changes of slider's are applied to stop selection in the way that the color
142 component being changed is applied to stops in selection only, while other components
143 remain unchanged in selected stops (e.g. when the user is changing the saturation,
144 new saturation is applied to selected stops preventing original hue, value and alpha in multiselection).
145
146 The convenient static functions getGradient() provide modal gradient dialogs, e.g.:
147
148 \snippet doc/src/snippets/code/tools_shared_qtgradienteditor_qtgradientdialog.cpp 0
149
150 In order to have more control over the properties of QtGradientDialog use
151 standard QDialog::exec() method:
152
153 \snippet doc/src/snippets/code/tools_shared_qtgradienteditor_qtgradientdialog.cpp 1
154
155 \sa {Gradient View Example}
156*/
157
158/*!
159 Constructs a gradient dialog with \a parent as parent widget.
160*/
161
162QtGradientDialog::QtGradientDialog(QWidget *parent)
163 : QDialog(parent), d_ptr(new QtGradientDialogPrivate())
164{
165// setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
166 d_ptr->q_ptr = this;
167 d_ptr->m_ui.setupUi(this);
168 QPushButton *button = d_ptr->m_ui.buttonBox->button(QDialogButtonBox::Ok);
169 if (button)
170 button->setAutoDefault(false);
171 button = d_ptr->m_ui.buttonBox->button(QDialogButtonBox::Cancel);
172 if (button)
173 button->setAutoDefault(false);
174 connect(d_ptr->m_ui.gradientEditor, &QtGradientEditor::aboutToShowDetails,
175 d_ptr.data(), &QtGradientDialogPrivate::slotAboutToShowDetails);
176}
177
178/*!
179 Destroys the gradient dialog
180*/
181
182QtGradientDialog::~QtGradientDialog()
183{
184}
185
186/*!
187 \property QtGradientDialog::gradient
188 \brief the gradient of the dialog
189*/
190void QtGradientDialog::setGradient(const QGradient &gradient)
191{
192 d_ptr->m_ui.gradientEditor->setGradient(gradient);
193}
194
195QGradient QtGradientDialog::gradient() const
196{
197 return d_ptr->m_ui.gradientEditor->gradient();
198}
199
200/*!
201 \property QtGradientDialog::backgroundCheckered
202 \brief whether the background of widgets able to show the colors with alpha channel is checkered.
203
204 \table
205 \row
206 \li \inlineimage qtgradientdialogbackgroundcheckered.png
207 \li \inlineimage qtgradientdialogbackgroundtransparent.png
208 \row
209 \li \snippet doc/src/snippets/code/tools_shared_qtgradienteditor_qtgradientdialog.cpp 2
210 \li \snippet doc/src/snippets/code/tools_shared_qtgradienteditor_qtgradientdialog.cpp 3
211 \endtable
212
213 When this property is set to true (the default) widgets inside gradient dialog like color button,
214 color sliders, gradient stops editor and gradient editor will show checkered background
215 in case of transparent colors. Otherwise the background of these widgets is transparent.
216*/
217
218bool QtGradientDialog::isBackgroundCheckered() const
219{
220 return d_ptr->m_ui.gradientEditor->isBackgroundCheckered();
221}
222
223void QtGradientDialog::setBackgroundCheckered(bool checkered)
224{
225 d_ptr->m_ui.gradientEditor->setBackgroundCheckered(checkered);
226}
227
228/*!
229 \property QtGradientDialog::detailsVisible
230 \brief whether details extension is visible.
231
232 When this property is set to true the details extension is visible. By default
233 this property is set to false and the details extension is hidden.
234
235 \sa detailsButtonVisible
236*/
237bool QtGradientDialog::detailsVisible() const
238{
239 return d_ptr->m_ui.gradientEditor->detailsVisible();
240}
241
242void QtGradientDialog::setDetailsVisible(bool visible)
243{
244 d_ptr->m_ui.gradientEditor->setDetailsVisible(visible);
245}
246
247/*!
248 \property QtGradientDialog::detailsButtonVisible
249 \brief whether the details button allowing for showing and hiding details extension is visible.
250
251 When this property is set to true (the default) the details button is visible and the user
252 can show and hide details extension interactively. Otherwise the button is hidden and the details
253 extension is always visible or hidded depending on the value of detailsVisible property.
254
255 \sa detailsVisible
256*/
257bool QtGradientDialog::isDetailsButtonVisible() const
258{
259 return d_ptr->m_ui.gradientEditor->isDetailsButtonVisible();
260}
261
262void QtGradientDialog::setDetailsButtonVisible(bool visible)
263{
264 d_ptr->m_ui.gradientEditor->setDetailsButtonVisible(visible);
265}
266
267/*!
268 Returns the current QColor::Spec used for the color sliders in the dialog.
269*/
270QColor::Spec QtGradientDialog::spec() const
271{
272 return d_ptr->m_ui.gradientEditor->spec();
273}
274
275/*!
276 Sets the current QColor::Spec to \a spec used for the color sliders in the dialog.
277*/
278void QtGradientDialog::setSpec(QColor::Spec spec)
279{
280 d_ptr->m_ui.gradientEditor->setSpec(spec);
281}
282
283/*!
284 Executes a modal gradient dialog, lets the user to specify a gradient, and returns that gradient.
285
286 If the user clicks \gui OK, the gradient specified by the user is returned. If the user clicks \gui Cancel, the \a initial gradient is returned.
287
288 The dialog is constructed with the given \a parent. \a caption is shown as the window title of the dialog and
289 \a initial is the initial gradient shown in the dialog. If the \a ok parameter is not-null,
290 the value it refers to is set to true if the user clicks \gui OK, and set to false if the user clicks \gui Cancel.
291*/
292QGradient QtGradientDialog::getGradient(bool *ok, const QGradient &initial, QWidget *parent, const QString &caption)
293{
294 QtGradientDialog dlg(parent);
295 if (!caption.isEmpty())
296 dlg.setWindowTitle(caption);
297 dlg.setGradient(initial);
298 const int res = dlg.exec();
299 if (ok) {
300 *ok = (res == QDialog::Accepted) ? true : false;
301 }
302 if (res == QDialog::Accepted)
303 return dlg.gradient();
304 return initial;
305}
306
307/*!
308 This method calls getGradient(ok, QLinearGradient(), parent, caption).
309*/
310QGradient QtGradientDialog::getGradient(bool *ok, QWidget *parent, const QString &caption)
311{
312 return getGradient(ok, QLinearGradient(), parent, caption);
313}
314
315QT_END_NAMESPACE
316
317#include "qtgradientdialog.moc"
Ui::QtGradientDialog m_ui
Combined button and popup list for selecting options.