aboutsummaryrefslogtreecommitdiffstats
path: root/examples/widgets/dialogs/licensewizard/licensewizard.py
blob: 28667d17309c77411fae7d39454ff82d0a35d0ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import sys
from enum import IntEnum
from pathlib import Path

from PySide6.QtCore import QRegularExpression
from PySide6.QtGui import QPixmap, QRegularExpressionValidator
from PySide6.QtPrintSupport import QPrintDialog, QPrinter
from PySide6.QtWidgets import (QCheckBox, QGridLayout, QLabel, QLineEdit,
                               QMessageBox, QRadioButton, QVBoxLayout, QWizard,
                               QWizardPage)

EMAIL_REGEXP = ".+@.+"


class Pages(IntEnum):
    Page_Intro = 0
    Page_Evaluate = 1
    Page_Register = 2
    Page_Details = 3
    Page_Conclusion = 4


class IntroPage(QWizardPage):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.setTitle("Introduction")

        path = Path(__file__).resolve().parent

        self.setPixmap(QWizard.WatermarkPixmap, QPixmap(path / "images" / "watermark.png"))
        self.top_label = QLabel(
            "This wizard will help you register your copy of "
            "<i>Super Product One</i>&trade; or start "
            "evaluating the product"
        )
        self.top_label.setWordWrap(True)

        self.register_radio_button = QRadioButton("&Register your copy")
        self.register_radio_button.setChecked(True)

        self.evaluate_radio_button = QRadioButton("&Evaluate the product for 30 days")
        layout = QVBoxLayout(self)
        layout.addWidget(self.top_label)
        layout.addWidget(self.register_radio_button)
        layout.addWidget(self.evaluate_radio_button)

    def nextId(self):
        if self.evaluate_radio_button.isChecked():
            return Pages.Page_Evaluate
        else:
            return Pages.Page_Register


class EvaluatePage(QWizardPage):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setTitle("Evaluate <i>Super Product One</i>&trade")
        self.setSubTitle(
            "Please fill both fields. Make sure to provide a valid "
            "email address (e.g., john.smith@example.com)."
        )

        self.name_label = QLabel("N&ame:")
        self.name_line_edit = QLineEdit()

        self.name_label.setBuddy(self.name_line_edit)

        self.email_label = QLabel("&Email address:")
        self.email_line_edit = QLineEdit()
        self.email_line_edit.setValidator(
            QRegularExpressionValidator(QRegularExpression(EMAIL_REGEXP), self)
        )
        self.email_label.setBuddy(self.email_line_edit)

        self.registerField("evaluate.name*", self.name_line_edit)
        self.registerField("evaluate.email*", self.email_line_edit)

        layout = QGridLayout(self)
        layout.addWidget(self.name_label, 0, 0)
        layout.addWidget(self.name_line_edit, 0, 1)
        layout.addWidget(self.email_label, 1, 0)
        layout.addWidget(self.email_line_edit, 1, 1)

    def nextId(self):
        return Pages.Page_Conclusion


class RegisterPage(QWizardPage):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setTitle("Register Your Copy of <i>Super Product One</i>&trade")
        self.setSubTitle("If you have an upgrade key, please fill in " "the appropriate field.")

        self.name_label = QLabel("N&ame:")
        self.name_line_edit = QLineEdit()
        self.name_label.setBuddy(self.name_line_edit)

        self.upgrade_key_label = QLabel("&Upgrade key:")
        self.upgrade_key_line_edit = QLineEdit()
        self.upgrade_key_label.setBuddy(self.upgrade_key_line_edit)

        self.registerField("register.name*", self.name_line_edit)
        self.registerField("register.upgradeKey", self.upgrade_key_line_edit)

        layout = QGridLayout(self)
        layout.addWidget(self.name_label, 0, 0)
        layout.addWidget(self.name_line_edit, 0, 1)
        layout.addWidget(self.upgrade_key_label, 1, 0)
        layout.addWidget(self.upgrade_key_line_edit, 1, 1)

    def nextId(self):
        if self.upgrade_key_line_edit.text():
            return Pages.Page_Details
        else:
            return Pages.Page_Conclusion


