-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Qt: Add search box to hotkey settings
- Loading branch information
Showing
3 changed files
with
120 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]> | ||
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <[email protected]> | ||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0 | ||
|
||
#include "hotkeysettingswidget.h" | ||
#include "controllersettingswindow.h" | ||
#include "util/input_manager.h" | ||
#include "inputbindingwidgets.h" | ||
#include "qtutils.h" | ||
#include "settingwidgetbinder.h" | ||
|
||
#include "util/input_manager.h" | ||
|
||
#include <QtGui/QResizeEvent> | ||
#include <QtWidgets/QGridLayout> | ||
#include <QtWidgets/QLabel> | ||
#include <QtWidgets/QLineEdit> | ||
#include <QtWidgets/QMessageBox> | ||
#include <QtWidgets/QScrollArea> | ||
|
||
|
@@ -20,13 +24,36 @@ HotkeySettingsWidget::HotkeySettingsWidget(QWidget* parent, ControllerSettingsWi | |
|
||
HotkeySettingsWidget::~HotkeySettingsWidget() = default; | ||
|
||
HotkeySettingsWidget::Container::Container(QWidget* parent) : QWidget(parent) | ||
{ | ||
m_search = new QLineEdit(this); | ||
m_search->setPlaceholderText(tr("Search...")); | ||
} | ||
|
||
HotkeySettingsWidget::Container::~Container() = default; | ||
|
||
void HotkeySettingsWidget::Container::resizeEvent(QResizeEvent* event) | ||
{ | ||
QWidget::resizeEvent(event); | ||
repositionSearchBox(); | ||
} | ||
|
||
void HotkeySettingsWidget::Container::repositionSearchBox() | ||
{ | ||
constexpr int box_width = 300; | ||
constexpr int box_padding = 8; | ||
const int x = std::max(width() - box_width - box_padding, 0); | ||
const int h = m_search->height(); | ||
m_search->setGeometry(x, box_padding, box_width, h); | ||
} | ||
|
||
void HotkeySettingsWidget::createUi() | ||
{ | ||
QGridLayout* layout = new QGridLayout(this); | ||
layout->setContentsMargins(0, 0, 0, 0); | ||
|
||
m_scroll_area = new QScrollArea(this); | ||
m_container = new QWidget(m_scroll_area); | ||
m_container = new Container(m_scroll_area); | ||
m_layout = new QVBoxLayout(m_container); | ||
m_scroll_area->setWidget(m_container); | ||
m_scroll_area->setWidgetResizable(true); | ||
|
@@ -38,6 +65,9 @@ void HotkeySettingsWidget::createUi() | |
layout->addWidget(m_scroll_area, 0, 0, 1, 1); | ||
|
||
setLayout(layout); | ||
|
||
m_container->searchBox()->raise(); | ||
connect(m_container->searchBox(), &QLineEdit::textChanged, this, &HotkeySettingsWidget::setFilter); | ||
} | ||
|
||
void HotkeySettingsWidget::createButtons() | ||
|
@@ -48,27 +78,35 @@ void HotkeySettingsWidget::createButtons() | |
const QString category(qApp->translate("Hotkeys", hotkey->category)); | ||
|
||
auto iter = m_categories.find(category); | ||
int target_row = 0; | ||
if (iter == m_categories.end()) | ||
{ | ||
QLabel* label = new QLabel(category, m_container); | ||
QFont label_font(label->font()); | ||
CategoryWidgets cw; | ||
cw.label = new QLabel(category, m_container); | ||
QFont label_font(cw.label->font()); | ||
label_font.setPointSizeF(14.0f); | ||
label->setFont(label_font); | ||
m_layout->addWidget(label); | ||
|
||
QLabel* line = new QLabel(m_container); | ||
line->setFrameShape(QFrame::HLine); | ||
line->setFixedHeight(4); | ||
m_layout->addWidget(line); | ||
|
||
QGridLayout* layout = new QGridLayout(); | ||
layout->setContentsMargins(0, 0, 0, 0); | ||
m_layout->addLayout(layout); | ||
iter = m_categories.insert(category, layout); | ||
cw.label->setFont(label_font); | ||
m_layout->addWidget(cw.label); | ||
|
||
cw.line = new QLabel(m_container); | ||
cw.line->setFrameShape(QFrame::HLine); | ||
cw.line->setFixedHeight(4); | ||
m_layout->addWidget(cw.line); | ||
|
||
cw.layout = new QGridLayout(); | ||
cw.layout->setContentsMargins(0, 0, 0, 0); | ||
m_layout->addLayout(cw.layout); | ||
iter = m_categories.insert(category, cw); | ||
|
||
// row count starts at 1 for some reason | ||
target_row = 0; | ||
} | ||
else | ||
{ | ||
target_row = iter->layout->rowCount(); | ||
} | ||
|
||
QGridLayout* layout = *iter; | ||
const int target_row = layout->count() / 2; | ||
QGridLayout* layout = iter->layout; | ||
|
||
QLabel* label = new QLabel(qApp->translate("Hotkeys", hotkey->display_name), m_container); | ||
layout->addWidget(label, target_row, 0); | ||
|
@@ -79,3 +117,28 @@ void HotkeySettingsWidget::createButtons() | |
layout->addWidget(bind, target_row, 1); | ||
} | ||
} | ||
|
||
void HotkeySettingsWidget::setFilter(const QString& filter) | ||
{ | ||
for (const CategoryWidgets& cw : m_categories) | ||
{ | ||
const int row_count = cw.layout->rowCount(); | ||
int visible_row_count = 0; | ||
for (int i = 0; i < row_count; i++) | ||
{ | ||
QLabel* label = qobject_cast<QLabel*>(cw.layout->itemAtPosition(i, 0)->widget()); | ||
InputBindingWidget* bind = qobject_cast<InputBindingWidget*>(cw.layout->itemAtPosition(i, 1)->widget()); | ||
if (!label || !bind) | ||
continue; | ||
|
||
const bool visible = (filter.isEmpty() || label->text().indexOf(filter, 0, Qt::CaseInsensitive) >= 0); | ||
label->setVisible(visible); | ||
bind->setVisible(visible); | ||
visible_row_count += static_cast<int>(visible); | ||
} | ||
|
||
const bool heading_visible = (visible_row_count > 0); | ||
cw.label->setVisible(heading_visible); | ||
cw.line->setVisible(heading_visible); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]> | ||
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <[email protected]> | ||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0 | ||
|
||
#pragma once | ||
|
||
#include <QtCore/QMap> | ||
#include <QtWidgets/QWidget> | ||
|
||
#include <array> | ||
#include <vector> | ||
|
||
class QLabel; | ||
class QLineEdit; | ||
class QScrollArea; | ||
class QGridLayout; | ||
class QVBoxLayout; | ||
|
@@ -23,13 +26,41 @@ class HotkeySettingsWidget : public QWidget | |
~HotkeySettingsWidget(); | ||
|
||
private: | ||
struct CategoryWidgets | ||
{ | ||
QLabel* label; | ||
QLabel* line; | ||
QGridLayout* layout; | ||
}; | ||
|
||
class Container final : public QWidget | ||
{ | ||
public: | ||
Container(QWidget* parent); | ||
~Container() override; | ||
|
||
QLineEdit* searchBox() const { return m_search; } | ||
|
||
void resizeEvent(QResizeEvent* event) override; | ||
|
||
private: | ||
void repositionSearchBox(); | ||
|
||
QLineEdit* m_search; | ||
}; | ||
|
||
void createUi(); | ||
void createButtons(); | ||
|
||
private Q_SLOTS: | ||
void setFilter(const QString& filter); | ||
|
||
private: | ||
ControllerSettingsWindow* m_dialog; | ||
QScrollArea* m_scroll_area = nullptr; | ||
QWidget* m_container = nullptr; | ||
Container* m_container = nullptr; | ||
QVBoxLayout* m_layout = nullptr; | ||
QLineEdit* m_search = nullptr; | ||
|
||
QMap<QString, QGridLayout*> m_categories; | ||
QMap<QString, CategoryWidgets> m_categories; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters