1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(capslockindicator): encapsulate event handling

Use an event filter on QCoreApplication instead of requiring the caller
to manually call updateIndicator() when the caps lock state changed.
This commit is contained in:
Colomban Wendling 2016-06-15 20:21:23 +02:00
parent f9190734d7
commit 5fc67284cd
3 changed files with 26 additions and 5 deletions

View File

@ -2,6 +2,7 @@
#ifdef QTOX_PLATFORM_EXT #ifdef QTOX_PLATFORM_EXT
#include "src/platform/capslock.h" #include "src/platform/capslock.h"
#endif #endif
#include <QCoreApplication>
CapsLockIndicator::CapsLockIndicator(QLineEdit *parent) : CapsLockIndicator::CapsLockIndicator(QLineEdit *parent) :
QAction(parent), QAction(parent),
@ -9,6 +10,8 @@ CapsLockIndicator::CapsLockIndicator(QLineEdit *parent) :
{ {
setIcon(QIcon(":img/caps_lock.svg")); setIcon(QIcon(":img/caps_lock.svg"));
setToolTip(tr("CAPS-LOCK ENABLED")); setToolTip(tr("CAPS-LOCK ENABLED"));
QCoreApplication::instance()->installEventFilter(this);
} }
void CapsLockIndicator::updateIndicator() void CapsLockIndicator::updateIndicator()
@ -24,3 +27,21 @@ void CapsLockIndicator::updateIndicator()
else else
parent->removeAction(this); parent->removeAction(this);
} }
bool CapsLockIndicator::eventFilter(QObject *obj, QEvent *event)
{
switch (event->type())
{
case QEvent::Show:
if (obj == this)
updateIndicator();
break;
case QEvent::KeyRelease:
updateIndicator();
break;
default:
break;
}
return QAction::eventFilter(obj, event);
}

View File

@ -8,6 +8,11 @@ class CapsLockIndicator : QAction
{ {
public: public:
CapsLockIndicator(QLineEdit *widget); CapsLockIndicator(QLineEdit *widget);
protected:
bool eventFilter(QObject *obj, QEvent *event);
private:
void updateIndicator(); void updateIndicator();
private: private:

View File

@ -120,11 +120,6 @@ bool LoginScreen::event(QEvent* event)
emit windowStateChanged(windowState()); emit windowStateChanged(windowState());
break; break;
#endif #endif
case QEvent::Show:
case QEvent::KeyRelease:
capsIndicator->updateIndicator();
confimCapsIndicator->updateIndicator();
break;
default: default:
break; break;
} }