21 #include <QHeaderView> 24 #include <variant_topic_tools/MessageVariable.h> 28 Q_DECLARE_METATYPE(variant_topic_tools::DataType)
36 MessageFieldTreeWidget::MessageFieldTreeWidget(QWidget* parent) :
39 headerItem()->setText(0,
"Name");
40 headerItem()->setText(1,
"Type");
42 #if QT_VERSION >= 0x050000 43 header()->setSectionResizeMode(QHeaderView::ResizeToContents);
45 header()->setResizeMode(QHeaderView::ResizeToContents);
61 MessageDataType& dataType) {
65 invisibleRootItem()->setData(1, Qt::UserRole, QVariant::
66 fromValue<variant_topic_tools::DataType>(dataType));
67 for (
size_t i = 0; i < dataType.getNumVariableMembers(); ++i)
68 addField(dataType.getVariableMember(i));
77 QTreeWidgetItem* item = invisibleRootItem();
80 return item->data(1, Qt::UserRole).value<variant_topic_tools::DataType>();
82 return variant_topic_tools::DataType();
101 QTreeWidgetItem* item = currentItem();
104 return item->data(1, Qt::UserRole).value<variant_topic_tools::DataType>();
106 return variant_topic_tools::DataType();
114 QTreeWidgetItem* item = invisibleRootItem();
115 QStringList fields = field.split(
"/");
117 while (item && !fields.isEmpty()) {
118 QVariant itemData = item->data(1, Qt::UserRole);
120 if (itemData.isValid()) {
121 variant_topic_tools::DataType fieldType = itemData.
122 value<variant_topic_tools::DataType>();
124 if (fieldType.isMessage()) {
125 QTreeWidgetItem* childItem =
findChild(item, 0, fields.front());
129 fields.removeFirst();
134 else if (fieldType.isArray()) {
135 bool indexOkay =
false;
136 size_t index = fields.front().toUInt(&indexOkay);
137 QSpinBox* spinBoxIndex =
static_cast<QSpinBox*
>(
138 itemWidget(item->child(0), 0));
140 if (indexOkay && (index < spinBoxIndex->maximum())) {
141 spinBoxIndex->blockSignals(
true);
142 spinBoxIndex->setValue(index);
143 spinBoxIndex->blockSignals(
false);
145 item = item->child(0);
146 fields.removeFirst();
153 item = invisibleRootItem();
158 QTreeWidget::setCurrentItem(item);
167 MessageVariable& variable, QTreeWidgetItem* parent) {
168 QTreeWidgetItem* item =
new QTreeWidgetItem();
170 item->setText(0, QString::fromStdString(variable.getName()));
171 item->setText(1, QString::fromStdString(variable.getType().
173 item->setData(1, Qt::UserRole, QVariant::fromValue(variable.getType()));
174 item->setFlags(Qt::ItemIsEnabled);
176 QFont typeFont = item->font(1);
177 typeFont.setItalic(
true);
178 item->setFont(1, typeFont);
181 parent->addChild(item);
183 addTopLevelItem(item);
185 if (variable.getType().isMessage()) {
186 variant_topic_tools::MessageDataType messageType = variable.getType();
188 for (
size_t i = 0; i < messageType.getNumVariableMembers(); ++i)
189 addField(messageType.getVariableMember(i), item);
191 else if (variable.getType().isArray()) {
192 variant_topic_tools::ArrayDataType arrayType = variable.getType();
194 QSpinBox* spinBoxIndex =
new QSpinBox(
this);
195 spinBoxIndex->setMinimum(0);
196 if (!arrayType.isDynamic())
197 spinBoxIndex->setMaximum(arrayType.getNumMembers()-1);
199 spinBoxIndex->setMaximum(std::numeric_limits<int>::max());
200 spinBoxIndex->setFrame(
false);
202 connect(spinBoxIndex, SIGNAL(valueChanged(
int)),
this,
205 QTreeWidgetItem* memberItem =
new QTreeWidgetItem();
206 memberItem->setText(1, QString::fromStdString(arrayType.getMemberType().
208 memberItem->setData(1, Qt::UserRole, QVariant::fromValue(arrayType.
210 memberItem->setFlags(Qt::ItemIsEnabled);
212 QFont memberTypeFont = memberItem->font(1);
213 memberTypeFont.setItalic(
true);
214 memberItem->setFont(1, memberTypeFont);
216 item->addChild(memberItem);
217 setItemWidget(memberItem, 0, spinBoxIndex);
219 if (arrayType.getMemberType().isMessage()) {
220 variant_topic_tools::MessageDataType messageMemberType = arrayType.
223 for (
size_t i = 0; i < messageMemberType.getNumVariableMembers(); ++i)
224 addField(messageMemberType.getVariableMember(i), memberItem);
226 else if (arrayType.getMemberType().isBuiltin()) {
227 variant_topic_tools::BuiltinDataType builtinMemberType = arrayType.
230 if (builtinMemberType.isNumeric())
231 memberItem->setFlags(memberItem->flags() | Qt::ItemIsSelectable);
234 else if (variable.getType().isBuiltin()) {
235 variant_topic_tools::BuiltinDataType builtinType = variable.getType();
237 if (builtinType.isNumeric())
238 item->setFlags(item->flags() | Qt::ItemIsSelectable);
243 int column,
const QString& text)
const {
244 for (
size_t i = 0; i < item->childCount(); ++i) {
245 if (item->child(i)->text(column) == text)
246 return item->child(i);
257 QTreeWidgetItem* previous) {
261 QString text = current->text(0);
263 if (text.isEmpty()) {
264 QSpinBox* spinBoxIndex =
static_cast<QSpinBox*
>(
265 itemWidget(current, 0));
267 text = QString::number(spinBoxIndex->value());
270 if (!field.isEmpty())
271 field = text+
"/"+field;
275 current = current->parent();