2014-09-10 17:27:45 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vshortcutpropertyeditor.cpp
|
|
|
|
** @author hedgeware <internal(at)hedgeware.net>
|
|
|
|
** @date
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** All rights reserved. This program and the accompanying materials
|
|
|
|
** are made available under the terms of the GNU Lesser General Public License
|
|
|
|
** (LGPL) version 2.1 which accompanies this distribution, and is available at
|
|
|
|
** http://www.gnu.org/licenses/lgpl-2.1.html
|
|
|
|
**
|
|
|
|
** This library is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
** Lesser General Public License for more details.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vshortcutpropertyeditor.h"
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QEvent>
|
2014-09-10 17:27:45 +02:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QKeyEvent>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QSizePolicy>
|
|
|
|
#include <Qt>
|
|
|
|
|
|
|
|
class QHBoxLayout;
|
|
|
|
class QKeyEvent;
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VShortcutEditWidget::VShortcutEditWidget(QWidget *parent)
|
2014-09-20 18:05:21 +02:00
|
|
|
: QWidget(parent), CurrentKeySequence(), LineEdit(nullptr)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// Create the line edit widget
|
|
|
|
LineEdit = new QLineEdit(this);
|
|
|
|
LineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
|
|
LineEdit->clear();
|
|
|
|
LineEdit->installEventFilter(this);
|
|
|
|
setFocusProxy(LineEdit);
|
2016-03-13 14:02:11 +01:00
|
|
|
connect(LineEdit, &QLineEdit::textEdited, this, &VShortcutEditWidget::onTextEdited);
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
// The layout (a horizontal layout)
|
|
|
|
QHBoxLayout* layout = new QHBoxLayout(this);
|
|
|
|
layout->setSpacing(0);
|
|
|
|
layout->setMargin(0);
|
|
|
|
layout->addWidget(LineEdit);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VShortcutEditWidget::~VShortcutEditWidget()
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// nothing needs to be done here
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
bool VPE::VShortcutEditWidget::eventFilter(QObject *obj, QEvent *event)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (obj == LineEdit)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::KeyPress)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
|
|
|
|
|
|
|
|
int keys = keyEvent->key();
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (keys != Qt::Key_Shift &&
|
2014-09-10 17:27:45 +02:00
|
|
|
keys != Qt::Key_Control &&
|
|
|
|
keys != Qt::Key_Meta &&
|
|
|
|
keys != Qt::Key_AltGr &&
|
|
|
|
keys != Qt::Key_Alt)
|
|
|
|
{
|
|
|
|
keys += keyEvent->modifiers();
|
|
|
|
setShortcut(QKeySequence(keys), true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QWidget::eventFilter(obj, event);
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
QString VPE::VShortcutEditWidget::getShortcutAsString() const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return CurrentKeySequence.toString();
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2016-12-21 13:02:55 +01:00
|
|
|
QKeySequence VPE::VShortcutEditWidget::getShortcut()
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return CurrentKeySequence;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VShortcutEditWidget::setShortcut(const QString &shortcut, bool emit_signal)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
setShortcut(QKeySequence::fromString(shortcut), emit_signal);
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VShortcutEditWidget::setShortcut(const QKeySequence &shortcut, bool emit_signal)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (shortcut != CurrentKeySequence)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
CurrentKeySequence = shortcut;
|
|
|
|
LineEdit->setText(CurrentKeySequence.toString());
|
2014-09-10 19:57:08 +02:00
|
|
|
if (emit_signal)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
emit dataChangedByUser(CurrentKeySequence, this);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VShortcutEditWidget::onTextEdited(const QString &text)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
setShortcut(text, true);
|
|
|
|
}
|