1 /*
2 This file is part of Telegram Desktop,
3 the official desktop application for the Telegram messaging service.
4 
5 For license and copyright information please follow this link:
6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
7 */
8 #include "passport/ui/passport_form_row.h"
9 
10 #include "ui/text/text_options.h"
11 #include "styles/style_passport.h"
12 #include "styles/style_layers.h"
13 
14 namespace Passport::Ui {
15 
FormRow(QWidget * parent)16 FormRow::FormRow(QWidget *parent)
17 : RippleButton(parent, st::passportRowRipple)
18 , _title(st::boxWideWidth / 2)
19 , _description(st::boxWideWidth / 2) {
20 }
21 
updateContent(const QString & title,const QString & description,bool ready,bool error,anim::type animated)22 void FormRow::updateContent(
23 		const QString &title,
24 		const QString &description,
25 		bool ready,
26 		bool error,
27 		anim::type animated) {
28 	_title.setText(
29 		st::semiboldTextStyle,
30 		title,
31 		NameTextOptions());
32 	_description.setText(
33 		st::defaultTextStyle,
34 		description,
35 		TextParseOptions {
36 			TextParseMultiline,
37 			0,
38 			0,
39 			Qt::LayoutDirectionAuto
40 		});
41 	_ready = ready && !error;
42 	if (_error != error) {
43 		_error = error;
44 		if (animated == anim::type::instant) {
45 			_errorAnimation.stop();
46 		} else {
47 			_errorAnimation.start(
48 				[=] { update(); },
49 				_error ? 0. : 1.,
50 				_error ? 1. : 0.,
51 				st::fadeWrapDuration);
52 		}
53 	}
54 	resizeToWidth(width());
55 	update();
56 }
57 
resizeGetHeight(int newWidth)58 int FormRow::resizeGetHeight(int newWidth) {
59 	const auto availableWidth = countAvailableWidth(newWidth);
60 	_titleHeight = _title.countHeight(availableWidth);
61 	_descriptionHeight = _description.countHeight(availableWidth);
62 	const auto result = st::passportRowPadding.top()
63 		+ _titleHeight
64 		+ st::passportRowSkip
65 		+ _descriptionHeight
66 		+ st::passportRowPadding.bottom();
67 	return result;
68 }
69 
countAvailableWidth(int newWidth) const70 int FormRow::countAvailableWidth(int newWidth) const {
71 	return newWidth
72 		- st::passportRowPadding.left()
73 		- st::passportRowPadding.right()
74 		- (_ready
75 			? st::passportRowReadyIcon
76 			: st::passportRowEmptyIcon).width()
77 		- st::passportRowIconSkip;
78 }
79 
countAvailableWidth() const80 int FormRow::countAvailableWidth() const {
81 	return countAvailableWidth(width());
82 }
83 
paintEvent(QPaintEvent * e)84 void FormRow::paintEvent(QPaintEvent *e) {
85 	Painter p(this);
86 
87 	paintRipple(p, 0, 0);
88 
89 	const auto left = st::passportRowPadding.left();
90 	const auto availableWidth = countAvailableWidth();
91 	auto top = st::passportRowPadding.top();
92 
93 	const auto error = _errorAnimation.value(_error ? 1. : 0.);
94 
95 	p.setPen(st::passportRowTitleFg);
96 	_title.drawLeft(p, left, top, availableWidth, width());
97 	top += _titleHeight + st::passportRowSkip;
98 
99 	p.setPen(anim::pen(
100 		st::passportRowDescriptionFg,
101 		st::boxTextFgError,
102 		error));
103 	_description.drawLeft(p, left, top, availableWidth, width());
104 	top += _descriptionHeight + st::passportRowPadding.bottom();
105 
106 	const auto &icon = _ready
107 		? st::passportRowReadyIcon
108 		: st::passportRowEmptyIcon;
109 	if (error > 0. && !_ready) {
110 		icon.paint(
111 			p,
112 			width() - st::passportRowPadding.right() - icon.width(),
113 			(height() - icon.height()) / 2,
114 			width(),
115 			anim::color(st::menuIconFgOver, st::boxTextFgError, error));
116 	} else {
117 		icon.paint(
118 			p,
119 			width() - st::passportRowPadding.right() - icon.width(),
120 			(height() - icon.height()) / 2,
121 			width());
122 	}
123 }
124 
125 } // namespace Passport::Ui
126