refactor listViewFocusController

This commit is contained in:
Cyril Anisimov 2024-10-30 07:46:22 +01:00
parent f020bdb6e8
commit dc6c1cdf49
2 changed files with 46 additions and 50 deletions

View file

@ -105,22 +105,25 @@ ListViewFocusController::~ListViewFocusController()
} }
void ListViewFocusController::viewAtCurrentIndex() 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...]";
QMetaObject::invokeMethod(m_listView, "positionViewAtBeginning"); QMetaObject::invokeMethod(m_listView, "positionViewAtBeginning");
break; break;
} }
case Section::Delegate: { case Section::Delegate: {
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;
} }
case Section::Footer: { case Section::Footer: {
qDebug() << "===>> [FOCUS ON END...]";
QMetaObject::invokeMethod(m_listView, "positionViewAtEnd"); QMetaObject::invokeMethod(m_listView, "positionViewAtEnd");
break; break;
} }
@ -140,11 +143,13 @@ int ListViewFocusController::currentIndex() const
void ListViewFocusController::nextDelegate() 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: { case Section::Default: {
if(hasHeader()) { if(hasHeader()) {
m_currentSection = Section::Header; m_currentSection = Section::Header;
viewToBegin(); viewAtCurrentIndex();
break; break;
} }
[[fallthrough]]; [[fallthrough]];
@ -152,6 +157,7 @@ void ListViewFocusController::nextDelegate()
case Section::Header: { case Section::Header: {
if (size() > 0) { if (size() > 0) {
m_currentSection = Section::Delegate; m_currentSection = Section::Delegate;
viewAtCurrentIndex();
break; break;
} }
[[fallthrough]]; [[fallthrough]];
@ -159,16 +165,18 @@ void ListViewFocusController::nextDelegate()
case Section::Delegate: case Section::Delegate:
if (m_delegateIndex < (size() - 1)) { if (m_delegateIndex < (size() - 1)) {
m_delegateIndex++; m_delegateIndex++;
viewAtCurrentIndex();
break; break;
} else if (hasFooter()) { } else if (hasFooter()) {
m_currentSection = Section::Footer; m_currentSection = Section::Footer;
viewToEnd(); viewAtCurrentIndex();
break; break;
} }
[[fallthrough]]; [[fallthrough]];
case Section::Footer: { case Section::Footer: {
m_isReturnNeeded = true; m_isReturnNeeded = true;
m_currentSection = Section::Default; m_currentSection = Section::Default;
viewAtCurrentIndex();
break; break;
} }
default: { default: {
@ -223,7 +231,7 @@ void ListViewFocusController::decrementIndex()
m_delegateIndex--; m_delegateIndex--;
} }
QQuickItem* ListViewFocusController::itemAtIndex(const int index) QQuickItem* ListViewFocusController::itemAtIndex(const int index) const
{ {
QQuickItem* item{nullptr}; QQuickItem* item{nullptr};
@ -234,7 +242,7 @@ QQuickItem* ListViewFocusController::itemAtIndex(const int index)
return item; return item;
} }
QQuickItem* ListViewFocusController::currentDelegate() QQuickItem* ListViewFocusController::currentDelegate() const
{ {
QQuickItem* result{nullptr}; QQuickItem* result{nullptr};
@ -259,7 +267,7 @@ QQuickItem* ListViewFocusController::currentDelegate()
return result; return result;
} }
QQuickItem* ListViewFocusController::focusedItem() QQuickItem* ListViewFocusController::focusedItem() const
{ {
return m_focusedItem; return m_focusedItem;
} }
@ -267,7 +275,7 @@ QQuickItem* ListViewFocusController::focusedItem()
void ListViewFocusController::focusNextItem() void ListViewFocusController::focusNextItem()
{ {
if (m_isReturnNeeded) { if (m_isReturnNeeded) {
qDebug() << "===>> RETURN IS NEEDED..."; qDebug() << "===>> [ RETURN IS NEEDED... ]";
return; return;
} }
@ -283,8 +291,8 @@ void ListViewFocusController::focusNextItem()
} }
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(); m_focusedItem->forceActiveFocus(Qt::TabFocusReason);
} }
void ListViewFocusController::focusPreviousItem() void ListViewFocusController::focusPreviousItem()
@ -308,8 +316,8 @@ void ListViewFocusController::focusPreviousItem()
} }
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(); m_focusedItem->forceActiveFocus(Qt::TabFocusReason);
} }
void ListViewFocusController::resetFocusChain() void ListViewFocusController::resetFocusChain()
@ -319,17 +327,27 @@ void ListViewFocusController::resetFocusChain()
m_focusedItemIndex = -1; m_focusedItemIndex = -1;
} }
bool ListViewFocusController::isFirstFocusItemInDelegate() bool ListViewFocusController::isFirstFocusItemInDelegate() const
{ {
return m_focusedItem && (m_focusedItem == m_focusChain.first()); return m_focusedItem && (m_focusedItem == m_focusChain.first());
} }
bool ListViewFocusController::isLastFocusItemInDelegate() bool ListViewFocusController::isLastFocusItemInDelegate() const
{ {
return m_focusedItem && (m_focusedItem == m_focusChain.last()); return m_focusedItem && (m_focusedItem == m_focusChain.last());
} }
bool ListViewFocusController::isFirstFocusItemInListView() bool ListViewFocusController::hasHeader() const
{
return m_header && !getItemsChain(m_header).isEmpty();
}
bool ListViewFocusController::hasFooter() const
{
return m_footer && !getItemsChain(m_footer).isEmpty();
}
bool ListViewFocusController::isFirstFocusItemInListView() const
{ {
switch (m_currentSection) { switch (m_currentSection) {
case Section::Footer: { case Section::Footer: {
@ -350,17 +368,7 @@ bool ListViewFocusController::isFirstFocusItemInListView()
} }
} }
bool ListViewFocusController::hasHeader() bool ListViewFocusController::isLastFocusItemInListView() const
{
return m_header && !getItemsChain(m_header).isEmpty();
}
bool ListViewFocusController::hasFooter()
{
return m_footer && !getItemsChain(m_footer).isEmpty();
}
bool ListViewFocusController::isLastFocusItemInListView()
{ {
switch (m_currentSection) { switch (m_currentSection) {
case Section::Default: { case Section::Default: {
@ -381,17 +389,7 @@ bool ListViewFocusController::isLastFocusItemInListView()
} }
} }
bool ListViewFocusController::isReturnNeeded() bool ListViewFocusController::isReturnNeeded() const
{ {
return m_isReturnNeeded; return m_isReturnNeeded;
} }
void ListViewFocusController::viewToBegin()
{
QMetaObject::invokeMethod(m_listView, "positionViewAtBeginning", Qt::AutoConnection);
}
void ListViewFocusController::viewToEnd()
{
QMetaObject::invokeMethod(m_listView, "positionViewAtEnd", Qt::AutoConnection);
}

View file

@ -25,7 +25,7 @@ void printItems(const QList<QObject*>& items, QObject* current_item);
*/ */
class ListViewFocusController : public QObject 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();
@ -36,14 +36,11 @@ public:
void focusNextItem(); void focusNextItem();
void focusPreviousItem(); void focusPreviousItem();
void resetFocusChain(); void resetFocusChain();
bool isFirstFocusItemInListView(); bool isFirstFocusItemInListView() const;
bool isFirstFocusItemInDelegate(); bool isFirstFocusItemInDelegate() const;
bool isLastFocusItemInListView(); bool isLastFocusItemInListView() const;
bool isLastFocusItemInDelegate(); bool isLastFocusItemInDelegate() const;
bool isReturnNeeded(); bool isReturnNeeded() const;
void viewToBegin();
void viewToEnd();
void viewAtCurrentIndex();
private: private:
enum class Section { enum class Section {
@ -55,12 +52,13 @@ private:
int size() const; int size() const;
int currentIndex() const; int currentIndex() const;
QQuickItem* itemAtIndex(const int index); void viewAtCurrentIndex() const;
QQuickItem* currentDelegate(); QQuickItem* itemAtIndex(const int index) const;
QQuickItem* focusedItem(); QQuickItem* currentDelegate() const;
QQuickItem* focusedItem() const;
bool hasHeader(); bool hasHeader() const;
bool hasFooter(); bool hasFooter() const;
QQuickItem* m_listView; QQuickItem* m_listView;
QList<QObject*> m_focusChain; QList<QObject*> m_focusChain;