Browse Source

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.
pull/3383/head
Colomban Wendling 9 years ago
parent
commit
5fc67284cd
  1. 21
      src/widget/capslockindicator.cpp
  2. 5
      src/widget/capslockindicator.h
  3. 5
      src/widget/loginscreen.cpp

21
src/widget/capslockindicator.cpp

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
#ifdef QTOX_PLATFORM_EXT
#include "src/platform/capslock.h"
#endif
#include <QCoreApplication>
CapsLockIndicator::CapsLockIndicator(QLineEdit *parent) :
QAction(parent),
@ -9,6 +10,8 @@ CapsLockIndicator::CapsLockIndicator(QLineEdit *parent) : @@ -9,6 +10,8 @@ CapsLockIndicator::CapsLockIndicator(QLineEdit *parent) :
{
setIcon(QIcon(":img/caps_lock.svg"));
setToolTip(tr("CAPS-LOCK ENABLED"));
QCoreApplication::instance()->installEventFilter(this);
}
void CapsLockIndicator::updateIndicator()
@ -24,3 +27,21 @@ void CapsLockIndicator::updateIndicator() @@ -24,3 +27,21 @@ void CapsLockIndicator::updateIndicator()
else
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);
}

5
src/widget/capslockindicator.h

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

5
src/widget/loginscreen.cpp

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

Loading…
Cancel
Save