File indexing completed on 2024-05-12 16:02:26

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Michael Abrahams <miabraha@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_cursor_cache.h"
0008 
0009 #include <QScreen>
0010 #include <QWindow>
0011 #include <QBitmap>
0012 #include <QImage>
0013 #include <qmath.h>
0014 #include <QDebug>
0015 #include <QPainter>
0016 #include <QApplication>
0017 
0018 #include "KoResourcePaths.h"
0019 
0020 Q_GLOBAL_STATIC(KisCursorCache, s_instance)
0021 
0022 namespace {
0023 
0024     QCursor loadImpl(const QString &cursorName, int hotspotX, int hotspotY) {
0025         QPixmap cursorImage = QPixmap(":/" + cursorName);
0026         if (cursorImage.isNull()) {
0027             qWarning() << "Could not load cursor from qrc, trying filesystem" << cursorName;
0028             cursorImage = QPixmap(KoResourcePaths::findAsset("kis_pics", cursorName));
0029             if (cursorImage.isNull()) {
0030                 qWarning() << "Could not load cursor from filesystem" << cursorName;
0031                 return Qt::ArrowCursor;
0032             }
0033         }
0034 
0035         return QCursor(cursorImage, hotspotX, hotspotY);
0036     }
0037 
0038 }
0039 
0040 KisCursorCache::KisCursorCache() {}
0041 
0042 KisCursorCache* KisCursorCache::instance()
0043 {
0044     return s_instance;
0045 }
0046 
0047 QCursor KisCursorCache::load(const QString & cursorName, int hotspotX, int hotspotY)
0048 {
0049     if (cursorHash.contains(cursorName)) {
0050         return cursorHash[ cursorName ].second;
0051     }
0052 
0053     // Otherwise, construct the cursor
0054     QCursor newCursor = loadImpl(cursorName, hotspotX, hotspotY);
0055     cursorHash.insert(cursorName, QPair<QPoint, QCursor>(QPoint(hotspotX, hotspotY), newCursor));
0056     return newCursor;
0057 }