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,12 +1,12 @@
#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();
@ -55,11 +55,7 @@ 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));
@ -77,21 +73,22 @@ void printItems(const QList<QObject*>& items, QObject* current_item)
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;
@ -102,14 +99,12 @@ ListViewFocusController::ListViewFocusController(QQuickItem* listView, QObject*
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
@ -241,9 +234,7 @@ 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;
} }
@ -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...";
@ -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...";
@ -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,12 +1,14 @@
#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 isEnabled(QObject *item);
bool isFocusable(QObject *item); bool isFocusable(QObject *item);
bool isMore(QObject *item1, QObject *item2); bool isMore(QObject *item1, QObject *item2);
@ -14,6 +16,7 @@ bool isLess(QObject* item1, QObject* item2);
QList<QObject *> getSubChain(QObject *object); QList<QObject *> getSubChain(QObject *object);
void printItems(const QList<QObject *> &items, QObject *current_item); 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