examples/widgets/icons/iconpreviewarea.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 
       
    46 //! [0]
       
    47 IconPreviewArea::IconPreviewArea(QWidget *parent)
       
    48     : QWidget(parent)
       
    49 {
       
    50     QGridLayout *mainLayout = new QGridLayout;
       
    51     setLayout(mainLayout);
       
    52 
       
    53     stateLabels[0] = createHeaderLabel(tr("Off"));
       
    54     stateLabels[1] = createHeaderLabel(tr("On"));
       
    55     Q_ASSERT(NumStates == 2);
       
    56 
       
    57     modeLabels[0] = createHeaderLabel(tr("Normal"));
       
    58     modeLabels[1] = createHeaderLabel(tr("Active"));
       
    59     modeLabels[2] = createHeaderLabel(tr("Disabled"));
       
    60     modeLabels[3] = createHeaderLabel(tr("Selected"));
       
    61     Q_ASSERT(NumModes == 4);
       
    62 
       
    63     for (int j = 0; j < NumStates; ++j)
       
    64         mainLayout->addWidget(stateLabels[j], j + 1, 0);
       
    65 
       
    66     for (int i = 0; i < NumModes; ++i) {
       
    67         mainLayout->addWidget(modeLabels[i], 0, i + 1);
       
    68 
       
    69         for (int j = 0; j < NumStates; ++j) {
       
    70             pixmapLabels[i][j] = createPixmapLabel();
       
    71             mainLayout->addWidget(pixmapLabels[i][j], j + 1, i + 1);
       
    72         }
       
    73     }
       
    74 }
       
    75 //! [0]
       
    76 
       
    77 //! [1]
       
    78 void IconPreviewArea::setIcon(const QIcon &icon)
       
    79 {
       
    80     this->icon = icon;
       
    81     updatePixmapLabels();
       
    82 }
       
    83 //! [1]
       
    84 
       
    85 //! [2]
       
    86 void IconPreviewArea::setSize(const QSize &size)
       
    87 {
       
    88     if (size != this->size) {
       
    89         this->size = size;
       
    90         updatePixmapLabels();
       
    91     }
       
    92 }
       
    93 //! [2]
       
    94 
       
    95 //! [3]
       
    96 QLabel *IconPreviewArea::createHeaderLabel(const QString &text)
       
    97 {
       
    98     QLabel *label = new QLabel(tr("<b>%1</b>").arg(text));
       
    99     label->setAlignment(Qt::AlignCenter);
       
   100     return label;
       
   101 }
       
   102 //! [3]
       
   103 
       
   104 //! [4]
       
   105 QLabel *IconPreviewArea::createPixmapLabel()
       
   106 {
       
   107     QLabel *label = new QLabel;
       
   108     label->setEnabled(false);
       
   109     label->setAlignment(Qt::AlignCenter);
       
   110     label->setFrameShape(QFrame::Box);
       
   111     label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   112     label->setBackgroundRole(QPalette::Base);
       
   113     label->setAutoFillBackground(true);
       
   114     label->setMinimumSize(132, 132);
       
   115     return label;
       
   116 }
       
   117 //! [4]
       
   118 
       
   119 //! [5]
       
   120 void IconPreviewArea::updatePixmapLabels()
       
   121 {
       
   122     for (int i = 0; i < NumModes; ++i) {
       
   123         QIcon::Mode mode;
       
   124         if (i == 0) {
       
   125             mode = QIcon::Normal;
       
   126         } else if (i == 1) {
       
   127             mode = QIcon::Active;
       
   128         } else if (i == 2) {
       
   129             mode = QIcon::Disabled;
       
   130         } else {
       
   131             mode = QIcon::Selected;
       
   132         }
       
   133 
       
   134         for (int j = 0; j < NumStates; ++j) {
       
   135             QIcon::State state = (j == 0) ? QIcon::Off : QIcon::On;
       
   136             QPixmap pixmap = icon.pixmap(size, mode, state);
       
   137             pixmapLabels[i][j]->setPixmap(pixmap);
       
   138             pixmapLabels[i][j]->setEnabled(!pixmap.isNull());
       
   139         }
       
   140     }
       
   141 }
       
   142 //! [5]