Skip to content

Commit

Permalink
refactor listViewFocusController
Browse files Browse the repository at this point in the history
  • Loading branch information
CyAn84 committed Oct 30, 2024
1 parent af411af commit 7b0713b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
70 changes: 34 additions & 36 deletions client/ui/controllers/listViewFocusController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,25 @@ ListViewFocusController::~ListViewFocusController()

}

void ListViewFocusController::viewAtCurrentIndex()
void ListViewFocusController::viewAtCurrentIndex() const
{
switch(m_currentSection) {
case Section::Default:
[[fallthrough]];
case Section::Header: {
qDebug() << "===>> [FOCUS ON BEGINNING...]";
QMetaObject::invokeMethod(m_listView, "positionViewAtBeginning");
break;
}
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)
break;
}
case Section::Footer: {
qDebug() << "===>> [FOCUS ON END...]";
QMetaObject::invokeMethod(m_listView, "positionViewAtEnd");
break;
}
Expand All @@ -140,35 +143,40 @@ int ListViewFocusController::currentIndex() const

void ListViewFocusController::nextDelegate()
{
const auto sectionName = m_currentSectionString[static_cast<int>(m_currentSection)];
qDebug() << "===>> [nextDelegate... current section: " << sectionName << " ]";
switch(m_currentSection) {
case Section::Default: {
if(hasHeader()) {
m_currentSection = Section::Header;
viewToBegin();
viewAtCurrentIndex();
break;
}
[[fallthrough]];
}
case Section::Header: {
if (size() > 0) {
m_currentSection = Section::Delegate;
viewAtCurrentIndex();
break;
}
[[fallthrough]];
}
case Section::Delegate:
if (m_delegateIndex < (size() - 1)) {
m_delegateIndex++;
viewAtCurrentIndex();
break;
} else if (hasFooter()) {
m_currentSection = Section::Footer;
viewToEnd();
viewAtCurrentIndex();
break;
}
[[fallthrough]];
case Section::Footer: {
m_isReturnNeeded = true;
m_currentSection = Section::Default;
viewAtCurrentIndex();
break;
}
default: {
Expand Down Expand Up @@ -223,7 +231,7 @@ void ListViewFocusController::decrementIndex()
m_delegateIndex--;
}

QQuickItem* ListViewFocusController::itemAtIndex(const int index)
QQuickItem* ListViewFocusController::itemAtIndex(const int index) const
{
QQuickItem* item{nullptr};

Expand All @@ -234,7 +242,7 @@ QQuickItem* ListViewFocusController::itemAtIndex(const int index)
return item;
}

QQuickItem* ListViewFocusController::currentDelegate()
QQuickItem* ListViewFocusController::currentDelegate() const
{
QQuickItem* result{nullptr};

Expand All @@ -259,15 +267,15 @@ QQuickItem* ListViewFocusController::currentDelegate()
return result;
}

QQuickItem* ListViewFocusController::focusedItem()
QQuickItem* ListViewFocusController::focusedItem() const
{
return m_focusedItem;
}

void ListViewFocusController::focusNextItem()
{
if (m_isReturnNeeded) {
qDebug() << "===>> RETURN IS NEEDED...";
qDebug() << "===>> [ RETURN IS NEEDED... ]";
return;
}

Expand All @@ -283,8 +291,8 @@ void ListViewFocusController::focusNextItem()
}
m_focusedItemIndex++;
m_focusedItem = qobject_cast<QQuickItem*>(m_focusChain.at(m_focusedItemIndex));
qDebug() << "==>> Focused Item: " << m_focusedItem << " with Index: " << m_focusedItemIndex;
m_focusedItem->forceActiveFocus();
qDebug() << "==>> [ Focused Item: " << m_focusedItem << " with Index: " << m_focusedItemIndex << " ]";
m_focusedItem->forceActiveFocus(Qt::TabFocusReason);
}

void ListViewFocusController::focusPreviousItem()
Expand All @@ -308,8 +316,8 @@ void ListViewFocusController::focusPreviousItem()
}
m_focusedItemIndex--;
m_focusedItem = qobject_cast<QQuickItem*>(m_focusChain.at(m_focusedItemIndex));
qDebug() << "==>> Focused Item: " << m_focusedItem << " with Index: " << m_focusedItemIndex;
m_focusedItem->forceActiveFocus();
qDebug() << "==>> [ Focused Item: " << m_focusedItem << " with Index: " << m_focusedItemIndex << " ]";
m_focusedItem->forceActiveFocus(Qt::TabFocusReason);
}

