FlowFrame.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /******************************************************************************
  2. Copyright (C) 2025 by Taylor Giampaolo <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <components/FlowFrame.hpp>
  15. #include <QKeyEvent>
  16. #include <QScrollArea>
  17. FlowFrame::FlowFrame(QWidget *parent) : QFrame(parent)
  18. {
  19. layout = new FlowLayout(this);
  20. setLayout(layout);
  21. }
  22. void FlowFrame::keyPressEvent(QKeyEvent *event)
  23. {
  24. QWidget *focused = focusWidget();
  25. if (!focused) {
  26. return;
  27. }
  28. int index = -1;
  29. for (int i = 0; i < layout->count(); ++i) {
  30. if (!layout->itemAt(i)->widget()) {
  31. continue;
  32. }
  33. auto focusProxy = layout->itemAt(i)->widget()->focusProxy();
  34. if (layout->itemAt(i)->widget() == focused || focusProxy == focused) {
  35. if (focusProxy == focused) {
  36. focused = layout->itemAt(i)->widget();
  37. }
  38. index = i;
  39. break;
  40. }
  41. }
  42. if (index == -1) {
  43. return;
  44. }
  45. const QRect focusedRect = focused->geometry();
  46. QWidget *nextFocus = nullptr;
  47. switch (event->key()) {
  48. case Qt::Key_Right:
  49. case Qt::Key_Down:
  50. case Qt::Key_Left:
  51. case Qt::Key_Up: {
  52. // Find next widget in the given direction
  53. int bestDistance = INT_MAX;
  54. for (int i = 0; i < layout->count(); ++i) {
  55. if (i == index) {
  56. continue;
  57. }
  58. QWidget *widget = layout->itemAt(i)->widget();
  59. const QRect rect = widget->geometry();
  60. bool isCandidate = false;
  61. int distance = INT_MAX;
  62. switch (event->key()) {
  63. case Qt::Key_Right:
  64. if (rect.left() > focusedRect.right()) {
  65. distance = (rect.left() - focusedRect.right()) +
  66. qAbs(rect.center().y() - focusedRect.center().y());
  67. isCandidate = true;
  68. }
  69. break;
  70. case Qt::Key_Left:
  71. if (rect.right() < focusedRect.left()) {
  72. distance = (focusedRect.left() - rect.right()) +
  73. qAbs(rect.center().y() - focusedRect.center().y());
  74. isCandidate = true;
  75. }
  76. break;
  77. case Qt::Key_Down:
  78. if (rect.top() > focusedRect.bottom()) {
  79. distance = (rect.top() - focusedRect.bottom()) +
  80. qAbs(rect.center().x() - focusedRect.center().x());
  81. isCandidate = true;
  82. }
  83. break;
  84. case Qt::Key_Up:
  85. if (rect.bottom() < focusedRect.top()) {
  86. distance = (focusedRect.top() - rect.bottom()) +
  87. qAbs(rect.center().x() - focusedRect.center().x());
  88. isCandidate = true;
  89. }
  90. break;
  91. }
  92. if (isCandidate && distance < bestDistance) {
  93. bestDistance = distance;
  94. nextFocus = widget;
  95. }
  96. }
  97. break;
  98. }
  99. default:
  100. QWidget::keyPressEvent(event);
  101. return;
  102. }
  103. if (nextFocus) {
  104. nextFocus->setFocus();
  105. QWidget *scrollParent = nextFocus->parentWidget();
  106. while (scrollParent) {
  107. QScrollArea *scrollArea = qobject_cast<QScrollArea *>(scrollParent);
  108. if (scrollArea) {
  109. scrollArea->ensureWidgetVisible(nextFocus, 20, 20);
  110. break;
  111. }
  112. scrollParent = scrollParent->parentWidget();
  113. }
  114. }
  115. }