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  }
53 
57  QStringList getNames() {
58  return resMap.keys();
59  }
60 
65  T* get(const QString& resName) {
66  if (!RS::mapContainsCaseInsensitive(resMap, resName)) {
67  // resource not found, check substitution map:
68  if (RS::mapContainsCaseInsensitive(resSubstitutionMap, resName)) {
69  // substitution found:
70  QString sub = RS::mapValueCaseInsensitive(resSubstitutionMap, resName);
71  if (sub.compare(resName, Qt::CaseInsensitive)==0) {
72  qWarning() << "recursive resource substitution:" << resName << "->" << sub;
73  // cannot substitute font with itself (avoid recursion):
74  return NULL;
75  }
76  return get(sub);
77  }
78  return NULL;
79  }
80 
81  T* res = RS::mapValueCaseInsensitive(resMap, resName);
82  if (res==NULL) {
83  qWarning("RResourceList::get: list contains NULL resource.");
84  Q_ASSERT(false);
85  return NULL;
86  }
87 
88  if (!res->isLoaded()) {
89  res->load();
90  }
91 
92  return res;
93  }
94 
95 public:
96  QMap<QString, T*> resMap;
97  QMap<QString, QString> resSubstitutionMap;
98 };
99 
100 //template <class T> QMap<QString, T*> RResourceList<T>::resMap;
101 //template <class T> QMap<QString, QString> RResourceList<T>::resSubstitutionMap;
102 
103 #endif
QMap< QString, QString > resSubstitutionMap
Definition: RResourceList.h:97
void uninit()
Definition: RResourceList.h:43
static T mapValueCaseInsensitive(const QMap< QString, T > &map, const QString &key)
Definition: RS.h:539
#define QCADCORE_EXPORT
Definition: core_global.h:10
QStringList getNames()
Definition: RResourceList.h:57
QMap< QString, T * > resMap
Definition: RResourceList.h:96
static bool mapContainsCaseInsensitive(const QMap< QString, T > &map, const QString &key)
Definition: RS.h:534
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RResourceList.h:38