on f398eec help sign in
unity8/plugins/Cursor/CursorImageProvider.cpp
294 lines 11 kB history raw
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "CursorImageProvider.h"
19#include <QCursor>
20#include <QDebug>
21#include <QFile>
22#include <QPainter>
23#include <QSvgRenderer>
25CursorImageProvider *CursorImageProvider::m_instance = nullptr;
27/////
28// BuiltInCursorImage
30BuiltInCursorImage::BuiltInCursorImage()
32 const char *svgString =
33 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
34 "<svg"
35 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
36 " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""
37 " xmlns:svg=\"http://www.w3.org/2000/svg\""
38 " xmlns=\"http://www.w3.org/2000/svg\""
39 " version=\"1.1\">"
40 " <path"
41 " style=\"fill:#ffffff;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:40;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\""
42 " d=\"M 20.504,50.94931 460.42533,518.14486 266.47603,515.61948 366.48114,719.16522 274.05218,770.68296 172.53185,559.56112 20.504,716.13476 Z\" />"
43 "</svg>";
45 qimage = QImage(20, 32, QImage::Format_ARGB32);
46 qimage.fill(Qt::transparent);
47 QPainter imagePainter(&qimage);
49 QSvgRenderer *svgRenderer = new QSvgRenderer(QByteArray(svgString));
50 svgRenderer->render(&imagePainter);
51 delete svgRenderer;
54/////
55// BlankCursorImage
58BlankCursorImage::BlankCursorImage()
60 qimage = QImage(1, 1, QImage::Format_ARGB32);
61 qimage.fill(Qt::transparent);
64/////
65// CustomCursorImage
68CustomCursorImage::CustomCursorImage(const QCursor &cursor)
70 qimage = cursor.pixmap().toImage();
71 hotspot = cursor.hotSpot();
74/////
75// XCursorImage
77XCursorImage::XCursorImage(const QString &theme, const QString &file)
78 : xcursorImages(nullptr)
80 // TODO: Consider grid unit value
81 // Hardcoding to a medium size for now
82 int preferredCursorHeightPx = 32;
84 xcursorImages = XcursorLibraryLoadImages(QFile::encodeName(file), QFile::encodeName(theme),
85 preferredCursorHeightPx);
86 if (!xcursorImages) {
87 return;
88 }
90 // Just take the first one. It will have multiple images in case of an animated cursor.
91 // TODO: Support animated cursors
92 if ( xcursorImages->nimage > 0) {
93 XcursorImage *xcursorImage = xcursorImages->images[0];
95 qimage = QImage((uchar*)xcursorImage->pixels,
96 xcursorImage->width, xcursorImage->height, QImage::Format_ARGB32);
98 hotspot.setX(xcursorImage->xhot);
99 hotspot.setY(xcursorImage->yhot);
100 }
103XCursorImage::~XCursorImage()
105 XcursorImagesDestroy(xcursorImages);
108/////
109// CursorImageProvider
111CursorImageProvider::CursorImageProvider()
112 : QQuickImageProvider(QQuickImageProvider::Image)
114 if (m_instance) {
115 qFatal("Cannot have multiple CursorImageProvider instances");
116 }
117 m_instance = this;
119 m_fallbackNames[QStringLiteral("closedhand")].append(QStringLiteral("grabbing"));
120 m_fallbackNames[QStringLiteral("closedhand")].append(QStringLiteral("dnd-none"));
122 m_fallbackNames[QStringLiteral("dnd-copy")].append(QStringLiteral("dnd-none"));
123 m_fallbackNames[QStringLiteral("dnd-copy")].append(QStringLiteral("grabbing"));
124 m_fallbackNames[QStringLiteral("dnd-copy")].append(QStringLiteral("closedhand"));
126 m_fallbackNames[QStringLiteral("dnd-move")].append(QStringLiteral("dnd-none"));
127 m_fallbackNames[QStringLiteral("dnd-move")].append(QStringLiteral("grabbing"));
128 m_fallbackNames[QStringLiteral("dnd-move")].append(QStringLiteral("closedhand"));
130 m_fallbackNames[QStringLiteral("dnd-link")].append(QStringLiteral("dnd-none"));
131 m_fallbackNames[QStringLiteral("dnd-link")].append(QStringLiteral("grabbing"));
132 m_fallbackNames[QStringLiteral("dnd-link")].append(QStringLiteral("closedhand"));
134 m_fallbackNames[QStringLiteral("forbidden")].append(QStringLiteral("crossed_circle")); // DMZ-White and DMZ-Black themes
135 m_fallbackNames[QStringLiteral("forbidden")].append(QStringLiteral("not-allowed"));
136 m_fallbackNames[QStringLiteral("forbidden")].append(QStringLiteral("circle"));
138 m_fallbackNames[QStringLiteral("hand")].append(QStringLiteral("pointing_hand"));
139 m_fallbackNames[QStringLiteral("hand")].append(QStringLiteral("pointer"));
141 m_fallbackNames[QStringLiteral("ibeam")].append(QStringLiteral("xterm"));
142 m_fallbackNames[QStringLiteral("ibeam")].append(QStringLiteral("text"));
144 m_fallbackNames[QStringLiteral("left_ptr")].append(QStringLiteral("default"));
145 m_fallbackNames[QStringLiteral("left_ptr")].append(QStringLiteral("top_left_arrow"));
146 m_fallbackNames[QStringLiteral("left_ptr")].append(QStringLiteral("left_arrow"));
148 m_fallbackNames[QStringLiteral("left_ptr_watch")].append(QStringLiteral("half-busy"));
149 m_fallbackNames[QStringLiteral("left_ptr_watch")].append(QStringLiteral("progress"));
151 m_fallbackNames[QStringLiteral("size_bdiag")].append(QStringLiteral("fd_double_arrow"));
152 m_fallbackNames[QStringLiteral("size_bdiag")].append(QStringLiteral("nesw-resize"));
154 m_fallbackNames[QStringLiteral("size_fdiag")].append(QStringLiteral("bd_double_arrow")); // DMZ-White and DMZ-Black themes
155 m_fallbackNames[QStringLiteral("size_fdiag")].append(QStringLiteral("nwse-resize"));
157 m_fallbackNames[QStringLiteral("size_hor")].append(QStringLiteral("sb_h_double_arrow")); // DMZ-White and DMZ-Black themes
158 m_fallbackNames[QStringLiteral("size_hor")].append(QStringLiteral("ew-resize"));
159 m_fallbackNames[QStringLiteral("size_hor")].append(QStringLiteral("h_double_arrow"));
161 m_fallbackNames[QStringLiteral("size_ver")].append(QStringLiteral("sb_v_double_arrow")); // DMZ-White and DMZ-Black themes
162 m_fallbackNames[QStringLiteral("size_ver")].append(QStringLiteral("ns-resize"));
163 m_fallbackNames[QStringLiteral("size_ver")].append(QStringLiteral("v_double_arrow"));
165 m_fallbackNames[QStringLiteral("split_h")].append(QStringLiteral("sb_h_double_arrow")); // DMZ-White and DMZ-Black themes
166 m_fallbackNames[QStringLiteral("split_h")].append(QStringLiteral("col-resize"));
168 m_fallbackNames[QStringLiteral("split_v")].append(QStringLiteral("sb_v_double_arrow")); // DMZ-White and DMZ-Black themes
169 m_fallbackNames[QStringLiteral("split_v")].append(QStringLiteral("row-resize"));
171 m_fallbackNames[QStringLiteral("up_arrow")].append(QStringLiteral("sb_up_arrow")); // DMZ-White and DMZ-Black themes
173 m_fallbackNames[QStringLiteral("watch")].append(QStringLiteral("wait"));
175 m_fallbackNames[QStringLiteral("whats_this")].append(QStringLiteral("left_ptr_help"));
176 m_fallbackNames[QStringLiteral("whats_this")].append(QStringLiteral("help"));
177 m_fallbackNames[QStringLiteral("whats_this")].append(QStringLiteral("question_arrow"));
179 m_fallbackNames[QStringLiteral("xterm")].append(QStringLiteral("ibeam"));
182CursorImageProvider::~CursorImageProvider()
184 {
185 QList< QMap<QString, CursorImage*> > cursorList = m_cursors.values();
187 for (int i = 0; i < cursorList.count(); ++i) {
188 QList<CursorImage*> cursorImageList = cursorList[i].values();
189 for (int j = 0; j < cursorImageList.count(); ++j) {
190 delete cursorImageList[j];
191 }
192 }
193 }
195 m_cursors.clear();
196 m_instance = nullptr;
199QImage CursorImageProvider::requestImage(const QString &cursorThemeAndName, QSize *size, const QSize & /*requestedSize*/)
201 CursorImage *cursorImage = fetchCursor(cursorThemeAndName);
202 size->setWidth(cursorImage->qimage.width());
203 size->setHeight(cursorImage->qimage.height());
205 return cursorImage->qimage;
208QPoint CursorImageProvider::hotspot(const QString &themeName, const QString &cursorName)
210 CursorImage *cursorImage = fetchCursor(themeName, cursorName);
211 if (cursorImage) {
212 return cursorImage->hotspot;
213 } else {
214 return QPoint(0,0);
215 }
218CursorImage *CursorImageProvider::fetchCursor(const QString &cursorThemeAndName)
220 QString themeName;
221 QString cursorName;
222 {
223 QStringList themeAndNameList = cursorThemeAndName.split('/');
224 if (themeAndNameList.size() != 2) {
225 return nullptr;
226 }
227 themeName = themeAndNameList[0];
228 cursorName = themeAndNameList[1];
229 }
231 return fetchCursor(themeName, cursorName);
234CursorImage *CursorImageProvider::fetchCursor(const QString &themeName, const QString &cursorName)
236 CursorImage *cursorImage = fetchCursorHelper(themeName, cursorName);
238 // Try some fallbacks
239 if (cursorImage->qimage.isNull()) {
240 if (m_fallbackNames.contains(cursorName)) {
241 const QStringList &fallbackNames = m_fallbackNames[cursorName];
242 int i = 0;
243 while (cursorImage->qimage.isNull() && i < fallbackNames.count()) {
244 qDebug().nospace() << "CursorImageProvider: "<< cursorName <<" not found, trying " << fallbackNames.at(i);
245 cursorImage = fetchCursorHelper(themeName, fallbackNames.at(i));
246 ++i;
247 }
248 }
249 }
251 // if it all fails, there must be at least a left_ptr
252 if (cursorImage->qimage.isNull() && cursorName != QLatin1String("left_ptr")) {
253 qDebug() << "CursorImageProvider:" << cursorName
254 << "not found (nor its fallbacks, if any). Going for \"left_ptr\" as a last resort.";
255 cursorImage = fetchCursorHelper(themeName, QStringLiteral("left_ptr"));
256 }
258 if (cursorImage->qimage.isNull()) {
259 // finally, go for the built-in cursor
260 qWarning() << "CursorImageProvider: couldn't find any cursors. Using the built-in one";
261 if (!m_builtInCursorImage) {
262 m_builtInCursorImage.reset(new BuiltInCursorImage);
263 }
264 cursorImage = m_builtInCursorImage.data();
265 }
267 return cursorImage;
270CursorImage *CursorImageProvider::fetchCursorHelper(const QString &themeName, const QString &cursorName)
272 if (cursorName == QLatin1String("blank")) {
273 return &m_blankCursorImage;
274 } else if (cursorName == QLatin1String("custom")) {
275 return m_customCursorImage.data();
276 } else {
277 QMap<QString, CursorImage*> &themeCursors = m_cursors[themeName];
279 if (!themeCursors.contains(cursorName)) {
280 themeCursors[cursorName] = new XCursorImage(themeName, cursorName);
281 }
283 return themeCursors[cursorName];
284 }
287void CursorImageProvider::setCustomCursor(const QCursor &customCursor)
289 if (customCursor.pixmap().isNull()) {
290 m_customCursorImage.reset();
291 } else {
292 m_customCursorImage.reset(new CustomCursorImage(customCursor));
293 }