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

« back to all changes in this revision

Viewing changes to doc/html/widgets-icons-mainwindow-cpp.html

  • 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
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<head>
 
6
    <title>Qt 4.0: mainwindow.cpp Example File (widgets/icons/mainwindow.cpp)</title>
 
7
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
8
a:link { color: #004faf; text-decoration: none }
 
9
a:visited { color: #672967; text-decoration: none }
 
10
td.postheader { font-family: sans-serif }
 
11
tr.address { font-family: sans-serif }
 
12
body { background: #ffffff; color: black; }</style>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
18
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">mainwindow.cpp Example File<br /><small><small>widgets/icons/mainwindow.cpp</small></small></h1>
 
20
<pre>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
23
    **
 
24
    ** This file is part of the documentation of the Qt Toolkit.
 
25
    **
 
26
    ** This file may be distributed under the terms of the Q Public License
 
27
** as defined by Trolltech AS of Norway and appearing in the file
 
28
** LICENSE.QPL included in the packaging of this file.
 
29
**
 
30
** This file may be distributed and/or modified under the terms of the
 
31
** GNU General Public License version 2 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.
 
34
**
 
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
36
**   information about Qt Commercial License Agreements.
 
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
39
**
 
40
** Contact info@trolltech.com if any conditions of this licensing are
 
41
** not clear to you.
 
42
    **
 
43
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
44
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
45
    **
 
46
    ****************************************************************************/
 
47
 
 
48
    #include &lt;QtGui&gt;
 
49
 
 
50
    #include &quot;iconpreviewarea.h&quot;
 
51
    #include &quot;iconsizespinbox.h&quot;
 
52
    #include &quot;imagedelegate.h&quot;
 
53
    #include &quot;mainwindow.h&quot;
 
54
 
 
55
    MainWindow::MainWindow()
 
56
    {
 
57
        centralWidget = new QWidget;
 
58
        setCentralWidget(centralWidget);
 
59
 
 
60
        createPreviewGroupBox();
 
61
        createImagesGroupBox();
 
62
        createIconSizeGroupBox();
 
63
 
 
64
        createActions();
 
65
        createMenus();
 
66
        createContextMenu();
 
67
 
 
68
        QGridLayout *mainLayout = new QGridLayout;
 
69
        mainLayout-&gt;addWidget(imagesGroupBox, 0, 0);
 
70
        mainLayout-&gt;addWidget(iconSizeGroupBox, 1, 0);
 
71
        mainLayout-&gt;addWidget(previewGroupBox, 0, 1, 2, 1);
 
72
        centralWidget-&gt;setLayout(mainLayout);
 
73
 
 
74
        setWindowTitle(tr(&quot;Icons&quot;));
 
75
        checkCurrentStyle();
 
76
        otherRadioButton-&gt;click();
 
77
 
 
78
        resize(860, 400);
 
79
    }
 
80
 
 
81
    void MainWindow::about()
 
82
    {
 
83
        QMessageBox::about(this, tr(&quot;About Icons&quot;),
 
84
                tr(&quot;The &lt;b&gt;Icons&lt;/b&gt; example illustrates how Qt renders an icon in &quot;
 
85
                   &quot;different modes (active, normal, and disabled) and states (on &quot;
 
86
                   &quot;and off) based on a set of images.&quot;));
 
87
    }
 
88
 
 
89
    void MainWindow::changeStyle(bool checked)
 
90
    {
 
91
        if (!checked)
 
92
            return;
 
93
 
 
94
        QAction *action = qobject_cast&lt;QAction *&gt;(sender());
 
95
        QStyle *style = QStyleFactory::create(action-&gt;data().toString());
 
96
        Q_ASSERT(style);
 
97
        QApplication::setStyle(style);
 
98
 
 
99
        smallRadioButton-&gt;setText(tr(&quot;Small (%1 ļæ½ %1)&quot;)
 
100
                .arg(style-&gt;pixelMetric(QStyle::PM_SmallIconSize)));
 
101
        largeRadioButton-&gt;setText(tr(&quot;Large (%1 ļæ½ %1)&quot;)
 
102
                .arg(style-&gt;pixelMetric(QStyle::PM_LargeIconSize)));
 
103
        toolBarRadioButton-&gt;setText(tr(&quot;Toolbars (%1 ļæ½ %1)&quot;)
 
104
                .arg(style-&gt;pixelMetric(QStyle::PM_ToolBarIconSize)));
 
105
        listViewRadioButton-&gt;setText(tr(&quot;List views (%1 ļæ½ %1)&quot;)
 
106
                .arg(style-&gt;pixelMetric(QStyle::PM_ListViewIconSize)));
 
107
        iconViewRadioButton-&gt;setText(tr(&quot;Icon views (%1 ļæ½ %1)&quot;)
 
108
                .arg(style-&gt;pixelMetric(QStyle::PM_IconViewIconSize)));
 
109
 
 
110
        changeSize();
 
111
    }
 
112
 
 
113
    void MainWindow::changeSize()
 
114
    {
 
115
        int extent;
 
116
 
 
117
        if (otherRadioButton-&gt;isChecked()) {
 
118
            extent = otherSpinBox-&gt;value();
 
119
        } else {
 
120
            QStyle::PixelMetric metric;
 
121
 
 
122
            if (smallRadioButton-&gt;isChecked()) {
 
123
                metric = QStyle::PM_SmallIconSize;
 
124
            } else if (largeRadioButton-&gt;isChecked()) {
 
125
                metric = QStyle::PM_LargeIconSize;
 
126
            } else if (toolBarRadioButton-&gt;isChecked()) {
 
127
                metric = QStyle::PM_ToolBarIconSize;
 
128
            } else if (listViewRadioButton-&gt;isChecked()) {
 
129
                metric = QStyle::PM_ListViewIconSize;
 
130
            } else {
 
131
                metric = QStyle::PM_IconViewIconSize;
 
132
            }
 
133
            extent = QApplication::style()-&gt;pixelMetric(metric);
 
134
        }
 
135
        previewArea-&gt;setSize(QSize(extent, extent));
 
136
        otherSpinBox-&gt;setEnabled(otherRadioButton-&gt;isChecked());
 
137
    }
 
138
 
 
139
    void MainWindow::changeIcon()
 
140
    {
 
141
        QIcon icon;
 
142
 
 
143
        for (int row = 0; row &lt; imagesTable-&gt;rowCount(); ++row) {
 
144
            QTableWidgetItem *item0 = imagesTable-&gt;item(row, 0);
 
145
            QTableWidgetItem *item1 = imagesTable-&gt;item(row, 1);
 
146
            QTableWidgetItem *item2 = imagesTable-&gt;item(row, 2);
 
147
 
 
148
            if (item0-&gt;checkState() == Qt::Checked) {
 
149
                QIcon::Mode mode;
 
150
                if (item1-&gt;text() == tr(&quot;Normal&quot;)) {
 
151
                    mode = QIcon::Normal;
 
152
                } else if (item1-&gt;text() == tr(&quot;Active&quot;)) {
 
153
                    mode = QIcon::Active;
 
154
                } else {
 
155
                    mode = QIcon::Disabled;
 
156
                }
 
157
 
 
158
                QIcon::State state;
 
159
                if (item2-&gt;text() == tr(&quot;On&quot;)) {
 
160
                    state = QIcon::On;
 
161
                } else {
 
162
                    state = QIcon::Off;
 
163
                }
 
164
 
 
165
                QString fileName = item0-&gt;data(Qt::UserRole).toString();
 
166
                QImage image(fileName);
 
167
                if (!image.isNull())
 
168
                    icon.addPixmap(QPixmap::fromImage(image), mode, state);
 
169
            }
 
170
        }
 
171
 
 
172
        previewArea-&gt;setIcon(icon);
 
173
    }
 
174
 
 
175
    void MainWindow::addImage()
 
176
    {
 
177
        QStringList fileNames = QFileDialog::getOpenFileNames(this,
 
178
                                        tr(&quot;Open Images&quot;), &quot;&quot;,
 
179
                                        tr(&quot;Images (*.png *.xpm *.jpg);;&quot;
 
180
                                           &quot;All Files (*)&quot;));
 
181
        if (!fileNames.isEmpty()) {
 
182
            foreach (QString fileName, fileNames) {
 
183
                int row = imagesTable-&gt;rowCount();
 
184
                imagesTable-&gt;setRowCount(row + 1);
 
185
 
 
186
                QString imageName = QFileInfo(fileName).baseName();
 
187
                QTableWidgetItem *item0 = new QTableWidgetItem(imageName);
 
188
                item0-&gt;setData(Qt::UserRole, fileName);
 
189
                item0-&gt;setFlags(item0-&gt;flags() &amp; ~Qt::ItemIsEditable);
 
190
 
 
191
                QTableWidgetItem *item1 = new QTableWidgetItem(tr(&quot;Normal&quot;));
 
192
                QTableWidgetItem *item2 = new QTableWidgetItem(tr(&quot;Off&quot;));
 
193
 
 
194
                if (guessModeStateAct-&gt;isChecked()) {
 
195
                    if (fileName.contains(&quot;_act&quot;)) {
 
196
                        item1-&gt;setText(tr(&quot;Active&quot;));
 
197
                    } else if (fileName.contains(&quot;_dis&quot;)) {
 
198
                        item1-&gt;setText(tr(&quot;Disabled&quot;));
 
199
                    }
 
200
 
 
201
                    if (fileName.contains(&quot;_on&quot;))
 
202
                        item2-&gt;setText(tr(&quot;On&quot;));
 
203
                }
 
204
 
 
205
                imagesTable-&gt;setItem(row, 0, item0);
 
206
                imagesTable-&gt;setItem(row, 1, item1);
 
207
                imagesTable-&gt;setItem(row, 2, item2);
 
208
                imagesTable-&gt;openPersistentEditor(item1);
 
209
                imagesTable-&gt;openPersistentEditor(item2);
 
210
 
 
211
                item0-&gt;setCheckState(Qt::Checked);
 
212
            }
 
213
        }
 
214
    }
 
215
 
 
216
    void MainWindow::removeAllImages()
 
217
    {
 
218
        imagesTable-&gt;setRowCount(0);
 
219
        changeIcon();
 
220
    }
 
221
 
 
222
    void MainWindow::createPreviewGroupBox()
 
223
    {
 
224
        previewGroupBox = new QGroupBox(tr(&quot;Preview&quot;));
 
225
 
 
226
        previewArea = new IconPreviewArea;
 
227
 
 
228
        QVBoxLayout *layout = new QVBoxLayout;
 
229
        layout-&gt;addWidget(previewArea);
 
230
        previewGroupBox-&gt;setLayout(layout);
 
231
    }
 
232
 
 
233
    void MainWindow::createImagesGroupBox()
 
234
    {
 
235
        imagesGroupBox = new QGroupBox(tr(&quot;Images&quot;));
 
236
        imagesGroupBox-&gt;setSizePolicy(QSizePolicy::Expanding,
 
237
                                      QSizePolicy::Expanding);
 
238
 
 
239
        QStringList labels;
 
240
        labels &lt;&lt; tr(&quot;Image&quot;) &lt;&lt; tr(&quot;Mode&quot;) &lt;&lt; tr(&quot;State&quot;);
 
241
 
 
242
        imagesTable = new QTableWidget;
 
243
        imagesTable-&gt;setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
 
244
        imagesTable-&gt;setSelectionMode(QAbstractItemView::NoSelection);
 
245
        imagesTable-&gt;setColumnCount(3);
 
246
        imagesTable-&gt;setHorizontalHeaderLabels(labels);
 
247
        imagesTable-&gt;setItemDelegate(new ImageDelegate(this));
 
248
 
 
249
        imagesTable-&gt;horizontalHeader()-&gt;resizeSection(0, 160);
 
250
        imagesTable-&gt;horizontalHeader()-&gt;resizeSection(1, 80);
 
251
        imagesTable-&gt;horizontalHeader()-&gt;resizeSection(2, 80);
 
252
        imagesTable-&gt;verticalHeader()-&gt;hide();
 
253
 
 
254
        connect(imagesTable, SIGNAL(itemChanged(QTableWidgetItem *)),
 
255
                this, SLOT(changeIcon()));
 
256
 
 
257
        QVBoxLayout *layout = new QVBoxLayout;
 
258
        layout-&gt;addWidget(imagesTable);
 
259
        imagesGroupBox-&gt;setLayout(layout);
 
260
    }
 
261
 
 
262
    void MainWindow::createIconSizeGroupBox()
 
263
    {
 
264
        iconSizeGroupBox = new QGroupBox(tr(&quot;Icon Size&quot;));
 
265
 
 
266
        smallRadioButton = new QRadioButton;
 
267
        largeRadioButton = new QRadioButton;
 
268
        toolBarRadioButton = new QRadioButton;
 
269
        listViewRadioButton = new QRadioButton;
 
270
        iconViewRadioButton = new QRadioButton;
 
271
        otherRadioButton = new QRadioButton(tr(&quot;Other:&quot;));
 
272
 
 
273
        otherSpinBox = new IconSizeSpinBox;
 
274
        otherSpinBox-&gt;setRange(8, 128);
 
275
        otherSpinBox-&gt;setValue(64);
 
276
 
 
277
        connect(toolBarRadioButton, SIGNAL(toggled(bool)),
 
278
                this, SLOT(changeSize()));
 
279
        connect(listViewRadioButton, SIGNAL(toggled(bool)),
 
280
                this, SLOT(changeSize()));
 
281
        connect(iconViewRadioButton, SIGNAL(toggled(bool)),
 
282
                this, SLOT(changeSize()));
 
283
        connect(smallRadioButton, SIGNAL(toggled(bool)), this, SLOT(changeSize()));
 
284
        connect(largeRadioButton, SIGNAL(toggled(bool)), this, SLOT(changeSize()));
 
285
        connect(otherRadioButton, SIGNAL(toggled(bool)), this, SLOT(changeSize()));
 
286
        connect(otherSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeSize()));
 
287
 
 
288
        QHBoxLayout *otherSizeLayout = new QHBoxLayout;
 
289
        otherSizeLayout-&gt;addWidget(otherRadioButton);
 
290
        otherSizeLayout-&gt;addWidget(otherSpinBox);
 
291
 
 
292
        QGridLayout *layout = new QGridLayout;
 
293
        layout-&gt;addWidget(smallRadioButton, 0, 0);
 
294
        layout-&gt;addWidget(largeRadioButton, 1, 0);
 
295
        layout-&gt;addWidget(toolBarRadioButton, 2, 0);
 
296
        layout-&gt;addWidget(listViewRadioButton, 0, 1);
 
297
        layout-&gt;addWidget(iconViewRadioButton, 1, 1);
 
298
        layout-&gt;addLayout(otherSizeLayout, 2, 1);
 
299
        iconSizeGroupBox-&gt;setLayout(layout);
 
300
    }
 
301
 
 
302
    void MainWindow::createActions()
 
303
    {
 
304
        addImageAct = new QAction(tr(&quot;&amp;Add Image...&quot;), this);
 
305
        addImageAct-&gt;setShortcut(tr(&quot;Ctrl+A&quot;));
 
306
        connect(addImageAct, SIGNAL(triggered()), this, SLOT(addImage()));
 
307
 
 
308
        removeAllImagesAct = new QAction(tr(&quot;&amp;Remove All Images&quot;), this);
 
309
        removeAllImagesAct-&gt;setShortcut(tr(&quot;Ctrl+R&quot;));
 
310
        connect(removeAllImagesAct, SIGNAL(triggered()),
 
311
                this, SLOT(removeAllImages()));
 
312
 
 
313
        exitAct = new QAction(tr(&quot;&amp;Exit&quot;), this);
 
314
        exitAct-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));
 
315
        connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
316
 
 
317
        styleActionGroup = new QActionGroup(this);
 
318
        foreach (QString styleName, QStyleFactory::keys()) {
 
319
            QAction *action = new QAction(styleActionGroup);
 
320
            action-&gt;setText(tr(&quot;%1 Style&quot;).arg(styleName));
 
321
            action-&gt;setData(styleName);
 
322
            action-&gt;setCheckable(true);
 
323
            connect(action, SIGNAL(triggered(bool)), this, SLOT(changeStyle(bool)));
 
324
        }
 
325
 
 
326
        guessModeStateAct = new QAction(tr(&quot;&amp;Guess Image Mode/State&quot;), this);
 
327
        guessModeStateAct-&gt;setCheckable(true);
 
328
        guessModeStateAct-&gt;setChecked(true);
 
329
 
 
330
        aboutAct = new QAction(tr(&quot;&amp;About&quot;), this);
 
331
        connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
332
 
 
333
        aboutQtAct = new QAction(tr(&quot;About &amp;Qt&quot;), this);
 
334
        connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
335
    }
 
