EditMatrixWidget.cpp
Go to the documentation of this file.
1 #include "EditMatrixWidget.h"
2 
3 #include <QGridLayout>
4 #include <QLabel>
5 
8 
9 #include "../ColorPalettes.h"
10 #include "../visitors/AronTreeWidgetCreator.h"
11 #include "NDArrayHelper.h"
12 
13 namespace armarx::skills::gui
14 {
16  long numCols,
18  QTreeWidgetItem* currentItem) :
19  CustomWidget(currentItem)
20  {
21  realRows = numRows;
22  realCols = numCols;
23  this->elemType = elemType;
24  // init surrounding layout
25  outerVerticalLayout = new QVBoxLayout();
26  innerHorizontalLayout = new QHBoxLayout();
27  outerVerticalLayout->addItem(innerHorizontalLayout);
28 
29 
30  QGridLayout* innerGrid = new QGridLayout();
31  const long createRows = std::min(numRows, MAX_ROWS_DISPLAY);
32  const long createCols = std::min(numCols, MAX_COLS_DISPLAY);
33 
34  for (long i = 0; i < createRows * createCols; ++i)
35  {
36  auto* edit = new QLineEdit();
37  dispElements.push_back(edit);
38  int currRow = i / createCols;
39  int currCol = i % createCols;
40 
41  innerGrid->addWidget(edit, currRow, currCol);
42  std::stringstream ss;
43  ss << "(" << currRow << ", " << currCol << ")";
44  std::string text = ss.str();
45 
46  edit->setText(text.c_str());
47  }
48  innerHorizontalLayout->addItem(innerGrid);
49 
50  // check, if we need to signal to the user, that not all elements are displayed.
51  if (numRows > MAX_ROWS_DISPLAY)
52  {
53  QLabel* fullLabel = new QLabel("...");
54  outerVerticalLayout->addWidget(fullLabel);
55  }
56  if (numCols > MAX_COLS_DISPLAY)
57  {
58  QLabel* fullLabel = new QLabel("...");
59  innerHorizontalLayout->addWidget(fullLabel);
60  }
61  setLayout(outerVerticalLayout);
62 
63  // add hidden elements in vector
64  {
65  const auto hiddenCols = numCols - createCols;
66  // the 0th element holds all columns that are
67  // to the right of the displayed cols AND besides the displayed rows!
68  hiddenElems.push_back(std::vector<std::string>(hiddenCols * createRows, ""));
69 
70  // add all rows that are hidden
71  for (long i = 0; i < numRows - createRows; ++i)
72  {
73  // add full cols; everything is hidden here
74  hiddenElems.push_back(std::vector<std::string>(numCols, ""));
75  }
76  }
77 
78  for (size_t i = 0; i < dispElements.size(); ++i)
79  {
80  ARMARX_CHECK(connect(
81  dispElements[i], SIGNAL(editingFinished()), this, SLOT(matrixElementChanged())));
82  }
83  }
84 
87  {
88  return dynamic_cast<EditMatrixWidget*>(elem);
89  }
90 
93  {
94  if (!elem)
95  {
96  return nullptr;
97  }
98  auto* casted = DynamicCast(elem);
99  ARMARX_CHECK_NOT_NULL(casted);
100  return casted;
101  }
102 
103  void
104  EditMatrixWidget::setText(long row, long col, const std::string& str)
105  {
106  ARMARX_CHECK(row < realRows);
107  ARMARX_CHECK(col < realCols);
108  auto dispCols = getDisplayedCols();
109  auto dispRows = getDisplayedRows();
110  if (row < dispRows && col < dispCols)
111  {
112  dispElements[row * dispCols + col]->setText(str.c_str());
113  dispElements[row * dispCols + col]->setCursorPosition(0);
114  }
115  else if (row < dispRows)
116  {
117  long idx = row * (realCols - dispCols) + col - dispCols;
118  hiddenElems.at(0).at(idx) = str;
119  }
120  else
121  {
122  hiddenElems.at(row - dispRows + 1).at(col) = str;
123  }
124  }
125 
126  std::string
127  EditMatrixWidget::getText(long row, long col)
128  {
129  ARMARX_CHECK(row < realRows);
130  ARMARX_CHECK(col < realCols);
131  const auto dispRows = getDisplayedRows();
132  const auto dispCols = getDisplayedCols();
133  if (row < dispRows && col < dispCols)
134  {
135  auto txt = dispElements.at(row * dispCols + col)->text();
136  return txt.toStdString();
137  }
138  else if (row < getDisplayedRows())
139  {
140  // the stuff besides the displayed rows
141  long idx = row * (realCols - dispCols) + col - dispCols;
142  return hiddenElems.at(0).at(idx);
143  }
144  else
145  {
146  // stuff beneath displayed rows
147  return hiddenElems.at(row - dispRows + 1).at(col);
148  }
149  }
150 
151  void
153  {
154  auto dispRows = getDisplayedRows();
155  auto dispCols = getDisplayedCols();
156  for (long row = 0; row < dispRows; ++row)
157  {
158  for (long col = 0; col < dispCols; ++col)
159  {
160  auto parsed = parseElement(row, col);
161  if (parsed.empty())
162  {
163  dispElements.at(row * dispCols + col)
164  ->setPalette(gui_color_palette::getErrorPalette());
165  }
166  else
167  {
168  dispElements.at(row * dispCols + col)
169  ->setPalette(gui_color_palette::getNormalPalette());
170  }
171  }
172  }
173  }
174 
175  bool
177  {
178  // also parse the hidden stuff!
179  for (long row = 0; row < realRows; ++row)
180  {
181  for (long col = 0; col < realCols; ++col)
182  {
183  auto parsed = parseElement(row, col);
184  if (parsed.empty())
185  {
186  if (row >= getDisplayedRows() || col >= getDisplayedCols())
187  {
189  << "Programming error! Could not parse content in EditMatrixWidget "
190  "that was set programatically from inside the SkillManagerPlugin. "
191  "The error occured in row "
192  << row << " and col " << col << ".";
193  }
194  return true;
195  }
196  }
197  }
198  return false;
199  }
200 
201  std::vector<unsigned char>
202  EditMatrixWidget::parseElement(long row, long col)
203  {
204  std::string str = getText(row, col);
205  try
206  {
207  switch (elemType)
208  {
209  case armarx::aron::type::matrix::UINT8:
210  return NDArrayHelper::toByteVector<uint8_t>(str);
211 
212  case armarx::aron::type::matrix::UINT16:
213  return NDArrayHelper::toByteVector<uint16_t>(str);
214 
215  case armarx::aron::type::matrix::UINT32:
216  return NDArrayHelper::toByteVector<uint32_t>(str);
217 
218  case armarx::aron::type::matrix::INT8:
219  return NDArrayHelper::toByteVector<int8_t>(str);
220 
221  case armarx::aron::type::matrix::INT16:
222  return NDArrayHelper::toByteVector<int16_t>(str);
223 
224  case armarx::aron::type::matrix::INT32:
225  return NDArrayHelper::toByteVector<int32_t>(str);
226 
227  case armarx::aron::type::matrix::INT64:
228  return NDArrayHelper::toByteVector<int64_t>(str);
229 
230  case armarx::aron::type::matrix::FLOAT32:
231  return NDArrayHelper::toByteVector<float>(str);
232 
233  case armarx::aron::type::matrix::FLOAT64:
234  return NDArrayHelper::toByteVector<double>(str);
235  }
236  }
237  catch (const simox::error::SimoxError& err)
238  {
239  return {};
240  }
241  return {};
242  }
243 
244  long
245  EditMatrixWidget::getDisplayedRows() const
246  {
247  return std::min(realRows, MAX_ROWS_DISPLAY);
248  }
249 
250  long
251  EditMatrixWidget::getDisplayedCols() const
252  {
253  return std::min(realCols, MAX_COLS_DISPLAY);
254  }
255 
256  void
257  EditMatrixWidget::matrixElementChanged()
258  {
259  blockSignals(true);
261  blockSignals(false);
262  emit elemChanged(overlayingItem, 1);
263  }
264 } // namespace armarx::skills::gui
armarx::skills::gui::EditMatrixWidget::setText
void setText(long row, long col, const std::string &str)
Definition: EditMatrixWidget.cpp:104
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
EditMatrixWidget.h
armarx::skills::gui::EditMatrixWidget::DynamicCastAndCheck
static EditMatrixWidget * DynamicCastAndCheck(QWidget *)
Definition: EditMatrixWidget.cpp:92
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::skills::gui::EditMatrixWidget
Definition: EditMatrixWidget.h:19
armarx::skills::gui::CustomWidget::overlayingItem
QTreeWidgetItem * overlayingItem
Definition: CustomWidget.h:21
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::skills::gui::EditMatrixWidget::hasParseErrors
bool hasParseErrors()
Definition: EditMatrixWidget.cpp:176
armarx::skills::gui
Definition: PeriodicUpdateWidget.cpp:11
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::skills::gui::CustomWidget::elemChanged
void elemChanged(QTreeWidgetItem *elem, int col)
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::skills::gui::CustomWidget
Definition: CustomWidget.h:9
ExpressionException.h
armarx::skills::gui::EditMatrixWidget::EditMatrixWidget
EditMatrixWidget()=delete
armarx::skills::gui::EditMatrixWidget::getText
std::string getText(long row, long col)
Definition: EditMatrixWidget.cpp:127
armarx::skills::gui::EditMatrixWidget::parseElement
std::vector< unsigned char > parseElement(long row, long col)
Definition: EditMatrixWidget.cpp:202
armarx::skills::gui::EditMatrixWidget::highlightUnparsableEntries
void highlightUnparsableEntries()
Definition: EditMatrixWidget.cpp:152
armarx::skills::gui::gui_color_palette::getErrorPalette
QPalette getErrorPalette()
Definition: ColorPalettes.cpp:6
Logging.h
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx::skills::gui::gui_color_palette::getNormalPalette
QPalette getNormalPalette()
Definition: ColorPalettes.cpp:14
armarx::skills::gui::EditMatrixWidget::DynamicCast
static EditMatrixWidget * DynamicCast(QWidget *)
Definition: EditMatrixWidget.cpp:86
NDArrayHelper.h