class DetailsPage(QWizardPage):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setTitle("Fill In Your Details")
        self.setSubTitle(
            "Please fill all three fields. Make sure to provide a valid "
            "email address (e.g., tanaka.aya@example.co.jp)."
        )

        self.company_label = QLabel("&Company name:")
        self.company_line_edit = QLineEdit()
        self.company_label.setBuddy(self.company_line_edit)

        self.email_label = QLabel("&Email address:")
        self.email_line_edit = QLineEdit()
        self.email_line_edit.setValidator(
            QRegularExpressionValidator(QRegularExpression(EMAIL_REGEXP), self)
        )
        self.email_label.setBuddy(self.email_line_edit)

        self.postal_label = QLabel("&Postal address:")
        self.postal_line_edit = QLineEdit()
        self.postal_label.setBuddy(self.postal_line_edit)

        self.registerField("details.company*", self.company_line_edit)
        self.registerField("details.email*", self.email_line_edit)
        self.registerField("details.postal*", self.postal_line_edit)

        layout = QGridLayout(self)
        layout.addWidget(self.company_label, 0, 0)
        layout.addWidget(self.company_line_edit, 0, 1)
        layout.addWidget(self.email_label, 1, 0)
        layout.addWidget(self.email_line_edit, 1, 1)
        layout.addWidget(self.postal_label, 2, 0)
        layout.addWidget(self.postal_line_edit, 2, 1)

    def nextId(self):
        return Pages.Page_Conclusion


class ConclusionPage(QWizardPage):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setTitle("Complete Your Registration")

        path = Path(__file__).resolve().parent
        self.setPixmap(QWizard.WatermarkPixmap, QPixmap(path / "images" / "watermark.png"))

        self.bottom_label = QLabel()
        self.bottom_label.setWordWrap(True)

        agreeCheckBox = QCheckBox("I agree to the terms of the license")

        self.registerField("conclusion.agree*", agreeCheckBox)

        layout = QVBoxLayout(self)
        layout.addWidget(self.bottom_label)
        layout.addWidget(agreeCheckBox)

        self.custom_button_clicked_signal_connected = False

    def nextId(self):
        return -1

    def initializePage(self):
        if self.wizard().hasVisitedPage(Pages.Page_Evaluate):
            license_text = "<u>Evaluation License Agreement:</u> "
            "You can use self software for 30 days and make one "
            "backup, but you are not allowed to distribute it."
        elif self.wizard().hasVisitedPage(Pages.Page_Details):
            license_text = (
                "<u>First-Time License Agreement:</u> "
                "You can use self software subject to the license "
                "you will receive by email."
            )

        else:
            license_text = (
                "<u>Upgrade License Agreement:</u> "
                "This software is licensed under the terms of your "
                "current license."
            )
        self.bottom_label.setText(license_text)

    def setVisible(self, visible: bool):
        super().setVisible(visible)
        if visible:
            self.wizard().setButtonText(QWizard.CustomButton1, "&Print")
            self.wizard().setOption(QWizard.HaveCustomButton1, True)

            if not self.custom_button_clicked_signal_connected:
                self.custom_button_clicked_signal_connected = True
                self.wizard().customButtonClicked.connect(self.print_button_clicked)
        else:
            self.wizard().setOption(QWizard.HaveCustomButton1, False)

            if self.custom_button_clicked_signal_connected:
                self.custom_button_clicked_signal_connected = False
                self.wizard().customButtonClicked.disconnect(self.print_button_clicked)

    def print_button_clicked(self):
        printer = QPrinter()

        dialog = QPrintDialog(printer, self)
        if dialog.exec():
            QMessageBox.warning(
                self,
                "Print License",
                "As an environmentally friendly measure, the "
                "license text will not actually be printed.",
            )


class LicenseWizard(QWizard):
    def __init__(self, parent=None):
        super().__init__(parent)

        self._pages = [
            IntroPage(),
            EvaluatePage(),
            RegisterPage(),
            DetailsPage(),
            ConclusionPage()
        ]

        for page in self._pages:
            self.addPage(page)

        self.setStartId(Pages.Page_Intro)

        if sys.platform == 'darwin':
            self.setWizardStyle(QWizard.ModernStyle)

        self.setOption(QWizard.HaveHelpButton, True)

        path = Path(__file__).resolve().parent
        self.setPixmap(QWizard.LogoPixmap, QPixmap(path / "images" / "logo.png"))

        self.helpRequested.connect(self.show_help)
        self.setWindowTitle("License Wizard")

        self.last_help_message: str = None

    def show_help(self):
        if self.currentId() == Pages.Page_Intro:
            message = "The decision you make here will affect which page you get to see next."

        elif self.currentId() == Pages.Page_Evaluate:
            message = (
                "Make sure to provide a valid email address, such as "
                "toni.buddenbrook@example.de."
            )

        elif self.currentId() == Pages.Page_Register:
            message = (
                "If you don't provide an upgrade key, you will be asked to fill in your details."
            )

        elif self.currentId() == Pages.Page_Details:
            message = (
                "Make sure to provide a valid email address, such as "
                "thomas.gradgrind@example.co.uk."
            )

        elif self.currentId() == Pages.Page_Conclusion:
            message = "You must accept the terms and conditions of the license to proceed."
        else:
            message = "This help is likely not to be of any help."

        if self.last_help_message == message:
            message = (
                "Sorry, I already gave what help I could. Maybe you should try asking a human?"
            )

        QMessageBox.information(self, "License Wizard Help", message)

        self.last_help_message = message