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