336
 
 
337
    void MainWindow::createMenus()
 
338
    {
 
339
        fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
 
340
        fileMenu-&gt;addAction(addImageAct);
 
341
        fileMenu-&gt;addAction(removeAllImagesAct);
 
342
        fileMenu-&gt;addSeparator();
 
343
        fileMenu-&gt;addAction(exitAct);
 
344
 
 
345
        viewMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;View&quot;));
 
346
        foreach (QAction *action, styleActionGroup-&gt;actions())
 
347
            viewMenu-&gt;addAction(action);
 
348
        viewMenu-&gt;addSeparator();
 
349
        viewMenu-&gt;addAction(guessModeStateAct);
 
350
 
 
351
        menuBar()-&gt;addSeparator();
 
352
 
 
353
        helpMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Help&quot;));
 
354
        helpMenu-&gt;addAction(aboutAct);
 
355
        helpMenu-&gt;addAction(aboutQtAct);
 
356
    }
 
357
 
 
358
    void MainWindow::createContextMenu()
 
359
    {
 
360
        imagesTable-&gt;setContextMenuPolicy(Qt::ActionsContextMenu);
 
361
        imagesTable-&gt;addAction(addImageAct);
 
362
        imagesTable-&gt;addAction(removeAllImagesAct);
 
363
    }
 
364
 
 
365
    void MainWindow::checkCurrentStyle()
 
366
    {
 
367
        foreach (QAction *action, styleActionGroup-&gt;actions()) {
 
368
            QString styleName = action-&gt;data().toString();
 
369
            QStyle *candidate = QStyleFactory::create(styleName);
 
370
            Q_ASSERT(candidate);
 
371
            if (candidate-&gt;metaObject()-&gt;className()
 
372
                    == QApplication::style()-&gt;metaObject()-&gt;className()) {
 
373
                action-&gt;trigger();
 
374
                return;
 
375
            }
 
376
            delete candidate;
 
377
        }
 
378
    }</pre>
 
379
<p /><address><hr /><div align="center">
 
380
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
381
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
382
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
383
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
384
</tr></table></div></address></body>
 
385
</html>