1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QtWidgets>
52#if defined(QT_PRINTSUPPORT_LIB)
53#include <QtPrintSupport/qtprintsupportglobal.h>
54#if QT_CONFIG(printdialog)
55#include <QPrinter>
56#include <QPrintDialog>
57#endif
58#endif
59
60#include "licensewizard.h"
61
62QString emailRegExp = QStringLiteral(".+@.+");
63
64//! [0] //! [1] //! [2]
65LicenseWizard::LicenseWizard(QWidget *parent)
66 : QWizard(parent)
67{
68//! [0]
69 setPage(Page_Intro, new IntroPage);
70 setPage(Page_Evaluate, new EvaluatePage);
71 setPage(Page_Register, new RegisterPage);
72 setPage(Page_Details, new DetailsPage);
73 setPage(Page_Conclusion, new ConclusionPage);
74//! [1]
75
76 setStartId(Page_Intro);
77//! [2]
78
79//! [3]
80#ifndef Q_OS_MAC
81//! [3] //! [4]
82 setWizardStyle(ModernStyle);
83#endif
84//! [4] //! [5]
85 setOption(HaveHelpButton, true);
86//! [5] //! [6]
87 setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo.png"));
88
89//! [7]
90 connect(this, &QWizard::helpRequested, this, &LicenseWizard::showHelp);
91//! [7]
92
93 setWindowTitle(tr("License Wizard"));
94//! [8]
95}
96//! [6] //! [8]
97
98//! [9] //! [10]
99void LicenseWizard::showHelp()
100//! [9] //! [11]
101{
102 static QString lastHelpMessage;
103
104 QString message;
105
106 switch (currentId()) {
107 case Page_Intro:
108 message = tr("The decision you make here will affect which page you "
109 "get to see next.");
110 break;
111//! [10] //! [11]
112 case Page_Evaluate:
113 message = tr("Make sure to provide a valid email address, such as "
114 "toni.buddenbrook@example.de.");
115 break;
116 case Page_Register:
117 message = tr("If you don't provide an upgrade key, you will be "
118 "asked to fill in your details.");
119 break;
120 case Page_Details:
121 message = tr("Make sure to provide a valid email address, such as "
122 "thomas.gradgrind@example.co.uk.");
123 break;
124 case Page_Conclusion:
125 message = tr("You must accept the terms and conditions of the "
126 "license to proceed.");
127 break;
128//! [12] //! [13]
129 default:
130 message = tr("This help is likely not to be of any help.");
131 }
132//! [12]
133
134 if (lastHelpMessage == message)
135 message = tr("Sorry, I already gave what help I could. "
136 "Maybe you should try asking a human?");
137
138//! [14]
139 QMessageBox::information(this, tr("License Wizard Help"), message);
140//! [14]
141
142 lastHelpMessage = message;
143//! [15]
144}
145//! [13] //! [15]
146
147//! [16]
148IntroPage::IntroPage(QWidget *parent)
149 : QWizardPage(parent)
150{
151 setTitle(tr("Introduction"));
152 setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark.png"));
153
154 topLabel = new QLabel(tr("This wizard will help you register your copy of "
155 "<i>Super Product One</i>&trade; or start "
156 "evaluating the product."));
157 topLabel->setWordWrap(true);
158
159 registerRadioButton = new QRadioButton(tr("&Register your copy"));
160 evaluateRadioButton = new QRadioButton(tr("&Evaluate the product for 30 "
161 "days"));
162 registerRadioButton->setChecked(true);
163
164 QVBoxLayout *layout = new QVBoxLayout;
165 layout->addWidget(topLabel);
166 layout->addWidget(registerRadioButton);
167 layout->addWidget(evaluateRadioButton);
168 setLayout(layout);
169}
170//! [16] //! [17]
171
172//! [18]
173int IntroPage::nextId() const
174//! [17] //! [19]
175{
176 if (evaluateRadioButton->isChecked()) {
177 return LicenseWizard::Page_Evaluate;
178 } else {
179 return LicenseWizard::Page_Register;
180 }
181}
182//! [18] //! [19]
183
184//! [20]
185EvaluatePage::EvaluatePage(QWidget *parent)
186 : QWizardPage(parent)
187{
188 setTitle(tr("Evaluate <i>Super Product One</i>&trade;"));
189 setSubTitle(tr("Please fill both fields. Make sure to provide a valid "
190 "email address (e.g., john.smith@example.com)."));
191
192 nameLabel = new QLabel(tr("N&ame:"));
193 nameLineEdit = new QLineEdit;
194//! [20]
195 nameLabel->setBuddy(nameLineEdit);
196
197 emailLabel = new QLabel(tr("&Email address:"));
198 emailLineEdit = new QLineEdit;
199 emailLineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression(emailRegExp), this));
200 emailLabel->setBuddy(emailLineEdit);
201
202//! [21]
203 registerField("evaluate.name*", nameLineEdit);
204 registerField("evaluate.email*", emailLineEdit);
205//! [21]
206
207 QGridLayout *layout = new QGridLayout;
208 layout->addWidget(nameLabel, 0, 0);
209 layout->addWidget(nameLineEdit, 0, 1);
210 layout->addWidget(emailLabel, 1, 0);
211 layout->addWidget(emailLineEdit, 1, 1);
212 setLayout(layout);
213//! [22]
214}
215//! [22]
216
217//! [23]
218int EvaluatePage::nextId() const
219{
220 return LicenseWizard::Page_Conclusion;
221}
222//! [23]
223
224RegisterPage::RegisterPage(QWidget *parent)
225 : QWizardPage(parent)
226{
227 setTitle(tr("Register Your Copy of <i>Super Product One</i>&trade;"));
228 setSubTitle(tr("If you have an upgrade key, please fill in "
229 "the appropriate field."));
230
231 nameLabel = new QLabel(tr("N&ame:"));
232 nameLineEdit = new QLineEdit;
233 nameLabel->setBuddy(nameLineEdit);
234
235 upgradeKeyLabel = new QLabel(tr("&Upgrade key:"));
236 upgradeKeyLineEdit = new QLineEdit;
237 upgradeKeyLabel->setBuddy(upgradeKeyLineEdit);
238
239 registerField("register.name*", nameLineEdit);
240 registerField("register.upgradeKey", upgradeKeyLineEdit);
241
242 QGridLayout *layout = new QGridLayout;
243 layout->addWidget(nameLabel, 0, 0);
244 layout->addWidget(nameLineEdit, 0, 1);
245 layout->addWidget(upgradeKeyLabel, 1, 0);
246 layout->addWidget(upgradeKeyLineEdit, 1, 1);
247 setLayout(layout);
248}
249
250//! [24]
251int RegisterPage::nextId() const
252{
253 if (upgradeKeyLineEdit->text().isEmpty()) {
254 return LicenseWizard::Page_Details;
255 } else {
256 return LicenseWizard::Page_Conclusion;
257 }
258}
259//! [24]
260
261DetailsPage::DetailsPage(QWidget *parent)
262 : QWizardPage(parent)
263{
264 setTitle(tr("Fill In Your Details"));
265 setSubTitle(tr("Please fill all three fields. Make sure to provide a valid "
266 "email address (e.g., tanaka.aya@example.co.jp)."));
267
268 companyLabel = new QLabel(tr("&Company name:"));
269 companyLineEdit = new QLineEdit;
270 companyLabel->setBuddy(companyLineEdit);
271
272 emailLabel = new QLabel(tr("&Email address:"));
273 emailLineEdit = new QLineEdit;
274 emailLineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression(emailRegExp), this));
275 emailLabel->setBuddy(emailLineEdit);
276
277 postalLabel = new QLabel(tr("&Postal address:"));
278 postalLineEdit = new QLineEdit;
279 postalLabel->setBuddy(postalLineEdit);
280
281 registerField("details.company*", companyLineEdit);
282 registerField("details.email*", emailLineEdit);
283 registerField("details.postal*", postalLineEdit);
284
285 QGridLayout *layout = new QGridLayout;
286 layout->addWidget(companyLabel, 0, 0);
287 layout->addWidget(companyLineEdit, 0, 1);
288 layout->addWidget(emailLabel, 1, 0);
289 layout->addWidget(emailLineEdit, 1, 1);
290 layout->addWidget(postalLabel, 2, 0);
291 layout->addWidget(postalLineEdit, 2, 1);
292 setLayout(layout);
293}
294
295//! [25]
296int DetailsPage::nextId() const
297{
298 return LicenseWizard::Page_Conclusion;
299}
300//! [25]
301
302ConclusionPage::ConclusionPage(QWidget *parent)
303 : QWizardPage(parent)
304{
305 setTitle(tr("Complete Your Registration"));
306 setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark.png"));
307
308 bottomLabel = new QLabel;
309 bottomLabel->setWordWrap(true);
310
311 agreeCheckBox = new QCheckBox(tr("I agree to the terms of the license"));
312
313 registerField("conclusion.agree*", agreeCheckBox);
314
315 QVBoxLayout *layout = new QVBoxLayout;
316 layout->addWidget(bottomLabel);
317 layout->addWidget(agreeCheckBox);
318 setLayout(layout);
319}
320
321//! [26]
322int ConclusionPage::nextId() const
323{
324 return -1;
325}
326//! [26]
327
328//! [27]
329void ConclusionPage::initializePage()
330{
331 QString licenseText;
332
333 if (wizard()->hasVisitedPage(LicenseWizard::Page_Evaluate)) {
334 licenseText = tr("<u>Evaluation License Agreement:</u> "
335 "You can use this software for 30 days and make one "
336 "backup, but you are not allowed to distribute it.");
337 } else if (wizard()->hasVisitedPage(LicenseWizard::Page_Details)) {
338 licenseText = tr("<u>First-Time License Agreement:</u> "
339 "You can use this software subject to the license "
340 "you will receive by email.");
341 } else {
342 licenseText = tr("<u>Upgrade License Agreement:</u> "
343 "This software is licensed under the terms of your "
344 "current license.");
345 }
346 bottomLabel->setText(licenseText);
347}
348//! [27]
349
350//! [28]
351void ConclusionPage::setVisible(bool visible)
352{
353 QWizardPage::setVisible(visible);
354
355 if (visible) {
356//! [29]
357 wizard()->setButtonText(QWizard::CustomButton1, tr("&Print"));
358 wizard()->setOption(QWizard::HaveCustomButton1, true);
359 connect(wizard(), &QWizard::customButtonClicked,
360 this, &ConclusionPage::printButtonClicked);
361//! [29]
362 } else {
363 wizard()->setOption(QWizard::HaveCustomButton1, false);
364 disconnect(wizard(), &QWizard::customButtonClicked,
365 this, &ConclusionPage::printButtonClicked);
366 }
367}
368//! [28]
369
370void ConclusionPage::printButtonClicked()
371{
372#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
373 QPrinter printer;
374 QPrintDialog dialog(&printer, this);
375 if (dialog.exec())
376 QMessageBox::warning(this, tr("Print License"),
377 tr("As an environmentally friendly measure, the "
378 "license text will not actually be printed."));
379#endif
380}
381