2018-03-31 05:45:40 +04: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
|
|
|
|
*/
|
2018-04-05 21:01:22 +04:00
|
|
|
#include "passport/passport_panel_edit_document.h"
|
2018-03-31 05:45:40 +04:00
|
|
|
|
|
|
|
#include "passport/passport_panel_controller.h"
|
|
|
|
#include "passport/passport_panel_details_row.h"
|
2018-04-04 23:26:40 +04:00
|
|
|
#include "passport/passport_panel_edit_scans.h"
|
2018-03-31 05:45:40 +04:00
|
|
|
#include "ui/widgets/input_fields.h"
|
|
|
|
#include "ui/widgets/scroll_area.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "ui/widgets/shadow.h"
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
#include "ui/wrap/fade_wrap.h"
|
|
|
|
#include "boxes/abstract_box.h"
|
2018-04-09 21:56:07 +04:00
|
|
|
#include "boxes/confirm_box.h"
|
2018-03-31 05:45:40 +04:00
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "styles/style_widgets.h"
|
|
|
|
#include "styles/style_boxes.h"
|
|
|
|
#include "styles/style_passport.h"
|
|
|
|
|
|
|
|
namespace Passport {
|
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
struct PanelEditDocument::Result {
|
|
|
|
ValueMap data;
|
|
|
|
ValueMap filesData;
|
|
|
|
};
|
|
|
|
|
2018-04-05 21:01:22 +04:00
|
|
|
PanelEditDocument::PanelEditDocument(
|
2018-03-31 05:45:40 +04:00
|
|
|
QWidget*,
|
|
|
|
not_null<PanelController*> controller,
|
2018-04-05 21:01:22 +04:00
|
|
|
Scheme scheme,
|
2018-03-31 05:45:40 +04:00
|
|
|
const ValueMap &data,
|
2018-04-03 22:24:31 +04:00
|
|
|
const ValueMap &scanData,
|
2018-03-31 05:45:40 +04:00
|
|
|
std::vector<ScanInfo> &&files)
|
|
|
|
: _controller(controller)
|
2018-04-05 21:01:22 +04:00
|
|
|
, _scheme(std::move(scheme))
|
2018-03-31 05:45:40 +04:00
|
|
|
, _scroll(this, st::passportPanelScroll)
|
|
|
|
, _topShadow(this)
|
|
|
|
, _bottomShadow(this)
|
|
|
|
, _done(
|
|
|
|
this,
|
|
|
|
langFactory(lng_passport_save_value),
|
|
|
|
st::passportPanelSaveValue) {
|
2018-04-05 21:01:22 +04:00
|
|
|
setupControls(data, &scanData, std::move(files));
|
2018-03-31 05:45:40 +04:00
|
|
|
}
|
|
|
|
|
2018-04-05 21:01:22 +04:00
|
|
|
PanelEditDocument::PanelEditDocument(
|
|
|
|
QWidget*,
|
|
|
|
not_null<PanelController*> controller,
|
|
|
|
Scheme scheme,
|
|
|
|
const ValueMap &data)
|
|
|
|
: _controller(controller)
|
|
|
|
, _scheme(std::move(scheme))
|
|
|
|
, _scroll(this, st::passportPanelScroll)
|
|
|
|
, _topShadow(this)
|
|
|
|
, _bottomShadow(this)
|
|
|
|
, _done(
|
|
|
|
this,
|
|
|
|
langFactory(lng_passport_save_value),
|
|
|
|
st::passportPanelSaveValue) {
|
|
|
|
setupControls(data, nullptr, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
void PanelEditDocument::setupControls(
|
2018-04-03 22:24:31 +04:00
|
|
|
const ValueMap &data,
|
2018-04-05 21:01:22 +04:00
|
|
|
const ValueMap *scanData,
|
2018-04-04 23:26:40 +04:00
|
|
|
std::vector<ScanInfo> &&files) {
|
|
|
|
const auto inner = setupContent(data, scanData, std::move(files));
|
2018-03-31 05:45:40 +04:00
|
|
|
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
|
|
|
_topShadow->toggleOn(
|
|
|
|
_scroll->scrollTopValue() | rpl::map(_1 > 0));
|
|
|
|
_done->addClickHandler([=] {
|
|
|
|
crl::on_main(this, [=] {
|
|
|
|
save();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:01:22 +04:00
|
|
|
not_null<Ui::RpWidget*> PanelEditDocument::setupContent(
|
2018-04-03 22:24:31 +04:00
|
|
|
const ValueMap &data,
|
2018-04-05 21:01:22 +04:00
|
|
|
const ValueMap *scanData,
|
2018-04-04 23:26:40 +04:00
|
|
|
std::vector<ScanInfo> &&files) {
|
2018-03-31 05:45:40 +04:00
|
|
|
const auto inner = _scroll->setOwnedWidget(
|
|
|
|
object_ptr<Ui::VerticalLayout>(this));
|
|
|
|
_scroll->widthValue(
|
|
|
|
) | rpl::start_with_next([=](int width) {
|
|
|
|
inner->resizeToWidth(width);
|
|
|
|
}, inner->lifetime());
|
|
|
|
|
2018-04-05 21:01:22 +04:00
|
|
|
if (scanData) {
|
|
|
|
_editScans = inner->add(
|
|
|
|
object_ptr<EditScans>(inner, _controller, std::move(files)));
|
|
|
|
}
|
2018-03-31 05:45:40 +04:00
|
|
|
|
|
|
|
inner->add(object_ptr<BoxContentDivider>(
|
|
|
|
inner,
|
|
|
|
st::passportFormDividerHeight));
|
|
|
|
inner->add(
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
inner,
|
2018-04-06 15:54:01 +04:00
|
|
|
_scheme.rowsHeader,
|
2018-03-31 05:45:40 +04:00
|
|
|
Ui::FlatLabel::InitType::Simple,
|
|
|
|
st::passportFormHeader),
|
|
|
|
st::passportDetailsHeaderPadding);
|
|
|
|
|
2018-04-05 21:01:22 +04:00
|
|
|
const auto valueOrEmpty = [&](
|
|
|
|
const ValueMap &values,
|
|
|
|
const QString &key) {
|
|
|
|
const auto &fields = values.fields;
|
|
|
|
if (const auto i = fields.find(key); i != fields.end()) {
|
2018-03-31 05:45:40 +04:00
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
};
|
|
|
|
|
2018-04-06 22:47:29 +04:00
|
|
|
for (auto i = 0, count = int(_scheme.rows.size()); i != count; ++i) {
|
|
|
|
const auto &row = _scheme.rows[i];
|
2018-04-05 21:01:22 +04:00
|
|
|
auto fields = (row.type == Scheme::ValueType::Fields)
|
|
|
|
? &data
|
|
|
|
: scanData;
|
|
|
|
if (!fields) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-06 22:47:29 +04:00
|
|
|
_details.emplace(i, inner->add(object_ptr<PanelDetailsRow>(
|
2018-04-05 21:01:22 +04:00
|
|
|
inner,
|
|
|
|
row.label,
|
|
|
|
valueOrEmpty(*fields, row.key))));
|
|
|
|
}
|
2018-03-31 05:45:40 +04:00
|
|
|
|
|
|
|
return inner;
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:01:22 +04:00
|
|
|
void PanelEditDocument::focusInEvent(QFocusEvent *e) {
|
2018-04-06 22:47:29 +04:00
|
|
|
for (const auto [index, row] : _details) {
|
2018-04-05 21:01:22 +04:00
|
|
|
if (row->setFocusFast()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-03-31 05:45:40 +04:00
|
|
|
}
|
|
|
|
|
2018-04-05 21:01:22 +04:00
|
|
|
void PanelEditDocument::resizeEvent(QResizeEvent *e) {
|
2018-03-31 05:45:40 +04:00
|
|
|
updateControlsGeometry();
|
|
|
|
}
|
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
bool PanelEditDocument::hasUnsavedChanges() const {
|
|
|
|
const auto result = collect();
|
|
|
|
return _controller->editScopeChanged(result.data, result.filesData);
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:01:22 +04:00
|
|
|
void PanelEditDocument::updateControlsGeometry() {
|
2018-03-31 05:45:40 +04:00
|
|
|
const auto submitTop = height() - _done->height();
|
|
|
|
_scroll->setGeometry(0, 0, width(), submitTop);
|
|
|
|
_topShadow->resizeToWidth(width());
|
|
|
|
_topShadow->moveToLeft(0, 0);
|
|
|
|
_bottomShadow->resizeToWidth(width());
|
|
|
|
_bottomShadow->moveToLeft(0, submitTop - st::lineWidth);
|
|
|
|
_done->resizeToWidth(width());
|
|
|
|
_done->moveToLeft(0, submitTop);
|
|
|
|
|
|
|
|
_scroll->updateBars();
|
|
|
|
}
|
|
|
|
|
2018-04-09 21:56:07 +04:00
|
|
|
PanelEditDocument::Result PanelEditDocument::collect() const {
|
|
|
|
auto result = Result();
|
2018-04-06 22:47:29 +04:00
|
|
|
for (const auto [i, field] : _details) {
|
2018-04-05 21:01:22 +04:00
|
|
|
const auto &row = _scheme.rows[i];
|
|
|
|
auto &fields = (row.type == Scheme::ValueType::Fields)
|
2018-04-09 21:56:07 +04:00
|
|
|
? result.data
|
|
|
|
: result.filesData;
|
|
|
|
fields.fields[row.key] = field->getValue();
|
2018-04-05 21:01:22 +04:00
|
|
|
}
|
2018-04-09 21:56:07 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PanelEditDocument::save() {
|
|
|
|
auto result = collect();
|
|
|
|
_controller->saveScope(
|
|
|
|
std::move(result.data),
|
|
|
|
std::move(result.filesData));
|
2018-03-31 05:45:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Passport
|