qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
passwordedit.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "passwordedit.h"
21 #ifdef QTOX_PLATFORM_EXT
22 #include "src/platform/capslock.h"
23 #endif
24 #include <QCoreApplication>
25 
26 // It isn't needed for OSX, because it shows indicator by default
27 #if defined(QTOX_PLATFORM_EXT) && !defined(Q_OS_OSX)
28 #define ENABLE_CAPSLOCK_INDICATOR
29 #endif
30 
32 
34  : QLineEdit(parent)
35  , action(new QAction(this))
36 {
37  setEchoMode(QLineEdit::Password);
38 
39 #ifdef ENABLE_CAPSLOCK_INDICATOR
40  action->setIcon(QIcon(":img/caps_lock.svg"));
41  action->setToolTip(tr("CAPS-LOCK ENABLED"));
42  addAction(action, QLineEdit::TrailingPosition);
43 #endif
44 }
45 
47 {
49 }
50 
52 {
53 #ifdef ENABLE_CAPSLOCK_INDICATOR
54  if (!eventHandler)
55  eventHandler = new EventHandler();
56  if (!eventHandler->actions.contains(action))
57  eventHandler->actions.append(action);
58 #endif
59 }
60 
62 {
63 #ifdef ENABLE_CAPSLOCK_INDICATOR
64  int idx;
65 
66  if (eventHandler && (idx = eventHandler->actions.indexOf(action)) >= 0) {
67  eventHandler->actions.remove(idx);
68  if (eventHandler->actions.isEmpty()) {
69  delete eventHandler;
70  eventHandler = nullptr;
71  }
72  }
73 #endif
74 }
75 
76 void PasswordEdit::showEvent(QShowEvent*)
77 {
78 #ifdef ENABLE_CAPSLOCK_INDICATOR
79  action->setVisible(Platform::capsLockEnabled());
80 #endif
82 }
83 
84 void PasswordEdit::hideEvent(QHideEvent*)
85 {
87 }
88 
89 #ifdef ENABLE_CAPSLOCK_INDICATOR
91 {
92  QCoreApplication::instance()->installEventFilter(this);
93 }
94 
96 {
97  QCoreApplication::instance()->removeEventFilter(this);
98 }
99 
101 {
102  bool caps = Platform::capsLockEnabled();
103 
104  for (QAction* action : actions)
105  action->setVisible(caps);
106 }
107 
108 bool PasswordEdit::EventHandler::eventFilter(QObject* obj, QEvent* event)
109 {
110  switch (event->type()) {
111  case QEvent::WindowActivate:
112  case QEvent::KeyRelease:
113  updateActions();
114  break;
115  default:
116  break;
117  }
118 
119  return QObject::eventFilter(obj, event);
120 }
121 #endif // ENABLE_CAPSLOCK_INDICATOR
PasswordEdit::EventHandler::updateActions
void updateActions()
PasswordEdit::EventHandler::EventHandler
EventHandler()
PasswordEdit::eventHandler
static EventHandler * eventHandler
Definition: passwordedit.h:54
PasswordEdit::EventHandler
Definition: passwordedit.h:37
passwordedit.h
PasswordEdit::EventHandler::eventFilter
bool eventFilter(QObject *obj, QEvent *event)
PasswordEdit::PasswordEdit
PasswordEdit(QWidget *parent)
Definition: passwordedit.cpp:33
PasswordEdit::EventHandler::actions
QVector< QAction * > actions
Definition: passwordedit.h:40
PasswordEdit::~PasswordEdit
~PasswordEdit()
Definition: passwordedit.cpp:46
PasswordEdit::registerHandler
void registerHandler()
Definition: passwordedit.cpp:51
PasswordEdit::action
QAction * action
Definition: passwordedit.h:52
PasswordEdit::unregisterHandler
void unregisterHandler()
Definition: passwordedit.cpp:61
PasswordEdit::EventHandler::~EventHandler
~EventHandler()
PasswordEdit::showEvent
virtual void showEvent(QShowEvent *event)
Definition: passwordedit.cpp:76
PasswordEdit::hideEvent
virtual void hideEvent(QHideEvent *event)
Definition: passwordedit.cpp:84
capslock.h