1
/****************************************************************************
3
** Copyright (C) 2005-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 "iconpreviewarea.h"
32
#include "iconsizespinbox.h"
33
#include "imagedelegate.h"
34
#include "mainwindow.h"
36
MainWindow::MainWindow()
38
centralWidget = new QWidget;
39
setCentralWidget(centralWidget);
41
createPreviewGroupBox();
42
createImagesGroupBox();
43
createIconSizeGroupBox();
49
QGridLayout *mainLayout = new QGridLayout;
50
mainLayout->addWidget(imagesGroupBox, 0, 0);
51
mainLayout->addWidget(iconSizeGroupBox, 1, 0);
52
mainLayout->addWidget(previewGroupBox, 0, 1, 2, 1);
53
centralWidget->setLayout(mainLayout);
55
setWindowTitle(tr("Icons"));
57
otherRadioButton->click();
62
void MainWindow::about()
64
QMessageBox::about(this, tr("About Icons"),
65
tr("The <b>Icons</b> example illustrates how Qt renders an icon in "
66
"different modes (active, normal, and disabled) and states (on "
67
"and off) based on a set of images."));
70
void MainWindow::changeStyle(bool checked)
75
QAction *action = qobject_cast<QAction *>(sender());
76
QStyle *style = QStyleFactory::create(action->data().toString());
78
QApplication::setStyle(style);
80
smallRadioButton->setText(tr("Small (%1 ļæ½ %1)")
81
.arg(style->pixelMetric(QStyle::PM_SmallIconSize)));
82
largeRadioButton->setText(tr("Large (%1 ļæ½ %1)")
83
.arg(style->pixelMetric(QStyle::PM_LargeIconSize)));
84
toolBarRadioButton->setText(tr("Toolbars (%1 ļæ½ %1)")
85
.arg(style->pixelMetric(QStyle::PM_ToolBarIconSize)));
86
listViewRadioButton->setText(tr("List views (%1 ļæ½ %1)")
87
.arg(style->pixelMetric(QStyle::PM_ListViewIconSize)));
88
iconViewRadioButton->setText(tr("Icon views (%1 ļæ½ %1)")
89
.arg(style->pixelMetric(QStyle::PM_IconViewIconSize)));
94
void MainWindow::changeSize()
98
if (otherRadioButton->isChecked()) {
99
extent = otherSpinBox->value();
101
QStyle::PixelMetric metric;
103
if (smallRadioButton->isChecked()) {
104
metric = QStyle::PM_SmallIconSize;
105
} else if (largeRadioButton->isChecked()) {
106
metric = QStyle::PM_LargeIconSize;
107
} else if (toolBarRadioButton->isChecked()) {
108
metric = QStyle::PM_ToolBarIconSize;
109
} else if (listViewRadioButton->isChecked()) {
110
metric = QStyle::PM_ListViewIconSize;
112
metric = QStyle::PM_IconViewIconSize;
114
extent = QApplication::style()->pixelMetric(metric);
116
previewArea->setSize(QSize(extent, extent));
117
otherSpinBox->setEnabled(otherRadioButton->isChecked());
120
void MainWindow::changeIcon()
124
for (int row = 0; row < imagesTable->rowCount(); ++row) {
125
QTableWidgetItem *item0 = imagesTable->item(row, 0);
126
QTableWidgetItem *item1 = imagesTable->item(row, 1);
127
QTableWidgetItem *item2 = imagesTable->item(row, 2);
129
if (item0->checkState() == Qt::Checked) {
131
if (item1->text() == tr("Normal")) {
132
mode = QIcon::Normal;
133
} else if (item1->text() == tr("Active")) {
134
mode = QIcon::Active;
136
mode = QIcon::Disabled;
140
if (item2->text() == tr("On")) {
146
QString fileName = item0->data(Qt::UserRole).toString();
147
QImage image(fileName);
149
icon.addPixmap(QPixmap::fromImage(image), mode, state);
153
previewArea->setIcon(icon);
156
void MainWindow::addImage()
158
QStringList fileNames = QFileDialog::getOpenFileNames(this,
159
tr("Open Images"), "",
160
tr("Images (*.png *.xpm *.jpg);;"
162
if (!fileNames.isEmpty()) {
163
foreach (QString fileName, fileNames) {
164
int row = imagesTable->rowCount();
165
imagesTable->setRowCount(row + 1);
167
QString imageName = QFileInfo(fileName).baseName();
168
QTableWidgetItem *item0 = new QTableWidgetItem(imageName);
169
item0->setData(Qt::UserRole, fileName);
170
item0->setFlags(item0->flags() & ~Qt::ItemIsEditable);
172
QTableWidgetItem *item1 = new QTableWidgetItem(tr("Normal"));
173
QTableWidgetItem *item2 = new QTableWidgetItem(tr("Off"));
175
if (guessModeStateAct->isChecked()) {
176
if (fileName.contains("_act")) {
177
item1->setText(tr("Active"));
178
} else if (fileName.contains("_dis")) {
179
item1->setText(tr("Disabled"));
182
if (fileName.contains("_on"))
183
item2->setText(tr("On"));
186
imagesTable->setItem(row, 0, item0);
187
imagesTable->setItem(row, 1, item1);
188
imagesTable->setItem(row, 2, item2);
189
imagesTable->openPersistentEditor(item1);
190
imagesTable->openPersistentEditor(item2);
192
item0->setCheckState(Qt::Checked);
197
void MainWindow::removeAllImages()
199
imagesTable->setRowCount(0);
203
void MainWindow::createPreviewGroupBox()
205
previewGroupBox = new QGroupBox(tr("Preview"));
207
previewArea = new IconPreviewArea;
209
QVBoxLayout *layout = new QVBoxLayout;
210
layout->addWidget(previewArea);
211
previewGroupBox->setLayout(layout);
214
void MainWindow::createImagesGroupBox()
216
imagesGroupBox = new QGroupBox(tr("Images"));
217
imagesGroupBox->setSizePolicy(QSizePolicy::Expanding,
218
QSizePolicy::Expanding);
221
labels << tr("Image") << tr("Mode") << tr("State");
223
imagesTable = new QTableWidget;
224
imagesTable->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
225
imagesTable->setSelectionMode(QAbstractItemView::NoSelection);
226
imagesTable->setColumnCount(3);
227
imagesTable->setHorizontalHeaderLabels(labels);
228
imagesTable->setItemDelegate(new ImageDelegate(this));
230
imagesTable->horizontalHeader()->resizeSection(0, 160);
231
imagesTable->horizontalHeader()->resizeSection(1, 80);
232
imagesTable->horizontalHeader()->resizeSection(2, 80);
233
imagesTable->verticalHeader()->hide();
235
connect(imagesTable, SIGNAL(itemChanged(QTableWidgetItem *)),
236
this, SLOT(changeIcon()));
238
QVBoxLayout *layout = new QVBoxLayout;
239
layout->addWidget(imagesTable);
240
imagesGroupBox->setLayout(layout);
243
void MainWindow::createIconSizeGroupBox()
245
iconSizeGroupBox = new QGroupBox(tr("Icon Size"));
247
smallRadioButton = new QRadioButton;
248
largeRadioButton = new QRadioButton;
249
toolBarRadioButton = new QRadioButton;
250
listViewRadioButton = new QRadioButton;
251
iconViewRadioButton = new QRadioButton;
252
otherRadioButton = new QRadioButton(tr("Other:"));
254
otherSpinBox = new IconSizeSpinBox;
255
otherSpinBox->setRange(8, 128);
256
otherSpinBox->setValue(64);
258
connect(toolBarRadioButton, SIGNAL(toggled(bool)),
259
this, SLOT(changeSize()));
260
connect(listViewRadioButton, SIGNAL(toggled(bool)),
261
this, SLOT(changeSize()));
262
connect(iconViewRadioButton, SIGNAL(toggled(bool)),
263
this, SLOT(changeSize()));
264
connect(smallRadioButton, SIGNAL(toggled(bool)), this, SLOT(changeSize()));
265
connect(largeRadioButton, SIGNAL(toggled(bool)), this, SLOT(changeSize()));
266
connect(otherRadioButton, SIGNAL(toggled(bool)), this, SLOT(changeSize()));
267
connect(otherSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeSize()));
269
QHBoxLayout *otherSizeLayout = new QHBoxLayout;
270
otherSizeLayout->addWidget(otherRadioButton);
271
otherSizeLayout->addWidget(otherSpinBox);
273
QGridLayout *layout = new QGridLayout;
274
layout->addWidget(smallRadioButton, 0, 0);
275
layout->addWidget(largeRadioButton, 1, 0);
276
layout->addWidget(toolBarRadioButton, 2, 0);
277
layout->addWidget(listViewRadioButton, 0, 1);
278
layout->addWidget(iconViewRadioButton, 1, 1);
279
layout->addLayout(otherSizeLayout, 2, 1);
280
iconSizeGroupBox->setLayout(layout);
283
void MainWindow::createActions()
285
addImageAct = new QAction(tr("&Add Image..."), this);
286
addImageAct->setShortcut(tr("Ctrl+A"));
287
connect(addImageAct, SIGNAL(triggered()), this, SLOT(addImage()));
289
removeAllImagesAct = new QAction(tr("&Remove All Images"), this);
290
removeAllImagesAct->setShortcut(tr("Ctrl+R"));
291
connect(removeAllImagesAct, SIGNAL(triggered()),
292
this, SLOT(removeAllImages()));
294
exitAct = new QAction(tr("&Exit"), this);
295
exitAct->setShortcut(tr("Ctrl+Q"));
296
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
298
styleActionGroup = new QActionGroup(this);
299
foreach (QString styleName, QStyleFactory::keys()) {
300
QAction *action = new QAction(styleActionGroup);
301
action->setText(tr("%1 Style").arg(styleName));
302
action->setData(styleName);
303
action->setCheckable(true);
304
connect(action, SIGNAL(triggered(bool)), this, SLOT(changeStyle(bool)));
307
guessModeStateAct = new QAction(tr("&Guess Image Mode/State"), this);
308
guessModeStateAct->setCheckable(true);
309
guessModeStateAct->setChecked(true);
311
aboutAct = new QAction(tr("&About"), this);
312
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
314
aboutQtAct = new QAction(tr("About &Qt"), this);
315
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
318
void MainWindow::createMenus()
320
fileMenu = menuBar()->addMenu(tr("&File"));
321
fileMenu->addAction(addImageAct);
322
fileMenu->addAction(removeAllImagesAct);
323
fileMenu->addSeparator();
324
fileMenu->addAction(exitAct);
326
viewMenu = menuBar()->addMenu(tr("&View"));
327
foreach (QAction *action, styleActionGroup->actions())
328
viewMenu->addAction(action);
329
viewMenu->addSeparator();
330
viewMenu->addAction(guessModeStateAct);
332
menuBar()->addSeparator();
334
helpMenu = menuBar()->addMenu(tr("&Help"));
335
helpMenu->addAction(aboutAct);
336
helpMenu->addAction(aboutQtAct);
339
void MainWindow::createContextMenu()
341
imagesTable->setContextMenuPolicy(Qt::ActionsContextMenu);
342
imagesTable->addAction(addImageAct);
343
imagesTable->addAction(removeAllImagesAct);
346
void MainWindow::checkCurrentStyle()
348
foreach (QAction *action, styleActionGroup->actions()) {
349
QString styleName = action->data().toString();
350
QStyle *candidate = QStyleFactory::create(styleName);
352
if (candidate->metaObject()->className()
353
== QApplication::style()->metaObject()->className()) {