ThumbnailManager.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /******************************************************************************
  2. Copyright (C) 2025 by Taylor Giampaolo <[email protected]>
  3. Lain Bailey <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #pragma once
  16. #include <obs.hpp>
  17. #include <QObject>
  18. #include <QPixmap>
  19. #include <QPointer>
  20. #include <QTimer>
  21. #include <deque>
  22. #include <functional>
  23. class ThumbnailItem : public QObject {
  24. Q_OBJECT
  25. friend class ThumbnailManager;
  26. friend class Thumbnail;
  27. std::string uuid;
  28. OBSWeakSource weakSource;
  29. QPixmap pixmap;
  30. void init(std::weak_ptr<ThumbnailItem> weakActiveItem);
  31. void imageUpdated(QImage image);
  32. public:
  33. ThumbnailItem(std::string uuid, OBSSource source);
  34. ~ThumbnailItem();
  35. inline bool isNull() const { return !weakSource || obs_weak_source_expired(weakSource); }
  36. inline const std::string &getUuid() const { return uuid; }
  37. signals:
  38. void updateThumbnail(QPixmap pixmap);
  39. };
  40. class Thumbnail : public QObject {
  41. Q_OBJECT
  42. friend class ThumbnailManager;
  43. std::shared_ptr<ThumbnailItem> item;
  44. private slots:
  45. void thumbnailUpdated(QPixmap pixmap);
  46. public:
  47. inline Thumbnail(std::shared_ptr<ThumbnailItem> item) : item(item) {}
  48. inline QPixmap getPixmap() const { return item->pixmap; }
  49. static constexpr QSize size = {320, 180};
  50. signals:
  51. void updateThumbnail(QPixmap pixmap);
  52. };
  53. class ThumbnailManager : public QObject {
  54. Q_OBJECT
  55. friend class ThumbnailItem;
  56. struct CachedItem {
  57. std::optional<QPixmap> pixmap;
  58. std::weak_ptr<ThumbnailItem> weakActiveItem;
  59. };
  60. std::deque<std::weak_ptr<ThumbnailItem>> newThumbnails;
  61. std::deque<std::weak_ptr<ThumbnailItem>> thumbnails;
  62. std::unordered_map<std::string, CachedItem> cachedThumbnails;
  63. QTimer updateTimer;
  64. bool updatePixmap(std::shared_ptr<ThumbnailItem> &item);
  65. void updateTick();
  66. void updateIntervalChanged(size_t newCount);
  67. public:
  68. explicit ThumbnailManager(QObject *parent = nullptr);
  69. ~ThumbnailManager();
  70. std::shared_ptr<Thumbnail> getThumbnail(OBSSource source);
  71. std::optional<QPixmap> getCachedThumbnail(OBSSource source);
  72. void preloadThumbnail(OBSSource source, QObject *object, std::function<void(QPixmap)> callback);
  73. private:
  74. Q_DISABLE_COPY_MOVE(ThumbnailManager);
  75. };