Enter key event binding in chat

This commit is contained in:
Blue 2019-04-10 18:22:52 +03:00
parent 3cc53dfaf6
commit d3f537856f
2 changed files with 26 additions and 10 deletions

View File

@ -36,7 +36,7 @@ Conversation::Conversation(Models::Contact* p_contact, QWidget* parent):
connect(contact, SIGNAL(childChanged(Models::Item*, int, int)), this, SLOT(onContactChanged(Models::Item*, int, int)));
connect(&ker, SIGNAL(enterPressed()), this, SLOT(onEnterPressed()));
m_ui->dialogBox->installEventFilter(&ker);
m_ui->messageEditor->installEventFilter(&ker);
}
Conversation::~Conversation()
@ -93,20 +93,33 @@ void Conversation::addMessage(const QMap<QString, QString>& data)
m_ui->dialogBox->append(data.value("from") + ": " + data.value("body"));
}
KeyEnterReceiver::KeyEnterReceiver(QObject* parent): QObject(parent), ownEvent(false) {}
bool KeyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
{
if (event->type()==QEvent::KeyPress) {
QEvent::Type type = event->type();
if (type == QEvent::KeyPress) {
QKeyEvent* key = static_cast<QKeyEvent*>(event);
if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
emit enterPressed();
} else {
return QObject::eventFilter(obj, event);
}
int k = key->key();
if (k == Qt::Key_Enter || k == Qt::Key_Return) {
Qt::KeyboardModifiers mod = key->modifiers();
if (mod & Qt::ControlModifier) {
mod = mod & ~Qt::ControlModifier;
QKeyEvent* nEvent = new QKeyEvent(event->type(), k, mod, key->text(), key->isAutoRepeat(), key->count());
QCoreApplication::postEvent(obj, nEvent);
ownEvent = true;
return true;
} else {
return QObject::eventFilter(obj, event);
if (ownEvent) {
ownEvent = false;
} else {
emit enterPressed();
return true;
}
return false;
}
}
}
return QObject::eventFilter(obj, event);
}
void Conversation::onEnterPressed()

View File

@ -32,7 +32,10 @@ class Conversation;
class KeyEnterReceiver : public QObject
{
Q_OBJECT
public:
KeyEnterReceiver(QObject* parent = 0);
protected:
bool ownEvent;
bool eventFilter(QObject* obj, QEvent* event);
signals: