tdf#130857 qt weld: Add tree view iter and implement some methods

Add a new QtInstanceTreeIter struct as the weld::TreeIter
implementaton for QtInstanceTreeView.
It uses the QModelIndex of the item it points to.

Add QtInstanceTreeView::modelIndex helper methods
to convert both, an int row index and a QtInstanceTreeIter
to a QModelIndex.
This can be used to deduplicate code when (re)implementing
the methods that have two variants that either take an
int row index or a TreeIter input param.

For QtInstanceTreeView::get_id, add a new variant that
takes a QModelIndex and move the existing logic from
the implementation for the int param there and reuse
it to implemetnat the variant taking a TreeIter param
as well.

Implement a few more methods taking or returning
a weld::TreeIter.

Change-Id: I0508ad14a43214faccc6a3cba208400031bb231c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178069
Tested-by: Jenkins
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
This commit is contained in:
Michael Weghorn 2024-12-08 00:51:02 +01:00
parent 254743e93f
commit aad48b06e7
2 changed files with 64 additions and 24 deletions

View file

@ -185,6 +185,9 @@ public:
using QtInstanceWidget::get_sensitive;
private:
QModelIndex modelIndex(int nPos) const;
static QModelIndex modelIndex(const weld::TreeIter& rIter);
OUString get_id(const QModelIndex& rModelIndex) const;
static QAbstractItemView::SelectionMode mapSelectionMode(SelectionMode eMode);
private Q_SLOTS:

View file

@ -15,6 +15,24 @@
// role used for the ID in the QStandardItem
constexpr int ROLE_ID = Qt::UserRole + 1000;
namespace
{
struct QtInstanceTreeIter final : public weld::TreeIter
{
QModelIndex m_aModelIndex;
explicit QtInstanceTreeIter(QModelIndex aModelIndex)
: m_aModelIndex(aModelIndex)
{
}
virtual bool equal(const TreeIter& rOther) const override
{
return m_aModelIndex == static_cast<const QtInstanceTreeIter&>(rOther).m_aModelIndex;
}
};
};
QtInstanceTreeView::QtInstanceTreeView(QTreeView* pTreeView)
: QtInstanceWidget(pTreeView)
, m_pTreeView(pTreeView)
@ -300,19 +318,7 @@ int QtInstanceTreeView::find_text(const OUString& rText) const
return nIndex;
}
OUString QtInstanceTreeView::get_id(int nPos) const
{
SolarMutexGuard g;
OUString sId;
GetQtInstance().RunInMainThread([&] {
QVariant aRoleData = m_pModel->data(m_pModel->index(nPos, 0), ROLE_ID);
if (aRoleData.canConvert<QString>())
sId = toOUString(aRoleData.toString());
});
return sId;
}
OUString QtInstanceTreeView::get_id(int nPos) const { return get_id(modelIndex(nPos)); }
int QtInstanceTreeView::find_id(const OUString& rId) const
{
@ -333,21 +339,32 @@ int QtInstanceTreeView::find_id(const OUString& rId) const
return nIndex;
}
std::unique_ptr<weld::TreeIter> QtInstanceTreeView::make_iterator(const weld::TreeIter*) const
std::unique_ptr<weld::TreeIter> QtInstanceTreeView::make_iterator(const weld::TreeIter* pOrig) const
{
assert(false && "Not implemented yet");
return nullptr;
const QModelIndex aIndex = pOrig ? modelIndex(*pOrig) : QModelIndex();
return std::make_unique<QtInstanceTreeIter>(aIndex);
}
void QtInstanceTreeView::copy_iterator(const weld::TreeIter&, weld::TreeIter&) const
void QtInstanceTreeView::copy_iterator(const weld::TreeIter& rSource, weld::TreeIter& rDest) const
{
assert(false && "Not implemented yet");
static_cast<QtInstanceTreeIter&>(rDest).m_aModelIndex = modelIndex(rSource);
}
bool QtInstanceTreeView::get_selected(weld::TreeIter*) const
bool QtInstanceTreeView::get_selected(weld::TreeIter* pIter) const
{
assert(false && "Not implemented yet");
return false;
SolarMutexGuard g;
bool bHasSelection = false;
GetQtInstance().RunInMainThread([&] {
const QModelIndexList aSelectedIndexes = m_pSelectionModel->selectedIndexes();
if (aSelectedIndexes.empty())
return;
bHasSelection = true;
if (pIter)
static_cast<QtInstanceTreeIter*>(pIter)->m_aModelIndex = aSelectedIndexes.first();
});
return bHasSelection;
}
bool QtInstanceTreeView::get_cursor(weld::TreeIter*) const
@ -498,10 +515,9 @@ void QtInstanceTreeView::set_id(const weld::TreeIter&, const OUString&)
assert(false && "Not implemented yet");
}
OUString QtInstanceTreeView::get_id(const weld::TreeIter&) const
OUString QtInstanceTreeView::get_id(const weld::TreeIter& rIter) const
{
assert(false && "Not implemented yet");
return OUString();
return get_id(modelIndex(rIter));
}
void QtInstanceTreeView::set_image(const weld::TreeIter&, const OUString&, int)
@ -774,6 +790,27 @@ QAbstractItemView::SelectionMode QtInstanceTreeView::mapSelectionMode(SelectionM
}
}
QModelIndex QtInstanceTreeView::modelIndex(int nPos) const { return m_pModel->index(nPos, 0); }
QModelIndex QtInstanceTreeView::modelIndex(const weld::TreeIter& rIter)
{
return static_cast<const QtInstanceTreeIter&>(rIter).m_aModelIndex;
}
OUString QtInstanceTreeView::get_id(const QModelIndex& rModelIndex) const
{
SolarMutexGuard g;
OUString sId;
GetQtInstance().RunInMainThread([&] {
QVariant aRoleData = m_pModel->data(rModelIndex, ROLE_ID);
if (aRoleData.canConvert<QString>())
sId = toOUString(aRoleData.toString());
});
return sId;
}
void QtInstanceTreeView::handleActivated()
{
SolarMutexGuard g;