1#!/usr/bin/env python
2
3
4#############################################################################
5##
6## Copyright (C) 2013 Riverbank Computing Limited.
7## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8## All rights reserved.
9##
10## This file is part of the examples of PyQt.
11##
12## $QT_BEGIN_LICENSE:BSD$
13## You may use this file under the terms of the BSD license as follows:
14##
15## "Redistribution and use in source and binary forms, with or without
16## modification, are permitted provided that the following conditions are
17## met:
18##   * Redistributions of source code must retain the above copyright
19##     notice, this list of conditions and the following disclaimer.
20##   * Redistributions in binary form must reproduce the above copyright
21##     notice, this list of conditions and the following disclaimer in
22##     the documentation and/or other materials provided with the
23##     distribution.
24##   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
25##     the names of its contributors may be used to endorse or promote
26##     products derived from this software without specific prior written
27##     permission.
28##
29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
40## $QT_END_LICENSE$
41##
42#############################################################################
43
44
45from PyQt5.QtCore import Qt
46from PyQt5.QtGui import QKeySequence
47from PyQt5.QtWidgets import (QAction, QActionGroup, QApplication, QFrame,
48        QLabel, QMainWindow, QMenu, QMessageBox, QSizePolicy, QVBoxLayout,
49        QWidget)
50
51
52class MainWindow(QMainWindow):
53    def __init__(self):
54        super(MainWindow, self).__init__()
55
56        widget = QWidget()
57        self.setCentralWidget(widget)
58
59        topFiller = QWidget()
60        topFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
61
62        self.infoLabel = QLabel(
63                "<i>Choose a menu option, or right-click to invoke a context menu</i>",
64                alignment=Qt.AlignCenter)
65        self.infoLabel.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
66
67        bottomFiller = QWidget()
68        bottomFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
69
70        vbox = QVBoxLayout()
71        vbox.setContentsMargins(5, 5, 5, 5)
72        vbox.addWidget(topFiller)
73        vbox.addWidget(self.infoLabel)
74        vbox.addWidget(bottomFiller)
75        widget.setLayout(vbox)
76
77        self.createActions()
78        self.createMenus()
79
80        message = "A context menu is available by right-clicking"
81        self.statusBar().showMessage(message)
82
83        self.setWindowTitle("Menus")
84        self.setMinimumSize(160,160)
85        self.resize(480,320)
86
87    def contextMenuEvent(self, event):
88        menu = QMenu(self)
89        menu.addAction(self.cutAct)
90        menu.addAction(self.copyAct)
91        menu.addAction(self.pasteAct)
92        menu.exec_(event.globalPos())
93
94    def newFile(self):
95        self.infoLabel.setText("Invoked <b>File|New</b>")
96
97    def open(self):
98        self.infoLabel.setText("Invoked <b>File|Open</b>")
99
100    def save(self):
101        self.infoLabel.setText("Invoked <b>File|Save</b>")
102
103    def print_(self):
104        self.infoLabel.setText("Invoked <b>File|Print</b>")
105
106    def undo(self):
107        self.infoLabel.setText("Invoked <b>Edit|Undo</b>")
108
109    def redo(self):
110        self.infoLabel.setText("Invoked <b>Edit|Redo</b>")
111
112    def cut(self):
113        self.infoLabel.setText("Invoked <b>Edit|Cut</b>")
114
115    def copy(self):
116        self.infoLabel.setText("Invoked <b>Edit|Copy</b>")
117
118    def paste(self):
119        self.infoLabel.setText("Invoked <b>Edit|Paste</b>")
120
121    def bold(self):
122        self.infoLabel.setText("Invoked <b>Edit|Format|Bold</b>")
123
124    def italic(self):
125        self.infoLabel.setText("Invoked <b>Edit|Format|Italic</b>")
126
127    def leftAlign(self):
128        self.infoLabel.setText("Invoked <b>Edit|Format|Left Align</b>")
129
130    def rightAlign(self):
131        self.infoLabel.setText("Invoked <b>Edit|Format|Right Align</b>")
132
133    def justify(self):
134        self.infoLabel.setText("Invoked <b>Edit|Format|Justify</b>")
135
136    def center(self):
137        self.infoLabel.setText("Invoked <b>Edit|Format|Center</b>")
138
139    def setLineSpacing(self):
140        self.infoLabel.setText("Invoked <b>Edit|Format|Set Line Spacing</b>")
141
142    def setParagraphSpacing(self):
143        self.infoLabel.setText("Invoked <b>Edit|Format|Set Paragraph Spacing</b>")
144
145    def about(self):
146        self.infoLabel.setText("Invoked <b>Help|About</b>")
147        QMessageBox.about(self, "About Menu",
148                "The <b>Menu</b> example shows how to create menu-bar menus "
149                "and context menus.")
150
151    def aboutQt(self):
152        self.infoLabel.setText("Invoked <b>Help|About Qt</b>")
153
154    def createActions(self):
155        self.newAct = QAction("&New", self, shortcut=QKeySequence.New,
156                statusTip="Create a new file", triggered=self.newFile)
157
158        self.openAct = QAction("&Open...", self, shortcut=QKeySequence.Open,
159                statusTip="Open an existing file", triggered=self.open)
160
161        self.saveAct = QAction("&Save", self, shortcut=QKeySequence.Save,
162                statusTip="Save the document to disk", triggered=self.save)
163
164        self.printAct = QAction("&Print...", self, shortcut=QKeySequence.Print,
165                statusTip="Print the document", triggered=self.print_)
166
167        self.exitAct = QAction("E&xit", self, shortcut="Ctrl+Q",
168                statusTip="Exit the application", triggered=self.close)
169
170        self.undoAct = QAction("&Undo", self, shortcut=QKeySequence.Undo,
171                statusTip="Undo the last operation", triggered=self.undo)
172
173        self.redoAct = QAction("&Redo", self, shortcut=QKeySequence.Redo,
174                statusTip="Redo the last operation", triggered=self.redo)
175
176        self.cutAct = QAction("Cu&t", self, shortcut=QKeySequence.Cut,
177                statusTip="Cut the current selection's contents to the clipboard",
178                triggered=self.cut)
179
180        self.copyAct = QAction("&Copy", self, shortcut=QKeySequence.Copy,
181                statusTip="Copy the current selection's contents to the clipboard",
182                triggered=self.copy)
183
184        self.pasteAct = QAction("&Paste", self, shortcut=QKeySequence.Paste,
185                statusTip="Paste the clipboard's contents into the current selection",
186                triggered=self.paste)
187
188        self.boldAct = QAction("&Bold", self, checkable=True,
189                shortcut="Ctrl+B", statusTip="Make the text bold",
190                triggered=self.bold)
191
192        boldFont = self.boldAct.font()
193        boldFont.setBold(True)
194        self.boldAct.setFont(boldFont)
195
196        self.italicAct = QAction("&Italic", self, checkable=True,
197                shortcut="Ctrl+I", statusTip="Make the text italic",
198                triggered=self.italic)
199
200        italicFont = self.italicAct.font()
201        italicFont.setItalic(True)
202        self.italicAct.setFont(italicFont)
203
204        self.setLineSpacingAct = QAction("Set &Line Spacing...", self,
205                statusTip="Change the gap between the lines of a paragraph",
206                triggered=self.setLineSpacing)
207
208        self.setParagraphSpacingAct = QAction("Set &Paragraph Spacing...",
209                self, statusTip="Change the gap between paragraphs",
210                triggered=self.setParagraphSpacing)
211
212        self.aboutAct = QAction("&About", self,
213                statusTip="Show the application's About box",
214                triggered=self.about)
215
216        self.aboutQtAct = QAction("About &Qt", self,
217                statusTip="Show the Qt library's About box",
218                triggered=self.aboutQt)
219        self.aboutQtAct.triggered.connect(QApplication.instance().aboutQt)
220
221        self.leftAlignAct = QAction("&Left Align", self, checkable=True,
222                shortcut="Ctrl+L", statusTip="Left align the selected text",
223                triggered=self.leftAlign)
224
225        self.rightAlignAct = QAction("&Right Align", self, checkable=True,
226                shortcut="Ctrl+R", statusTip="Right align the selected text",
227                triggered=self.rightAlign)
228
229        self.justifyAct = QAction("&Justify", self, checkable=True,
230                shortcut="Ctrl+J", statusTip="Justify the selected text",
231                triggered=self.justify)
232
233        self.centerAct = QAction("&Center", self, checkable=True,
234                shortcut="Ctrl+C", statusTip="Center the selected text",
235                triggered=self.center)
236
237        self.alignmentGroup = QActionGroup(self)
238        self.alignmentGroup.addAction(self.leftAlignAct)
239        self.alignmentGroup.addAction(self.rightAlignAct)
240        self.alignmentGroup.addAction(self.justifyAct)
241        self.alignmentGroup.addAction(self.centerAct)
242        self.leftAlignAct.setChecked(True)
243
244    def createMenus(self):
245        self.fileMenu = self.menuBar().addMenu("&File")
246        self.fileMenu.addAction(self.newAct)
247        self.fileMenu.addAction(self.openAct)
248        self.fileMenu.addAction(self.saveAct)
249        self.fileMenu.addAction(self.printAct)
250        self.fileMenu.addSeparator()
251        self.fileMenu.addAction(self.exitAct)
252
253        self.editMenu = self.menuBar().addMenu("&Edit")
254        self.editMenu.addAction(self.undoAct)
255        self.editMenu.addAction(self.redoAct)
256        self.editMenu.addSeparator()
257        self.editMenu.addAction(self.cutAct)
258        self.editMenu.addAction(self.copyAct)
259        self.editMenu.addAction(self.pasteAct)
260        self.editMenu.addSeparator()
261
262        self.helpMenu = self.menuBar().addMenu("&Help")
263        self.helpMenu.addAction(self.aboutAct)
264        self.helpMenu.addAction(self.aboutQtAct)
265
266        self.formatMenu = self.editMenu.addMenu("&Format")
267        self.formatMenu.addAction(self.boldAct)
268        self.formatMenu.addAction(self.italicAct)
269        self.formatMenu.addSeparator().setText("Alignment")
270        self.formatMenu.addAction(self.leftAlignAct)
271        self.formatMenu.addAction(self.rightAlignAct)
272        self.formatMenu.addAction(self.justifyAct)
273        self.formatMenu.addAction(self.centerAct)
274        self.formatMenu.addSeparator()
275        self.formatMenu.addAction(self.setLineSpacingAct)
276        self.formatMenu.addAction(self.setParagraphSpacingAct)
277
278
279if __name__ == '__main__':
280
281    import sys
282
283    app = QApplication(sys.argv)
284    window = MainWindow()
285    window.show()
286    sys.exit(app.exec_())
287