Diana Software
LGuiSessionReader.cc
Go to the documentation of this file.
1 #include "LGuiSessionReader.hh"
2 #include <fstream>
3 #include <string>
4 #include "QGGraphicalCutBase.hh"
5 #include "QGTextFileKeys.hh"
6 #include "QObject.hh"
7 #include "QString.hh"
8 
10 
11 using std::string;
12 
13 using namespace Diana;
14 
16 {
17 }
18 
20 {
21  Close();
22 }
23 
24 const QObject* LGuiSessionReader::GetQObject(const std::string& name, const std::string& className, QError& err) const
25 {
26  // check for already existing object
27  err = QERR_SUCCESS;
28  std::map<std::string, QObject*>::const_iterator object
29  = fQObjectCache->find(name);
30  if(object != fQObjectCache->end()) return object->second;
31 
32  QObject* obj = NULL;
33 
34  if(className == "Diana::QString") {
35  obj = (QString*) new QString(GetString(name));
36  } else {
38  err.SetDescription(GetName(),__LINE__,std::string("Invalid type \"")+className+"\"");
39  }
40  // fill cache
41  if(obj){
42  obj->Validate();
43  (*fQObjectCache)[name] = obj;
44  }
45 
46  return obj;
47 }
48 
49 std::string LGuiSessionReader::GetString(const std::string& name) const
50 {
51  QGGraphicalCutBase *currentGraphicalCut = 0;
52  bool buildingString = false;
53  bool buildingGraphicalCut = false;
54 
55  string cuts;
56  string line;
57  std::ifstream file(fFilename.c_str());
58  while (getline(file, line)) {
59  string key, value;
60  string::size_type delimiter = line.find_first_of(" ");
61  if (delimiter != string::npos) {
62  key = line.substr(0, delimiter);
63  value = line.substr(delimiter + 1);
64  } else {
65  key = line;
66  value = "";
67  }
68  if (key == CUTS_STRING_KEY) {
69  buildingString = true;
70  buildingGraphicalCut = false;
71  currentGraphicalCut = 0;
72  } else if (key == GRAPHICAL_CUT_KEY) {
73  buildingString = false;
74  buildingGraphicalCut = true;
75  currentGraphicalCut = new QGGraphicalCutBase();
76  } else {
77  if (buildingString) {
78  cuts += line;
79  } else if (buildingGraphicalCut && currentGraphicalCut) {
80  currentGraphicalCut->SetProperty(key, value);
81  }
82  }
83  }
84  file.close();
85  return cuts;
86 }
87 
88 
89 QError LGuiSessionReader::Open(const string& filename, const std::string& opt)
90 {
92  fGuiSessionFile.open(filename.c_str());
93  if (!fGuiSessionFile) {
95  err.SetDescription(filename);
96  }
97  fFilename = filename;
98  return err;
99 }
100 
102 {
104  fGuiSessionFile.close();
105  return err;
106 }
err
Definition: CheckOF.C:114
Global reader for GUI sessions.
@ QERR_UNKNOWN_ERR
Definition: QError.hh:108
@ QERR_CANNOT_OPEN_FILE
Definition: QError.hh:33
@ QERR_SUCCESS
Definition: QError.hh:27
#define GRAPHICAL_CUT_KEY
#define CUTS_STRING_KEY
#define REGISTER_GLOBAL_READER(clazz, ext)
std::map< std::string, Diana::QObject * > * fQObjectCache
std::string GetString(const std::string &name) const
std::ifstream fGuiSessionFile
const Diana::QObject * GetQObject(const std::string &name, const std::string &className, QError &err) const
QError Close()
Close file, called by QGlobalReaderDispatcher.
QError Open(const std::string &filename, const std::string &opt="")
Open file, called by QGlobalReaderDispatcher.
error class with error type and description
Definition: QError.hh:115
Base class for GUI graphical cuts. This class has minimal dependencies on other GUI classes so it is ...
void SetProperty(const std::string &key, const std::string &value)
Set property (used for opening a session)
Abstract class for global readers.
const std::string & GetName() const
Definition: QNamed.hh:19
string wrapped into a QObject
Definition: QString.hh:18
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...