1
/****************************************************************************
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "mainwindow.h"
33
MainWindow::MainWindow()
35
QWidget *w = new QWidget;
38
QWidget *topFiller = new QWidget;
39
topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
41
infoLabel = new QLabel(tr("<i>Choose a menu option, or right-click to "
42
"invoke a context menu</i>"));
43
infoLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
44
infoLabel->setAlignment(Qt::AlignCenter);
46
QWidget *bottomFiller = new QWidget;
47
bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
49
QVBoxLayout *vbox = new QVBoxLayout;
51
vbox->addWidget(topFiller);
52
vbox->addWidget(infoLabel);
53
vbox->addWidget(bottomFiller);
59
statusBar()->showMessage(tr("A context menu is available by right-clicking"));
61
setWindowTitle(tr("Menus"));
62
setMinimumSize(160, 160);
66
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
69
menu.addAction(cutAct);
70
menu.addAction(copyAct);
71
menu.addAction(pasteAct);
72
menu.exec(event->globalPos());
75
void MainWindow::newFile()
77
infoLabel->setText(tr("Invoked <b>File|New</b>"));
80
void MainWindow::open()
82
infoLabel->setText(tr("Invoked <b>File|Open</b>"));
85
void MainWindow::save()
87
infoLabel->setText(tr("Invoked <b>File|Save</b>"));
90
void MainWindow::print()
92
infoLabel->setText(tr("Invoked <b>File|Print</b>"));
95
void MainWindow::undo()
97
infoLabel->setText(tr("Invoked <b>Edit|Undo</b>"));
100
void MainWindow::redo()
102
infoLabel->setText(tr("Invoked <b>Edit|Redo</b>"));
105
void MainWindow::cut()
107
infoLabel->setText(tr("Invoked <b>Edit|Cut</b>"));
110
void MainWindow::copy()
112
infoLabel->setText(tr("Invoked <b>Edit|Copy</b>"));
115
void MainWindow::paste()
117
infoLabel->setText(tr("Invoked <b>Edit|Paste</b>"));
120
void MainWindow::bold()
122
infoLabel->setText(tr("Invoked <b>Edit|Format|Bold</b>"));
125
void MainWindow::italic()
127
infoLabel->setText(tr("Invoked <b>Edit|Format|Italic</b>"));
130
void MainWindow::leftAlign()
132
infoLabel->setText(tr("Invoked <b>Edit|Format|Left Align</b>"));
135
void MainWindow::rightAlign()
137
infoLabel->setText(tr("Invoked <b>Edit|Format|Right Align</b>"));
140
void MainWindow::justify()
142
infoLabel->setText(tr("Invoked <b>Edit|Format|Justify</b>"));
145
void MainWindow::center()
147
infoLabel->setText(tr("Invoked <b>Edit|Format|Center</b>"));
150
void MainWindow::setLineSpacing()
152
infoLabel->setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>"));
155
void MainWindow::setParagraphSpacing()
157
infoLabel->setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>"));
160
void MainWindow::about()
162
infoLabel->setText(tr("Invoked <b>Help|About</b>"));
163
QMessageBox::about(this, tr("About Menu"),
164
tr("The <b>Menu</b> example shows how to create "
165
"menu-bar menus and context menus."));
168
void MainWindow::aboutQt()
170
infoLabel->setText(tr("Invoked <b>Help|About Qt</b>"));
173
void MainWindow::createActions()
175
newAct = new QAction(tr("&New"), this);
176
newAct->setShortcut(tr("Ctrl+N"));
177
newAct->setStatusTip(tr("Create a new file"));
178
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
180
openAct = new QAction(tr("&Open..."), this);
181
openAct->setShortcut(tr("Ctrl+O"));
182
openAct->setStatusTip(tr("Open an existing file"));
183
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
185
saveAct = new QAction(tr("&Save"), this);
186
saveAct->setShortcut(tr("Ctrl+S"));
187
saveAct->setStatusTip(tr("Save the document to disk"));
188
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
190
printAct = new QAction(tr("&Print..."), this);
191
printAct->setShortcut(tr("Ctrl+P"));
192
printAct->setStatusTip(tr("Print the document"));
193
connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
195
exitAct = new QAction(tr("E&xit"), this);
196
exitAct->setShortcut(tr("Ctrl+Q"));
197
exitAct->setStatusTip(tr("Exit the application"));
198
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
200
undoAct = new QAction(tr("&Undo"), this);
201
undoAct->setShortcut(tr("Ctrl+Z"));
202
undoAct->setStatusTip(tr("Undo the last operation"));
203
connect(undoAct, SIGNAL(triggered()), this, SLOT(undo()));
205
redoAct = new QAction(tr("&Redo"), this);
206
redoAct->setShortcut(tr("Ctrl+Y"));
207
redoAct->setStatusTip(tr("Redo the last operation"));
208
connect(redoAct, SIGNAL(triggered()), this, SLOT(redo()));
210
cutAct = new QAction(tr("Cu&t"), this);
211
cutAct->setShortcut(tr("Ctrl+X"));
212
cutAct->setStatusTip(tr("Cut the current selection's contents to the "
214
connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
216
copyAct = new QAction(tr("&Copy"), this);
217
copyAct->setShortcut(tr("Ctrl+C"));
218
copyAct->setStatusTip(tr("Copy the current selection's contents to the "
220
connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
222
pasteAct = new QAction(tr("&Paste"), this);
223
pasteAct->setShortcut(tr("Ctrl+V"));
224
pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
226
connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
228
boldAct = new QAction(tr("&Bold"), this);
229
boldAct->setCheckable(true);
230
boldAct->setShortcut(tr("Ctrl+B"));
231
boldAct->setStatusTip(tr("Make the text bold"));
232
connect(boldAct, SIGNAL(triggered()), this, SLOT(bold()));
234
QFont boldFont = boldAct->font();
235
boldFont.setBold(true);
236
boldAct->setFont(boldFont);
238
italicAct = new QAction(tr("&Italic"), this);
239
italicAct->setCheckable(true);
240
italicAct->setShortcut(tr("Ctrl+I"));
241
italicAct->setStatusTip(tr("Make the text italic"));
242
connect(italicAct, SIGNAL(triggered()), this, SLOT(italic()));
244
QFont italicFont = italicAct->font();
245
italicFont.setItalic(true);
246
italicAct->setFont(italicFont);
248
leftAlignAct = new QAction(tr("&Left Align"), this);
249
leftAlignAct->setCheckable(true);
250
leftAlignAct->setShortcut(tr("Ctrl+L"));
251
leftAlignAct->setStatusTip(tr("Left align the selected text"));
252
connect(leftAlignAct, SIGNAL(triggered()), this, SLOT(leftAlign()));
254
rightAlignAct = new QAction(tr("&Right Align"), this);
255
rightAlignAct->setCheckable(true);
256
rightAlignAct->setShortcut(tr("Ctrl+R"));
257
rightAlignAct->setStatusTip(tr("Right align the selected text"));
258
connect(rightAlignAct, SIGNAL(triggered()), this, SLOT(rightAlign()));
260
justifyAct = new QAction(tr("&Justify"), this);
261
justifyAct->setCheckable(true);
262
justifyAct->setShortcut(tr("Ctrl+J"));
263
justifyAct->setStatusTip(tr("Justify the selected text"));
264
connect(justifyAct, SIGNAL(triggered()), this, SLOT(justify()));
266
centerAct = new QAction(tr("&Center"), this);
267
centerAct->setCheckable(true);
268
centerAct->setShortcut(tr("Ctrl+E"));
269
centerAct->setStatusTip(tr("Center the selected text"));
270
connect(centerAct, SIGNAL(triggered()), this, SLOT(center()));
272
alignmentGroup = new QActionGroup(this);
273
alignmentGroup->addAction(leftAlignAct);
274
alignmentGroup->addAction(rightAlignAct);
275
alignmentGroup->addAction(justifyAct);
276
alignmentGroup->addAction(centerAct);
277
leftAlignAct->setChecked(true);
279
setLineSpacingAct = new QAction(tr("Set &Line Spacing..."), this);
280
setLineSpacingAct->setStatusTip(tr("Change the gap between the lines of a "
282
connect(setLineSpacingAct, SIGNAL(triggered()), this, SLOT(setLineSpacing()));
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()));
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()));
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()));
299
void MainWindow::createMenus()
301
fileMenu = menuBar()->addMenu(tr("&File"));
302
fileMenu->addAction(newAct);
303
fileMenu->addAction(openAct);
304
fileMenu->addAction(saveAct);
305
fileMenu->addAction(printAct);
306
fileMenu->addSeparator();
307
fileMenu->addAction(exitAct);
309
editMenu = menuBar()->addMenu(tr("&Edit"));
310
editMenu->addAction(undoAct);
311
editMenu->addAction(redoAct);
312
editMenu->addSeparator();
313
editMenu->addAction(cutAct);
314
editMenu->addAction(copyAct);
315
editMenu->addAction(pasteAct);
316
editMenu->addSeparator();
318
formatMenu = editMenu->addMenu(tr("&Format"));
319
formatMenu->addAction(boldAct);
320
formatMenu->addAction(italicAct);
321
formatMenu->addSeparator();
322
formatMenu->addAction(leftAlignAct);
323
formatMenu->addAction(rightAlignAct);
324
formatMenu->addAction(justifyAct);
325
formatMenu->addAction(centerAct);
326
formatMenu->addSeparator();
327
formatMenu->addAction(setLineSpacingAct);
328
formatMenu->addAction(setParagraphSpacingAct);
330
helpMenu = menuBar()->addMenu(tr("&Help"));
331
helpMenu->addAction(aboutAct);
332
helpMenu->addAction(aboutQtAct);