| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/image/qicon.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com> | - | ||||||||||||||||||
| 5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
| 6 | ** | - | ||||||||||||||||||
| 7 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||||||||
| 8 | ** | - | ||||||||||||||||||
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||
| 10 | ** Commercial License Usage | - | ||||||||||||||||||
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
| 12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
| 13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||
| 17 | ** | - | ||||||||||||||||||
| 18 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
| 20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
| 22 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
| 25 | ** | - | ||||||||||||||||||
| 26 | ** GNU General Public License Usage | - | ||||||||||||||||||
| 27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
| 28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
| 29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
| 30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
| 32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
| 33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
| 36 | ** | - | ||||||||||||||||||
| 37 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 38 | ** | - | ||||||||||||||||||
| 39 | ****************************************************************************/ | - | ||||||||||||||||||
| 40 | - | |||||||||||||||||||
| 41 | #include "qicon.h" | - | ||||||||||||||||||
| 42 | #include "qicon_p.h" | - | ||||||||||||||||||
| 43 | #include "qiconengine.h" | - | ||||||||||||||||||
| 44 | #include "qiconengineplugin.h" | - | ||||||||||||||||||
| 45 | #include "qimagereader.h" | - | ||||||||||||||||||
| 46 | #include "private/qfactoryloader_p.h" | - | ||||||||||||||||||
| 47 | #include "private/qiconloader_p.h" | - | ||||||||||||||||||
| 48 | #include "qpainter.h" | - | ||||||||||||||||||
| 49 | #include "qfileinfo.h" | - | ||||||||||||||||||
| 50 | #include <qmimedatabase.h> | - | ||||||||||||||||||
| 51 | #include <qmimetype.h> | - | ||||||||||||||||||
| 52 | #include "qpixmapcache.h" | - | ||||||||||||||||||
| 53 | #include "qvariant.h" | - | ||||||||||||||||||
| 54 | #include "qcache.h" | - | ||||||||||||||||||
| 55 | #include "qdebug.h" | - | ||||||||||||||||||
| 56 | #include "qdir.h" | - | ||||||||||||||||||
| 57 | #include "qpalette.h" | - | ||||||||||||||||||
| 58 | #include "qmath.h" | - | ||||||||||||||||||
| 59 | - | |||||||||||||||||||
| 60 | #include "private/qhexstring_p.h" | - | ||||||||||||||||||
| 61 | #include "private/qguiapplication_p.h" | - | ||||||||||||||||||
| 62 | #include "qpa/qplatformtheme.h" | - | ||||||||||||||||||
| 63 | - | |||||||||||||||||||
| 64 | #ifndef QT_NO_ICON | - | ||||||||||||||||||
| 65 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 66 | - | |||||||||||||||||||
| 67 | /*! | - | ||||||||||||||||||
| 68 | \enum QIcon::Mode | - | ||||||||||||||||||
| 69 | - | |||||||||||||||||||
| 70 | This enum type describes the mode for which a pixmap is intended | - | ||||||||||||||||||
| 71 | to be used. The currently defined modes are: | - | ||||||||||||||||||
| 72 | - | |||||||||||||||||||
| 73 | \value Normal | - | ||||||||||||||||||
| 74 | Display the pixmap when the user is | - | ||||||||||||||||||
| 75 | not interacting with the icon, but the | - | ||||||||||||||||||
| 76 | functionality represented by the icon is available. | - | ||||||||||||||||||
| 77 | \value Disabled | - | ||||||||||||||||||
| 78 | Display the pixmap when the | - | ||||||||||||||||||
| 79 | functionality represented by the icon is not available. | - | ||||||||||||||||||
| 80 | \value Active | - | ||||||||||||||||||
| 81 | Display the pixmap when the | - | ||||||||||||||||||
| 82 | functionality represented by the icon is available and | - | ||||||||||||||||||
| 83 | the user is interacting with the icon, for example, moving the | - | ||||||||||||||||||
| 84 | mouse over it or clicking it. | - | ||||||||||||||||||
| 85 | \value Selected | - | ||||||||||||||||||
| 86 | Display the pixmap when the item represented by the icon is | - | ||||||||||||||||||
| 87 | selected. | - | ||||||||||||||||||
| 88 | */ | - | ||||||||||||||||||
| 89 | - | |||||||||||||||||||
| 90 | /*! | - | ||||||||||||||||||
| 91 | \enum QIcon::State | - | ||||||||||||||||||
| 92 | - | |||||||||||||||||||
| 93 | This enum describes the state for which a pixmap is intended to be | - | ||||||||||||||||||
| 94 | used. The \e state can be: | - | ||||||||||||||||||
| 95 | - | |||||||||||||||||||
| 96 | \value Off Display the pixmap when the widget is in an "off" state | - | ||||||||||||||||||
| 97 | \value On Display the pixmap when the widget is in an "on" state | - | ||||||||||||||||||
| 98 | */ | - | ||||||||||||||||||
| 99 | - | |||||||||||||||||||
| 100 | static QBasicAtomicInt serialNumCounter = Q_BASIC_ATOMIC_INITIALIZER(1); | - | ||||||||||||||||||
| 101 | - | |||||||||||||||||||
| 102 | static void qt_cleanup_icon_cache(); | - | ||||||||||||||||||
| 103 | namespace { | - | ||||||||||||||||||
| 104 | struct IconCache : public QCache<QString, QIcon> | - | ||||||||||||||||||
| 105 | { | - | ||||||||||||||||||
| 106 | IconCache() | - | ||||||||||||||||||
| 107 | { | - | ||||||||||||||||||
| 108 | // ### note: won't readd if QApplication is re-created! | - | ||||||||||||||||||
| 109 | qAddPostRoutine(qt_cleanup_icon_cache); | - | ||||||||||||||||||
| 110 | } | - | ||||||||||||||||||
| 111 | }; | - | ||||||||||||||||||
| 112 | } | - | ||||||||||||||||||
| 113 | - | |||||||||||||||||||
| 114 | Q_GLOBAL_STATIC(IconCache, qtIconCache) | - | ||||||||||||||||||
| 115 | - | |||||||||||||||||||
| 116 | static void qt_cleanup_icon_cache() | - | ||||||||||||||||||
| 117 | { | - | ||||||||||||||||||
| 118 | qtIconCache()->clear(); | - | ||||||||||||||||||
| 119 | } | - | ||||||||||||||||||
| 120 | - | |||||||||||||||||||
| 121 | /*! \internal | - | ||||||||||||||||||
| 122 | - | |||||||||||||||||||
| 123 | Returns the effective device pixel ratio, using | - | ||||||||||||||||||
| 124 | the provided window pointer if possible. | - | ||||||||||||||||||
| 125 | - | |||||||||||||||||||
| 126 | if Qt::AA_UseHighDpiPixmaps is not set this function | - | ||||||||||||||||||
| 127 | returns 1.0 to keep non-hihdpi aware code working. | - | ||||||||||||||||||
| 128 | */ | - | ||||||||||||||||||
| 129 | static qreal qt_effective_device_pixel_ratio(QWindow *window = 0) | - | ||||||||||||||||||
| 130 | { | - | ||||||||||||||||||
| 131 | if (!qApp->testAttribute(Qt::AA_UseHighDpiPixmaps)) | - | ||||||||||||||||||
| 132 | return qreal(1.0); | - | ||||||||||||||||||
| 133 | - | |||||||||||||||||||
| 134 | if (window) | - | ||||||||||||||||||
| 135 | return window->devicePixelRatio(); | - | ||||||||||||||||||
| 136 | - | |||||||||||||||||||
| 137 | return qApp->devicePixelRatio(); // Don't know which window to target. | - | ||||||||||||||||||
| 138 | } | - | ||||||||||||||||||
| 139 | - | |||||||||||||||||||
| 140 | QIconPrivate::QIconPrivate() | - | ||||||||||||||||||
| 141 | : engine(0), ref(1), | - | ||||||||||||||||||
| 142 | serialNum(serialNumCounter.fetchAndAddRelaxed(1)), | - | ||||||||||||||||||
| 143 | detach_no(0), | - | ||||||||||||||||||
| 144 | is_mask(false) | - | ||||||||||||||||||
| 145 | { | - | ||||||||||||||||||
| 146 | } | - | ||||||||||||||||||
| 147 | - | |||||||||||||||||||
| 148 | /*! \internal | - | ||||||||||||||||||
| 149 | Computes the displayDevicePixelRatio for a pixmap. | - | ||||||||||||||||||
| 150 | - | |||||||||||||||||||
| 151 | If displayDevicePixelRatio is 1.0 the reurned value is 1.0, always. | - | ||||||||||||||||||
| 152 | - | |||||||||||||||||||
| 153 | For a displayDevicePixelRatio of 2.0 the returned value will be between | - | ||||||||||||||||||
| 154 | 1.0 and 2.0, depending on requestedSize and actualsize: | - | ||||||||||||||||||
| 155 | * If actualsize < requestedSize : 1.0 (not enough pixels for a normal-dpi pixmap) | - | ||||||||||||||||||
| 156 | * If actualsize == requestedSize * 2.0 : 2.0 (enough pixels for a high-dpi pixmap) | - | ||||||||||||||||||
| 157 | * else : a scaled value between 1.0 and 2.0. (pixel count is between normal-dpi and high-dpi) | - | ||||||||||||||||||
| 158 | */ | - | ||||||||||||||||||
| 159 | qreal QIconPrivate::pixmapDevicePixelRatio(qreal displayDevicePixelRatio, const QSize &requestedSize, const QSize &actualSize) | - | ||||||||||||||||||
| 160 | { | - | ||||||||||||||||||
| 161 | QSize targetSize = requestedSize * displayDevicePixelRatio; | - | ||||||||||||||||||
| 162 | qreal scale = 0.5 * (qreal(actualSize.width()) / qreal(targetSize.width()) + | - | ||||||||||||||||||
| 163 | qreal(actualSize.height() / qreal(targetSize.height()))); | - | ||||||||||||||||||
| 164 | return qMax(qreal(1.0), displayDevicePixelRatio *scale); | - | ||||||||||||||||||
| 165 | } | - | ||||||||||||||||||
| 166 | - | |||||||||||||||||||
| 167 | QPixmapIconEngine::QPixmapIconEngine() | - | ||||||||||||||||||
| 168 | { | - | ||||||||||||||||||
| 169 | } | - | ||||||||||||||||||
| 170 | - | |||||||||||||||||||
| 171 | QPixmapIconEngine::QPixmapIconEngine(const QPixmapIconEngine &other) | - | ||||||||||||||||||
| 172 | : QIconEngine(other), pixmaps(other.pixmaps) | - | ||||||||||||||||||
| 173 | { | - | ||||||||||||||||||
| 174 | } | - | ||||||||||||||||||
| 175 | - | |||||||||||||||||||
| 176 | QPixmapIconEngine::~QPixmapIconEngine() | - | ||||||||||||||||||
| 177 | { | - | ||||||||||||||||||
| 178 | } | - | ||||||||||||||||||
| 179 | - | |||||||||||||||||||
| 180 | void QPixmapIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) | - | ||||||||||||||||||
| 181 | { | - | ||||||||||||||||||
| 182 | QSize pixmapSize = rect.size() * qt_effective_device_pixel_ratio(0); | - | ||||||||||||||||||
| 183 | QPixmap px = pixmap(pixmapSize, mode, state); | - | ||||||||||||||||||
| 184 | painter->drawPixmap(rect, px); | - | ||||||||||||||||||
| 185 | } | - | ||||||||||||||||||
| 186 | - | |||||||||||||||||||
| 187 | static inline int area(const QSize &s) { return s.width() * s.height(); } | - | ||||||||||||||||||
| 188 | - | |||||||||||||||||||
| 189 | // returns the smallest of the two that is still larger than or equal to size. | - | ||||||||||||||||||
| 190 | static QPixmapIconEngineEntry *bestSizeMatch( const QSize &size, QPixmapIconEngineEntry *pa, QPixmapIconEngineEntry *pb) | - | ||||||||||||||||||
| 191 | { | - | ||||||||||||||||||
| 192 | int s = area(size); | - | ||||||||||||||||||
| 193 | if (pa->size == QSize() && pa->pixmap.isNull()) { | - | ||||||||||||||||||
| 194 | pa->pixmap = QPixmap(pa->fileName); | - | ||||||||||||||||||
| 195 | pa->size = pa->pixmap.size(); | - | ||||||||||||||||||
| 196 | } | - | ||||||||||||||||||
| 197 | int a = area(pa->size); | - | ||||||||||||||||||
| 198 | if (pb->size == QSize() && pb->pixmap.isNull()) { | - | ||||||||||||||||||
| 199 | pb->pixmap = QPixmap(pb->fileName); | - | ||||||||||||||||||
| 200 | pb->size = pb->pixmap.size(); | - | ||||||||||||||||||
| 201 | } | - | ||||||||||||||||||
| 202 | int b = area(pb->size); | - | ||||||||||||||||||
| 203 | int res = a; | - | ||||||||||||||||||
| 204 | if (qMin(a,b) >= s) | - | ||||||||||||||||||
| 205 | res = qMin(a,b); | - | ||||||||||||||||||
| 206 | else | - | ||||||||||||||||||
| 207 | res = qMax(a,b); | - | ||||||||||||||||||
| 208 | if (res == a) | - | ||||||||||||||||||
| 209 | return pa; | - | ||||||||||||||||||
| 210 | return pb; | - | ||||||||||||||||||
| 211 | } | - | ||||||||||||||||||
| 212 | - | |||||||||||||||||||
| 213 | QPixmapIconEngineEntry *QPixmapIconEngine::tryMatch(const QSize &size, QIcon::Mode mode, QIcon::State state) | - | ||||||||||||||||||
| 214 | { | - | ||||||||||||||||||
| 215 | QPixmapIconEngineEntry *pe = 0; | - | ||||||||||||||||||
| 216 | for (int i = 0; i < pixmaps.count(); ++i) | - | ||||||||||||||||||
| 217 | if (pixmaps.at(i).mode == mode && pixmaps.at(i).state == state) { | - | ||||||||||||||||||
| 218 | if (pe) | - | ||||||||||||||||||
| 219 | pe = bestSizeMatch(size, &pixmaps[i], pe); | - | ||||||||||||||||||
| 220 | else | - | ||||||||||||||||||
| 221 | pe = &pixmaps[i]; | - | ||||||||||||||||||
| 222 | } | - | ||||||||||||||||||
| 223 | return pe; | - | ||||||||||||||||||
| 224 | } | - | ||||||||||||||||||
| 225 | - | |||||||||||||||||||
| 226 | - | |||||||||||||||||||
| 227 | QPixmapIconEngineEntry *QPixmapIconEngine::bestMatch(const QSize &size, QIcon::Mode mode, QIcon::State state, bool sizeOnly) | - | ||||||||||||||||||
| 228 | { | - | ||||||||||||||||||
| 229 | QPixmapIconEngineEntry *pe = tryMatch(size, mode, state); | - | ||||||||||||||||||
| 230 | while (!pe){ | - | ||||||||||||||||||
| 231 | QIcon::State oppositeState = (state == QIcon::On) ? QIcon::Off : QIcon::On; | - | ||||||||||||||||||
| 232 | if (mode == QIcon::Disabled || mode == QIcon::Selected) { | - | ||||||||||||||||||
| 233 | QIcon::Mode oppositeMode = (mode == QIcon::Disabled) ? QIcon::Selected : QIcon::Disabled; | - | ||||||||||||||||||
| 234 | if ((pe = tryMatch(size, QIcon::Normal, state))) | - | ||||||||||||||||||
| 235 | break; | - | ||||||||||||||||||
| 236 | if ((pe = tryMatch(size, QIcon::Active, state))) | - | ||||||||||||||||||
| 237 | break; | - | ||||||||||||||||||
| 238 | if ((pe = tryMatch(size, mode, oppositeState))) | - | ||||||||||||||||||
| 239 | break; | - | ||||||||||||||||||
| 240 | if ((pe = tryMatch(size, QIcon::Normal, oppositeState))) | - | ||||||||||||||||||
| 241 | break; | - | ||||||||||||||||||
| 242 | if ((pe = tryMatch(size, QIcon::Active, oppositeState))) | - | ||||||||||||||||||
| 243 | break; | - | ||||||||||||||||||
| 244 | if ((pe = tryMatch(size, oppositeMode, state))) | - | ||||||||||||||||||
| 245 | break; | - | ||||||||||||||||||
| 246 | if ((pe = tryMatch(size, oppositeMode, oppositeState))) | - | ||||||||||||||||||
| 247 | break; | - | ||||||||||||||||||
| 248 | } else { | - | ||||||||||||||||||
| 249 | QIcon::Mode oppositeMode = (mode == QIcon::Normal) ? QIcon::Active : QIcon::Normal; | - | ||||||||||||||||||
| 250 | if ((pe = tryMatch(size, oppositeMode, state))) | - | ||||||||||||||||||
| 251 | break; | - | ||||||||||||||||||
| 252 | if ((pe = tryMatch(size, mode, oppositeState))) | - | ||||||||||||||||||
| 253 | break; | - | ||||||||||||||||||
| 254 | if ((pe = tryMatch(size, oppositeMode, oppositeState))) | - | ||||||||||||||||||
| 255 | break; | - | ||||||||||||||||||
| 256 | if ((pe = tryMatch(size, QIcon::Disabled, state))) | - | ||||||||||||||||||
| 257 | break; | - | ||||||||||||||||||
| 258 | if ((pe = tryMatch(size, QIcon::Selected, state))) | - | ||||||||||||||||||
| 259 | break; | - | ||||||||||||||||||
| 260 | if ((pe = tryMatch(size, QIcon::Disabled, oppositeState))) | - | ||||||||||||||||||
| 261 | break; | - | ||||||||||||||||||
| 262 | if ((pe = tryMatch(size, QIcon::Selected, oppositeState))) | - | ||||||||||||||||||
| 263 | break; | - | ||||||||||||||||||
| 264 | } | - | ||||||||||||||||||
| 265 | - | |||||||||||||||||||
| 266 | if (!pe) | - | ||||||||||||||||||
| 267 | return pe; | - | ||||||||||||||||||
| 268 | } | - | ||||||||||||||||||
| 269 | - | |||||||||||||||||||
| 270 | if (sizeOnly ? (pe->size.isNull() || !pe->size.isValid()) : pe->pixmap.isNull()) { | - | ||||||||||||||||||
| 271 | pe->pixmap = QPixmap(pe->fileName); | - | ||||||||||||||||||
| 272 | if (!pe->pixmap.isNull()) | - | ||||||||||||||||||
| 273 | pe->size = pe->pixmap.size(); | - | ||||||||||||||||||
| 274 | } | - | ||||||||||||||||||
| 275 | - | |||||||||||||||||||
| 276 | return pe; | - | ||||||||||||||||||
| 277 | } | - | ||||||||||||||||||
| 278 | - | |||||||||||||||||||
| 279 | QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) | - | ||||||||||||||||||
| 280 | { | - | ||||||||||||||||||
| 281 | QPixmap pm; | - | ||||||||||||||||||
| 282 | QPixmapIconEngineEntry *pe = bestMatch(size, mode, state, false); | - | ||||||||||||||||||
| 283 | if (pe)
| 0 | ||||||||||||||||||
| 284 | pm = pe->pixmap; never executed: pm = pe->pixmap; | 0 | ||||||||||||||||||
| 285 | - | |||||||||||||||||||
| 286 | if (pm.isNull()) {
| 0 | ||||||||||||||||||
| 287 | int idx = pixmaps.count(); | - | ||||||||||||||||||
| 288 | while (--idx >= 0) {
| 0 | ||||||||||||||||||
| 289 | if (pe == &pixmaps[.at(idx]))) {
| 0 | ||||||||||||||||||
| 290 | pixmaps.remove(idx); | - | ||||||||||||||||||
| 291 | break; never executed: break; | 0 | ||||||||||||||||||
| 292 | } | - | ||||||||||||||||||
| 293 | } never executed: end of block | 0 | ||||||||||||||||||
| 294 | if (pixmaps.isEmpty())
| 0 | ||||||||||||||||||
| 295 | return pm; never executed: return pm; | 0 | ||||||||||||||||||
| 296 | else | - | ||||||||||||||||||
| 297 | return pixmap(size, mode, state); never executed: return pixmap(size, mode, state); | 0 | ||||||||||||||||||
| 298 | } | - | ||||||||||||||||||
| 299 | - | |||||||||||||||||||
| 300 | QSize actualSize = pm.size(); | - | ||||||||||||||||||
| 301 | if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
| 0 | ||||||||||||||||||
| 302 | actualSize.scale(size, Qt::KeepAspectRatio); never executed: actualSize.scale(size, Qt::KeepAspectRatio); | 0 | ||||||||||||||||||
| 303 | - | |||||||||||||||||||
| 304 | QString key = QLatin1String("qt_") | - | ||||||||||||||||||
| 305 | % HexString<quint64>(pm.cacheKey()) | - | ||||||||||||||||||
| 306 | % HexString<uint>(pe ? pe->mode : QIcon::Normal) | - | ||||||||||||||||||
| 307 | % HexString<quint64>(QGuiApplication::palette().cacheKey()) | - | ||||||||||||||||||
| 308 | % HexString<uint>(actualSize.width()) | - | ||||||||||||||||||
| 309 | % HexString<uint>(actualSize.height()); | - | ||||||||||||||||||
| 310 | - | |||||||||||||||||||
| 311 | if (mode == QIcon::Active) {
| 0 | ||||||||||||||||||
| 312 | if (QPixmapCache::find(key % HexString<uint>(mode), pm))
| 0 | ||||||||||||||||||
| 313 | return pm; // horray never executed: return pm; | 0 | ||||||||||||||||||
| 314 | if (QPixmapCache::find(key % HexString<uint>(QIcon::Normal), pm)) {
| 0 | ||||||||||||||||||
| 315 | QPixmap active = pm; | - | ||||||||||||||||||
| 316 | if (QGuiApplication *guiApp = qobject_cast<QGuiApplication *>(qApp))
| 0 | ||||||||||||||||||
| 317 | active = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(QIcon::Active, pm); never executed: active = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(QIcon::Active, pm); | 0 | ||||||||||||||||||
| 318 | if (pm.cacheKey() == active.cacheKey())
| 0 | ||||||||||||||||||
| 319 | return pm; never executed: return pm; | 0 | ||||||||||||||||||
| 320 | } never executed: end of block | 0 | ||||||||||||||||||
| 321 | } never executed: end of block | 0 | ||||||||||||||||||
| 322 | - | |||||||||||||||||||
| 323 | if (!QPixmapCache::find(key % HexString<uint>(mode), pm)) {
| 0 | ||||||||||||||||||
| 324 | if (pm.size() != actualSize)
| 0 | ||||||||||||||||||
| 325 | pm = pm.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); never executed: pm = pm.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); | 0 | ||||||||||||||||||
| 326 | if (pe->mode != mode && mode != QIcon::Normal) {
| 0 | ||||||||||||||||||
| 327 | QPixmap generated = pm; | - | ||||||||||||||||||
| 328 | if (QGuiApplication *guiApp = qobject_cast<QGuiApplication *>(qApp))
| 0 | ||||||||||||||||||
| 329 | generated = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(mode, pm); never executed: generated = static_cast<QGuiApplicationPrivate*>(QObjectPrivate::get(guiApp))->applyQIconStyleHelper(mode, pm); | 0 | ||||||||||||||||||
| 330 | if (!generated.isNull())
| 0 | ||||||||||||||||||
| 331 | pm = generated; never executed: pm = generated; | 0 | ||||||||||||||||||
| 332 | } never executed: end of block | 0 | ||||||||||||||||||
| 333 | QPixmapCache::insert(key % HexString<uint>(mode), pm); | - | ||||||||||||||||||
| 334 | } never executed: end of block | 0 | ||||||||||||||||||
| 335 | return pm; never executed: return pm; | 0 | ||||||||||||||||||
| 336 | } | - | ||||||||||||||||||
| 337 | - | |||||||||||||||||||
| 338 | QSize QPixmapIconEngine::actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) | - | ||||||||||||||||||
| 339 | { | - | ||||||||||||||||||
| 340 | QSize actualSize; | - | ||||||||||||||||||
| 341 | if (QPixmapIconEngineEntry *pe = bestMatch(size, mode, state, true)) | - | ||||||||||||||||||
| 342 | actualSize = pe->size; | - | ||||||||||||||||||
| 343 | - | |||||||||||||||||||
| 344 | if (actualSize.isNull()) | - | ||||||||||||||||||
| 345 | return actualSize; | - | ||||||||||||||||||
| 346 | - | |||||||||||||||||||
| 347 | if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height())) | - | ||||||||||||||||||
| 348 | actualSize.scale(size, Qt::KeepAspectRatio); | - | ||||||||||||||||||
| 349 | return actualSize; | - | ||||||||||||||||||
| 350 | } | - | ||||||||||||||||||
| 351 | - | |||||||||||||||||||
| 352 | void QPixmapIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state) | - | ||||||||||||||||||
| 353 | { | - | ||||||||||||||||||
| 354 | if (!pixmap.isNull()) { | - | ||||||||||||||||||
| 355 | QPixmapIconEngineEntry *pe = tryMatch(pixmap.size(), mode, state); | - | ||||||||||||||||||
| 356 | if(pe && pe->size == pixmap.size()) { | - | ||||||||||||||||||
| 357 | pe->pixmap = pixmap; | - | ||||||||||||||||||
| 358 | pe->fileName.clear(); | - | ||||||||||||||||||
| 359 | } else { | - | ||||||||||||||||||
| 360 | pixmaps += QPixmapIconEngineEntry(pixmap, mode, state); | - | ||||||||||||||||||
| 361 | } | - | ||||||||||||||||||
| 362 | } | - | ||||||||||||||||||
| 363 | } | - | ||||||||||||||||||
| 364 | - | |||||||||||||||||||
| 365 | // Read out original image depth as set by ICOReader | - | ||||||||||||||||||
| 366 | static inline int origIcoDepth(const QImage &image) | - | ||||||||||||||||||
| 367 | { | - | ||||||||||||||||||
| 368 | const QString s = image.text(QStringLiteral("_q_icoOrigDepth")); | - | ||||||||||||||||||
| 369 | return s.isEmpty() ? 32 : s.toInt(); | - | ||||||||||||||||||
| 370 | } | - | ||||||||||||||||||
| 371 | - | |||||||||||||||||||
| 372 | static inline int findBySize(const QVector<QImage> &images, const QSize &size) | - | ||||||||||||||||||
| 373 | { | - | ||||||||||||||||||
| 374 | for (int i = 0; i < images.size(); ++i) { | - | ||||||||||||||||||
| 375 | if (images.at(i).size() == size) | - | ||||||||||||||||||
| 376 | return i; | - | ||||||||||||||||||
| 377 | } | - | ||||||||||||||||||
| 378 | return -1; | - | ||||||||||||||||||
| 379 | } | - | ||||||||||||||||||
| 380 | - | |||||||||||||||||||
| 381 | // Convenience class providing a bool read() function. | - | ||||||||||||||||||
| 382 | namespace { | - | ||||||||||||||||||
| 383 | class ImageReader | - | ||||||||||||||||||
| 384 | { | - | ||||||||||||||||||
| 385 | public: | - | ||||||||||||||||||
| 386 | ImageReader(const QString &fileName) : m_reader(fileName), m_atEnd(false) {} | - | ||||||||||||||||||
| 387 | - | |||||||||||||||||||
| 388 | QByteArray format() const { return m_reader.format(); } | - | ||||||||||||||||||
| 389 | - | |||||||||||||||||||
| 390 | bool read(QImage *image) | - | ||||||||||||||||||
| 391 | { | - | ||||||||||||||||||
| 392 | if (m_atEnd) | - | ||||||||||||||||||
| 393 | return false; | - | ||||||||||||||||||
| 394 | *image = m_reader.read(); | - | ||||||||||||||||||
| 395 | if (!image->size().isValid()) { | - | ||||||||||||||||||
| 396 | m_atEnd = true; | - | ||||||||||||||||||
| 397 | return false; | - | ||||||||||||||||||
| 398 | } | - | ||||||||||||||||||
| 399 | m_atEnd = !m_reader.jumpToNextImage(); | - | ||||||||||||||||||
| 400 | return true; | - | ||||||||||||||||||
| 401 | } | - | ||||||||||||||||||
| 402 | - | |||||||||||||||||||
| 403 | private: | - | ||||||||||||||||||
| 404 | QImageReader m_reader; | - | ||||||||||||||||||
| 405 | bool m_atEnd; | - | ||||||||||||||||||
| 406 | }; | - | ||||||||||||||||||
| 407 | } // namespace | - | ||||||||||||||||||
| 408 | - | |||||||||||||||||||
| 409 | void QPixmapIconEngine::addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state) | - | ||||||||||||||||||
| 410 | { | - | ||||||||||||||||||
| 411 | if (fileName.isEmpty())
| 0 | ||||||||||||||||||
| 412 | return; never executed: return; | 0 | ||||||||||||||||||
| 413 | const QString abs = fileName.startsWith(QLatin1Char(':')) ? fileName : QFileInfo(fileName).absoluteFilePath();
| 0 | ||||||||||||||||||
| 414 | const bool ignoreSize = !size.isValid(); | - | ||||||||||||||||||
| 415 | ImageReader imageReader(abs); | - | ||||||||||||||||||
| 416 | const QByteArray format = imageReader.format(); | - | ||||||||||||||||||
| 417 | if (format.isEmpty()) // Device failed to open or unsupported format.
| 0 | ||||||||||||||||||
| 418 | return; never executed: return; | 0 | ||||||||||||||||||
| 419 | QImage image; | - | ||||||||||||||||||
| 420 | if (format != "ico") {
| 0 | ||||||||||||||||||
| 421 | if (ignoreSize) { // No size specified: Add all images.
| 0 | ||||||||||||||||||
| 422 | while (imageReader.read(&image))
| 0 | ||||||||||||||||||
| 423 | pixmaps += QPixmapIconEngineEntry(abs, image, mode, state); never executed: pixmaps += QPixmapIconEngineEntry(abs, image, mode, state); | 0 | ||||||||||||||||||
| 424 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 425 | // Try to match size. If that fails, add a placeholder with the filename and empty pixmap for the size. | - | ||||||||||||||||||
| 426 | while (imageReader.read(&image) && image.size() != size) {} never executed: end of block
| 0 | ||||||||||||||||||
| 427 | pixmaps += image.size() == size ?
| 0 | ||||||||||||||||||
| 428 | QPixmapIconEngineEntry(abs, image, mode, state) : QPixmapIconEngineEntry(abs, size, mode, state); | - | ||||||||||||||||||
| 429 | } never executed: end of block | 0 | ||||||||||||||||||
| 430 | return; never executed: return; | 0 | ||||||||||||||||||
| 431 | } | - | ||||||||||||||||||
| 432 | // Special case for reading Windows ".ico" files. Historically (QTBUG-39287), | - | ||||||||||||||||||
| 433 | // these files may contain low-resolution images. As this information is lost, | - | ||||||||||||||||||
| 434 | // ICOReader sets the original format as an image text key value. Read all matching | - | ||||||||||||||||||
| 435 | // images into a list trying to find the highest quality per size. | - | ||||||||||||||||||
| 436 | QVector<QImage> icoImages; | - | ||||||||||||||||||
| 437 | while (imageReader.read(&image)) {
| 0 | ||||||||||||||||||
| 438 | if (ignoreSize || image.size() == size) {
| 0 | ||||||||||||||||||
| 439 | const int position = findBySize(icoImages, image.size()); | - | ||||||||||||||||||
| 440 | if (position >= 0) { // Higher quality available? -> replace.
| 0 | ||||||||||||||||||
| 441 | if (origIcoDepth(image) > origIcoDepth(icoImages.at(position)))
| 0 | ||||||||||||||||||
| 442 | icoImages[position] = image; never executed: icoImages[position] = image; | 0 | ||||||||||||||||||
| 443 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 444 | icoImages.append(image); | - | ||||||||||||||||||
| 445 | } never executed: end of block | 0 | ||||||||||||||||||
| 446 | } | - | ||||||||||||||||||
| 447 | } never executed: end of block | 0 | ||||||||||||||||||
| 448 | foreachfor (const QImage &i ,: qAsConst(icoImages))) | - | ||||||||||||||||||
| 449 | pixmaps += QPixmapIconEngineEntry(abs, i, mode, state); never executed: pixmaps += QPixmapIconEngineEntry(abs, i, mode, state); | 0 | ||||||||||||||||||
| 450 | if (icoImages.isEmpty() && !ignoreSize) // Add placeholder with the filename and empty pixmap for the size.
| 0 | ||||||||||||||||||
| 451 | pixmaps += QPixmapIconEngineEntry(abs, size, mode, state); never executed: pixmaps += QPixmapIconEngineEntry(abs, size, mode, state); | 0 | ||||||||||||||||||
| 452 | } never executed: end of block | 0 | ||||||||||||||||||
| 453 | - | |||||||||||||||||||
| 454 | QString QPixmapIconEngine::key() const | - | ||||||||||||||||||
| 455 | { | - | ||||||||||||||||||
| 456 | return QLatin1String("QPixmapIconEngine"); | - | ||||||||||||||||||
| 457 | } | - | ||||||||||||||||||
| 458 | - | |||||||||||||||||||
| 459 | QIconEngine *QPixmapIconEngine::clone() const | - | ||||||||||||||||||
| 460 | { | - | ||||||||||||||||||
| 461 | return new QPixmapIconEngine(*this); | - | ||||||||||||||||||
| 462 | } | - | ||||||||||||||||||
| 463 | - | |||||||||||||||||||
| 464 | bool QPixmapIconEngine::read(QDataStream &in) | - | ||||||||||||||||||
| 465 | { | - | ||||||||||||||||||
| 466 | int num_entries; | - | ||||||||||||||||||
| 467 | QPixmap pm; | - | ||||||||||||||||||
| 468 | QString fileName; | - | ||||||||||||||||||
| 469 | QSize sz; | - | ||||||||||||||||||
| 470 | uint mode; | - | ||||||||||||||||||
| 471 | uint state; | - | ||||||||||||||||||
| 472 | - | |||||||||||||||||||
| 473 | in >> num_entries; | - | ||||||||||||||||||
| 474 | for (int i=0; i < num_entries; ++i) { | - | ||||||||||||||||||
| 475 | if (in.atEnd()) { | - | ||||||||||||||||||
| 476 | pixmaps.clear(); | - | ||||||||||||||||||
| 477 | return false; | - | ||||||||||||||||||
| 478 | } | - | ||||||||||||||||||
| 479 | in >> pm; | - | ||||||||||||||||||
| 480 | in >> fileName; | - | ||||||||||||||||||
| 481 | in >> sz; | - | ||||||||||||||||||
| 482 | in >> mode; | - | ||||||||||||||||||
| 483 | in >> state; | - | ||||||||||||||||||
| 484 | if (pm.isNull()) { | - | ||||||||||||||||||
| 485 | addFile(fileName, sz, QIcon::Mode(mode), QIcon::State(state)); | - | ||||||||||||||||||
| 486 | } else { | - | ||||||||||||||||||
| 487 | QPixmapIconEngineEntry pe(fileName, sz, QIcon::Mode(mode), QIcon::State(state)); | - | ||||||||||||||||||
| 488 | pe.pixmap = pm; | - | ||||||||||||||||||
| 489 | pixmaps += pe; | - | ||||||||||||||||||
| 490 | } | - | ||||||||||||||||||
| 491 | } | - | ||||||||||||||||||
| 492 | return true; | - | ||||||||||||||||||
| 493 | } | - | ||||||||||||||||||
| 494 | - | |||||||||||||||||||
| 495 | bool QPixmapIconEngine::write(QDataStream &out) const | - | ||||||||||||||||||
| 496 | { | - | ||||||||||||||||||
| 497 | int num_entries = pixmaps.size(); | - | ||||||||||||||||||
| 498 | out << num_entries; | - | ||||||||||||||||||
| 499 | for (int i=0; i < num_entries; ++i) { | - | ||||||||||||||||||
| 500 | if (pixmaps.at(i).pixmap.isNull()) | - | ||||||||||||||||||
| 501 | out << QPixmap(pixmaps.at(i).fileName); | - | ||||||||||||||||||
| 502 | else | - | ||||||||||||||||||
| 503 | out << pixmaps.at(i).pixmap; | - | ||||||||||||||||||
| 504 | out << pixmaps.at(i).fileName; | - | ||||||||||||||||||
| 505 | out << pixmaps.at(i).size; | - | ||||||||||||||||||
| 506 | out << (uint) pixmaps.at(i).mode; | - | ||||||||||||||||||
| 507 | out << (uint) pixmaps.at(i).state; | - | ||||||||||||||||||
| 508 | } | - | ||||||||||||||||||
| 509 | return true; | - | ||||||||||||||||||
| 510 | } | - | ||||||||||||||||||
| 511 | - | |||||||||||||||||||
| 512 | void QPixmapIconEngine::virtual_hook(int id, void *data) | - | ||||||||||||||||||
| 513 | { | - | ||||||||||||||||||
| 514 | switch (id) { | - | ||||||||||||||||||
| 515 | case QIconEngine::AvailableSizesHook: { | - | ||||||||||||||||||
| 516 | QIconEngine::AvailableSizesArgument &arg = | - | ||||||||||||||||||
| 517 | *reinterpret_cast<QIconEngine::AvailableSizesArgument*>(data); | - | ||||||||||||||||||
| 518 | arg.sizes.clear(); | - | ||||||||||||||||||
| 519 | for (int i = 0; i < pixmaps.size(); ++i) { | - | ||||||||||||||||||
| 520 | QPixmapIconEngineEntry &pe = pixmaps[i]; | - | ||||||||||||||||||
| 521 | if (pe.size == QSize() && pe.pixmap.isNull()) { | - | ||||||||||||||||||
| 522 | pe.pixmap = QPixmap(pe.fileName); | - | ||||||||||||||||||
| 523 | pe.size = pe.pixmap.size(); | - | ||||||||||||||||||
| 524 | } | - | ||||||||||||||||||
| 525 | if (pe.mode == arg.mode && pe.state == arg.state && !pe.size.isEmpty()) | - | ||||||||||||||||||
| 526 | arg.sizes.push_back(pe.size); | - | ||||||||||||||||||
| 527 | } | - | ||||||||||||||||||
| 528 | break; | - | ||||||||||||||||||
| 529 | } | - | ||||||||||||||||||
| 530 | default: | - | ||||||||||||||||||
| 531 | QIconEngine::virtual_hook(id, data); | - | ||||||||||||||||||
| 532 | } | - | ||||||||||||||||||
| 533 | } | - | ||||||||||||||||||
| 534 | - | |||||||||||||||||||
| #ifndef QT_NO_LIBRARYQ_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, | ||||||||||||||||||||
| 536 | (QIconEngineFactoryInterface_iid, QLatin1String("/iconengines"), Qt::CaseInsensitive)) | - | ||||||||||||||||||
| 537 | - | |||||||||||||||||||
| 538 | QFactoryLoader *qt_iconEngineFactoryLoader() | - | ||||||||||||||||||
| 539 | { | - | ||||||||||||||||||
| 540 | return loader(); | - | ||||||||||||||||||
| 541 | } | - | ||||||||||||||||||
| 542 | - | |||||||||||||||||||
| 543 | - | |||||||||||||||||||
| #endif/*! | ||||||||||||||||||||
| 545 | \class QIcon | - | ||||||||||||||||||
| 546 | - | |||||||||||||||||||
| 547 | \brief The QIcon class provides scalable icons in different modes | - | ||||||||||||||||||
| 548 | and states. | - | ||||||||||||||||||
| 549 | - | |||||||||||||||||||
| 550 | \ingroup painting | - | ||||||||||||||||||
| 551 | \ingroup shared | - | ||||||||||||||||||
| 552 | \inmodule QtGui | - | ||||||||||||||||||
| 553 | - | |||||||||||||||||||
| 554 | A QIcon can generate smaller, larger, active, and disabled pixmaps | - | ||||||||||||||||||
| 555 | from the set of pixmaps it is given. Such pixmaps are used by Qt | - | ||||||||||||||||||
| 556 | widgets to show an icon representing a particular action. | - | ||||||||||||||||||
| 557 | - | |||||||||||||||||||
| 558 | The simplest use of QIcon is to create one from a QPixmap file or | - | ||||||||||||||||||
| 559 | resource, and then use it, allowing Qt to work out all the required | - | ||||||||||||||||||
| 560 | icon styles and sizes. For example: | - | ||||||||||||||||||
| 561 | - | |||||||||||||||||||
| 562 | \snippet code/src_gui_image_qicon.cpp 0 | - | ||||||||||||||||||
| 563 | - | |||||||||||||||||||
| 564 | To undo a QIcon, simply set a null icon in its place: | - | ||||||||||||||||||
| 565 | - | |||||||||||||||||||
| 566 | \snippet code/src_gui_image_qicon.cpp 1 | - | ||||||||||||||||||
| 567 | - | |||||||||||||||||||
| 568 | Use the QImageReader::supportedImageFormats() and | - | ||||||||||||||||||
| 569 | QImageWriter::supportedImageFormats() functions to retrieve a | - | ||||||||||||||||||
| 570 | complete list of the supported file formats. | - | ||||||||||||||||||
| 571 | - | |||||||||||||||||||
| 572 | When you retrieve a pixmap using pixmap(QSize, Mode, State), and no | - | ||||||||||||||||||
| 573 | pixmap for this given size, mode and state has been added with | - | ||||||||||||||||||
| 574 | addFile() or addPixmap(), then QIcon will generate one on the | - | ||||||||||||||||||
| 575 | fly. This pixmap generation happens in a QIconEngineV2. The default | - | ||||||||||||||||||
| 576 | engine scales pixmaps down if required, but never up, and it uses | - | ||||||||||||||||||
| 577 | the current style to calculate a disabled appearance. By using | - | ||||||||||||||||||
| 578 | custom icon engines, you can customize every aspect of generated | - | ||||||||||||||||||
| 579 | icons. With QIconEnginePluginV2 it is possible to register different | - | ||||||||||||||||||
| 580 | icon engines for different file suffixes, making it possible for | - | ||||||||||||||||||
| 581 | third parties to provide additional icon engines to those included | - | ||||||||||||||||||
| 582 | with Qt. | - | ||||||||||||||||||
| 583 | - | |||||||||||||||||||
| 584 | \note Since Qt 4.2, an icon engine that supports SVG is included. | - | ||||||||||||||||||
| 585 | - | |||||||||||||||||||
| 586 | \section1 Making Classes that Use QIcon | - | ||||||||||||||||||
| 587 | - | |||||||||||||||||||
| 588 | If you write your own widgets that have an option to set a small | - | ||||||||||||||||||
| 589 | pixmap, consider allowing a QIcon to be set for that pixmap. The | - | ||||||||||||||||||
| 590 | Qt class QToolButton is an example of such a widget. | - | ||||||||||||||||||
| 591 | - | |||||||||||||||||||
| 592 | Provide a method to set a QIcon, and when you draw the icon, choose | - | ||||||||||||||||||
| 593 | whichever pixmap is appropriate for the current state of your widget. | - | ||||||||||||||||||
| 594 | For example: | - | ||||||||||||||||||
| 595 | \snippet code/src_gui_image_qicon.cpp 2 | - | ||||||||||||||||||
| 596 | - | |||||||||||||||||||
| 597 | You might also make use of the \c Active mode, perhaps making your | - | ||||||||||||||||||
| 598 | widget \c Active when the mouse is over the widget (see \l | - | ||||||||||||||||||
| 599 | QWidget::enterEvent()), while the mouse is pressed pending the | - | ||||||||||||||||||
| 600 | release that will activate the function, or when it is the currently | - | ||||||||||||||||||
| 601 | selected item. If the widget can be toggled, the "On" mode might be | - | ||||||||||||||||||
| 602 | used to draw a different icon. | - | ||||||||||||||||||
| 603 | - | |||||||||||||||||||
| 604 | \image icon.png QIcon | - | ||||||||||||||||||
| 605 | - | |||||||||||||||||||
| 606 | \note QIcon needs a QGuiApplication instance before the icon is created. | - | ||||||||||||||||||
| 607 | - | |||||||||||||||||||
| 608 | \sa {fowler}{GUI Design Handbook: Iconic Label}, {Icons Example} | - | ||||||||||||||||||
| 609 | */ | - | ||||||||||||||||||
| 610 | - | |||||||||||||||||||
| 611 | - | |||||||||||||||||||
| 612 | /*! | - | ||||||||||||||||||
| 613 | Constructs a null icon. | - | ||||||||||||||||||
| 614 | */ | - | ||||||||||||||||||
| 615 | QIcon::QIcon() Q_DECL_NOEXCEPT | - | ||||||||||||||||||
| 616 | : d(0) | - | ||||||||||||||||||
| 617 | { | - | ||||||||||||||||||
| 618 | } never executed: end of block | 0 | ||||||||||||||||||
| 619 | - | |||||||||||||||||||
| 620 | /*! | - | ||||||||||||||||||
| 621 | Constructs an icon from a \a pixmap. | - | ||||||||||||||||||
| 622 | */ | - | ||||||||||||||||||
| 623 | QIcon::QIcon(const QPixmap &pixmap) | - | ||||||||||||||||||
| 624 | :d(0) | - | ||||||||||||||||||
| 625 | { | - | ||||||||||||||||||
| 626 | addPixmap(pixmap); | - | ||||||||||||||||||
| 627 | } | - | ||||||||||||||||||
| 628 | - | |||||||||||||||||||
| 629 | /*! | - | ||||||||||||||||||
| 630 | Constructs a copy of \a other. This is very fast. | - | ||||||||||||||||||
| 631 | */ | - | ||||||||||||||||||
| 632 | QIcon::QIcon(const QIcon &other) | - | ||||||||||||||||||
| 633 | :d(other.d) | - | ||||||||||||||||||
| 634 | { | - | ||||||||||||||||||
| 635 | if (d) | - | ||||||||||||||||||
| 636 | d->ref.ref(); | - | ||||||||||||||||||
| 637 | } | - | ||||||||||||||||||
| 638 | - | |||||||||||||||||||
| 639 | /*! | - | ||||||||||||||||||
| 640 | \fn QIcon::QIcon(QIcon &&other) | - | ||||||||||||||||||
| 641 | - | |||||||||||||||||||
| 642 | Move-constructs a QIcon instance, making it point to the same object | - | ||||||||||||||||||
| 643 | that \a other was pointing to. | - | ||||||||||||||||||
| 644 | */ | - | ||||||||||||||||||
| 645 | - | |||||||||||||||||||
| 646 | /*! | - | ||||||||||||||||||
| 647 | Constructs an icon from the file with the given \a fileName. The | - | ||||||||||||||||||
| 648 | file will be loaded on demand. | - | ||||||||||||||||||
| 649 | - | |||||||||||||||||||
| 650 | If \a fileName contains a relative path (e.g. the filename only) | - | ||||||||||||||||||
| 651 | the relevant file must be found relative to the runtime working | - | ||||||||||||||||||
| 652 | directory. | - | ||||||||||||||||||
| 653 | - | |||||||||||||||||||
| 654 | The file name can be either refer to an actual file on disk or to | - | ||||||||||||||||||
| 655 | one of the application's embedded resources. See the | - | ||||||||||||||||||
| 656 | \l{resources.html}{Resource System} overview for details on how to | - | ||||||||||||||||||
| 657 | embed images and other resource files in the application's | - | ||||||||||||||||||
| 658 | executable. | - | ||||||||||||||||||
| 659 | - | |||||||||||||||||||
| 660 | Use the QImageReader::supportedImageFormats() and | - | ||||||||||||||||||
| 661 | QImageWriter::supportedImageFormats() functions to retrieve a | - | ||||||||||||||||||
| 662 | complete list of the supported file formats. | - | ||||||||||||||||||
| 663 | */ | - | ||||||||||||||||||
| 664 | QIcon::QIcon(const QString &fileName) | - | ||||||||||||||||||
| 665 | : d(0) | - | ||||||||||||||||||
| 666 | { | - | ||||||||||||||||||
| 667 | addFile(fileName); | - | ||||||||||||||||||
| 668 | } | - | ||||||||||||||||||
| 669 | - | |||||||||||||||||||
| 670 | - | |||||||||||||||||||
| 671 | /*! | - | ||||||||||||||||||
| 672 | Creates an icon with a specific icon \a engine. The icon takes | - | ||||||||||||||||||
| 673 | ownership of the engine. | - | ||||||||||||||||||
| 674 | */ | - | ||||||||||||||||||
| 675 | QIcon::QIcon(QIconEngine *engine) | - | ||||||||||||||||||
| 676 | :d(new QIconPrivate) | - | ||||||||||||||||||
| 677 | { | - | ||||||||||||||||||
| 678 | d->engine = engine; | - | ||||||||||||||||||
| 679 | } | - | ||||||||||||||||||
| 680 | - | |||||||||||||||||||
| 681 | /*! | - | ||||||||||||||||||
| 682 | Destroys the icon. | - | ||||||||||||||||||
| 683 | */ | - | ||||||||||||||||||
| 684 | QIcon::~QIcon() | - | ||||||||||||||||||
| 685 | { | - | ||||||||||||||||||
| 686 | if (d && !d->ref.deref()) | - | ||||||||||||||||||
| 687 | delete d; | - | ||||||||||||||||||
| 688 | } | - | ||||||||||||||||||
| 689 | - | |||||||||||||||||||
| 690 | /*! | - | ||||||||||||||||||
| 691 | Assigns the \a other icon to this icon and returns a reference to | - | ||||||||||||||||||
| 692 | this icon. | - | ||||||||||||||||||
| 693 | */ | - | ||||||||||||||||||
| 694 | QIcon &QIcon::operator=(const QIcon &other) | - | ||||||||||||||||||
| 695 | { | - | ||||||||||||||||||
| 696 | if (other.d) | - | ||||||||||||||||||
| 697 | other.d->ref.ref(); | - | ||||||||||||||||||
| 698 | if (d && !d->ref.deref()) | - | ||||||||||||||||||
| 699 | delete d; | - | ||||||||||||||||||
| 700 | d = other.d; | - | ||||||||||||||||||
| 701 | return *this; | - | ||||||||||||||||||
| 702 | } | - | ||||||||||||||||||
| 703 | - | |||||||||||||||||||
| 704 | /*! | - | ||||||||||||||||||
| 705 | \fn QIcon &QIcon::operator=(QIcon &&other) | - | ||||||||||||||||||
| 706 | - | |||||||||||||||||||
| 707 | Move-assigns \a other to this QIcon instance. | - | ||||||||||||||||||
| 708 | - | |||||||||||||||||||
| 709 | \since 5.2 | - | ||||||||||||||||||
| 710 | */ | - | ||||||||||||||||||
| 711 | - | |||||||||||||||||||
| 712 | /*! | - | ||||||||||||||||||
| 713 | \fn void QIcon::swap(QIcon &other) | - | ||||||||||||||||||
| 714 | \since 4.8 | - | ||||||||||||||||||
| 715 | - | |||||||||||||||||||
| 716 | Swaps icon \a other with this icon. This operation is very | - | ||||||||||||||||||
| 717 | fast and never fails. | - | ||||||||||||||||||
| 718 | */ | - | ||||||||||||||||||
| 719 | - | |||||||||||||||||||
| 720 | /*! | - | ||||||||||||||||||
| 721 | Returns the icon as a QVariant. | - | ||||||||||||||||||
| 722 | */ | - | ||||||||||||||||||
| 723 | QIcon::operator QVariant() const | - | ||||||||||||||||||
| 724 | { | - | ||||||||||||||||||
| 725 | return QVariant(QVariant::Icon, this); | - | ||||||||||||||||||
| 726 | } | - | ||||||||||||||||||
| 727 | - | |||||||||||||||||||
| 728 | /*! \fn int QIcon::serialNumber() const | - | ||||||||||||||||||
| 729 | \obsolete | - | ||||||||||||||||||
| 730 | - | |||||||||||||||||||
| 731 | Returns a number that identifies the contents of this | - | ||||||||||||||||||
| 732 | QIcon object. Distinct QIcon objects can have | - | ||||||||||||||||||
| 733 | the same serial number if they refer to the same contents | - | ||||||||||||||||||
| 734 | (but they don't have to). Also, the serial number of | - | ||||||||||||||||||
| 735 | a QIcon object may change during its lifetime. | - | ||||||||||||||||||
| 736 | - | |||||||||||||||||||
| 737 | Use cacheKey() instead. | - | ||||||||||||||||||
| 738 | - | |||||||||||||||||||
| 739 | A null icon always has a serial number of 0. | - | ||||||||||||||||||
| 740 | - | |||||||||||||||||||
| 741 | Serial numbers are mostly useful in conjunction with caching. | - | ||||||||||||||||||
| 742 | - | |||||||||||||||||||
| 743 | \sa QPixmap::serialNumber() | - | ||||||||||||||||||
| 744 | */ | - | ||||||||||||||||||
| 745 | - | |||||||||||||||||||
| 746 | /*! | - | ||||||||||||||||||
| 747 | Returns a number that identifies the contents of this QIcon | - | ||||||||||||||||||
| 748 | object. Distinct QIcon objects can have the same key if | - | ||||||||||||||||||
| 749 | they refer to the same contents. | - | ||||||||||||||||||
| 750 | \since 4.3 | - | ||||||||||||||||||
| 751 | - | |||||||||||||||||||
| 752 | The cacheKey() will change when the icon is altered via | - | ||||||||||||||||||
| 753 | addPixmap() or addFile(). | - | ||||||||||||||||||
| 754 | - | |||||||||||||||||||
| 755 | Cache keys are mostly useful in conjunction with caching. | - | ||||||||||||||||||
| 756 | - | |||||||||||||||||||
| 757 | \sa QPixmap::cacheKey() | - | ||||||||||||||||||
| 758 | */ | - | ||||||||||||||||||
| 759 | qint64 QIcon::cacheKey() const | - | ||||||||||||||||||
| 760 | { | - | ||||||||||||||||||
| 761 | if (!d) | - | ||||||||||||||||||
| 762 | return 0; | - | ||||||||||||||||||
| 763 | return (((qint64) d->serialNum) << 32) | ((qint64) (d->detach_no)); | - | ||||||||||||||||||
| 764 | } | - | ||||||||||||||||||
| 765 | - | |||||||||||||||||||
| 766 | /*! | - | ||||||||||||||||||
| 767 | Returns a pixmap with the requested \a size, \a mode, and \a | - | ||||||||||||||||||
| 768 | state, generating one if necessary. The pixmap might be smaller than | - | ||||||||||||||||||
| 769 | requested, but never larger. | - | ||||||||||||||||||
| 770 | - | |||||||||||||||||||
| 771 | Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this | - | ||||||||||||||||||
| 772 | function to return pixmaps that are larger than the requested size. Such | - | ||||||||||||||||||
| 773 | images will have a devicePixelRatio larger than 1. | - | ||||||||||||||||||
| 774 | - | |||||||||||||||||||
| 775 | \sa actualSize(), paint() | - | ||||||||||||||||||
| 776 | */ | - | ||||||||||||||||||
| 777 | QPixmap QIcon::pixmap(const QSize &size, Mode mode, State state) const | - | ||||||||||||||||||
| 778 | { | - | ||||||||||||||||||
| 779 | if (!d) | - | ||||||||||||||||||
| 780 | return QPixmap(); | - | ||||||||||||||||||
| 781 | return pixmap(0, size, mode, state); | - | ||||||||||||||||||
| 782 | } | - | ||||||||||||||||||
| 783 | - | |||||||||||||||||||
| 784 | /*! | - | ||||||||||||||||||
| 785 | \fn QPixmap QIcon::pixmap(int w, int h, Mode mode = Normal, State state = Off) const | - | ||||||||||||||||||
| 786 | - | |||||||||||||||||||
| 787 | \overload | - | ||||||||||||||||||
| 788 | - | |||||||||||||||||||
| 789 | Returns a pixmap of size QSize(\a w, \a h). The pixmap might be smaller than | - | ||||||||||||||||||
| 790 | requested, but never larger. | - | ||||||||||||||||||
| 791 | - | |||||||||||||||||||
| 792 | Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this | - | ||||||||||||||||||
| 793 | function to return pixmaps that are larger than the requested size. Such | - | ||||||||||||||||||
| 794 | images will have a devicePixelRatio larger than 1. | - | ||||||||||||||||||
| 795 | */ | - | ||||||||||||||||||
| 796 | - | |||||||||||||||||||
| 797 | /*! | - | ||||||||||||||||||
| 798 | \fn QPixmap QIcon::pixmap(int extent, Mode mode = Normal, State state = Off) const | - | ||||||||||||||||||
| 799 | - | |||||||||||||||||||
| 800 | \overload | - | ||||||||||||||||||
| 801 | - | |||||||||||||||||||
| 802 | Returns a pixmap of size QSize(\a extent, \a extent). The pixmap might be smaller | - | ||||||||||||||||||
| 803 | than requested, but never larger. | - | ||||||||||||||||||
| 804 | - | |||||||||||||||||||
| 805 | Setting the Qt::AA_UseHighDpiPixmaps application attribute enables this | - | ||||||||||||||||||
| 806 | function to return pixmaps that are larger than the requested size. Such | - | ||||||||||||||||||
| 807 | images will have a devicePixelRatio larger than 1. | - | ||||||||||||||||||
| 808 | */ | - | ||||||||||||||||||
| 809 | - | |||||||||||||||||||
| 810 | /*! Returns the actual size of the icon for the requested \a size, \a | - | ||||||||||||||||||
| 811 | mode, and \a state. The result might be smaller than requested, but | - | ||||||||||||||||||
| 812 | never larger. The returned size is in device-independent pixels (This | - | ||||||||||||||||||
| 813 | is relevant for high-dpi pixmaps.) | - | ||||||||||||||||||
| 814 | - | |||||||||||||||||||
| 815 | \sa pixmap(), paint() | - | ||||||||||||||||||
| 816 | */ | - | ||||||||||||||||||
| 817 | QSize QIcon::actualSize(const QSize &size, Mode mode, State state) const | - | ||||||||||||||||||
| 818 | { | - | ||||||||||||||||||
| 819 | if (!d) | - | ||||||||||||||||||
| 820 | return QSize(); | - | ||||||||||||||||||
| 821 | return actualSize(0, size, mode, state); | - | ||||||||||||||||||
| 822 | } | - | ||||||||||||||||||
| 823 | - | |||||||||||||||||||
| 824 | /*! | - | ||||||||||||||||||
| 825 | \since 5.1 | - | ||||||||||||||||||
| 826 | - | |||||||||||||||||||
| 827 | Returns a pixmap with the requested \a window \a size, \a mode, and \a | - | ||||||||||||||||||
| 828 | state, generating one if necessary. | - | ||||||||||||||||||
| 829 | - | |||||||||||||||||||
| 830 | The pixmap can be smaller than the requested size. If \a window is on | - | ||||||||||||||||||
| 831 | a high-dpi display the pixmap can be larger. In that case it will have | - | ||||||||||||||||||
| 832 | a devicePixelRatio larger than 1. | - | ||||||||||||||||||
| 833 | - | |||||||||||||||||||
| 834 | \sa actualSize(), paint() | - | ||||||||||||||||||
| 835 | */ | - | ||||||||||||||||||
| 836 | QPixmap QIcon::pixmap(QWindow *window, const QSize &size, Mode mode, State state) const | - | ||||||||||||||||||
| 837 | { | - | ||||||||||||||||||
| 838 | if (!d) | - | ||||||||||||||||||
| 839 | return QPixmap(); | - | ||||||||||||||||||
| 840 | - | |||||||||||||||||||
| 841 | qreal devicePixelRatio = qt_effective_device_pixel_ratio(window); | - | ||||||||||||||||||
| 842 | - | |||||||||||||||||||
| 843 | // Handle the simple normal-dpi case: | - | ||||||||||||||||||
| 844 | if (!(devicePixelRatio > 1.0)) { | - | ||||||||||||||||||
| 845 | QPixmap pixmap = d->engine->pixmap(size, mode, state); | - | ||||||||||||||||||
| 846 | pixmap.setDevicePixelRatio(1.0); | - | ||||||||||||||||||
| 847 | return pixmap; | - | ||||||||||||||||||
| 848 | } | - | ||||||||||||||||||
| 849 | - | |||||||||||||||||||
| 850 | // Try get a pixmap that is big enough to be displayed at device pixel resolution. | - | ||||||||||||||||||
| 851 | QPixmap pixmap = d->engine->pixmap(size * devicePixelRatio, mode, state); | - | ||||||||||||||||||
| 852 | pixmap.setDevicePixelRatio(d->pixmapDevicePixelRatio(devicePixelRatio, size, pixmap.size())); | - | ||||||||||||||||||
| 853 | return pixmap; | - | ||||||||||||||||||
| 854 | } | - | ||||||||||||||||||
| 855 | - | |||||||||||||||||||
| 856 | /*! | - | ||||||||||||||||||
| 857 | \since 5.1 | - | ||||||||||||||||||
| 858 | - | |||||||||||||||||||
| 859 | Returns the actual size of the icon for the requested \a window \a size, \a | - | ||||||||||||||||||
| 860 | mode, and \a state. | - | ||||||||||||||||||
| 861 | - | |||||||||||||||||||
| 862 | The pixmap can be smaller than the requested size. The returned size | - | ||||||||||||||||||
| 863 | is in device-independent pixels (This is relevant for high-dpi pixmaps.) | - | ||||||||||||||||||
| 864 | - | |||||||||||||||||||
| 865 | \sa actualSize(), pixmap(), paint() | - | ||||||||||||||||||
| 866 | */ | - | ||||||||||||||||||
| 867 | QSize QIcon::actualSize(QWindow *window, const QSize &size, Mode mode, State state) const | - | ||||||||||||||||||
| 868 | { | - | ||||||||||||||||||
| 869 | if (!d) | - | ||||||||||||||||||
| 870 | return QSize(); | - | ||||||||||||||||||
| 871 | - | |||||||||||||||||||
| 872 | qreal devicePixelRatio = qt_effective_device_pixel_ratio(window); | - | ||||||||||||||||||
| 873 | - | |||||||||||||||||||
| 874 | // Handle the simple normal-dpi case: | - | ||||||||||||||||||
| 875 | if (!(devicePixelRatio > 1.0)) | - | ||||||||||||||||||
| 876 | return d->engine->actualSize(size, mode, state); | - | ||||||||||||||||||
| 877 | - | |||||||||||||||||||
| 878 | QSize actualSize = d->engine->actualSize(size * devicePixelRatio, mode, state); | - | ||||||||||||||||||
| 879 | return actualSize / d->pixmapDevicePixelRatio(devicePixelRatio, size, actualSize); | - | ||||||||||||||||||
| 880 | } | - | ||||||||||||||||||
| 881 | - | |||||||||||||||||||
| 882 | /*! | - | ||||||||||||||||||
| 883 | Uses the \a painter to paint the icon with specified \a alignment, | - | ||||||||||||||||||
| 884 | required \a mode, and \a state into the rectangle \a rect. | - | ||||||||||||||||||
| 885 | - | |||||||||||||||||||
| 886 | \sa actualSize(), pixmap() | - | ||||||||||||||||||
| 887 | */ | - | ||||||||||||||||||
| 888 | void QIcon::paint(QPainter *painter, const QRect &rect, Qt::Alignment alignment, Mode mode, State state) const | - | ||||||||||||||||||
| 889 | { | - | ||||||||||||||||||
| 890 | if (!d || !painter) | - | ||||||||||||||||||
| 891 | return; | - | ||||||||||||||||||
| 892 | - | |||||||||||||||||||
| 893 | // Copy of QStyle::alignedRect | - | ||||||||||||||||||
| 894 | const QSize size = d->engine->actualSize(rect.size(), mode, state); | - | ||||||||||||||||||
| 895 | alignment = QGuiApplicationPrivate::visualAlignment(painter->layoutDirection(), alignment); | - | ||||||||||||||||||
| 896 | int x = rect.x(); | - | ||||||||||||||||||
| 897 | int y = rect.y(); | - | ||||||||||||||||||
| 898 | int w = size.width(); | - | ||||||||||||||||||
| 899 | int h = size.height(); | - | ||||||||||||||||||
| 900 | if ((alignment & Qt::AlignVCenter) == Qt::AlignVCenter) | - | ||||||||||||||||||
| 901 | y += rect.size().height()/2 - h/2; | - | ||||||||||||||||||
| 902 | else if ((alignment & Qt::AlignBottom) == Qt::AlignBottom) | - | ||||||||||||||||||
| 903 | y += rect.size().height() - h; | - | ||||||||||||||||||
| 904 | if ((alignment & Qt::AlignRight) == Qt::AlignRight) | - | ||||||||||||||||||
| 905 | x += rect.size().width() - w; | - | ||||||||||||||||||
| 906 | else if ((alignment & Qt::AlignHCenter) == Qt::AlignHCenter) | - | ||||||||||||||||||
| 907 | x += rect.size().width()/2 - w/2; | - | ||||||||||||||||||
| 908 | QRect alignedRect(x, y, w, h); | - | ||||||||||||||||||
| 909 | - | |||||||||||||||||||
| 910 | d->engine->paint(painter, alignedRect, mode, state); | - | ||||||||||||||||||
| 911 | } | - | ||||||||||||||||||
| 912 | - | |||||||||||||||||||
| 913 | /*! | - | ||||||||||||||||||
| 914 | \fn void QIcon::paint(QPainter *painter, int x, int y, int w, int h, Qt::Alignment alignment, | - | ||||||||||||||||||
| 915 | Mode mode, State state) const | - | ||||||||||||||||||
| 916 | - | |||||||||||||||||||
| 917 | \overload | - | ||||||||||||||||||
| 918 | - | |||||||||||||||||||
| 919 | Paints the icon into the rectangle QRect(\a x, \a y, \a w, \a h). | - | ||||||||||||||||||
| 920 | */ | - | ||||||||||||||||||
| 921 | - | |||||||||||||||||||
| 922 | /*! | - | ||||||||||||||||||
| 923 | Returns \c true if the icon is empty; otherwise returns \c false. | - | ||||||||||||||||||
| 924 | - | |||||||||||||||||||
| 925 | An icon is empty if it has neither a pixmap nor a filename. | - | ||||||||||||||||||
| 926 | - | |||||||||||||||||||
| 927 | Note: Even a non-null icon might not be able to create valid | - | ||||||||||||||||||
| 928 | pixmaps, eg. if the file does not exist or cannot be read. | - | ||||||||||||||||||
| 929 | */ | - | ||||||||||||||||||
| 930 | bool QIcon::isNull() const | - | ||||||||||||||||||
| 931 | { | - | ||||||||||||||||||
| 932 | return !d ;|| d->engine->isNull(); never executed: return !d || d->engine->isNull(); | 0 | ||||||||||||||||||
| 933 | } | - | ||||||||||||||||||
| 934 | - | |||||||||||||||||||
| 935 | /*!\internal | - | ||||||||||||||||||
| 936 | */ | - | ||||||||||||||||||
| 937 | bool QIcon::isDetached() const | - | ||||||||||||||||||
| 938 | { | - | ||||||||||||||||||
| 939 | return !d || d->ref.load() == 1; | - | ||||||||||||||||||
| 940 | } | - | ||||||||||||||||||
| 941 | - | |||||||||||||||||||
| 942 | /*! \internal | - | ||||||||||||||||||
| 943 | */ | - | ||||||||||||||||||
| 944 | void QIcon::detach() | - | ||||||||||||||||||
| 945 | { | - | ||||||||||||||||||
| 946 | if (d) {
| 0 | ||||||||||||||||||
| 947 | if (d->engine->isNull())
| 0 | ||||||||||||||||||
| 948 | if (!d->ref.deref())
| 0 | ||||||||||||||||||
| 949 | delete d; never executed: delete d; | 0 | ||||||||||||||||||
| 950 | d = 0; | - | ||||||||||||||||||
| 951 | return; never executed: return; | 0 | ||||||||||||||||||
| 952 | } else
| 0 | ||||||||||||||||||
| 953 | QIconPrivate *x = new QIconPrivate; | - | ||||||||||||||||||
| 954 | x->engine = d->engine->clone(); | - | ||||||||||||||||||
| 955 | if (!d->ref.deref())
| 0 | ||||||||||||||||||
| 956 | delete d; never executed: delete d; | 0 | ||||||||||||||||||
| 957 | d = x; | - | ||||||||||||||||||
| 958 | } never executed: end of block | 0 | ||||||||||||||||||
| 959 | ++d->detach_no; | - | ||||||||||||||||||
| 960 | } never executed: end of block | 0 | ||||||||||||||||||
| 961 | } never executed: end of block | 0 | ||||||||||||||||||
| 962 | - | |||||||||||||||||||
| 963 | /*! | - | ||||||||||||||||||
| 964 | Adds \a pixmap to the icon, as a specialization for \a mode and | - | ||||||||||||||||||
| 965 | \a state. | - | ||||||||||||||||||
| 966 | - | |||||||||||||||||||
| 967 | Custom icon engines are free to ignore additionally added | - | ||||||||||||||||||
| 968 | pixmaps. | - | ||||||||||||||||||
| 969 | - | |||||||||||||||||||
| 970 | \sa addFile() | - | ||||||||||||||||||
| 971 | */ | - | ||||||||||||||||||
| 972 | void QIcon::addPixmap(const QPixmap &pixmap, Mode mode, State state) | - | ||||||||||||||||||
| 973 | { | - | ||||||||||||||||||
| 974 | if (pixmap.isNull())
| 0 | ||||||||||||||||||
| 975 | return; never executed: return; | 0 | ||||||||||||||||||
| 976 | detach(); | - | ||||||||||||||||||
| 977 | if (!d) {
| 0 | ||||||||||||||||||
| 978 | d = new QIconPrivate; | - | ||||||||||||||||||
| 979 | d->engine = new QPixmapIconEngine; | - | ||||||||||||||||||
| 980 | } else { never executed: end of block | 0 | ||||||||||||||||||
| detach(); never executed: }end of blocknever executed: end of block | ||||||||||||||||||||
| 981 | d->engine->addPixmap(pixmap, mode, state); | - | ||||||||||||||||||
| 982 | } never executed: end of block | 0 | ||||||||||||||||||
| 983 | - | |||||||||||||||||||
| void QIcon::addFilestatic QIconEngine *iconEngineFromSuffix(const QString &fileName, const QSize &size, Mode mode, State state) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| if (fileName.isEmpty()) | ||||||||||||||||||||
| return; | ||||||||||||||||||||
| if (!dQString &suffix) | ||||||||||||||||||||
| 985 | { | - | ||||||||||||||||||
| 986 | #ifndef QT_NO_LIBRARY
| 0 | ||||||||||||||||||
QFileInfo info(fileName);
| ||||||||||||||||||||
QString suffix = info.suffix();
| ||||||||||||||||||||
| 987 | const int index = loader()->indexOf(suffix); | - | ||||||||||||||||||
| 988 | if (index != -1) {
| 0 | ||||||||||||||||||
| 989 | if (QIconEnginePlugin *factory = qobject_cast<QIconEnginePlugin*>(loader()->instance(index))) {
| 0 | ||||||||||||||||||
| 990 | if (QIconEngine *engine =return factory->create(fileName)) { never executed: return factory->create(fileName); | 0 | ||||||||||||||||||
| d = new QIconPrivate; never executed: return factory->create(fileName); | ||||||||||||||||||||
| d->engine = engine; never executed: );return factory->create(fileName);never executed: return factory->create(fileName); | ||||||||||||||||||||
| 991 | } | - | ||||||||||||||||||
| 992 | } never executed: end of block | 0 | ||||||||||||||||||
| 993 | } never executed: end of block | 0 | ||||||||||||||||||
| 994 | return nullptr; never executed: return nullptr; | 0 | ||||||||||||||||||
| 995 | } | - | ||||||||||||||||||
| 996 | - | |||||||||||||||||||
| #endif | ||||||||||||||||||||
| /*! Adds an image from the file with the given \a fileName to the | ||||||||||||||||||||
| 998 | icon, as a specialization for \a size, \a mode and \a state. The | - | ||||||||||||||||||
| 999 | file will be loaded on demand. Note: custom icon engines are free | - | ||||||||||||||||||
| 1000 | to ignore additionally added pixmaps. | - | ||||||||||||||||||
| 1001 | - | |||||||||||||||||||
| 1002 | If \a fileName contains a relative path (e.g. the filename only) | - | ||||||||||||||||||
| 1003 | the relevant file must be found relative to the runtime working | - | ||||||||||||||||||
| 1004 | directory. | - | ||||||||||||||||||
| 1005 | - | |||||||||||||||||||
| 1006 | The file name can be either refer to an actual file on disk or to | - | ||||||||||||||||||
| 1007 | one of the application's embedded resources. See the | - | ||||||||||||||||||
| 1008 | \l{resources.html}{Resource System} overview for details on how to | - | ||||||||||||||||||
| 1009 | embed images and other resource files in the application's | - | ||||||||||||||||||
| 1010 | executable. | - | ||||||||||||||||||
| 1011 | - | |||||||||||||||||||
| 1012 | Use the QImageReader::supportedImageFormats() and | - | ||||||||||||||||||
| 1013 | QImageWriter::supportedImageFormats() functions to retrieve a | - | ||||||||||||||||||
| 1014 | complete list of the supported file formats. | - | ||||||||||||||||||
| 1015 | - | |||||||||||||||||||
| 1016 | If a high resolution version of the image exists (identified by | - | ||||||||||||||||||
| 1017 | the suffix \c @2x on the base name), it is automatically loaded | - | ||||||||||||||||||
| 1018 | and added with the \e{device pixel ratio} set to a value of 2. | - | ||||||||||||||||||
| 1019 | This can be disabled by setting the environment variable | - | ||||||||||||||||||
| 1020 | \c QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING (see QImageReader). | - | ||||||||||||||||||
| 1021 | - | |||||||||||||||||||
| 1022 | \note When you add a non-empty filename to a QIcon, the icon becomes | - | ||||||||||||||||||
| 1023 | non-null, even if the file doesn't exist or points to a corrupt file. | - | ||||||||||||||||||
| 1024 | - | |||||||||||||||||||
| 1025 | \sa addPixmap(), QPixmap::devicePixelRatio() | - | ||||||||||||||||||
| 1026 | */ | - | ||||||||||||||||||
| 1027 | void QIcon::addFile(const QString &fileName, const QSize &size, Mode mode, State state) | - | ||||||||||||||||||
| 1028 | { | - | ||||||||||||||||||
| 1029 | if (fileName.isEmpty())
| 0 | ||||||||||||||||||
| 1030 | return; never executed: return; | 0 | ||||||||||||||||||
| 1031 | detach(); | - | ||||||||||||||||||
| 1032 | if (!d) {
| 0 | ||||||||||||||||||
| 1033 | - | |||||||||||||||||||
| 1034 | QFileInfo info(fileName); | - | ||||||||||||||||||
| 1035 | QIconEngine *engine = iconEngineFromSuffix(fileName, info.suffix()); | - | ||||||||||||||||||
| 1036 | #ifndef QT_NO_MIMETYPE | - | ||||||||||||||||||
| 1037 | if (!engine)
| 0 | ||||||||||||||||||
| 1038 | engine = iconEngineFromSuffix(fileName, QMimeDatabase().mimeTypeForFile(info).preferredSuffix()); never executed: engine = iconEngineFromSuffix(fileName, QMimeDatabase().mimeTypeForFile(info).preferredSuffix()); | 0 | ||||||||||||||||||
| 1039 | #endif // !QT_NO_MIMETYPE | - | ||||||||||||||||||
| 1040 | d = new QIconPrivate; | - | ||||||||||||||||||
| 1041 | d->engine = engine ? engine : new QPixmapIconEngine;
| 0 | ||||||||||||||||||
| 1042 | } never executed: end of block | 0 | ||||||||||||||||||
| } else { never executed: end of block | ||||||||||||||||||||
| detach(); never executed: }end of blocknever executed: end of block | ||||||||||||||||||||
| 1043 | - | |||||||||||||||||||
| 1044 | d->engine->addFile(fileName, size, mode, state); | - | ||||||||||||||||||
| 1045 | - | |||||||||||||||||||
| 1046 | // Check if a "@Nx" file exists and add it. | - | ||||||||||||||||||
| 1047 | QString atNxFileName = qt_findAtNxFile(fileName, qApp->devicePixelRatio()); | - | ||||||||||||||||||
| 1048 | if (atNxFileName != fileName)
| 0 | ||||||||||||||||||
| 1049 | d->engine->addFile(atNxFileName, size, mode, state); never executed: d->engine->addFile(atNxFileName, size, mode, state); | 0 | ||||||||||||||||||
| 1050 | } never executed: end of block | 0 | ||||||||||||||||||
| 1051 | - | |||||||||||||||||||
| 1052 | /*! | - | ||||||||||||||||||
| 1053 | \since 4.5 | - | ||||||||||||||||||
| 1054 | - | |||||||||||||||||||
| 1055 | Returns a list of available icon sizes for the specified \a mode and | - | ||||||||||||||||||
| 1056 | \a state. | - | ||||||||||||||||||
| 1057 | */ | - | ||||||||||||||||||
| 1058 | QList<QSize> QIcon::availableSizes(Mode mode, State state) const | - | ||||||||||||||||||
| 1059 | { | - | ||||||||||||||||||
| 1060 | if (!d || !d->engine) | - | ||||||||||||||||||
| 1061 | return QList<QSize>(); | - | ||||||||||||||||||
| 1062 | return d->engine->availableSizes(mode, state); | - | ||||||||||||||||||
| 1063 | } | - | ||||||||||||||||||
| 1064 | - | |||||||||||||||||||
| 1065 | /*! | - | ||||||||||||||||||
| 1066 | \since 4.7 | - | ||||||||||||||||||
| 1067 | - | |||||||||||||||||||
| 1068 | Returns the name used to create the icon, if available. | - | ||||||||||||||||||
| 1069 | - | |||||||||||||||||||
| 1070 | Depending on the way the icon was created, it may have an associated | - | ||||||||||||||||||
| 1071 | name. This is the case for icons created with fromTheme() or icons | - | ||||||||||||||||||
| 1072 | using a QIconEngine which supports the QIconEngineV2::IconNameHook. | - | ||||||||||||||||||
| 1073 | - | |||||||||||||||||||
| 1074 | \sa fromTheme(), QIconEngine | - | ||||||||||||||||||
| 1075 | */ | - | ||||||||||||||||||
| 1076 | QString QIcon::name() const | - | ||||||||||||||||||
| 1077 | { | - | ||||||||||||||||||
| 1078 | if (!d || !d->engine) | - | ||||||||||||||||||
| 1079 | return QString(); | - | ||||||||||||||||||
| 1080 | return d->engine->iconName(); | - | ||||||||||||||||||
| 1081 | } | - | ||||||||||||||||||
| 1082 | - | |||||||||||||||||||
| 1083 | /*! | - | ||||||||||||||||||
| 1084 | \since 4.6 | - | ||||||||||||||||||
| 1085 | - | |||||||||||||||||||
| 1086 | Sets the search paths for icon themes to \a paths. | - | ||||||||||||||||||
| 1087 | \sa themeSearchPaths(), fromTheme(), setThemeName() | - | ||||||||||||||||||
| 1088 | */ | - | ||||||||||||||||||
| 1089 | void QIcon::setThemeSearchPaths(const QStringList &paths) | - | ||||||||||||||||||
| 1090 | { | - | ||||||||||||||||||
| 1091 | QIconLoader::instance()->setThemeSearchPath(paths); | - | ||||||||||||||||||
| 1092 | } | - | ||||||||||||||||||
| 1093 | - | |||||||||||||||||||
| 1094 | /*! | - | ||||||||||||||||||
| 1095 | \since 4.6 | - | ||||||||||||||||||
| 1096 | - | |||||||||||||||||||
| 1097 | Returns the search paths for icon themes. | - | ||||||||||||||||||
| 1098 | - | |||||||||||||||||||
| 1099 | The default value will depend on the platform: | - | ||||||||||||||||||
| 1100 | - | |||||||||||||||||||
| 1101 | On X11, the search path will use the XDG_DATA_DIRS environment | - | ||||||||||||||||||
| 1102 | variable if available. | - | ||||||||||||||||||
| 1103 | - | |||||||||||||||||||
| 1104 | By default all platforms will have the resource directory | - | ||||||||||||||||||
| 1105 | \c{:\icons} as a fallback. You can use "rcc -project" to generate a | - | ||||||||||||||||||
| 1106 | resource file from your icon theme. | - | ||||||||||||||||||
| 1107 | - | |||||||||||||||||||
| 1108 | \sa setThemeSearchPaths(), fromTheme(), setThemeName() | - | ||||||||||||||||||
| 1109 | */ | - | ||||||||||||||||||
| 1110 | QStringList QIcon::themeSearchPaths() | - | ||||||||||||||||||
| 1111 | { | - | ||||||||||||||||||
| 1112 | return QIconLoader::instance()->themeSearchPaths(); | - | ||||||||||||||||||
| 1113 | } | - | ||||||||||||||||||
| 1114 | - | |||||||||||||||||||
| 1115 | /*! | - | ||||||||||||||||||
| 1116 | \since 4.6 | - | ||||||||||||||||||
| 1117 | - | |||||||||||||||||||
| 1118 | Sets the current icon theme to \a name. | - | ||||||||||||||||||
| 1119 | - | |||||||||||||||||||
| 1120 | The \a name should correspond to a directory name in the | - | ||||||||||||||||||
| 1121 | themeSearchPath() containing an index.theme | - | ||||||||||||||||||
| 1122 | file describing it's contents. | - | ||||||||||||||||||
| 1123 | - | |||||||||||||||||||
| 1124 | \sa themeSearchPaths(), themeName() | - | ||||||||||||||||||
| 1125 | */ | - | ||||||||||||||||||
| 1126 | void QIcon::setThemeName(const QString &name) | - | ||||||||||||||||||
| 1127 | { | - | ||||||||||||||||||
| 1128 | QIconLoader::instance()->setThemeName(name); | - | ||||||||||||||||||
| 1129 | } | - | ||||||||||||||||||
| 1130 | - | |||||||||||||||||||
| 1131 | /*! | - | ||||||||||||||||||
| 1132 | \since 4.6 | - | ||||||||||||||||||
| 1133 | - | |||||||||||||||||||
| 1134 | Returns the name of the current icon theme. | - | ||||||||||||||||||
| 1135 | - | |||||||||||||||||||
| 1136 | On X11, the current icon theme depends on your desktop | - | ||||||||||||||||||
| 1137 | settings. On other platforms it is not set by default. | - | ||||||||||||||||||
| 1138 | - | |||||||||||||||||||
| 1139 | \sa setThemeName(), themeSearchPaths(), fromTheme(), | - | ||||||||||||||||||
| 1140 | hasThemeIcon() | - | ||||||||||||||||||
| 1141 | */ | - | ||||||||||||||||||
| 1142 | QString QIcon::themeName() | - | ||||||||||||||||||
| 1143 | { | - | ||||||||||||||||||
| 1144 | return QIconLoader::instance()->themeName(); | - | ||||||||||||||||||
| 1145 | } | - | ||||||||||||||||||
| 1146 | - | |||||||||||||||||||
| 1147 | /*! | - | ||||||||||||||||||
| 1148 | \since 4.6 | - | ||||||||||||||||||
| 1149 | - | |||||||||||||||||||
| 1150 | Returns the QIcon corresponding to \a name in the current | - | ||||||||||||||||||
| 1151 | icon theme. | - | ||||||||||||||||||
| 1152 | - | |||||||||||||||||||
| 1153 | The latest version of the freedesktop icon specification and naming | - | ||||||||||||||||||
| 1154 | specification can be obtained here: | - | ||||||||||||||||||
| 1155 | - | |||||||||||||||||||
| 1156 | \list | - | ||||||||||||||||||
| 1157 | \li \l{http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html} | - | ||||||||||||||||||
| 1158 | \li \l{http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html} | - | ||||||||||||||||||
| 1159 | \endlist | - | ||||||||||||||||||
| 1160 | - | |||||||||||||||||||
| 1161 | To fetch an icon from the current icon theme: | - | ||||||||||||||||||
| 1162 | - | |||||||||||||||||||
| 1163 | \snippet code/src_gui_image_qicon.cpp 3 | - | ||||||||||||||||||
| 1164 | - | |||||||||||||||||||
| 1165 | \note By default, only X11 will support themed icons. In order to | - | ||||||||||||||||||
| 1166 | use themed icons on Mac and Windows, you will have to bundle a | - | ||||||||||||||||||
| 1167 | compliant theme in one of your themeSearchPaths() and set the | - | ||||||||||||||||||
| 1168 | appropriate themeName(). | - | ||||||||||||||||||
| 1169 | - | |||||||||||||||||||
| 1170 | \note Qt will make use of GTK's icon-theme.cache if present to speed up | - | ||||||||||||||||||
| 1171 | the lookup. These caches can be generated using gtk-update-icon-cache: | - | ||||||||||||||||||
| 1172 | \l{https://developer.gnome.org/gtk3/stable/gtk-update-icon-cache.html}. | - | ||||||||||||||||||
| 1173 | - | |||||||||||||||||||
| 1174 | \sa themeName(), setThemeName(), themeSearchPaths() | - | ||||||||||||||||||
| 1175 | */ | - | ||||||||||||||||||
| 1176 | QIcon QIcon::fromTheme(const QString &name, const QIcon &fallback) | - | ||||||||||||||||||
| 1177 | { | - | ||||||||||||||||||
| 1178 | QIcon icon; | - | ||||||||||||||||||
| 1179 | - | |||||||||||||||||||
| 1180 | if (qtIconCache()->contains(name)) {
| 0 | ||||||||||||||||||
| 1181 | icon = *qtIconCache()->object(name); | - | ||||||||||||||||||
| 1182 | } else if (QDir::isAbsolutePath(name)) { never executed: end of block
| 0 | ||||||||||||||||||
| 1183 | return QIcon never executed: (name);return QIcon(name);never executed: return QIcon(name); | 0 | ||||||||||||||||||
| 1184 | } else { | - | ||||||||||||||||||
| 1185 | QPlatformTheme * const platformTheme = QGuiApplicationPrivate::platformTheme(); | - | ||||||||||||||||||
| 1186 | bool hasUserTheme = QIconLoader::instance()->hasUserTheme(); | - | ||||||||||||||||||
| 1187 | QIconEngine * const engine = (platformTheme && !hasUserTheme) ? platformTheme->createIconEngine(name)
| 0 | ||||||||||||||||||
| 1188 | : new QIconLoaderEngine(name); | - | ||||||||||||||||||
| 1189 | QIcon *cachedIcon = new QIcon(engine); | - | ||||||||||||||||||
| 1190 | icon = *cachedIcon; | - | ||||||||||||||||||
| 1191 | qtIconCache()->insert(name, cachedIcon); | - | ||||||||||||||||||
| 1192 | } never executed: end of block | 0 | ||||||||||||||||||
| 1193 | - | |||||||||||||||||||
| 1194 | return icon; never executed: return icon; | 0 | ||||||||||||||||||
| 1195 | } | - | ||||||||||||||||||
| 1196 | - | |||||||||||||||||||
| 1197 | /*! | - | ||||||||||||||||||
| 1198 | \overload | - | ||||||||||||||||||
| 1199 | - | |||||||||||||||||||
| 1200 | Returns the QIcon corresponding to \a name in the current | - | ||||||||||||||||||
| 1201 | icon theme. If no such icon is found in the current theme | - | ||||||||||||||||||
| 1202 | \a fallback is returned instead. | - | ||||||||||||||||||
| 1203 | - | |||||||||||||||||||
| 1204 | If you want to provide a guaranteed fallback for platforms that | - | ||||||||||||||||||
| 1205 | do not support theme icons, you can use the second argument: | - | ||||||||||||||||||
| 1206 | - | |||||||||||||||||||
| 1207 | \snippet code/src_gui_image_qicon.cpp 4 | - | ||||||||||||||||||
| 1208 | */ | - | ||||||||||||||||||
| 1209 | QIcon QIcon::fromTheme(const QString &name, const QIcon &fallback) | - | ||||||||||||||||||
| 1210 | { | - | ||||||||||||||||||
| 1211 | QIcon icon = fromTheme(name); | - | ||||||||||||||||||
| 1212 | - | |||||||||||||||||||
| 1213 | if (qApp &&icon.isNull() || icon.availableSizes().isEmpty())
| 0 | ||||||||||||||||||
| 1214 | return fallback; never executed: return fallback; | 0 | ||||||||||||||||||
| 1215 | - | |||||||||||||||||||
| 1216 | return icon; never executed: return icon; | 0 | ||||||||||||||||||
| 1217 | } | - | ||||||||||||||||||
| 1218 | - | |||||||||||||||||||
| 1219 | /*! | - | ||||||||||||||||||
| 1220 | \since 4.6 | - | ||||||||||||||||||
| 1221 | - | |||||||||||||||||||
| 1222 | Returns \c true if there is an icon available for \a name in the | - | ||||||||||||||||||
| 1223 | current icon theme, otherwise returns \c false. | - | ||||||||||||||||||
| 1224 | - | |||||||||||||||||||
| 1225 | \sa themeSearchPaths(), fromTheme(), setThemeName() | - | ||||||||||||||||||
| 1226 | */ | - | ||||||||||||||||||
| 1227 | bool QIcon::hasThemeIcon(const QString &name) | - | ||||||||||||||||||
| 1228 | { | - | ||||||||||||||||||
| 1229 | QIcon icon = fromTheme(name); | - | ||||||||||||||||||
| 1230 | - | |||||||||||||||||||
| 1231 | return icon.name() == name; | - | ||||||||||||||||||
| 1232 | } | - | ||||||||||||||||||
| 1233 | - | |||||||||||||||||||
| 1234 | /*! | - | ||||||||||||||||||
| 1235 | \since 5.6 | - | ||||||||||||||||||
| 1236 | - | |||||||||||||||||||
| 1237 | Indicate that this icon is a mask image(boolean \a isMask), and hence can | - | ||||||||||||||||||
| 1238 | potentially be modified based on where it's displayed. | - | ||||||||||||||||||
| 1239 | \sa isMask() | - | ||||||||||||||||||
| 1240 | */ | - | ||||||||||||||||||
| 1241 | void QIcon::setIsMask(bool isMask) | - | ||||||||||||||||||
| 1242 | { | - | ||||||||||||||||||
| 1243 | if (!d) { | - | ||||||||||||||||||
| 1244 | d = new QIconPrivate; | - | ||||||||||||||||||
| 1245 | d->engine = new QPixmapIconEngine; | - | ||||||||||||||||||
| 1246 | } else { | - | ||||||||||||||||||
| 1247 | detach(); | - | ||||||||||||||||||
| 1248 | } | - | ||||||||||||||||||
| 1249 | d->is_mask = isMask; | - | ||||||||||||||||||
| 1250 | } | - | ||||||||||||||||||
| 1251 | - | |||||||||||||||||||
| 1252 | /*! | - | ||||||||||||||||||
| 1253 | \since 5.6 | - | ||||||||||||||||||
| 1254 | - | |||||||||||||||||||
| 1255 | Returns \c true if this icon has been marked as a mask image. | - | ||||||||||||||||||
| 1256 | Certain platforms render mask icons differently (for example, | - | ||||||||||||||||||
| 1257 | menu icons on \macos). | - | ||||||||||||||||||
| 1258 | - | |||||||||||||||||||
| 1259 | \sa setIsMask() | - | ||||||||||||||||||
| 1260 | */ | - | ||||||||||||||||||
| 1261 | bool QIcon::isMask() const | - | ||||||||||||||||||
| 1262 | { | - | ||||||||||||||||||
| 1263 | if (!d) | - | ||||||||||||||||||
| 1264 | return false; | - | ||||||||||||||||||
| 1265 | return d->is_mask; | - | ||||||||||||||||||
| 1266 | } | - | ||||||||||||||||||
| 1267 | - | |||||||||||||||||||
| 1268 | /***************************************************************************** | - | ||||||||||||||||||
| 1269 | QIcon stream functions | - | ||||||||||||||||||
| 1270 | *****************************************************************************/ | - | ||||||||||||||||||
| 1271 | #if !defined(QT_NO_DATASTREAM) | - | ||||||||||||||||||
| 1272 | /*! | - | ||||||||||||||||||
| 1273 | \fn QDataStream &operator<<(QDataStream &stream, const QIcon &icon) | - | ||||||||||||||||||
| 1274 | \relates QIcon | - | ||||||||||||||||||
| 1275 | \since 4.2 | - | ||||||||||||||||||
| 1276 | - | |||||||||||||||||||
| 1277 | Writes the given \a icon to the given \a stream as a PNG | - | ||||||||||||||||||
| 1278 | image. If the icon contains more than one image, all images will | - | ||||||||||||||||||
| 1279 | be written to the stream. Note that writing the stream to a file | - | ||||||||||||||||||
| 1280 | will not produce a valid image file. | - | ||||||||||||||||||
| 1281 | */ | - | ||||||||||||||||||
| 1282 | - | |||||||||||||||||||
| 1283 | QDataStream &operator<<(QDataStream &s, const QIcon &icon) | - | ||||||||||||||||||
| 1284 | { | - | ||||||||||||||||||
| 1285 | if (s.version() >= QDataStream::Qt_4_3) { | - | ||||||||||||||||||
| 1286 | if (icon.isNull()) { | - | ||||||||||||||||||
| 1287 | s << QString(); | - | ||||||||||||||||||
| 1288 | } else { | - | ||||||||||||||||||
| 1289 | s << icon.d->engine->key(); | - | ||||||||||||||||||
| 1290 | icon.d->engine->write(s); | - | ||||||||||||||||||
| 1291 | } | - | ||||||||||||||||||
| 1292 | } else if (s.version() == QDataStream::Qt_4_2) { | - | ||||||||||||||||||
| 1293 | if (icon.isNull()) { | - | ||||||||||||||||||
| 1294 | s << 0; | - | ||||||||||||||||||
| 1295 | } else { | - | ||||||||||||||||||
| 1296 | QPixmapIconEngine *engine = static_cast<QPixmapIconEngine *>(icon.d->engine); | - | ||||||||||||||||||
| 1297 | int num_entries = engine->pixmaps.size(); | - | ||||||||||||||||||
| 1298 | s << num_entries; | - | ||||||||||||||||||
| 1299 | for (int i=0; i < num_entries; ++i) { | - | ||||||||||||||||||
| 1300 | s << engine->pixmaps.at(i).pixmap; | - | ||||||||||||||||||
| 1301 | s << engine->pixmaps.at(i).fileName; | - | ||||||||||||||||||
| 1302 | s << engine->pixmaps.at(i).size; | - | ||||||||||||||||||
| 1303 | s << (uint) engine->pixmaps.at(i).mode; | - | ||||||||||||||||||
| 1304 | s << (uint) engine->pixmaps.at(i).state; | - | ||||||||||||||||||
| 1305 | } | - | ||||||||||||||||||
| 1306 | } | - | ||||||||||||||||||
| 1307 | } else { | - | ||||||||||||||||||
| 1308 | s << QPixmap(icon.pixmap(22,22)); | - | ||||||||||||||||||
| 1309 | } | - | ||||||||||||||||||
| 1310 | return s; | - | ||||||||||||||||||
| 1311 | } | - | ||||||||||||||||||
| 1312 | - | |||||||||||||||||||
| 1313 | /*! | - | ||||||||||||||||||
| 1314 | \fn QDataStream &operator>>(QDataStream &stream, QIcon &icon) | - | ||||||||||||||||||
| 1315 | \relates QIcon | - | ||||||||||||||||||
| 1316 | \since 4.2 | - | ||||||||||||||||||
| 1317 | - | |||||||||||||||||||
| 1318 | Reads an image, or a set of images, from the given \a stream into | - | ||||||||||||||||||
| 1319 | the given \a icon. | - | ||||||||||||||||||
| 1320 | */ | - | ||||||||||||||||||
| 1321 | - | |||||||||||||||||||
| 1322 | QDataStream &operator>>(QDataStream &s, QIcon &icon) | - | ||||||||||||||||||
| 1323 | { | - | ||||||||||||||||||
| 1324 | if (s.version() >= QDataStream::Qt_4_3) {
| 0 | ||||||||||||||||||
| 1325 | icon = QIcon(); | - | ||||||||||||||||||
| 1326 | QString key; | - | ||||||||||||||||||
| 1327 | s >> key; | - | ||||||||||||||||||
| 1328 | if (key == QLatin1String("QPixmapIconEngine")) {
| 0 | ||||||||||||||||||
| 1329 | icon.d = new QIconPrivate; | - | ||||||||||||||||||
| 1330 | QIconEngine *engine = new QPixmapIconEngine; | - | ||||||||||||||||||
| 1331 | icon.d->engine = engine; | - | ||||||||||||||||||
| 1332 | engine->read(s); | - | ||||||||||||||||||
| 1333 | } else if (key == QLatin1String("QIconLoaderEngine")) { never executed: end of block
| 0 | ||||||||||||||||||
| 1334 | icon.d = new QIconPrivate; | - | ||||||||||||||||||
| 1335 | QIconEngine *engine = new QIconLoaderEngine(); | - | ||||||||||||||||||
| 1336 | icon.d->engine = engine; | - | ||||||||||||||||||
| 1337 | engine->read(s); | - | ||||||||||||||||||
| 1338 | #ifndef QT_NO_LIBRARY} else { never executed: end of block | 0 | ||||||||||||||||||
| 1339 | const int index = loader()->indexOf(key); | - | ||||||||||||||||||
| 1340 | if (index != -1) {
| 0 | ||||||||||||||||||
| 1341 | if (QIconEnginePlugin *factory = qobject_cast<QIconEnginePlugin*>(loader()->instance(index))) {
| 0 | ||||||||||||||||||
| 1342 | if (QIconEngine *engine= factory->create()) {
| 0 | ||||||||||||||||||
| 1343 | icon.d = new QIconPrivate; | - | ||||||||||||||||||
| 1344 | icon.d->engine = engine; | - | ||||||||||||||||||
| 1345 | engine->read(s); | - | ||||||||||||||||||
| 1346 | } // factory never executed: end of block | 0 | ||||||||||||||||||
| 1347 | } // instance never executed: end of block | 0 | ||||||||||||||||||
| 1348 | } // index#endif never executed: end of block | 0 | ||||||||||||||||||
| 1349 | } never executed: end of block | 0 | ||||||||||||||||||
| 1350 | } else if (s.version() == QDataStream::Qt_4_2) {
| 0 | ||||||||||||||||||
| 1351 | icon = QIcon(); | - | ||||||||||||||||||
| 1352 | int num_entries; | - | ||||||||||||||||||
| 1353 | QPixmap pm; | - | ||||||||||||||||||
| 1354 | QString fileName; | - | ||||||||||||||||||
| 1355 | QSize sz; | - | ||||||||||||||||||
| 1356 | uint mode; | - | ||||||||||||||||||
| 1357 | uint state; | - | ||||||||||||||||||
| 1358 | - | |||||||||||||||||||
| 1359 | s >> num_entries; | - | ||||||||||||||||||
| 1360 | for (int i=0; i < num_entries; ++i) {
| 0 | ||||||||||||||||||
| 1361 | s >> pm; | - | ||||||||||||||||||
| 1362 | s >> fileName; | - | ||||||||||||||||||
| 1363 | s >> sz; | - | ||||||||||||||||||
| 1364 | s >> mode; | - | ||||||||||||||||||
| 1365 | s >> state; | - | ||||||||||||||||||
| 1366 | if (pm.isNull())
| 0 | ||||||||||||||||||
| 1367 | icon.addFile(fileName, sz, QIcon::Mode(mode), QIcon::State(state)); never executed: icon.addFile(fileName, sz, QIcon::Mode(mode), QIcon::State(state)); | 0 | ||||||||||||||||||
| 1368 | else | - | ||||||||||||||||||
| 1369 | icon.addPixmap(pm, QIcon::Mode(mode), QIcon::State(state)); never executed: icon.addPixmap(pm, QIcon::Mode(mode), QIcon::State(state)); | 0 | ||||||||||||||||||
| 1370 | } | - | ||||||||||||||||||
| 1371 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 1372 | QPixmap pm; | - | ||||||||||||||||||
| 1373 | s >> pm; | - | ||||||||||||||||||
| 1374 | icon.addPixmap(pm); | - | ||||||||||||||||||
| 1375 | } never executed: end of block | 0 | ||||||||||||||||||
| 1376 | return s; never executed: return s; | 0 | ||||||||||||||||||
| 1377 | } | - | ||||||||||||||||||
| 1378 | - | |||||||||||||||||||
| 1379 | #endif //QT_NO_DATASTREAM | - | ||||||||||||||||||
| 1380 | - | |||||||||||||||||||
| 1381 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||||||||
| 1382 | QDebug operator<<(QDebug dbg, const QIcon &i) | - | ||||||||||||||||||
| 1383 | { | - | ||||||||||||||||||
| 1384 | QDebugStateSaver saver(dbg); | - | ||||||||||||||||||
| 1385 | dbg.resetFormat(); | - | ||||||||||||||||||
| 1386 | dbg.nospace(); | - | ||||||||||||||||||
| 1387 | dbg << "QIcon("; | - | ||||||||||||||||||
| 1388 | if (i.isNull()) { | - | ||||||||||||||||||
| 1389 | dbg << "null"; | - | ||||||||||||||||||
| 1390 | } else { | - | ||||||||||||||||||
| 1391 | if (!i.name().isEmpty()) | - | ||||||||||||||||||
| 1392 | dbg << i.name() << ','; | - | ||||||||||||||||||
| 1393 | dbg << "availableSizes[normal,Off]=" << i.availableSizes() | - | ||||||||||||||||||
| 1394 | << ",cacheKey=" << showbase << hex << i.cacheKey() << dec << noshowbase; | - | ||||||||||||||||||
| 1395 | } | - | ||||||||||||||||||
| 1396 | dbg << ')'; | - | ||||||||||||||||||
| 1397 | return dbg; | - | ||||||||||||||||||
| 1398 | } | - | ||||||||||||||||||
| 1399 | #endif | - | ||||||||||||||||||
| 1400 | - | |||||||||||||||||||
| 1401 | /*! | - | ||||||||||||||||||
| 1402 | \fn DataPtr &QIcon::data_ptr() | - | ||||||||||||||||||
| 1403 | \internal | - | ||||||||||||||||||
| 1404 | */ | - | ||||||||||||||||||
| 1405 | - | |||||||||||||||||||
| 1406 | /*! | - | ||||||||||||||||||
| 1407 | \typedef QIcon::DataPtr | - | ||||||||||||||||||
| 1408 | \internal | - | ||||||||||||||||||
| 1409 | */ | - | ||||||||||||||||||
| 1410 | - | |||||||||||||||||||
| 1411 | /*! | - | ||||||||||||||||||
| 1412 | \internal | - | ||||||||||||||||||
| 1413 | \since 5.6 | - | ||||||||||||||||||
| 1414 | Attempts to find a suitable @Nx file for the given \a targetDevicePixelRatio | - | ||||||||||||||||||
| 1415 | Returns the the \a baseFileName if no such file was found. | - | ||||||||||||||||||
| 1416 | - | |||||||||||||||||||
| 1417 | Given base foo.png and a target dpr of 2.5, this function will look for | - | ||||||||||||||||||
| 1418 | foo@3x.png, then foo@2x, then fall back to foo.png if not found. | - | ||||||||||||||||||
| 1419 | - | |||||||||||||||||||
| 1420 | \a sourceDevicePixelRatio will be set to the value of N if the argument is | - | ||||||||||||||||||
| 1421 | a non-null pointer | - | ||||||||||||||||||
| 1422 | */ | - | ||||||||||||||||||
| 1423 | QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio, | - | ||||||||||||||||||
| 1424 | qreal *sourceDevicePixelRatio) | - | ||||||||||||||||||
| 1425 | { | - | ||||||||||||||||||
| 1426 | if (targetDevicePixelRatio <= 1.0) | - | ||||||||||||||||||
| 1427 | return baseFileName; | - | ||||||||||||||||||
| 1428 | - | |||||||||||||||||||
| 1429 | static bool disableNxImageLoading = !qEnvironmentVariableIsEmpty("QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING"); | - | ||||||||||||||||||
| 1430 | if (disableNxImageLoading) | - | ||||||||||||||||||
| 1431 | return baseFileName; | - | ||||||||||||||||||
| 1432 | - | |||||||||||||||||||
| 1433 | int dotIndex = baseFileName.lastIndexOf(QLatin1Char('.')); | - | ||||||||||||||||||
| 1434 | if (dotIndex == -1) /* no dot */ | - | ||||||||||||||||||
| 1435 | dotIndex = baseFileName.size(); /* append */ | - | ||||||||||||||||||
| 1436 | - | |||||||||||||||||||
| 1437 | QString atNxfileName = baseFileName; | - | ||||||||||||||||||
| 1438 | atNxfileName.insert(dotIndex, QLatin1String("@2x")); | - | ||||||||||||||||||
| 1439 | // Check for @Nx, ..., @3x, @2x file versions, | - | ||||||||||||||||||
| 1440 | for (int n = qMin(qCeil(targetDevicePixelRatio), 9); n > 1; --n) { | - | ||||||||||||||||||
| 1441 | atNxfileName[dotIndex + 1] = QLatin1Char('0' + n); | - | ||||||||||||||||||
| 1442 | if (QFile::exists(atNxfileName)) { | - | ||||||||||||||||||
| 1443 | if (sourceDevicePixelRatio) | - | ||||||||||||||||||
| 1444 | *sourceDevicePixelRatio = n; | - | ||||||||||||||||||
| 1445 | return atNxfileName; | - | ||||||||||||||||||
| 1446 | } | - | ||||||||||||||||||
| 1447 | } | - | ||||||||||||||||||
| 1448 | - | |||||||||||||||||||
| 1449 | return baseFileName; | - | ||||||||||||||||||
| 1450 | } | - | ||||||||||||||||||
| 1451 | - | |||||||||||||||||||
| 1452 | QT_END_NAMESPACE | - | ||||||||||||||||||
| 1453 | #endif //QT_NO_ICON | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |