2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-30 22:16:14 +00:00

Intro redesign done.

This commit is contained in:
John Preston
2016-11-24 22:28:23 +03:00
parent 6e0394dd42
commit 3da0533339
71 changed files with 2150 additions and 1776 deletions

View File

@@ -29,200 +29,106 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
#include "application.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/input_fields.h"
#include "ui/widgets/labels.h"
#include "ui/buttons/peer_avatar_button.h"
IntroSignup::IntroSignup(IntroWidget *parent) : IntroStep(parent)
, a_errorAlpha(0)
, a_photoOver(0)
, _a_error(animation(this, &IntroSignup::step_error))
, _a_photo(animation(this, &IntroSignup::step_photo))
, _next(this, lang(lng_intro_finish), st::introNextButton)
namespace Intro {
SignupWidget::SignupWidget(QWidget *parent, Widget::Data *data) : Step(parent, data)
, _photo(this, st::introPhotoSize, st::introPhotoIconPosition)
, _first(this, st::introName, lang(lng_signup_firstname))
, _last(this, st::introName, lang(lng_signup_lastname))
, _invertOrder(langFirstNameGoesSecond())
, _checkRequest(this) {
setVisible(false);
setGeometry(parent->innerRect());
connect(_next, SIGNAL(clicked()), this, SLOT(onSubmitName()));
connect(_checkRequest, SIGNAL(timeout()), this, SLOT(onCheckRequest()));
_photo->setClickedCallback([this] {
auto imgExtensions = cImgExtensions();
auto filter = qsl("Image files (*") + imgExtensions.join(qsl(" *")) + qsl(");;") + filedialogAllFilesFilter();
_readPhotoFileQueryId = FileDialog::queryReadFile(lang(lng_choose_images), filter);
});
subscribe(FileDialog::QueryDone(), [this](const FileDialog::QueryUpdate &update) {
notifyFileQueryUpdated(update);
});
if (_invertOrder) {
setTabOrder(_last, _first);
}
setErrorCentered(true);
setTitleText(lang(lng_signup_title));
setDescriptionText(lang(lng_signup_desc));
setMouseTracking(true);
}
void IntroSignup::mouseMoveEvent(QMouseEvent *e) {
bool photoOver = QRect(_phLeft, _phTop, st::introPhotoSize, st::introPhotoSize).contains(e->pos());
if (photoOver != _photoOver) {
_photoOver = photoOver;
if (_photoSmall.isNull()) {
a_photoOver.start(_photoOver ? 1 : 0);
_a_photo.start();
}
void SignupWidget::notifyFileQueryUpdated(const FileDialog::QueryUpdate &update) {
if (_readPhotoFileQueryId != update.queryId) {
return;
}
_readPhotoFileQueryId = 0;
if (update.remoteContent.isEmpty() && update.filePaths.isEmpty()) {
return;
}
setCursor(_photoOver ? style::cur_pointer : style::cur_default);
}
void IntroSignup::mousePressEvent(QMouseEvent *e) {
mouseMoveEvent(e);
if (QRect(_phLeft, _phTop, st::introPhotoSize, st::introPhotoSize).contains(e->pos())) {
QStringList imgExtensions(cImgExtensions());
QString filter(qsl("Image files (*") + imgExtensions.join(qsl(" *")) + qsl(");;") + filedialogAllFilesFilter());
QImage img;
QString file;
QByteArray remoteContent;
if (filedialogGetOpenFile(file, remoteContent, lang(lng_choose_images), filter)) {
if (!remoteContent.isEmpty()) {
img = App::readImage(remoteContent);
} else {
if (!file.isEmpty()) {
img = App::readImage(file);
}
}
} else {
return;
}
if (img.isNull() || img.width() > 10 * img.height() || img.height() > 10 * img.width()) {
showError(lang(lng_bad_photo));
return;
}
PhotoCropBox *box = new PhotoCropBox(img, PeerId(0));
connect(box, SIGNAL(ready(const QImage &)), this, SLOT(onPhotoReady(const QImage &)));
Ui::showLayer(box);
}
}
void IntroSignup::paintEvent(QPaintEvent *e) {
bool trivial = (rect() == e->rect());
Painter p(this);
if (!trivial) {
p.setClipRect(e->rect());
}
if (trivial || e->rect().intersects(_textRect)) {
p.setFont(st::introHeaderFont->f);
p.drawText(_textRect, lang(lng_signup_title), style::al_top);
p.setFont(st::introFont->f);
p.drawText(_textRect, lang(lng_signup_desc), style::al_bottom);
}
if (_a_error.animating() || error.length()) {
p.setOpacity(a_errorAlpha.current());
QRect errRect;
if (_invertOrder) {
errRect = QRect((width() - st::introErrorWidth) / 2, (_first->y() + _first->height() + _next->y() - st::introErrorHeight) / 2, st::introErrorWidth, st::introErrorHeight);
} else {
errRect = QRect((width() - st::introErrorWidth) / 2, (_last->y() + _last->height() + _next->y() - st::introErrorHeight) / 2, st::introErrorWidth, st::introErrorHeight);
}
p.setFont(st::introErrorFont);
p.setPen(st::introErrorFg);
p.drawText(errRect, error, QTextOption(style::al_center));
p.setOpacity(1);
}
if (_photoSmall.isNull()) {
float64 o = a_photoOver.current();
QRect phRect(_phLeft, _phTop, st::introPhotoSize, st::introPhotoSize);
if (o > 0) {
if (o < 1) {
QColor c;
c.setRedF(st::newGroupPhotoBg->c.redF() * (1. - o) + st::newGroupPhotoBgOver->c.redF() * o);
c.setGreenF(st::newGroupPhotoBg->c.greenF() * (1. - o) + st::newGroupPhotoBgOver->c.greenF() * o);
c.setBlueF(st::newGroupPhotoBg->c.blueF() * (1. - o) + st::newGroupPhotoBgOver->c.blueF() * o);
p.fillRect(phRect, c);
} else {
p.fillRect(phRect, st::newGroupPhotoBgOver);
}
} else {
p.fillRect(phRect, st::newGroupPhotoBg);
}
st::newGroupPhotoIcon.paintInCenter(p, phRect);
QImage img;
if (!update.remoteContent.isEmpty()) {
img = App::readImage(update.remoteContent);
} else {
p.drawPixmap(_phLeft, _phTop, _photoSmall);
img = App::readImage(update.filePaths.front());
}
if (img.isNull() || img.width() > 10 * img.height() || img.height() > 10 * img.width()) {
showError(lang(lng_bad_photo));
return;
}
auto box = new PhotoCropBox(img, PeerId(0));
connect(box, SIGNAL(ready(const QImage &)), this, SLOT(onPhotoReady(const QImage &)));
Ui::showLayer(box);
}
void IntroSignup::resizeEvent(QResizeEvent *e) {
_phLeft = (width() - _next->width()) / 2;
_phTop = st::introTextTop + st::introTextSize.height() + st::introCountry.top;
if (e->oldSize().width() != width()) {
_next->move((width() - _next->width()) / 2, st::introBtnTop);
if (_invertOrder) {
_last->move((width() - _next->width()) / 2 + _next->width() - _last->width(), _phTop);
_first->move((width() - _next->width()) / 2 + _next->width() - _first->width(), _last->y() + st::introCountry.height + st::introCountry.ptrSize.height() + st::introPhoneTop);
} else {
_first->move((width() - _next->width()) / 2 + _next->width() - _first->width(), _phTop);
_last->move((width() - _next->width()) / 2 + _next->width() - _last->width(), _first->y() + st::introCountry.height + st::introCountry.ptrSize.height() + st::introPhoneTop);
}
}
_textRect = QRect((width() - st::introTextSize.width()) / 2, st::introTextTop, st::introTextSize.width(), st::introTextSize.height());
}
void SignupWidget::resizeEvent(QResizeEvent *e) {
Step::resizeEvent(e);
void IntroSignup::showError(const QString &err) {
if (!_a_error.animating() && err == error) return;
auto photoRight = contentLeft() + st::introNextButton.width;
auto photoTop = contentTop() + st::introPhotoTop;
_photo->moveToLeft(photoRight - _photo->width(), photoTop);
if (err.length()) {
error = err;
a_errorAlpha.start(1);
} else {
a_errorAlpha.start(0);
}
_a_error.start();
}
void IntroSignup::step_error(float64 ms, bool timer) {
float64 dt = ms / st::introErrorDuration;
if (dt >= 1) {
_a_error.stop();
a_errorAlpha.finish();
if (!a_errorAlpha.current()) {
error.clear();
}
} else {
a_errorAlpha.update(dt, anim::linear);
}
if (timer) update();
}
void IntroSignup::step_photo(float64 ms, bool timer) {
float64 dt = ms / st::introErrorDuration;
if (dt >= 1) {
_a_photo.stop();
a_photoOver.finish();
} else {
a_photoOver.update(dt, anim::linear);
}
if (timer) update();
}
void IntroSignup::activate() {
IntroStep::activate();
auto firstTop = contentTop() + st::introStepFieldTop;
auto secondTop = firstTop + st::introName.height + st::introPhoneTop;
if (_invertOrder) {
_last->moveToLeft(contentLeft(), firstTop);
_first->moveToLeft(contentLeft(), secondTop);
} else {
_first->moveToLeft(contentLeft(), firstTop);
_last->moveToLeft(contentLeft(), secondTop);
}
}
void SignupWidget::setInnerFocus() {
if (_invertOrder || _last->hasFocus()) {
_last->setFocus();
} else {
_first->setFocus();
}
}
void IntroSignup::cancelled() {
if (_sentRequest) {
MTP::cancel(base::take(_sentRequest));
}
void SignupWidget::activate() {
Step::activate();
_first->show();
_last->show();
_photo->show();
setInnerFocus();
}
void IntroSignup::stopCheck() {
void SignupWidget::cancelled() {
MTP::cancel(base::take(_sentRequest));
}
void SignupWidget::stopCheck() {
_checkRequest->stop();
}
void IntroSignup::onCheckRequest() {
void SignupWidget::onCheckRequest() {
int32 status = MTP::state(_sentRequest);
if (status < 0) {
int32 leftms = -status;
@@ -244,13 +150,12 @@ void IntroSignup::onCheckRequest() {
}
}
void IntroSignup::onPhotoReady(const QImage &img) {
_photoBig = img;
_photoSmall = App::pixmapFromImageInPlace(img.scaled(st::introPhotoSize * cIntRetinaFactor(), st::introPhotoSize * cIntRetinaFactor(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
_photoSmall.setDevicePixelRatio(cRetinaFactor());
void SignupWidget::onPhotoReady(const QImage &img) {
_photoImage = img;
_photo->setImage(_photoImage);
}
void IntroSignup::nameSubmitDone(const MTPauth_Authorization &result) {
void SignupWidget::nameSubmitDone(const MTPauth_Authorization &result) {
stopCheck();
_first->setDisabled(false);
_last->setDisabled(false);
@@ -259,10 +164,10 @@ void IntroSignup::nameSubmitDone(const MTPauth_Authorization &result) {
showError(lang(lng_server_error));
return;
}
intro()->finish(d.vuser, _photoBig);
finish(d.vuser, _photoImage);
}
bool IntroSignup::nameSubmitFail(const RPCError &error) {
bool SignupWidget::nameSubmitFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
stopCheck();
_first->setDisabled(false);
@@ -284,7 +189,7 @@ bool IntroSignup::nameSubmitFail(const RPCError &error) {
if (err == qstr("PHONE_NUMBER_INVALID") || err == qstr("PHONE_CODE_EXPIRED") ||
err == qstr("PHONE_CODE_EMPTY") || err == qstr("PHONE_CODE_INVALID") ||
err == qstr("PHONE_NUMBER_OCCUPIED")) {
intro()->onBack();
goBack();
return true;
} else if (err == "FIRSTNAME_INVALID") {
showError(lang(lng_bad_name));
@@ -308,29 +213,29 @@ bool IntroSignup::nameSubmitFail(const RPCError &error) {
return false;
}
void IntroSignup::onInputChange() {
void SignupWidget::onInputChange() {
showError(QString());
}
void IntroSignup::onSubmitName(bool force) {
void SignupWidget::submit() {
if (_invertOrder) {
if ((_last->hasFocus() || _last->text().trimmed().length()) && !_first->text().trimmed().length()) {
if ((_last->hasFocus() || _last->getLastText().trimmed().length()) && !_first->getLastText().trimmed().length()) {
_first->setFocus();
return;
} else if (!_last->text().trimmed().length()) {
} else if (!_last->getLastText().trimmed().length()) {
_last->setFocus();
return;
}
} else {
if ((_first->hasFocus() || _first->text().trimmed().length()) && !_last->text().trimmed().length()) {
if ((_first->hasFocus() || _first->getLastText().trimmed().length()) && !_last->getLastText().trimmed().length()) {
_last->setFocus();
return;
} else if (!_first->text().trimmed().length()) {
} else if (!_first->getLastText().trimmed().length()) {
_first->setFocus();
return;
}
}
if (!force && !_first->isEnabled()) return;
if (!_first->isEnabled()) return;
_first->setDisabled(true);
_last->setDisabled(true);
@@ -338,11 +243,13 @@ void IntroSignup::onSubmitName(bool force) {
showError(QString());
_firstName = _first->text().trimmed();
_lastName = _last->text().trimmed();
_sentRequest = MTP::send(MTPauth_SignUp(MTP_string(intro()->getPhone()), MTP_string(intro()->getPhoneHash()), MTP_string(intro()->getCode()), MTP_string(_firstName), MTP_string(_lastName)), rpcDone(&IntroSignup::nameSubmitDone), rpcFail(&IntroSignup::nameSubmitFail));
_firstName = _first->getLastText().trimmed();
_lastName = _last->getLastText().trimmed();
_sentRequest = MTP::send(MTPauth_SignUp(MTP_string(getData()->phone), MTP_string(getData()->phoneHash), MTP_string(getData()->code), MTP_string(_firstName), MTP_string(_lastName)), rpcDone(&SignupWidget::nameSubmitDone), rpcFail(&SignupWidget::nameSubmitFail));
}
void IntroSignup::onSubmit() {
onSubmitName();
QString SignupWidget::nextButtonText() const {
return lang(lng_intro_finish);
}
} // namespace Intro