1111#include < QLineEdit>
1212#include < QMenu>
1313#include < QMessageBox>
14+ #include < QPainter>
1415#include < QPushButton>
1516#include < QString>
17+ #include < QStyleOptionViewItem>
18+ #include < QTextDocument>
1619#include < QVariant>
1720#include < QWidget>
1821#include < algorithm>
@@ -134,6 +137,8 @@ void SelectedItemModel::makePropertyItem(const Descriptor::Property& property,
134137 // as children rows
135138 if (auto sel_set = std::any_cast<Descriptor::PropertyList>(&value)) {
136139 value_item = makePropertyList (name_item, sel_set->begin (), sel_set->end ());
140+ } else if (auto sel_set = std::any_cast<PropertyTable>(&value)) {
141+ value_item = makePropertyTable (name_item, *sel_set);
137142 } else if (auto sel_set = std::any_cast<SelectionSet>(&value)) {
138143 value_item = makeList (name_item, sel_set->begin (), sel_set->end ());
139144 } else if (auto v_list = std::any_cast<std::vector<std::any>>(&value)) {
@@ -189,9 +194,7 @@ QStandardItem* SelectedItemModel::makeList(QStandardItem* name_item,
189194 name_item->appendRow ({index_item, selected_item});
190195 }
191196
192- QString items = QString::number (index) + " items" ;
193-
194- return makeItem (items);
197+ return makeItem (QString::number (index) + " items" );
195198}
196199
197200template <typename Iterator>
@@ -204,9 +207,50 @@ QStandardItem* SelectedItemModel::makePropertyList(QStandardItem* name_item,
204207 name_item->appendRow ({makeItem (name, true ), makeItem (value)});
205208 }
206209
207- QString items = QString::number (name_item->rowCount ()) + " items" ;
210+ return makeItem (QString::number (name_item->rowCount ()) + " items" );
211+ }
212+
213+ QStandardItem* SelectedItemModel::makePropertyTable (QStandardItem* name_item,
214+ const PropertyTable& table)
215+ {
216+ const int rows = table.getData ().size ();
217+ const int columns = table.getColumnHeaders ().size ();
218+
219+ QStandardItem* table_item = makeItem (QString ());
220+
221+ QString html;
222+ html += " <style>th, td {padding: 2px; border: 1px solid black; text-align: center; vertical-align: middle;}</style>" ;
223+ html += " <table>" ;
224+ // setup column headers
225+ html += " <tr><th />" ;
226+ for (int col = 0 ; col < columns; col++) {
227+ html += " <th>" ;
228+ html += QString::fromStdString (table.getColumnHeaders ().at (col))
229+ .replace (" \n " , " <br>" );
230+ html += " </th>" ;
231+ }
232+ html += " </tr>" ;
233+ // add rows
234+ for (int row = 0 ; row < rows; row++) {
235+ html += " <tr>" ;
236+ html += " <th>" ;
237+ html += QString::fromStdString (table.getRowHeaders ().at (row))
238+ .replace (" \n " , " <br>" );
239+ html += " </th>" ;
240+ for (int col = 0 ; col < columns; col++) {
241+ html += " <td>" ;
242+ html += QString::fromStdString (table.getData ().at (row).at (col))
243+ .replace (" \n " , " <br>" );
244+ html += " </td>" ;
245+ }
246+ html += " </tr>" ;
247+ }
248+ html += " </table>" ;
249+ table_item->setData (html, EditorItemDelegate::kHtml );
250+
251+ name_item->appendRow ({nullptr , table_item});
208252
209- return makeItem (items);
253+ return makeItem (QString::number (rows * columns) + " items" );
210254}
211255
212256void SelectedItemModel::makeItemEditor (const std::string& name,
@@ -240,7 +284,7 @@ void SelectedItemModel::makeItemEditor(const std::string& name,
240284
241285EditorItemDelegate::EditorItemDelegate (SelectedItemModel* model,
242286 QObject* parent)
243- : QItemDelegate (parent),
287+ : QStyledItemDelegate (parent),
244288 model_(model),
245289 background_(model->getEditableColor ())
246290{
@@ -383,6 +427,51 @@ EditorItemDelegate::EditType EditorItemDelegate::getEditorType(
383427 return EditorItemDelegate::kString ;
384428}
385429
430+ void EditorItemDelegate::paint (QPainter* painter,
431+ const QStyleOptionViewItem& option,
432+ const QModelIndex& index) const
433+ {
434+ if (index.model ()->data (index, kHtml ).toString ().isEmpty ()) {
435+ QStyledItemDelegate::paint (painter, option, index);
436+ return ;
437+ }
438+
439+ QStyleOptionViewItem options (option);
440+ initStyleOption (&options, index);
441+
442+ painter->save ();
443+
444+ QTextDocument doc;
445+ doc.setHtml (index.model ()->data (index, kHtml ).toString ());
446+
447+ /* Call this to get the focus rect and selection background. */
448+ options.text = " " ;
449+ options.widget ->style ()->drawControl (
450+ QStyle::CE_ItemViewItem, &options, painter);
451+
452+ /* Draw using text document. */
453+ painter->translate (options.rect .left (), options.rect .top ());
454+ const QRectF clip (0 , 0 , options.rect .width (), options.rect .height ());
455+ doc.drawContents (painter, clip);
456+
457+ painter->restore ();
458+ }
459+
460+ QSize EditorItemDelegate::sizeHint (const QStyleOptionViewItem& option,
461+ const QModelIndex& index) const
462+ {
463+ if (index.model ()->data (index, kHtml ).toString ().isEmpty ()) {
464+ return QStyledItemDelegate::sizeHint (option, index);
465+ }
466+ QStyleOptionViewItem options (option);
467+ initStyleOption (&options, index);
468+
469+ QTextDocument doc;
470+ doc.setHtml (index.model ()->data (index, kHtml ).toString ());
471+ doc.setTextWidth (options.rect .width ());
472+ return QSize (doc.idealWidth (), doc.size ().height ());
473+ }
474+
386475// //////
387476
388477ActionLayout::ActionLayout (QWidget* parent) : QLayout(parent)
0 commit comments