[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 |
|
---|
| 43 | #include "mainwindow.h"
|
---|
| 44 |
|
---|
| 45 | //! [0]
|
---|
| 46 | MainWindow::MainWindow()
|
---|
| 47 | {
|
---|
| 48 | QWidget *widget = new QWidget;
|
---|
| 49 | setCentralWidget(widget);
|
---|
| 50 | //! [0]
|
---|
| 51 |
|
---|
| 52 | //! [1]
|
---|
| 53 | QWidget *topFiller = new QWidget;
|
---|
| 54 | topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
---|
| 55 |
|
---|
| 56 | infoLabel = new QLabel(tr("<i>Choose a menu option, or right-click to "
|
---|
| 57 | "invoke a context menu</i>"));
|
---|
| 58 | infoLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
|
---|
| 59 | infoLabel->setAlignment(Qt::AlignCenter);
|
---|
| 60 |
|
---|
| 61 | QWidget *bottomFiller = new QWidget;
|
---|
| 62 | bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
---|
| 63 |
|
---|
| 64 | QVBoxLayout *layout = new QVBoxLayout;
|
---|
| 65 | layout->setMargin(5);
|
---|
| 66 | layout->addWidget(topFiller);
|
---|
| 67 | layout->addWidget(infoLabel);
|
---|
| 68 | layout->addWidget(bottomFiller);
|
---|
| 69 | widget->setLayout(layout);
|
---|
| 70 | //! [1]
|
---|
| 71 |
|
---|
| 72 | //! [2]
|
---|
| 73 | createActions();
|
---|
| 74 | createMenus();
|
---|
| 75 |
|
---|
| 76 | QString message = tr("A context menu is available by right-clicking");
|
---|
| 77 | statusBar()->showMessage(message);
|
---|
| 78 |
|
---|
| 79 | setWindowTitle(tr("Menus"));
|
---|
| 80 | setMinimumSize(160, 160);
|
---|
| 81 | resize(480, 320);
|
---|
| 82 | }
|
---|
| 83 | //! [2]
|
---|
| 84 |
|
---|
| 85 | //! [3]
|
---|
| 86 | void MainWindow::contextMenuEvent(QContextMenuEvent *event)
|
---|
| 87 | {
|
---|
| 88 | QMenu menu(this);
|
---|
| 89 | menu.addAction(cutAct);
|
---|
| 90 | menu.addAction(copyAct);
|
---|
| 91 | menu.addAction(pasteAct);
|
---|
| 92 | menu.exec(event->globalPos());
|
---|
| 93 | }
|
---|
| 94 | //! [3]
|
---|
| 95 |
|
---|
| 96 | void MainWindow::newFile()
|
---|
| 97 | {
|
---|
| 98 | infoLabel->setText(tr("Invoked <b>File|New</b>"));
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | void MainWindow::open()
|
---|
| 102 | {
|
---|
| 103 | infoLabel->setText(tr("Invoked <b>File|Open</b>"));
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void MainWindow::save()
|
---|
| 107 | {
|
---|
| 108 | infoLabel->setText(tr("Invoked <b>File|Save</b>"));
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | void MainWindow::print()
|
---|
| 112 | {
|
---|
| 113 | infoLabel->setText(tr("Invoked <b>File|Print</b>"));
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void MainWindow::undo()
|
---|
| 117 | {
|
---|
| 118 | infoLabel->setText(tr("Invoked <b>Edit|Undo</b>"));
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void MainWindow::redo()
|
---|
| 122 | {
|
---|
| 123 | infoLabel->setText(tr("Invoked <b>Edit|Redo</b>"));
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | void MainWindow::cut()
|
---|
| 127 | {
|
---|
| 128 | infoLabel->setText(tr("Invoked <b>Edit|Cut</b>"));
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void MainWindow::copy()
|
---|
| 132 | {
|
---|
| 133 | infoLabel->setText(tr("Invoked <b>Edit|Copy</b>"));
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void MainWindow::paste()
|
---|
| 137 | {
|
---|
| 138 | infoLabel->setText(tr("Invoked <b>Edit|Paste</b>"));
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | void MainWindow::bold()
|
---|
| 142 | {
|
---|
| 143 | infoLabel->setText(tr("Invoked <b>Edit|Format|Bold</b>"));
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | void MainWindow::italic()
|
---|
| 147 | {
|
---|
| 148 | infoLabel->setText(tr("Invoked <b>Edit|Format|Italic</b>"));
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | void MainWindow::leftAlign()
|
---|
| 152 | {
|
---|
| 153 | infoLabel->setText(tr("Invoked <b>Edit|Format|Left Align</b>"));
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | void MainWindow::rightAlign()
|
---|
| 157 | {
|
---|
| 158 | infoLabel->setText(tr("Invoked <b>Edit|Format|Right Align</b>"));
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | void MainWindow::justify()
|
---|
| 162 | {
|
---|
| 163 | infoLabel->setText(tr("Invoked <b>Edit|Format|Justify</b>"));
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | void MainWindow::center()
|
---|
| 167 | {
|
---|
| 168 | infoLabel->setText(tr("Invoked <b>Edit|Format|Center</b>"));
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | void MainWindow::setLineSpacing()
|
---|
| 172 | {
|
---|
| 173 | infoLabel->setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>"));
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | void MainWindow::setParagraphSpacing()
|
---|
| 177 | {
|
---|
| 178 | infoLabel->setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>"));
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | void MainWindow::about()
|
---|
| 182 | {
|
---|
| 183 | infoLabel->setText(tr("Invoked <b>Help|About</b>"));
|
---|
| 184 | QMessageBox::about(this, tr("About Menu"),
|
---|
| 185 | tr("The <b>Menu</b> example shows how to create "
|
---|
| 186 | "menu-bar menus and context menus."));
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | void MainWindow::aboutQt()
|
---|
| 190 | {
|
---|
| 191 | infoLabel->setText(tr("Invoked <b>Help|About Qt</b>"));
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | //! [4]
|
---|
| 195 | void MainWindow::createActions()
|
---|
| 196 | {
|
---|
| 197 | //! [5]
|
---|
| 198 | newAct = new QAction(tr("&New"), this);
|
---|
| 199 | newAct->setShortcuts(QKeySequence::New);
|
---|
| 200 | newAct->setStatusTip(tr("Create a new file"));
|
---|
| 201 | connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
|
---|
| 202 | //! [4]
|
---|
| 203 |
|
---|
| 204 | openAct = new QAction(tr("&Open..."), this);
|
---|
| 205 | openAct->setShortcuts(QKeySequence::Open);
|
---|
| 206 | openAct->setStatusTip(tr("Open an existing file"));
|
---|
| 207 | connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
|
---|
| 208 | //! [5]
|
---|
| 209 |
|
---|
| 210 | saveAct = new QAction(tr("&Save"), this);
|
---|
| 211 | saveAct->setShortcuts(QKeySequence::Save);
|
---|
| 212 | saveAct->setStatusTip(tr("Save the document to disk"));
|
---|
| 213 | connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
|
---|
| 214 |
|
---|
| 215 | printAct = new QAction(tr("&Print..."), this);
|
---|
| 216 | printAct->setShortcuts(QKeySequence::Print);
|
---|
| 217 | printAct->setStatusTip(tr("Print the document"));
|
---|
| 218 | connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
|
---|
| 219 |
|
---|
| 220 | exitAct = new QAction(tr("E&xit"), this);
|
---|
[561] | 221 | exitAct->setShortcuts(QKeySequence::Quit);
|
---|
[2] | 222 | exitAct->setStatusTip(tr("Exit the application"));
|
---|
| 223 | connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
|
---|
| 224 |
|
---|
| 225 | undoAct = new QAction(tr("&Undo"), this);
|
---|
| 226 | undoAct->setShortcuts(QKeySequence::Undo);
|
---|
| 227 | undoAct->setStatusTip(tr("Undo the last operation"));
|
---|
| 228 | connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
|
---|
| 229 |
|
---|
| 230 | redoAct = new QAction(tr("&Redo"), this);
|
---|
| 231 | redoAct->setShortcuts(QKeySequence::Redo);
|
---|
| 232 | redoAct->setStatusTip(tr("Redo the last operation"));
|
---|
| 233 | connect(redoAct, SIGNAL(triggered()), this, SLOT(redo()));
|
---|
| 234 |
|
---|
| 235 | cutAct = new QAction(tr("Cu&t"), this);
|
---|
| 236 | cutAct->setShortcuts(QKeySequence::Cut);
|
---|
| 237 | cutAct->setStatusTip(tr("Cut the current selection's contents to the "
|
---|
| 238 | "clipboard"));
|
---|
| 239 | connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
|
---|
| 240 |
|
---|
| 241 | copyAct = new QAction(tr("&Copy"), this);
|
---|
[561] | 242 | copyAct->setShortcuts(QKeySequence::Copy);
|
---|
[2] | 243 | copyAct->setStatusTip(tr("Copy the current selection's contents to the "
|
---|
| 244 | "clipboard"));
|
---|
| 245 | connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
|
---|
| 246 |
|
---|
| 247 | pasteAct = new QAction(tr("&Paste"), this);
|
---|
| 248 | pasteAct->setShortcuts(QKeySequence::Paste);
|
---|
| 249 | pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
|
---|
| 250 | "selection"));
|
---|
| 251 | connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
|
---|
| 252 |
|
---|
| 253 | boldAct = new QAction(tr("&Bold"), this);
|
---|
| 254 | boldAct->setCheckable(true);
|
---|
[561] | 255 | boldAct->setShortcut(QKeySequence::Bold);
|
---|
[2] | 256 | boldAct->setStatusTip(tr("Make the text bold"));
|
---|
| 257 | connect(boldAct, SIGNAL(triggered()), this, SLOT(bold()));
|
---|
| 258 |
|
---|
| 259 | QFont boldFont = boldAct->font();
|
---|
| 260 | boldFont.setBold(true);
|
---|
| 261 | boldAct->setFont(boldFont);
|
---|
| 262 |
|
---|
| 263 | italicAct = new QAction(tr("&Italic"), this);
|
---|
| 264 | italicAct->setCheckable(true);
|
---|
[561] | 265 | italicAct->setShortcut(QKeySequence::Italic);
|
---|
[2] | 266 | italicAct->setStatusTip(tr("Make the text italic"));
|
---|
| 267 | connect(italicAct, SIGNAL(triggered()), this, SLOT(italic()));
|
---|
| 268 |
|
---|
| 269 | QFont italicFont = italicAct->font();
|
---|
| 270 | italicFont.setItalic(true);
|
---|
| 271 | italicAct->setFont(italicFont);
|
---|
| 272 |
|
---|
| 273 | setLineSpacingAct = new QAction(tr("Set &Line Spacing..."), this);
|
---|
| 274 | setLineSpacingAct->setStatusTip(tr("Change the gap between the lines of a "
|
---|
| 275 | "paragraph"));
|
---|
| 276 | connect(setLineSpacingAct, SIGNAL(triggered()), this, SLOT(setLineSpacing()));
|
---|
| 277 |
|
---|
| 278 | setParagraphSpacingAct = new QAction(tr("Set &Paragraph Spacing..."), this);
|
---|
| 279 | setLineSpacingAct->setStatusTip(tr("Change the gap between paragraphs"));
|
---|
| 280 | connect(setParagraphSpacingAct, SIGNAL(triggered()),
|
---|
| 281 | this, SLOT(setParagraphSpacing()));
|
---|
| 282 |
|
---|
| 283 | aboutAct = new QAction(tr("&About"), this);
|
---|
| 284 | aboutAct->setStatusTip(tr("Show the application's About box"));
|
---|
| 285 | connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
|
---|
| 286 |
|
---|
| 287 | aboutQtAct = new QAction(tr("About &Qt"), this);
|
---|
| 288 | aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
|
---|
| 289 | connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
---|
| 290 | connect(aboutQtAct, SIGNAL(triggered()), this, SLOT(aboutQt()));
|
---|
| 291 |
|
---|
| 292 | leftAlignAct = new QAction(tr("&Left Align"), this);
|
---|
| 293 | leftAlignAct->setCheckable(true);
|
---|
| 294 | leftAlignAct->setShortcut(tr("Ctrl+L"));
|
---|
| 295 | leftAlignAct->setStatusTip(tr("Left align the selected text"));
|
---|
| 296 | connect(leftAlignAct, SIGNAL(triggered()), this, SLOT(leftAlign()));
|
---|
| 297 |
|
---|
| 298 | rightAlignAct = new QAction(tr("&Right Align"), this);
|
---|
| 299 | rightAlignAct->setCheckable(true);
|
---|
| 300 | rightAlignAct->setShortcut(tr("Ctrl+R"));
|
---|
| 301 | rightAlignAct->setStatusTip(tr("Right align the selected text"));
|
---|
| 302 | connect(rightAlignAct, SIGNAL(triggered()), this, SLOT(rightAlign()));
|
---|
| 303 |
|
---|
| 304 | justifyAct = new QAction(tr("&Justify"), this);
|
---|
| 305 | justifyAct->setCheckable(true);
|
---|
| 306 | justifyAct->setShortcut(tr("Ctrl+J"));
|
---|
| 307 | justifyAct->setStatusTip(tr("Justify the selected text"));
|
---|
| 308 | connect(justifyAct, SIGNAL(triggered()), this, SLOT(justify()));
|
---|
| 309 |
|
---|
| 310 | centerAct = new QAction(tr("&Center"), this);
|
---|
| 311 | centerAct->setCheckable(true);
|
---|
| 312 | centerAct->setShortcut(tr("Ctrl+E"));
|
---|
| 313 | centerAct->setStatusTip(tr("Center the selected text"));
|
---|
| 314 | connect(centerAct, SIGNAL(triggered()), this, SLOT(center()));
|
---|
| 315 |
|
---|
| 316 | //! [6] //! [7]
|
---|
| 317 | alignmentGroup = new QActionGroup(this);
|
---|
| 318 | alignmentGroup->addAction(leftAlignAct);
|
---|
| 319 | alignmentGroup->addAction(rightAlignAct);
|
---|
| 320 | alignmentGroup->addAction(justifyAct);
|
---|
| 321 | alignmentGroup->addAction(centerAct);
|
---|
| 322 | leftAlignAct->setChecked(true);
|
---|
| 323 | //! [6]
|
---|
| 324 | }
|
---|
| 325 | //! [7]
|
---|
| 326 |
|
---|
| 327 | //! [8]
|
---|
| 328 | void MainWindow::createMenus()
|
---|
| 329 | {
|
---|
| 330 | //! [9] //! [10]
|
---|
| 331 | fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
| 332 | fileMenu->addAction(newAct);
|
---|
| 333 | //! [9]
|
---|
| 334 | fileMenu->addAction(openAct);
|
---|
| 335 | //! [10]
|
---|
| 336 | fileMenu->addAction(saveAct);
|
---|
| 337 | fileMenu->addAction(printAct);
|
---|
| 338 | //! [11]
|
---|
| 339 | fileMenu->addSeparator();
|
---|
| 340 | //! [11]
|
---|
| 341 | fileMenu->addAction(exitAct);
|
---|
| 342 |
|
---|
| 343 | editMenu = menuBar()->addMenu(tr("&Edit"));
|
---|
| 344 | editMenu->addAction(undoAct);
|
---|
| 345 | editMenu->addAction(redoAct);
|
---|
| 346 | editMenu->addSeparator();
|
---|
| 347 | editMenu->addAction(cutAct);
|
---|
| 348 | editMenu->addAction(copyAct);
|
---|
| 349 | editMenu->addAction(pasteAct);
|
---|
| 350 | editMenu->addSeparator();
|
---|
| 351 |
|
---|
| 352 | helpMenu = menuBar()->addMenu(tr("&Help"));
|
---|
| 353 | helpMenu->addAction(aboutAct);
|
---|
| 354 | helpMenu->addAction(aboutQtAct);
|
---|
| 355 | //! [8]
|
---|
| 356 |
|
---|
| 357 | //! [12]
|
---|
| 358 | formatMenu = editMenu->addMenu(tr("&Format"));
|
---|
| 359 | formatMenu->addAction(boldAct);
|
---|
| 360 | formatMenu->addAction(italicAct);
|
---|
| 361 | formatMenu->addSeparator()->setText(tr("Alignment"));
|
---|
| 362 | formatMenu->addAction(leftAlignAct);
|
---|
| 363 | formatMenu->addAction(rightAlignAct);
|
---|
| 364 | formatMenu->addAction(justifyAct);
|
---|
| 365 | formatMenu->addAction(centerAct);
|
---|
| 366 | formatMenu->addSeparator();
|
---|
| 367 | formatMenu->addAction(setLineSpacingAct);
|
---|
| 368 | formatMenu->addAction(setParagraphSpacingAct);
|
---|
| 369 | }
|
---|
| 370 | //! [12]
|
---|