From db72b73795956f6f1076d74ecc8b6680eb85a434 Mon Sep 17 00:00:00 2001 From: jussi Date: Thu, 5 Dec 2019 16:57:06 +0200 Subject: [PATCH] qt: add a way to expand editor in AssignableEditorDelegate --- .../qt/data/AssignableEditorDelegate.cpp | 36 ++++++++++++++++--- .../qt/data/AssignableEditorDelegate.h | 7 +++- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/modules/interface/qt/data/AssignableEditorDelegate.cpp b/src/modules/interface/qt/data/AssignableEditorDelegate.cpp index 3c84cfb..5041e0a 100644 --- a/src/modules/interface/qt/data/AssignableEditorDelegate.cpp +++ b/src/modules/interface/qt/data/AssignableEditorDelegate.cpp @@ -7,8 +7,10 @@ #include #include +#include -AssignableEditorDelegate::AssignableEditorDelegate(QObject *parent) : QStyledItemDelegate(parent) { +AssignableEditorDelegate::AssignableEditorDelegate(QTreeView *treeView, QObject *parent) : QStyledItemDelegate(parent) { + m_treeView = treeView; } QWidget *AssignableEditorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const @@ -43,9 +45,35 @@ QWidget *AssignableEditorDelegate::createEditor(QWidget *parent, const QStyleOpt } void AssignableEditorDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { - editor->setGeometry(option.rect); - //editor->setGeometry(QRect(QPoint(option.rect.topLeft()), QPoint(500, 500))); - //editor->setGeometry(option.widget->rect()); + if (index != m_spannedIndex) { + // Span editor across all columns + const int maxCol = index.model()->columnCount(index.parent()); + QRect tempRect = m_treeView->visualRect(index.model()->index(index.row(), 0, index.parent())); + int top = tempRect.top(); + int left = tempRect.left(); + int bottom = tempRect.bottom(); + int right= tempRect.right(); + + for(int i = 1; i < maxCol ; i++){ + tempRect = m_treeView->visualRect(index.model()->index(index.row(), i, index.parent())); + if (tempRect.top() bottom) { + bottom = tempRect.bottom(); + } + if (Q_LIKELY(tempRect.right() > right)) { + right= tempRect.right(); + } + } + editor->setGeometry(QRect(QPoint(left, top),QPoint(right, bottom))); + } + else { + editor->setGeometry(option.rect); + } } void AssignableEditorDelegate::setEditorData(QWidget* editor, const QModelIndex &index) const { diff --git a/src/modules/interface/qt/data/AssignableEditorDelegate.h b/src/modules/interface/qt/data/AssignableEditorDelegate.h index 7ef2f2e..063cd82 100644 --- a/src/modules/interface/qt/data/AssignableEditorDelegate.h +++ b/src/modules/interface/qt/data/AssignableEditorDelegate.h @@ -2,11 +2,12 @@ #include #include +#include class AssignableEditorDelegate : public QStyledItemDelegate { Q_OBJECT public: - AssignableEditorDelegate(QObject *parent = nullptr); + AssignableEditorDelegate(QTreeView *treeView, QObject *parent = nullptr); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; @@ -14,7 +15,11 @@ public: void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; + + void setSpannedIndex(const QModelIndex &index) {m_spannedIndex = index;} private: + QModelIndex m_spannedIndex; // Index of the editor that should span all columns QSize m_customExpansionSize; bool m_customExpansionSizeNeeded; + QTreeView *m_treeView; // Tree view that the delegate belongs to };