1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "iconpreviewarea.h" |
52 | |
53 | #include <QGridLayout> |
54 | #include <QLabel> |
55 | |
56 | //! [0] |
57 | IconPreviewArea::IconPreviewArea(QWidget *parent) |
58 | : QWidget(parent) |
59 | { |
60 | QGridLayout *mainLayout = new QGridLayout(this); |
61 | |
62 | for (int row = 0; row < NumStates; ++row) { |
63 | stateLabels[row] = createHeaderLabel(text: IconPreviewArea::iconStateNames().at(i: row)); |
64 | mainLayout->addWidget(stateLabels[row], row: row + 1, column: 0); |
65 | } |
66 | Q_ASSERT(NumStates == 2); |
67 | |
68 | for (int column = 0; column < NumModes; ++column) { |
69 | modeLabels[column] = createHeaderLabel(text: IconPreviewArea::iconModeNames().at(i: column)); |
70 | mainLayout->addWidget(modeLabels[column], row: 0, column: column + 1); |
71 | } |
72 | Q_ASSERT(NumModes == 4); |
73 | |
74 | for (int column = 0; column < NumModes; ++column) { |
75 | for (int row = 0; row < NumStates; ++row) { |
76 | pixmapLabels[column][row] = createPixmapLabel(); |
77 | mainLayout->addWidget(pixmapLabels[column][row], row: row + 1, column: column + 1); |
78 | } |
79 | } |
80 | } |
81 | //! [0] |
82 | |
83 | //! [42] |
84 | QVector<QIcon::Mode> IconPreviewArea::iconModes() |
85 | { |
86 | static const QVector<QIcon::Mode> result = {QIcon::Normal, QIcon::Active, QIcon::Disabled, QIcon::Selected}; |
87 | return result; |
88 | } |
89 | |
90 | QVector<QIcon::State> IconPreviewArea::iconStates() |
91 | { |
92 | static const QVector<QIcon::State> result = {QIcon::Off, QIcon::On}; |
93 | return result; |
94 | } |
95 | |
96 | QStringList IconPreviewArea::iconModeNames() |
97 | { |
98 | static const QStringList result = {tr(s: "Normal" ), tr(s: "Active" ), tr(s: "Disabled" ), tr(s: "Selected" )}; |
99 | return result; |
100 | } |
101 | |
102 | QStringList IconPreviewArea::iconStateNames() |
103 | { |
104 | static const QStringList result = {tr(s: "Off" ), tr(s: "On" )}; |
105 | return result; |
106 | } |
107 | //! [42] |
108 | |
109 | //! [1] |
110 | void IconPreviewArea::setIcon(const QIcon &icon) |
111 | { |
112 | this->icon = icon; |
113 | updatePixmapLabels(); |
114 | } |
115 | //! [1] |
116 | |
117 | //! [2] |
118 | void IconPreviewArea::setSize(const QSize &size) |
119 | { |
120 | if (size != this->size) { |
121 | this->size = size; |
122 | updatePixmapLabels(); |
123 | } |
124 | } |
125 | //! [2] |
126 | |
127 | //! [3] |
128 | QLabel *IconPreviewArea::(const QString &text) |
129 | { |
130 | QLabel *label = new QLabel(tr(s: "<b>%1</b>" ).arg(a: text)); |
131 | label->setAlignment(Qt::AlignCenter); |
132 | return label; |
133 | } |
134 | //! [3] |
135 | |
136 | //! [4] |
137 | QLabel *IconPreviewArea::createPixmapLabel() |
138 | { |
139 | QLabel *label = new QLabel; |
140 | label->setEnabled(false); |
141 | label->setAlignment(Qt::AlignCenter); |
142 | label->setFrameShape(QFrame::Box); |
143 | label->setSizePolicy(hor: QSizePolicy::Expanding, ver: QSizePolicy::Expanding); |
144 | label->setBackgroundRole(QPalette::Base); |
145 | label->setAutoFillBackground(true); |
146 | label->setMinimumSize(minw: 132, minh: 132); |
147 | return label; |
148 | } |
149 | //! [4] |
150 | |
151 | //! [5] |
152 | void IconPreviewArea::updatePixmapLabels() |
153 | { |
154 | QWindow *window = nullptr; |
155 | if (const QWidget *nativeParent = nativeParentWidget()) |
156 | window = nativeParent->windowHandle(); |
157 | for (int column = 0; column < NumModes; ++column) { |
158 | for (int row = 0; row < NumStates; ++row) { |
159 | const QPixmap pixmap = |
160 | icon.pixmap(window, size, mode: IconPreviewArea::iconModes().at(i: column), |
161 | state: IconPreviewArea::iconStates().at(i: row)); |
162 | QLabel *pixmapLabel = pixmapLabels[column][row]; |
163 | pixmapLabel->setPixmap(pixmap); |
164 | pixmapLabel->setEnabled(!pixmap.isNull()); |
165 | QString toolTip; |
166 | if (!pixmap.isNull()) { |
167 | const QSize actualSize = icon.actualSize(size); |
168 | toolTip = |
169 | tr(s: "Size: %1x%2\nActual size: %3x%4\nDevice pixel ratio: %5" ) |
170 | .arg(a: size.width()).arg(a: size.height()) |
171 | .arg(a: actualSize.width()).arg(a: actualSize.height()) |
172 | .arg(a: pixmap.devicePixelRatioF()); |
173 | } |
174 | pixmapLabel->setToolTip(toolTip); |
175 | } |
176 | } |
177 | } |
178 | //! [5] |
179 | |