-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
189 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#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(); | ||
} | ||
}); | ||
|
||
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); | ||
|
||
reloadShortcuts(); | ||
} | ||
|
||
ShortcutEditor::~ShortcutEditor() | ||
{ | ||
|
||
} | ||
|
||
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); | ||
#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) | ||
keyseqEdit->setClearButtonEnabled(true); | ||
#endif // QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) | ||
#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.append(keyseqEdit->keySequence()); | ||
} | ||
} | ||
m_shortcutEdit->setShortcuts(shortcuts); | ||
reloadShortcuts(); | ||
} | ||
|
||
// ---------------------------------------- | ||
|
||
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 : m_shortcuts) { | ||
shortcutTexts.append(shortcut.toString()); | ||
} | ||
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(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#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(); | ||
|
||
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(); | ||
|
||
private: | ||
QList<QKeySequence> m_shortcuts; | ||
QLabel * m_shortcutsLabel; | ||
QToolButton * m_setShortcutButton; | ||
}; |