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 {
27  ConditionItemModel::ConditionItemModel() : QStandardItemModel()
28  {
29  reset();
30  }
31 
32  void
33  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 
51  void
53  {
54  clear();
55  setRowCount(0);
56  setColumnCount(6);
57 
58  // setup header
59  setHorizontalHeaderItem(0, new QStandardItem("id"));
60  setHorizontalHeaderItem(1, new QStandardItem("component"));
61  setHorizontalHeaderItem(2, new QStandardItem("state"));
62  setHorizontalHeaderItem(3, new QStandardItem("event"));
63  setHorizontalHeaderItem(4, new QStandardItem("fired"));
64  setHorizontalHeaderItem(5, new QStandardItem("description"));
65  }
66 
67  void
68  ConditionItemModel::updateOrInsertCondition(int id, const ConditionRootPtr& condition)
69  {
70  // loop through all rows
71  bool update = false;
72  int updateRow = 0;
73 
74  for (int r = 0; r < rowCount(); r++)
75  {
76  QStandardItem* current = item(r, 0);
77 
78  if (current->data(CONDITION_ITEM_ID) == id)
79  {
80  update = true;
81  updateRow = r;
82  }
83  }
84 
85  // check if new row is required
86  if (!update)
87  {
88  insertRow(rowCount());
89  updateRow = rowCount() - 1;
90 
91  // initial row content
92  QString id_string;
93  id_string.sprintf("%06d", id);
94 
95  QStandardItem* index = new QStandardItem(id_string);
96  index->setData(QVariant(id), CONDITION_ITEM_ID);
97 
98  setItem(updateRow, 0, index);
99  setItem(updateRow, 1, new QStandardItem("component"));
100  setItem(updateRow, 2, new QStandardItem("state"));
101  setItem(updateRow, 3, new QStandardItem("event"));
102  setItem(updateRow, 4, new QStandardItem("false"));
103  setItem(updateRow, 5, new QStandardItem("dsc"));
104  }
105 
106  // update fields
107  std::string component, event;
108  extractFields(condition->event->eventName, component, event);
109 
110  item(updateRow, 0)->setData(QVariant(false), CONDITION_ITEM_DELETE);
111  item(updateRow, 1)->setData(QVariant(component.c_str()), Qt::DisplayRole);
112  item(updateRow, 2)
113  ->setData(QVariant(condition->event->eventReceiverName.c_str()), Qt::DisplayRole);
114  item(updateRow, 3)->setData(QVariant(event.c_str()), Qt::DisplayRole);
115 
116  if (condition->fired)
117  {
118  item(updateRow, 4)->setData(QVariant("true"), Qt::DisplayRole);
119  item(updateRow, 4)->setBackground(Qt::green);
120  }
121  else
122  {
123  item(updateRow, 4)->setData(QVariant("false"), Qt::DisplayRole);
124  item(updateRow, 4)->setBackground(Qt::red);
125  }
126 
127  item(updateRow, 5)->setData(QVariant(condition->description.c_str()), Qt::DisplayRole);
128  }
129 
130  void
131  ConditionItemModel::markAllForDelete()
132  {
133  // mark all elements for deletion
134  for (int r = 0; r < rowCount(); r++)
135  {
136  item(r, 0)->setData(QVariant(true), CONDITION_ITEM_DELETE);
137  }
138  }
139 
140  void
141  ConditionItemModel::deleteUnusedItems()
142  {
143  // find unused rows
144  std::vector<int> removeRows;
145 
146  for (int r = 0; r < rowCount(); r++)
147  {
148  if (item(r, 0)->data(CONDITION_ITEM_DELETE).toBool())
149  {
150  removeRows.push_back(r);
151  }
152  }
153 
154  // remove unused rows
155  std::vector<int>::iterator iter = removeRows.begin();
156 
157  while (iter != removeRows.end())
158  {
159  removeRow(*iter);
160  iter++;
161  }
162  }
163 
164  void
165  ConditionItemModel::extractFields(std::string in,
166  std::string& componentName,
167  std::string& eventName)
168  {
169  std::size_t pos = in.find("_EventDistributor");
170 
171  if (pos != std::string::npos)
172  {
173  componentName = in.substr(0, pos - 1);
174  }
175 
176  std::size_t posEvent = in.find("__Event:");
177  std::size_t posRecv = in.find("__EventRecv:");
178 
179  if ((posEvent == std::string::npos) || (posRecv == std::string::npos))
180  {
181  return;
182  }
183 
184  posEvent += 8;
185  posRecv += 12;
186 
187  eventName = in.substr(posEvent, posRecv - posEvent - 13);
188  }
189 } // namespace armarx
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::ProxyType::component
@ component
CONDITION_ITEM_DELETE
#define CONDITION_ITEM_DELETE
Definition: ConditionItemModel.h:32
armarx::ConditionItemModel::reset
void reset()
Definition: ConditionItemModel.cpp:52
ConditionItemModel.h
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::red
QColor red()
Definition: StyleSheets.h:78
CONDITION_ITEM_ID
#define CONDITION_ITEM_ID
Definition: ConditionItemModel.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
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