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 // TODO: Consider grid unit value
80 // Hardcoding to a medium size for now
81 int preferredCursorHeightPx = 32;
83 xcursorImages = XcursorLibraryLoadImages(QFile::encodeName(file), QFile::encodeName(theme),
84 preferredCursorHeightPx);
89 // Just take the first one. It will have multiple images in case of an animated cursor.
90 // TODO: Support animated cursors
91 if ( xcursorImages->nimage > 0) {
92 XcursorImage *xcursorImage = xcursorImages->images[0];
94 qimage = QImage((uchar*)xcursorImage->pixels,
95 xcursorImage->width, xcursorImage->height, QImage::Format_ARGB32);
97 hotspot.setX(xcursorImage->xhot);
98 hotspot.setY(xcursorImage->yhot);
102XCursorImage::~XCursorImage()
104 XcursorImagesDestroy(xcursorImages);
108// CursorImageProvider
110CursorImageProvider::CursorImageProvider()
111 : QQuickImageProvider(QQuickImageProvider::Image)
114 qFatal("Cannot have multiple CursorImageProvider instances");
118 m_fallbackNames[QLatin1String("closedhand")].append(QLatin1String("grabbing"));
119 m_fallbackNames[QLatin1String("closedhand")].append(QLatin1String("dnd-none"));
121 m_fallbackNames[QLatin1String("dnd-copy")].append(QLatin1String("dnd-none"));
122 m_fallbackNames[QLatin1String("dnd-copy")].append(QLatin1String("grabbing"));
123 m_fallbackNames[QLatin1String("dnd-copy")].append(QLatin1String("closedhand"));
125 m_fallbackNames[QLatin1String("dnd-move")].append(QLatin1String("dnd-none"));
126 m_fallbackNames[QLatin1String("dnd-move")].append(QLatin1String("grabbing"));
127 m_fallbackNames[QLatin1String("dnd-move")].append(QLatin1String("closedhand"));
129 m_fallbackNames[QLatin1String("dnd-link")].append(QLatin1String("dnd-none"));
130 m_fallbackNames[QLatin1String("dnd-link")].append(QLatin1String("grabbing"));
131 m_fallbackNames[QLatin1String("dnd-link")].append(QLatin1String("closedhand"));
133 m_fallbackNames[QLatin1String("forbidden")].append(QLatin1String("crossed_circle")); // DMZ-White and DMZ-Black themes
134 m_fallbackNames[QLatin1String("forbidden")].append(QLatin1String("not-allowed"));
135 m_fallbackNames[QLatin1String("forbidden")].append(QLatin1String("circle"));
137 m_fallbackNames[QLatin1String("hand")].append(QLatin1String("pointing_hand"));
138 m_fallbackNames[QLatin1String("hand")].append(QLatin1String("pointer"));
140 m_fallbackNames[QLatin1String("ibeam")].append(QLatin1String("xterm"));
141 m_fallbackNames[QLatin1String("ibeam")].append(QLatin1String("text"));
143 m_fallbackNames[QLatin1String("left_ptr")].append(QLatin1String("default"));
144 m_fallbackNames[QLatin1String("left_ptr")].append(QLatin1String("top_left_arrow"));
145 m_fallbackNames[QLatin1String("left_ptr")].append(QLatin1String("left_arrow"));
147 m_fallbackNames[QLatin1String("left_ptr_watch")].append(QLatin1String("half-busy"));
148 m_fallbackNames[QLatin1String("left_ptr_watch")].append(QLatin1String("progress"));
150 m_fallbackNames[QLatin1String("size_bdiag")].append(QLatin1String("fd_double_arrow"));
151 m_fallbackNames[QLatin1String("size_bdiag")].append(QLatin1String("nesw-resize"));
153 m_fallbackNames[QLatin1String("size_fdiag")].append(QLatin1String("bd_double_arrow")); // DMZ-White and DMZ-Black themes
154 m_fallbackNames[QLatin1String("size_fdiag")].append(QLatin1String("nwse-resize"));
156 m_fallbackNames[QLatin1String("size_hor")].append(QLatin1String("sb_h_double_arrow")); // DMZ-White and DMZ-Black themes
157 m_fallbackNames[QLatin1String("size_hor")].append(QLatin1String("ew-resize"));
158 m_fallbackNames[QLatin1String("size_hor")].append(QLatin1String("h_double_arrow"));
160 m_fallbackNames[QLatin1String("size_ver")].append(QLatin1String("sb_v_double_arrow")); // DMZ-White and DMZ-Black themes
161 m_fallbackNames[QLatin1String("size_ver")].append(QLatin1String("ns-resize"));
162 m_fallbackNames[QLatin1String("size_ver")].append(QLatin1String("v_double_arrow"));
164 m_fallbackNames[QLatin1String("split_h")].append(QLatin1String("sb_h_double_arrow")); // DMZ-White and DMZ-Black themes
165 m_fallbackNames[QLatin1String("split_h")].append(QLatin1String("col-resize"));
167 m_fallbackNames[QLatin1String("split_v")].append(QLatin1String("sb_v_double_arrow")); // DMZ-White and DMZ-Black themes
168 m_fallbackNames[QLatin1String("split_v")].append(QLatin1String("row-resize"));
170 m_fallbackNames[QLatin1String("up_arrow")].append(QLatin1String("sb_up_arrow")); // DMZ-White and DMZ-Black themes
172 m_fallbackNames[QLatin1String("watch")].append(QLatin1String("wait"));
174 m_fallbackNames[QLatin1String("whats_this")].append(QLatin1String("left_ptr_help"));
175 m_fallbackNames[QLatin1String("whats_this")].append(QLatin1String("help"));
176 m_fallbackNames[QLatin1String("whats_this")].append(QLatin1String("question_arrow"));
178 m_fallbackNames[QLatin1String("xterm")].append(QLatin1String("ibeam"));
181CursorImageProvider::~CursorImageProvider()
184 QList< QMap<QString, CursorImage*> > cursorList = m_cursors.values();
186 for (int i = 0; i < cursorList.count(); ++i) {
187 QList<CursorImage*> cursorImageList = cursorList[i].values();
188 for (int j = 0; j < cursorImageList.count(); ++j) {
189 delete cursorImageList[j];
195 m_instance = nullptr;
198QImage CursorImageProvider::requestImage(const QString &cursorThemeAndName, QSize *size, const QSize & /*requestedSize*/)
200 CursorImage *cursorImage = fetchCursor(cursorThemeAndName);
201 size->setWidth(cursorImage->qimage.width());
202 size->setHeight(cursorImage->qimage.height());
204 return cursorImage->qimage;
207QPoint CursorImageProvider::hotspot(const QString &themeName, const QString &cursorName)
209 CursorImage *cursorImage = fetchCursor(themeName, cursorName);
211 return cursorImage->hotspot;
217CursorImage *CursorImageProvider::fetchCursor(const QString &cursorThemeAndName)
222 QStringList themeAndNameList = cursorThemeAndName.split("/");
223 if (themeAndNameList.size() != 2) {
226 themeName = themeAndNameList[0];
227 cursorName = themeAndNameList[1];
230 return fetchCursor(themeName, cursorName);
233CursorImage *CursorImageProvider::fetchCursor(const QString &themeName, const QString &cursorName)
235 CursorImage *cursorImage = fetchCursorHelper(themeName, cursorName);
237 // Try some fallbacks
238 if (cursorImage->qimage.isNull()) {
239 if (m_fallbackNames.contains(cursorName)) {
240 const QStringList &fallbackNames = m_fallbackNames[cursorName];
242 while (cursorImage->qimage.isNull() && i < fallbackNames.count()) {
243 qDebug().nospace() << "CursorImageProvider: "<< cursorName <<" not found, trying " << fallbackNames.at(i);
244 cursorImage = fetchCursorHelper(themeName, fallbackNames.at(i));
250 // if it all fails, there must be at least a left_ptr
251 if (cursorImage->qimage.isNull() && cursorName != "left_ptr") {
252 qDebug() << "CursorImageProvider:" << cursorName
253 << "not found (nor its fallbacks, if any). Going for \"left_ptr\" as a last resort.";
254 cursorImage = fetchCursorHelper(themeName, "left_ptr");
257 if (cursorImage->qimage.isNull()) {
258 // finally, go for the built-in cursor
259 qWarning() << "CursorImageProvider: couldn't find any cursors. Using the built-in one";
260 if (!m_builtInCursorImage) {
261 m_builtInCursorImage.reset(new BuiltInCursorImage);
263 cursorImage = m_builtInCursorImage.data();
269CursorImage *CursorImageProvider::fetchCursorHelper(const QString &themeName, const QString &cursorName)
271 if (cursorName == QLatin1String("blank")) {
272 return &m_blankCursorImage;
273 } else if (cursorName == QLatin1String("custom")) {
274 return m_customCursorImage.data();
276 QMap<QString, CursorImage*> &themeCursors = m_cursors[themeName];
278 if (!themeCursors.contains(cursorName)) {
279 themeCursors[cursorName] = new XCursorImage(themeName, cursorName);
282 return themeCursors[cursorName];
286void CursorImageProvider::setCustomCursor(const QCursor &customCursor)
288 if (customCursor.pixmap().isNull()) {
289 m_customCursorImage.reset();
291 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