examples/mainwindows/menus/mainwindow.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "mainwindow.h"
       
    45 
       
    46 //! [0]
       
    47 MainWindow::MainWindow()
       
    48 {
       
    49     QWidget *widget = new QWidget;
       
    50     setCentralWidget(widget);
       
    51 //! [0]
       
    52 
       
    53 //! [1]
       
    54     QWidget *topFiller = new QWidget;
       
    55     topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
    56 
       
    57     infoLabel = new QLabel(tr("<i>Choose a menu option, or right-click to "
       
    58                               "invoke a context menu</i>"));
       
    59     infoLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
       
    60     infoLabel->setAlignment(Qt::AlignCenter);
       
    61 
       
    62     QWidget *bottomFiller = new QWidget;
       
    63     bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
    64 
       
    65     QVBoxLayout *layout = new QVBoxLayout;
       
    66     layout->setMargin(5);
       
    67     layout->addWidget(topFiller);
       
    68     layout->addWidget(infoLabel);
       
    69     layout->addWidget(bottomFiller);
       
    70     widget->setLayout(layout);
       
    71 //! [1]
       
    72 
       
    73 //! [2]
       
    74     createActions();
       
    75     createMenus();
       
    76 
       
    77     QString message = tr("A context menu is available by right-clicking");
       
    78     statusBar()->showMessage(message);
       
    79 
       
    80     setWindowTitle(tr("Menus"));
       
    81     setMinimumSize(160, 160);
       
    82     resize(480, 320);
       
    83 }
       
    84 //! [2]
       
    85 
       
    86 //! [3]
       
    87 void MainWindow::contextMenuEvent(QContextMenuEvent *event)
       
    88 {
       
    89     QMenu menu(this);
       
    90     menu.addAction(cutAct);
       
    91     menu.addAction(copyAct);
       
    92     menu.addAction(pasteAct);
       
    93     menu.exec(event->globalPos());
       
    94 }
       
    95 //! [3]
       
    96 
       
    97 void MainWindow::newFile()
       
    98 {
       
    99     infoLabel->setText(tr("Invoked <b>File|New</b>"));
       
   100 }
       
   101 
       
   102 void MainWindow::open()
       
   103 {
       
   104     infoLabel->setText(tr("Invoked <b>File|Open</b>"));
       
   105 }
       
   106 
       
   107 void MainWindow::save()
       
   108 {
       
   109     infoLabel->setText(tr("Invoked <b>File|Save</b>"));
       
   110 }
       
   111 
       
   112 void MainWindow::print()
       
   113 {
       
   114     infoLabel->setText(tr("Invoked <b>File|Print</b>"));
       
   115 }
       
   116 
       
   117 void MainWindow::undo()
       
   118 {
       
   119     infoLabel->setText(tr("Invoked <b>Edit|Undo</b>"));
       
   120 }
       
   121 
       
   122 void MainWindow::redo()
       
   123 {
       
   124     infoLabel->setText(tr("Invoked <b>Edit|Redo</b>"));
       
   125 }
       
   126 
       
   127 void MainWindow::cut()
       
   128 {
       
   129     infoLabel->setText(tr("Invoked <b>Edit|Cut</b>"));
       
   130 }
       
   131 
       
   132 void MainWindow::copy()
       
   133 {
       
   134     infoLabel->setText(tr("Invoked <b>Edit|Copy</b>"));
       
   135 }
       
   136 
       
   137 void MainWindow::paste()
       
   138 {
       
   139     infoLabel->setText(tr("Invoked <b>Edit|Paste</b>"));
       
   140 }
       
   141 
       
   142 void MainWindow::bold()
       
   143 {
       
   144     infoLabel->setText(tr("Invoked <b>Edit|Format|Bold</b>"));
       
   145 }
       
   146 
       
   147 void MainWindow::italic()
       
   148 {
       
   149     infoLabel->setText(tr("Invoked <b>Edit|Format|Italic</b>"));
       
   150 }
       
   151 
       
   152 void MainWindow::leftAlign()
       
   153 {
       
   154     infoLabel->setText(tr("Invoked <b>Edit|Format|Left Align</b>"));
       
   155 }
       
   156 
       
   157 void MainWindow::rightAlign()
       
   158 {
       
   159     infoLabel->setText(tr("Invoked <b>Edit|Format|Right Align</b>"));
       
   160 }
       
   161 
       
   162 void MainWindow::justify()
       
   163 {
       
   164     infoLabel->setText(tr("Invoked <b>Edit|Format|Justify</b>"));
       
   165 }
       
   166 
       
   167 void MainWindow::center()
       
   168 {
       
   169     infoLabel->setText(tr("Invoked <b>Edit|Format|Center</b>"));
       
   170 }
       
   171 
       
   172 void MainWindow::setLineSpacing()
       
   173 {
       
   174     infoLabel->setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>"));
       
   175 }
       
   176 
       
   177 void MainWindow::setParagraphSpacing()
       
   178 {
       
   179     infoLabel->setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>"));
       
   180 }
       
   181 
       
   182 void MainWindow::about()
       
   183 {
       
   184     infoLabel->setText(tr("Invoked <b>Help|About</b>"));
       
   185     QMessageBox::about(this, tr("About Menu"),
       
   186             tr("The <b>Menu</b> example shows how to create "
       
   187                "menu-bar menus and context menus."));
       
   188 }
       
   189 
       
   190 void MainWindow::aboutQt()
       
   191 {
       
   192     infoLabel->setText(tr("Invoked <b>Help|About Qt</b>"));
       
   193 }
       
   194 
       
   195 //! [4]
       
   196 void MainWindow::createActions()
       
   197 {
       
   198 //! [5]
       
   199     newAct = new QAction(tr("&New"), this);
       
   200     newAct->setShortcuts(QKeySequence::New);
       
   201     newAct->setStatusTip(tr("Create a new file"));
       
   202     connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
       
   203 //! [4]
       
   204 
       
   205     openAct = new QAction(tr("&Open..."), this);
       
   206     openAct->setShortcuts(QKeySequence::Open);
       
   207     openAct->setStatusTip(tr("Open an existing file"));
       
   208     connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
       
   209 //! [5]
       
   210 
       
   211     saveAct = new QAction(tr("&Save"), this);
       
   212     saveAct->setShortcuts(QKeySequence::Save);
       
   213     saveAct->setStatusTip(tr("Save the document to disk"));
       
   214     connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
       
   215 
       
   216     printAct = new QAction(tr("&Print..."), this);
       
   217     printAct->setShortcuts(QKeySequence::Print);
       
   218     printAct->setStatusTip(tr("Print the document"));
       
   219     connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
       
   220 
       
   221     exitAct = new QAction(tr("E&xit"), this);
       
   222     exitAct->setShortcuts(QKeySequence::Quit);
       
   223     exitAct->setStatusTip(tr("Exit the application"));
       
   224     connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
       
   225 
       
   226     undoAct = new QAction(tr("&Undo"), this);
       
   227     undoAct->setShortcuts(QKeySequence::Undo);
       
   228     undoAct->setStatusTip(tr("Undo the last operation"));
       
   229     connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
       
   230 
       
   231     redoAct = new QAction(tr("&Redo"), this);
       
   232     redoAct->setShortcuts(QKeySequence::Redo);
       
   233     redoAct->setStatusTip(tr("Redo the last operation"));
       
   234     connect(redoAct, SIGNAL(triggered()), this, SLOT(redo()));
       
   235 
       
   236     cutAct = new QAction(tr("Cu&t"), this);
       
   237     cutAct->setShortcuts(QKeySequence::Cut);
       
   238     cutAct->setStatusTip(tr("Cut the current selection's contents to the "
       
   239                             "clipboard"));
       
   240     connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
       
   241 
       
   242     copyAct = new QAction(tr("&Copy"), this);
       
   243     copyAct->setShortcuts(QKeySequence::Copy);
       
   244     copyAct->setStatusTip(tr("Copy the current selection's contents to the "
       
   245                              "clipboard"));
       
   246     connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
       
   247 
       
   248     pasteAct = new QAction(tr("&Paste"), this);
       
   249     pasteAct->setShortcuts(QKeySequence::Paste);
       
   250     pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
       
   251                               "selection"));
       
   252     connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
       
   253 
       
   254     boldAct = new QAction(tr("&Bold"), this);
       
   255     boldAct->setCheckable(true);
       
   256     boldAct->setShortcut(QKeySequence::Bold);
       
   257     boldAct->setStatusTip(tr("Make the text bold"));
       
   258     connect(boldAct, SIGNAL(triggered()), this, SLOT(bold()));
       
   259 
       
   260     QFont boldFont = boldAct->font();
       
   261     boldFont.setBold(true);
       
   262     boldAct->setFont(boldFont);
       
   263 
       
   264     italicAct = new QAction(tr("&Italic"), this);
       
   265     italicAct->setCheckable(true);
       
   266     italicAct->setShortcut(QKeySequence::Italic);
       
   267     italicAct->setStatusTip(tr("Make the text italic"));
       
   268     connect(italicAct, SIGNAL(triggered()), this, SLOT(italic()));
       
   269 
       
   270     QFont italicFont = italicAct->font();
       
   271     italicFont.setItalic(true);
       
   272     italicAct->setFont(italicFont);
       
   273 
       
   274     setLineSpacingAct = new QAction(tr("Set &Line Spacing..."), this);
       
   275     setLineSpacingAct->setStatusTip(tr("Change the gap between the lines of a "
       
   276                                        "paragraph"));
       
   277     connect(setLineSpacingAct, SIGNAL(triggered()), this, SLOT(setLineSpacing()));
       
   278 
       
   279     setParagraphSpacingAct = new QAction(tr("Set &Paragraph Spacing..."), this);
       
   280     setLineSpacingAct->setStatusTip(tr("Change the gap between paragraphs"));
       
   281     connect(setParagraphSpacingAct, SIGNAL(triggered()),
       
   282             this, SLOT(setParagraphSpacing()));
       
   283 
       
   284     aboutAct = new QAction(tr("&About"), this);
       
   285     aboutAct->setStatusTip(tr("Show the application's About box"));
       
   286     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
       
   287 
       
   288     aboutQtAct = new QAction(tr("About &Qt"), this);
       
   289     aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
       
   290     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
       
   291     connect(aboutQtAct, SIGNAL(triggered()), this, SLOT(aboutQt()));
       
   292 
       
   293     leftAlignAct = new QAction(tr("&Left Align"), this);
       
   294     leftAlignAct->setCheckable(true);
       
   295     leftAlignAct->setShortcut(tr("Ctrl+L"));
       
   296     leftAlignAct->setStatusTip(tr("Left align the selected text"));
       
   297     connect(leftAlignAct, SIGNAL(triggered()), this, SLOT(leftAlign()));
       
   298 
       
   299     rightAlignAct = new QAction(tr("&Right Align"), this);
       
   300     rightAlignAct->setCheckable(true);
       
   301     rightAlignAct->setShortcut(tr("Ctrl+R"));
       
   302     rightAlignAct->setStatusTip(tr("Right align the selected text"));
       
   303     connect(rightAlignAct, SIGNAL(triggered()), this, SLOT(rightAlign()));
       
   304 
       
   305     justifyAct = new QAction(tr("&Justify"), this);
       
   306     justifyAct->setCheckable(true);
       
   307     justifyAct->setShortcut(tr("Ctrl+J"));
       
   308     justifyAct->setStatusTip(tr("Justify the selected text"));
       
   309     connect(justifyAct, SIGNAL(triggered()), this, SLOT(justify()));
       
   310 
       
   311     centerAct = new QAction(tr("&Center"), this);
       
   312     centerAct->setCheckable(true);
       
   313     centerAct->setShortcut(tr("Ctrl+E"));
       
   314     centerAct->setStatusTip(tr("Center the selected text"));
       
   315     connect(centerAct, SIGNAL(triggered()), this, SLOT(center()));
       
   316 
       
   317 //! [6] //! [7]
       
   318     alignmentGroup = new QActionGroup(this);
       
   319     alignmentGroup->addAction(leftAlignAct);
       
   320     alignmentGroup->addAction(rightAlignAct);
       
   321     alignmentGroup->addAction(justifyAct);
       
   322     alignmentGroup->addAction(centerAct);
       
   323     leftAlignAct->setChecked(true);
       
   324 //! [6]
       
   325 }
       
   326 //! [7]
       
   327 
       
   328 //! [8]
       
   329 void MainWindow::createMenus()
       
   330 {
       
   331 //! [9] //! [10]
       
   332     fileMenu = menuBar()->addMenu(tr("&File"));
       
   333     fileMenu->addAction(newAct);
       
   334 //! [9]
       
   335     fileMenu->addAction(openAct);
       
   336 //! [10]
       
   337     fileMenu->addAction(saveAct);
       
   338     fileMenu->addAction(printAct);
       
   339 //! [11]
       
   340     fileMenu->addSeparator();
       
   341 //! [11]
       
   342     fileMenu->addAction(exitAct);
       
   343 
       
   344     editMenu = menuBar()->addMenu(tr("&Edit"));
       
   345     editMenu->addAction(undoAct);
       
   346     editMenu->addAction(redoAct);
       
   347     editMenu->addSeparator();
       
   348     editMenu->addAction(cutAct);
       
   349     editMenu->addAction(copyAct);
       
   350     editMenu->addAction(pasteAct);
       
   351     editMenu->addSeparator();
       
   352 
       
   353     helpMenu = menuBar()->addMenu(tr("&Help"));
       
   354     helpMenu->addAction(aboutAct);
       
   355     helpMenu->addAction(aboutQtAct);
       
   356 //! [8]
       
   357 
       
   358 //! [12]
       
   359     formatMenu = editMenu->addMenu(tr("&Format"));
       
   360     formatMenu->addAction(boldAct);
       
   361     formatMenu->addAction(italicAct);
       
   362     formatMenu->addSeparator()->setText(tr("Alignment"));
       
   363     formatMenu->addAction(leftAlignAct);
       
   364     formatMenu->addAction(rightAlignAct);
       
   365     formatMenu->addAction(justifyAct);
       
   366     formatMenu->addAction(centerAct);
       
   367     formatMenu->addSeparator();
       
   368     formatMenu->addAction(setLineSpacingAct);
       
   369     formatMenu->addAction(setParagraphSpacingAct);
       
   370 }
       
   371 //! [12]