ConditionItemModel.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::Gui
17 * @author Kai Welke (welke@kit.edu)
18 * @copyright 2012 Humanoids Group, IAIM, IFA
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "ConditionItemModel.h"
24 
25 namespace armarx
26 {
28  : QStandardItemModel()
29  {
30  reset();
31  }
32 
33  void ConditionItemModel::update(const ConditionRegistry& registry)
34  {
35  // mark all items for delete
36  markAllForDelete();
37 
38  // update iterm
39  ConditionRegistry::const_iterator iter = registry.begin();
40 
41  while (iter != registry.end())
42  {
43  updateOrInsertCondition(iter->first, ConditionRootPtr::dynamicCast(iter->second));
44  iter++;
45  }
46 
47  // delete all unused items
48  deleteUnusedItems();
49  }
50 
52  {
53  clear();
54  setRowCount(0);
55  setColumnCount(6);
56 
57  // setup header
58  setHorizontalHeaderItem(0, new QStandardItem("id"));
59  setHorizontalHeaderItem(1, new QStandardItem("component"));
60  setHorizontalHeaderItem(2, new QStandardItem("state"));
61  setHorizontalHeaderItem(3, new QStandardItem("event"));
62  setHorizontalHeaderItem(4, new QStandardItem("fired"));
63  setHorizontalHeaderItem(5, new QStandardItem("description"));
64  }
65 
66  void ConditionItemModel::updateOrInsertCondition(int id, const ConditionRootPtr& condition)
67  {
68  // loop through all rows
69  bool update = false;
70  int updateRow = 0;
71 
72  for (int r = 0 ; r < rowCount() ; r++)
73  {
74  QStandardItem* current = item(r, 0);
75 
76  if (current->data(CONDITION_ITEM_ID) == id)
77  {
78  update = true;
79  updateRow = r;
80  }
81  }
82 
83  // check if new row is required
84  if (!update)
85  {
86  insertRow(rowCount());
87  updateRow = rowCount() - 1;
88 
89  // initial row content
90  QString id_string;
91  id_string.sprintf("%06d", id);
92 
93  QStandardItem* index = new QStandardItem(id_string);
94  index->setData(QVariant(id), CONDITION_ITEM_ID);
95 
96  setItem(updateRow, 0, index);
97  setItem(updateRow, 1, new QStandardItem("component"));
98  setItem(updateRow, 2, new QStandardItem("state"));
99  setItem(updateRow, 3, new QStandardItem("event"));
100  setItem(updateRow, 4, new QStandardItem("false"));
101  setItem(updateRow, 5, new QStandardItem("dsc"));
102  }
103 
104  // update fields
105  std::string component, event;
106  extractFields(condition->event->eventName, component, event);
107 
108  item(updateRow, 0)->setData(QVariant(false), CONDITION_ITEM_DELETE);
109  item(updateRow, 1)->setData(QVariant(component.c_str()), Qt::DisplayRole);
110  item(updateRow, 2)->setData(QVariant(condition->event->eventReceiverName.c_str()), Qt::DisplayRole);
111  item(updateRow, 3)->setData(QVariant(event.c_str()), Qt::DisplayRole);
112 
113  if (condition->fired)
114  {
115  item(updateRow, 4)->setData(QVariant("true"), Qt::DisplayRole);
116  item(updateRow, 4)->setBackground(Qt::green);
117  }
118  else
119  {
120  item(updateRow, 4)->setData(QVariant("false"), Qt::DisplayRole);
121  item(updateRow, 4)->setBackground(Qt::red);
122  }
123 
124  item(updateRow, 5)->setData(QVariant(condition->description.c_str()), Qt::DisplayRole);
125  }
126 
127  void ConditionItemModel::markAllForDelete()
128  {
129  // mark all elements for deletion
130  for (int r = 0 ; r < rowCount() ; r++)
131  {
132  item(r, 0)->setData(QVariant(true), CONDITION_ITEM_DELETE);
133  }
134  }
135 
136  void ConditionItemModel::deleteUnusedItems()
137  {
138  // find unused rows
139  std::vector<int> removeRows;
140 
141  for (int r = 0 ; r < rowCount() ; r++)
142  {
143  if (item(r, 0)->data(CONDITION_ITEM_DELETE).toBool())
144  {
145  removeRows.push_back(r);
146  }
147  }
148 
149  // remove unused rows
150  std::vector<int>::iterator iter = removeRows.begin();
151 
152  while (iter != removeRows.end())
153  {
154  removeRow(*iter);
155  iter++;
156  }
157  }
158 
159  void ConditionItemModel::extractFields(std::string in, std::string& componentName, std::string& eventName)
160  {
161  std::size_t pos = in.find("_EventDistributor");
162 
163  if (pos != std::string::npos)
164  {
165  componentName = in.substr(0, pos - 1);
166  }
167 
168  std::size_t posEvent = in.find("__Event:");
169  std::size_t posRecv = in.find("__EventRecv:");
170 
171  if ((posEvent == std::string::npos) || (posRecv == std::string::npos))
172  {
173  return;
174  }
175 
176  posEvent += 8;
177  posRecv += 12;
178 
179  eventName = in.substr(posEvent, posRecv - posEvent - 13);
180  }
181 }
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::ProxyType::component
@ component
CONDITION_ITEM_DELETE
#define CONDITION_ITEM_DELETE
Definition: ConditionItemModel.h:31
armarx::ConditionItemModel::reset
void reset()
Definition: ConditionItemModel.cpp:51
ConditionItemModel.h
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::red
QColor red()
Definition: StyleSheets.h:76
CONDITION_ITEM_ID
#define CONDITION_ITEM_ID
Definition: ConditionItemModel.h:32
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ConditionItemModel::ConditionItemModel
ConditionItemModel()
Definition: ConditionItemModel.cpp:27
armarx::ConditionItemModel::update
void update(const ConditionRegistry &registry)
Definition: ConditionItemModel.cpp:33
armarx::green
QColor green()
Definition: StyleSheets.h:72