File: qdciiohandler.cpp

package info (click to toggle)
dde-qt5integration 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,528 kB
  • sloc: cpp: 12,089; xml: 154; sh: 13; makefile: 10
file content (212 lines) | stat: -rw-r--r-- 4,913 bytes parent folder | download
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (C) 2022 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "qdciiohandler.h"

#ifndef QT_NO_SVGRENDERER

#include "qimage.h"
#include "qpixmap.h"
#include "qpainter.h"
#include "qvariant.h"
#include "qbuffer.h"
#include "qdebug.h"

#include <DDciIcon>

DGUI_USE_NAMESPACE

QT_BEGIN_NAMESPACE

class QDciIOHandlerPrivate
{
public:
    QDciIOHandlerPrivate(QDciIOHandler *qq)
        : q(qq)
    {}

    bool load(QIODevice *device);

    QDciIOHandler *q;
    DDciIcon icon;
    DDciIconMatchResult current = nullptr;
    DDciIcon::Theme theme = DDciIcon::Light;
    DDciIcon::Mode mode = DDciIcon::Normal;

    bool loaded = false;
    bool readDone = false;

    int defaultSize = 0;
    int scaledSize = 0;
    QColor backColor;
};

bool QDciIOHandlerPrivate::load(QIODevice *device)
{
    if (!device)
        return false;

    if (loaded)
        return current;
    if (q->format().isEmpty() && !q->canRead())
        return false;

    loaded = true;

    QBuffer *buf = qobject_cast<QBuffer *>(device);
    if (buf) {
        const QByteArray &ba = buf->data();
        icon = DDciIcon(QByteArray::fromRawData(ba.constData() + buf->pos(), ba.size() - buf->pos()));
        buf->seek(ba.size());
    } else {
        icon = DDciIcon(device->readAll());
    }

    current = nullptr;
    if (icon.isNull())
        return false;

    if (q->format().startsWith(QByteArrayLiteral("dci-"))) {
        const auto sections = q->format().split('-');
        if (sections.size() == 3) {
            if (sections[1] == QByteArrayLiteral("dark"))
                theme = DDciIcon::Dark;
            if (sections[2] == QByteArrayLiteral("disabled"))
                mode = DDciIcon::Disabled;
            else if (sections[2] == QByteArrayLiteral("hover"))
                mode = DDciIcon::Hover;
            else if (sections[2] == QByteArrayLiteral("pressed"))
                mode = DDciIcon::Pressed;
        }
    }

    current = icon.matchIcon(0, theme, mode, DDciIcon::DontFallbackMode);

    if (!current)
        return false;

    defaultSize = icon.actualSize(current);

    return true;
}

QDciIOHandler::QDciIOHandler()
    : d(new QDciIOHandlerPrivate(this))
{

}

QDciIOHandler::~QDciIOHandler()
{
    delete d;
}

bool QDciIOHandler::canRead() const
{
    if (!device())
        return false;
    if (d->loaded && !d->readDone)
        return true;

    QByteArray buf = device()->peek(4);
    if (buf.startsWith(QByteArrayLiteral("DCI"))) {
        // Decide format from content..
        if (format().isEmpty())
            setFormat("dci");

        return true;
    }
    return false;
}

QByteArray QDciIOHandler::name() const
{
    return "dci";
}

bool QDciIOHandler::read(QImage *image)
{
    if (d->readDone || d->load(device())) {
        Q_ASSERT(d->current);
        int finalSize = d->scaledSize > 0 ? d->scaledSize : d->defaultSize;

        if (finalSize > 0) {
            DDciIconPalette palette(QColor::Invalid, d->backColor);
            *image = d->icon.pixmap(1.0, finalSize, d->current, palette).toImage();
        }
        d->readDone = true;
        return true;
    }

    return false;
}

QVariant QDciIOHandler::option(ImageOption option) const
{
    switch(option) {
    case ImageFormat:
        return QImage::Format_ARGB32_Premultiplied;
    case Size:
        d->load(device());
        return QSize(d->defaultSize, d->defaultSize);
    case ScaledSize:
        return QSize(d->scaledSize, d->scaledSize);
    case BackgroundColor:
        return d->backColor;
    case Animation:
        // TODO: Support animation for DDciIcon
        return false;
    case Endianness:
        return QSysInfo::LittleEndian;
    case SupportedSubTypes:
        if (d->load(device()) && d->icon.isSupportedAttribute(d->current, DDciIcon::HasPalette))
            return QVariant::fromValue(QByteArrayList { QByteArrayLiteral("HasPalette") });
        return QVariant::fromValue(QByteArrayList());
    default:
        break;
    }
    return QVariant();
}

void QDciIOHandler::setOption(ImageOption option, const QVariant &value)
{
    switch(option) {
    case ScaledSize: {
        const QSize &size = value.toSize();
        d->scaledSize = qMin(size.width(), size.height());
        break;
    }
    case BackgroundColor:
        d->backColor = qvariant_cast<QColor>(value);
    default:
        break;
    }
}

bool QDciIOHandler::supportsOption(ImageOption option) const
{
    switch(option)
    {
    case Size:
    case ScaledSize:
    case ImageFormat:
    case Animation:
    case BackgroundColor:
    case Endianness:
    case SupportedSubTypes:
        return true;
    default:
        break;
    }
    return false;
}

bool QDciIOHandler::canRead(QIODevice *device)
{
    QByteArray buf = device->peek(4);
    return buf.startsWith(QByteArrayLiteral("DCI"));
}

QT_END_NAMESPACE

#endif // QT_NO_SVGRENDERER