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