mirror of
https://github.com/stenzek/duckstation.git
synced 2025-06-07 20:15:32 +00:00
Qt: Add search box to hotkey settings
This commit is contained in:
parent
b2c8d25638
commit
dda10d3576
@ -1,14 +1,18 @@
|
|||||||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <stenzek@gmail.com>
|
||||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||||
|
|
||||||
#include "hotkeysettingswidget.h"
|
#include "hotkeysettingswidget.h"
|
||||||
#include "controllersettingswindow.h"
|
#include "controllersettingswindow.h"
|
||||||
#include "util/input_manager.h"
|
|
||||||
#include "inputbindingwidgets.h"
|
#include "inputbindingwidgets.h"
|
||||||
#include "qtutils.h"
|
#include "qtutils.h"
|
||||||
#include "settingwidgetbinder.h"
|
#include "settingwidgetbinder.h"
|
||||||
|
|
||||||
|
#include "util/input_manager.h"
|
||||||
|
|
||||||
|
#include <QtGui/QResizeEvent>
|
||||||
#include <QtWidgets/QGridLayout>
|
#include <QtWidgets/QGridLayout>
|
||||||
#include <QtWidgets/QLabel>
|
#include <QtWidgets/QLabel>
|
||||||
|
#include <QtWidgets/QLineEdit>
|
||||||
#include <QtWidgets/QMessageBox>
|
#include <QtWidgets/QMessageBox>
|
||||||
#include <QtWidgets/QScrollArea>
|
#include <QtWidgets/QScrollArea>
|
||||||
|
|
||||||
@ -20,13 +24,36 @@ HotkeySettingsWidget::HotkeySettingsWidget(QWidget* parent, ControllerSettingsWi
|
|||||||
|
|
||||||
HotkeySettingsWidget::~HotkeySettingsWidget() = default;
|
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()
|
void HotkeySettingsWidget::createUi()
|
||||||
{
|
{
|
||||||
QGridLayout* layout = new QGridLayout(this);
|
QGridLayout* layout = new QGridLayout(this);
|
||||||
layout->setContentsMargins(0, 0, 0, 0);
|
layout->setContentsMargins(0, 0, 0, 0);
|
||||||
|
|
||||||
m_scroll_area = new QScrollArea(this);
|
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_layout = new QVBoxLayout(m_container);
|
||||||
m_scroll_area->setWidget(m_container);
|
m_scroll_area->setWidget(m_container);
|
||||||
m_scroll_area->setWidgetResizable(true);
|
m_scroll_area->setWidgetResizable(true);
|
||||||
@ -38,6 +65,9 @@ void HotkeySettingsWidget::createUi()
|
|||||||
layout->addWidget(m_scroll_area, 0, 0, 1, 1);
|
layout->addWidget(m_scroll_area, 0, 0, 1, 1);
|
||||||
|
|
||||||
setLayout(layout);
|
setLayout(layout);
|
||||||
|
|
||||||
|
m_container->searchBox()->raise();
|
||||||
|
connect(m_container->searchBox(), &QLineEdit::textChanged, this, &HotkeySettingsWidget::setFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HotkeySettingsWidget::createButtons()
|
void HotkeySettingsWidget::createButtons()
|
||||||
@ -48,27 +78,35 @@ void HotkeySettingsWidget::createButtons()
|
|||||||
const QString category(qApp->translate("Hotkeys", hotkey->category));
|
const QString category(qApp->translate("Hotkeys", hotkey->category));
|
||||||
|
|
||||||
auto iter = m_categories.find(category);
|
auto iter = m_categories.find(category);
|
||||||
|
int target_row = 0;
|
||||||
if (iter == m_categories.end())
|
if (iter == m_categories.end())
|
||||||
{
|
{
|
||||||
QLabel* label = new QLabel(category, m_container);
|
CategoryWidgets cw;
|
||||||
QFont label_font(label->font());
|
cw.label = new QLabel(category, m_container);
|
||||||
|
QFont label_font(cw.label->font());
|
||||||
label_font.setPointSizeF(14.0f);
|
label_font.setPointSizeF(14.0f);
|
||||||
label->setFont(label_font);
|
cw.label->setFont(label_font);
|
||||||
m_layout->addWidget(label);
|
m_layout->addWidget(cw.label);
|
||||||
|
|
||||||
QLabel* line = new QLabel(m_container);
|
cw.line = new QLabel(m_container);
|
||||||
line->setFrameShape(QFrame::HLine);
|
cw.line->setFrameShape(QFrame::HLine);
|
||||||
line->setFixedHeight(4);
|
cw.line->setFixedHeight(4);
|
||||||
m_layout->addWidget(line);
|
m_layout->addWidget(cw.line);
|
||||||
|
|
||||||
QGridLayout* layout = new QGridLayout();
|
cw.layout = new QGridLayout();
|
||||||
layout->setContentsMargins(0, 0, 0, 0);
|
cw.layout->setContentsMargins(0, 0, 0, 0);
|
||||||
m_layout->addLayout(layout);
|
m_layout->addLayout(cw.layout);
|
||||||
iter = m_categories.insert(category, 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;
|
QGridLayout* layout = iter->layout;
|
||||||
const int target_row = layout->count() / 2;
|
|
||||||
|
|
||||||
QLabel* label = new QLabel(qApp->translate("Hotkeys", hotkey->display_name), m_container);
|
QLabel* label = new QLabel(qApp->translate("Hotkeys", hotkey->display_name), m_container);
|
||||||
layout->addWidget(label, target_row, 0);
|
layout->addWidget(label, target_row, 0);
|
||||||
@ -79,3 +117,28 @@ void HotkeySettingsWidget::createButtons()
|
|||||||
layout->addWidget(bind, target_row, 1);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <stenzek@gmail.com>
|
||||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QtCore/QMap>
|
#include <QtCore/QMap>
|
||||||
#include <QtWidgets/QWidget>
|
#include <QtWidgets/QWidget>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
class QLabel;
|
||||||
|
class QLineEdit;
|
||||||
class QScrollArea;
|
class QScrollArea;
|
||||||
class QGridLayout;
|
class QGridLayout;
|
||||||
class QVBoxLayout;
|
class QVBoxLayout;
|
||||||
@ -23,13 +26,41 @@ public:
|
|||||||
~HotkeySettingsWidget();
|
~HotkeySettingsWidget();
|
||||||
|
|
||||||
private:
|
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 createUi();
|
||||||
void createButtons();
|
void createButtons();
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void setFilter(const QString& filter);
|
||||||
|
|
||||||
|
private:
|
||||||
ControllerSettingsWindow* m_dialog;
|
ControllerSettingsWindow* m_dialog;
|
||||||
QScrollArea* m_scroll_area = nullptr;
|
QScrollArea* m_scroll_area = nullptr;
|
||||||
QWidget* m_container = nullptr;
|
Container* m_container = nullptr;
|
||||||
QVBoxLayout* m_layout = nullptr;
|
QVBoxLayout* m_layout = nullptr;
|
||||||
|
QLineEdit* m_search = nullptr;
|
||||||
|
|
||||||
QMap<QString, QGridLayout*> m_categories;
|
QMap<QString, CategoryWidgets> m_categories;
|
||||||
};
|
};
|
||||||
|
@ -87,8 +87,10 @@ void InputBindingWidget::updateText()
|
|||||||
// fix up accelerators, and if it's too long, ellipsise it
|
// fix up accelerators, and if it's too long, ellipsise it
|
||||||
if (binding_text.contains('&'))
|
if (binding_text.contains('&'))
|
||||||
binding_text = binding_text.replace(QStringLiteral("&"), QStringLiteral("&&"));
|
binding_text = binding_text.replace(QStringLiteral("&"), QStringLiteral("&&"));
|
||||||
if (binding_text.length() > 35)
|
|
||||||
binding_text = binding_text.left(35).append(QStringLiteral("..."));
|
const int max_length = (width() < 300) ? 35 : 60;
|
||||||
|
if (binding_text.length() > max_length)
|
||||||
|
binding_text = binding_text.left(max_length).append(QStringLiteral("..."));
|
||||||
setText(binding_text);
|
setText(binding_text);
|
||||||
setToolTip(QStringLiteral("%1\n\n%2\n%3").arg(binding_text).arg(tr(help_text)).arg(tr(help_clear_text)));
|
setToolTip(QStringLiteral("%1\n\n%2\n%3").arg(binding_text).arg(tr(help_text)).arg(tr(help_clear_text)));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user