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