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 #include <argos3/core/wrappers/lua/lua_vector2.h>
13 #include <argos3/core/wrappers/lua/lua_vector3.h>
14 #include <argos3/core/wrappers/lua/lua_quaternion.h>
15 
16 #include <sstream>
17 
18 namespace argos {
19 
20  /****************************************/
21  /****************************************/
22 
24  bool b_remove_empty_tables,
25  QObject* pc_parent) :
26  QAbstractItemModel(pc_parent),
27  m_ptState(pt_state),
28  m_bRemoveEmptyTables(b_remove_empty_tables) {
29  m_pcDataRoot = new CQTOpenGLLuaStateTreeItem();
30  }
31 
32  /****************************************/
33  /****************************************/
34 
36  delete m_pcDataRoot;
37  }
38 
39  /****************************************/
40  /****************************************/
41 
42  QVariant CQTOpenGLLuaStateTreeModel::data(const QModelIndex& c_index,
43  int n_role) const {
44  if(!c_index.isValid()) {
45  return QVariant();
46  }
47  if(n_role != Qt::DisplayRole) {
48  return QVariant();
49  }
50  CQTOpenGLLuaStateTreeItem* pcItem = static_cast<CQTOpenGLLuaStateTreeItem*>(c_index.internalPointer());
51  return pcItem->GetData(c_index.column());
52  }
53 
54  /****************************************/
55  /****************************************/
56 
57  Qt::ItemFlags CQTOpenGLLuaStateTreeModel::flags(const QModelIndex& c_index) const {
58  if (!c_index.isValid()) {
59  return Qt::NoItemFlags;
60  }
61  else {
62  return Qt::ItemIsEnabled;
63  }
64  }
65 
66  /****************************************/
67  /****************************************/
68 
69  QModelIndex CQTOpenGLLuaStateTreeModel::index(int n_row,
70  int n_column,
71  const QModelIndex& c_parent) const {
72  if(!hasIndex(n_row, n_column, c_parent)) {
73  return QModelIndex();
74  }
75  CQTOpenGLLuaStateTreeItem* pcParentItem;
76  if(!c_parent.isValid()) {
77  pcParentItem = m_pcDataRoot;
78  }
79  else {
80  pcParentItem = static_cast<CQTOpenGLLuaStateTreeItem*>(c_parent.internalPointer());
81  }
82  CQTOpenGLLuaStateTreeItem* pcChildItem = pcParentItem->GetChild(n_row);
83  if(pcChildItem) {
84  return createIndex(n_row, n_column, pcChildItem);
85  }
86  else {
87  return QModelIndex();
88  }
89  }
90 
91  /****************************************/
92  /****************************************/
93 
94  QModelIndex CQTOpenGLLuaStateTreeModel::parent(const QModelIndex& c_index) const {
95  if (!c_index.isValid()) {
96  return QModelIndex();
97  }
98  CQTOpenGLLuaStateTreeItem* pcChildItem = static_cast<CQTOpenGLLuaStateTreeItem*>(c_index.internalPointer());
99  CQTOpenGLLuaStateTreeItem* pcParentItem = pcChildItem->GetParent();
100  if (pcParentItem == m_pcDataRoot) {
101  return QModelIndex();
102  }
103  else {
104  return createIndex(pcParentItem->GetRow(), 0, pcParentItem);
105  }
106  }
107 
108  /****************************************/
109  /****************************************/
110 
111  int CQTOpenGLLuaStateTreeModel::rowCount(const QModelIndex& c_parent) const {
112  CQTOpenGLLuaStateTreeItem* pcParentItem;
113  if(c_parent.column() > 0) {
114  return 0;
115  }
116  if(!c_parent.isValid()) {
117  pcParentItem = m_pcDataRoot;
118  }
119  else {
120  pcParentItem = static_cast<CQTOpenGLLuaStateTreeItem*>(c_parent.internalPointer());
121  }
122  return pcParentItem->GetNumChildren();
123  }
124 
125  /****************************************/
126  /****************************************/
127 
128  void CQTOpenGLLuaStateTreeModel::SetLuaState(lua_State* pt_state) {
129  m_ptState = pt_state;
130  Refresh();
131  }
132 
133  /****************************************/
134  /****************************************/
135 
137  beginResetModel();
138  delete m_pcDataRoot;
139  m_pcDataRoot = new CQTOpenGLLuaStateTreeItem();
140  lua_pushnil(m_ptState);
141  lua_getglobal(m_ptState, "_G");
142  ProcessLuaState(m_ptState, m_pcDataRoot);
143  m_pcDataRoot->SortChildren();
144  lua_pop(m_ptState, 2);
145  endResetModel();
146  }
147 
148  /****************************************/
149  /****************************************/
150 
152  Refresh();
153  }
154 
155  /****************************************/
156  /****************************************/
157 
159  CQTOpenGLLuaStateTreeItem* pc_item) {
160  QList<QVariant> cData;
161  switch(lua_type(pt_state, -2)) {
162  case LUA_TBOOLEAN:
163  cData << lua_toboolean(pt_state, -2);
164  break;
165  case LUA_TNUMBER:
166  cData << lua_tonumber(pt_state, -2);
167  break;
168  case LUA_TSTRING:
169  cData << lua_tostring(pt_state, -2);
170  break;
171  default: break;
172  }
173  if(lua_istable(pt_state, -1)) {
174  CQTOpenGLLuaStateTreeItem* pcChild = new CQTOpenGLLuaStateTreeItem(cData, pc_item);
175  pc_item->AddChild(pcChild);
176  lua_pushnil(pt_state);
177  while(lua_next(pt_state, -2)) {
178  if(IsTypeVisitable(pt_state)) {
179  ProcessLuaState(pt_state, pcChild);
180  }
181  lua_pop(pt_state, 1);
182  }
183  if(m_bRemoveEmptyTables) {
184  if(pcChild->GetNumChildren() == 0) {
185  pc_item->RemoveChild(pcChild);
186  }
187  }
188  }
189  else if(lua_isuserdata(pt_state, -1)) {
190  std::ostringstream cBuffer;
191  cBuffer << std::fixed;
192  void *pvUserdatum = lua_touserdata(pt_state, -1);
193  if (lua_getmetatable(pt_state, -1)) {
194  lua_getfield(pt_state, LUA_REGISTRYINDEX, CLuaVector2::GetTypeId().c_str()); /* get correct metatable */
195  if (lua_rawequal(pt_state, -1, -2)) {
196  /* remove the two metatables */
197  lua_pop(pt_state, 2);
198  /* add the entry to the table */
199  CVector2* pcVector2 = static_cast<CVector2*>(pvUserdatum);
200  cBuffer << std::setprecision(3)
201  << pcVector2->GetX() << ", "
202  << pcVector2->GetY();
203  cData << cBuffer.str().c_str();
204  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
205  }
206  else {
207  /* remove the vector2 metatable */
208  lua_pop(pt_state, 1);
209  lua_getfield(pt_state, LUA_REGISTRYINDEX, CLuaVector3::GetTypeId().c_str());
210  if (lua_rawequal(pt_state, -1, -2)) {
211  /* remove the two metatables */
212  lua_pop(pt_state, 2);
213  /* add the entry to the table */
214  CVector3* pcVector3 = static_cast<CVector3*>(pvUserdatum);
215  cBuffer << std::setprecision(3)
216  << pcVector3->GetX() << ", "
217  << pcVector3->GetY() << ", "
218  << pcVector3->GetZ();
219  cData << cBuffer.str().c_str();
220  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
221  }
222  else {
223  /* remove the vector3 metatable */
224  lua_pop(pt_state, 1);
225  lua_getfield(pt_state, LUA_REGISTRYINDEX, CLuaQuaternion::GetTypeId().c_str());
226  if (lua_rawequal(pt_state, -1, -2)) {
227  /* remove the two metatables */
228  lua_pop(pt_state, 2);
229  /* add the entry to the table */
230  CQuaternion* pcQuaternion = static_cast<CQuaternion*>(pvUserdatum);
231  CRadians cZ, cY, cX;
232  pcQuaternion->ToEulerAngles(cZ, cY, cX);
233  cBuffer << std::setprecision(1)
234  << ToDegrees(cZ).GetValue() << ", "
235  << ToDegrees(cY).GetValue() << ", "
236  << ToDegrees(cX).GetValue();
237  cData << cBuffer.str().c_str();
238  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
239  }
240  }
241  }
242  }
243  }
244  else {
245  switch(lua_type(pt_state, -1)) {
246  case LUA_TBOOLEAN:
247  cData << lua_toboolean(pt_state, -1);
248  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
249  break;
250  case LUA_TNUMBER:
251  cData << lua_tonumber(pt_state, -1);
252  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
253  break;
254  case LUA_TSTRING:
255  cData << lua_tostring(pt_state, -1);
256  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
257  break;
258  case LUA_TFUNCTION:
259  cData[0] = cData[0].toString() + tr("()");
260  pc_item->AddChild(new CQTOpenGLLuaStateTreeItem(cData, pc_item));
261  break;
262  default:
263  break;
264  }
265  }
266  }
267 
268  /****************************************/
269  /****************************************/
270 
272  bool b_remove_empty_tables,
273  QObject* pc_parent) :
274  CQTOpenGLLuaStateTreeModel(pt_state, b_remove_empty_tables, pc_parent) {}
275 
276  /****************************************/
277  /****************************************/
278 
280  Qt::Orientation e_orientation,
281  int n_role) const {
282  if(e_orientation != Qt::Horizontal ||
283  n_role != Qt::DisplayRole ||
284  n_section > 1) {
285  return QVariant();
286  }
287  else {
288  return n_section == 0 ? tr("Variable") : tr("Value");
289  }
290  }
291 
292  /****************************************/
293  /****************************************/
294 
295  int CQTOpenGLLuaStateTreeVariableModel::columnCount(const QModelIndex&) const {
296  return 2;
297  }
298 
299  /****************************************/
300  /****************************************/
301 
303  int nValueType = lua_type(pt_state, -1);
304  int nKeyType = lua_type(pt_state, -2);
305  if(nValueType == LUA_TSTRING || nValueType == LUA_TNUMBER || nValueType == LUA_TBOOLEAN || nValueType == LUA_TUSERDATA) {
306  if(nKeyType != LUA_TSTRING) {
307  return true;
308  }
309  else if(nKeyType == LUA_TSTRING) {
310  return std::string(lua_tostring(pt_state, -2)) != "_VERSION";
311  }
312  }
313  else if(nValueType == LUA_TTABLE) {
314  if(nKeyType == LUA_TNUMBER) {
315  return true;
316  }
317  else if(nKeyType == LUA_TSTRING) {
318  return
319  std::string(lua_tostring(pt_state, -2)) != "_G" &&
320  std::string(lua_tostring(pt_state, -2)) != "coroutine" &&
321  std::string(lua_tostring(pt_state, -2)) != "debug" &&
322  std::string(lua_tostring(pt_state, -2)) != "io" &&
323  std::string(lua_tostring(pt_state, -2)) != "os" &&
324  std::string(lua_tostring(pt_state, -2)) != "package" &&
325  std::string(lua_tostring(pt_state, -2)) != "string" &&
326  std::string(lua_tostring(pt_state, -2)) != "table";
327  }
328  }
329  return false;
330  }
331 
332  /****************************************/
333  /****************************************/
334 
336  bool b_remove_empty_tables,
337  QObject* pc_parent) :
338  CQTOpenGLLuaStateTreeModel(pt_state, b_remove_empty_tables, pc_parent) {}
339 
340  /****************************************/
341  /****************************************/
342 
344  Qt::Orientation e_orientation,
345  int n_role) const {
346  return QVariant();
347  }
348 
349  /****************************************/
350  /****************************************/
351 
352  int CQTOpenGLLuaStateTreeFunctionModel::columnCount(const QModelIndex&) const {
353  return 1;
354  }
355 
356  /****************************************/
357  /****************************************/
358 
360  int nValueType = lua_type(pt_state, -1);
361  int nKeyType = lua_type(pt_state, -2);
362  if(nValueType == LUA_TFUNCTION && nKeyType == LUA_TSTRING) {
363  return
364  std::string(lua_tostring(pt_state, -2)) != "assert" &&
365  std::string(lua_tostring(pt_state, -2)) != "collectgarbage" &&
366  std::string(lua_tostring(pt_state, -2)) != "dofile" &&
367  std::string(lua_tostring(pt_state, -2)) != "error" &&
368  std::string(lua_tostring(pt_state, -2)) != "gcinfo" &&
369  std::string(lua_tostring(pt_state, -2)) != "getfenv" &&
370  std::string(lua_tostring(pt_state, -2)) != "getmetatable" &&
371  std::string(lua_tostring(pt_state, -2)) != "ipairs" &&
372  std::string(lua_tostring(pt_state, -2)) != "load" &&
373  std::string(lua_tostring(pt_state, -2)) != "loadfile" &&
374  std::string(lua_tostring(pt_state, -2)) != "loadstring" &&
375  std::string(lua_tostring(pt_state, -2)) != "module" &&
376  std::string(lua_tostring(pt_state, -2)) != "newproxy" &&
377  std::string(lua_tostring(pt_state, -2)) != "next" &&
378  std::string(lua_tostring(pt_state, -2)) != "pairs" &&
379  std::string(lua_tostring(pt_state, -2)) != "pcall" &&
380  std::string(lua_tostring(pt_state, -2)) != "rawequal" &&
381  std::string(lua_tostring(pt_state, -2)) != "rawget" &&
382  std::string(lua_tostring(pt_state, -2)) != "rawset" &&
383  std::string(lua_tostring(pt_state, -2)) != "require" &&
384  std::string(lua_tostring(pt_state, -2)) != "select" &&
385  std::string(lua_tostring(pt_state, -2)) != "setfenv" &&
386  std::string(lua_tostring(pt_state, -2)) != "setmetatable" &&
387  std::string(lua_tostring(pt_state, -2)) != "unpack" &&
388  std::string(lua_tostring(pt_state, -2)) != "xpcall";
389  }
390  else if(nValueType == LUA_TTABLE) {
391  if(nKeyType == LUA_TNUMBER) {
392  return true;
393  }
394  else if(nKeyType == LUA_TSTRING) {
395  return
396  std::string(lua_tostring(pt_state, -2)) != "_G" &&
397  std::string(lua_tostring(pt_state, -2)) != "coroutine" &&
398  std::string(lua_tostring(pt_state, -2)) != "debug" &&
399  std::string(lua_tostring(pt_state, -2)) != "io" &&
400  std::string(lua_tostring(pt_state, -2)) != "os" &&
401  std::string(lua_tostring(pt_state, -2)) != "package";
402  }
403  }
404  return false;
405  }
406 
407  /****************************************/
408  /****************************************/
409 
410 }
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
CDegrees ToDegrees(const CRadians &c_radians)
Converts CRadians to CDegrees.
Definition: angles.h:489
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
Real GetValue() const
Returns the value in degrees.
Definition: angles.h:322
void ToEulerAngles(CRadians &c_z_angle, CRadians &c_y_angle, CRadians &c_x_angle) const
Definition: quaternion.h:172
A 2D vector class.
Definition: vector2.h:27
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector2.h:110
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector2.h:94
A 3D vector class.
Definition: vector3.h:31
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector3.h:105
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector3.h:121
Real GetZ() const
Returns the z coordinate of this vector.
Definition: vector3.h:137
static const std::string & GetTypeId()
static const std::string & GetTypeId()
Definition: lua_vector2.h:30
static const std::string & GetTypeId()
Definition: lua_vector3.h:30
void AddChild(CQTOpenGLLuaStateTreeItem *pc_child)
void RemoveChild(CQTOpenGLLuaStateTreeItem *pc_child)
CQTOpenGLLuaStateTreeItem * GetChild(size_t un_idx)
virtual int rowCount(const QModelIndex &c_parent=QModelIndex()) const
virtual QVariant data(const QModelIndex &c_index, int n_role) const
void ProcessLuaState(lua_State *pt_state, CQTOpenGLLuaStateTreeItem *pc_item)
CQTOpenGLLuaStateTreeModel(lua_State *pt_state, bool b_remove_empty_tables, QObject *pc_parent=0)
virtual bool IsTypeVisitable(lua_State *pt_state)=0
virtual QModelIndex parent(const QModelIndex &c_index) const
virtual QModelIndex index(int n_row, int n_column, const QModelIndex &c_parent=QModelIndex()) const
virtual Qt::ItemFlags flags(const QModelIndex &c_index) const
CQTOpenGLLuaStateTreeVariableModel(lua_State *pt_state, bool b_remove_empty_tables, QObject *pc_parent=0)
virtual int columnCount(const QModelIndex &c_parent=QModelIndex()) const
virtual QVariant headerData(int n_section, Qt::Orientation e_orientation, int n_role=Qt::DisplayRole) const
virtual int columnCount(const QModelIndex &c_parent=QModelIndex()) const
CQTOpenGLLuaStateTreeFunctionModel(lua_State *pt_state, bool b_remove_empty_tables, QObject *pc_parent=0)
virtual QVariant headerData(int n_section, Qt::Orientation e_orientation, int n_role=Qt::DisplayRole) const