Merge pull request #334 from Kirillpt/issue_292

fix #292
This commit is contained in:
DeepBlueV7.X 2020-11-25 00:22:15 +01:00 committed by GitHub
commit 45e91a7ee4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 8 deletions

View File

@ -16,9 +16,11 @@
*/ */
#include <QDesktopServices> #include <QDesktopServices>
#include <QFontMetrics>
#include <QLabel> #include <QLabel>
#include <QPainter> #include <QPainter>
#include <QStyleOption> #include <QStyleOption>
#include <QtMath>
#include <mtx/identifiers.hpp> #include <mtx/identifiers.hpp>
#include <mtx/requests.hpp> #include <mtx/requests.hpp>
@ -95,8 +97,6 @@ LoginPage::LoginPage(QWidget *parent)
"address there, if your server doesn't support .well-known lookup.\nExample: " "address there, if your server doesn't support .well-known lookup.\nExample: "
"@user:server.my\nIf Nheko fails to discover your homeserver, it will show you a " "@user:server.my\nIf Nheko fails to discover your homeserver, it will show you a "
"field to enter the server manually.")); "field to enter the server manually."));
matrixid_input_->setValidator(
new QRegularExpressionValidator(QRegularExpression("@.+?:.{3,}"), this));
spinner_ = new LoadingIndicator(this); spinner_ = new LoadingIndicator(this);
spinner_->setFixedHeight(40); spinner_->setFixedHeight(40);
@ -110,6 +110,12 @@ LoginPage::LoginPage(QWidget *parent)
matrixidLayout_ = new QHBoxLayout(); matrixidLayout_ = new QHBoxLayout();
matrixidLayout_->addWidget(matrixid_input_, 0, Qt::AlignVCenter); matrixidLayout_->addWidget(matrixid_input_, 0, Qt::AlignVCenter);
QFont font;
error_matrixid_label_ = new QLabel(this);
error_matrixid_label_->setFont(font);
error_matrixid_label_->setWordWrap(true);
password_input_ = new TextField(this); password_input_ = new TextField(this);
password_input_->setLabel(tr("Password")); password_input_->setLabel(tr("Password"));
password_input_->setEchoMode(QLineEdit::Password); password_input_->setEchoMode(QLineEdit::Password);
@ -132,10 +138,13 @@ LoginPage::LoginPage(QWidget *parent)
serverLayout_->addWidget(serverInput_, 0, Qt::AlignVCenter); serverLayout_->addWidget(serverInput_, 0, Qt::AlignVCenter);
form_layout_->addLayout(matrixidLayout_); form_layout_->addLayout(matrixidLayout_);
form_layout_->addWidget(error_matrixid_label_, 0, Qt::AlignHCenter);
form_layout_->addWidget(password_input_); form_layout_->addWidget(password_input_);
form_layout_->addWidget(deviceName_, Qt::AlignHCenter); form_layout_->addWidget(deviceName_, Qt::AlignHCenter);
form_layout_->addLayout(serverLayout_); form_layout_->addLayout(serverLayout_);
error_matrixid_label_->hide();
button_layout_ = new QHBoxLayout(); button_layout_ = new QHBoxLayout();
button_layout_->setSpacing(0); button_layout_->setSpacing(0);
button_layout_->setContentsMargins(0, 0, 0, 30); button_layout_->setContentsMargins(0, 0, 0, 30);
@ -149,8 +158,6 @@ LoginPage::LoginPage(QWidget *parent)
button_layout_->addWidget(login_button_); button_layout_->addWidget(login_button_);
button_layout_->addStretch(1); button_layout_->addStretch(1);
QFont font;
error_label_ = new QLabel(this); error_label_ = new QLabel(this);
error_label_->setFont(font); error_label_->setFont(font);
error_label_->setWordWrap(true); error_label_->setWordWrap(true);
@ -183,9 +190,30 @@ LoginPage::LoginPage(QWidget *parent)
void void
LoginPage::loginError(const QString &msg) LoginPage::loginError(const QString &msg)
{ {
auto rect = QFontMetrics(font()).boundingRect(msg);
int width = rect.width();
int height = rect.height();
error_label_->setFixedHeight(qCeil(width / 200) * height);
error_label_->setText(msg); error_label_->setText(msg);
} }
void
LoginPage::matrixIdError(const QString &msg)
{
error_matrixid_label_->show();
error_matrixid_label_->setText(msg);
matrixid_input_->setValid(false);
}
bool
LoginPage::isMatrixIdValid()
{
QRegularExpressionValidator v(QRegularExpression("@.+?:.{3,}"), this);
QString s = matrixid_input_->text();
int pos = 0;
return v.validate(s, pos) == QValidator::Acceptable;
}
void void
LoginPage::onMatrixIdEntered() LoginPage::onMatrixIdEntered()
{ {
@ -193,10 +221,20 @@ LoginPage::onMatrixIdEntered()
User user; User user;
if (!isMatrixIdValid()) {
matrixIdError("You have entered an invalid Matrix ID e.g @joe:matrix.org");
return;
} else {
error_matrixid_label_->setText("");
error_matrixid_label_->hide();
matrixid_input_->setValid(true);
}
try { try {
user = parse<User>(matrixid_input_->text().toStdString()); user = parse<User>(matrixid_input_->text().toStdString());
} catch (const std::exception &e) { } catch (const std::exception &e) {
return loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); matrixIdError("You have entered an invalid Matrix ID e.g @joe:matrix.org");
return;
} }
QString homeServer = QString::fromStdString(user.hostname()); QString homeServer = QString::fromStdString(user.hostname());
@ -307,7 +345,7 @@ LoginPage::onServerAddressEntered()
void void
LoginPage::versionError(const QString &error) LoginPage::versionError(const QString &error)
{ {
error_label_->setText(error); loginError(error);
serverInput_->show(); serverInput_->show();
spinner_->stop(); spinner_->stop();
@ -345,10 +383,20 @@ LoginPage::onLoginButtonClicked()
User user; User user;
if (!isMatrixIdValid()) {
matrixIdError("You have entered an invalid Matrix ID e.g @joe:matrix.org");
return;
} else {
error_matrixid_label_->setText("");
error_matrixid_label_->hide();
matrixid_input_->setValid(true);
}
try { try {
user = parse<User>(matrixid_input_->text().toStdString()); user = parse<User>(matrixid_input_->text().toStdString());
} catch (const std::exception &e) { } catch (const std::exception &e) {
return loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); matrixIdError("You have entered an invalid Matrix ID e.g @joe:matrix.org");
return;
} }
if (loginMethod == LoginMethod::Password) { if (loginMethod == LoginMethod::Password) {

View File

@ -67,6 +67,7 @@ protected:
public slots: public slots:
// Displays errors produced during the login. // Displays errors produced during the login.
void loginError(const QString &msg); void loginError(const QString &msg);
void matrixIdError(const QString &msg);
private slots: private slots:
// Callback for the back button. // Callback for the back button.
@ -112,6 +113,7 @@ private:
QLabel *logo_; QLabel *logo_;
QLabel *error_label_; QLabel *error_label_;
QLabel *error_matrixid_label_;
QHBoxLayout *serverLayout_; QHBoxLayout *serverLayout_;
QHBoxLayout *matrixidLayout_; QHBoxLayout *matrixidLayout_;

View File

@ -69,6 +69,18 @@ TextField::hasLabel() const
return show_label_; return show_label_;
} }
bool
TextField::isValid() const
{
return is_valid_;
}
void
TextField::setValid(bool valid)
{
is_valid_ = valid;
}
void void
TextField::setLabelFontSize(qreal size) TextField::setLabelFontSize(qreal size)
{ {
@ -147,7 +159,7 @@ QColor
TextField::underlineColor() const TextField::underlineColor() const
{ {
if (!underline_color_.isValid()) { if (!underline_color_.isValid()) {
if (hasAcceptableInput() || !isModified()) if ((hasAcceptableInput() && isValid()) || !isModified())
return QPalette().color(QPalette::Highlight); return QPalette().color(QPalette::Highlight);
else else
return Qt::red; return Qt::red;

View File

@ -30,6 +30,7 @@ public:
void setLabelFontSize(qreal size); void setLabelFontSize(qreal size);
void setShowLabel(bool value); void setShowLabel(bool value);
void setUnderlineColor(const QColor &color); void setUnderlineColor(const QColor &color);
void setValid(bool valid);
QColor inkColor() const; QColor inkColor() const;
QColor labelColor() const; QColor labelColor() const;
@ -37,6 +38,7 @@ public:
QColor backgroundColor() const; QColor backgroundColor() const;
QString label() const; QString label() const;
bool hasLabel() const; bool hasLabel() const;
bool isValid() const;
qreal labelFontSize() const; qreal labelFontSize() const;
protected: protected:
@ -54,6 +56,7 @@ private:
TextFieldLabel *label_; TextFieldLabel *label_;
TextFieldStateMachine *state_machine_; TextFieldStateMachine *state_machine_;
bool show_label_; bool show_label_;
bool is_valid_;
qreal label_font_size_; qreal label_font_size_;
}; };