NDEVR
API Documentation
QTIconManager.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Widgets
28File: QTIconManager
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/SoftwareService.h>
35#include <NDEVR/Buffer.h>
36#include <NDEVR/String.h>
37#include <NDEVR/File.h>
38#include <NDEVR/Dictionary.h>
39#include <NDEVR/RGBColor.h>
40#include <NDEVR/LABColor.h>
41#include <NDEVR/Resource.h>
42#include <NDEVR/TranslatedString.h>
43#include <NDEVR/UUID.h>
44#include <NDEVR/Pointer.h>
45#include <qiconengine.h>
46#include <QTransform>
47#include <QIcon>
48#include <QPointer>
49class QImage;
50class QPixmap;
51class QIcon;
52class QWidget;
53namespace NDEVR
54{
55 class Log;
56 class HSBColor;
66
69 struct NDEVR_WIDGETS_API IconThemeSettings
70 {
71 IconThemeSettings();
78 void setup(const TranslatedString& name, const StringView& theme_icon, const StringView& theme_icon_location);
89 UUID id = Constant<UUID>::Invalid;
98 bool operator==(const IconThemeSettings& settings) const;
99 bool operator!=(const IconThemeSettings& settings) const;
100 private:
101 mutable DynamicPointer<IconThemeDisplaySettings> m_theme_display_settings;
102 };
103
109 {
110 public:
116 : m_id(id)
117 {}
118
125 [[nodiscard]] virtual QIcon createIcon(const StringView& icon, const IconThemeSettings& theme, QIcon::Mode mode = QIcon::Mode::Normal) = 0;
133 [[nodiscard]] virtual bool canProcess(const StringView& icon, const IconThemeSettings& theme, QIcon::Mode mode) const = 0;
138 [[nodiscard]] UUID id() const { return m_id; }
139
140 private:
141 UUID m_id;
142 };
143
146 class NDEVR_WIDGETS_API DeferredImageIconEngine : public QIconEngine
147 {
148 public:
154 DeferredImageIconEngine(const StringView& icon, QIcon::Mode mode = QIcon::Mode::Normal);
165 void setIcon(const StringView& icon);
173 virtual void paint(QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state) override;
181 virtual QPixmap pixmap(const QSize& size, QIcon::Mode mode, QIcon::State state) override;
186 virtual QIconEngine* clone(void) const override;
194 QSize actualSize(const QSize& size, QIcon::Mode mode, QIcon::State state) override;
195 public:
198 QIcon::Mode m_override_mode;
199 mutable QSize m_default_size;
200 };
201
207 {
208 public:
214 virtual bool canPotentiallyResolve(const StringView& pixmap_resource) = 0;
223 virtual QPixmap resolve(const StringView& pixmap_resource, QIcon::Mode mode, const QSize& requested_size, Dictionary<UUID, QPixmap>& pixmaps) = 0;
231 virtual bool requestSet(const StringView& pixmap_resource, QPixmap map, QIcon::Mode mode) = 0;
236 virtual UUID id() const = 0;
237 };
238
242 class NDEVR_WIDGETS_API QTIconManager
243 {
244 friend class NDEVRSVGIconEngine;
245 friend class DeferredImageIconEngine;
246 public:
251 static void SetLightingTheme(const IconThemeSettings& theme);
263 static void GetIconResource(const std::function<void(const StringView&, const QIcon&)>& on_finished, const StringView& icon, QIcon::Mode mode = QIcon::Mode::Normal);
271 static void GetIconResource(const std::function<void(const StringView&, const QIcon&)>& on_finished, const StringView& icon, const IconThemeSettings& theme, QIcon::Mode mode = QIcon::Mode::Normal);
279 static void GetPixmapResource(const std::function<void(const StringView&, const QPixmap&)>& on_finished, const StringView& pixmap, QIcon::Mode mode = QIcon::Mode::Normal, const QSize& requested_size = QSize());
286 static QIcon GetIconResourceBlocking(const StringView& icon, QIcon::Mode mode = QIcon::Mode::Normal);
294 static QIcon GetIconResourceBlocking(const StringView& icon, const IconThemeSettings& theme, QIcon::Mode mode = QIcon::Mode::Normal);
302 static QPixmap LocalPixmapResource(const StringView& pixmap, QIcon::Mode mode = QIcon::Mode::Normal, const QSize& requested_size = QSize());
310 static bool RequestSet(const StringView& pixmap_resource, QPixmap map, QIcon::Mode mode = QIcon::Mode::Normal);
315 static void PostIconResourceLoad(const StringView& id);
321 static void SetWindowIcon(const StringView& s, QWidget* widget);
326 static void ClearWindowIcon(QWidget* widget);
332 static Buffer<std::pair<LABColor, LABColor>> ConvertColorPalette(const Buffer<std::pair<RGBColor, RGBColor>>& palette);
342 static void ConvertColorTheme(const Buffer<std::pair<RGBColor, RGBColor>>& colors, const Buffer<File>& in, const File& out_location, bool preserve_brightness, bool preserve_alpha, Log* log = nullptr);
352 static void ConvertColorTheme(const Buffer<std::pair<RGBColor, RGBColor>>& colors, const File& in_location, const File& out_location, bool preserve_brightness, bool preserve_alpha, Log* log = nullptr);
360 static void ConvertColorTheme(const Buffer<std::pair<LABColor, LABColor>>& color, QImage& image, bool preserve_brightness, bool preserve_alpha);
368 static QImage AutoCrop(const QImage& pixmap, int margin = 0, bool preserve_ratio = false);
374 static const QPixmap& disabledPixmap(const StringView& icon_name);
381 static QImage FillBackground(const QImage& pixmap, RGBColor color);
387 static QImage CleanupImageForIcon(const QImage& image);
397 static void addIconTheme(const IconThemeSettings& theme);
419 static constexpr const char* NDEVRThemeLocation() { return "NDEVR Icons/"; }
424 static void AddIconEngine(NDEVRIconEngine* engine);
431 static String GetSVGPath(const StringView& icon, const IconThemeSettings& theme);
449 static void AddResolver(PixmapResolver* resolver);
450 protected:
463 static QIcon icon(const StringView& icon, QIcon::Mode mode = QIcon::Mode::Normal);
471 static QIcon icon(const StringView& icon, const IconThemeSettings& theme, QIcon::Mode mode = QIcon::Mode::Normal);
479 static QPixmap pixmap(const StringView& pixmap, QIcon::Mode mode = QIcon::Mode::Normal, const QSize& requested_size = QSize());
480 private:
481 static Dictionary<UUID, Buffer<std::function<void(const StringView&, const QIcon&)>*>> s_download_waiters;
482 static Dictionary<UUID, PixmapResolver*> s_resolvers;
483 static Dictionary<QWidget*, String> s_window_registered;
484 static Dictionary<UUID, QIcon> s_icon_dictionary;
485 static Dictionary<UUID, QPixmap> s_pixmap_dictionary;
486 static Dictionary<UUID, QPixmap> s_disabled_pixmap_dictionary;
487 static Dictionary<UUID, NDEVRIconEngine*> s_icon_engines;
488 static Buffer<IconThemeSettings> s_icon_themes;
489 };
490 NDEVR_WIDGETS_T_API template class NDEVR_WIDGETS_API StringStream<IconThemeSettings>;
491}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
QPixmap m_cached_pixmap
Cached pixmap to avoid redundant rendering.
virtual QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override
Returns a pixmap for the icon at the specified size, mode, and state.
void setIcon(const StringView &icon)
Changes the icon resource identifier.
virtual QIconEngine * clone(void) const override
Creates a copy of this icon engine.
virtual void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) override
Paints the icon into the given rectangle using the specified painter.
String m_icon
The icon resource identifier.
DeferredImageIconEngine(const StringView &icon, QIcon::Mode mode=QIcon::Mode::Normal)
Constructs a deferred icon engine that delays resource loading until paint time.
QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) override
Returns the actual rendered size for the given constraints.
void setIconTheme(const IconThemeSettings theme)
Sets the icon theme to use for rendering.
QIcon::Mode m_override_mode
The icon mode override for rendering.
QSize m_default_size
The default size determined from the icon resource.
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
Provides a modifiable pointer that has shared ownership of a dynamically allocated object.
Definition Pointer.hpp:356
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
A color defined by hue, saturation, and brightness components.
Definition HSBColor.h:62
Serves as the primary program interface for processes to report issues and allows any number of LogSt...
Definition Log.h:50
Used to render any icons or fetch any pixmaps within the software.
virtual bool canProcess(const StringView &icon, const IconThemeSettings &theme, QIcon::Mode mode) const =0
Checks whether this engine can render the specified icon.
virtual QIcon createIcon(const StringView &icon, const IconThemeSettings &theme, QIcon::Mode mode=QIcon::Mode::Normal)=0
Creates a QIcon for the specified icon resource and theme.
UUID id() const
Returns the unique identifier for this icon engine.
NDEVRIconEngine(const UUID &id)
Constructs an icon engine with the given unique identifier.
Base class for logic that can be used to resolve a Pixmap resource and turn it into a QPixmap for use...
virtual UUID id() const =0
Returns the unique identifier for this resolver.
virtual QPixmap resolve(const StringView &pixmap_resource, QIcon::Mode mode, const QSize &requested_size, Dictionary< UUID, QPixmap > &pixmaps)=0
Resolves a pixmap resource identifier into a QPixmap.
virtual bool requestSet(const StringView &pixmap_resource, QPixmap map, QIcon::Mode mode)=0
Requests that a pixmap be stored for the given resource identifier.
virtual bool canPotentiallyResolve(const StringView &pixmap_resource)=0
Checks whether this resolver can potentially resolve the given pixmap resource.
Manages all Icons and Pixmaps used by the UI in the NDEVR Engine.
static const QPixmap & disabledPixmap(const StringView &icon_name)
Returns a cached disabled (grayed-out) version of the given icon.
static void ConvertColorTheme(const Buffer< std::pair< LABColor, LABColor > > &color, QImage &image, bool preserve_brightness, bool preserve_alpha)
Applies a LAB color theme conversion directly to a QImage.
static QImage AutoCrop(const QImage &pixmap, int margin=0, bool preserve_ratio=false)
Removes transparent borders from an image, optionally adding a margin.
static void SetLightingTheme(const IconThemeSettings &theme)
Sets the user's icon theme.
static void ConvertColorTheme(const Buffer< std::pair< RGBColor, RGBColor > > &colors, const Buffer< File > &in, const File &out_location, bool preserve_brightness, bool preserve_alpha, Log *log=nullptr)
Converts a set of icon files from one color theme to another.
static Resource< IconThemeSettings > s_icon_theme
Resource holding the user's selected icon theme.
static QImage FillBackground(const QImage &pixmap, RGBColor color)
Fills the transparent background of an image with the specified color.
static QIcon icon(const StringView &icon, const IconThemeSettings &theme, QIcon::Mode mode=QIcon::Mode::Normal)
Resolves an icon using a specific theme.
static void ConvertColorTheme(const Buffer< std::pair< RGBColor, RGBColor > > &colors, const File &in_location, const File &out_location, bool preserve_brightness, bool preserve_alpha, Log *log=nullptr)
Converts icon files at a given location from one color theme to another.
static String ImageCacheLocation(const StringView &icon)
Returns the cache file path for a rendered image.
static void AddResolver(PixmapResolver *resolver)
Registers a pixmap resolver for resolving custom pixmap resources.
static QIcon GetIconResourceBlocking(const StringView &icon, const IconThemeSettings &theme, QIcon::Mode mode=QIcon::Mode::Normal)
Synchronously fetches an icon resource using a specific theme.
static void SetOverrideTheme(IconThemeSettings *theme)
Sets a temporary override theme that takes precedence over the user's saved theme.
static void GetIconResource(const std::function< void(const StringView &, const QIcon &)> &on_finished, const StringView &icon, const IconThemeSettings &theme, QIcon::Mode mode=QIcon::Mode::Normal)
Asynchronously fetches an icon resource using a specific theme, invoking the callback when ready.
static Resource< IconThemeSettings * > s_overide_icon_theme
Resource holding the override icon theme pointer.
static QPixmap LocalPixmapResource(const StringView &pixmap, QIcon::Mode mode=QIcon::Mode::Normal, const QSize &requested_size=QSize())
Synchronously resolves a local pixmap resource.
static void SetWindowIcon(const StringView &s, QWidget *widget)
Sets the window icon for a widget to the specified icon resource.
static const IconThemeSettings & EffectiveIconTheme()
Returns the currently effective icon theme, accounting for any overrides.
static QPixmap pixmap(const StringView &pixmap, QIcon::Mode mode=QIcon::Mode::Normal, const QSize &requested_size=QSize())
Resolves a pixmap resource.
static void addIconTheme(const IconThemeSettings &theme)
Registers a new icon theme.
static const IconThemeSettings & NDEVRIconTheme()
Returns the default NDEVR icon theme.
static String GetSVGPath(const StringView &icon, const IconThemeSettings &theme)
Returns the file path to an SVG icon resource for the given theme.
static constexpr const char * NDEVRThemeLocation()
Returns the file path prefix for the default NDEVR icon theme resources.
static String IconCacheLocation(const StringView &icon, const IconThemeSettings &theme)
Returns the cache file path for a rendered icon.
static void ClearWindowIcon(QWidget *widget)
Clears the window icon previously set on a widget.
static QIcon icon(const StringView &icon, QIcon::Mode mode=QIcon::Mode::Normal)
Resolves an icon using the current theme.
static bool RequestSet(const StringView &pixmap_resource, QPixmap map, QIcon::Mode mode=QIcon::Mode::Normal)
Stores a pixmap for the given resource identifier in available resolvers.
static void GetPixmapResource(const std::function< void(const StringView &, const QPixmap &)> &on_finished, const StringView &pixmap, QIcon::Mode mode=QIcon::Mode::Normal, const QSize &requested_size=QSize())
Asynchronously fetches a pixmap resource, invoking the callback when ready.
static void PostIconResourceLoad(const StringView &id)
Notifies any pending callbacks that an icon resource has finished loading.
static void GetIconResource(const std::function< void(const StringView &, const QIcon &)> &on_finished, const StringView &icon, QIcon::Mode mode=QIcon::Mode::Normal)
Asynchronously fetches an icon resource using the current theme, invoking the callback when ready.
static QIcon GetIconResourceBlocking(const StringView &icon, QIcon::Mode mode=QIcon::Mode::Normal)
Synchronously fetches an icon resource using the current theme.
static const Buffer< IconThemeSettings > & iconThemes()
Returns all registered icon themes.
static File GetImagePath(const StringView &icon)
Returns the file path for an image resource.
static Buffer< std::pair< LABColor, LABColor > > ConvertColorPalette(const Buffer< std::pair< RGBColor, RGBColor > > &palette)
Converts an RGB color palette to LAB color space for perceptual color operations.
static const IconThemeSettings & OutlineIconTheme()
Returns the built-in outline icon theme.
static void AddIconEngine(NDEVRIconEngine *engine)
Registers an icon rendering engine.
static QImage CleanupImageForIcon(const QImage &image)
Cleans up an image to make it suitable for use as an icon.
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:57
A core part of the engine, stores variables that can be listened to with ResourceListener which will ...
Definition Resource.h:42
Software Service Managers take a Software service to modify the behavior of the software.
Base interface for services that extend or modify software behavior through modules.
Logic for reading or writing to a string or a user friendly, TranslatedString.
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer s...
Definition UUID.h:61
The primary namespace for the NDEVR SDK.
double fltp08
Defines an alias representing an 8 byte floating-point number.
@ icon
Icon identifier for the object.
Stores display information for how to draw icons, when supported.
String theme_icon_location
The file or resource path where the theme icon is located.
TranslatedString theme_name
The user-facing translated name of the icon theme.
String theme_icon
The icon resource identifier for the theme preview.
Stores information for how to draw icons, when supported.
void setup(const TranslatedString &name, const StringView &theme_icon, const StringView &theme_icon_location)
Configures the display settings for this icon theme.
IconThemeDisplaySettings & displaySettings()
Returns a mutable reference to the display settings for this theme.
fltp08 pixel_size
Default pixel size for rendered icons.
RGBColor primary_color
Primary color applied to icon rendering.
bool blend_color
Whether to blend theme colors when colorizing icons.
RGBColor accent_color
Accent color for icon highlights and emphasis.
RGBColor outline_color
Color used for icon outlines and borders.
RGBColor highlight_color
Color used for highlighted or selected icon elements.
RGBColor secondary_color
Secondary color applied to icon rendering.
bool colorize_svgs
Whether to apply theme colors to SVG icons.
const IconThemeDisplaySettings & displaySettings() const
Returns a const reference to the display settings for this theme.