EditMatrixWidget.cpp
Go to the documentation of this file.
1#include "EditMatrixWidget.h"
2
3#include <QGridLayout>
4#include <QLabel>
5
8
11#include "NDArrayHelper.h"
12
13namespace armarx
14{
16 long numCols,
17 aron::type::matrix::ElementType elemType,
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);
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)
165 }
166 else
167 {
168 dispElements.at(row * dispCols + col)
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>
203 {
204 std::string str = getText(row, col);
205 try
206 {
207 switch (elemType)
208 {
209 case armarx::aron::type::matrix::UINT8:
211
212 case armarx::aron::type::matrix::UINT16:
214
215 case armarx::aron::type::matrix::UINT32:
217
218 case armarx::aron::type::matrix::INT8:
220
221 case armarx::aron::type::matrix::INT16:
223
224 case armarx::aron::type::matrix::INT32:
226
227 case armarx::aron::type::matrix::INT64:
229
230 case armarx::aron::type::matrix::FLOAT32:
232
233 case armarx::aron::type::matrix::FLOAT64:
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);
263 }
264} // namespace armarx
std::string str(const T &t)
static std::vector< unsigned char > toByteVector(const std::string &str)
void elemChanged(QTreeWidgetItem *elem, int col)
QTreeWidgetItem * overlayingItem
CustomWidget(QTreeWidgetItem *overlayingItem)
static EditMatrixWidget * DynamicCast(QWidget *)
std::string getText(long row, long col)
void setText(long row, long col, const std::string &str)
static EditMatrixWidget * DynamicCastAndCheck(QWidget *)
std::vector< unsigned char > parseElement(long row, long col)
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#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...
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
This file offers overloads of toIce() and fromIce() functions for STL container types.