Skip to content

Commit

Permalink
refactor shortcut mechanisms
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinmo committed May 9, 2019
1 parent 6abfb79 commit 83cab50
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
16 changes: 16 additions & 0 deletions palette/include/widgets/qpalette_inner.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,27 @@ class PALETTE_EXPORT QPaletteInner : public QFrame {
QItems* items_;
QShortcut* shortcut_;

// Registered shortcuts not overriden by Qt::ShortcutOverride event
QSet<QKeySequence> registered_keys_;

bool eventFilter(QObject* obj, QEvent* event) override;
void keyPressEvent(QKeyEvent* e) override;
void showEvent(QShowEvent* event) override;
void closeEvent(QCloseEvent* event) override;

void arrowPressed(int delta);

template<typename T>
QShortcut *registerShortcut(QKeySequence sequence, T callback, bool override=true) {
if(override)
registered_keys_.insert(sequence);

auto *shortcut = new QShortcut(QKeySequence(sequence), this);
connect(shortcut, &QShortcut::activated, callback);

return shortcut;
}

public:
QPaletteInner(QWidget* parent, const QString& name, const QVector<Action>& items, const QString& closeKey);

Expand Down
41 changes: 26 additions & 15 deletions palette/src/widgets/qpalette_inner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,34 @@ QPaletteInner::QPaletteInner(QWidget* parent, const QString& name, const QVector
items_->installEventFilter(this);

if (!closeKey.isEmpty()) {
shortcut_ = new QShortcut(QKeySequence(closeKey), this);
connect(shortcut_, &QShortcut::activated, this, &QPaletteInner::close);
shortcut_ = registerShortcut({ closeKey }, [=]() {close(); }, false);
}

registerShortcut({ "Ctrl+J" }, [=]() {arrowPressed(-1); });
registerShortcut({ "Ctrl+K" }, [=]() {arrowPressed(+1); });
}

void QPaletteInner::arrowPressed(int delta) {
auto new_row = items_->currentIndex().row() + delta;
auto row_count = items_->model()->rowCount();

if (new_row < 0)
new_row = 0;
else if (new_row >= row_count)
new_row = row_count - 1;

items_->setCurrentIndex(items_->model()->index(new_row, 0));
}

void QPaletteInner::keyPressEvent(QKeyEvent* e) {
void QPaletteInner::keyPressEvent(QKeyEvent * e) {
if (e->key() != Qt::Key_Escape)
QFrame::keyPressEvent(e);
else {
close();
}
}

bool QPaletteInner::eventFilter(QObject* obj, QEvent* event) {
bool QPaletteInner::eventFilter(QObject * obj, QEvent * event) {
switch (event->type()) {
case QEvent::KeyPress: {
auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
Expand All @@ -83,15 +97,7 @@ bool QPaletteInner::eventFilter(QObject* obj, QEvent* event) {
case Qt::Key_Down:
case Qt::Key_Up: {
/* We manually process Up/Down key to handle corner cases and for ease of edits */
int delta = keyEvent->key() == Qt::Key_Down ? 1 : -1;
auto new_row = items_->currentIndex().row() + delta;

if (new_row == -1)
new_row = 0;
else if (new_row == items_->model()->rowCount())
new_row--;

items_->setCurrentIndex(items_->model()->index(new_row, 0));
arrowPressed(keyEvent->key() == Qt::Key_Down ? 1 : -1);
return true;
}
case Qt::Key_Escape:
Expand Down Expand Up @@ -119,8 +125,13 @@ bool QPaletteInner::eventFilter(QObject* obj, QEvent* event) {
}
}
case QEvent::ShortcutOverride: {
/* Handling ShortcutOverride prevents UI from hooking the shortcuts used in the command palette */
event->accept();
auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
/*
Handling ShortcutOverride prevents UI from hooking the shortcuts used in the command palette.
If the shortcut is registered by registerShortcut(), it's not overriden.
*/
if(!registered_keys_.contains(QKeySequence(keyEvent->key() | keyEvent->modifiers())))
event->accept();
return true;
}
default:
Expand Down

0 comments on commit 83cab50

Please sign in to comment.