examples/widgets/icons/mainwindow.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "iconpreviewarea.h"
       
    45 #include "iconsizespinbox.h"
       
    46 #include "imagedelegate.h"
       
    47 #include "mainwindow.h"
       
    48 
       
    49 //! [0]
       
    50 MainWindow::MainWindow()
       
    51 {
       
    52     centralWidget = new QWidget;
       
    53     setCentralWidget(centralWidget);
       
    54 
       
    55     createPreviewGroupBox();
       
    56     createImagesGroupBox();
       
    57     createIconSizeGroupBox();
       
    58 
       
    59     createActions();
       
    60     createMenus();
       
    61     createContextMenu();
       
    62 
       
    63     QGridLayout *mainLayout = new QGridLayout;
       
    64     mainLayout->addWidget(previewGroupBox, 0, 0, 1, 2);
       
    65     mainLayout->addWidget(imagesGroupBox, 1, 0);
       
    66     mainLayout->addWidget(iconSizeGroupBox, 1, 1);
       
    67     centralWidget->setLayout(mainLayout);
       
    68 
       
    69     setWindowTitle(tr("Icons"));
       
    70     checkCurrentStyle();
       
    71     otherRadioButton->click();
       
    72 
       
    73     resize(minimumSizeHint());
       
    74 }
       
    75 //! [0]
       
    76 
       
    77 //! [1]
       
    78 void MainWindow::about()
       
    79 {
       
    80     QMessageBox::about(this, tr("About Icons"),
       
    81             tr("The <b>Icons</b> example illustrates how Qt renders an icon in "
       
    82                "different modes (active, normal, disabled, and selected) and "
       
    83                "states (on and off) based on a set of images."));
       
    84 }
       
    85 //! [1]
       
    86 
       
    87 //! [2]
       
    88 void MainWindow::changeStyle(bool checked)
       
    89 {
       
    90     if (!checked)
       
    91         return;
       
    92 
       
    93     QAction *action = qobject_cast<QAction *>(sender());
       
    94 //! [2] //! [3]
       
    95     QStyle *style = QStyleFactory::create(action->data().toString());
       
    96 //! [3] //! [4]
       
    97     Q_ASSERT(style);
       
    98     QApplication::setStyle(style);
       
    99 
       
   100     smallRadioButton->setText(tr("Small (%1 x %1)")
       
   101             .arg(style->pixelMetric(QStyle::PM_SmallIconSize)));
       
   102     largeRadioButton->setText(tr("Large (%1 x %1)")
       
   103             .arg(style->pixelMetric(QStyle::PM_LargeIconSize)));
       
   104     toolBarRadioButton->setText(tr("Toolbars (%1 x %1)")
       
   105             .arg(style->pixelMetric(QStyle::PM_ToolBarIconSize)));
       
   106     listViewRadioButton->setText(tr("List views (%1 x %1)")
       
   107             .arg(style->pixelMetric(QStyle::PM_ListViewIconSize)));
       
   108     iconViewRadioButton->setText(tr("Icon views (%1 x %1)")
       
   109             .arg(style->pixelMetric(QStyle::PM_IconViewIconSize)));
       
   110     tabBarRadioButton->setText(tr("Tab bars (%1 x %1)")
       
   111             .arg(style->pixelMetric(QStyle::PM_TabBarIconSize)));
       
   112 
       
   113     changeSize();
       
   114 }
       
   115 //! [4]
       
   116 
       
   117 //! [5]
       
   118 void MainWindow::changeSize(bool checked)
       
   119 {
       
   120     if (!checked)
       
   121         return;
       
   122 
       
   123     int extent;
       
   124 
       
   125     if (otherRadioButton->isChecked()) {
       
   126         extent = otherSpinBox->value();
       
   127     } else {
       
   128         QStyle::PixelMetric metric;
       
   129 
       
   130         if (smallRadioButton->isChecked()) {
       
   131             metric = QStyle::PM_SmallIconSize;
       
   132         } else if (largeRadioButton->isChecked()) {
       
   133             metric = QStyle::PM_LargeIconSize;
       
   134         } else if (toolBarRadioButton->isChecked()) {
       
   135             metric = QStyle::PM_ToolBarIconSize;
       
   136         } else if (listViewRadioButton->isChecked()) {
       
   137             metric = QStyle::PM_ListViewIconSize;
       
   138         } else if (iconViewRadioButton->isChecked()) {
       
   139             metric = QStyle::PM_IconViewIconSize;
       
   140         } else {
       
   141             metric = QStyle::PM_TabBarIconSize;
       
   142         }
       
   143         extent = QApplication::style()->pixelMetric(metric);
       
   144     }
       
   145     previewArea->setSize(QSize(extent, extent));
       
   146     otherSpinBox->setEnabled(otherRadioButton->isChecked());
       
   147 }
       
   148 //! [5]
       
   149 
       
   150 //! [6]
       
   151 void MainWindow::changeIcon()
       
   152 {
       
   153     QIcon icon;
       
   154 
       
   155     for (int row = 0; row < imagesTable->rowCount(); ++row) {
       
   156         QTableWidgetItem *item0 = imagesTable->item(row, 0);
       
   157         QTableWidgetItem *item1 = imagesTable->item(row, 1);
       
   158         QTableWidgetItem *item2 = imagesTable->item(row, 2);
       
   159 
       
   160         if (item0->checkState() == Qt::Checked) {
       
   161             QIcon::Mode mode;
       
   162             if (item1->text() == tr("Normal")) {
       
   163                 mode = QIcon::Normal;
       
   164             } else if (item1->text() == tr("Active")) {
       
   165                 mode = QIcon::Active;
       
   166             } else if (item1->text() == tr("Disabled")) {
       
   167                 mode = QIcon::Disabled;
       
   168             } else {
       
   169                 mode = QIcon::Selected;
       
   170             }
       
   171 
       
   172             QIcon::State state;
       
   173             if (item2->text() == tr("On")) {
       
   174                 state = QIcon::On;
       
   175             } else {
       
   176                 state = QIcon::Off;
       
   177 //! [6] //! [7]
       
   178             }
       
   179 //! [7]
       
   180 
       
   181 //! [8]
       
   182             QString fileName = item0->data(Qt::UserRole).toString();
       
   183             QImage image(fileName);
       
   184             if (!image.isNull())
       
   185                 icon.addPixmap(QPixmap::fromImage(image), mode, state);
       
   186 //! [8] //! [9]
       
   187         }
       
   188 //! [9] //! [10]
       
   189     }
       
   190 //! [10]
       
   191 
       
   192 //! [11]
       
   193     previewArea->setIcon(icon);
       
   194 }
       
   195 //! [11]
       
   196 
       
   197 //! [12]
       
   198 void MainWindow::addImages()
       
   199 {
       
   200     QStringList fileNames = QFileDialog::getOpenFileNames(this,
       
   201                                     tr("Open Images"), "",
       
   202                                     tr("Images (*.png *.xpm *.jpg);;"
       
   203                                        "All Files (*)"));
       
   204     if (!fileNames.isEmpty()) {
       
   205         foreach (QString fileName, fileNames) {
       
   206             int row = imagesTable->rowCount();
       
   207             imagesTable->setRowCount(row + 1);
       
   208 //! [12]
       
   209 
       
   210 //! [13]
       
   211             QString imageName = QFileInfo(fileName).baseName();
       
   212 //! [13] //! [14]
       
   213             QTableWidgetItem *item0 = new QTableWidgetItem(imageName);
       
   214             item0->setData(Qt::UserRole, fileName);
       
   215             item0->setFlags(item0->flags() & ~Qt::ItemIsEditable);
       
   216 //! [14]
       
   217 
       
   218 //! [15]
       
   219             QTableWidgetItem *item1 = new QTableWidgetItem(tr("Normal"));
       
   220 //! [15] //! [16]
       
   221             QTableWidgetItem *item2 = new QTableWidgetItem(tr("Off"));
       
   222 
       
   223             if (guessModeStateAct->isChecked()) {
       
   224                 if (fileName.contains("_act")) {
       
   225                     item1->setText(tr("Active"));
       
   226                 } else if (fileName.contains("_dis")) {
       
   227                     item1->setText(tr("Disabled"));
       
   228                 } else if (fileName.contains("_sel")) {
       
   229                     item1->setText(tr("Selected"));
       
   230                 }
       
   231 
       
   232                 if (fileName.contains("_on"))
       
   233                     item2->setText(tr("On"));
       
   234 //! [16] //! [17]
       
   235             }
       
   236 //! [17]
       
   237 
       
   238 //! [18]
       
   239             imagesTable->setItem(row, 0, item0);
       
   240 //! [18] //! [19]
       
   241             imagesTable->setItem(row, 1, item1);
       
   242             imagesTable->setItem(row, 2, item2);
       
   243             imagesTable->openPersistentEditor(item1);
       
   244             imagesTable->openPersistentEditor(item2);
       
   245 
       
   246             item0->setCheckState(Qt::Checked);
       
   247         }
       
   248     }
       
   249 }
       
   250 //! [19]
       
   251 
       
   252 //! [20]
       
   253 void MainWindow::removeAllImages()
       
   254 {
       
   255     imagesTable->setRowCount(0);
       
   256     changeIcon();
       
   257 }
       
   258 //! [20]
       
   259 
       
   260 void MainWindow::createPreviewGroupBox()
       
   261 {
       
   262     previewGroupBox = new QGroupBox(tr("Preview"));
       
   263 
       
   264     previewArea = new IconPreviewArea;
       
   265 
       
   266     QVBoxLayout *layout = new QVBoxLayout;
       
   267     layout->addWidget(previewArea);
       
   268     previewGroupBox->setLayout(layout);
       
   269 }
       
   270 
       
   271 //! [21]
       
   272 void MainWindow::createImagesGroupBox()
       
   273 {
       
   274     imagesGroupBox = new QGroupBox(tr("Images"));
       
   275 
       
   276     imagesTable = new QTableWidget;
       
   277     imagesTable->setSelectionMode(QAbstractItemView::NoSelection);
       
   278     imagesTable->setItemDelegate(new ImageDelegate(this));
       
   279 //! [21]
       
   280 
       
   281 //! [22]
       
   282     QStringList labels;
       
   283 //! [22] //! [23]
       
   284     labels << tr("Image") << tr("Mode") << tr("State");
       
   285 
       
   286     imagesTable->horizontalHeader()->setDefaultSectionSize(90);
       
   287     imagesTable->setColumnCount(3);
       
   288     imagesTable->setHorizontalHeaderLabels(labels);
       
   289     imagesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
       
   290     imagesTable->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
       
   291     imagesTable->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
       
   292     imagesTable->verticalHeader()->hide();
       
   293 //! [23]
       
   294 
       
   295 //! [24]
       
   296     connect(imagesTable, SIGNAL(itemChanged(QTableWidgetItem *)),
       
   297 //! [24] //! [25]
       
   298             this, SLOT(changeIcon()));
       
   299 
       
   300     QVBoxLayout *layout = new QVBoxLayout;
       
   301     layout->addWidget(imagesTable);
       
   302     imagesGroupBox->setLayout(layout);
       
   303 }
       
   304 //! [25]
       
   305 
       
   306 //! [26]
       
   307 void MainWindow::createIconSizeGroupBox()
       
   308 {
       
   309     iconSizeGroupBox = new QGroupBox(tr("Icon Size"));
       
   310 
       
   311     smallRadioButton = new QRadioButton;
       
   312     largeRadioButton = new QRadioButton;
       
   313     toolBarRadioButton = new QRadioButton;
       
   314     listViewRadioButton = new QRadioButton;
       
   315     iconViewRadioButton = new QRadioButton;
       
   316     tabBarRadioButton = new QRadioButton;
       
   317     otherRadioButton = new QRadioButton(tr("Other:"));
       
   318 
       
   319     otherSpinBox = new IconSizeSpinBox;
       
   320     otherSpinBox->setRange(8, 128);
       
   321     otherSpinBox->setValue(64);
       
   322 //! [26]
       
   323 
       
   324 //! [27]
       
   325     connect(smallRadioButton, SIGNAL(toggled(bool)),
       
   326             this, SLOT(changeSize(bool)));
       
   327     connect(largeRadioButton, SIGNAL(toggled(bool)),
       
   328             this, SLOT(changeSize(bool)));
       
   329     connect(toolBarRadioButton, SIGNAL(toggled(bool)),
       
   330             this, SLOT(changeSize(bool)));
       
   331     connect(listViewRadioButton, SIGNAL(toggled(bool)),
       
   332             this, SLOT(changeSize(bool)));
       
   333     connect(iconViewRadioButton, SIGNAL(toggled(bool)),
       
   334             this, SLOT(changeSize(bool)));
       
   335     connect(tabBarRadioButton, SIGNAL(toggled(bool)),
       
   336             this, SLOT(changeSize(bool)));
       
   337     connect(otherRadioButton, SIGNAL(toggled(bool)),
       
   338             this, SLOT(changeSize(bool)));
       
   339     connect(otherSpinBox, SIGNAL(valueChanged(int)), this, SLOT(changeSize()));
       
   340 
       
   341     QHBoxLayout *otherSizeLayout = new QHBoxLayout;
       
   342     otherSizeLayout->addWidget(otherRadioButton);
       
   343     otherSizeLayout->addWidget(otherSpinBox);
       
   344     otherSizeLayout->addStretch();
       
   345 
       
   346     QGridLayout *layout = new QGridLayout;
       
   347     layout->addWidget(smallRadioButton, 0, 0);
       
   348     layout->addWidget(largeRadioButton, 1, 0);
       
   349     layout->addWidget(toolBarRadioButton, 2, 0);
       
   350     layout->addWidget(listViewRadioButton, 0, 1);
       
   351     layout->addWidget(iconViewRadioButton, 1, 1);
       
   352     layout->addWidget(tabBarRadioButton, 2, 1);
       
   353     layout->addLayout(otherSizeLayout, 3, 0, 1, 2);
       
   354     layout->setRowStretch(4, 1);
       
   355     iconSizeGroupBox->setLayout(layout);
       
   356 }
       
   357 //! [27]
       
   358 
       
   359 //! [28]
       
   360 void MainWindow::createActions()
       
   361 {
       
   362     addImagesAct = new QAction(tr("&Add Images..."), this);
       
   363     addImagesAct->setShortcut(tr("Ctrl+A"));
       
   364     connect(addImagesAct, SIGNAL(triggered()), this, SLOT(addImages()));
       
   365 
       
   366     removeAllImagesAct = new QAction(tr("&Remove All Images"), this);
       
   367     removeAllImagesAct->setShortcut(tr("Ctrl+R"));
       
   368     connect(removeAllImagesAct, SIGNAL(triggered()),
       
   369             this, SLOT(removeAllImages()));
       
   370 
       
   371     exitAct = new QAction(tr("&Quit"), this);
       
   372     exitAct->setShortcuts(QKeySequence::Quit);
       
   373     connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
       
   374 
       
   375     styleActionGroup = new QActionGroup(this);
       
   376     foreach (QString styleName, QStyleFactory::keys()) {
       
   377         QAction *action = new QAction(styleActionGroup);
       
   378         action->setText(tr("%1 Style").arg(styleName));
       
   379         action->setData(styleName);
       
   380         action->setCheckable(true);
       
   381         connect(action, SIGNAL(triggered(bool)), this, SLOT(changeStyle(bool)));
       
   382     }
       
   383 
       
   384     guessModeStateAct = new QAction(tr("&Guess Image Mode/State"), this);
       
   385     guessModeStateAct->setCheckable(true);
       
   386     guessModeStateAct->setChecked(true);
       
   387 
       
   388     aboutAct = new QAction(tr("&About"), this);
       
   389     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
       
   390 
       
   391     aboutQtAct = new QAction(tr("About &Qt"), this);
       
   392     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
       
   393 }
       
   394 //! [28]
       
   395 
       
   396 //! [29]
       
   397 void MainWindow::createMenus()
       
   398 {
       
   399     fileMenu = menuBar()->addMenu(tr("&File"));
       
   400     fileMenu->addAction(addImagesAct);
       
   401     fileMenu->addAction(removeAllImagesAct);
       
   402     fileMenu->addSeparator();
       
   403     fileMenu->addAction(exitAct);
       
   404 
       
   405     viewMenu = menuBar()->addMenu(tr("&View"));
       
   406     foreach (QAction *action, styleActionGroup->actions())
       
   407         viewMenu->addAction(action);
       
   408     viewMenu->addSeparator();
       
   409     viewMenu->addAction(guessModeStateAct);
       
   410 
       
   411     menuBar()->addSeparator();
       
   412 
       
   413     helpMenu = menuBar()->addMenu(tr("&Help"));
       
   414     helpMenu->addAction(aboutAct);
       
   415     helpMenu->addAction(aboutQtAct);
       
   416 }
       
   417 //! [29]
       
   418 
       
   419 //! [30]
       
   420 void MainWindow::createContextMenu()
       
   421 {
       
   422     imagesTable->setContextMenuPolicy(Qt::ActionsContextMenu);
       
   423     imagesTable->addAction(addImagesAct);
       
   424     imagesTable->addAction(removeAllImagesAct);
       
   425 }
       
   426 //! [30]
       
   427 
       
   428 //! [31]
       
   429 void MainWindow::checkCurrentStyle()
       
   430 {
       
   431     foreach (QAction *action, styleActionGroup->actions()) {
       
   432         QString styleName = action->data().toString();
       
   433         QStyle *candidate = QStyleFactory::create(styleName);
       
   434         Q_ASSERT(candidate);
       
   435         if (candidate->metaObject()->className()
       
   436                 == QApplication::style()->metaObject()->className()) {
       
   437             action->trigger();
       
   438             return;
       
   439         }
       
   440         delete candidate;
       
   441     }
       
   442 }
       
   443 //! [31]