mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Merge pull request #3383
Colomban Wendling (10): fix(capslockindicator): fix altering the line edit height fix(capslock_x11): properly release the X display handle refactor(capslockindicator): encapsulate event handling fix(capslockindicator): also update indicator when the app gets focus refactor(capslockindicator): expose as a QAction to simplify API refactor(capslockindicator): use a single shared event handler refactor(capslockindicator): avoid overhead on OSX refactor: replace CapsLockIndicator with new PasswordEdit widget fix(passwordfields): use PasswordEdit widget for all password fields refactor(passwordedit): don't add the caps indicator at all if disabled
This commit is contained in:
commit
fd2e5859fb
4
qtox.pro
4
qtox.pro
|
@ -354,7 +354,7 @@ HEADERS += \
|
|||
src/widget/about/aboutuser.h \
|
||||
src/widget/form/groupinviteform.h \
|
||||
src/widget/tool/profileimporter.h \
|
||||
src/widget/capslockindicator.h
|
||||
src/widget/passwordedit.h
|
||||
|
||||
SOURCES += \
|
||||
src/ipc.cpp \
|
||||
|
@ -471,4 +471,4 @@ SOURCES += \
|
|||
src/widget/about/aboutuser.cpp \
|
||||
src/widget/form/groupinviteform.cpp \
|
||||
src/widget/tool/profileimporter.cpp \
|
||||
src/widget/capslockindicator.cpp
|
||||
src/widget/passwordedit.cpp
|
||||
|
|
|
@ -130,11 +130,7 @@
|
|||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QLineEdit" name="newPass">
|
||||
<property name="echoMode">
|
||||
<enum>QLineEdit::Password</enum>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="PasswordEdit" name="newPass"/>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="label_8">
|
||||
|
@ -147,11 +143,7 @@
|
|||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QLineEdit" name="newPassConfirm">
|
||||
<property name="echoMode">
|
||||
<enum>QLineEdit::Password</enum>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="PasswordEdit" name="newPassConfirm"/>
|
||||
</item>
|
||||
<item row="7" column="0" colspan="2">
|
||||
<widget class="QProgressBar" name="passStrengthMeter">
|
||||
|
@ -302,11 +294,7 @@
|
|||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="loginPassword">
|
||||
<property name="echoMode">
|
||||
<enum>QLineEdit::Password</enum>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="PasswordEdit" name="loginPassword"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="autoLoginCB">
|
||||
|
@ -431,6 +419,13 @@
|
|||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>PasswordEdit</class>
|
||||
<extends>QLineEdit</extends>
|
||||
<header>src/widget/passwordedit.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources>
|
||||
<include location="../res.qrc"/>
|
||||
</resources>
|
||||
|
|
|
@ -35,6 +35,7 @@ bool Platform::capsLockEnabled()
|
|||
unsigned n;
|
||||
XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
|
||||
caps_state = (n & 0x01) == 1;
|
||||
XCloseDisplay(d);
|
||||
}
|
||||
return caps_state;
|
||||
}
|
||||
|
|
|
@ -1,54 +0,0 @@
|
|||
#include "capslockindicator.h"
|
||||
#ifdef QTOX_PLATFORM_EXT
|
||||
#include "src/platform/capslock.h"
|
||||
#endif
|
||||
|
||||
CapsLockIndicator::CapsLockIndicator(QWidget *parent) : QToolButton(parent)
|
||||
{
|
||||
cleanInputStyle = parentWidget()->styleSheet();
|
||||
|
||||
QIcon icon = QIcon(":img/caps_lock.svg");
|
||||
setIcon(icon);
|
||||
setCursor(Qt::ArrowCursor);
|
||||
setStyleSheet("border: none; padding: 0; color: white");
|
||||
setToolTip(tr("CAPS-LOCK ENABLED"));
|
||||
updateSize();
|
||||
}
|
||||
|
||||
void CapsLockIndicator::updateSize()
|
||||
{
|
||||
inputSize = parentWidget()->size();
|
||||
move(inputSize.width() - inputSize.height(), 0);
|
||||
|
||||
int side = inputSize.height() - 5;
|
||||
QSize iconSize(side, side);
|
||||
setIconSize(iconSize);
|
||||
}
|
||||
|
||||
void CapsLockIndicator::show()
|
||||
{
|
||||
QToolButton::show();
|
||||
|
||||
QString style = QString("padding: -3px %1px -3px -6px; color: white").arg(iconSize().width() - 3);
|
||||
parentWidget()->setStyleSheet(style);
|
||||
}
|
||||
|
||||
void CapsLockIndicator::hide()
|
||||
{
|
||||
QToolButton::hide();
|
||||
parentWidget()->setStyleSheet(cleanInputStyle);
|
||||
}
|
||||
|
||||
void CapsLockIndicator::updateIndicator()
|
||||
{
|
||||
bool caps = false;
|
||||
// It doesn't needed for OSX, because it shows indicator by default
|
||||
#if defined(QTOX_PLATFORM_EXT) && !defined(Q_OS_OSX)
|
||||
caps = Platform::capsLockEnabled();
|
||||
#endif
|
||||
|
||||
if (caps)
|
||||
show();
|
||||
else
|
||||
hide();
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
#ifndef CAPSLOCKINDICATOR_H
|
||||
#define CAPSLOCKINDICATOR_H
|
||||
|
||||
#include <QToolButton>
|
||||
|
||||
class CapsLockIndicator : QToolButton
|
||||
{
|
||||
public:
|
||||
CapsLockIndicator(QWidget *widget);
|
||||
void updateIndicator();
|
||||
void updateSize();
|
||||
|
||||
private:
|
||||
void show();
|
||||
void hide();
|
||||
|
||||
private:
|
||||
QString cleanInputStyle;
|
||||
QSize inputSize;
|
||||
};
|
||||
#endif // CAPSLOCKINDICATOR_H
|
|
@ -43,18 +43,10 @@
|
|||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QLineEdit" name="repasswordlineEdit">
|
||||
<property name="echoMode">
|
||||
<enum>QLineEdit::Password</enum>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="PasswordEdit" name="repasswordlineEdit"/>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QLineEdit" name="passwordlineEdit">
|
||||
<property name="echoMode">
|
||||
<enum>QLineEdit::Password</enum>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="PasswordEdit" name="passwordlineEdit"/>
|
||||
</item>
|
||||
<item row="6" column="0" colspan="2">
|
||||
<widget class="QProgressBar" name="passStrengthMeter">
|
||||
|
@ -96,6 +88,13 @@
|
|||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>PasswordEdit</class>
|
||||
<extends>QLineEdit</extends>
|
||||
<header>src/widget/passwordedit.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<tabstops>
|
||||
<tabstop>passwordlineEdit</tabstop>
|
||||
<tabstop>repasswordlineEdit</tabstop>
|
||||
|
|
|
@ -58,9 +58,6 @@ LoginScreen::LoginScreen(QWidget *parent) :
|
|||
connect(ui->autoLoginCB, &QCheckBox::stateChanged, this, &LoginScreen::onAutoLoginToggled);
|
||||
connect(ui->importButton, &QPushButton::clicked, this, &LoginScreen::onImportProfile);
|
||||
|
||||
capsIndicator = new CapsLockIndicator(ui->newPass);
|
||||
confimCapsIndicator = new CapsLockIndicator(ui->newPassConfirm);
|
||||
|
||||
reset();
|
||||
this->setStyleSheet(Style::getStylesheet(":/ui/loginScreen/loginScreen.css"));
|
||||
|
||||
|
@ -72,8 +69,6 @@ LoginScreen::~LoginScreen()
|
|||
{
|
||||
Translator::unregister(this);
|
||||
delete ui;
|
||||
delete capsIndicator;
|
||||
delete confimCapsIndicator;
|
||||
}
|
||||
|
||||
void LoginScreen::reset()
|
||||
|
@ -120,11 +115,6 @@ bool LoginScreen::event(QEvent* event)
|
|||
emit windowStateChanged(windowState());
|
||||
break;
|
||||
#endif
|
||||
case QEvent::Show:
|
||||
case QEvent::KeyRelease:
|
||||
capsIndicator->updateIndicator();
|
||||
confimCapsIndicator->updateIndicator();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -136,8 +126,6 @@ bool LoginScreen::event(QEvent* event)
|
|||
void LoginScreen::onNewProfilePageClicked()
|
||||
{
|
||||
ui->stackedWidget->setCurrentIndex(0);
|
||||
capsIndicator->updateSize();
|
||||
confimCapsIndicator->updateSize();
|
||||
}
|
||||
|
||||
void LoginScreen::onLoginPageClicked()
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
#ifndef LOGINSCREEN_H
|
||||
#define LOGINSCREEN_H
|
||||
|
||||
#include "capslockindicator.h"
|
||||
#include <QWidget>
|
||||
#include <QShortcut>
|
||||
#include <QToolButton>
|
||||
|
@ -67,8 +66,6 @@ private:
|
|||
private:
|
||||
Ui::LoginScreen *ui;
|
||||
QShortcut quitShortcut;
|
||||
CapsLockIndicator *capsIndicator;
|
||||
CapsLockIndicator *confimCapsIndicator;
|
||||
};
|
||||
|
||||
#endif // LOGINSCREEN_H
|
||||
|
|
103
src/widget/passwordedit.cpp
Normal file
103
src/widget/passwordedit.cpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
#include "passwordedit.h"
|
||||
#ifdef QTOX_PLATFORM_EXT
|
||||
#include "src/platform/capslock.h"
|
||||
#endif
|
||||
#include <QCoreApplication>
|
||||
|
||||
// It isn't needed for OSX, because it shows indicator by default
|
||||
#if defined(QTOX_PLATFORM_EXT) && !defined(Q_OS_OSX)
|
||||
#define ENABLE_CAPSLOCK_INDICATOR
|
||||
#endif
|
||||
|
||||
PasswordEdit::EventHandler* PasswordEdit::eventHandler{nullptr};
|
||||
|
||||
PasswordEdit::PasswordEdit(QWidget* parent) :
|
||||
QLineEdit(parent),
|
||||
action(new QAction(this))
|
||||
{
|
||||
setEchoMode(QLineEdit::Password);
|
||||
|
||||
#ifdef ENABLE_CAPSLOCK_INDICATOR
|
||||
action->setIcon(QIcon(":img/caps_lock.svg"));
|
||||
action->setToolTip(tr("CAPS-LOCK ENABLED"));
|
||||
addAction(action, QLineEdit::TrailingPosition);
|
||||
#endif
|
||||
}
|
||||
|
||||
PasswordEdit::~PasswordEdit()
|
||||
{
|
||||
unregisterHandler();
|
||||
}
|
||||
|
||||
void PasswordEdit::registerHandler()
|
||||
{
|
||||
#ifdef ENABLE_CAPSLOCK_INDICATOR
|
||||
if (!eventHandler)
|
||||
eventHandler = new EventHandler();
|
||||
if (!eventHandler->actions.contains(action))
|
||||
eventHandler->actions.append(action);
|
||||
#endif
|
||||
}
|
||||
|
||||
void PasswordEdit::unregisterHandler()
|
||||
{
|
||||
#ifdef ENABLE_CAPSLOCK_INDICATOR
|
||||
if (eventHandler && eventHandler->actions.contains(action))
|
||||
{
|
||||
eventHandler->actions.removeOne(action);
|
||||
if (eventHandler->actions.isEmpty())
|
||||
{
|
||||
delete eventHandler;
|
||||
eventHandler = nullptr;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void PasswordEdit::showEvent(QShowEvent*)
|
||||
{
|
||||
#ifdef ENABLE_CAPSLOCK_INDICATOR
|
||||
action->setVisible(Platform::capsLockEnabled());
|
||||
#endif
|
||||
registerHandler();
|
||||
}
|
||||
|
||||
void PasswordEdit::hideEvent(QHideEvent*)
|
||||
{
|
||||
unregisterHandler();
|
||||
}
|
||||
|
||||
#ifdef ENABLE_CAPSLOCK_INDICATOR
|
||||
PasswordEdit::EventHandler::EventHandler()
|
||||
{
|
||||
QCoreApplication::instance()->installEventFilter(this);
|
||||
}
|
||||
|
||||
PasswordEdit::EventHandler::~EventHandler()
|
||||
{
|
||||
QCoreApplication::instance()->removeEventFilter(this);
|
||||
}
|
||||
|
||||
void PasswordEdit::EventHandler::updateActions()
|
||||
{
|
||||
bool caps = Platform::capsLockEnabled();
|
||||
|
||||
for (QAction* action : actions)
|
||||
action->setVisible(caps);
|
||||
}
|
||||
|
||||
bool PasswordEdit::EventHandler::eventFilter(QObject *obj, QEvent *event)
|
||||
{
|
||||
switch (event->type())
|
||||
{
|
||||
case QEvent::WindowActivate:
|
||||
case QEvent::KeyRelease:
|
||||
updateActions();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return QObject::eventFilter(obj, event);
|
||||
}
|
||||
#endif // ENABLE_CAPSLOCK_INDICATOR
|
37
src/widget/passwordedit.h
Normal file
37
src/widget/passwordedit.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
#ifndef PASSWORDEDIT_H
|
||||
#define PASSWORDEDIT_H
|
||||
|
||||
#include <QAction>
|
||||
#include <QLineEdit>
|
||||
|
||||
class PasswordEdit : public QLineEdit
|
||||
{
|
||||
public:
|
||||
PasswordEdit(QWidget *parent);
|
||||
~PasswordEdit();
|
||||
|
||||
protected:
|
||||
virtual void showEvent(QShowEvent* event);
|
||||
virtual void hideEvent(QHideEvent* event);
|
||||
|
||||
private:
|
||||
class EventHandler : QObject
|
||||
{
|
||||
public:
|
||||
QVector<QAction*> actions;
|
||||
|
||||
EventHandler();
|
||||
~EventHandler();
|
||||
void updateActions();
|
||||
bool eventFilter(QObject *obj, QEvent *event);
|
||||
};
|
||||
|
||||
void registerHandler();
|
||||
void unregisterHandler();
|
||||
|
||||
private:
|
||||
QAction* action;
|
||||
|
||||
static EventHandler* eventHandler;
|
||||
};
|
||||
#endif // PASSWORDEDIT_H
|
Loading…
Reference in New Issue
Block a user