Warning: include(php/utility.php): Failed to open stream: No such file or directory in /home/argos/argos3/doc/api/embedded/a00871_source.php on line 2

Warning: include(): Failed opening 'php/utility.php' for inclusion (include_path='.:/usr/lib64/php') in /home/argos/argos3/doc/api/embedded/a00871_source.php on line 2
The ARGoS Website

qtopengl_lua_statetree_model.cpp
Go to the documentation of this file.
1 
9 
10 #include <argos3/core/utility/logging/argos_log.h>
11 #include <argos3/core/wrappers/lua/lua_utility.h>
12 
13 namespace argos {
14 
15  /****************************************/
16  /****************************************/
17 
19  bool b_remove_empty_tables,
20  QObject* pc_parent) :
21  QAbstractItemModel(pc_parent),
22  m_ptState(pt_state),
23  m_bRemoveEmptyTables(b_remove_empty_tables) {
24  m_pcDataRoot = new CQTOpenGLLuaStateTreeItem();
25  }
26 
27  /****************************************/
28  /****************************************/
29 
31  delete m_pcDataRoot;
32  }
33 
34  /****************************************/
35  /****************************************/
36 
37  QVariant CQTOpenGLLuaStateTreeModel::data(const QModelIndex& c_index,
38  int n_role) const {
39  if(!c_index.isValid()) {
40  return QVariant();
41  }
42  if(n_role != Qt::DisplayRole) {
43  return QVariant();
44  }
45  CQTOpenGLLuaStateTreeItem* pcItem = static_cast<CQTOpenGLLuaStateTreeItem*>(c_index.internalPointer());
46  return pcItem->GetData(c_index.column());
47  }
48 
49  /****************************************/
50  /****************************************/
51 
52  Qt::ItemFlags CQTOpenGLLuaStateTreeModel::flags(const QModelIndex& c_index) const {
53  if (!c_index.isValid()) {
54  return 0;
55  }
56  else {
57  return Qt::ItemIsEnabled;
58  }
59  }
60 
61  /****************************************/
62  /****************************************/
63 
64  QModelIndex CQTOpenGLLuaStateTreeModel::index(int n_row,
65  int n_column,
66  const QModelIndex& c_parent) const {
67  if(!hasIndex(n_row, n_column, c_parent)) {
68  return QModelIndex();
69  }
70  CQTOpenGLLuaStateTreeItem* pcParentItem;
71  if(!c_parent.isValid()) {
72  pcParentItem = m_pcDataRoot;
73  }
74  else {
75  pcParentItem = static_cast<CQTOpenGLLuaStateTreeItem*>(c_parent.internalPointer());
76  }
77  CQTOpenGLLuaStateTreeItem* pcChildItem = pcParentItem->GetChild(n_row);
78  if(pcChildItem) {
79  return createIndex(n_row, n_column, pcChildItem);
80  }
81  else {
82  return QModelIndex();
83  }
84  }
85 
86  /****************************************/
87  /****************************************/
88 
89  QModelIndex CQTOpenGLLuaStateTreeModel::parent(const QModelIndex& c_index) const {
90  if (!c_index.isValid()) {
91  return QModelIndex();
92  }
93  CQTOpenGLLuaStateTreeItem* pcChildItem = static_cast<CQTOpenGLLuaStateTreeItem*>(c_index.internalPointer());
94  CQTOpenGLLuaStateTreeItem* pcParentItem = pcChildItem->GetParent();
95  if (pcParentItem == m_pcDataRoot) {
96  return QModelIndex();
97  }
98  else {
99  return createIndex(pcParentItem->GetRow(), 0, pcParentItem);
100  }
101  }
102 
103  /****************************************/
104  /****************************************/
105 
106  int CQTOpenGLLuaStateTreeModel::rowCount(const QModelIndex& c_parent) const {
107  CQTOpenGLLuaStateTreeItem* pcParentItem;
108  if(c_parent.column() > 0) {
109  return 0;
110  }
111  if(!c_parent.isValid()) {
112  pcParentItem = m_pcDataRoot;
113  }
114  else {
115  pcParentItem = static_cast<CQTOpenGLLuaStateTreeItem*>(c_parent.internalPointer());
116  }
117  return pcParentItem->GetNumChildren();
118  }
119 
120  /****************************************/
121  /****************************************/
122 
123  void CQTOpenGLLuaStateTreeModel::SetLuaState(lua_State* pt_state) {
124  m_ptState = pt_state;
125  Refresh();
126  }
127 
128  /****************************************/
129  /****************************************/
130 
132  beginResetModel();
133  delete m_pcDataRoot;
134  m_pcDataRoot = new CQTOpenGLLuaStateTreeItem();
135  lua_pushnil(m_ptState);
136  lua_getglobal(m_ptState, "_G");
137  ProcessLuaState(m_ptState, m_pcDataRoot);
138  m_pcDataRoot->SortChildren();
139  lua_pop(m_ptState, 2);
140  endResetModel();
141  }
142 
143  /****************************************/
144  /****************************************/
145 
147  Refresh();
148  }
149 
150  /****************************************/
151  /****************************************/
152 
154  CQTOpenGLLuaStateTreeItem* pc_item) {
155  QList<QVariant> cData;
156  switch(lua_type(pt_state, -2)) {
157  case LUA_TBOOLEAN:
158  cData << lua_toboolean(pt_state, -2);
159  break;
160  case LUA_TNUMBER:
161  cData << lua_tonumber(pt_state, -2);
162  break;
163  case LUA_TSTRING:
164  cData << lua_tostring(pt_state, -2);
165  break;
166  default: break;
167  }
168  if(lua_istable(pt_state, -1)) {
169  CQTOpenGLLuaStateTreeItem* pcChild = new CQTOpenGLLuaStateTreeItem(cData, pc_item);
170  pc_item->AddChild(pcChild);
171  lua_pushnil(pt_state);
172  while(lua_next(pt_state, -2)) {
173  if(IsTypeVisitable(pt_state)) {
174  ProcessLuaState(pt_state, pcChild);
175  }
176  lua_pop(pt_state, 1);
177  }
178  if(m_bRemoveEmptyTables) {
179  if(pcChild->GetNumChildren() == 0) {
180  pc_item->RemoveChild(pcChild);
181  }
182  }
183  }
184  else {
185  switch(lua_type(pt_state, -1)) {
186  case LUA_TBOOLEAN:
187  cData << lua_toboolean(pt_state, -1);
188  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
189  break;
190  case LUA_TNUMBER:
191  cData << lua_tonumber(pt_state, -1);
192  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
193  break;
194  case LUA_TSTRING:
195  cData << lua_tostring(pt_state, -1);
196  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
197  break;
198  case LUA_TFUNCTION:
199  cData[0] = cData[0].toString() + tr("()");
200  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
201  break;
202  default:
203  break;
204  }
205  }
206  }
207 
208  /****************************************/
209  /****************************************/
210 
212  bool b_remove_empty_tables,
213  QObject* pc_parent) :
214  CQTOpenGLLuaStateTreeModel(pt_state, b_remove_empty_tables, pc_parent) {}
215 
216  /****************************************/
217  /****************************************/
218 
220  Qt::Orientation e_orientation,
221  int n_role) const {
222  if(e_orientation != Qt::Horizontal ||
223  n_role != Qt::DisplayRole ||
224  n_section > 1) {
225  return QVariant();
226  }
227  else {
228  return n_section == 0 ? tr("Variable") : tr("Value");
229  }
230  }
231 
232  /****************************************/
233  /****************************************/
234 
235  int CQTOpenGLLuaStateTreeVariableModel::columnCount(const QModelIndex&) const {
236  return 2;
237  }
238 
239  /****************************************/
240  /****************************************/
241 
243  int nValueType = lua_type(pt_state, -1);
244  int nKeyType = lua_type(pt_state, -2);
245  if(nValueType == LUA_TSTRING || nValueType == LUA_TNUMBER || nValueType == LUA_TBOOLEAN) {
246  if(nKeyType != LUA_TSTRING) {
247  return true;
248  }
249  else if(nKeyType == LUA_TSTRING) {
250  return std::string(lua_tostring(pt_state, -2)) != "_VERSION";
251  }
252  }
253  else if(nValueType == LUA_TTABLE) {
254  if(nKeyType == LUA_TNUMBER) {
255  return true;
256  }
257  else if(nKeyType == LUA_TSTRING) {
258  return
259  std::string(lua_tostring(pt_state, -2)) != "_G" &&
260  std::string(lua_tostring(pt_state, -2)) != "coroutine" &&
261  std::string(lua_tostring(pt_state, -2)) != "debug" &&
262  std::string(lua_tostring(pt_state, -2)) != "io" &&
263  std::string(lua_tostring(pt_state, -2)) != "os" &&
264  std::string(lua_tostring(pt_state, -2)) != "package" &&
265  std::string(lua_tostring(pt_state, -2)) != "string" &&
266  std::string(lua_tostring(pt_state, -2)) != "table";
267  }
268  }
269  return false;
270  }
271 
272  /****************************************/
273  /****************************************/
274 
276  bool b_remove_empty_tables,
277  QObject* pc_parent) :
278  CQTOpenGLLuaStateTreeModel(pt_state, b_remove_empty_tables, pc_parent) {}
279 
280  /****************************************/
281  /****************************************/
282 
284  Qt::Orientation e_orientation,
285  int n_role) const {
286  return QVariant();
287  }
288 
289  /****************************************/
290  /****************************************/
291 
292  int CQTOpenGLLuaStateTreeFunctionModel::columnCount(const QModelIndex&) const {
293  return 1;
294  }
295 
296  /****************************************/
297  /****************************************/
298 
300  int nValueType = lua_type(pt_state, -1);
301  int nKeyType = lua_type(pt_state, -2);
302  if(nValueType == LUA_TFUNCTION && nKeyType == LUA_TSTRING) {
303  return
304  std::string(lua_tostring(pt_state, -2)) != "assert" &&
305  std::string(lua_tostring(pt_state, -2)) != "collectgarbage" &&
306  std::string(lua_tostring(pt_state, -2)) != "dofile" &&
307  std::string(lua_tostring(pt_state, -2)) != "error" &&
308  std::string(lua_tostring(pt_state, -2)) != "gcinfo" &&
309  std::string(lua_tostring(pt_state, -2)) != "getfenv" &&
310  std::string(lua_tostring(pt_state, -2)) != "getmetatable" &&
311  std::string(lua_tostring(pt_state, -2)) != "ipairs" &&
312  std::string(lua_tostring(pt_state, -2)) != "load" &&
313  std::string(lua_tostring(pt_state, -2)) != "loadfile" &&
314  std::string(lua_tostring(pt_state, -2)) != "loadstring" &&
315  std::string(lua_tostring(pt_state, -2)) != "module" &&
316  std::string(lua_tostring(pt_state, -2)) != "newproxy" &&
317  std::string(lua_tostring(pt_state, -2)) != "next" &&
318  std::string(lua_tostring(pt_state, -2)) != "pairs" &&
319  std::string(lua_tostring(pt_state, -2)) != "pcall" &&
320  std::string(lua_tostring(pt_state, -2)) != "rawequal" &&
321  std::string(lua_tostring(pt_state, -2)) != "rawget" &&
322  std::string(lua_tostring(pt_state, -2)) != "rawset" &&
323  std::string(lua_tostring(pt_state, -2)) != "require" &&
324  std::string(lua_tostring(pt_state, -2)) != "select" &&
325  std::string(lua_tostring(pt_state, -2)) != "setfenv" &&
326  std::string(lua_tostring(pt_state, -2)) != "setmetatable" &&
327  std::string(lua_tostring(pt_state, -2)) != "unpack" &&
328  std::string(lua_tostring(pt_state, -2)) != "xpcall";
329  }
330  else if(nValueType == LUA_TTABLE) {
331  if(nKeyType == LUA_TNUMBER) {
332  return true;
333  }
334  else if(nKeyType == LUA_TSTRING) {
335  return
336  std::string(lua_tostring(pt_state, -2)) != "_G" &&
337  std::string(lua_tostring(pt_state, -2)) != "coroutine" &&
338  std::string(lua_tostring(pt_state, -2)) != "debug" &&
339  std::string(lua_tostring(pt_state, -2)) != "io" &&
340  std::string(lua_tostring(pt_state, -2)) != "os" &&
341  std::string(lua_tostring(pt_state, -2)) != "package";
342  }
343  }
344  return false;
345  }
346 
347  /****************************************/
348  /****************************************/
349 
350 }
CQTOpenGLLuaStateTreeFunctionModel(lua_State *pt_state, bool b_remove_empty_tables, QObject *pc_parent=0)
CQTOpenGLLuaStateTreeModel(lua_State *pt_state, bool b_remove_empty_tables, QObject *pc_parent=0)
virtual bool IsTypeVisitable(lua_State *pt_state)=0
virtual Qt::ItemFlags flags(const QModelIndex &c_index) const
virtual QVariant data(const QModelIndex &c_index, int n_role) const
virtual QVariant headerData(int n_section, Qt::Orientation e_orientation, int n_role=Qt::DisplayRole) const
void ProcessLuaState(lua_State *pt_state, CQTOpenGLLuaStateTreeItem *pc_item)
virtual QModelIndex index(int n_row, int n_column, const QModelIndex &c_parent=QModelIndex()) const
CQTOpenGLLuaStateTreeVariableModel(lua_State *pt_state, bool b_remove_empty_tables, QObject *pc_parent=0)
virtual int columnCount(const QModelIndex &c_parent=QModelIndex()) const
CQTOpenGLLuaStateTreeItem * GetChild(size_t un_idx)
void AddChild(CQTOpenGLLuaStateTreeItem *pc_child)
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
virtual int rowCount(const QModelIndex &c_parent=QModelIndex()) const
virtual QModelIndex parent(const QModelIndex &c_index) const
virtual QVariant headerData(int n_section, Qt::Orientation e_orientation, int n_role=Qt::DisplayRole) const
void RemoveChild(CQTOpenGLLuaStateTreeItem *pc_child)
virtual int columnCount(const QModelIndex &c_parent=QModelIndex()) const