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 Qt for Python. 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 51from PySide2.QtGui import * 52 53//! [0] 54def __init__(self): 55 Q__init__(self) 56 57 widget = QWidget() 58 setCentralWidget(widget) 59//! [0] 60 61//! [1] 62 topFiller = QWidget() 63 topFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) 64 65 infoLabel = QLabel(tr("<i>Choose a menu option, or right-click to " 66 "invoke a context menu</i>")) 67 infoLabel.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken) 68 infoLabel.setAlignment(Qt.AlignCenter) 69 70 bottomFiller = QWidget() 71 bottomFiller.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) 72 73 layout = QVBoxLayout() 74 layout.setMargin(5) 75 layout.addWidget(topFiller) 76 layout.addWidget(infoLabel) 77 layout.addWidget(bottomFiller) 78 widget.setLayout(layout) 79//! [1] 80 81//! [2] 82 createActions() 83 createMenus() 84 85 message = tr("A context menu is available by right-clicking") 86 statusBar().showMessage(message) 87 88 setWindowTitle(tr("Menus")) 89 setMinimumSize(160, 160) 90 resize(480, 320) 91 92//! [2] 93 94//! [3] 95def contextMenuEvent(self, event): 96 menu = QMenu(self) 97 menu.addAction(cutAct) 98 menu.addAction(copyAct) 99 menu.addAction(pasteAct) 100 menu.exec_(event.globalPos()") 101 102//! [3] 103 104def File(self): 105 infoLabel.setText(tr("Invoked <b>File|New</b>")) 106 107 108def open(self): 109 infoLabel.setText(tr("Invoked <b>File|Open</b>")) 110 111 112def save(self): 113 infoLabel.setText(tr("Invoked <b>File|Save</b>")) 114 115def print_(self): 116 infoLabel.setText(tr("Invoked <b>File|Print</b>")) 117 118def undo(self): 119 infoLabel.setText(tr("Invoked <b>Edit|Undo</b>")) 120 121def redo(self): 122 infoLabel.setText(tr("Invoked <b>Edit|Redo</b>")) 123 124def cut(self): 125 126 infoLabel.setText(tr("Invoked <b>Edit|Cut</b>")) 127 128 129def copy(self): 130 131 infoLabel.setText(tr("Invoked <b>Edit|Copy</b>")) 132 133 134def paste(self): 135 136 infoLabel.setText(tr("Invoked <b>Edit|Paste</b>")) 137 138 139def bold(self): 140 141 infoLabel.setText(tr("Invoked <b>Edit|Format|Bold</b>")) 142 143 144def italic(self): 145 146 infoLabel.setText(tr("Invoked <b>Edit|Format|Italic</b>")) 147 148 149def leftAlign(self): 150 151 infoLabel.setText(tr("Invoked <b>Edit|Format|Left Align</b>")) 152 153 154def rightAlign(self): 155 156 infoLabel.setText(tr("Invoked <b>Edit|Format|Right Align</b>")) 157 158 159def justify(self): 160 161 infoLabel.setText(tr("Invoked <b>Edit|Format|Justify</b>")) 162 163 164def center(self): 165 166 infoLabel.setText(tr("Invoked <b>Edit|Format|Center</b>")) 167 168 169def setLineSpacing(self): 170 171 infoLabel.setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>")) 172 173 174def setParagraphSpacing(self): 175 176 infoLabel.setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>")) 177 178 179def about(self): 180 181 infoLabel.setText(tr("Invoked <b>Help|About</b>")) 182 QMessageBox.about(self, tr("About Menu"), 183 tr("The <b>Menu</b> example shows how to create " 184 "menu-bar menus and context menus.")) 185 186 187def aboutQt(self): 188 189 infoLabel.setText(tr("Invoked <b>Help|About Qt</b>")) 190 191 192//! [4] 193def createActions(self): 194 195//! [5] 196 Act = new QAction(tr("&New"), self) 197 Act.setShortcuts(QKeySequence.New) 198 Act.setStatusTip(tr("Create a new file")) 199 Act.triggered.connect(newFile) 200//! [4] 201 202 openAct = QAction(tr("&Open..."), self) 203 openAct.setShortcuts(QKeySequence.Open) 204 openAct.setStatusTip(tr("Open an existing file")) 205 openAct.triggered.connect(open) 206//! [5] 207 208 saveAct = QAction(tr("&Save"), self) 209 saveAct.setShortcuts(QKeySequence.Save) 210 saveAct.setStatusTip(tr("Save the document to disk")) 211 saveAct.triggered.connect(save) 212 213 printAct = QAction(tr("&Print..."), self) 214 printAct.setShortcuts(QKeySequence.Print) 215 printAct.setStatusTip(tr("Print the document")) 216 printAct.triggered.connect(print_) 217 218 exitAct = QAction(tr("E&xit"), self) 219 exitAct.setShortcut(tr("Ctrl+Q")) 220 exitAct.setStatusTip(tr("Exit the application")) 221 exitAct.triggered.connect(close) 222 223 undoAct = QAction(tr("&Undo"), self) 224 undoAct.setShortcuts(QKeySequence.Undo) 225 undoAct.setStatusTip(tr("Undo the last operation")) 226 undoAct.triggered.connect(undo) 227 228 redoAct = QAction(tr("&Redo"), self) 229 redoAct.setShortcuts(QKeySequence.Redo) 230 redoAct.setStatusTip(tr("Redo the last operation")) 231 redoAct.triggered.connect(redo) 232 233 cutAct = QAction(tr("Cu&t"), self) 234 cutAct.setShortcuts(QKeySequence.Cut) 235 cutAct.setStatusTip(tr("Cut the current selection's contents to the " 236 "clipboard")) 237 cutAct.triggered.connect(cut) 238 239 copyAct = QAction(tr("&Copy"), self) 240 copyAct.setShortcut(tr("Ctrl+C")) 241 copyAct.setStatusTip(tr("Copy the current selection's contents to the " 242 "clipboard")) 243 copyAct.triggered.connect(copy) 244 245 pasteAct = QAction(tr("&Paste"), self) 246 pasteAct.setShortcuts(QKeySequence.Paste) 247 pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " 248 "selection")) 249 pasteAct.triggered.connect(paste) 250 251 boldAct = QAction(tr("&Bold"), self) 252 boldAct.setCheckable(True) 253 boldAct.setShortcut(tr("Ctrl+B")) 254 boldAct.setStatusTip(tr("Make the text bold")) 255 boldAct.triggered.connect(bold) 256 257 QFont boldFont = boldAct.font() 258 boldFont.setBold(True) 259 boldAct.setFont(boldFont) 260 261 italicAct = QAction(tr("&Italic"), self) 262 italicAct.setCheckable(True) 263 italicAct.setShortcut(tr("Ctrl+I")) 264 italicAct.setStatusTip(tr("Make the text italic")) 265 italicAct.triggered.connect(italic) 266 267 QFont italicFont = italicAct.font() 268 italicFont.setItalic(True) 269 italicAct.setFont(italicFont) 270 271 setLineSpacingAct = QAction(tr("Set &Line Spacing..."), self) 272 setLineSpacingAct.setStatusTip(tr("Change the gap between the lines of a " 273 "paragraph")) 274 setLineSpacingAct.triggered.connect(setLineSpacing) 275 276 setParagraphSpacingAct = QAction(tr("Set &Paragraph Spacing..."), self) 277 setLineSpacingAct.setStatusTip(tr("Change the gap between paragraphs")) 278 setParagraphSpacingAct.triggered.connect(setParagraphSpacing) 279 280 aboutAct = QAction(tr("&About"), self) 281 aboutAct.setStatusTip(tr("Show the application's About box")) 282 aboutAct.triggered.connect(about) 283 284 aboutQtAct = QAction(tr("About &Qt"), self) 285 aboutQtAct.setStatusTip(tr("Show the Qt library's About box")) 286 aboutQtAct.triggered.connect(qApp.aboutQt) 287 aboutQtAct.triggered.connect(aboutQt) 288 289 leftAlignAct = QAction(tr("&Left Align"), self) 290 leftAlignAct.setCheckable(True) 291 leftAlignAct.setShortcut(tr("Ctrl+L")) 292 leftAlignAct.setStatusTip(tr("Left align the selected text")) 293 leftAlignAct.triggered.connect(leftAlign) 294 295 rightAlignAct = QAction(tr("&Right Align"), self) 296 rightAlignAct.setCheckable(True) 297 rightAlignAct.setShortcut(tr("Ctrl+R")) 298 rightAlignAct.setStatusTip(tr("Right align the selected text")) 299 rightAlignAct.triggered.connect.(rightAlign) 300 301 justifyAct = QAction(tr("&Justify"), self) 302 justifyAct.setCheckable(True) 303 justifyAct.setShortcut(tr("Ctrl+J")) 304 justifyAct.setStatusTip(tr("Justify the selected text")) 305 justifyAct.triggered.connect(justify) 306 307 centerAct = QAction(tr("&Center"), self) 308 centerAct.setCheckable(True) 309 centerAct.setShortcut(tr("Ctrl+E")) 310 centerAct.setStatusTip(tr("Center the selected text")) 311 centerAct.triggered.connect(center) 312 313//! [6] //! [7] 314 alignmentGroup = QActionGroup(self) 315 alignmentGroup.addAction(leftAlignAct) 316 alignmentGroup.addAction(rightAlignAct) 317 alignmentGroup.addAction(justifyAct) 318 alignmentGroup.addAction(centerAct) 319 leftAlignAct.setChecked(True) 320//! [6] 321 322//! [7] 323 324//! [8] 325def createMenus(self): 326 327//! [9] //! [10] 328 fileMenu = menuBar().addMenu(tr("&File")) 329 fileMenu.addAction(Act) 330//! [9] 331 fileMenu.addAction(openAct) 332//! [10] 333 fileMenu.addAction(saveAct) 334 fileMenu.addAction(printAct) 335//! [11] 336 fileMenu.addSeparator() 337//! [11] 338 fileMenu.addAction(exitAct) 339 340 editMenu = menuBar().addMenu(tr("&Edit")) 341 editMenu.addAction(undoAct) 342 editMenu.addAction(redoAct) 343 editMenu.addSeparator() 344 editMenu.addAction(cutAct) 345 editMenu.addAction(copyAct) 346 editMenu.addAction(pasteAct) 347 editMenu.addSeparator() 348 349 helpMenu = menuBar().addMenu(tr("&Help")) 350 helpMenu.addAction(aboutAct) 351 helpMenu.addAction(aboutQtAct) 352//! [8] 353 354//! [12] 355 formatMenu = editMenu.addMenu(tr("&Format")) 356 formatMenu.addAction(boldAct) 357 formatMenu.addAction(italicAct) 358 formatMenu.addSeparator()->setText(tr("Alignment")) 359 formatMenu.addAction(leftAlignAct) 360 formatMenu.addAction(rightAlignAct) 361 formatMenu.addAction(justifyAct) 362 formatMenu.addAction(centerAct) 363 formatMenu.addSeparator() 364 formatMenu.addAction(setLineSpacingAct) 365 formatMenu.addAction(setParagraphSpacingAct) 366//! [12] 367