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