void ListViewFocusController::resetFocusChain()
Expand All @@ -319,17 +327,27 @@ void ListViewFocusController::resetFocusChain()
m_focusedItemIndex = -1;
}

bool ListViewFocusController::isFirstFocusItemInDelegate()
bool ListViewFocusController::isFirstFocusItemInDelegate() const
{
return m_focusedItem && (m_focusedItem == m_focusChain.first());
}

bool ListViewFocusController::isLastFocusItemInDelegate()
bool ListViewFocusController::isLastFocusItemInDelegate() const
{
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) {
case Section::Footer: {
Expand All @@ -350,17 +368,7 @@ bool ListViewFocusController::isFirstFocusItemInListView()
}
}

bool ListViewFocusController::hasHeader()
{
return m_header && !getItemsChain(m_header).isEmpty();
}

bool ListViewFocusController::hasFooter()
{
return m_footer && !getItemsChain(m_footer).isEmpty();
}

bool ListViewFocusController::isLastFocusItemInListView()
bool ListViewFocusController::isLastFocusItemInListView() const
{
switch (m_currentSection) {
case Section::Default: {
Expand All @@ -381,17 +389,7 @@ bool ListViewFocusController::isLastFocusItemInListView()
}
}

bool ListViewFocusController::isReturnNeeded()
bool ListViewFocusController::isReturnNeeded() const
{
return m_isReturnNeeded;
}

void ListViewFocusController::viewToBegin()
{
QMetaObject::invokeMethod(m_listView, "positionViewAtBeginning", Qt::AutoConnection);
}

void ListViewFocusController::viewToEnd()
{
QMetaObject::invokeMethod(m_listView, "positionViewAtEnd", Qt::AutoConnection);
}
26 changes: 12 additions & 14 deletions client/ui/controllers/listViewFocusController.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void printItems(const QList<QObject*>& items, QObject* current_item);
*/
class ListViewFocusController : public QObject
{
// Q_OBJECT
Q_OBJECT
public:
explicit ListViewFocusController(QQuickItem* listView, QObject* parent = nullptr);
~ListViewFocusController();
Expand All @@ -36,14 +36,11 @@ class ListViewFocusController : public QObject
void focusNextItem();
void focusPreviousItem();
void resetFocusChain();
bool isFirstFocusItemInListView();
bool isFirstFocusItemInDelegate();
bool isLastFocusItemInListView();
bool isLastFocusItemInDelegate();
bool isReturnNeeded();
void viewToBegin();
void viewToEnd();
void viewAtCurrentIndex();
bool isFirstFocusItemInListView() const;
bool isFirstFocusItemInDelegate() const;
bool isLastFocusItemInListView() const;
bool isLastFocusItemInDelegate() const;
bool isReturnNeeded() const;

private:
enum class Section {
Expand All @@ -55,12 +52,13 @@ class ListViewFocusController : public QObject

int size() const;
int currentIndex() const;
QQuickItem* itemAtIndex(const int index);
QQuickItem* currentDelegate();
QQuickItem* focusedItem();
void viewAtCurrentIndex() const;
QQuickItem* itemAtIndex(const int index) const;
QQuickItem* currentDelegate() const;
QQuickItem* focusedItem() const;

bool hasHeader();
bool hasFooter();
bool hasHeader() const;
bool hasFooter() const;

QQuickItem* m_listView;
QList<QObject*> m_focusChain;
Expand Down

0 comments on commit 7b0713b

Please sign in to comment.