2 * Copyright (C) 2015 Canonical, Ltd.
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.
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.
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/>.
17#include "CursorImageProvider.h"
23#include <QSvgRenderer>
25CursorImageProvider *CursorImageProvider::m_instance = nullptr;
30BuiltInCursorImage::BuiltInCursorImage()
32 const char *svgString =
33 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
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\""
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\" />"
45 qimage = QImage(20, 32, QImage::Format_ARGB32);
46 QPainter imagePainter(&qimage);
48 QSvgRenderer *svgRenderer = new QSvgRenderer(QByteArray(svgString));
49 svgRenderer->render(&imagePainter);
57BlankCursorImage::BlankCursorImage()
59 qimage = QImage(1, 1, QImage::Format_ARGB32);
60 qimage.fill(Qt::transparent);
67CustomCursorImage::CustomCursorImage(const QCursor &cursor)
69 qimage = cursor.pixmap().toImage();
70 hotspot = cursor.hotSpot();
76XCursorImage::XCursorImage(const QString &theme, const QString &file)
77 : xcursorImages(nullptr)
79 xcursorImages = XcursorLibraryLoadImages(QFile::encodeName(file), QFile::encodeName(theme), 32);
85 for (int i = 0; i < xcursorImages->nimage && !loaded; ++i) {
86 XcursorImage *xcursorImage = xcursorImages->images[i];
87 if (xcursorImage->size == 32) {
89 qimage = QImage((uchar*)xcursorImage->pixels,
90 xcursorImage->width, xcursorImage->height, QImage::Format_ARGB32);
92 hotspot.setX(xcursorImage->xhot);
93 hotspot.setY(xcursorImage->yhot);
100XCursorImage::~XCursorImage()
102 XcursorImagesDestroy(xcursorImages);
106// CursorImageProvider
108CursorImageProvider::CursorImageProvider()
109 : QQuickImageProvider(QQuickImageProvider::Image)
112 qFatal("Cannot have multiple CursorImageProvider instances");
116 m_fallbackNames[QLatin1String("closedhand")].append(QLatin1String("grabbing"));
117 m_fallbackNames[QLatin1String("closedhand")].append(QLatin1String("dnd-none"));
119 m_fallbackNames[QLatin1String("dnd-copy")].append(QLatin1String("dnd-none"));
120 m_fallbackNames[QLatin1String("dnd-copy")].append(QLatin1String("grabbing"));
121 m_fallbackNames[QLatin1String("dnd-copy")].append(QLatin1String("closedhand"));
123 m_fallbackNames[QLatin1String("dnd-move")].append(QLatin1String("dnd-none"));
124 m_fallbackNames[QLatin1String("dnd-move")].append(QLatin1String("grabbing"));
125 m_fallbackNames[QLatin1String("dnd-move")].append(QLatin1String("closedhand"));
127 m_fallbackNames[QLatin1String("dnd-link")].append(QLatin1String("dnd-none"));
128 m_fallbackNames[QLatin1String("dnd-link")].append(QLatin1String("grabbing"));
129 m_fallbackNames[QLatin1String("dnd-link")].append(QLatin1String("closedhand"));
131 m_fallbackNames[QLatin1String("forbidden")].append(QLatin1String("crossed_circle")); // DMZ-White and DMZ-Black themes
132 m_fallbackNames[QLatin1String("forbidden")].append(QLatin1String("not-allowed"));
133 m_fallbackNames[QLatin1String("forbidden")].append(QLatin1String("circle"));
135 m_fallbackNames[QLatin1String("hand")].append(QLatin1String("pointing_hand"));
136 m_fallbackNames[QLatin1String("hand")].append(QLatin1String("pointer"));
138 m_fallbackNames[QLatin1String("ibeam")].append(QLatin1String("xterm"));
139 m_fallbackNames[QLatin1String("ibeam")].append(QLatin1String("text"));
141 m_fallbackNames[QLatin1String("left_ptr")].append(QLatin1String("default"));
142 m_fallbackNames[QLatin1String("left_ptr")].append(QLatin1String("top_left_arrow"));
143 m_fallbackNames[QLatin1String("left_ptr")].append(QLatin1String("left_arrow"));
145 m_fallbackNames[QLatin1String("left_ptr_watch")].append(QLatin1String("half-busy"));
146 m_fallbackNames[QLatin1String("left_ptr_watch")].append(QLatin1String("progress"));
148 m_fallbackNames[QLatin1String("size_bdiag")].append(QLatin1String("fd_double_arrow"));
149 m_fallbackNames[QLatin1String("size_bdiag")].append(QLatin1String("nesw-resize"));
151 m_fallbackNames[QLatin1String("size_fdiag")].append(QLatin1String("bd_double_arrow")); // DMZ-White and DMZ-Black themes
152 m_fallbackNames[QLatin1String("size_fdiag")].append(QLatin1String("nwse-resize"));
154 m_fallbackNames[QLatin1String("size_hor")].append(QLatin1String("sb_h_double_arrow")); // DMZ-White and DMZ-Black themes
155 m_fallbackNames[QLatin1String("size_hor")].append(QLatin1String("ew-resize"));
156 m_fallbackNames[QLatin1String("size_hor")].append(QLatin1String("h_double_arrow"));
158 m_fallbackNames[QLatin1String("size_ver")].append(QLatin1String("sb_v_double_arrow")); // DMZ-White and DMZ-Black themes
159 m_fallbackNames[QLatin1String("size_ver")].append(QLatin1String("ns-resize"));
160 m_fallbackNames[QLatin1String("size_ver")].append(QLatin1String("v_double_arrow"));
162 m_fallbackNames[QLatin1String("split_h")].append(QLatin1String("sb_h_double_arrow")); // DMZ-White and DMZ-Black themes
163 m_fallbackNames[QLatin1String("split_h")].append(QLatin1String("col-resize"));
165 m_fallbackNames[QLatin1String("split_v")].append(QLatin1String("sb_v_double_arrow")); // DMZ-White and DMZ-Black themes
166 m_fallbackNames[QLatin1String("split_v")].append(QLatin1String("row-resize"));
168 m_fallbackNames[QLatin1String("up_arrow")].append(QLatin1String("sb_up_arrow")); // DMZ-White and DMZ-Black themes
170 m_fallbackNames[QLatin1String("watch")].append(QLatin1String("wait"));
172 m_fallbackNames[QLatin1String("whats_this")].append(QLatin1String("left_ptr_help"));
173 m_fallbackNames[QLatin1String("whats_this")].append(QLatin1String("help"));
174 m_fallbackNames[QLatin1String("whats_this")].append(QLatin1String("question_arrow"));
176 m_fallbackNames[QLatin1String("xterm")].append(QLatin1String("ibeam"));
179CursorImageProvider::~CursorImageProvider()
182 QList< QMap<QString, CursorImage*> > cursorList = m_cursors.values();
184 for (int i = 0; i < cursorList.count(); ++i) {
185 QList<CursorImage*> cursorImageList = cursorList[i].values();
186 for (int j = 0; j < cursorImageList.count(); ++j) {
187 delete cursorImageList[j];
193 m_instance = nullptr;
196QImage CursorImageProvider::requestImage(const QString &cursorThemeAndName, QSize *size, const QSize & /*requestedSize*/)
198 CursorImage *cursorImage = fetchCursor(cursorThemeAndName);
199 size->setWidth(cursorImage->qimage.width());
200 size->setHeight(cursorImage->qimage.height());
202 return cursorImage->qimage;
205QPoint CursorImageProvider::hotspot(const QString &themeName, const QString &cursorName)
207 CursorImage *cursorImage = fetchCursor(themeName, cursorName);
209 return cursorImage->hotspot;
215CursorImage *CursorImageProvider::fetchCursor(const QString &cursorThemeAndName)
220 QStringList themeAndNameList = cursorThemeAndName.split("/");
221 if (themeAndNameList.size() != 2) {
224 themeName = themeAndNameList[0];
225 cursorName = themeAndNameList[1];
228 return fetchCursor(themeName, cursorName);
231CursorImage *CursorImageProvider::fetchCursor(const QString &themeName, const QString &cursorName)
233 CursorImage *cursorImage = fetchCursorHelper(themeName, cursorName);
235 // Try some fallbacks
236 if (cursorImage->qimage.isNull()) {
237 if (m_fallbackNames.contains(cursorName)) {
238 const QStringList &fallbackNames = m_fallbackNames[cursorName];
240 while (cursorImage->qimage.isNull() && i < fallbackNames.count()) {
241 qDebug().nospace() << "CursorImageProvider: "<< cursorName <<" not found, trying " << fallbackNames.at(i);
242 cursorImage = fetchCursorHelper(themeName, fallbackNames.at(i));
248 // if it all fails, there must be at least a left_ptr
249 if (cursorImage->qimage.isNull() && cursorName != "left_ptr") {
250 qDebug() << "CursorImageProvider:" << cursorName
251 << "not found (nor its fallbacks, if any). Going for \"left_ptr\" as a last resort.";
252 cursorImage = fetchCursorHelper(themeName, "left_ptr");
255 if (cursorImage->qimage.isNull()) {
256 // finally, go for the built-in cursor
257 qWarning() << "CursorImageProvider: couldn't find any cursors. Using the built-in one";
258 if (!m_builtInCursorImage) {
259 m_builtInCursorImage.reset(new BuiltInCursorImage);
261 cursorImage = m_builtInCursorImage.data();
267CursorImage *CursorImageProvider::fetchCursorHelper(const QString &themeName, const QString &cursorName)
269 if (cursorName == QLatin1String("blank")) {
270 return &m_blankCursorImage;
271 } else if (cursorName == QLatin1String("custom")) {
272 return m_customCursorImage.data();
274 QMap<QString, CursorImage*> &themeCursors = m_cursors[themeName];
276 if (!themeCursors.contains(cursorName)) {
277 themeCursors[cursorName] = new XCursorImage(themeName, cursorName);
280 return themeCursors[cursorName];
284void CursorImageProvider::setCustomCursor(const QCursor &customCursor)
286 if (customCursor.pixmap().isNull()) {
287 m_customCursorImage.reset();
289 m_customCursorImage.reset(new CustomCursorImage(customCursor));
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list