source: trunk/examples/widgets/icons/mainwindow.cpp@ 1147

Last change on this file since 1147 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 14.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "iconpreviewarea.h"
44#include "iconsizespinbox.h"
45#include "imagedelegate.h"
46#include "mainwindow.h"
47
48//! [0]
49MainWindow::MainWindow()
50{
51 centralWidget = new QWidget;
52 setCentralWidget(centralWidget);
53
54 createPreviewGroupBox();
55 createImagesGroupBox();
56 createIconSizeGroupBox();
57
58 createActions();
59 createMenus();
60 createContextMenu();
61
62 QGridLayout *mainLayout = new QGridLayout;
63 mainLayout->addWidget(previewGroupBox, 0, 0, 1, 2);
64 mainLayout->addWidget(imagesGroupBox, 1, 0);
65 mainLayout->addWidget(iconSizeGroupBox, 1, 1);
66 centralWidget->setLayout(mainLayout);
67
68 setWindowTitle(tr("Icons"));
69 checkCurrentStyle();
70 otherRadioButton->click();
71
72 resize(minimumSizeHint());
73}
74//! [0]
75
76//! [1]
77void MainWindow::about()
78{
79 QMessageBox::about(this, tr("About Icons"),
80 tr("The <b>Icons</b> example illustrates how Qt renders an icon in "
81 "different modes (active, normal, disabled, and selected) and "
82 "states (on and off) based on a set of images."));
83}
84//! [1]
85
86//! [2]
87void MainWindow::changeStyle(bool checked)
88{
89 if (!checked)
90 return;
91
92 QAction *action = qobject_cast<QAction *>(sender());
93//! [2] //! [3]
94 QStyle *style = QStyleFactory::create(action->data().toString());
95//! [3] //! [4]
96 Q_ASSERT(style);
97 QApplication::setStyle(style);
98
99 smallRadioButton->setText(tr("Small (%1 x %1)")
100 .arg(style->pixelMetric(QStyle::PM_SmallIconSize)));
101 largeRadioButton->setText(tr("Large (%1 x %1)")
102 .arg(style->pixelMetric(QStyle::PM_LargeIconSize)));
103 toolBarRadioButton->setText(tr("Toolbars (%1 x %1)")
104 .arg(style->pixelMetric(QStyle::PM_ToolBarIconSize)));
105 listViewRadioButton->setText(tr("List views (%1 x %1)")
106 .arg(style->pixelMetric(QStyle::PM_ListViewIconSize)));
107 iconViewRadioButton->setText(tr("Icon views (%1 x %1)")
108 .arg(style->pixelMetric(QStyle::PM_IconViewIconSize)));
109 tabBarRadioButton->setText(tr("Tab bars (%1 x %1)")
110 .arg(style->pixelMetric(QStyle::PM_TabBarIconSize)));
111
112 changeSize();
113}
114//! [4]
115
116//! [5]
117void MainWindow::changeSize(bool checked)
118{
119 if (!checked)
120 return;
121
122 int extent;
123
124 if (otherRadioButton->isChecked()) {
125 extent = otherSpinBox->value();
126 } else {
127 QStyle::PixelMetric metric;
128
129 if (smallRadioButton->isChecked()) {
130 metric = QStyle::PM_SmallIconSize;
131 } else if (largeRadioButton->isChecked()) {
132 metric = QStyle::PM_LargeIconSize;
133 } else if (toolBarRadioButton->isChecked()) {
134 metric = QStyle::PM_ToolBarIconSize;
135 } else if (listViewRadioButton->isChecked()) {
136 metric = QStyle::PM_ListViewIconSize;
137 } else if (iconViewRadioButton->isChecked()) {
138 metric = QStyle::PM_IconViewIconSize;
139 } else {
140 metric = QStyle::PM_TabBarIconSize;
141 }
142 extent = QApplication::style()->pixelMetric(metric);
143 }
144 previewArea->setSize(QSize(extent, extent));
145 otherSpinBox->setEnabled(otherRadioButton->isChecked());
146}
147//! [5]
148
149//! [6]
150void MainWindow::changeIcon()
151{
152 QIcon icon;
153
154 for (int row = 0; row < imagesTable->rowCount(); ++row) {
155 QTableWidgetItem *item0 = imagesTable->item(row, 0);
156 QTableWidgetItem *item1 = imagesTable->item(row, 1);
157 QTableWidgetItem *item2 = imagesTable->item(row, 2);
158
159 if (item0->checkState() == Qt::Checked) {
160 QIcon::Mode mode;
161 if (item1->text() == tr("Normal")) {
162 mode = QIcon::Normal;
163 } else if (item1->text() == tr("Active")) {
164 mode = QIcon::Active;
165 } else if (item1->text() == tr("Disabled")) {
166 mode = QIcon::Disabled;
167 } else {
168 mode = QIcon::Selected;
169 }
170
171 QIcon::State state;
172 if (item2->text() == tr("On")) {
173 state = QIcon::On;
174 } else {
175 state = QIcon::Off;
176//! [6] //! [7]
177 }
178//! [7]
179
180//! [8]
181 QString fileName = item0->data(Qt::UserRole).toString();
182 QImage image(fileName);
183 if (!image.isNull())
184 icon.addPixmap(QPixmap::fromImage(image), mode, state);
185//! [8] //! [9]
186 }
187//! [9] //! [10]
188 }
189//! [10]
190
191//! [11]
192 previewArea->setIcon(icon);
193}
194//! [11]
195
196//! [12]
197void MainWindow::addImages()
198{
199 QStringList fileNames = QFileDialog::getOpenFileNames(this,
200 tr("Open Images"), "",
201 tr("Images (*.png *.xpm *.jpg);;"
202 "All Files (*)"));
203 if (!fileNames.isEmpty()) {
204 foreach (QString fileName, fileNames) {
205 int row = imagesTable->rowCount();
206 imagesTable->setRowCount(row + 1);
207//! [12]
208
209//! [13]
210 QString imageName = QFileInfo(fileName).baseName();
211//! [13] //! [14]
212 QTableWidgetItem *item0 = new QTableWidgetItem(imageName);
213 item0->setData(Qt::UserRole, fileName);
214 item0->setFlags(item0->flags() & ~Qt::ItemIsEditable);
215//! [14]
216
217//! [15]
218 QTableWidgetItem *item1 = new QTableWidgetItem(tr("Normal"));
219//! [15] //! [16]
220 QTableWidgetItem *item2 = new QTableWidgetItem(tr("Off"));
221
222 if (guessModeStateAct->isChecked()) {
223 if (fileName.contains("_act")) {
224 item1->setText(tr("Active"));
225 } else if (fileName.contains("_dis")) {
226 item1->setText(tr("Disabled"));
227 } else if (fileName.contains("_sel")) {
228 item1->setText(tr("Selected"));
229 }
230
231 if (fileName.contains("_on"))
232 item2->setText(tr("On"));
233//! [16] //! [17]
234 }
235//! [17]
236
237//! [18]
238 imagesTable->setItem(row, 0, item0);
239//! [18] //! [19]
240 imagesTable->setItem(row, 1, item1);
241 imagesTable->setItem(row, 2, item2);
242 imagesTable->openPersistentEditor(item1);
243 imagesTable->openPersistentEditor(item2);
244
245 item0->setCheckState(Qt::Checked);
246 }
247 }
248}
249//! [19]
250
251//! [20]
252void MainWindow::removeAllImages()
253{
254 imagesTable->setRowCount(0);
255 changeIcon();
256}
257//! [20]
258
259void MainWindow::createPreviewGroupBox()
260{
261 previewGroupBox = new QGroupBox(tr("Preview"));
262
263 previewArea = new IconPreviewArea;
264
265 QVBoxLayout *layout = new QVBoxLayout;
266 layout->addWidget(previewArea);
267 previewGroupBox->setLayout(layout);
268}
269
270//! [21]
271void MainWindow::createImagesGroupBox()
272{
273 imagesGroupBox = new QGroupBox(tr("Images"));
274
275 imagesTable = new QTableWidget;
276 imagesTable->setSelectionMode(QAbstractItemView::NoSelection);
277 imagesTable->setItemDelegate(new ImageDelegate(this));
278//! [21]
279
280//! [22]
281 QStringList labels;
282//! [22] //! [23]
283 labels << tr("Image") << tr("Mode") << tr("State");
284
285 imagesTable->horizontalHeader()->setDefaultSectionSize(90);
286 imagesTable->setColumnCount(3);
287 imagesTable->setHorizontalHeaderLabels(labels);
288 imagesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
289 imagesTable->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
290 imagesTable->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
291 imagesTable->verticalHeader()->hide();
292//! [23]
293
294//! [24]
295 connect(imagesTable, SIGNAL(itemChanged(QTableWidgetItem*)),
296//! [24] //! [25]
297 this, SLOT(changeIcon()));
298
299 QVBoxLayout *layout = new QVBoxLayout;
300 layout->addWidget(imagesTable);
301 imagesGroupBox->setLayout(layout);
302}
303//! [25]
304
305//! [26]
306void MainWindow::createIconSizeGroupBox()
307{
308 iconSizeGroupBox = new QGroupBox(tr("Icon Size"));
309
310 smallRadioButton = new QRadioButton;
311 largeRadioButton = new QRadioButton;
312 toolBarRadioButton = new QRadioButton;
313 listViewRadioButton = new QRadioButton;
314 iconViewRadioButton = new QRadioButton;
315 tabBarRadioButton = new QRadioButton;
316 otherRadioButton = new QRadioButton(tr("Other:"));
317
318 otherSpinBox = new IconSizeSpinBox;
319 otherSpinBox->setRange(8, 128);
320 otherSpinBox->setValue(64);
321//! [26]
322
323//! [27]
324 connect(smallRadioButton, SIGNAL(toggled(bool)),
325 this, SLOT(changeSize(bool)));
326 connect(largeRadioButton, SIGNAL(toggled(bool)),
327 this, SLOT(changeSize(bool)));
328 connect(toolBarRadioButton, SIGNAL(toggled(bool)),
329 this, SLOT(changeSize(bool)));
330 connect(listViewRadioButton, SIGNAL(toggled(bool)),
331 this, SLOT(changeSize(bool)));
332 connect(iconViewRadioButton, SIGNAL(toggled(bool)),
333 this, SLOT(changeSize(bool)));
334 connect(tabBarRadioButton, SIGNAL(toggled(bool)),
335 this, SLOT(changeSize(bool)));
336 connect(otherRadioButton, SIGNAL(toggled(bool)),
337 this, SLOT(changeSize(bool)));
338 connect(otherSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeSize()));
339
340 QHBoxLayout *otherSizeLayout = new QHBoxLayout;
341 otherSizeLayout->addWidget(otherRadioButton);
342 otherSizeLayout->addWidget(otherSpinBox);
343 otherSizeLayout->addStretch();
344
345 QGridLayout *layout = new QGridLayout;
346 layout->addWidget(smallRadioButton, 0, 0);
347 layout->addWidget(largeRadioButton, 1, 0);
348 layout->addWidget(toolBarRadioButton, 2, 0);
349 layout->addWidget(listViewRadioButton, 0, 1);
350 layout->addWidget(iconViewRadioButton, 1, 1);
351 layout->addWidget(tabBarRadioButton, 2, 1);
352 layout->addLayout(otherSizeLayout, 3, 0, 1, 2);
353 layout->setRowStretch(4, 1);
354 iconSizeGroupBox->setLayout(layout);
355}
356//! [27]
357
358//! [28]
359void MainWindow::createActions()
360{
361 addImagesAct = new QAction(tr("&Add Images..."), this);
362 addImagesAct->setShortcut(tr("Ctrl+A"));
363 connect(addImagesAct, SIGNAL(triggered()), this, SLOT(addImages()));
364
365 removeAllImagesAct = new QAction(tr("&Remove All Images"), this);
366 removeAllImagesAct->setShortcut(tr("Ctrl+R"));
367 connect(removeAllImagesAct, SIGNAL(triggered()),
368 this, SLOT(removeAllImages()));
369
370 exitAct = new QAction(tr("&Quit"), this);
371 exitAct->setShortcuts(QKeySequence::Quit);
372 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
373
374 styleActionGroup = new QActionGroup(this);
375 foreach (QString styleName, QStyleFactory::keys()) {
376 QAction *action = new QAction(styleActionGroup);
377 action->setText(tr("%1 Style").arg(styleName));
378 action->setData(styleName);
379 action->setCheckable(true);
380 connect(action, SIGNAL(triggered(bool)), this, SLOT(changeStyle(bool)));
381 }
382
383 guessModeStateAct = new QAction(tr("&Guess Image Mode/State"), this);
384 guessModeStateAct->setCheckable(true);
385 guessModeStateAct->setChecked(true);
386
387 aboutAct = new QAction(tr("&About"), this);
388 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
389
390 aboutQtAct = new QAction(tr("About &Qt"), this);
391 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
392}
393//! [28]
394
395//! [29]
396void MainWindow::createMenus()
397{
398 fileMenu = menuBar()->addMenu(tr("&File"));
399 fileMenu->addAction(addImagesAct);
400 fileMenu->addAction(removeAllImagesAct);
401 fileMenu->addSeparator();
402 fileMenu->addAction(exitAct);
403
404 viewMenu = menuBar()->addMenu(tr("&View"));
405 foreach (QAction *action, styleActionGroup->actions())
406 viewMenu->addAction(action);
407 viewMenu->addSeparator();
408 viewMenu->addAction(guessModeStateAct);
409
410 menuBar()->addSeparator();
411
412 helpMenu = menuBar()->addMenu(tr("&Help"));
413 helpMenu->addAction(aboutAct);
414 helpMenu->addAction(aboutQtAct);
415}
416//! [29]
417
418//! [30]
419void MainWindow::createContextMenu()
420{
421 imagesTable->setContextMenuPolicy(Qt::ActionsContextMenu);
422 imagesTable->addAction(addImagesAct);
423 imagesTable->addAction(removeAllImagesAct);
424}
425//! [30]
426
427//! [31]
428void MainWindow::checkCurrentStyle()
429{
430 foreach (QAction *action, styleActionGroup->actions()) {
431 QString styleName = action->data().toString();
432 QStyle *candidate = QStyleFactory::create(styleName);
433 Q_ASSERT(candidate);
434 if (candidate->metaObject()->className()
435 == QApplication::style()->metaObject()->className()) {
436 action->trigger();
437 return;
438 }
439 delete candidate;
440 }
441}
442//! [31]
Note: See TracBrowser for help on using the repository browser.