Beispiel #1
0
void ConfirmPhoneBox::prepare() {
	_about.create(this, st::confirmPhoneAboutLabel);
	TextWithEntities aboutText;
	auto formattedPhone = App::formatPhone(_phone);
	aboutText.text = lng_confirm_phone_about(lt_phone, formattedPhone);
	auto phonePosition = aboutText.text.indexOf(formattedPhone);
	if (phonePosition >= 0) {
		aboutText.entities.push_back(EntityInText(EntityInTextBold, phonePosition, formattedPhone.size()));
	}
	_about->setMarkedText(aboutText);

	_code.create(this, st::confirmPhoneCodeField, langFactory(lng_code_ph));
	_code->setAutoSubmit(_sentCodeLength, [this] { onSendCode(); });
	_code->setChangedCallback([this] { showError(QString()); });

	setTitle(langFactory(lng_confirm_phone_title));

	addButton(langFactory(lng_confirm_phone_send), [this] { onSendCode(); });
	addButton(langFactory(lng_cancel), [this] { closeBox(); });

	setDimensions(st::boxWidth, st::usernamePadding.top() + _code->height() + st::usernameSkip + _about->height() + st::usernameSkip);

	connect(_code, SIGNAL(submitted(bool)), this, SLOT(onSendCode()));

	showChildren();
}
Beispiel #2
0
void ConfirmPhoneBox::onCodeChanged() {
	if (_fixing) return;

	_fixing = true;
	QString newText, now = _code->getLastText();
	int oldPos = _code->textCursor().position(), newPos = -1;
	int oldLen = now.size(), digitCount = 0;
	for_const (auto ch, now) {
		if (ch.isDigit()) {
			++digitCount;
		}
	}

	if (_sentCodeLength > 0 && digitCount > _sentCodeLength) {
		digitCount = _sentCodeLength;
	}
	bool strict = (_sentCodeLength > 0 && digitCount == _sentCodeLength);

	newText.reserve(oldLen);
	int i = 0;
	for_const (auto ch, now) {
		if (i++ == oldPos) {
			newPos = newText.length();
		}
		if (ch.isDigit()) {
			if (!digitCount--) {
				break;
			}
			newText += ch;
			if (strict && !digitCount) {
				break;
			}
		}
	}
	if (newPos < 0) {
		newPos = newText.length();
	}
	if (newText != now) {
		now = newText;
		_code->setText(now);
		_code->setCursorPosition(newPos);
	}
	_fixing = false;

	showError(QString());
	if (strict) {
		onSendCode();
	}
}