revert: remove user custom shortcut
This commit is contained in:
@@ -112,7 +112,6 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
|
||||
QTimer::singleShot(0, this, [this](){
|
||||
m_am->setupShortcuts();
|
||||
Settings::instance()->applyUserShortcuts(this);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -138,47 +138,6 @@ void Settings::setHiDpiScaleFactorBehavior(Qt::HighDpiScaleFactorRoundingPolicy
|
||||
m_qsettings->sync();
|
||||
}
|
||||
|
||||
void Settings::applyUserShortcuts(QWidget *widget)
|
||||
{
|
||||
m_qsettings->beginGroup("shortcuts");
|
||||
const QStringList shortcutNames = m_qsettings->allKeys();
|
||||
for (const QString & name : shortcutNames) {
|
||||
QList<QKeySequence> shortcuts = m_qsettings->value(name).value<QList<QKeySequence>>();
|
||||
setShortcutsForAction(widget, name, shortcuts, false);
|
||||
}
|
||||
m_qsettings->endGroup();
|
||||
}
|
||||
|
||||
bool Settings::setShortcutsForAction(QWidget *widget, const QString &objectName,
|
||||
QList<QKeySequence> shortcuts, bool writeConfig)
|
||||
{
|
||||
QAction * targetAction = nullptr;
|
||||
for (QAction * action : widget->actions()) {
|
||||
if (action->objectName() == objectName) {
|
||||
targetAction = action;
|
||||
} else {
|
||||
for (const QKeySequence & shortcut : std::as_const(shortcuts)) {
|
||||
if (action->shortcuts().contains(shortcut)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (targetAction) {
|
||||
targetAction->setShortcuts(shortcuts);
|
||||
}
|
||||
|
||||
if (targetAction && writeConfig) {
|
||||
m_qsettings->beginGroup("shortcuts");
|
||||
m_qsettings->setValue(objectName, QVariant::fromValue(shortcuts));
|
||||
m_qsettings->endGroup();
|
||||
m_qsettings->sync();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN)
|
||||
#include <windows.h>
|
||||
// QCoreApplication::applicationDirPath() parses the "applicationDirPath" from arg0, which...
|
||||
|
||||
@@ -50,10 +50,6 @@ public:
|
||||
void setInitWindowSizeBehavior(WindowSizeBehavior wsb);
|
||||
void setHiDpiScaleFactorBehavior(Qt::HighDpiScaleFactorRoundingPolicy hidpi);
|
||||
|
||||
void applyUserShortcuts(QWidget * widget);
|
||||
bool setShortcutsForAction(QWidget * widget, const QString & objectName,
|
||||
QList<QKeySequence> shortcuts, bool writeConfig = true);
|
||||
|
||||
private:
|
||||
Settings();
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#include "settingsdialog.h"
|
||||
|
||||
#include "settings.h"
|
||||
#include "shortcutedit.h"
|
||||
|
||||
#include <QAction>
|
||||
#include <QCheckBox>
|
||||
@@ -37,42 +36,6 @@ SettingsDialog::SettingsDialog(QWidget *parent)
|
||||
QFormLayout * settingsForm = new QFormLayout(settingsFormHolder);
|
||||
settingsTabs->addTab(settingsFormHolder, tr("Options"));
|
||||
|
||||
QSplitter * shortcutEditorSplitter = new QSplitter;
|
||||
shortcutEditorSplitter->setOrientation(Qt::Vertical);
|
||||
shortcutEditorSplitter->setChildrenCollapsible(false);
|
||||
QScrollArea * shortcutScrollArea = new QScrollArea;
|
||||
shortcutEditorSplitter->addWidget(shortcutScrollArea);
|
||||
shortcutScrollArea->setWidgetResizable(true);
|
||||
shortcutScrollArea->setMinimumHeight(200);
|
||||
QWidget * shortcutsFormHolder = new QWidget;
|
||||
QFormLayout * shortcutsForm = new QFormLayout(shortcutsFormHolder);
|
||||
shortcutScrollArea->setWidget(shortcutsFormHolder);
|
||||
settingsTabs->addTab(shortcutEditorSplitter, tr("Shortcuts"));
|
||||
|
||||
for (const QAction * action : parent->actions()) {
|
||||
ShortcutEdit * shortcutEdit = new ShortcutEdit;
|
||||
shortcutEdit->setObjectName(QLatin1String("shortcut_") + action->objectName());
|
||||
shortcutEdit->setShortcuts(action->shortcuts());
|
||||
shortcutsForm->addRow(action->text(), shortcutEdit);
|
||||
connect(shortcutEdit, &ShortcutEdit::editButtonClicked, this, [=](){
|
||||
if (shortcutEditorSplitter->count() == 1) shortcutEditorSplitter->addWidget(new QWidget);
|
||||
ShortcutEditor * shortcutEditor = new ShortcutEditor(shortcutEdit);
|
||||
shortcutEditor->setDescription(tr("Editing shortcuts for action \"%1\":").arg(action->text()));
|
||||
QWidget * oldEditor = shortcutEditorSplitter->replaceWidget(1, shortcutEditor);
|
||||
shortcutEditorSplitter->setSizes({shortcutEditorSplitter->height(), 1});
|
||||
oldEditor->deleteLater();
|
||||
});
|
||||
connect(shortcutEdit, &ShortcutEdit::applyShortcutsRequested, this, [=](QList<QKeySequence> newShortcuts){
|
||||
bool succ = Settings::instance()->setShortcutsForAction(parent, shortcutEdit->objectName().mid(9),
|
||||
newShortcuts);
|
||||
if (!succ) {
|
||||
QMessageBox::warning(this, tr("Failed to set shortcuts"),
|
||||
tr("Please check if shortcuts are duplicated with existing shortcuts."));
|
||||
}
|
||||
shortcutEdit->setShortcuts(action->shortcuts());
|
||||
});
|
||||
}
|
||||
|
||||
static QList< QPair<Settings::DoubleClickBehavior, QString> > _dc_options {
|
||||
{ Settings::DoubleClickBehavior::Ignore, tr("Do nothing") },
|
||||
{ Settings::DoubleClickBehavior::Close, tr("Close the window") },
|
||||
|
||||
@@ -1,126 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2024 Gary Wang <opensource@blumia.net>
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "shortcutedit.h"
|
||||
|
||||
#include <QLabel>
|
||||
#include <QToolButton>
|
||||
#include <QHBoxLayout>
|
||||
#include <QFormLayout>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QKeySequenceEdit>
|
||||
|
||||
ShortcutEditor::ShortcutEditor(ShortcutEdit * shortcutEdit, QWidget * parent)
|
||||
: QWidget(parent)
|
||||
, m_descriptionLabel(new QLabel)
|
||||
, m_shortcutEdit(shortcutEdit)
|
||||
, m_shortcutLayout(new QFormLayout)
|
||||
{
|
||||
Q_CHECK_PTR(m_shortcutEdit);
|
||||
|
||||
QDialogButtonBox * buttons = new QDialogButtonBox(QDialogButtonBox::Apply | QDialogButtonBox::Discard);
|
||||
|
||||
QVBoxLayout * layout = new QVBoxLayout(this);
|
||||
layout->addWidget(m_descriptionLabel);
|
||||
layout->addLayout(m_shortcutLayout);
|
||||
layout->addWidget(buttons);
|
||||
|
||||
connect(buttons, &QDialogButtonBox::clicked, this, [=](QAbstractButton *button){
|
||||
if ((QPushButton *)button == buttons->button(QDialogButtonBox::Apply)) {
|
||||
applyShortcuts();
|
||||
} else {
|
||||
reloadShortcuts();
|
||||
}
|
||||
});
|
||||
connect(shortcutEdit, &ShortcutEdit::shortcutsChanged, this, &ShortcutEditor::reloadShortcuts);
|
||||
|
||||
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
|
||||
|
||||
reloadShortcuts();
|
||||
}
|
||||
|
||||
void ShortcutEditor::setDescription(const QString &desc)
|
||||
{
|
||||
m_descriptionLabel->setText(desc);
|
||||
}
|
||||
|
||||
void ShortcutEditor::reloadShortcuts()
|
||||
{
|
||||
if (!m_keySequenceEdits.isEmpty()) {
|
||||
for (QKeySequenceEdit * keyseqEdit : m_keySequenceEdits) {
|
||||
m_shortcutLayout->removeRow(keyseqEdit);
|
||||
}
|
||||
m_keySequenceEdits.clear();
|
||||
}
|
||||
|
||||
QList<QKeySequence> shortcuts = m_shortcutEdit->shortcuts();
|
||||
shortcuts.append(QKeySequence());
|
||||
for (const QKeySequence & shortcut : shortcuts) {
|
||||
QKeySequenceEdit * keyseqEdit = new QKeySequenceEdit(this);
|
||||
keyseqEdit->setClearButtonEnabled(true);
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
||||
keyseqEdit->setMaximumSequenceLength(1);
|
||||
#endif // QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
||||
keyseqEdit->setKeySequence(shortcut);
|
||||
m_keySequenceEdits.append(keyseqEdit);
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_keySequenceEdits.count(); i++) {
|
||||
m_shortcutLayout->addRow(tr("Shortcut #%1").arg(i + 1), m_keySequenceEdits.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
void ShortcutEditor::applyShortcuts()
|
||||
{
|
||||
QList<QKeySequence> shortcuts;
|
||||
for (const QKeySequenceEdit * keyseqEdit : m_keySequenceEdits) {
|
||||
if (!keyseqEdit->keySequence().isEmpty() && !shortcuts.contains(keyseqEdit->keySequence())) {
|
||||
shortcuts.append(keyseqEdit->keySequence());
|
||||
}
|
||||
}
|
||||
emit m_shortcutEdit->applyShortcutsRequested(shortcuts);
|
||||
}
|
||||
|
||||
// ----------------------------------------
|
||||
|
||||
ShortcutEdit::ShortcutEdit(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, m_shortcutsLabel(new QLabel(this))
|
||||
, m_setShortcutButton(new QToolButton(this))
|
||||
{
|
||||
m_setShortcutButton->setText("...");
|
||||
|
||||
QHBoxLayout * layout = new QHBoxLayout(this);
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
layout->addWidget(m_shortcutsLabel, 1);
|
||||
layout->addWidget(m_setShortcutButton);
|
||||
|
||||
connect(this, &ShortcutEdit::shortcutsChanged, this, [=](){
|
||||
QStringList shortcutTexts;
|
||||
for (const QKeySequence & shortcut : std::as_const(m_shortcuts)) {
|
||||
shortcutTexts.append(shortcut.toString(QKeySequence::NativeText));
|
||||
}
|
||||
m_shortcutsLabel->setText(shortcutTexts.isEmpty() ? tr("No shortcuts") : shortcutTexts.join(", "));
|
||||
m_shortcutsLabel->setDisabled(shortcutTexts.isEmpty());
|
||||
});
|
||||
|
||||
connect(m_setShortcutButton, &QToolButton::clicked, this, &ShortcutEdit::editButtonClicked);
|
||||
|
||||
adjustSize();
|
||||
}
|
||||
|
||||
ShortcutEdit::~ShortcutEdit()
|
||||
{
|
||||
}
|
||||
|
||||
QList<QKeySequence> ShortcutEdit::shortcuts() const
|
||||
{
|
||||
return m_shortcuts;
|
||||
}
|
||||
|
||||
void ShortcutEdit::setShortcuts(const QList<QKeySequence> &shortcuts)
|
||||
{
|
||||
m_shortcuts = shortcuts;
|
||||
emit shortcutsChanged();
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2024 Gary Wang <opensource@blumia.net>
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
#include <QList>
|
||||
#include <QKeySequence>
|
||||
|
||||
class QLabel;
|
||||
class QFormLayout;
|
||||
class QToolButton;
|
||||
class QKeySequenceEdit;
|
||||
class ShortcutEdit;
|
||||
class ShortcutEditor : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ShortcutEditor(ShortcutEdit * shortcutEdit, QWidget * parent = nullptr);
|
||||
~ShortcutEditor() = default;
|
||||
|
||||
void setDescription(const QString & desc);
|
||||
|
||||
void reloadShortcuts();
|
||||
void applyShortcuts();
|
||||
|
||||
private:
|
||||
QLabel * m_descriptionLabel;
|
||||
ShortcutEdit * m_shortcutEdit;
|
||||
QFormLayout * m_shortcutLayout;
|
||||
QList<QKeySequenceEdit *> m_keySequenceEdits;
|
||||
};
|
||||
|
||||
class ShortcutEdit : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QList<QKeySequence> shortcuts MEMBER m_shortcuts WRITE setShortcuts NOTIFY shortcutsChanged)
|
||||
public:
|
||||
explicit ShortcutEdit(QWidget * parent = nullptr);
|
||||
~ShortcutEdit();
|
||||
|
||||
QList<QKeySequence> shortcuts() const;
|
||||
void setShortcuts(const QList<QKeySequence> &shortcuts);
|
||||
|
||||
signals:
|
||||
void shortcutsChanged();
|
||||
void editButtonClicked();
|
||||
void applyShortcutsRequested(QList<QKeySequence> newShortcuts);
|
||||
|
||||
private:
|
||||
QList<QKeySequence> m_shortcuts;
|
||||
QLabel * m_shortcutsLabel;
|
||||
QToolButton * m_setShortcutButton;
|
||||
};
|
||||
Reference in New Issue
Block a user