Unity 8
CursorImageProvider.cpp
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  */
16 
17 #include "CursorImageProvider.h"
18 
19 #include <QCursor>
20 #include <QDebug>
21 #include <QFile>
22 #include <QPainter>
23 #include <QSvgRenderer>
24 
25 CursorImageProvider *CursorImageProvider::m_instance = nullptr;
26 
28 // BuiltInCursorImage
29 
30 BuiltInCursorImage::BuiltInCursorImage()
31 {
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>";
44 
45  qimage = QImage(20, 32, QImage::Format_ARGB32);
46  qimage.fill(Qt::transparent);
47  QPainter imagePainter(&qimage);
48 
49  QSvgRenderer *svgRenderer = new QSvgRenderer(QByteArray(svgString));
50  svgRenderer->render(&imagePainter);
51  delete svgRenderer;
52 }
53 
55 // BlankCursorImage
56 
57 
58 BlankCursorImage::BlankCursorImage()
59 {
60  qimage = QImage(1, 1, QImage::Format_ARGB32);
61  qimage.fill(Qt::transparent);
62 }
63 
65 // CustomCursorImage
66 
67 
68 CustomCursorImage::CustomCursorImage(const QCursor &cursor)
69 {
70  qimage = cursor.pixmap().toImage();
71  hotspot = cursor.hotSpot();
72 }
73 
75 // XCursorImage
76 
77 XCursorImage::XCursorImage(const QString &theme, const QString &file)
78  : xcursorImages(nullptr)
79 {
80  // TODO: Consider grid unit value
81  // Hardcoding to a medium size for now
82  int preferredCursorHeightPx = 32;
83 
84  xcursorImages = XcursorLibraryLoadImages(QFile::encodeName(file), QFile::encodeName(theme),
85  preferredCursorHeightPx);
86  if (!xcursorImages) {
87  return;
88  }
89 
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];
94 
95  qimage = QImage((uchar*)xcursorImage->pixels,
96  xcursorImage->width, xcursorImage->height, QImage::Format_ARGB32);
97 
98  hotspot.setX(xcursorImage->xhot);
99  hotspot.setY(xcursorImage->yhot);
100  }
101 }
102 
103 XCursorImage::~XCursorImage()
104 {
105  XcursorImagesDestroy(xcursorImages);
106 }
107 
109 // CursorImageProvider
110 
111 CursorImageProvider::CursorImageProvider()
112  : QQuickImageProvider(QQuickImageProvider::Image)
113 {
114  if (m_instance) {
115  qFatal("Cannot have multiple CursorImageProvider instances");
116  }
117  m_instance = this;
118 
119  m_fallbackNames[QStringLiteral("closedhand")].append(QStringLiteral("grabbing"));
120  m_fallbackNames[QStringLiteral("closedhand")].append(QStringLiteral("dnd-none"));
121 
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"));
125 
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"));
129 
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"));
133 
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"));
137 
138  m_fallbackNames[QStringLiteral("hand")].append(QStringLiteral("pointing_hand"));
139  m_fallbackNames[QStringLiteral("hand")].append(QStringLiteral("pointer"));
140 
141  m_fallbackNames[QStringLiteral("ibeam")].append(QStringLiteral("xterm"));
142  m_fallbackNames[QStringLiteral("ibeam")].append(QStringLiteral("text"));
143 
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"));
147 
148  m_fallbackNames[QStringLiteral("left_ptr_watch")].append(QStringLiteral("half-busy"));
149  m_fallbackNames[QStringLiteral("left_ptr_watch")].append(QStringLiteral("progress"));
150 
151  m_fallbackNames[QStringLiteral("size_bdiag")].append(QStringLiteral("fd_double_arrow"));
152  m_fallbackNames[QStringLiteral("size_bdiag")].append(QStringLiteral("nesw-resize"));
153 
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"));
156 
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"));
160 
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"));
164 
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"));
167 
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"));
170 
171  m_fallbackNames[QStringLiteral("up_arrow")].append(QStringLiteral("sb_up_arrow")); // DMZ-White and DMZ-Black themes
172 
173  m_fallbackNames[QStringLiteral("watch")].append(QStringLiteral("wait"));
174 
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"));
178 
179  m_fallbackNames[QStringLiteral("xterm")].append(QStringLiteral("ibeam"));
180 }
181 
182 CursorImageProvider::~CursorImageProvider()
183 {
184  {
185  QList< QMap<QString, CursorImage*> > cursorList = m_cursors.values();
186 
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  }
194 
195  m_cursors.clear();
196  m_instance = nullptr;
197 }
198 
199 QImage CursorImageProvider::requestImage(const QString &cursorThemeAndName, QSize *size, const QSize & /*requestedSize*/)
200 {
201  CursorImage *cursorImage = fetchCursor(cursorThemeAndName);
202  size->setWidth(cursorImage->qimage.width());
203  size->setHeight(cursorImage->qimage.height());
204 
205  return cursorImage->qimage;
206 }
207 
208 QPoint CursorImageProvider::hotspot(const QString &themeName, const QString &cursorName)
209 {
210  CursorImage *cursorImage = fetchCursor(themeName, cursorName);
211  if (cursorImage) {
212  return cursorImage->hotspot;
213  } else {
214  return QPoint(0,0);
215  }
216 }
217 
218 CursorImage *CursorImageProvider::fetchCursor(const QString &cursorThemeAndName)
219 {
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  }
230 
231  return fetchCursor(themeName, cursorName);
232 }
233 
234 CursorImage *CursorImageProvider::fetchCursor(const QString &themeName, const QString &cursorName)
235 {
236  CursorImage *cursorImage = fetchCursorHelper(themeName, cursorName);
237 
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  }
250 
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  }
257 
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  }
266 
267  return cursorImage;
268 }
269 
270 CursorImage *CursorImageProvider::fetchCursorHelper(const QString &themeName, const QString &cursorName)
271 {
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];
278 
279  if (!themeCursors.contains(cursorName)) {
280  themeCursors[cursorName] = new XCursorImage(themeName, cursorName);
281  }
282 
283  return themeCursors[cursorName];
284  }
285 }
286 
287 void CursorImageProvider::setCustomCursor(const QCursor &customCursor)
288 {
289  if (customCursor.pixmap().isNull()) {
290  m_customCursorImage.reset();
291  } else {
292  m_customCursorImage.reset(new CustomCursorImage(customCursor));
293  }
294 }