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