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, int rec=0) {
82  QString resNameSub = getSubName(resName);
83 
84  // check if resource is available:
85  if (!RS::mapContainsCaseInsensitive(resMap, resNameSub)) {
86  return NULL;
87  }
88 
89  T* res = RS::mapValueCaseInsensitive(resMap, resNameSub);
90  if (res==NULL) {
91  qWarning("RResourceList::get: list contains NULL resource.");
92  Q_ASSERT(false);
93  return NULL;
94  }
95 
96  if (!res->isLoaded()) {
97  res->load();
98  }
99 
100  return res;
101  }
102 
103 public:
104  QMap<QString, T*> resMap;
105  QMap<QString, QString> resSubstitutionMap;
106 };
107 
108 //template <class T> QMap<QString, T*> RResourceList<T>::resMap;
109 //template <class T> QMap<QString, QString> RResourceList<T>::resSubstitutionMap;
110 
111 #endif
QMap< QString, QString > resSubstitutionMap
Definition: RResourceList.h:105
void uninit()
Definition: RResourceList.h:43
static T mapValueCaseInsensitive(const QMap< QString, T > &map, const QString &key)
Definition: RS.h:562
#define QCADCORE_EXPORT
Definition: core_global.h:10
QStringList getNames()
Definition: RResourceList.h:58
QString getSubName(const QString &resName, int rec=0)
Definition: RResourceList.h:62
QMap< QString, T * > resMap
Definition: RResourceList.h:104
static bool mapContainsCaseInsensitive(const QMap< QString, T > &map, const QString &key)
Definition: RS.h:557
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RResourceList.h:38