Unity 8
CursorImageProvider.h
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 #ifndef CURSORIMAGEPROVIDER_H
18 #define CURSORIMAGEPROVIDER_H
19 
20 #include <QQuickImageProvider>
21 #include <QScopedPointer>
22 
23 // xcursor static lib
24 extern "C"
25 {
26 #include <xcursor.h>
27 }
28 
29 class CursorImage {
30 public:
31  virtual ~CursorImage() {}
32 
33  QImage qimage;
34 
35  // TODO: consider if there's a need to animate the hotspot
36  // ie, if there's a need to make it an array of points, one for each frame.
37  // Maybe no single xcursor (or at least the ones we know of or use)
38  // vary its hotspot position through its animation.
39  QPoint hotspot;
40 
41  int frameWidth{0};
42  int frameHeight{0};
43  int frameCount{1};
44  int frameDuration{40};
45 };
46 
47 class XCursorImage : public CursorImage {
48 public:
49  XCursorImage(const QString &theme, const QString &file);
50  virtual ~XCursorImage();
51 };
52 
53 class BuiltInCursorImage : public CursorImage {
54 public:
55  BuiltInCursorImage();
56 };
57 
58 class BlankCursorImage : public CursorImage {
59 public:
60  BlankCursorImage();
61 };
62 
63 class CustomCursorImage : public CursorImage {
64 public:
65  CustomCursorImage(const QCursor &cursor);
66 };
67 
68 class CursorImageProvider : public QQuickImageProvider
69 {
70 public:
71  CursorImageProvider();
72  virtual ~CursorImageProvider();
73 
74  static CursorImageProvider *instance() { return m_instance; }
75 
76 
77  QImage requestImage(const QString &cursorName, QSize *size, const QSize &requestedSize) override;
78 
79  CursorImage *fetchCursor(const QString &themeName, const QString &cursorName);
80 
81  void setCustomCursor(const QCursor &customCursor);
82 
83 private:
84  CursorImage *fetchCursor(const QString &cursorThemeAndName);
85  CursorImage *fetchCursorHelper(const QString &themeName, const QString &cursorName);
86 
87  // themeName -> (cursorName -> cursorImage)
88  // TODO: discard old, unused, cursors
89  QMap<QString, QMap<QString, CursorImage*> > m_cursors;
90 
91  QScopedPointer<CursorImage> m_builtInCursorImage;
92  BlankCursorImage m_blankCursorImage;
93  QScopedPointer<CursorImage> m_customCursorImage;
94 
95  QMap<QString, QStringList> m_fallbackNames;
96 
97  static CursorImageProvider *m_instance;
98 };
99 
100 #endif // CURSORIMAGEPROVIDER_H