Gui: Make AccelLineEdit a subclass of QKeySequenceEdit
The class QKeySequenceEdit already implements the relevant functionality and properly handles shortcuts using the Shift key. This fixes issue 19320
This commit is contained in:
@@ -219,8 +219,8 @@ void DlgCustomActionsImp::onActionListWidgetItemActivated(QTreeWidgetItem* item)
|
||||
ui->actionMenu->setText(QString::fromUtf8(pScript->getMenuText()));
|
||||
ui->actionToolTip->setText(QString::fromUtf8(pScript->getToolTipText()));
|
||||
ui->actionStatus->setText(QString::fromUtf8(pScript->getStatusTip()));
|
||||
ui->actionAccel->setText(
|
||||
ShortcutManager::instance()->getShortcut(actionName.constData(), pScript->getAccel()));
|
||||
ui->actionAccel->setKeySequence(QKeySequence(
|
||||
ShortcutManager::instance()->getShortcut(actionName.constData(), pScript->getAccel())));
|
||||
ui->pixmapLabel->clear();
|
||||
m_sPixmap.clear();
|
||||
const char* name = pScript->getPixmap();
|
||||
@@ -290,9 +290,10 @@ void DlgCustomActionsImp::onButtonAddActionClicked()
|
||||
ui->pixmapLabel->clear();
|
||||
m_sPixmap.clear();
|
||||
|
||||
if (!ui->actionAccel->text().isEmpty()) {
|
||||
if (!ui->actionAccel->isEmpty()) {
|
||||
QString text = ui->actionAccel->text();
|
||||
ShortcutManager::instance()->setShortcut(actionName.constData(),
|
||||
ui->actionAccel->text().toLatin1().constData());
|
||||
text.toLatin1().constData());
|
||||
}
|
||||
ui->actionAccel->clear();
|
||||
|
||||
@@ -353,8 +354,9 @@ void DlgCustomActionsImp::onButtonReplaceActionClicked()
|
||||
ui->pixmapLabel->clear();
|
||||
m_sPixmap.clear();
|
||||
|
||||
if (!ui->actionAccel->text().isEmpty()) {
|
||||
macro->setAccel(ui->actionAccel->text().toLatin1());
|
||||
if (!ui->actionAccel->isEmpty()) {
|
||||
QString text = ui->actionAccel->text();
|
||||
macro->setAccel(text.toLatin1());
|
||||
}
|
||||
ui->actionAccel->clear();
|
||||
|
||||
@@ -521,7 +523,6 @@ void DlgCustomActionsImp::changeEvent(QEvent* e)
|
||||
ui->retranslateUi(this);
|
||||
ui->actionListWidget->clear();
|
||||
showActions();
|
||||
ui->actionAccel->setText(qApp->translate("Gui::AccelLineEdit", "none"));
|
||||
}
|
||||
QWidget::changeEvent(e);
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ void DlgCustomKeyboardImp::setupConnections()
|
||||
this, &DlgCustomKeyboardImp::onButtonResetClicked);
|
||||
connect(ui->buttonResetAll, &QPushButton::clicked,
|
||||
this, &DlgCustomKeyboardImp::onButtonResetAllClicked);
|
||||
connect(ui->editShortcut, &AccelLineEdit::textChanged,
|
||||
connect(ui->editShortcut, &AccelLineEdit::keySequenceChanged,
|
||||
this, &DlgCustomKeyboardImp::onEditShortcutTextChanged);
|
||||
// clang-format on
|
||||
}
|
||||
@@ -336,11 +336,11 @@ DlgCustomKeyboardImp::initCommandWidgets(QTreeWidget* commandTreeWidget,
|
||||
auto timer = new QTimer(priorityList);
|
||||
timer->setSingleShot(true);
|
||||
if (currentShortcut) {
|
||||
QObject::connect(currentShortcut, &QLineEdit::textChanged, timer, [timer]() {
|
||||
QObject::connect(currentShortcut, &AccelLineEdit::keySequenceChanged, timer, [timer]() {
|
||||
timer->start(200);
|
||||
});
|
||||
}
|
||||
QObject::connect(editShortcut, &QLineEdit::textChanged, timer, [timer]() {
|
||||
QObject::connect(editShortcut, &AccelLineEdit::keySequenceChanged, timer, [timer]() {
|
||||
timer->start(200);
|
||||
});
|
||||
QObject::connect(ShortcutManager::instance(),
|
||||
@@ -368,10 +368,10 @@ void DlgCustomKeyboardImp::populatePriorityList(QTreeWidget* priorityList,
|
||||
|
||||
priorityList->clear();
|
||||
QString sc;
|
||||
if (!editor->isNone() && editor->text().size()) {
|
||||
if (!editor->isEmpty()) {
|
||||
sc = editor->text();
|
||||
}
|
||||
else if (curShortcut && !curShortcut->isNone()) {
|
||||
else if (curShortcut && !curShortcut->isEmpty()) {
|
||||
sc = curShortcut->text();
|
||||
}
|
||||
|
||||
@@ -466,10 +466,10 @@ void DlgCustomKeyboardImp::onCommandTreeWidgetCurrentItemChanged(QTreeWidgetItem
|
||||
QKeySequence ks2 = QString::fromLatin1(cmd->getAccel());
|
||||
QKeySequence ks3 = ui->editShortcut->text();
|
||||
if (ks.isEmpty()) {
|
||||
ui->accelLineEditShortcut->setText(tr("none"));
|
||||
ui->accelLineEditShortcut->clear();
|
||||
}
|
||||
else {
|
||||
ui->accelLineEditShortcut->setText(ks.toString(QKeySequence::NativeText));
|
||||
ui->accelLineEditShortcut->setKeySequence(ks);
|
||||
}
|
||||
|
||||
ui->buttonAssign->setEnabled(!ui->editShortcut->text().isEmpty() && (ks != ks3));
|
||||
@@ -500,7 +500,7 @@ void DlgCustomKeyboardImp::setShortcutOfCurrentAction(const QString& accelText)
|
||||
if (!accelText.isEmpty()) {
|
||||
QKeySequence shortcut = accelText;
|
||||
portableText = shortcut.toString(QKeySequence::PortableText);
|
||||
ui->accelLineEditShortcut->setText(accelText);
|
||||
ui->accelLineEditShortcut->setKeySequence(shortcut);
|
||||
ui->editShortcut->clear();
|
||||
}
|
||||
else {
|
||||
@@ -538,7 +538,7 @@ void DlgCustomKeyboardImp::onButtonResetClicked()
|
||||
ShortcutManager::instance()->reset(name);
|
||||
|
||||
QString txt = ShortcutManager::instance()->getShortcut(name);
|
||||
ui->accelLineEditShortcut->setText((txt.isEmpty() ? tr("none") : txt));
|
||||
ui->accelLineEditShortcut->setKeySequence(QKeySequence(txt));
|
||||
ui->buttonReset->setEnabled(false);
|
||||
}
|
||||
|
||||
@@ -550,7 +550,7 @@ void DlgCustomKeyboardImp::onButtonResetAllClicked()
|
||||
}
|
||||
|
||||
/** Checks for an already occupied shortcut. */
|
||||
void DlgCustomKeyboardImp::onEditShortcutTextChanged(const QString&)
|
||||
void DlgCustomKeyboardImp::onEditShortcutTextChanged(const QKeySequence&)
|
||||
{
|
||||
QTreeWidgetItem* item = ui->commandTreeWidget->currentItem();
|
||||
if (item) {
|
||||
@@ -560,7 +560,7 @@ void DlgCustomKeyboardImp::onEditShortcutTextChanged(const QString&)
|
||||
CommandManager& cCmdMgr = Application::Instance->commandManager();
|
||||
Command* cmd = cCmdMgr.getCommandByName(name.constData());
|
||||
|
||||
if (!ui->editShortcut->isNone()) {
|
||||
if (!ui->editShortcut->isEmpty()) {
|
||||
ui->buttonAssign->setEnabled(true);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -108,7 +108,7 @@ protected:
|
||||
void onButtonClearClicked();
|
||||
void onButtonResetClicked();
|
||||
void onButtonResetAllClicked();
|
||||
void onEditShortcutTextChanged(const QString&);
|
||||
void onEditShortcutTextChanged(const QKeySequence&);
|
||||
|
||||
protected Q_SLOTS:
|
||||
void onAddMacroAction(const QByteArray&) override;
|
||||
|
||||
@@ -358,105 +358,39 @@ void ActionSelector::onDownButtonClicked()
|
||||
|
||||
/**
|
||||
* Constructs a line edit with no text.
|
||||
* The \a parent argument is sent to the QLineEdit constructor.
|
||||
* The \a parent argument is sent to the QKeySequenceEdit constructor.
|
||||
*/
|
||||
AccelLineEdit::AccelLineEdit ( QWidget * parent )
|
||||
: QLineEdit(parent)
|
||||
AccelLineEdit::AccelLineEdit(QWidget* parent)
|
||||
: QKeySequenceEdit(parent)
|
||||
{
|
||||
setPlaceholderText(tr("Press a keyboard shortcut"));
|
||||
setClearButtonEnabled(true);
|
||||
keyPressedCount = 0;
|
||||
if (auto le = findChild<QLineEdit*>()) {
|
||||
le->setClearButtonEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
bool AccelLineEdit::isNone() const
|
||||
AccelLineEdit::AccelLineEdit(const QKeySequence& keySequence, QWidget* parent)
|
||||
: QKeySequenceEdit(keySequence, parent)
|
||||
{
|
||||
return text().isEmpty();
|
||||
if (auto le = findChild<QLineEdit*>()) {
|
||||
le->setClearButtonEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks which keys are pressed and show it as text.
|
||||
*/
|
||||
void AccelLineEdit::keyPressEvent (QKeyEvent * e)
|
||||
void AccelLineEdit::setReadOnly(bool value)
|
||||
{
|
||||
if (isReadOnly()) {
|
||||
QLineEdit::keyPressEvent(e);
|
||||
return;
|
||||
if (auto le = findChild<QLineEdit*>()) {
|
||||
le->setReadOnly(value);
|
||||
}
|
||||
}
|
||||
|
||||
QString txtLine = text();
|
||||
bool AccelLineEdit::isEmpty() const
|
||||
{
|
||||
return keySequence().isEmpty();
|
||||
}
|
||||
|
||||
int key = e->key();
|
||||
Qt::KeyboardModifiers state = e->modifiers();
|
||||
|
||||
// Backspace clears the shortcut if text is present, else sets Backspace as shortcut.
|
||||
// If a modifier is pressed without any other key, return.
|
||||
// AltGr is not a modifier but doesn't have a QString representation.
|
||||
switch(key) {
|
||||
case Qt::Key_Backspace:
|
||||
case Qt::Key_Delete:
|
||||
if (state == Qt::NoModifier) {
|
||||
keyPressedCount = 0;
|
||||
if (isNone()) {
|
||||
QKeySequence ks(key);
|
||||
setText(ks.toString(QKeySequence::NativeText));
|
||||
}
|
||||
else {
|
||||
clear();
|
||||
}
|
||||
}
|
||||
case Qt::Key_Control:
|
||||
case Qt::Key_Shift:
|
||||
case Qt::Key_Alt:
|
||||
case Qt::Key_Meta:
|
||||
case Qt::Key_AltGr:
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (txtLine.isEmpty()) {
|
||||
// Text maybe cleared by QLineEdit's built in clear button
|
||||
keyPressedCount = 0;
|
||||
} else {
|
||||
// 4 keys are allowed for QShortcut
|
||||
switch (keyPressedCount) {
|
||||
case 4:
|
||||
keyPressedCount = 0;
|
||||
txtLine.clear();
|
||||
break;
|
||||
case 0:
|
||||
txtLine.clear();
|
||||
break;
|
||||
default:
|
||||
txtLine += QStringLiteral(",");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Handles modifiers applying a mask.
|
||||
if ((state & Qt::ControlModifier) == Qt::ControlModifier) {
|
||||
QKeySequence ks(Qt::CTRL);
|
||||
txtLine += ks.toString(QKeySequence::NativeText);
|
||||
}
|
||||
if ((state & Qt::AltModifier) == Qt::AltModifier) {
|
||||
QKeySequence ks(Qt::ALT);
|
||||
txtLine += ks.toString(QKeySequence::NativeText);
|
||||
}
|
||||
if ((state & Qt::ShiftModifier) == Qt::ShiftModifier) {
|
||||
QKeySequence ks(Qt::SHIFT);
|
||||
txtLine += ks.toString(QKeySequence::NativeText);
|
||||
}
|
||||
if ((state & Qt::MetaModifier) == Qt::MetaModifier) {
|
||||
QKeySequence ks(Qt::META);
|
||||
txtLine += ks.toString(QKeySequence::NativeText);
|
||||
}
|
||||
|
||||
// Handles normal keys
|
||||
QKeySequence ks(key);
|
||||
txtLine += ks.toString(QKeySequence::NativeText);
|
||||
|
||||
setText(txtLine);
|
||||
keyPressedCount++;
|
||||
QString AccelLineEdit::text() const
|
||||
{
|
||||
return keySequence().toString(QKeySequence::NativeText);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include <QButtonGroup>
|
||||
#include <QDialog>
|
||||
#include <QElapsedTimer>
|
||||
#include <QKeySequenceEdit>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QListWidget>
|
||||
@@ -132,19 +133,16 @@ private:
|
||||
* The AccelLineEdit class provides a lineedit to specify shortcuts.
|
||||
* \author Werner Mayer
|
||||
*/
|
||||
class GuiExport AccelLineEdit : public QLineEdit
|
||||
class GuiExport AccelLineEdit : public QKeySequenceEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
AccelLineEdit(QWidget * parent=nullptr);
|
||||
bool isNone() const;
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent * e) override;
|
||||
|
||||
private:
|
||||
int keyPressedCount;
|
||||
explicit AccelLineEdit(QWidget* parent = nullptr);
|
||||
explicit AccelLineEdit(const QKeySequence& keySequence, QWidget* parent = nullptr);
|
||||
void setReadOnly(bool value);
|
||||
bool isEmpty() const;
|
||||
QString text() const;
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user