fix ListView navigation

This commit is contained in:
Cyril Anisimov 2024-10-17 03:52:57 +02:00
parent f3df9eb5f5
commit 89ac585e07
5 changed files with 117 additions and 81 deletions

View file

@ -6,6 +6,67 @@
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
bool isListView(QObject* item)
{
return item->inherits("QQuickListView");
}
bool isOnTheScene(QObject* object)
{
QQuickItem* item = qobject_cast<QQuickItem*>(object);
if (!item) {
qWarning() << "Couldn't recognize object as item";
return false;
}
if (!item->isVisible()) {
// qDebug() << "===>> The item is not visible: " << item;
return false;
}
QRectF itemRect = item->mapRectToScene(item->childrenRect());
QQuickWindow* window = item->window();
if (!window) {
qWarning() << "Couldn't get the window on the Scene check";
return false;
}
const auto contentItem = window->contentItem();
if (!contentItem) {
qWarning() << "Couldn't get the content item on the Scene check";
return false;
}
QRectF windowRect = contentItem->childrenRect();
const auto res = (windowRect.contains(itemRect) || isListView(item));
// qDebug() << (res ? "===>> item is inside the Scene" : "===>> ITEM IS OUTSIDE THE SCENE") << " itemRect: " << itemRect << "; windowRect: " << windowRect;
return res;
}
QList<QObject*> getSubChain(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)
&& isOnTheScene(child)
&& isEnabled(child)
) {
res.append(child);
} else {
res.append(getSubChain(child));
}
}
return res;
}
FocusController::FocusController(QQmlApplicationEngine* engine, QObject *parent) FocusController::FocusController(QQmlApplicationEngine* engine, QObject *parent)
: QObject{parent} : QObject{parent}
, m_engine{engine} , m_engine{engine}
@ -25,16 +86,16 @@ FocusController::FocusController(QQmlApplicationEngine* engine, QObject *parent)
}); });
} }
void FocusController::nextItem(bool isForwardOrder) void FocusController::nextItem(Direction direction)
{ {
if (m_lvfc) { if (m_lvfc) {
isForwardOrder ? focusNextListViewItem() : focusPreviousListViewItem(); direction == Direction::Forward ? focusNextListViewItem() : focusPreviousListViewItem();
qDebug() << "===>> [handling the ListView]"; qDebug() << "===>> [handling the ListView]";
return; return;
} }
reload(isForwardOrder); reload(direction);
if(m_focusChain.empty()) { if(m_focusChain.empty()) {
qWarning() << "There are no items to navigate"; qWarning() << "There are no items to navigate";
@ -60,13 +121,13 @@ void FocusController::nextItem(bool isForwardOrder)
if(isListView(m_focusedItem)) { if(isListView(m_focusedItem)) {
qDebug() << "===>> [Found ListView]"; qDebug() << "===>> [Found ListView]";
m_lvfc = new ListViewFocusController(m_focusedItem, this); m_lvfc = new ListViewFocusController(m_focusedItem, this);
if(isForwardOrder) { if(direction == Direction::Forward) {
m_lvfc->viewToBegin(); m_lvfc->viewToBegin();
m_lvfc->nextElement(); m_lvfc->nextDelegate();
focusNextListViewItem(); focusNextListViewItem();
} else { } else {
m_lvfc->viewToEnd(); m_lvfc->viewToEnd();
m_lvfc->previousElement(); m_lvfc->previousDelegate();
focusPreviousListViewItem(); focusPreviousListViewItem();
} }
return; return;
@ -90,62 +151,67 @@ void FocusController::nextItem(bool isForwardOrder)
void FocusController::focusNextListViewItem() void FocusController::focusNextListViewItem()
{ {
m_lvfc->focusNextItem();
if (m_lvfc->isLastFocusItemInListView() || m_lvfc->isReturnNeeded()) { if (m_lvfc->isLastFocusItemInListView() || m_lvfc->isReturnNeeded()) {
qDebug() << "===>> [Last item in ListView was reached]"; qDebug() << "===>> [Last item in ListView was reached. Going to the NEXT element after ListView]";
delete m_lvfc; delete m_lvfc;
m_lvfc = nullptr; m_lvfc = nullptr;
nextItem(Direction::Forward);
return;
} else if (m_lvfc->isLastFocusItemInDelegate()) { } else if (m_lvfc->isLastFocusItemInDelegate()) {
qDebug() << "===>> [End of delegate elements was reached. Going to the next delegate]"; qDebug() << "===>> [End of delegate elements was reached. Going to the next delegate]";
m_lvfc->resetFocusChain(); m_lvfc->resetFocusChain();
m_lvfc->nextElement(); m_lvfc->nextDelegate();
m_lvfc->viewAtCurrentIndex(); m_lvfc->viewAtCurrentIndex();
} }
m_lvfc->focusNextItem();
} }
void FocusController::focusPreviousListViewItem() void FocusController::focusPreviousListViewItem()
{ {
m_lvfc->focusPreviousItem();
if (m_lvfc->isFirstFocusItemInListView() || m_lvfc->isReturnNeeded()) { if (m_lvfc->isFirstFocusItemInListView() || m_lvfc->isReturnNeeded()) {
qDebug() << "===>> [First item in ListView was reached. Going to the PREVIOUS element after ListView]";
delete m_lvfc; delete m_lvfc;
m_lvfc = nullptr; m_lvfc = nullptr;
nextItem(Direction::Backward);
return;
} else if (m_lvfc->isFirstFocusItemInDelegate()) { } else if (m_lvfc->isFirstFocusItemInDelegate()) {
m_lvfc->resetFocusChain(); m_lvfc->resetFocusChain();
m_lvfc->decrementIndex(); m_lvfc->previousDelegate();
m_lvfc->viewAtCurrentIndex(); m_lvfc->viewAtCurrentIndex();
} }
m_lvfc->focusPreviousItem();
} }
void FocusController::nextKeyTabItem() void FocusController::nextKeyTabItem()
{ {
nextItem(true); nextItem(Direction::Forward);
} }
void FocusController::previousKeyTabItem() void FocusController::previousKeyTabItem()
{ {
nextItem(false); nextItem(Direction::Backward);
} }
void FocusController::nextKeyUpItem() void FocusController::nextKeyUpItem()
{ {
nextItem(false); nextItem(Direction::Backward);
} }
void FocusController::nextKeyDownItem() void FocusController::nextKeyDownItem()
{ {
nextItem(true); nextItem(Direction::Forward);
} }
void FocusController::nextKeyLeftItem() void FocusController::nextKeyLeftItem()
{ {
nextItem(false); nextItem(Direction::Backward);
} }
void FocusController::nextKeyRightItem() void FocusController::nextKeyRightItem()
{ {
nextItem(true); nextItem(Direction::Forward);
} }
void FocusController::setFocusOnDefaultItem() void FocusController::setFocusOnDefaultItem()
@ -154,7 +220,7 @@ void FocusController::setFocusOnDefaultItem()
m_defaultFocusItem->forceActiveFocus(); m_defaultFocusItem->forceActiveFocus();
} }
void FocusController::reload(bool isForwardOrder) void FocusController::reload(Direction direction)
{ {
m_focusChain.clear(); m_focusChain.clear();
@ -174,7 +240,7 @@ void FocusController::reload(bool isForwardOrder)
m_focusChain.append(getSubChain(rootObject)); m_focusChain.append(getSubChain(rootObject));
std::sort(m_focusChain.begin(), m_focusChain.end(), isForwardOrder? isLess : isMore); std::sort(m_focusChain.begin(), m_focusChain.end(), direction == Direction::Forward ? isLess : isMore);
if (m_focusChain.empty()) { if (m_focusChain.empty()) {
qWarning() << "Focus chain is empty!"; qWarning() << "Focus chain is empty!";

View file

@ -35,10 +35,15 @@ public:
Q_INVOKABLE void dropRootObject(QObject* object); Q_INVOKABLE void dropRootObject(QObject* object);
private: private:
void nextItem(bool isForwardOrder); enum class Direction {
Forward,
Backward,
};
void nextItem(Direction direction);
void focusNextListViewItem(); void focusNextListViewItem();
void focusPreviousListViewItem(); void focusPreviousListViewItem();
void reload(bool isForwardOrder); void reload(Direction direction);
QSharedPointer<QQmlApplicationEngine> m_engine; // Pointer to engine to get root object QSharedPointer<QQmlApplicationEngine> m_engine; // Pointer to engine to get root object
QList<QObject*> m_focusChain; // List of current objects to be focused QList<QObject*> m_focusChain; // List of current objects to be focused

View file

@ -10,7 +10,6 @@
bool isVisible(QObject* item) bool isVisible(QObject* item)
{ {
const auto res = item->property("visible").toBool(); const auto res = item->property("visible").toBool();
// qDebug() << "==>> " << (res ? "VISIBLE" : "NOT visible") << item;
return res; return res;
} }
@ -39,50 +38,13 @@ bool isMore(QObject* item1, QObject* item2)
return !isLess(item1, item2); return !isLess(item1, item2);
} }
bool isListView(QObject* item)
{
return item->inherits("QQuickListView");
}
bool isOnTheScene(QObject* object)
{
QQuickItem* item = qobject_cast<QQuickItem*>(object);
if (!item) {
qWarning() << "Couldn't recognize object as item";
return false;
}
if (!item->isVisible()) {
// qDebug() << "===>> The item is not visible: " << item;
return false;
}
QRectF itemRect = item->mapRectToScene(item->childrenRect());
QQuickWindow* window = item->window();
if (!window) {
qWarning() << "Couldn't get the window on the Scene check";
return false;
}
const auto contentItem = window->contentItem();
if (!contentItem) {
qWarning() << "Couldn't get the content item on the Scene check";
return false;
}
QRectF windowRect = contentItem->childrenRect();
const auto res = (windowRect.contains(itemRect) || isListView(item));
// qDebug() << (res ? "===>> item is inside the Scene" : "===>> ITEM IS OUTSIDE THE SCENE") << " itemRect: " << itemRect << "; windowRect: " << windowRect;
return res;
}
bool isEnabled(QObject* obj) bool isEnabled(QObject* obj)
{ {
const auto item = qobject_cast<QQuickItem*>(obj); const auto item = qobject_cast<QQuickItem*>(obj);
return item && item->isEnabled(); return item && item->isEnabled();
} }
QList<QObject*> getSubChain(QObject* object) QList<QObject*> getItemsChain(QObject* object)
{ {
QList<QObject*> res; QList<QObject*> res;
if (!object) { if (!object) {
@ -95,12 +57,12 @@ QList<QObject*> getSubChain(QObject* object)
for(const auto child : children) { for(const auto child : children) {
if (child if (child
&& isFocusable(child) && isFocusable(child)
&& isOnTheScene(child)
&& isEnabled(child) && isEnabled(child)
&& isVisible(child)
) { ) {
res.append(child); res.append(child);
} else { } else {
res.append(getSubChain(child)); res.append(getItemsChain(child));
} }
} }
return res; return res;
@ -175,13 +137,13 @@ int ListViewFocusController::currentIndex() const
return m_delegateIndex; return m_delegateIndex;
} }
void ListViewFocusController::nextElement() void ListViewFocusController::nextDelegate()
{ {
qDebug() << "===>> Current section: " << m_currentSectionString.at(static_cast<qsizetype>(m_currentSection));
switch(m_currentSection) { switch(m_currentSection) {
case Section::Default: { case Section::Default: {
if(m_header) { if(m_header) {
m_currentSection = Section::Header; m_currentSection = Section::Header;
viewToBegin();
break; break;
} }
} }
@ -197,6 +159,7 @@ void ListViewFocusController::nextElement()
break; break;
} else if (m_footer) { } else if (m_footer) {
m_currentSection = Section::Footer; m_currentSection = Section::Footer;
viewToEnd();
break; break;
} }
case Section::Footer: { case Section::Footer: {
@ -209,10 +172,9 @@ void ListViewFocusController::nextElement()
break; break;
} }
} }
} }
void ListViewFocusController::previousElement() void ListViewFocusController::previousDelegate()
{ {
switch(m_currentSection) { switch(m_currentSection) {
case Section::Default: { case Section::Default: {
@ -224,7 +186,6 @@ void ListViewFocusController::previousElement()
case Section::Footer: { case Section::Footer: {
if (size() > 0) { if (size() > 0) {
m_currentSection = Section::Delegate; m_currentSection = Section::Delegate;
m_focusedItemIndex = size() - 1; // workarount to default value == -1
break; break;
} }
} }
@ -298,16 +259,17 @@ QQuickItem* ListViewFocusController::focusedItem()
void ListViewFocusController::focusNextItem() void ListViewFocusController::focusNextItem()
{ {
if (m_isReturnNeeded) { if (m_isReturnNeeded) {
qDebug() << "===>> RETURN IS NEEDED...";
return; return;
} }
if (m_focusChain.empty()) { if (m_focusChain.empty()) {
qDebug() << "Empty focusChain with current delegate: " << currentDelegate() << "Scanning for elements..."; qDebug() << "Empty focusChain with current delegate: " << currentDelegate() << "Scanning for elements...";
m_focusChain = getSubChain(currentDelegate()); m_focusChain = getItemsChain(currentDelegate());
} }
if (m_focusChain.empty()) { if (m_focusChain.empty()) {
qWarning() << "No elements found. Returning from ListView..."; qWarning() << "No elements found in the delegate. Going to next delegate...";
nextElement(); nextDelegate();
focusNextItem(); focusNextItem();
return; return;
} }
@ -325,14 +287,17 @@ void ListViewFocusController::focusPreviousItem()
if (m_focusChain.empty()) { if (m_focusChain.empty()) {
qDebug() << "Empty focusChain with current delegate: " << currentDelegate() << "Scanning for elements..."; qDebug() << "Empty focusChain with current delegate: " << currentDelegate() << "Scanning for elements...";
m_focusChain = getSubChain(currentDelegate()); m_focusChain = getItemsChain(currentDelegate());
} }
if (m_focusChain.empty()) { if (m_focusChain.empty()) {
qWarning() << "No elements found. Returning from ListView..."; qWarning() << "No elements found in the delegate. Going to next delegate...";
previousElement(); previousDelegate();
focusPreviousItem(); focusPreviousItem();
return; return;
} }
if (m_focusedItemIndex == -1) {
m_focusedItemIndex = m_focusChain.size();
}
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;

View file

@ -7,7 +7,8 @@
#include <QQuickItem> #include <QQuickItem>
bool isListView(QObject* item); bool isEnabled(QObject* item);
bool isFocusable(QObject* item);
bool isMore(QObject* item1, QObject* item2); bool isMore(QObject* item1, QObject* item2);
bool isLess(QObject* item1, QObject* item2); bool isLess(QObject* item1, QObject* item2);
QList<QObject*> getSubChain(QObject* object); QList<QObject*> getSubChain(QObject* object);
@ -29,8 +30,8 @@ public:
explicit ListViewFocusController(QQuickItem* listView, QObject* parent = nullptr); explicit ListViewFocusController(QQuickItem* listView, QObject* parent = nullptr);
~ListViewFocusController(); ~ListViewFocusController();
void nextElement(); void nextDelegate();
void previousElement(); void previousDelegate();
void decrementIndex(); void decrementIndex();
void focusNextItem(); void focusNextItem();
void focusPreviousItem(); void focusPreviousItem();

View file

@ -15,6 +15,7 @@ import "Pages2"
Window { Window {
id: root id: root
objectName: "mainWindow" objectName: "mainWindow"
visible: true visible: true
width: GC.screenWidth width: GC.screenWidth
height: GC.screenHeight height: GC.screenHeight
@ -32,7 +33,7 @@ Window {
title: "AmneziaVPN" title: "AmneziaVPN"
Item { Item { // This item is needed for focus handling
id: defaultFocusItem id: defaultFocusItem
objectName: "defaultFocusItem" objectName: "defaultFocusItem"
@ -210,8 +211,6 @@ Window {
clickedFunc: function() { clickedFunc: function() {
hidePassword = !hidePassword hidePassword = !hidePassword
} }
// KeyNavigation.tab: saveButton
} }
BasicButtonType { BasicButtonType {