aboutsummaryrefslogtreecommitdiffstats
path: root/examples/widgets/richtext/orderform/orderform.py
blob: fd0be01fe0d7fd5ec9fa3423144928eb26521f4e (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
# Copyright (C) 2013 Riverbank Computing Limited.
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

"""PySide6 port of the widgets/richtext/orderform example from Qt v5.x"""

import sys

from PySide6.QtCore import QDate, Qt, Slot
from PySide6.QtGui import (QFont, QTextCharFormat, QTextCursor,
                           QTextFrameFormat, QTextLength, QTextTableFormat)
from PySide6.QtWidgets import (QApplication, QCheckBox, QDialog,
                               QDialogButtonBox, QGridLayout, QLabel,
                               QLineEdit, QMainWindow, QMenu, QMessageBox,
                               QTableWidget, QTableWidgetItem, QTabWidget,
                               QTextEdit)
from PySide6.QtPrintSupport import QAbstractPrintDialog, QPrintDialog, QPrinter


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        file_menu = QMenu("&File", self)
        new_action = file_menu.addAction("&New...")
        new_action.setShortcut("Ctrl+N")
        self._print_action = file_menu.addAction("&Print...", self.print_file)
        self._print_action.setShortcut("Ctrl+P")
        self._print_action.setEnabled(False)
        quit_action = file_menu.addAction("E&xit")
        quit_action.setShortcut("Ctrl+Q")
        self.menuBar().addMenu(file_menu)

        self.letters = QTabWidget()

        new_action.triggered.connect(self.open_dialog)
        quit_action.triggered.connect(self.close)

        self.setCentralWidget(self.letters)
        self.setWindowTitle("Order Form")

    def create_letter(self, name, address, orderItems, sendOffers):
        editor = QTextEdit()
        tab_index = self.letters.addTab(editor, name)
        self.letters.setCurrentIndex(tab_index)

        cursor = editor.textCursor()
        cursor.movePosition(QTextCursor.Start)
        top_frame = cursor.currentFrame()
        top_frame_format = top_frame.frameFormat()
        top_frame_format.setPadding(16)
        top_frame.setFrameFormat(top_frame_format)

        text_format = QTextCharFormat()
        bold_format = QTextCharFormat()
        bold_format.setFontWeight(QFont.Bold)

        reference_frame_format = QTextFrameFormat()
        reference_frame_format.setBorder(1)
        reference_frame_format.setPadding(8)
        reference_frame_format.setPosition(QTextFrameFormat.FloatRight)
        reference_frame_format.setWidth(QTextLength(QTextLength.PercentageLength, 40))
        cursor.insertFrame(reference_frame_format)

        cursor.insertText("A company", bold_format)
        cursor.insertBlock()
        cursor.insertText("321 City Street")
        cursor.insertBlock()
        cursor.insertText("Industry Park")
        cursor.insertBlock()
        cursor.insertText("Another country")

        cursor.setPosition(top_frame.lastPosition())

        cursor.insertText(name, text_format)
        for line in address.split("\n"):
            cursor.insertBlock()
            cursor.insertText(line)

        cursor.insertBlock()
        cursor.insertBlock()

        date = QDate.currentDate()
        date_str = date.toString('d MMMM yyyy')
        cursor.insertText(f"Date: {date_str}", text_format)
        cursor.insertBlock()

        body_frame_format = QTextFrameFormat()
        body_frame_format.setWidth(QTextLength(QTextLength.PercentageLength, 100))
        cursor.insertFrame(body_frame_format)

        cursor.insertText("I would like to place an order for the following "
                "items:", text_format)
        cursor.insertBlock()
        cursor.insertBlock()

        order_table_format = QTextTableFormat()
        order_table_format.setAlignment(Qt.AlignHCenter)
        order_table = cursor.insertTable(1, 2, order_table_format)

        order_frame_format = cursor.currentFrame().frameFormat()
        order_frame_format.setBorder(1)
        cursor.currentFrame().setFrameFormat(order_frame_format)

        cursor = order_table.cellAt(0, 0).firstCursorPosition()
        cursor.insertText("Product", bold_format)
        cursor = order_table.cellAt(0, 1).firstCursorPosition()
        cursor.insertText("Quantity", bold_format)

        for text, quantity in orderItems:
            row = order_table.rows()

            order_table.insertRows(row, 1)
            cursor = order_table.cellAt(row, 0).firstCursorPosition()
            cursor.insertText(text, text_format)
            cursor = order_table.cellAt(row, 1).firstCursorPosition()
            cursor.insertText(str(quantity), text_format)

        cursor.setPosition(top_frame.lastPosition())

        cursor.insertBlock()

        cursor.insertText("Please update my records to take account of the "
                "following privacy information:")
        cursor.insertBlock()

        offers_table = cursor.insertTable(2, 2)

        cursor = offers_table.cellAt(0, 1).firstCursorPosition()
        cursor.insertText("I want to receive more information about your "
                "company's products and special offers.", text_format)
        cursor = offers_table.cellAt(1, 1).firstCursorPosition()
        cursor.insertText("I do not want to receive any promotional "
                "information from your company.", text_format)

        if sendOffers:
            cursor = offers_table.cellAt(0, 0).firstCursorPosition()
        else:
            cursor = offers_table.cellAt(1, 0).firstCursorPosition()

        cursor.insertText('X', bold_format)

        cursor.setPosition(top_frame.lastPosition())
        cursor.insertBlock()
        cursor.insertText("Sincerely,", text_format)
        cursor.insertBlock()
        cursor.insertBlock()
        cursor.insertBlock()
        cursor.insertText(name)

        self._print_action.setEnabled(True)

    def create_sample(self):
        dialog = DetailsDialog('Dialog with default values', self)
        self.create_letter('Mr Smith',
                '12 High Street\nSmall Town\nThis country',
                dialog.order_items(), True)

    @Slot()
    def open_dialog(self):
        dialog = DetailsDialog("Enter Customer Details", self)

        if dialog.exec() == QDialog.Accepted:
            self.create_letter(dialog.sender_name(), dialog.sender_address(),
                    dialog.order_items(), dialog.send_offers())

    @Slot()
    def print_file(self):
        editor = self.letters.currentWidget()
        printer = QPrinter()

        dialog = QPrintDialog(printer, self)
        dialog.setWindowTitle("Print Document")

        if editor.textCursor().hasSelection():
            dialog.addEnabledOption(QAbstractPrintDialog.PrintSelection)

        if dialog.exec() != QDialog.Accepted:
            return

        editor.print_(printer)


class DetailsDialog(QDialog):
    def __init__(self, title, parent):
        super().__init__(parent)

        self.items = ("T-shirt", "Badge", "Reference book", "Coffee cup")

        name_label = QLabel("Name:")
        address_label = QLabel("Address:")
        address_label.setAlignment(Qt.AlignLeft | Qt.AlignTop)

        self._name_edit = QLineEdit()
        self._address_edit = QTextEdit()
        self._offers_check_box = QCheckBox("Send information about "
                "products and special offers:")

        self.setup_items_table()

        button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)

        button_box.accepted.connect(self.verify)
        button_box.rejected.connect(self.reject)

        main_layout = QGridLayout(self)
        main_layout.addWidget(name_label, 0, 0)
        main_layout.addWidget(self._name_edit, 0, 1)
        main_layout.addWidget(address_label, 1, 0)
        main_layout.addWidget(self._address_edit, 1, 1)
        main_layout.addWidget(self._items_table, 0, 2, 2, 1)
        main_layout.addWidget(self._offers_check_box, 2, 1, 1, 2)
        main_layout.addWidget(button_box, 3, 0, 1, 3)

        self.setWindowTitle(title)

    def setup_items_table(self):
        self._items_table = QTableWidget(len(self.items), 2)

        for row, item in enumerate(self.items):
            name = QTableWidgetItem(item)
            name.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
            self._items_table.setItem(row, 0, name)
            quantity = QTableWidgetItem('1')
            self._items_table.setItem(row, 1, quantity)

    def order_items(self):
        order_list = []

        for row in range(len(self.items)):
            text = self._items_table.item(row, 0).text()
            quantity = int(self._items_table.item(row, 1).data(Qt.DisplayRole))
            order_list.append((text, max(0, quantity)))

        return order_list

    def sender_name(self):
        return self._name_edit.text()

    def sender_address(self):
        return self._address_edit.toPlainText()

    def send_offers(self):
        return self._offers_check_box.isChecked()

    @Slot()
    def verify(self):
        if self._name_edit.text() and self._address_edit.toPlainText():
            self.accept()
            return

        answer = QMessageBox.warning(self, "Incomplete Form",
                "The form does not contain all the necessary information.\n"
                "Do you want to discard it?",
                QMessageBox.Yes, QMessageBox.No)

        if answer == QMessageBox.Yes:
            self.reject()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.resize(640, 480)
    window.show()
    window.create_sample()
    sys.exit(app.exec())