Merge pull request #4670 from owncloud/implement_ep_1136_2nd_attempt

Make postfixlineedit more userfriendly, rename email id string to "Email"
This commit is contained in:
Klaas Freitag 2016-04-15 10:17:03 +02:00
commit e2622310df
8 changed files with 70 additions and 106 deletions

View file

@ -48,7 +48,7 @@ OwncloudHttpCredsPage::OwncloudHttpCredsPage(QWidget* parent)
// default, handled in ui file
break;
case Theme::UserIDEmail:
_ui.usernameLabel->setText(tr("&E-mail address"));
_ui.usernameLabel->setText(tr("&Email"));
break;
case Theme::UserIDCustom:
_ui.usernameLabel->setText(theme->customUserID());

View file

@ -53,10 +53,12 @@ OwncloudSetupPage::OwncloudSetupPage(QWidget *parent)
if (theme->overrideServerUrl().isEmpty()) {
_ui.leUrl->setPostfix(theme->wizardUrlPostfix());
_ui.leUrl->setPlaceholderText(theme->wizardUrlHint());
} else {
_ui.leUrl->setEnabled(false);
}
registerField( QLatin1String("OCUrl*"), _ui.leUrl );
_ui.resultLayout->addWidget( _progressIndi );
@ -136,12 +138,12 @@ void OwncloudSetupPage::slotUrlChanged(const QString& url)
void OwncloudSetupPage::slotUrlEditFinished()
{
QString url = _ui.leUrl->text();
QString url = _ui.leUrl->fullText();
if (QUrl(url).isRelative()) {
// no scheme defined, set one
url.prepend("https://");
}
_ui.leUrl->setText(url);
_ui.leUrl->setFullText(url);
}
bool OwncloudSetupPage::isComplete() const
@ -210,7 +212,7 @@ int OwncloudSetupPage::nextId() const
QString OwncloudSetupPage::url() const
{
QString url = _ui.leUrl->text().simplified();
QString url = _ui.leUrl->fullText().simplified();
return url;
}
@ -246,7 +248,7 @@ void OwncloudSetupPage::setErrorString( const QString& err, bool retryHTTPonly )
_ui.errorLabel->setVisible(false);
} else {
if (retryHTTPonly) {
QUrl url(_ui.leUrl->text());
QUrl url(_ui.leUrl->fullText());
if (url.scheme() == "https") {
// Ask the user how to proceed when connecting to a https:// URL fails.
// It is possible that the server is secured with client-side TLS certificates,
@ -260,7 +262,7 @@ void OwncloudSetupPage::setErrorString( const QString& err, bool retryHTTPonly )
case OwncloudConnectionMethodDialog::No_TLS:
{
url.setScheme("http");
_ui.leUrl->setText(url.toString());
_ui.leUrl->setFullText(url.toString());
// skip ahead to next page, since the user would expect us to retry automatically
wizard()->next();
}

View file

@ -12,51 +12,30 @@
* for more details.
*/
#include <QRegExpValidator>
#include <QRegExp>
#include <QStyle>
#include <QStyleOptionFrame>
#include <QDebug>
#include "postfixlineedit.h"
namespace OCC {
// Helper class
/**
* @brief A QRegExValidator with no Intermediate validation state.
*
* Along with a pre-set text in a lineedit, this enforces a certain text
* to always be present.
*/
class StrictRegExpValidator : public QRegExpValidator
{
public:
explicit StrictRegExpValidator(const QRegExp& rx, QObject *parent = 0) :
QRegExpValidator(rx, parent) {}
virtual QValidator::State validate(QString& input, int& pos) const Q_DECL_OVERRIDE;
};
QValidator::State StrictRegExpValidator::validate(QString &input, int &pos) const
{
QValidator::State state = QRegExpValidator::validate(input, pos);
if (state == QValidator::Intermediate)
state = QValidator::Invalid;
return state;
}
// Begin of URLLineEdit impl
const int horizontalMargin(4);
const int verticalMargin(4);
PostfixLineEdit::PostfixLineEdit(QWidget *parent)
: QLineEdit(parent)
: QLineEdit(parent)
{
connect(this, SIGNAL(textChanged(const QString&)), SLOT(slotTextChanged(const QString&)));
}
void PostfixLineEdit::setPostfix(const QString &postfix)
{
_postfix = postfix;
QFontMetricsF fm(font());
QMargins tm = textMargins();
tm.setRight(tm.right()+fm.width(_postfix)+verticalMargin);
setTextMargins(tm);
}
QString PostfixLineEdit::postfix() const
@ -64,70 +43,39 @@ QString PostfixLineEdit::postfix() const
return _postfix;
}
void PostfixLineEdit::focusInEvent(QFocusEvent *ev)
QString PostfixLineEdit::fullText() const
{
QLineEdit::focusInEvent(ev);
ensureValidationEngaged();
setSelection(0 , maxUserInputLength());
return text() + _postfix;
}
void PostfixLineEdit::focusOutEvent(QFocusEvent *ev)
void PostfixLineEdit::setFullText(const QString &text)
{
QLineEdit::focusOutEvent(ev);
showPlaceholder();
}
void PostfixLineEdit::slotTextChanged(const QString &)
{
ensureValidationEngaged();
}
void PostfixLineEdit::mouseReleaseEvent(QMouseEvent *ev)
{
QLineEdit::mouseReleaseEvent(ev);
// ensure selections still work
if (selectedText().isEmpty()) {
limitCursorPlacement();
QString prefixString = text;
if (prefixString.endsWith(postfix())) {
prefixString.chop(postfix().length());
}
qDebug() << prefixString;
setText(prefixString);
}
void PostfixLineEdit::ensureValidationEngaged()
void PostfixLineEdit::paintEvent(QPaintEvent *pe)
{
if (_postfix.isEmpty())
return;
if (text().isEmpty()) {
// also called from setText via slotTextChanged
bool old = blockSignals(true);
setText(_postfix);
blockSignals(old);
}
if (!validator()) {
QRegExp rx(QString("*%1").arg(_postfix));
rx.setPatternSyntax(QRegExp::Wildcard);
QRegExpValidator *val = new StrictRegExpValidator(rx);
setValidator(val);
}
}
QLineEdit::paintEvent(pe);
QPainter p(this);
void PostfixLineEdit::showPlaceholder()
{
if (text() == _postfix && !placeholderText().isNull()) {
setValidator(0);
setText(QString());
}
}
//
p.setPen(palette().color(QPalette::Disabled, QPalette::Text));
QFontMetricsF fm(font());
int start = rect().right()-fm.width(_postfix);
QStyleOptionFrame panel;
initStyleOption(&panel);
QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
r.setTop(r.top()+horizontalMargin-1);
QRect postfixRect(r);
int PostfixLineEdit::maxUserInputLength() const
{
return text().length() - _postfix.length();
}
void PostfixLineEdit::limitCursorPlacement()
{
if (cursorPosition() > maxUserInputLength()) {
setCursorPosition(maxUserInputLength());
}
postfixRect.setLeft(start-verticalMargin);
p.drawText(postfixRect, _postfix);
}
} // namespace OCC

View file

@ -16,40 +16,40 @@
#define OCC_POSTFIXLINEEDIT_H
#include <QLineEdit>
#include <QPaintEvent>
#include <QPainter>
namespace OCC {
/**
* @brief A class with a non-removable postfix string.
* @brief A lineedit class with a pre-set postfix.
*
* Useful e.g. for setting a fixed domain name.
*/
class PostfixLineEdit : public QLineEdit
{
Q_OBJECT
public:
PostfixLineEdit(QWidget *parent = 0);
/// Sets a non-removeable postfix string
void setPostfix(const QString &postfix);
/// @return the currently set postfix. Use @ref text() to retrieve the full text.
PostfixLineEdit(QWidget *parent);
/** @brief sets an optional postfix shown greyed out */
void setPostfix(const QString& postfix);
/** @brief retrives the postfix */
QString postfix() const;
/** @brief retrieves combined text() and postfix() */
QString fullText() const;
/** @brief sets text() from full text, discarding prefix() */
void setFullText(const QString &text);
protected:
void mouseReleaseEvent(QMouseEvent*) Q_DECL_OVERRIDE;
void focusInEvent(QFocusEvent *) Q_DECL_OVERRIDE;
void focusOutEvent(QFocusEvent *) Q_DECL_OVERRIDE;
private slots:
void slotTextChanged(const QString&);
void paintEvent(QPaintEvent *pe);
private:
void ensureValidationEngaged();
void showPlaceholder();
int maxUserInputLength() const;
void limitCursorPlacement();
QString _postfix;
};
} // namespace OCC
#endif // OCC_POSTFIXLINEEDIT_H

View file

@ -109,6 +109,7 @@ QPixmap ownCloudTheme::wizardHeaderLogo() const
{
return QPixmap(hidpiFileName(":/client/theme/colored/wizard_logo.png"));
}
#endif
QString ownCloudTheme::appName() const

View file

@ -47,6 +47,7 @@ public:
QColor wizardHeaderTitleColor() const Q_DECL_OVERRIDE;
QPixmap wizardHeaderLogo() const Q_DECL_OVERRIDE;
#endif
private:

View file

@ -453,6 +453,11 @@ QString Theme::wizardUrlPostfix() const
return QString();
}
QString Theme::wizardUrlHint() const
{
return QString();
}
} // end namespace client

View file

@ -285,6 +285,13 @@ public:
*/
virtual QString wizardUrlPostfix() const;
/**
* @brief String that will be shown as long as no text has been entered by the user.
*
* @return An empty string, unless reimplemented
*/
virtual QString wizardUrlHint() const;
protected:
#ifndef TOKEN_AUTH_ONLY