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 <QQuickItem>
#include <QQueue>
#include <QPointF>
#include <QRectF>
#include <QQueue>
#include <QQuickWindow>
#include <QRectF>
namespace focusControlTools
{
bool isVisible(QObject *item)
{
const auto res = item->property("visible").toBool();
@ -55,11 +55,7 @@ QList<QObject*> getItemsChain(QObject* object)
const auto children = object->children();
for (const auto child : children) {
if (child
&& isFocusable(child)
&& isEnabled(child)
&& isVisible(child)
) {
if (child && isFocusable(child) && isEnabled(child) && isVisible(child)) {
res.append(child);
} else {
res.append(getItemsChain(child));
@ -77,21 +73,22 @@ void printItems(const QList<QObject*>& items, QObject* current_item)
qDebug() << prefix << " Item: " << i << " with coords: " << coords;
}
}
} // namespace focusControlTools
///////////////////////////////////////////////////////////////////////////////////////////////////
ListViewFocusController::ListViewFocusController(QQuickItem *listView, QObject *parent)
: QObject{parent}
, m_listView{listView}
, m_focusChain{}
, m_currentSection{Section::Default}
, m_header{nullptr}
, m_footer{nullptr}
, m_focusedItem{nullptr}
, m_focusedItemIndex{-1}
, m_delegateIndex{0}
, m_isReturnNeeded{false}
, m_currentSectionString {"Default", "Header", "Delegate", "Footer"}
: QObject { parent },
m_listView { listView },
m_focusChain {},
m_currentSection { Section::Default },
m_header { nullptr },
m_footer { nullptr },
m_focusedItem { nullptr },
m_focusedItemIndex { -1 },
m_delegateIndex { 0 },
m_isReturnNeeded { false },
m_currentSectionString { "Default", "Header", "Delegate", "Footer" }
{
QVariant headerItemProperty = m_listView->property("headerItem");
m_header = headerItemProperty.canConvert<QQuickItem *>() ? headerItemProperty.value<QQuickItem *>() : nullptr;
@ -102,14 +99,12 @@ ListViewFocusController::ListViewFocusController(QQuickItem* listView, QObject*
ListViewFocusController::~ListViewFocusController()
{
}
void ListViewFocusController::viewAtCurrentIndex() const
{
switch (m_currentSection) {
case Section::Default:
[[fallthrough]];
case Section::Default: [[fallthrough]];
case Section::Header: {
qDebug() << "===>> [FOCUS ON BEGINNING...]";
QMetaObject::invokeMethod(m_listView, "positionViewAtBeginning");
@ -117,8 +112,7 @@ void ListViewFocusController::viewAtCurrentIndex() const
}
case Section::Delegate: {
qDebug() << "===>> [FOCUS ON INDEX...]";
QMetaObject::invokeMethod(m_listView, "positionViewAtIndex",
Q_ARG(int, m_delegateIndex), // Index
QMetaObject::invokeMethod(m_listView, "positionViewAtIndex", Q_ARG(int, m_delegateIndex), // Index
Q_ARG(int, 2)); // PositionMode (0 = Visible)
break;
}
@ -128,7 +122,6 @@ void ListViewFocusController::viewAtCurrentIndex() const
break;
}
}
}
int ListViewFocusController::size() const
@ -241,9 +234,7 @@ QQuickItem* ListViewFocusController::itemAtIndex(const int index) const
{
QQuickItem *item { nullptr };
QMetaObject::invokeMethod(m_listView, "itemAtIndex",
Q_RETURN_ARG(QQuickItem*, item),
Q_ARG(int, index));
QMetaObject::invokeMethod(m_listView, "itemAtIndex", Q_RETURN_ARG(QQuickItem *, item), Q_ARG(int, index));
return item;
}
@ -285,7 +276,7 @@ void ListViewFocusController::focusNextItem()
return;
}
m_focusChain = getItemsChain(currentDelegate());
m_focusChain = focusControlTools::getItemsChain(currentDelegate());
if (m_focusChain.empty()) {
qWarning() << "No elements found in the delegate. Going to next delegate...";
@ -307,7 +298,7 @@ void ListViewFocusController::focusPreviousItem()
if (m_focusChain.empty()) {
qDebug() << "Empty focusChain with current delegate: " << currentDelegate() << "Scanning for elements...";
m_focusChain = getItemsChain(currentDelegate());
m_focusChain = focusControlTools::getItemsChain(currentDelegate());
}
if (m_focusChain.empty()) {
qWarning() << "No elements found in the delegate. Going to next delegate...";
@ -343,12 +334,12 @@ bool ListViewFocusController::isLastFocusItemInDelegate() const
bool ListViewFocusController::hasHeader() const
{
return m_header && !getItemsChain(m_header).isEmpty();
return m_header && !focusControlTools::getItemsChain(m_header).isEmpty();
}
bool ListViewFocusController::hasFooter() const
{
return m_footer && !getItemsChain(m_footer).isEmpty();
return m_footer && !focusControlTools::getItemsChain(m_footer).isEmpty();
}
bool ListViewFocusController::isFirstFocusItemInListView() const
@ -366,9 +357,7 @@ bool ListViewFocusController::isFirstFocusItemInListView() const
case Section::Default: {
return true;
}
default:
qWarning() << "Wrong section";
return true;
default: qWarning() << "Wrong section"; return true;
}
}
@ -387,9 +376,7 @@ bool ListViewFocusController::isLastFocusItemInListView() const
case Section::Footer: {
return isLastFocusItemInDelegate();
}
default:
qWarning() << "Wrong section";
return true;
default: qWarning() << "Wrong section"; return true;
}
}

View file

@ -1,12 +1,14 @@
#ifndef LISTVIEWFOCUSCONTROLLER_H
#define LISTVIEWFOCUSCONTROLLER_H
#include <QList>
#include <QObject>
#include <QStack>
#include <QSharedPointer>
#include <QQuickItem>
#include <QSharedPointer>
#include <QStack>
namespace focusControlTools
{
bool isEnabled(QObject *item);
bool isFocusable(QObject *item);
bool isMore(QObject *item1, QObject *item2);
@ -14,6 +16,7 @@ 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