Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_cursor_cache.cpp
Go to the documentation of this file.
1
2/*
3 * SPDX-FileCopyrightText: 2016 Michael Abrahams <miabraha@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#include "kis_cursor_cache.h"
9
10#include <QScreen>
11#include <QWindow>
12#include <QBitmap>
13#include <qmath.h>
14#include <QDebug>
15#include <QPainter>
16#include <QApplication>
17
18#include "KoResourcePaths.h"
19
21
22namespace {
23
24 QCursor loadImpl(const QString &cursorName, int hotspotX, int hotspotY, int width, int height) {
25#ifdef Q_OS_ANDROID
26 // On Android, large cursors render glitchy, so we avoid scaling the cursors till we find a solution to that.
27 QPixmap cursorImage = QPixmap(":/" + cursorName);
28#else
29 QPixmap cursorImage = QIcon(":/" + cursorName).pixmap(width > -1? width: 32, height > -1? height: 32);
30#endif
31
32 if (cursorImage.isNull()) {
33 qWarning() << "Could not load cursor from qrc, trying filesystem" << cursorName;
34 cursorImage = QPixmap(KoResourcePaths::findAsset("kis_pics", cursorName));
35 if (cursorImage.isNull()) {
36 qWarning() << "Could not load cursor from filesystem" << cursorName;
37 return Qt::ArrowCursor;
38 }
39 }
40
41 int hX = hotspotX;
42 int hY = hotspotY;
43#ifdef Q_OS_LINUX
44 if (qEnvironmentVariable("QT_QPA_PLATFORM") == "xcb") {
45 hX = (hotspotX >= 0? hotspotX * cursorImage.devicePixelRatio(): (cursorImage.width() /2));
46 hY = (hotspotY >= 0? hotspotY * cursorImage.devicePixelRatio(): (cursorImage.height()/2));
47 }
48#endif
49
50 return QCursor(cursorImage, hX, hY);
51 }
52
53}
54
56
58{
59 return s_instance;
60}
61
62QCursor KisCursorCache::load(const QString & cursorName, int width, int height, int hotspotX, int hotspotY)
63{
64 if (cursorHash.contains(cursorName)) {
65 return cursorHash[ cursorName ].second;
66 }
67
68 // Otherwise, construct the cursor
69 QCursor newCursor = loadImpl(cursorName, hotspotX, hotspotY, width, height);
70 cursorHash.insert(cursorName, QPair<QPoint, QCursor>(QPoint(hotspotX, hotspotY), newCursor));
71 return newCursor;
72}
Q_GLOBAL_STATIC(KisStoragePluginRegistry, s_instance)
QCursor load(const QString &cursorName, int width, int height, int hotspotX, int hotspotY)
static KisCursorCache * instance()
QHash< QString, QPair< QPoint, QCursor > > cursorHash
static QString findAsset(const QString &type, const QString &fileName)