icon.cpp 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

31 32
#include "icon.h"
#include "qtcassert.h"
33 34 35 36 37 38 39 40
#include "theme/theme.h"
#include "stylehelper.h"

#include <QApplication>
#include <QIcon>
#include <QImage>
#include <QMetaEnum>
#include <QPainter>
41
#include <QPaintEngine>
42
#include <QWidget>
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

namespace Utils {

static QPixmap maskToColorAndAlpha(const QPixmap &mask, const QColor &color)
{
    QImage result(mask.toImage().convertToFormat(QImage::Format_ARGB32));
    result.setDevicePixelRatio(mask.devicePixelRatio());
    QRgb *bitsStart = reinterpret_cast<QRgb*>(result.bits());
    const QRgb *bitsEnd = bitsStart + result.width() * result.height();
    const QRgb tint = color.rgb() & 0x00ffffff;
    const qreal alpha = color.alphaF();
    for (QRgb *pixel = bitsStart; pixel < bitsEnd; ++pixel) {
        QRgb pixelAlpha = ~(*pixel & 0xff) * alpha;
        *pixel = (pixelAlpha << 24) | tint;
    }
    return QPixmap::fromImage(result);
}

typedef QPair<QPixmap, QColor> MaskAndColor;
typedef QList<MaskAndColor> MasksAndColors;
63
static MasksAndColors masksAndColors(const Icon &icon, int dpr)
64 65
{
    MasksAndColors result;
66 67 68 69
    for (const IconMaskAndColor &i: icon) {
        const QString &fileName = i.first;
        const QColor color = creatorTheme()->color(i.second);
        const QString dprFileName = StyleHelper::availableImageResolutions(i.first).contains(dpr) ?
70 71 72 73 74 75
                    StyleHelper::imageFileWithResolution(fileName, dpr) : fileName;
        result.append(qMakePair(QPixmap(dprFileName), color));
    }
    return result;
}

Alessandro Portale's avatar
Alessandro Portale committed
76
static void smearPixmap(QPainter *painter, const QPixmap &pixmap, qreal radius)
77
{
Alessandro Portale's avatar
Alessandro Portale committed
78 79 80 81 82 83 84 85 86 87
    const qreal nagative = -radius - 0.01; // Workaround for QPainter rounding behavior
    const qreal positive = radius;
    painter->drawPixmap(QPointF(nagative, nagative), pixmap);
    painter->drawPixmap(QPointF(0, nagative), pixmap);
    painter->drawPixmap(QPointF(positive, nagative), pixmap);
    painter->drawPixmap(QPointF(positive, 0), pixmap);
    painter->drawPixmap(QPointF(positive, positive), pixmap);
    painter->drawPixmap(QPointF(0, positive), pixmap);
    painter->drawPixmap(QPointF(nagative, positive), pixmap);
    painter->drawPixmap(QPointF(nagative, 0), pixmap);
88 89 90 91 92 93 94 95 96 97
}

static QPixmap combinedMask(const MasksAndColors &masks)
{
    if (masks.count() == 1)
        return masks.first().first;

    QPixmap result(masks.first().first);
    QPainter p(&result);
    p.setCompositionMode(QPainter::CompositionMode_Darken);
98
    auto maskImage = masks.constBegin();
99 100 101 102 103
    maskImage++;
    for (;maskImage != masks.constEnd(); ++maskImage) {
        p.save();
        p.setOpacity(0.4);
        p.setCompositionMode(QPainter::CompositionMode_Lighten);
Alessandro Portale's avatar
Alessandro Portale committed
104
        smearPixmap(&p, maskToColorAndAlpha((*maskImage).first, Qt::white), 0.5);
105 106 107 108 109 110 111
        p.restore();
        p.drawPixmap(0, 0, (*maskImage).first);
    }
    p.end();
    return result;
}

Alessandro Portale's avatar
Alessandro Portale committed
112
static QPixmap masksToIcon(const MasksAndColors &masks, const QPixmap &combinedMask, Icon::Style style)
113 114 115 116 117 118 119 120
{
    QPixmap result(combinedMask.size());
    result.setDevicePixelRatio(combinedMask.devicePixelRatio());
    result.fill(Qt::transparent);
    QPainter p(&result);

    for (MasksAndColors::const_iterator maskImage = masks.constBegin();
         maskImage != masks.constEnd(); ++maskImage) {
121
        if (style == Icon::Style::TintedWithShadow && maskImage != masks.constBegin()) {
122 123 124 125
            // Punch a transparent outline around an overlay.
            p.save();
            p.setOpacity(0.4);
            p.setCompositionMode(QPainter::CompositionMode_DestinationOut);
Alessandro Portale's avatar
Alessandro Portale committed
126
            smearPixmap(&p, maskToColorAndAlpha((*maskImage).first, Qt::white), 0.5);
127 128 129 130 131
            p.restore();
        }
        p.drawPixmap(0, 0, maskToColorAndAlpha((*maskImage).first, (*maskImage).second));
    }

Alessandro Portale's avatar
Alessandro Portale committed
132
    if (style == Icon::Style::TintedWithShadow) {
133 134 135 136 137 138 139 140 141 142 143
        const QPixmap shadowMask = maskToColorAndAlpha(combinedMask, Qt::black);
        p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
        p.setOpacity(0.05);
        p.drawPixmap(QPointF(0, -0.501), shadowMask);
        p.drawPixmap(QPointF(-0.501, 0), shadowMask);
        p.drawPixmap(QPointF(0.5, 0), shadowMask);
        p.drawPixmap(QPointF(0.5, 0.5), shadowMask);
        p.drawPixmap(QPointF(-0.501, 0.5), shadowMask);
        p.setOpacity(0.2);
        p.drawPixmap(0, 1, shadowMask);
    }
144 145 146 147 148 149

    p.end();

    return result;
}

150
static QPixmap combinedPlainPixmaps(const QVector<IconMaskAndColor> &images)
151
{
152 153 154 155 156 157 158
    QPixmap result(StyleHelper::dpiSpecificImageFile(images.first().first));
    auto pixmap = images.constBegin();
    pixmap++;
    for (;pixmap != images.constEnd(); ++pixmap) {
        const QPixmap overlay(StyleHelper::dpiSpecificImageFile((*pixmap).first));
        result.paintEngine()->painter()->drawPixmap(0, 0, overlay);
    }
159 160 161
    return result;
}

162 163 164 165 166 167 168 169 170 171 172 173
Icon::Icon()
{
}

Icon::Icon(std::initializer_list<IconMaskAndColor> args, Style style)
    : QVector<IconMaskAndColor>(args)
    , m_style(style)
{
}

Icon::Icon(const QString &imageFileName)
    : m_style(Style::Plain)
174
{
175 176 177 178 179 180 181 182 183 184 185 186 187
    append({imageFileName, Theme::Color(-1)});
}

QIcon Icon::icon() const
{
    if (isEmpty()) {
        return QIcon();
    } else if (m_style == Style::Plain) {
        return QIcon(combinedPlainPixmaps(*this));
    } else {
        QIcon result;
        const MasksAndColors masks = masksAndColors(*this, qRound(qApp->devicePixelRatio()));
        const QPixmap combinedMask = Utils::combinedMask(masks);
Alessandro Portale's avatar
Alessandro Portale committed
188
        result.addPixmap(masksToIcon(masks, combinedMask, m_style));
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

        QColor disabledColor = creatorTheme()->palette().mid().color();
        disabledColor.setAlphaF(0.6);
        result.addPixmap(maskToColorAndAlpha(combinedMask, disabledColor), QIcon::Disabled);
        return result;
    }
}

QPixmap Icon::pixmap() const
{
    if (isEmpty()) {
        return QPixmap();
    } else if (m_style == Style::Plain) {
        return combinedPlainPixmaps(*this);
    } else {
        const MasksAndColors masks =
                masksAndColors(*this, qRound(qApp->devicePixelRatio()));
        const QPixmap combinedMask = Utils::combinedMask(masks);
Alessandro Portale's avatar
Alessandro Portale committed
207
        return masksToIcon(masks, combinedMask, m_style);
208
    }
209 210
}

211
QString Icon::imageFileName() const
212
{
213 214
    QTC_ASSERT(length() == 1, return QString());
    return first().first;
215 216
}

217
QIcon Icon::sideBarIcon(const Icon &classic, const Icon &flat)
218
{
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    QIcon result;
    if (creatorTheme()->flag(Theme::FlatSideBarIcons)) {
        result = flat.icon();
    } else {
        const QPixmap pixmap = classic.pixmap();
        result.addPixmap(pixmap);
        // Ensure that the icon contains a disabled state of that size, since
        // Since we have icons with mixed sizes (e.g. DEBUG_START), and want to
        // avoid that QIcon creates scaled versions of missing QIcon::Disabled
        // sizes.
        result.addPixmap(StyleHelper::disabledSideBarIcon(pixmap), QIcon::Disabled);
    }
    return result;
}

QIcon Icon::modeIcon(const Icon &classic, const Icon &flat, const Icon &flatActive)
{
    QIcon result = sideBarIcon(classic, flat);
    if (creatorTheme()->flag(Theme::FlatSideBarIcons))
        result.addPixmap(flatActive.pixmap(), QIcon::Active);
    return result;
}

QIcon Icon::combinedIcon(const QList<QIcon> &icons)
{
    QIcon result;
    QWindow *window = QApplication::allWidgets().first()->windowHandle();
    for (const QIcon &icon: icons)
        for (const QIcon::Mode mode: {QIcon::Disabled, QIcon::Normal})
            for (const QSize &size: icon.availableSizes(mode))
                result.addPixmap(icon.pixmap(window, size, mode), mode);
    return result;
251 252 253
}

} // namespace Utils