1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
43
#include "iconpreviewarea.h"
44
#include "iconsizespinbox.h"
45
#include "imagedelegate.h"
46
#include "mainwindow.h"
49
MainWindow::MainWindow()
51
centralWidget = new QWidget;
52
setCentralWidget(centralWidget);
54
createPreviewGroupBox();
55
createImagesGroupBox();
56
createIconSizeGroupBox();
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);
68
setWindowTitle(tr("Icons"));
70
otherRadioButton->click();
72
resize(minimumSizeHint());
77
void MainWindow::about()
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."));
87
void MainWindow::changeStyle(bool checked)
92
QAction *action = qobject_cast<QAction *>(sender());
94
QStyle *style = QStyleFactory::create(action->data().toString());
97
QApplication::setStyle(style);
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)));
117
void MainWindow::changeSize(bool checked)
124
if (otherRadioButton->isChecked()) {
125
extent = otherSpinBox->value();
127
QStyle::PixelMetric metric;
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;
140
metric = QStyle::PM_TabBarIconSize;
142
extent = QApplication::style()->pixelMetric(metric);
144
previewArea->setSize(QSize(extent, extent));
145
otherSpinBox->setEnabled(otherRadioButton->isChecked());
150
void MainWindow::changeIcon()
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);
159
if (item0->checkState() == Qt::Checked) {
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;
168
mode = QIcon::Selected;
172
if (item2->text() == tr("On")) {
181
QString fileName = item0->data(Qt::UserRole).toString();
182
QImage image(fileName);
184
icon.addPixmap(QPixmap::fromImage(image), mode, state);
192
previewArea->setIcon(icon);
197
void MainWindow::addImages()
199
QStringList fileNames = QFileDialog::getOpenFileNames(this,
200
tr("Open Images"), "",
201
tr("Images (*.png *.xpm *.jpg);;"
203
if (!fileNames.isEmpty()) {
204
foreach (QString fileName, fileNames) {
205
int row = imagesTable->rowCount();
206
imagesTable->setRowCount(row + 1);
210
QString imageName = QFileInfo(fileName).baseName();
212
QTableWidgetItem *item0 = new QTableWidgetItem(imageName);
213
item0->setData(Qt::UserRole, fileName);
214
item0->setFlags(item0->flags() & ~Qt::ItemIsEditable);
218
QTableWidgetItem *item1 = new QTableWidgetItem(tr("Normal"));
220
QTableWidgetItem *item2 = new QTableWidgetItem(tr("Off"));
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"));
231
if (fileName.contains("_on"))
232
item2->setText(tr("On"));
238
imagesTable->setItem(row, 0, item0);
240
imagesTable->setItem(row, 1, item1);
241
imagesTable->setItem(row, 2, item2);
242
imagesTable->openPersistentEditor(item1);
243
imagesTable->openPersistentEditor(item2);
245
item0->setCheckState(Qt::Checked);
252
void MainWindow::removeAllImages()
254
imagesTable->setRowCount(0);
259
void MainWindow::createPreviewGroupBox()
261
previewGroupBox = new QGroupBox(tr("Preview"));
263
previewArea = new IconPreviewArea;
265
QVBoxLayout *layout = new QVBoxLayout;
266
layout->addWidget(previewArea);
267
previewGroupBox->setLayout(layout);
271
void MainWindow::createImagesGroupBox()
273
imagesGroupBox = new QGroupBox(tr("Images"));
275
imagesTable = new QTableWidget;
276
imagesTable->setSelectionMode(QAbstractItemView::NoSelection);
277
imagesTable->setItemDelegate(new ImageDelegate(this));
283
labels << tr("Image") << tr("Mode") << tr("State");
285
imagesTable->horizontalHeader()->setDefaultSectionSize(90);
286
imagesTable->setColumnCount(3);
287
imagesTable->setHorizontalHeaderLabels(labels);
288
imagesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
289
imagesTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
290
imagesTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
291
imagesTable->verticalHeader()->hide();
295
connect(imagesTable, SIGNAL(itemChanged(QTableWidgetItem*)),
297
this, SLOT(changeIcon()));
299
QVBoxLayout *layout = new QVBoxLayout;
300
layout->addWidget(imagesTable);
301
imagesGroupBox->setLayout(layout);
306
void MainWindow::createIconSizeGroupBox()
308
iconSizeGroupBox = new QGroupBox(tr("Icon Size"));
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:"));
318
otherSpinBox = new IconSizeSpinBox;
319
otherSpinBox->setRange(8, 128);
320
otherSpinBox->setValue(64);
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()));
340
QHBoxLayout *otherSizeLayout = new QHBoxLayout;
341
otherSizeLayout->addWidget(otherRadioButton);
342
otherSizeLayout->addWidget(otherSpinBox);
343
otherSizeLayout->addStretch();
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);
359
void MainWindow::createActions()
361
addImagesAct = new QAction(tr("&Add Images..."), this);
362
addImagesAct->setShortcut(tr("Ctrl+A"));
363
connect(addImagesAct, SIGNAL(triggered()), this, SLOT(addImages()));
365
removeAllImagesAct = new QAction(tr("&Remove All Images"), this);
366
removeAllImagesAct->setShortcut(tr("Ctrl+R"));
367
connect(removeAllImagesAct, SIGNAL(triggered()),
368
this, SLOT(removeAllImages()));
370
exitAct = new QAction(tr("&Quit"), this);
371
exitAct->setShortcuts(QKeySequence::Quit);
372
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
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)));
383
guessModeStateAct = new QAction(tr("&Guess Image Mode/State"), this);
384
guessModeStateAct->setCheckable(true);
385
guessModeStateAct->setChecked(true);
387
aboutAct = new QAction(tr("&About"), this);
388
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
390
aboutQtAct = new QAction(tr("About &Qt"), this);
391
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
396
void MainWindow::createMenus()
398
fileMenu = menuBar()->addMenu(tr("&File"));
399
fileMenu->addAction(addImagesAct);
400
fileMenu->addAction(removeAllImagesAct);
401
fileMenu->addSeparator();
402
fileMenu->addAction(exitAct);
404
viewMenu = menuBar()->addMenu(tr("&View"));
405
foreach (QAction *action, styleActionGroup->actions())
406
viewMenu->addAction(action);
407
viewMenu->addSeparator();
408
viewMenu->addAction(guessModeStateAct);
410
menuBar()->addSeparator();
412
helpMenu = menuBar()->addMenu(tr("&Help"));
413
helpMenu->addAction(aboutAct);
414
helpMenu->addAction(aboutQtAct);
419
void MainWindow::createContextMenu()
421
imagesTable->setContextMenuPolicy(Qt::ActionsContextMenu);
422
imagesTable->addAction(addImagesAct);
423
imagesTable->addAction(removeAllImagesAct);
428
void MainWindow::checkCurrentStyle()
430
foreach (QAction *action, styleActionGroup->actions()) {
431
QString styleName = action->data().toString();
432
QStyle *candidate = QStyleFactory::create(styleName);
434
if (candidate->metaObject()->className()
435
== QApplication::style()->metaObject()->className()) {