1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-01 09:24:10 +01:00

LP-286 get TreeItem using data() method instead of internalPointer() method

not only it is not legal to use internalPointer() outside of the model class
but it also requires to do nasty index juggling when using a proxy model
This commit is contained in:
Philippe Renon 2016-04-23 18:06:41 +02:00 committed by Mateusz Kaduk
parent 0f339dfef0
commit 8db244b30a
3 changed files with 20 additions and 41 deletions

View File

@ -30,53 +30,45 @@
#include "browseritemdelegate.h"
#include "fieldtreeitem.h"
BrowserItemDelegate::BrowserItemDelegate(TreeSortFilterProxyModel *proxyModel, QObject *parent) :
BrowserItemDelegate::BrowserItemDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
this->proxyModel = proxyModel;
}
{}
QWidget *BrowserItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & option,
const QModelIndex & proxyIndex) const
const QModelIndex & index) const
{
Q_UNUSED(option)
QModelIndex index = proxyModel->mapToSource(proxyIndex);
FieldTreeItem *item = static_cast<FieldTreeItem *>(index.internalPointer());
QWidget *editor = item->createEditor(parent);
FieldTreeItem * item = static_cast<FieldTreeItem *>(index.data(Qt::UserRole).value<void *>());
QWidget *editor = item->createEditor(parent);
Q_ASSERT(editor);
return editor;
}
void BrowserItemDelegate::setEditorData(QWidget *editor,
const QModelIndex & proxyIndex) const
const QModelIndex & index) const
{
QModelIndex index = proxyModel->mapToSource(proxyIndex);
FieldTreeItem *item = static_cast<FieldTreeItem *>(index.internalPointer());
QVariant value = proxyIndex.model()->data(proxyIndex, Qt::EditRole);
FieldTreeItem *item = static_cast<FieldTreeItem *>(index.data(Qt::UserRole).value<void *>());
QVariant value = index.model()->data(index, Qt::EditRole);
item->setEditorValue(editor, value);
}
void BrowserItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &proxyIndex) const
const QModelIndex &index) const
{
QModelIndex index = proxyModel->mapToSource(proxyIndex);
FieldTreeItem *item = static_cast<FieldTreeItem *>(index.internalPointer());
FieldTreeItem *item = static_cast<FieldTreeItem *>(index.data(Qt::UserRole).value<void *>());
QVariant value = item->getEditorValue(editor);
bool ret = model->setData(proxyIndex, value, Qt::EditRole);
bool ret = model->setData(index, value, Qt::EditRole);
Q_ASSERT(ret);
}
void BrowserItemDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex & /* index */) const
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}

View File

@ -31,12 +31,10 @@
#include <QStyledItemDelegate>
class TreeSortFilterProxyModel;
class BrowserItemDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
explicit BrowserItemDelegate(TreeSortFilterProxyModel *proxyModel, QObject *parent = 0);
explicit BrowserItemDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
@ -49,14 +47,6 @@ public:
const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem & option,
const QModelIndex &index) const;
signals:
public slots:
private:
TreeSortFilterProxyModel *proxyModel;
};
#endif // BROWSERITEMDELEGATE_H

View File

@ -56,8 +56,7 @@ UAVObjectBrowserWidget::UAVObjectBrowserWidget(QWidget *parent) : QWidget(parent
m_browser->treeView->setModel(m_modelProxy);
m_browser->treeView->setColumnWidth(0, 300);
BrowserItemDelegate *m_delegate = new BrowserItemDelegate(m_modelProxy);
m_browser->treeView->setItemDelegate(m_delegate);
m_browser->treeView->setItemDelegate(new BrowserItemDelegate());
m_browser->treeView->setEditTriggers(QAbstractItemView::AllEditTriggers);
m_browser->treeView->setSelectionBehavior(QAbstractItemView::SelectItems);
m_mustacheTemplate = loadFileIntoString(QString(":/uavobjectbrowser/resources/uavodescription.mustache"));
@ -191,8 +190,8 @@ void UAVObjectBrowserWidget::requestUpdate()
ObjectTreeItem *UAVObjectBrowserWidget::findCurrentObjectTreeItem()
{
QModelIndex current = m_modelProxy->mapToSource(m_browser->treeView->currentIndex());
TreeItem *item = static_cast<TreeItem *>(current.internalPointer());
QModelIndex current = m_browser->treeView->currentIndex();
TreeItem *item = static_cast<TreeItem *>(current.data(Qt::UserRole).value<void *>());
ObjectTreeItem *objItem = 0;
while (item) {
@ -277,11 +276,9 @@ void UAVObjectBrowserWidget::currentChanged(const QModelIndex &current, const QM
{
Q_UNUSED(previous);
QModelIndex cindex = m_modelProxy->mapToSource(current);
TreeItem *item = static_cast<TreeItem *>(cindex.internalPointer());
bool enable = true;
if (cindex == QModelIndex()) {
TreeItem *item = static_cast<TreeItem *>(current.data(Qt::UserRole).value<void *>());
bool enable = true;
if (!current.isValid()) {
enable = false;
}
TopTreeItem *top = dynamic_cast<TopTreeItem *>(item);