| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- /******************************************************************************
- Copyright (C) 2025 by Taylor Giampaolo <[email protected]>
- Lain Bailey <[email protected]>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- ******************************************************************************/
- #pragma once
- #include <obs.hpp>
- #include <QObject>
- #include <QPixmap>
- #include <QPointer>
- #include <QTimer>
- #include <deque>
- #include <functional>
- class ThumbnailItem : public QObject {
- Q_OBJECT
- friend class ThumbnailManager;
- friend class Thumbnail;
- std::string uuid;
- OBSWeakSource weakSource;
- QPixmap pixmap;
- void init(std::weak_ptr<ThumbnailItem> weakActiveItem);
- void imageUpdated(QImage image);
- public:
- ThumbnailItem(std::string uuid, OBSSource source);
- ~ThumbnailItem();
- inline bool isNull() const { return !weakSource || obs_weak_source_expired(weakSource); }
- inline const std::string &getUuid() const { return uuid; }
- signals:
- void updateThumbnail(QPixmap pixmap);
- };
- class Thumbnail : public QObject {
- Q_OBJECT
- friend class ThumbnailManager;
- std::shared_ptr<ThumbnailItem> item;
- private slots:
- void thumbnailUpdated(QPixmap pixmap);
- public:
- inline Thumbnail(std::shared_ptr<ThumbnailItem> item) : item(item) {}
- inline QPixmap getPixmap() const { return item->pixmap; }
- static constexpr QSize size = {320, 180};
- signals:
- void updateThumbnail(QPixmap pixmap);
- };
- class ThumbnailManager : public QObject {
- Q_OBJECT
- friend class ThumbnailItem;
- struct CachedItem {
- std::optional<QPixmap> pixmap;
- std::weak_ptr<ThumbnailItem> weakActiveItem;
- };
- std::deque<std::weak_ptr<ThumbnailItem>> newThumbnails;
- std::deque<std::weak_ptr<ThumbnailItem>> thumbnails;
- std::unordered_map<std::string, CachedItem> cachedThumbnails;
- QTimer updateTimer;
- bool updatePixmap(std::shared_ptr<ThumbnailItem> &item);
- void updateTick();
- void updateIntervalChanged(size_t newCount);
- public:
- explicit ThumbnailManager(QObject *parent = nullptr);
- ~ThumbnailManager();
- std::shared_ptr<Thumbnail> getThumbnail(OBSSource source);
- std::optional<QPixmap> getCachedThumbnail(OBSSource source);
- void preloadThumbnail(OBSSource source, QObject *object, std::function<void(QPixmap)> callback);
- private:
- Q_DISABLE_COPY_MOVE(ThumbnailManager);
- };
|