2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2016-04-21 17:57:29 +00:00
|
|
|
#include "ui/countryinput.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
2016-10-22 13:03:20 +00:00
|
|
|
#include "ui/widgets/multi_select.h"
|
2016-12-05 11:01:08 +00:00
|
|
|
#include "ui/effects/ripple_animation.h"
|
2021-03-23 16:06:59 +00:00
|
|
|
#include "ui/boxes/country_select_box.h"
|
2019-06-22 12:01:54 +00:00
|
|
|
#include "data/data_countries.h"
|
2020-11-04 15:50:17 +00:00
|
|
|
#include "base/qt_adapters.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2016-10-20 16:32:15 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2016-11-24 19:28:23 +00:00
|
|
|
#include "styles/style_intro.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-06-22 12:01:54 +00:00
|
|
|
QString LastValidISO;
|
2016-10-22 13:03:20 +00:00
|
|
|
|
|
|
|
} // namespace
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2021-03-23 16:06:59 +00:00
|
|
|
CountryInput::CountryInput(QWidget *parent, const style::InputField &st)
|
|
|
|
: RpWidget(parent)
|
2016-11-24 19:28:23 +00:00
|
|
|
, _st(st)
|
2019-06-19 15:09:03 +00:00
|
|
|
, _text(tr::lng_country_code(tr::now)) {
|
2016-12-09 18:56:01 +00:00
|
|
|
resize(_st.width, _st.heightMin);
|
|
|
|
|
|
|
|
auto placeholderFont = _st.placeholderFont->f;
|
|
|
|
placeholderFont.setStyleStrategy(QFont::PreferMatch);
|
2021-03-13 20:08:07 +00:00
|
|
|
//auto metrics = QFontMetrics(placeholderFont);
|
2019-06-19 15:09:03 +00:00
|
|
|
auto placeholder = QString();// metrics.elidedText(tr::lng_country_fake_ph(tr::now), Qt::ElideRight, availableWidth);
|
2016-12-09 18:56:01 +00:00
|
|
|
if (!placeholder.isNull()) {
|
|
|
|
_placeholderPath.addText(0, QFontMetrics(placeholderFont).ascent(), placeholderFont, placeholder);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CountryInput::paintEvent(QPaintEvent *e) {
|
2016-11-24 19:28:23 +00:00
|
|
|
Painter p(this);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
QRect r(rect().intersected(e->rect()));
|
|
|
|
if (_st.textBg->c.alphaF() > 0.) {
|
|
|
|
p.fillRect(r, _st.textBg);
|
|
|
|
}
|
|
|
|
if (_st.border) {
|
2016-12-09 18:56:01 +00:00
|
|
|
p.fillRect(0, height() - _st.border, width(), _st.border, _st.borderFg);
|
2016-11-24 19:28:23 +00:00
|
|
|
}
|
2016-06-24 16:58:41 +00:00
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
st::introCountryIcon.paint(p, width() - st::introCountryIcon.width() - st::introCountryIconPosition.x(), st::introCountryIconPosition.y(), width());
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-06-24 16:58:41 +00:00
|
|
|
p.setFont(_st.font);
|
2016-11-24 19:28:23 +00:00
|
|
|
p.setPen(_st.textFg);
|
|
|
|
p.drawText(rect().marginsRemoved(_st.textMargins), _text, _st.textAlign);
|
2016-12-09 18:56:01 +00:00
|
|
|
if (!_placeholderPath.isEmpty()) {
|
|
|
|
auto placeholderShiftDegree = 1.;
|
|
|
|
p.save();
|
|
|
|
p.setClipRect(r);
|
|
|
|
|
|
|
|
auto placeholderTop = anim::interpolate(0, _st.placeholderShift, placeholderShiftDegree);
|
|
|
|
|
|
|
|
QRect r(rect().marginsRemoved(_st.textMargins + _st.placeholderMargins));
|
|
|
|
r.moveTop(r.top() + placeholderTop);
|
|
|
|
if (rtl()) r.moveLeft(width() - r.left() - r.width());
|
|
|
|
|
|
|
|
auto placeholderScale = 1. - (1. - _st.placeholderScale) * placeholderShiftDegree;
|
|
|
|
auto placeholderFg = anim::color(_st.placeholderFg, _st.placeholderFgActive, 0.);
|
|
|
|
placeholderFg = anim::color(placeholderFg, _st.placeholderFgError, 0.);
|
|
|
|
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(placeholderFg);
|
|
|
|
p.translate(r.topLeft());
|
|
|
|
p.scale(placeholderScale, placeholderScale);
|
|
|
|
p.drawPath(_placeholderPath);
|
|
|
|
|
|
|
|
p.restore();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CountryInput::mouseMoveEvent(QMouseEvent *e) {
|
2016-11-24 19:28:23 +00:00
|
|
|
bool newActive = rect().contains(e->pos());
|
2014-05-30 08:53:19 +00:00
|
|
|
if (_active != newActive) {
|
|
|
|
_active = newActive;
|
|
|
|
setCursor(_active ? style::cur_pointer : style::cur_default);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountryInput::mousePressEvent(QMouseEvent *e) {
|
|
|
|
mouseMoveEvent(e);
|
|
|
|
if (_active) {
|
2021-03-23 16:06:59 +00:00
|
|
|
auto box = Ui::show(Box<Ui::CountrySelectBox>());
|
|
|
|
box->countryChosen(
|
|
|
|
) | rpl::start_with_next([=](QString iso) {
|
|
|
|
chooseCountry(iso);
|
|
|
|
}, lifetime());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void CountryInput::enterEventHook(QEvent *e) {
|
2014-05-30 08:53:19 +00:00
|
|
|
setMouseTracking(true);
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void CountryInput::leaveEventHook(QEvent *e) {
|
2014-05-30 08:53:19 +00:00
|
|
|
setMouseTracking(false);
|
|
|
|
_active = false;
|
|
|
|
setCursor(style::cur_default);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountryInput::onChooseCode(const QString &code) {
|
2015-12-07 18:09:05 +00:00
|
|
|
Ui::hideLayer();
|
2018-05-30 15:08:12 +00:00
|
|
|
_chosenIso = QString();
|
2014-05-30 08:53:19 +00:00
|
|
|
if (code.length()) {
|
2019-06-22 12:01:54 +00:00
|
|
|
const auto &byCode = Data::CountriesByCode();
|
|
|
|
const auto i = byCode.constFind(code);
|
|
|
|
if (i != byCode.cend()) {
|
|
|
|
const auto info = *i;
|
|
|
|
_chosenIso = LastValidISO = info->iso2;
|
2014-05-30 08:53:19 +00:00
|
|
|
setText(QString::fromUtf8(info->name));
|
|
|
|
} else {
|
2019-06-19 15:09:03 +00:00
|
|
|
setText(tr::lng_bad_country_code(tr::now));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-06-19 15:09:03 +00:00
|
|
|
setText(tr::lng_country_code(tr::now));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:06:59 +00:00
|
|
|
bool CountryInput::chooseCountry(const QString &iso) {
|
2015-12-07 18:09:05 +00:00
|
|
|
Ui::hideLayer();
|
2015-10-11 08:37:24 +00:00
|
|
|
|
2019-06-22 12:01:54 +00:00
|
|
|
const auto &byISO2 = Data::CountriesByISO2();
|
|
|
|
const auto i = byISO2.constFind(iso);
|
|
|
|
const auto info = (i != byISO2.cend()) ? (*i) : nullptr;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-05-30 15:08:12 +00:00
|
|
|
_chosenIso = QString();
|
2014-05-30 08:53:19 +00:00
|
|
|
if (info) {
|
2019-06-22 12:01:54 +00:00
|
|
|
_chosenIso = LastValidISO = info->iso2;
|
2014-05-30 08:53:19 +00:00
|
|
|
setText(QString::fromUtf8(info->name));
|
2021-03-03 18:22:42 +00:00
|
|
|
codeChanged(info->code);
|
2014-05-30 08:53:19 +00:00
|
|
|
update();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountryInput::setText(const QString &newText) {
|
2016-11-24 19:28:23 +00:00
|
|
|
_text = _st.font->elided(newText, width() - _st.textMargins.left() - _st.textMargins.right());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|