Configurable shortcuts

This commit is contained in:
marco 2015-08-09 20:48:21 +02:00
parent b0bf94e378
commit beaa4e9383
17 changed files with 1075 additions and 409 deletions

View file

@ -0,0 +1,150 @@
#include "secuenceedit.h"
#include "../settingscache.h"
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QEvent>
#include <QKeyEvent>
#include <QToolTip>
SecuenceEdit::SecuenceEdit(QString name, QWidget *parent) : QWidget(parent)
{
this->shorcutName = name;
currentKey = 0;
maxKeys = 4;
keys = 0;
valid = false;
lineEdit = new QLineEdit(this);
clearButton = new QPushButton("", this);
defaultButton = new QPushButton("", this);
lineEdit->setMinimumWidth(100);
clearButton->setMaximumWidth(lineEdit->height());
defaultButton->setMaximumWidth(lineEdit->height());
clearButton->setIcon(QIcon(":/resources/icon_clearsearch.svg"));
defaultButton->setIcon(QIcon(":/resources/icon_update.png"));
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
layout->addWidget(lineEdit);
layout->addWidget(clearButton);
layout->addWidget(defaultButton);
connect(clearButton,SIGNAL(clicked()),this,SLOT(removeLastShortcut()));
connect(defaultButton,SIGNAL(clicked()),this,SLOT(restoreDefault()));
lineEdit->installEventFilter(this);
lineEdit->setText(settingsCache->shortcuts().getShortcutString(name));
}
QString SecuenceEdit::getSecuence()
{
return lineEdit->text();
}
void SecuenceEdit::removeLastShortcut()
{
QString secuences = lineEdit->text();
if(!secuences.isEmpty())
{
if(secuences.lastIndexOf(";") > 0){
QString valid = secuences.left(secuences.lastIndexOf(";"));
lineEdit->setText(valid);
}
else
lineEdit->clear();
updateSettings();
}
}
void SecuenceEdit::restoreDefault()
{
lineEdit->setText(settingsCache->shortcuts().getDefaultShortcutString(shorcutName));
updateSettings();
}
bool SecuenceEdit::eventFilter(QObject *, QEvent * event)
{
if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
{
QKeyEvent * keyEvent = (QKeyEvent *) event;
if(event->type() == QEvent::KeyPress && !keyEvent->isAutoRepeat())
processKey(keyEvent);
else if (event->type() == QEvent::KeyRelease && !keyEvent->isAutoRepeat())
finishShortcut();
return true;
}
return false;
}
void SecuenceEdit::processKey(QKeyEvent* e)
{
int key = e->key();
if(key != Qt::Key_Control && key != Qt::Key_Shift
&& key != Qt::Key_Meta && key != Qt::Key_Alt)
{
valid = true;
key |= translateModifiers(e->modifiers(), e->text());
}
keys = key;
currentKey++;
if(currentKey >= key)
finishShortcut();
}
int SecuenceEdit::translateModifiers(Qt::KeyboardModifiers state, const QString &text)
{
int result = 0;
// The shift modifier only counts when it is not used to type a symbol
// that is only reachable using the shift key anyway
if ((state & Qt::ShiftModifier) && (text.isEmpty() ||
!text.at(0).isPrint() ||
text.at(0).isLetterOrNumber() ||
text.at(0).isSpace()))
result |= Qt::SHIFT;
if (state & Qt::ControlModifier)
result |= Qt::CTRL;
if (state & Qt::MetaModifier)
result |= Qt::META;
if (state & Qt::AltModifier)
result |= Qt::ALT;
return result;
}
void SecuenceEdit::finishShortcut()
{
QKeySequence secuence(keys);
if(!secuence.isEmpty() && valid)
{
QString secuenceString = secuence.toString();
if(settingsCache->shortcuts().isValid(shorcutName,secuenceString))
{
if(!lineEdit->text().isEmpty())
{
if(lineEdit->text().contains(secuenceString))
return;
lineEdit->setText(lineEdit->text() + ";");
}
lineEdit->setText(lineEdit->text() + secuenceString);
}
else
{
QToolTip::showText(lineEdit->mapToGlobal(QPoint()), tr("Shortcut already in use"));
}
}
currentKey = 0;
keys = 0;
valid = false;
updateSettings();
}
void SecuenceEdit::updateSettings()
{
settingsCache->shortcuts().setShortcuts(shorcutName,lineEdit->text());
}

View file

@ -0,0 +1,40 @@
#ifndef SECUENCEEDIT_H
#define SECUENCEEDIT_H
#include <QWidget>
#include <QKeySequence>
class QLineEdit;
class QPushButton;
class QEvent;
class SecuenceEdit : public QWidget
{
Q_OBJECT
public:
SecuenceEdit(QString shorcutName, QWidget *parent = 0);
QString getSecuence();
signals:
private slots:
void removeLastShortcut();
void restoreDefault();
protected:
bool eventFilter(QObject *, QEvent *event);
private:
QString shorcutName;
QLineEdit *lineEdit;
QPushButton *clearButton;
QPushButton *defaultButton;
int keys;
int currentKey;
int maxKeys;
bool valid;
void processKey(QKeyEvent *e);
int translateModifiers(Qt::KeyboardModifiers state, const QString &text);
void finishShortcut();
void updateSettings();
};
#endif // SECUENCEEDIT_H

View file

@ -0,0 +1,18 @@
#include "shortcutstab.h"
#include "ui_shortcutstab.h"
ShortcutsTab::ShortcutsTab() :
ui(new Ui::shortcutsTab)
{
ui->setupUi(this);
}
void ShortcutsTab::retranslateUi()
{
ui->retranslateUi(this);
}
ShortcutsTab::~ShortcutsTab()
{
delete ui;
}

View file

@ -0,0 +1,25 @@
#ifndef SHORTCUTSTAB_H
#define SHORTCUTSTAB_H
#include <QWidget>
#include "../dlg_settings.h"
namespace Ui {
class shortcutsTab;
}
class ShortcutsTab : public AbstractSettingsPage
{
Q_OBJECT
public:
ShortcutsTab();
void retranslateUi();
~ShortcutsTab();
private:
Ui::shortcutsTab *ui;
};
#endif // SHORTCUTSTAB_H