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

« back to all changes in this revision

Viewing changes to examples/widgets/icons/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) 2005-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 "iconpreviewarea.h"
 
32
#include "iconsizespinbox.h"
 
33
#include "imagedelegate.h"
 
34
#include "mainwindow.h"
 
35
 
 
36
MainWindow::MainWindow()
 
37
{
 
38
    centralWidget = new QWidget;
 
39
    setCentralWidget(centralWidget);
 
40
 
 
41
    createPreviewGroupBox();
 
42
    createImagesGroupBox();
 
43
    createIconSizeGroupBox();
 
44
 
 
45
    createActions();
 
46
    createMenus();
 
47
    createContextMenu();
 
48
 
 
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);
 
54
 
 
55
    setWindowTitle(tr("Icons"));
 
56
    checkCurrentStyle();
 
57
    otherRadioButton->click();
 
58
 
 
59
    resize(860, 400);
 
60
}
 
61
 
 
62
void MainWindow::about()
 
63
{
 
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."));
 
68
}
 
69
 
 
70
void MainWindow::changeStyle(bool checked)
 
71
{
 
72
    if (!checked)
 
73
        return;
 
74
 
 
75
    QAction *action = qobject_cast<QAction *>(sender());
 
76
    QStyle *style = QStyleFactory::create(action->data().toString());
 
77
    Q_ASSERT(style);
 
78
    QApplication::setStyle(style);
 
79
 
 
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)));
 
90
 
 
91
    changeSize();
 
92
}
 
93
 
 
94
void MainWindow::changeSize()
 
95
{
 
96
    int extent;
 
97
 
 
98
    if (otherRadioButton->isChecked()) {
 
99
        extent = otherSpinBox->value();
 
100
    } else {
 
101
        QStyle::PixelMetric metric;
 
102
 
 
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;
 
111
        } else {
 
112
            metric = QStyle::PM_IconViewIconSize;
 
113
        }
 
114
        extent = QApplication::style()->pixelMetric(metric);
 
115
    }
 
116
    previewArea->setSize(QSize(extent, extent));
 
117
    otherSpinBox->setEnabled(otherRadioButton->isChecked());
 
118
}
 
119
 
 
120
void MainWindow::changeIcon()
 
121
{
 
122
    QIcon icon;
 
123
 
 
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);
 
128
 
 
129
        if (item0->checkState() == Qt::Checked) {
 
130
            QIcon::Mode mode;
 
131
            if (item1->text() == tr("Normal")) {
 
132
                mode = QIcon::Normal;
 
133
            } else if (item1->text() == tr("Active")) {
 
134
                mode = QIcon::Active;
 
135
            } else {
 
136
                mode = QIcon::Disabled;
 
137
            }
 
138
 
 
139
            QIcon::State state;
 
140
            if (item2->text() == tr("On")) {
 
141
                state = QIcon::On;
 
142
            } else {
 
143
                state = QIcon::Off;
 
144
            }
 
145
 
 
146
            QString fileName = item0->data(Qt::UserRole).toString();
 
147
            QImage image(fileName);
 
148
            if (!image.isNull())
 
149
                icon.addPixmap(QPixmap::fromImage(image), mode, state);
 
150
        }
 
151
    }
 
152
 
 
153
    previewArea->setIcon(icon);
 
154
}
 
155
 
 
156
void MainWindow::addImage()
 
157
{
 
158
    QStringList fileNames = QFileDialog::getOpenFileNames(this,
 
159
                                    tr("Open Images"), "",
 
160
                                    tr("Images (*.png *.xpm *.jpg);;"
 
161
                                       "All Files (*)"));
 
162
    if (!fileNames.isEmpty()) {
 
163
        foreach (QString fileName, fileNames) {
 
164
            int row = imagesTable->rowCount();
 
165
            imagesTable->setRowCount(row + 1);
 
166
 
 
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);
 
171
 
 
172
            QTableWidgetItem *item1 = new QTableWidgetItem(tr("Normal"));
 
173
            QTableWidgetItem *item2 = new QTableWidgetItem(tr("Off"));
 
174
 
 
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"));
 
180
                }
 
181
 
 
182
                if (fileName.contains("_on"))
 
183
                    item2->setText(tr("On"));
 
184
            }
 
185
 
 
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);
 
191
 
 
192
            item0->setCheckState(Qt::Checked);
 
193
        }
 
194
    }
 
195
}
 
196
 
 
197
void MainWindow::removeAllImages()
 
198
{
 
199
    imagesTable->setRowCount(0);
 
200
    changeIcon();
 
201
}
 
202
 
 
203
void MainWindow::createPreviewGroupBox()
 
204
{
 
205
    previewGroupBox = new QGroupBox(tr("Preview"));
 
206
 
 
207
    previewArea = new IconPreviewArea;
 
208
 
 
209
    QVBoxLayout *layout = new QVBoxLayout;
 
210
    layout->addWidget(previewArea);
 
211
    previewGroupBox->setLayout(layout);
 
212
}
 
