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,115 +1,110 @@
#include "listViewFocusController.h"
#include <QQuickItem>
#include <QQueue>
#include <QPointF>
#include <QRectF>
#include <QQueue>
#include <QQuickWindow>
#include <QRectF>
bool isVisible(QObject* item)
namespace focusControlTools
{
const auto res = item->property("visible").toBool();
return res;
}
bool isFocusable(QObject* item)
{
const auto res = item->property("isFocusable").toBool();
return res;
}
QPointF getItemCenterPointOnScene(QQuickItem* item)
{
const auto x0 = item->x() + (item->width() / 2);
const auto y0 = item->y() + (item->height() / 2);
return item->parentItem()->mapToScene(QPointF{x0, y0});
}
bool isLess(QObject* item1, QObject* item2)
{
const auto p1 = getItemCenterPointOnScene(qobject_cast<QQuickItem*>(item1));
const auto p2 = getItemCenterPointOnScene(qobject_cast<QQuickItem*>(item2));
return (p1.y() == p2.y()) ? (p1.x() < p2.x()) : (p1.y() < p2.y());
}
bool isMore(QObject* item1, QObject* item2)
{
return !isLess(item1, item2);
}
bool isEnabled(QObject* obj)
{
const auto item = qobject_cast<QQuickItem*>(obj);
return item && item->isEnabled();
}
QList<QObject*> getItemsChain(QObject* object)
{
QList<QObject*> res;
if (!object) {
qDebug() << "The object is NULL";
bool isVisible(QObject *item)
{
const auto res = item->property("visible").toBool();
return res;
}
const auto children = object->children();
bool isFocusable(QObject *item)
{
const auto res = item->property("isFocusable").toBool();
return res;
}
for(const auto child : children) {
if (child
&& isFocusable(child)
&& isEnabled(child)
&& isVisible(child)
) {
res.append(child);
} else {
res.append(getItemsChain(child));
QPointF getItemCenterPointOnScene(QQuickItem *item)
{
const auto x0 = item->x() + (item->width() / 2);
const auto y0 = item->y() + (item->height() / 2);
return item->parentItem()->mapToScene(QPointF { x0, y0 });
}
bool isLess(QObject *item1, QObject *item2)
{
const auto p1 = getItemCenterPointOnScene(qobject_cast<QQuickItem *>(item1));
const auto p2 = getItemCenterPointOnScene(qobject_cast<QQuickItem *>(item2));
return (p1.y() == p2.y()) ? (p1.x() < p2.x()) : (p1.y() < p2.y());
}
bool isMore(QObject *item1, QObject *item2)
{
return !isLess(item1, item2);
}
bool isEnabled(QObject *obj)
{
const auto item = qobject_cast<QQuickItem *>(obj);
return item && item->isEnabled();
}
QList<QObject *> getItemsChain(QObject *object)
{
QList<QObject *> res;
if (!object) {
qDebug() << "The object is NULL";
return res;
}
const auto children = object->children();
for (const auto child : children) {
if (child && isFocusable(child) && isEnabled(child) && isVisible(child)) {
res.append(child);
} else {
res.append(getItemsChain(child));
}
}
return res;
}
void printItems(const QList<QObject *> &items, QObject *current_item)
{
for (const auto &item : items) {
QQuickItem *i = qobject_cast<QQuickItem *>(item);
QPointF coords { getItemCenterPointOnScene(i) };
QString prefix = current_item == i ? "==>" : " ";
qDebug() << prefix << " Item: " << i << " with coords: " << coords;
}
}
return res;
}
void printItems(const QList<QObject*>& items, QObject* current_item)
{
for(const auto& item : items) {
QQuickItem* i = qobject_cast<QQuickItem*>(item);
QPointF coords {getItemCenterPointOnScene(i)};
QString prefix = current_item == i ? "==>" : " ";
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"}
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" }
{
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");
m_footer = footerItemProperty.canConvert<QQuickItem*>() ? footerItemProperty.value<QQuickItem*>() : nullptr;
m_footer = footerItemProperty.canConvert<QQuickItem *>() ? footerItemProperty.value<QQuickItem *>() : nullptr;
}
ListViewFocusController::~ListViewFocusController()
{
}
void ListViewFocusController::viewAtCurrentIndex() const
{
switch(m_currentSection) {
case Section::Default:
[[fallthrough]];
switch (m_currentSection) {
case Section::Default: [[fallthrough]];
case Section::Header: {
qDebug() << "===>> [FOCUS ON BEGINNING...]";
QMetaObject::invokeMethod(m_listView, "positionViewAtBeginning");
@ -117,9 +112,8 @@ void ListViewFocusController::viewAtCurrentIndex() const
}
case Section::Delegate: {
qDebug() << "===>> [FOCUS ON INDEX...]";
QMetaObject::invokeMethod(m_listView, "positionViewAtIndex",
Q_ARG(int, m_delegateIndex), // Index
Q_ARG(int, 2)); // PositionMode (0 = Visible)
QMetaObject::invokeMethod(m_listView, "positionViewAtIndex", Q_ARG(int, m_delegateIndex), // Index
Q_ARG(int, 2)); // PositionMode (0 = Visible)
break;
}
case Section::Footer: {
@ -128,7 +122,6 @@ void ListViewFocusController::viewAtCurrentIndex() const
break;
}
}
}
int ListViewFocusController::size() const
@ -151,9 +144,9 @@ void ListViewFocusController::nextDelegate()
{
const auto sectionName = m_currentSectionString[static_cast<int>(m_currentSection)];
qDebug() << "===>> [nextDelegate... current section: " << sectionName << " ]";
switch(m_currentSection) {
switch (m_currentSection) {
case Section::Default: {
if(hasHeader()) {
if (hasHeader()) {
m_currentSection = Section::Header;
viewAtCurrentIndex();
break;
@ -194,9 +187,9 @@ void ListViewFocusController::nextDelegate()
void ListViewFocusController::previousDelegate()
{
switch(m_currentSection) {
switch (m_currentSection) {
case Section::Default: {
if(hasFooter()) {
if (hasFooter()) {
m_currentSection = Section::Footer;
break;
}
@ -237,22 +230,20 @@ void ListViewFocusController::decrementIndex()
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",
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;
}
QQuickItem* ListViewFocusController::currentDelegate() const
QQuickItem *ListViewFocusController::currentDelegate() const
{
QQuickItem* result{nullptr};
QQuickItem *result { nullptr };
switch(m_currentSection) {
switch (m_currentSection) {
case Section::Default: {
qWarning() << "No elements...";
break;
@ -273,7 +264,7 @@ QQuickItem* ListViewFocusController::currentDelegate() const
return result;
}
QQuickItem* ListViewFocusController::focusedItem() const
QQuickItem *ListViewFocusController::focusedItem() const
{
return m_focusedItem;
}
@ -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...";
@ -294,7 +285,7 @@ void ListViewFocusController::focusNextItem()
return;
}
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 << " ]";
m_focusedItem->forceActiveFocus(Qt::TabFocusReason);
}
@ -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...";
@ -319,7 +310,7 @@ void ListViewFocusController::focusPreviousItem()
m_focusedItemIndex = m_focusChain.size();
}
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 << " ]";
m_focusedItem->forceActiveFocus(Qt::TabFocusReason);
}
@ -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;
}
}