Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdbustraytypes.cpp
Go to the documentation of this file.
1// Copyright (C) 2009 Marco Martin <notmart@gmail.com>
2// Copyright (C) 2016 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QT_NO_SYSTEMTRAYICON
6
7#include "qdbustraytypes_p.h"
8
9#include <QDBusConnection>
10#include <QDBusMetaType>
11#include <QImage>
12#include <QIcon>
13#include <QIconEngine>
14#include <QImage>
15#include <QPixmap>
16#include <QDebug>
17#include <QtEndian>
18#include <QPainter>
19#include <QGuiApplication>
20#include <qpa/qplatformmenu.h>
21#include <private/qdbusplatformmenu_p.h>
22#include <private/qicon_p.h>
23
25
29
30static const int IconSizeLimit = 64;
31static const int IconNormalSmallSize = 22;
32static const int IconNormalMediumSize = 64;
33
35{
37 if (icon.isNull())
38 return ret;
39 QIconEngine *engine = const_cast<QIcon &>(icon).data_ptr()->engine;
40 QList<QSize> sizes = engine->availableSizes(QIcon::Normal, QIcon::Off);
41
42 // Omit any size larger than 64 px, to save D-Bus bandwidth;
43 // ensure that 22px or smaller exists, because it's a common size;
44 // and ensure that something between 22px and 64px exists, for better scaling to other sizes.
45 bool hasSmallIcon = false;
46 bool hasMediumIcon = false;
47 QList<QSize> toRemove;
48 for (const QSize &size : std::as_const(sizes)) {
49 int maxSize = qMax(size.width(), size.height());
50 if (maxSize <= IconNormalSmallSize)
51 hasSmallIcon = true;
52 else if (maxSize <= IconNormalMediumSize)
53 hasMediumIcon = true;
54 else if (maxSize > IconSizeLimit)
55 toRemove << size;
56 }
57 for (const QSize &size : std::as_const(toRemove))
58 sizes.removeOne(size);
59 if (!hasSmallIcon)
61 if (!hasMediumIcon)
63
64 ret.reserve(sizes.size());
65 for (const QSize &size : std::as_const(sizes)) {
66 // Protocol specifies ARGB32 format in network byte order
67 QImage im = engine->pixmap(size, QIcon::Normal, QIcon::Off).toImage().convertToFormat(QImage::Format_ARGB32);
68 // letterbox if necessary to make it square
69 if (im.height() != im.width()) {
70 int maxSize = qMax(im.width(), im.height());
71 QImage padded(maxSize, maxSize, QImage::Format_ARGB32);
72 padded.fill(Qt::transparent);
73 QPainter painter(&padded);
74 painter.drawImage((maxSize - im.width()) / 2, (maxSize - im.height()) / 2, im);
75 im = padded;
76 }
77 // copy and endian-convert
78 QXdgDBusImageStruct kim(im.width(), im.height());
79 qToBigEndian<quint32>(im.constBits(), im.width() * im.height(), kim.data.data());
80
81 ret << kim;
82 }
83 return ret;
84}
85
86// Marshall the ImageStruct data into a D-Bus argument
88{
90 argument << icon.width;
91 argument << icon.height;
92 argument << icon.data;
94 return argument;
95}
96
97// Retrieve the ImageStruct data from the D-Bus argument
99{
103
105 argument >> width;
106 argument >> height;
107 argument >> data;
109
110 icon.width = width;
111 icon.height = height;
112 icon.data = data;
113
114 return argument;
115}
116
117// Marshall the ImageVector data into a D-Bus argument
119{
120 argument.beginArray(qMetaTypeId<QXdgDBusImageStruct>());
121 for (int i = 0; i < iconVector.size(); ++i) {
122 argument << iconVector[i];
123 }
125 return argument;
126}
127
128// Retrieve the ImageVector data from the D-Bus argument
130{
132 iconVector.clear();
133
134 while (!argument.atEnd()) {
135 QXdgDBusImageStruct element;
136 argument >> element;
137 iconVector.append(element);
138 }
139
141
142 return argument;
143}
144
145// Marshall the ToolTipStruct data into a D-Bus argument
147{
149 argument << toolTip.icon;
150 argument << toolTip.image;
151 argument << toolTip.title;
152 argument << toolTip.subTitle;
154 return argument;
155}
156
157// Retrieve the ToolTipStruct data from the D-Bus argument
159{
163 QString subTitle;
164
166 argument >> icon;
167 argument >> image;
168 argument >> title;
169 argument >> subTitle;
171
172 toolTip.icon = icon;
173 toolTip.image = image;
174 toolTip.title = title;
175 toolTip.subTitle = subTitle;
176
177 return argument;
178}
179
181#endif // QT_NO_SYSTEMTRAYICON
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtDBus
void endArray()
Closes a D-Bus array opened with beginArray().
void beginStructure()
Opens a new D-Bus structure suitable for appending new arguments.
bool atEnd() const
Returns true if there are no more elements to be extracted from this QDBusArgument.
void endStructure()
Closes a D-Bus structure opened with beginStructure().
void beginArray(int elementMetaTypeId)
The QIconEngine class provides an abstract base class for QIcon renderers.
Definition qiconengine.h:15
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition qicon.cpp:1019
@ Normal
Definition qicon.h:22
@ Off
Definition qicon.h:23
\inmodule QtGui
Definition qimage.h:37
int width() const
Returns the width of the image.
int height() const
Returns the height of the image.
@ Format_ARGB32
Definition qimage.h:47
void fill(uint pixel)
Fills the entire image with the given pixelValue.
Definition qimage.cpp:1758
const uchar * constBits() const
Returns a pointer to the first pixel data.
Definition qimage.cpp:1733
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void drawImage(const QRectF &targetRect, const QImage &image, const QRectF &sourceRect, Qt::ImageConversionFlags flags=Qt::AutoColor)
Draws the rectangular portion source of the given image into the target rectangle in the paint device...
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
@ transparent
Definition qnamespace.h:47
Definition image.cpp:4
static const int IconNormalMediumSize
const QDBusArgument & operator>>(const QDBusArgument &argument, QXdgDBusImageStruct &icon)
static const int IconNormalSmallSize
static QT_BEGIN_NAMESPACE const int IconSizeLimit
QXdgDBusImageVector iconToQXdgDBusImageVector(const QIcon &icon)
const QDBusArgument & operator<<(QDBusArgument &argument, const QXdgDBusImageStruct &icon)
return ret
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1390
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint GLsizei width
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
const void * data_ptr(const QTransform &t)
Definition qpainter_p.h:48
int qint32
Definition qtypes.h:49
QString title
[35]
QPainter painter(this)
[7]
QDBusArgument argument
QJSEngine engine
[0]
QXdgDBusImageVector image