213
 
 
214
void MainWindow::createImagesGroupBox()
 
215
{
 
216
    imagesGroupBox = new QGroupBox(tr("Images"));
 
217
    imagesGroupBox->setSizePolicy(QSizePolicy::Expanding,
 
218
                                  QSizePolicy::Expanding);
 
219
 
 
220
    QStringList labels;
 
221
    labels << tr("Image") << tr("Mode") << tr("State");
 
222
 
 
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));
 
229
 
 
230
    imagesTable->horizontalHeader()->resizeSection(0, 160);
 
231
    imagesTable->horizontalHeader()->resizeSection(1, 80);
 
232
    imagesTable->horizontalHeader()->resizeSection(2, 80);
 
233
    imagesTable->verticalHeader()->hide();
 
234
 
 
235
    connect(imagesTable, SIGNAL(itemChanged(QTableWidgetItem *)),
 
236
            this, SLOT(changeIcon()));
 
237
 
 
238
    QVBoxLayout *layout = new QVBoxLayout;
 
239
    layout->addWidget(imagesTable);
 
240
    imagesGroupBox->setLayout(layout);
 
241
}
 
242
 
 
243
void MainWindow::createIconSizeGroupBox()
 
244
{
 
245
    iconSizeGroupBox = new QGroupBox(tr("Icon Size"));
 
246
 
 
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:"));
 
253
 
 
254
    otherSpinBox = new IconSizeSpinBox;
 
255
    otherSpinBox->setRange(8, 128);
 
256
    otherSpinBox->setValue(64);
 
257
 
 
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()));
 
268
 
 
269
    QHBoxLayout *otherSizeLayout = new QHBoxLayout;
 
270
    otherSizeLayout->addWidget(otherRadioButton);
 
271
    otherSizeLayout->addWidget(otherSpinBox);
 
272
 
 
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);
 
281
}
 
282
 
 
283
void MainWindow::createActions()
 
284
{
 
285
    addImageAct = new QAction(tr("&Add Image..."), this);
 
286
    addImageAct->setShortcut(tr("Ctrl+A"));
 
287
    connect(addImageAct, SIGNAL(triggered()), this, SLOT(addImage()));
 
288
 
 
289
    removeAllImagesAct = new QAction(tr("&Remove All Images"), this);
 
290
    removeAllImagesAct->setShortcut(tr("Ctrl+R"));
 
291
    connect(removeAllImagesAct, SIGNAL(triggered()),
 
292
            this, SLOT(removeAllImages()));
 
293
 
 
294
    exitAct = new QAction(tr("&Exit"), this);
 
295
    exitAct->setShortcut(tr("Ctrl+Q"));
 
296
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
297
 
 
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)));
 
305
    }
 
306
 
 
307
    guessModeStateAct = new QAction(tr("&Guess Image Mode/State"), this);
 
308
    guessModeStateAct->setCheckable(true);
 
309
    guessModeStateAct->setChecked(true);
 
310
 
 
311
    aboutAct = new QAction(tr("&About"), this);
 
312
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
313
 
 
314
    aboutQtAct = new QAction(tr("About &Qt"), this);
 
315
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
316
}
 
317
 
 
318
void MainWindow::createMenus()
 
319
{
 
320
    fileMenu = menuBar()->addMenu(tr("&File"));
 
321
    fileMenu->addAction(addImageAct);
 
322
    fileMenu->addAction(removeAllImagesAct);
 
323
    fileMenu->addSeparator();
 
324
    fileMenu->addAction(exitAct);
 
325
 
 
326
    viewMenu = menuBar()->addMenu(tr("&View"));
 
327
    foreach (QAction *action, styleActionGroup->actions())
 
328
        viewMenu->addAction(action);
 
329
    viewMenu->addSeparator();
 
330
    viewMenu->addAction(guessModeStateAct);
 
331
 
 
332
    menuBar()->addSeparator();
 
333
 
 
334
    helpMenu = menuBar()->addMenu(tr("&Help"));
 
335
    helpMenu->addAction(aboutAct);
 
336
    helpMenu->addAction(aboutQtAct);
 
337
}
 
338
 
 
339
void MainWindow::createContextMenu()
 
340
{
 
341
    imagesTable->setContextMenuPolicy(Qt::ActionsContextMenu);
 
342
    imagesTable->addAction(addImageAct);
 
343
    imagesTable->addAction(removeAllImagesAct);
 
344
}
 
345
 
 
346
void MainWindow::checkCurrentStyle()
 
347
{
 
348
    foreach (QAction *action, styleActionGroup->actions()) {
 
349
        QString styleName = action->data().toString();
 
350
        QStyle *candidate = QStyleFactory::create(styleName);
 
351
        Q_ASSERT(candidate);
 
352
        if (candidate->metaObject()->className()
 
353
                == QApplication::style()->metaObject()->className()) {
 
354
            action->trigger();
 
355
            return;
 
356
        }
 
357
        delete candidate;
 
358
    }
 
359
}