Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/platformsupport/dbustray/qdbustraytypes.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2009 Marco Martin <notmart@gmail.com> | - | ||||||
4 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||
5 | ** Contact: http://www.qt.io/licensing/ | - | ||||||
6 | ** | - | ||||||
7 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||
8 | ** | - | ||||||
9 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||
16 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||
21 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||
22 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||
23 | ** following information to ensure the GNU Lesser General Public License | - | ||||||
24 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||
25 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||
26 | ** | - | ||||||
27 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||
28 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||
29 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||
30 | ** | - | ||||||
31 | ** $QT_END_LICENSE$ | - | ||||||
32 | ** | - | ||||||
33 | ****************************************************************************/ | - | ||||||
34 | - | |||||||
35 | #ifndef QT_NO_SYSTEMTRAYICON | - | ||||||
36 | - | |||||||
37 | #include "qdbustraytypes_p.h" | - | ||||||
38 | - | |||||||
39 | #include <QDBusConnection> | - | ||||||
40 | #include <QDBusMetaType> | - | ||||||
41 | #include <QImage> | - | ||||||
42 | #include <QIcon> | - | ||||||
43 | #include <QImage> | - | ||||||
44 | #include <QPixmap> | - | ||||||
45 | #include <QDebug> | - | ||||||
46 | #include <QtEndian> | - | ||||||
47 | #include <QPainter> | - | ||||||
48 | #include <QGuiApplication> | - | ||||||
49 | #include <qpa/qplatformmenu.h> | - | ||||||
50 | #include "qdbusplatformmenu_p.h" | - | ||||||
51 | - | |||||||
52 | QT_BEGIN_NAMESPACE | - | ||||||
53 | - | |||||||
54 | static const int IconSizeLimit = 64; | - | ||||||
55 | static const int IconNormalSmallSize = 22; | - | ||||||
56 | static const int IconNormalMediumSize = 64; | - | ||||||
57 | - | |||||||
58 | QXdgDBusImageVector iconToQXdgDBusImageVector(const QIcon &icon) | - | ||||||
59 | { | - | ||||||
60 | QXdgDBusImageVector ret; | - | ||||||
61 | QList<QSize> sizes = icon.availableSizes(); | - | ||||||
62 | - | |||||||
63 | // Omit any size larger than 64 px, to save D-Bus bandwidth; | - | ||||||
64 | // ensure that 22px or smaller exists, because it's a common size; | - | ||||||
65 | // and ensure that something between 22px and 64px exists, for better scaling to other sizes. | - | ||||||
66 | bool hasSmallIcon = false; | - | ||||||
67 | bool hasMediumIcon = false; | - | ||||||
68 | qreal dpr = qGuiApp->devicePixelRatio(); | - | ||||||
69 | QList<QSize> toRemove; | - | ||||||
70 | Q_FOREACH (const QSize &size, sizes) { | - | ||||||
71 | int maxSize = qMax(size.width(), size.height()); | - | ||||||
72 | if (maxSize <= IconNormalSmallSize * dpr)
| 0 | ||||||
73 | hasSmallIcon = true; never executed: hasSmallIcon = true; | 0 | ||||||
74 | else if (maxSize <= IconNormalMediumSize * dpr)
| 0 | ||||||
75 | hasMediumIcon = true; never executed: hasMediumIcon = true; | 0 | ||||||
76 | else if (maxSize > IconSizeLimit * dpr)
| 0 | ||||||
77 | toRemove << size; never executed: toRemove << size; | 0 | ||||||
78 | } never executed: end of block | 0 | ||||||
79 | Q_FOREACH (const QSize &size, toRemove) | - | ||||||
80 | sizes.removeOne(size); never executed: sizes.removeOne(size); | 0 | ||||||
81 | if (!hasSmallIcon)
| 0 | ||||||
82 | sizes.append(QSize(IconNormalSmallSize * dpr, IconNormalSmallSize * dpr)); never executed: sizes.append(QSize(IconNormalSmallSize * dpr, IconNormalSmallSize * dpr)); | 0 | ||||||
83 | if (!hasMediumIcon)
| 0 | ||||||
84 | sizes.append(QSize(IconNormalMediumSize * dpr, IconNormalMediumSize * dpr)); never executed: sizes.append(QSize(IconNormalMediumSize * dpr, IconNormalMediumSize * dpr)); | 0 | ||||||
85 | - | |||||||
86 | ret.reserve(sizes.size()); | - | ||||||
87 | foreach (QSize size, sizes) { | - | ||||||
88 | // Protocol specifies ARGB32 format in network byte order | - | ||||||
89 | QImage im = icon.pixmap(size).toImage().convertToFormat(QImage::Format_ARGB32); | - | ||||||
90 | // letterbox if necessary to make it square | - | ||||||
91 | if (im.height() != im.width()) {
| 0 | ||||||
92 | int maxSize = qMax(im.width(), im.height()); | - | ||||||
93 | QImage padded(maxSize, maxSize, QImage::Format_ARGB32); | - | ||||||
94 | padded.fill(Qt::transparent); | - | ||||||
95 | QPainter painter(&padded); | - | ||||||
96 | painter.drawImage((maxSize - im.width()) / 2, (maxSize - im.height()) / 2, im); | - | ||||||
97 | im = padded; | - | ||||||
98 | } never executed: end of block | 0 | ||||||
99 | // copy and endian-convert | - | ||||||
100 | QXdgDBusImageStruct kim(im.width(), im.height()); | - | ||||||
101 | const uchar *end = im.constBits() + im.byteCount(); | - | ||||||
102 | uchar *dest = reinterpret_cast<uchar *>(kim.data.data()); | - | ||||||
103 | for (const uchar *src = im.constBits(); src < end; src += 4, dest += 4)
| 0 | ||||||
104 | qToUnaligned(qToBigEndian<quint32>(qFromUnaligned<quint32>(src)), dest); never executed: qToUnaligned(qToBigEndian<quint32>(qFromUnaligned<quint32>(src)), dest); | 0 | ||||||
105 | - | |||||||
106 | ret << kim; | - | ||||||
107 | } never executed: end of block | 0 | ||||||
108 | return ret; never executed: return ret; | 0 | ||||||
109 | } | - | ||||||
110 | - | |||||||
111 | // Marshall the ImageStruct data into a D-Bus argument | - | ||||||
112 | const QDBusArgument &operator<<(QDBusArgument &argument, const QXdgDBusImageStruct &icon) | - | ||||||
113 | { | - | ||||||
114 | argument.beginStructure(); | - | ||||||
115 | argument << icon.width; | - | ||||||
116 | argument << icon.height; | - | ||||||
117 | argument << icon.data; | - | ||||||
118 | argument.endStructure(); | - | ||||||
119 | return argument; never executed: return argument; | 0 | ||||||
120 | } | - | ||||||
121 | - | |||||||
122 | // Retrieve the ImageStruct data from the D-Bus argument | - | ||||||
123 | const QDBusArgument &operator>>(const QDBusArgument &argument, QXdgDBusImageStruct &icon) | - | ||||||
124 | { | - | ||||||
125 | qint32 width; | - | ||||||
126 | qint32 height; | - | ||||||
127 | QByteArray data; | - | ||||||
128 | - | |||||||
129 | argument.beginStructure(); | - | ||||||
130 | argument >> width; | - | ||||||
131 | argument >> height; | - | ||||||
132 | argument >> data; | - | ||||||
133 | argument.endStructure(); | - | ||||||
134 | - | |||||||
135 | icon.width = width; | - | ||||||
136 | icon.height = height; | - | ||||||
137 | icon.data = data; | - | ||||||
138 | - | |||||||
139 | return argument; never executed: return argument; | 0 | ||||||
140 | } | - | ||||||
141 | - | |||||||
142 | // Marshall the ImageVector data into a D-Bus argument | - | ||||||
143 | const QDBusArgument &operator<<(QDBusArgument &argument, const QXdgDBusImageVector &iconVector) | - | ||||||
144 | { | - | ||||||
145 | argument.beginArray(qMetaTypeId<QXdgDBusImageStruct>()); | - | ||||||
146 | for (int i = 0; i < iconVector.size(); ++i) {
| 0 | ||||||
147 | argument << iconVector[i]; | - | ||||||
148 | } never executed: end of block | 0 | ||||||
149 | argument.endArray(); | - | ||||||
150 | return argument; never executed: return argument; | 0 | ||||||
151 | } | - | ||||||
152 | - | |||||||
153 | // Retrieve the ImageVector data from the D-Bus argument | - | ||||||
154 | const QDBusArgument &operator>>(const QDBusArgument &argument, QXdgDBusImageVector &iconVector) | - | ||||||
155 | { | - | ||||||
156 | argument.beginArray(); | - | ||||||
157 | iconVector.clear(); | - | ||||||
158 | - | |||||||
159 | while (!argument.atEnd()) {
| 0 | ||||||
160 | QXdgDBusImageStruct element; | - | ||||||
161 | argument >> element; | - | ||||||
162 | iconVector.append(element); | - | ||||||
163 | } never executed: end of block | 0 | ||||||
164 | - | |||||||
165 | argument.endArray(); | - | ||||||
166 | - | |||||||
167 | return argument; never executed: return argument; | 0 | ||||||
168 | } | - | ||||||
169 | - | |||||||
170 | // Marshall the ToolTipStruct data into a D-Bus argument | - | ||||||
171 | const QDBusArgument &operator<<(QDBusArgument &argument, const QXdgDBusToolTipStruct &toolTip) | - | ||||||
172 | { | - | ||||||
173 | argument.beginStructure(); | - | ||||||
174 | argument << toolTip.icon; | - | ||||||
175 | argument << toolTip.image; | - | ||||||
176 | argument << toolTip.title; | - | ||||||
177 | argument << toolTip.subTitle; | - | ||||||
178 | argument.endStructure(); | - | ||||||
179 | return argument; never executed: return argument; | 0 | ||||||
180 | } | - | ||||||
181 | - | |||||||
182 | // Retrieve the ToolTipStruct data from the D-Bus argument | - | ||||||
183 | const QDBusArgument &operator>>(const QDBusArgument &argument, QXdgDBusToolTipStruct &toolTip) | - | ||||||
184 | { | - | ||||||
185 | QString icon; | - | ||||||
186 | QXdgDBusImageVector image; | - | ||||||
187 | QString title; | - | ||||||
188 | QString subTitle; | - | ||||||
189 | - | |||||||
190 | argument.beginStructure(); | - | ||||||
191 | argument >> icon; | - | ||||||
192 | argument >> image; | - | ||||||
193 | argument >> title; | - | ||||||
194 | argument >> subTitle; | - | ||||||
195 | argument.endStructure(); | - | ||||||
196 | - | |||||||
197 | toolTip.icon = icon; | - | ||||||
198 | toolTip.image = image; | - | ||||||
199 | toolTip.title = title; | - | ||||||
200 | toolTip.subTitle = subTitle; | - | ||||||
201 | - | |||||||
202 | return argument; never executed: return argument; | 0 | ||||||
203 | } | - | ||||||
204 | - | |||||||
205 | QT_END_NAMESPACE | - | ||||||
206 | #endif // QT_NO_SYSTEMTRAYICON | - | ||||||
Source code | Switch to Preprocessed file |