OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
IconManager.h
Go to the documentation of this file.
1
4// ###########################################################################################################################################################################################################################################################################################################################
5
6#pragma once
7
8// OpenTwin header
10#include "OTCore/Logger.h"
11
12// Qt header
13#include <QtCore/qstring.h>
14#include <QtCore/qbytearray.h>
15#include <QtCore/qstringlist.h>
16#include <QtGui/qicon.h>
17#include <QtGui/qmovie.h>
18#include <QtGui/qpixmap.h>
19
20// std header
21#include <map>
22#include <mutex>
23
24namespace ot {
25
33 public:
35 static IconManager& instance(void);
36
37 static bool addSearchPath(const QString& _path);
38
39 static const QStringList& searchPaths(void);
40
42 static QIcon& getIcon(const QString& _subPath);
43
45 static QPixmap& getPixmap(const QString& _subPath);
46
48 static QMovie& getMovie(const QString& _subPath);
49
51 static QByteArray& getSvgData(const QString& _subPath);
52
54 static void setApplicationIcon(const QIcon& _icon);
55
58 static const QIcon& getApplicationIcon(void);
59
60 private:
61 template <class T>
62 T& getOrCreate(const QString& _subPath, std::map<QString, T*>& _dataMap, T& _default);
63
64 QString findFullPath(const QString& _subPath);
65
67 virtual ~IconManager();
68
69 QStringList m_searchPaths;
70 std::mutex m_mutex;
71 std::map<QString, QIcon*> m_icons;
72 std::map<QString, QPixmap*> m_pixmaps;
73 std::map<QString, QMovie*> m_movies;
74 std::map<QString, QByteArray> m_svgData;
75 QIcon m_emptyIcon;
76 QPixmap m_emptyPixmap;
77 QMovie m_emptyMovie;
78 QByteArray m_emptySvgData;
79 QIcon m_applicationIcon;
80 };
81
82}
83
84template <class T>
85T& ot::IconManager::getOrCreate(const QString& _subPath, std::map<QString, T*>& _dataMap, T& _default) {
86 this->m_mutex.lock();
87
88 // Find existing
89 const auto& it = _dataMap.find(_subPath);
90 if (it != _dataMap.end()) {
91 this->m_mutex.unlock();
92 return *it->second;
93 }
94
95 // Find new
96 QString path = this->findFullPath(_subPath);
97 if (path.isEmpty()) {
98 this->m_mutex.unlock();
99 OT_LOG_EAS("Icon \"" + _subPath.toStdString() + "\" not found");
100 return _default;
101 }
102
103 // Create and store new
104 T* newEntry = new T(path);
105 _dataMap.insert_or_assign(_subPath, newEntry);
106
107 this->m_mutex.unlock();
108 return *newEntry;
109}
OpenTwin Logging system.
#define OT_LOG_EAS(___text)
Log a error message according to the service logger configuration and assert.
Definition Logger.h:191
#define OT_WIDGETS_API_EXPORT
Definition OTWidgetsAPIExport.h:12
The IconManager loads images or animations and caches them. The IconManager has a search path list....
Definition IconManager.h:32
Definition Connector.h:8