diff --git a/client/ui/controllers/listViewFocusController.cpp b/client/ui/controllers/listViewFocusController.cpp index c26167d1..f6290869 100644 --- a/client/ui/controllers/listViewFocusController.cpp +++ b/client/ui/controllers/listViewFocusController.cpp @@ -1,115 +1,110 @@ #include "listViewFocusController.h" -#include -#include #include -#include +#include #include +#include - -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(item1)); - const auto p2 = getItemCenterPointOnScene(qobject_cast(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(obj); - return item && item->isEnabled(); -} - -QList getItemsChain(QObject* object) -{ - QList 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(item1)); + const auto p2 = getItemCenterPointOnScene(qobject_cast(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(obj); + return item && item->isEnabled(); + } + + QList getItemsChain(QObject *object) + { + QList 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 &items, QObject *current_item) + { + for (const auto &item : items) { + QQuickItem *i = qobject_cast(item); + QPointF coords { getItemCenterPointOnScene(i) }; + QString prefix = current_item == i ? "==>" : " "; + qDebug() << prefix << " Item: " << i << " with coords: " << coords; } } - return res; -} - -void printItems(const QList& items, QObject* current_item) -{ - for(const auto& item : items) { - QQuickItem* i = qobject_cast(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() ? headerItemProperty.value() : nullptr; + m_header = headerItemProperty.canConvert() ? headerItemProperty.value() : nullptr; QVariant footerItemProperty = m_listView->property("footerItem"); - m_footer = footerItemProperty.canConvert() ? footerItemProperty.value() : nullptr; + m_footer = footerItemProperty.canConvert() ? footerItemProperty.value() : 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(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(m_focusChain.at(m_focusedItemIndex)); + m_focusedItem = qobject_cast(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(m_focusChain.at(m_focusedItemIndex)); + m_focusedItem = qobject_cast(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; } } diff --git a/client/ui/controllers/listViewFocusController.h b/client/ui/controllers/listViewFocusController.h index df14b6be..86015022 100644 --- a/client/ui/controllers/listViewFocusController.h +++ b/client/ui/controllers/listViewFocusController.h @@ -1,19 +1,22 @@ #ifndef LISTVIEWFOCUSCONTROLLER_H #define LISTVIEWFOCUSCONTROLLER_H +#include #include -#include -#include #include +#include +#include +namespace focusControlTools +{ + bool isEnabled(QObject *item); + bool isFocusable(QObject *item); + bool isMore(QObject *item1, QObject *item2); + bool isLess(QObject *item1, QObject *item2); + QList getSubChain(QObject *object); -bool isEnabled(QObject* item); -bool isFocusable(QObject* item); -bool isMore(QObject* item1, QObject* item2); -bool isLess(QObject* item1, QObject* item2); -QList getSubChain(QObject* object); - -void printItems(const QList& items, QObject* current_item); + void printItems(const QList &items, QObject *current_item); +} /*! * \brief The ListViewFocusController class manages the focus of elements in ListView @@ -27,7 +30,7 @@ class ListViewFocusController : public QObject { Q_OBJECT public: - explicit ListViewFocusController(QQuickItem* listView, QObject* parent = nullptr); + explicit ListViewFocusController(QQuickItem *listView, QObject *parent = nullptr); ~ListViewFocusController(); void nextDelegate(); @@ -54,19 +57,19 @@ private: int currentIndex() const; void setDelegateIndex(int index); void viewAtCurrentIndex() const; - QQuickItem* itemAtIndex(const int index) const; - QQuickItem* currentDelegate() const; - QQuickItem* focusedItem() const; + QQuickItem *itemAtIndex(const int index) const; + QQuickItem *currentDelegate() const; + QQuickItem *focusedItem() const; bool hasHeader() const; bool hasFooter() const; - QQuickItem* m_listView; - QList m_focusChain; + QQuickItem *m_listView; + QList m_focusChain; Section m_currentSection; - QQuickItem* m_header; - QQuickItem* m_footer; - QQuickItem* m_focusedItem; // Pointer to focused item on Delegate + QQuickItem *m_header; + QQuickItem *m_footer; + QQuickItem *m_focusedItem; // Pointer to focused item on Delegate qsizetype m_focusedItemIndex; qsizetype m_delegateIndex; bool m_isReturnNeeded;