2021-03-23 16:06:59 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "payments/ui/payments_edit_information.h"
|
|
|
|
|
|
|
|
#include "payments/ui/payments_panel_delegate.h"
|
2021-03-26 15:23:12 +00:00
|
|
|
#include "payments/ui/payments_field.h"
|
2021-03-23 16:06:59 +00:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
2021-03-30 17:18:39 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
2021-03-23 16:06:59 +00:00
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
#include "ui/wrap/fade_wrap.h"
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "styles/style_payments.h"
|
|
|
|
#include "styles/style_passport.h"
|
|
|
|
|
|
|
|
namespace Payments::Ui {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kMaxStreetSize = 64;
|
|
|
|
constexpr auto kMaxPostcodeSize = 10;
|
|
|
|
constexpr auto kMaxNameSize = 64;
|
|
|
|
constexpr auto kMaxEmailSize = 128;
|
|
|
|
constexpr auto kMaxPhoneSize = 16;
|
2021-03-29 12:16:54 +00:00
|
|
|
constexpr auto kMinCitySize = 2;
|
|
|
|
constexpr auto kMaxCitySize = 64;
|
2021-03-23 16:06:59 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
EditInformation::EditInformation(
|
|
|
|
QWidget *parent,
|
|
|
|
const Invoice &invoice,
|
|
|
|
const RequestedInformation ¤t,
|
2021-03-25 15:27:30 +00:00
|
|
|
InformationField field,
|
2021-03-23 16:06:59 +00:00
|
|
|
not_null<PanelDelegate*> delegate)
|
|
|
|
: _delegate(delegate)
|
|
|
|
, _invoice(invoice)
|
|
|
|
, _information(current)
|
|
|
|
, _scroll(this, st::passportPanelScroll)
|
|
|
|
, _topShadow(this)
|
|
|
|
, _bottomShadow(this)
|
2021-03-31 06:19:14 +00:00
|
|
|
, _submit(
|
|
|
|
this,
|
|
|
|
tr::lng_settings_save(),
|
|
|
|
st::paymentsPanelButton)
|
|
|
|
, _cancel(
|
2021-03-23 16:06:59 +00:00
|
|
|
this,
|
2021-03-31 06:19:14 +00:00
|
|
|
tr::lng_cancel(),
|
|
|
|
st::paymentsPanelButton) {
|
2021-03-23 16:06:59 +00:00
|
|
|
setupControls();
|
|
|
|
}
|
|
|
|
|
2021-03-26 15:23:12 +00:00
|
|
|
EditInformation::~EditInformation() = default;
|
|
|
|
|
2021-03-25 15:27:30 +00:00
|
|
|
void EditInformation::setFocus(InformationField field) {
|
2021-03-24 11:30:01 +00:00
|
|
|
_focusField = field;
|
2021-03-26 15:23:12 +00:00
|
|
|
if (const auto control = lookupField(field)) {
|
|
|
|
_scroll->ensureWidgetVisible(control->widget());
|
|
|
|
control->setFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditInformation::setFocusFast(InformationField field) {
|
|
|
|
_focusField = field;
|
|
|
|
if (const auto control = lookupField(field)) {
|
|
|
|
_scroll->ensureWidgetVisible(control->widget());
|
2021-03-24 11:30:01 +00:00
|
|
|
control->setFocusFast();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:27:30 +00:00
|
|
|
void EditInformation::showError(InformationField field) {
|
2021-03-26 15:23:12 +00:00
|
|
|
if (const auto control = lookupField(field)) {
|
|
|
|
_scroll->ensureWidgetVisible(control->widget());
|
|
|
|
control->showError();
|
2021-03-24 11:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:06:59 +00:00
|
|
|
void EditInformation::setupControls() {
|
|
|
|
const auto inner = setupContent();
|
|
|
|
|
2021-03-31 06:19:14 +00:00
|
|
|
_submit->addClickHandler([=] {
|
2021-03-23 16:06:59 +00:00
|
|
|
_delegate->panelValidateInformation(collect());
|
|
|
|
});
|
2021-03-31 06:19:14 +00:00
|
|
|
_cancel->addClickHandler([=] {
|
|
|
|
_delegate->panelCancelEdit();
|
|
|
|
});
|
2021-03-23 16:06:59 +00:00
|
|
|
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
|
|
|
_topShadow->toggleOn(
|
|
|
|
_scroll->scrollTopValue() | rpl::map(_1 > 0));
|
|
|
|
_bottomShadow->toggleOn(rpl::combine(
|
|
|
|
_scroll->scrollTopValue(),
|
|
|
|
_scroll->heightValue(),
|
|
|
|
inner->heightValue(),
|
|
|
|
_1 + _2 < _3));
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<RpWidget*> EditInformation::setupContent() {
|
|
|
|
const auto inner = _scroll->setOwnedWidget(
|
|
|
|
object_ptr<VerticalLayout>(this));
|
|
|
|
|
|
|
|
_scroll->widthValue(
|
|
|
|
) | rpl::start_with_next([=](int width) {
|
|
|
|
inner->resizeToWidth(width);
|
|
|
|
}, inner->lifetime());
|
|
|
|
|
|
|
|
const auto showBox = [=](object_ptr<BoxContent> box) {
|
|
|
|
_delegate->panelShowBox(std::move(box));
|
|
|
|
};
|
2021-04-01 15:06:48 +00:00
|
|
|
auto last = (Field*)nullptr;
|
2021-03-26 15:23:12 +00:00
|
|
|
const auto add = [&](FieldConfig &&config) {
|
|
|
|
auto result = std::make_unique<Field>(inner, std::move(config));
|
|
|
|
inner->add(result->ownedWidget(), st::paymentsFieldPadding);
|
2021-04-01 15:06:48 +00:00
|
|
|
if (last) {
|
|
|
|
last->setNextField(result.get());
|
|
|
|
result->setPreviousField(last);
|
|
|
|
}
|
|
|
|
last = result.get();
|
2021-03-26 15:23:12 +00:00
|
|
|
return result;
|
|
|
|
};
|
2021-03-23 16:06:59 +00:00
|
|
|
if (_invoice.isShippingAddressRequested) {
|
2021-03-26 15:23:12 +00:00
|
|
|
_street1 = add({
|
|
|
|
.placeholder = tr::lng_payments_address_street1(),
|
|
|
|
.value = _information.shippingAddress.address1,
|
2021-03-29 12:16:54 +00:00
|
|
|
.validator = RangeLengthValidator(1, kMaxStreetSize),
|
2021-03-26 15:23:12 +00:00
|
|
|
});
|
|
|
|
_street2 = add({
|
|
|
|
.placeholder = tr::lng_payments_address_street2(),
|
|
|
|
.value = _information.shippingAddress.address2,
|
2021-03-29 12:16:54 +00:00
|
|
|
.validator = MaxLengthValidator(kMaxStreetSize),
|
2021-03-26 15:23:12 +00:00
|
|
|
});
|
|
|
|
_city = add({
|
|
|
|
.placeholder = tr::lng_payments_address_city(),
|
|
|
|
.value = _information.shippingAddress.city,
|
2021-03-29 12:16:54 +00:00
|
|
|
.validator = RangeLengthValidator(kMinCitySize, kMaxCitySize),
|
2021-03-26 15:23:12 +00:00
|
|
|
});
|
|
|
|
_state = add({
|
|
|
|
.placeholder = tr::lng_payments_address_state(),
|
|
|
|
.value = _information.shippingAddress.state,
|
|
|
|
});
|
|
|
|
_country = add({
|
|
|
|
.type = FieldType::Country,
|
|
|
|
.placeholder = tr::lng_payments_address_country(),
|
|
|
|
.value = _information.shippingAddress.countryIso2,
|
2021-03-29 12:16:54 +00:00
|
|
|
.validator = RequiredFinishedValidator(),
|
2021-03-26 17:09:09 +00:00
|
|
|
.showBox = showBox,
|
|
|
|
.defaultCountry = _information.defaultCountry,
|
2021-03-26 15:23:12 +00:00
|
|
|
});
|
|
|
|
_postcode = add({
|
|
|
|
.placeholder = tr::lng_payments_address_postcode(),
|
|
|
|
.value = _information.shippingAddress.postcode,
|
2021-03-29 12:16:54 +00:00
|
|
|
.validator = RangeLengthValidator(1, kMaxPostcodeSize),
|
2021-03-26 15:23:12 +00:00
|
|
|
});
|
2021-03-23 16:06:59 +00:00
|
|
|
}
|
|
|
|
if (_invoice.isNameRequested) {
|
2021-03-26 15:23:12 +00:00
|
|
|
_name = add({
|
|
|
|
.placeholder = tr::lng_payments_info_name(),
|
|
|
|
.value = _information.name,
|
2021-03-29 12:16:54 +00:00
|
|
|
.validator = RangeLengthValidator(1, kMaxNameSize),
|
2021-03-26 15:23:12 +00:00
|
|
|
});
|
2021-03-23 16:06:59 +00:00
|
|
|
}
|
|
|
|
if (_invoice.isEmailRequested) {
|
2021-03-26 15:23:12 +00:00
|
|
|
_email = add({
|
2021-03-29 12:16:54 +00:00
|
|
|
.type = FieldType::Email,
|
2021-03-26 15:23:12 +00:00
|
|
|
.placeholder = tr::lng_payments_info_email(),
|
|
|
|
.value = _information.email,
|
2021-03-29 12:16:54 +00:00
|
|
|
.validator = RangeLengthValidator(1, kMaxEmailSize),
|
2021-03-26 15:23:12 +00:00
|
|
|
});
|
2021-03-23 16:06:59 +00:00
|
|
|
}
|
|
|
|
if (_invoice.isPhoneRequested) {
|
2021-03-26 15:23:12 +00:00
|
|
|
_phone = add({
|
2021-03-29 12:16:54 +00:00
|
|
|
.type = FieldType::Phone,
|
2021-03-26 15:23:12 +00:00
|
|
|
.placeholder = tr::lng_payments_info_phone(),
|
|
|
|
.value = _information.phone,
|
2021-03-29 12:16:54 +00:00
|
|
|
.validator = RangeLengthValidator(1, kMaxPhoneSize),
|
|
|
|
.defaultPhone = _information.defaultPhone,
|
2021-03-26 15:23:12 +00:00
|
|
|
});
|
2021-03-23 16:06:59 +00:00
|
|
|
}
|
2021-04-01 14:05:46 +00:00
|
|
|
const auto emailToProvider = _invoice.isEmailRequested
|
|
|
|
&& _invoice.emailSentToProvider;
|
|
|
|
const auto phoneToProvider = _invoice.isPhoneRequested
|
|
|
|
&& _invoice.phoneSentToProvider;
|
|
|
|
if (emailToProvider || phoneToProvider) {
|
|
|
|
inner->add(
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
inner,
|
|
|
|
((emailToProvider && phoneToProvider)
|
|
|
|
? tr::lng_payments_to_provider_phone_email
|
|
|
|
: emailToProvider
|
|
|
|
? tr::lng_payments_to_provider_email
|
|
|
|
: tr::lng_payments_to_provider_phone)(
|
2021-04-01 14:39:44 +00:00
|
|
|
lt_provider,
|
2021-04-01 14:05:46 +00:00
|
|
|
rpl::single(_invoice.provider)),
|
|
|
|
st::paymentsToProviderLabel),
|
|
|
|
st::paymentsToProviderPadding);
|
|
|
|
}
|
2021-03-30 17:18:39 +00:00
|
|
|
_save = inner->add(
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
inner,
|
|
|
|
tr::lng_payments_save_information(tr::now),
|
|
|
|
true),
|
|
|
|
st::paymentsSaveCheckboxPadding);
|
2021-04-01 15:06:48 +00:00
|
|
|
|
|
|
|
if (last) {
|
|
|
|
last->submitted(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_delegate->panelValidateInformation(collect());
|
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:06:59 +00:00
|
|
|
return inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditInformation::resizeEvent(QResizeEvent *e) {
|
|
|
|
updateControlsGeometry();
|
|
|
|
}
|
|
|
|
|
2021-03-24 11:30:01 +00:00
|
|
|
void EditInformation::focusInEvent(QFocusEvent *e) {
|
2021-03-26 15:23:12 +00:00
|
|
|
if (const auto control = lookupField(_focusField)) {
|
|
|
|
control->setFocus();
|
2021-03-24 11:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:06:59 +00:00
|
|
|
void EditInformation::updateControlsGeometry() {
|
2021-03-31 06:19:14 +00:00
|
|
|
const auto &padding = st::paymentsPanelPadding;
|
|
|
|
const auto buttonsHeight = padding.top()
|
|
|
|
+ _cancel->height()
|
|
|
|
+ padding.bottom();
|
|
|
|
const auto buttonsTop = height() - buttonsHeight;
|
|
|
|
_scroll->setGeometry(0, 0, width(), buttonsTop);
|
2021-03-23 16:06:59 +00:00
|
|
|
_topShadow->resizeToWidth(width());
|
|
|
|
_topShadow->moveToLeft(0, 0);
|
|
|
|
_bottomShadow->resizeToWidth(width());
|
2021-03-31 06:19:14 +00:00
|
|
|
_bottomShadow->moveToLeft(0, buttonsTop - st::lineWidth);
|
|
|
|
auto right = padding.right();
|
|
|
|
_submit->moveToRight(right, buttonsTop + padding.top());
|
|
|
|
right += _submit->width() + padding.left();
|
|
|
|
_cancel->moveToRight(right, buttonsTop + padding.top());
|
2021-03-23 16:06:59 +00:00
|
|
|
|
|
|
|
_scroll->updateBars();
|
|
|
|
}
|
|
|
|
|
2021-03-26 15:23:12 +00:00
|
|
|
auto EditInformation::lookupField(InformationField field) const -> Field* {
|
2021-03-24 11:30:01 +00:00
|
|
|
switch (field) {
|
2021-03-26 15:23:12 +00:00
|
|
|
case InformationField::ShippingStreet: return _street1.get();
|
|
|
|
case InformationField::ShippingCity: return _city.get();
|
|
|
|
case InformationField::ShippingState: return _state.get();
|
|
|
|
case InformationField::ShippingCountry: return _country.get();
|
|
|
|
case InformationField::ShippingPostcode: return _postcode.get();
|
|
|
|
case InformationField::Name: return _name.get();
|
|
|
|
case InformationField::Email: return _email.get();
|
|
|
|
case InformationField::Phone: return _phone.get();
|
2021-03-24 11:30:01 +00:00
|
|
|
}
|
2021-03-26 15:23:12 +00:00
|
|
|
Unexpected("Unknown field in EditInformation::lookupField.");
|
2021-03-24 11:30:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 16:06:59 +00:00
|
|
|
RequestedInformation EditInformation::collect() const {
|
|
|
|
return {
|
2021-03-29 12:16:54 +00:00
|
|
|
.defaultPhone = _information.defaultPhone,
|
|
|
|
.defaultCountry = _information.defaultCountry,
|
2021-03-30 17:18:39 +00:00
|
|
|
.save = _save->checked(),
|
2021-03-26 15:23:12 +00:00
|
|
|
.name = _name ? _name->value() : QString(),
|
|
|
|
.phone = _phone ? _phone->value() : QString(),
|
|
|
|
.email = _email ? _email->value() : QString(),
|
2021-03-23 16:06:59 +00:00
|
|
|
.shippingAddress = {
|
2021-03-26 15:23:12 +00:00
|
|
|
.address1 = _street1 ? _street1->value() : QString(),
|
|
|
|
.address2 = _street2 ? _street2->value() : QString(),
|
|
|
|
.city = _city ? _city->value() : QString(),
|
|
|
|
.state = _state ? _state->value() : QString(),
|
|
|
|
.countryIso2 = _country ? _country->value() : QString(),
|
|
|
|
.postcode = _postcode ? _postcode->value() : QString(),
|
2021-03-23 16:06:59 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Payments::Ui
|