format ListViewFocusController

This commit is contained in:
Cyril Anisimov 2024-12-16 22:12:00 +01:00
parent 9620a24d6a
commit a57e94e4f2
2 changed files with 122 additions and 132 deletions

View file

@ -1,52 +1,52 @@
#include "listViewFocusController.h" #include "listViewFocusController.h"
#include <QQuickItem>
#include <QQueue>
#include <QPointF> #include <QPointF>
#include <QRectF> #include <QQueue>
#include <QQuickWindow> #include <QQuickWindow>
#include <QRectF>
namespace focusControlTools
bool isVisible(QObject* item)
{ {
bool isVisible(QObject *item)
{
const auto res = item->property("visible").toBool(); const auto res = item->property("visible").toBool();
return res; return res;
} }
bool isFocusable(QObject* item) bool isFocusable(QObject *item)
{ {
const auto res = item->property("isFocusable").toBool(); const auto res = item->property("isFocusable").toBool();
return res; return res;
} }
QPointF getItemCenterPointOnScene(QQuickItem* item) QPointF getItemCenterPointOnScene(QQuickItem *item)
{ {
const auto x0 = item->x() + (item->width() / 2); const auto x0 = item->x() + (item->width() / 2);
const auto y0 = item->y() + (item->height() / 2); const auto y0 = item->y() + (item->height() / 2);
return item->parentItem()->mapToScene(QPointF{x0, y0}); return item->parentItem()->mapToScene(QPointF { x0, y0 });
} }
bool isLess(QObject* item1, QObject* item2) bool isLess(QObject *item1, QObject *item2)
{ {
const auto p1 = getItemCenterPointOnScene(qobject_cast<QQuickItem*>(item1)); const auto p1 = getItemCenterPointOnScene(qobject_cast<QQuickItem *>(item1));
const auto p2 = getItemCenterPointOnScene(qobject_cast<QQuickItem*>(item2)); const auto p2 = getItemCenterPointOnScene(qobject_cast<QQuickItem *>(item2));
return (p1.y() == p2.y()) ? (p1.x() < p2.x()) : (p1.y() < p2.y()); return (p1.y() == p2.y()) ? (p1.x() < p2.x()) : (p1.y() < p2.y());
} }
bool isMore(QObject* item1, QObject* item2) bool isMore(QObject *item1, QObject *item2)
{ {
return !isLess(item1, item2); return !isLess(item1, item2);
} }
bool isEnabled(QObject* obj) bool isEnabled(QObject *obj)
{ {
const auto item = qobject_cast<QQuickItem*>(obj); const auto item = qobject_cast<QQuickItem *>(obj);
return item && item->isEnabled(); return item && item->isEnabled();
} }
QList<QObject*> getItemsChain(QObject* object) QList<QObject *> getItemsChain(QObject *object)
{ {
QList<QObject*> res; QList<QObject *> res;
if (!object) { if (!object) {
qDebug() << "The object is NULL"; qDebug() << "The object is NULL";
return res; return res;
@ -54,62 +54,57 @@ QList<QObject*> getItemsChain(QObject* object)
const auto children = object->children(); const auto children = object->children();
for(const auto child : children) { for (const auto child : children) {
if (child if (child && isFocusable(child) && isEnabled(child) && isVisible(child)) {
&& isFocusable(child)
&& isEnabled(child)
&& isVisible(child)
) {
res.append(child); res.append(child);
} else { } else {
res.append(getItemsChain(child)); res.append(getItemsChain(child));
} }
} }
return res; return res;
} }
void printItems(const QList<QObject*>& items, QObject* current_item) void printItems(const QList<QObject *> &items, QObject *current_item)
{ {
for(const auto& item : items) { for (const auto &item : items) {
QQuickItem* i = qobject_cast<QQuickItem*>(item); QQuickItem *i = qobject_cast<QQuickItem *>(item);
QPointF coords {getItemCenterPointOnScene(i)}; QPointF coords { getItemCenterPointOnScene(i) };
QString prefix = current_item == i ? "==>" : " "; QString prefix = current_item == i ? "==>" : " ";
qDebug() << prefix << " Item: " << i << " with coords: " << coords; qDebug() << prefix << " Item: " << i << " with coords: " << coords;
} }
} }
} // namespace focusControlTools
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
ListViewFocusController::ListViewFocusController(QQuickItem* listView, QObject* parent) ListViewFocusController::ListViewFocusController(QQuickItem *listView, QObject *parent)
: QObject{parent} : QObject { parent },
, m_listView{listView} m_listView { listView },
, m_focusChain{} m_focusChain {},
, m_currentSection{Section::Default} m_currentSection { Section::Default },
, m_header{nullptr} m_header { nullptr },
, m_footer{nullptr} m_footer { nullptr },
, m_focusedItem{nullptr} m_focusedItem { nullptr },
, m_focusedItemIndex{-1} m_focusedItemIndex { -1 },
, m_delegateIndex{0} m_delegateIndex { 0 },
, m_isReturnNeeded{false} m_isReturnNeeded { false },
, m_currentSectionString {"Default", "Header", "Delegate", "Footer"} m_currentSectionString { "Default", "Header", "Delegate", "Footer" }
{ {
QVariant headerItemProperty = m_listView->property("headerItem"); QVariant headerItemProperty = m_listView->property("headerItem");
m_header = headerItemProperty.canConvert<QQuickItem*>() ? headerItemProperty.value<QQuickItem*>() : nullptr; m_header = headerItemProperty.canConvert<QQuickItem *>() ? headerItemProperty.value<QQuickItem *>() : nullptr;
QVariant footerItemProperty = m_listView->property("footerItem"); QVariant footerItemProperty = m_listView->property("footerItem");
m_footer = footerItemProperty.canConvert<QQuickItem*>() ? footerItemProperty.value<QQuickItem*>() : nullptr; m_footer = footerItemProperty.canConvert<QQuickItem *>() ? footerItemProperty.value<QQuickItem *>() : nullptr;
} }
ListViewFocusController::~ListViewFocusController() ListViewFocusController::~ListViewFocusController()
{ {
} }
void ListViewFocusController::viewAtCurrentIndex() const void ListViewFocusController::viewAtCurrentIndex() const
{ {
switch(m_currentSection) { switch (m_currentSection) {
case Section::Default: case Section::Default: [[fallthrough]];
[[fallthrough]];
case Section::Header: { case Section::Header: {
qDebug() << "===>> [FOCUS ON BEGINNING...]"; qDebug() << "===>> [FOCUS ON BEGINNING...]";
QMetaObject::invokeMethod(m_listView, "positionViewAtBeginning"); QMetaObject::invokeMethod(m_listView, "positionViewAtBeginning");
@ -117,8 +112,7 @@ void ListViewFocusController::viewAtCurrentIndex() const
} }
case Section::Delegate: { case Section::Delegate: {
qDebug() << "===>> [FOCUS ON INDEX...]"; qDebug() << "===>> [FOCUS ON INDEX...]";
QMetaObject::invokeMethod(m_listView, "positionViewAtIndex", QMetaObject::invokeMethod(m_listView, "positionViewAtIndex", Q_ARG(int, m_delegateIndex), // Index
Q_ARG(int, m_delegateIndex), // Index
Q_ARG(int, 2)); // PositionMode (0 = Visible) Q_ARG(int, 2)); // PositionMode (0 = Visible)
break; break;
} }
@ -128,7 +122,6 @@ void ListViewFocusController::viewAtCurrentIndex() const
break; break;
} }
} }
} }
int ListViewFocusController::size() const int ListViewFocusController::size() const
@ -151,9 +144,9 @@ void ListViewFocusController::nextDelegate()
{ {
const auto sectionName = m_currentSectionString[static_cast<int>(m_currentSection)]; const auto sectionName = m_currentSectionString[static_cast<int>(m_currentSection)];
qDebug() << "===>> [nextDelegate... current section: " << sectionName << " ]"; qDebug() << "===>> [nextDelegate... current section: " << sectionName << " ]";
switch(m_currentSection) { switch (m_currentSection) {
case Section::Default: { case Section::Default: {
if(hasHeader()) { if (hasHeader()) {
m_currentSection = Section::Header; m_currentSection = Section::Header;
viewAtCurrentIndex(); viewAtCurrentIndex();
break; break;
@ -194,9 +187,9 @@ void ListViewFocusController::nextDelegate()
void ListViewFocusController::previousDelegate() void ListViewFocusController::previousDelegate()
{ {
switch(m_currentSection) { switch (m_currentSection) {
case Section::Default: { case Section::Default: {
if(hasFooter()) { if (hasFooter()) {
m_currentSection = Section::Footer; m_currentSection = Section::Footer;
break; break;
} }
@ -237,22 +230,20 @@ void ListViewFocusController::decrementIndex()
m_delegateIndex--; m_delegateIndex--;
} }
QQuickItem* ListViewFocusController::itemAtIndex(const int index) const QQuickItem *ListViewFocusController::itemAtIndex(const int index) const
{ {
QQuickItem* item{nullptr}; QQuickItem *item { nullptr };
QMetaObject::invokeMethod(m_listView, "itemAtIndex", QMetaObject::invokeMethod(m_listView, "itemAtIndex", Q_RETURN_ARG(QQuickItem *, item), Q_ARG(int, index));
Q_RETURN_ARG(QQuickItem*, item),
Q_ARG(int, index));
return item; return item;
} }
QQuickItem* ListViewFocusController::currentDelegate() const QQuickItem *ListViewFocusController::currentDelegate() const
{ {
QQuickItem* result{nullptr}; QQuickItem *result { nullptr };
switch(m_currentSection) { switch (m_currentSection) {
case Section::Default: { case Section::Default: {
qWarning() << "No elements..."; qWarning() << "No elements...";
break; break;
@ -273,7 +264,7 @@ QQuickItem* ListViewFocusController::currentDelegate() const
return result; return result;
} }
QQuickItem* ListViewFocusController::focusedItem() const QQuickItem *ListViewFocusController::focusedItem() const
{ {
return m_focusedItem; return m_focusedItem;
} }
@ -285,7 +276,7 @@ void ListViewFocusController::focusNextItem()
return; return;
} }
m_focusChain = getItemsChain(currentDelegate()); m_focusChain = focusControlTools::getItemsChain(currentDelegate());
if (m_focusChain.empty()) { if (m_focusChain.empty()) {
qWarning() << "No elements found in the delegate. Going to next delegate..."; qWarning() << "No elements found in the delegate. Going to next delegate...";
@ -294,7 +285,7 @@ void ListViewFocusController::focusNextItem()
return; return;
} }
m_focusedItemIndex++; m_focusedItemIndex++;
m_focusedItem = qobject_cast<QQuickItem*>(m_focusChain.at(m_focusedItemIndex)); m_focusedItem = qobject_cast<QQuickItem *>(m_focusChain.at(m_focusedItemIndex));
qDebug() << "==>> [ Focused Item: " << m_focusedItem << " with Index: " << m_focusedItemIndex << " ]"; qDebug() << "==>> [ Focused Item: " << m_focusedItem << " with Index: " << m_focusedItemIndex << " ]";
m_focusedItem->forceActiveFocus(Qt::TabFocusReason); m_focusedItem->forceActiveFocus(Qt::TabFocusReason);
} }
@ -307,7 +298,7 @@ void ListViewFocusController::focusPreviousItem()
if (m_focusChain.empty()) { if (m_focusChain.empty()) {
qDebug() << "Empty focusChain with current delegate: " << currentDelegate() << "Scanning for elements..."; qDebug() << "Empty focusChain with current delegate: " << currentDelegate() << "Scanning for elements...";
m_focusChain = getItemsChain(currentDelegate()); m_focusChain = focusControlTools::getItemsChain(currentDelegate());
} }
if (m_focusChain.empty()) { if (m_focusChain.empty()) {
qWarning() << "No elements found in the delegate. Going to next delegate..."; qWarning() << "No elements found in the delegate. Going to next delegate...";
@ -319,7 +310,7 @@ void ListViewFocusController::focusPreviousItem()
m_focusedItemIndex = m_focusChain.size(); m_focusedItemIndex = m_focusChain.size();
} }
m_focusedItemIndex--; m_focusedItemIndex--;
m_focusedItem = qobject_cast<QQuickItem*>(m_focusChain.at(m_focusedItemIndex)); m_focusedItem = qobject_cast<QQuickItem *>(m_focusChain.at(m_focusedItemIndex));
qDebug() << "==>> [ Focused Item: " << m_focusedItem << " with Index: " << m_focusedItemIndex << " ]"; qDebug() << "==>> [ Focused Item: " << m_focusedItem << " with Index: " << m_focusedItemIndex << " ]";
m_focusedItem->forceActiveFocus(Qt::TabFocusReason); m_focusedItem->forceActiveFocus(Qt::TabFocusReason);
} }
@ -343,12 +334,12 @@ bool ListViewFocusController::isLastFocusItemInDelegate() const
bool ListViewFocusController::hasHeader() const bool ListViewFocusController::hasHeader() const
{ {
return m_header && !getItemsChain(m_header).isEmpty(); return m_header && !focusControlTools::getItemsChain(m_header).isEmpty();
} }
bool ListViewFocusController::hasFooter() const bool ListViewFocusController::hasFooter() const
{ {
return m_footer && !getItemsChain(m_footer).isEmpty(); return m_footer && !focusControlTools::getItemsChain(m_footer).isEmpty();
} }
bool ListViewFocusController::isFirstFocusItemInListView() const bool ListViewFocusController::isFirstFocusItemInListView() const
@ -366,9 +357,7 @@ bool ListViewFocusController::isFirstFocusItemInListView() const
case Section::Default: { case Section::Default: {
return true; return true;
} }
default: default: qWarning() << "Wrong section"; return true;
qWarning() << "Wrong section";
return true;
} }
} }
@ -387,9 +376,7 @@ bool ListViewFocusController::isLastFocusItemInListView() const
case Section::Footer: { case Section::Footer: {
return isLastFocusItemInDelegate(); return isLastFocusItemInDelegate();
} }
default: default: qWarning() << "Wrong section"; return true;
qWarning() << "Wrong section";
return true;
} }
} }

View file

@ -1,19 +1,22 @@
#ifndef LISTVIEWFOCUSCONTROLLER_H #ifndef LISTVIEWFOCUSCONTROLLER_H
#define LISTVIEWFOCUSCONTROLLER_H #define LISTVIEWFOCUSCONTROLLER_H
#include <QList>
#include <QObject> #include <QObject>
#include <QStack>
#include <QSharedPointer>
#include <QQuickItem> #include <QQuickItem>
#include <QSharedPointer>
#include <QStack>
namespace focusControlTools
{
bool isEnabled(QObject *item);
bool isFocusable(QObject *item);
bool isMore(QObject *item1, QObject *item2);
bool isLess(QObject *item1, QObject *item2);
QList<QObject *> getSubChain(QObject *object);
bool isEnabled(QObject* item); void printItems(const QList<QObject *> &items, QObject *current_item);
bool isFocusable(QObject* item); }
bool isMore(QObject* item1, QObject* item2);
bool isLess(QObject* item1, QObject* item2);
QList<QObject*> getSubChain(QObject* object);
void printItems(const QList<QObject*>& items, QObject* current_item);
/*! /*!
* \brief The ListViewFocusController class manages the focus of elements in ListView * \brief The ListViewFocusController class manages the focus of elements in ListView
@ -27,7 +30,7 @@ class ListViewFocusController : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ListViewFocusController(QQuickItem* listView, QObject* parent = nullptr); explicit ListViewFocusController(QQuickItem *listView, QObject *parent = nullptr);
~ListViewFocusController(); ~ListViewFocusController();
void nextDelegate(); void nextDelegate();
@ -54,19 +57,19 @@ private:
int currentIndex() const; int currentIndex() const;
void setDelegateIndex(int index); void setDelegateIndex(int index);
void viewAtCurrentIndex() const; void viewAtCurrentIndex() const;
QQuickItem* itemAtIndex(const int index) const; QQuickItem *itemAtIndex(const int index) const;
QQuickItem* currentDelegate() const; QQuickItem *currentDelegate() const;
QQuickItem* focusedItem() const; QQuickItem *focusedItem() const;
bool hasHeader() const; bool hasHeader() const;
bool hasFooter() const; bool hasFooter() const;
QQuickItem* m_listView; QQuickItem *m_listView;
QList<QObject*> m_focusChain; QList<QObject *> m_focusChain;
Section m_currentSection; Section m_currentSection;
QQuickItem* m_header; QQuickItem *m_header;
QQuickItem* m_footer; QQuickItem *m_footer;
QQuickItem* m_focusedItem; // Pointer to focused item on Delegate QQuickItem *m_focusedItem; // Pointer to focused item on Delegate
qsizetype m_focusedItemIndex; qsizetype m_focusedItemIndex;
qsizetype m_delegateIndex; qsizetype m_delegateIndex;
bool m_isReturnNeeded; bool m_isReturnNeeded;