QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
RResourceList.h
Go to the documentation of this file.
1
20#ifndef RRESOURCELIST_H
21#define RRESOURCELIST_H
22
23#include "core_global.h"
24
25#include <QDebug>
26#include <QString>
27#include <QMap>
28
29#include "RS.h"
30
36template <class T>
38public:
39// void init() {
40// }
41
42 void uninit() {
43 QMapIterator<QString, T*> it(resMap);
44
45 while (it.hasNext()) {
46 it.next();
47 delete it.value();
48 }
49
50 resMap.clear();
51 resSubstitutionMap.clear();
52 }
53
57 QStringList getNames() {
58 return resMap.keys();
59 }
60
61 QString getSubName(const QString& resName, int rec=0) {
62 // check substitution map first:
63 if (RS::mapContainsCaseInsensitive(resSubstitutionMap, resName)) {
64 // substitution found:
65 QString subName = RS::mapValueCaseInsensitive(resSubstitutionMap, resName);
66 if (subName.compare(resName, Qt::CaseInsensitive)==0 || rec>16) {
67 qWarning() << "recursive resource substitution:" << resName << "->" << subName;
68 // cannot substitute font with itself (avoid recursion):
69 return QString();
70 }
71 return getSubName(subName, ++rec);
72 }
73 return resName;
74 }
75
80 T* get(const QString& resName, bool substitute = true) {
81 QString resNameSub = resName;
82 if (substitute) {
83 resNameSub = getSubName(resName);
84 }
85
86 // check if resource is available:
87 if (!RS::mapContainsCaseInsensitive(resMap, resNameSub)) {
88 return NULL;
89 }
90
91 T* res = RS::mapValueCaseInsensitive(resMap, resNameSub);
92 if (res==NULL) {
93 qWarning("RResourceList::get: list contains NULL resource.");
94 Q_ASSERT(false);
95 return NULL;
96 }
97
98 if (!res->isLoaded()) {
99 res->load();
100 }
101
102 return res;
103 }
104
105public:
108};
109
110//template <class T> QMap<QString, T*> RResourceList<T>::resMap;
111//template <class T> QMap<QString, QString> RResourceList<T>::resSubstitutionMap;
112
113#endif
Definition RDebug.h:38
Copyright (c) 2011-2018 by Andrew Mustun.
Definition RResourceList.h:37
QString getSubName(const QString &resName, int rec=0)
Definition RResourceList.h:61
QStringList getNames()
Definition RResourceList.h:57
T * get(const QString &resName, bool substitute=true)
Definition RResourceList.h:80
QMap< QString, QString > resSubstitutionMap
Definition RResourceList.h:107
void uninit()
Definition RResourceList.h:42
QMap< QString, T * > resMap
Definition RResourceList.h:106
static T mapValueCaseInsensitive(const QMap< QString, T > &map, const QString &key)
Definition RS.h:801
static bool mapContainsCaseInsensitive(const QMap< QString, T > &map, const QString &key)
Definition RS.h:796
#define QCADCORE_EXPORT
Definition core_global.h:10
#define NULL
Definition opennurbs_system.h:256