~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/mainwindows/menus/mainwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include "mainwindow.h"
 
32
 
 
33
MainWindow::MainWindow()
 
34
{
 
35
    QWidget *w = new QWidget;
 
36
    setCentralWidget(w);
 
37
 
 
38
    QWidget *topFiller = new QWidget;
 
39
    topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
40
 
 
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);
 
45
 
 
46
    QWidget *bottomFiller = new QWidget;
 
47
    bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
48
 
 
49
    QVBoxLayout *vbox = new QVBoxLayout;
 
50
    vbox->setMargin(5);
 
51
    vbox->addWidget(topFiller);
 
52
    vbox->addWidget(infoLabel);
 
53
    vbox->addWidget(bottomFiller);
 
54
    w->setLayout(vbox);
 
55
 
 
56
    createActions();
 
57
    createMenus();
 
58
 
 
59
    statusBar()->showMessage(tr("A context menu is available by right-clicking"));
 
60
 
 
61
    setWindowTitle(tr("Menus"));
 
62
    setMinimumSize(160, 160);
 
63
    resize(480, 320);
 
64
}
 
65
 
 
66
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
 
67
{
 
68
    QMenu menu(this);
 
69
    menu.addAction(cutAct);
 
70
    menu.addAction(copyAct);
 
71
    menu.addAction(pasteAct);
 
72
    menu.exec(event->globalPos());
 
73
}
 
74
 
 
75
void MainWindow::newFile()
 
76
{
 
77
    infoLabel->setText(tr("Invoked <b>File|New</b>"));
 
78
}
 
79
 
 
80
void MainWindow::open()
 
81
{
 
82
    infoLabel->setText(tr("Invoked <b>File|Open</b>"));
 
83
}
 
84
 
 
85
void MainWindow::save()
 
86
{
 
87
    infoLabel->setText(tr("Invoked <b>File|Save</b>"));
 
88
}
 
89
 
 
90
void MainWindow::print()
 
91
{
 
92
    infoLabel->setText(tr("Invoked <b>File|Print</b>"));
 
93
}
 
94
 
 
95
void MainWindow::undo()
 
96
{
 
97
    infoLabel->setText(tr("Invoked <b>Edit|Undo</b>"));
 
98
}
 
99
 
 
100
void MainWindow::redo()
 
101
{
 
102
    infoLabel->setText(tr("Invoked <b>Edit|Redo</b>"));
 
103
}
 
104
 
 
105
void MainWindow::cut()
 
106
{
 
107
    infoLabel->setText(tr("Invoked <b>Edit|Cut</b>"));
 
108
}
 
109
 
 
110
void MainWindow::copy()
 
111
{
 
112
    infoLabel->setText(tr("Invoked <b>Edit|Copy</b>"));
 
113
}
 
114
 
 
115
void MainWindow::paste()
 
116
{
 
117
    infoLabel->setText(tr("Invoked <b>Edit|Paste</b>"));
 
118
}
 
119
 
 
120
void MainWindow::bold()
 
121
{
 
122
    infoLabel->setText(tr("Invoked <b>Edit|Format|Bold</b>"));
 
123
}
 
124
 
 
125
void MainWindow::italic()
 
126
{
 
127
    infoLabel->setText(tr("Invoked <b>Edit|Format|Italic</b>"));
 
128
}
 
129
 
 
130
void MainWindow::leftAlign()
 
131
{
 
132
    infoLabel->setText(tr("Invoked <b>Edit|Format|Left Align</b>"));
 
133
}
 
134
 
 
135
void MainWindow::rightAlign()
 
136
{
 
137
    infoLabel->setText(tr("Invoked <b>Edit|Format|Right Align</b>"));
 
138
}
 
139
 
 
140
void MainWindow::justify()
 
141
{
 
142
    infoLabel->setText(tr("Invoked <b>Edit|Format|Justify</b>"));
 
143
}
 
144
 
 
145
void MainWindow::center()
 
146
{
 
147
    infoLabel->setText(tr("Invoked <b>Edit|Format|Center</b>"));
 
148
}
 
149
 
 
150
void MainWindow::setLineSpacing()
 
151
{
 
152
    infoLabel->setText(tr("Invoked <b>Edit|Format|Set Line Spacing</b>"));
 
153
}
 
154
 
 
155
void MainWindow::setParagraphSpacing()
 
156
{
 
157
    infoLabel->setText(tr("Invoked <b>Edit|Format|Set Paragraph Spacing</b>"));
 
158
}
 
159
 
 
160
void MainWindow::about()
 
161
{
 
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."));
 
166
}
 
167
 
 
168
void MainWindow::aboutQt()
 
169
{
 
170
    infoLabel->setText(tr("Invoked <b>Help|About Qt</b>"));
 
171
}
 
172
 
 
173
void MainWindow::createActions()
 
174
{
 
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()));
 
179
 
 
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()));
 
184
 
 
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()));
 
189
 
 
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()));
 
194
 
 
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()));
 
199
 
 
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()));
 
204
 
 
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()));
 
209
 
 
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 "
 
213
                            "clipboard"));
 
214
    connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
 
215
 
 
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 "
 
219
                             "clipboard"));
 
220
    connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
 
221
 
 
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 "
 
225
                              "selection"));
 
226
    connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
 
227
 
 
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()));
 
233
 
 
234
    QFont boldFont = boldAct->font();
 
235
    boldFont.setBold(true);
 
236
    boldAct->setFont(boldFont);
 
237
 
 
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()));
 
243
 
 
244
    QFont italicFont = italicAct->font();
 
245
    italicFont.setItalic(true);
 
246
    italicAct->setFont(italicFont);
 
247
 
 
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()));
 
253
 
 
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()));
 
259
 
 
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()));
 
265
 
 
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()));
 
271
 
 
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);
 
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
 
 
299
void MainWindow::createMenus()
 
300
{
 
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);
 
308
 
 
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();
 
317
 
 
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);
 
329
 
 
330
    helpMenu = menuBar()->addMenu(tr("&Help"));
 
331
    helpMenu->addAction(aboutAct);
 
332
    helpMenu->addAction(aboutQtAct);
 
333
}