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"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "application.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"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "countries.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 {
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
typedef QList<const CountryInfo *> CountriesFiltered;
|
|
|
|
typedef QVector<int> CountriesIds;
|
|
|
|
typedef QHash<QChar, CountriesIds> CountriesByLetter;
|
|
|
|
typedef QVector<QString> CountryNames;
|
|
|
|
typedef QVector<CountryNames> CountriesNames;
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
CountriesByCode _countriesByCode;
|
|
|
|
CountriesByISO2 _countriesByISO2;
|
2014-05-30 08:53:19 +00:00
|
|
|
CountriesFiltered countriesFiltered, countriesAll, *countriesNow = &countriesAll;
|
|
|
|
CountriesByLetter countriesByLetter;
|
|
|
|
CountriesNames countriesNames;
|
|
|
|
|
2015-10-11 08:37:24 +00:00
|
|
|
QString lastValidISO;
|
2014-05-30 08:53:19 +00:00
|
|
|
int countriesCount = sizeof(countries) / sizeof(countries[0]);
|
|
|
|
|
|
|
|
void initCountries() {
|
2015-04-02 10:33:19 +00:00
|
|
|
if (!_countriesByCode.isEmpty()) return;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
_countriesByCode.reserve(countriesCount);
|
|
|
|
_countriesByISO2.reserve(countriesCount);
|
2014-05-30 08:53:19 +00:00
|
|
|
for (int i = 0; i < countriesCount; ++i) {
|
|
|
|
const CountryInfo *info(countries + i);
|
2015-04-02 10:33:19 +00:00
|
|
|
_countriesByCode.insert(info->code, info);
|
|
|
|
CountriesByISO2::const_iterator already = _countriesByISO2.constFind(info->iso2);
|
|
|
|
if (already != _countriesByISO2.cend()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
QString badISO = info->iso2;
|
2014-06-16 09:31:10 +00:00
|
|
|
(void)badISO;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-04-02 10:33:19 +00:00
|
|
|
_countriesByISO2.insert(info->iso2, info);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
countriesAll.reserve(countriesCount);
|
|
|
|
countriesFiltered.reserve(countriesCount);
|
|
|
|
countriesNames.resize(countriesCount);
|
|
|
|
}
|
2016-10-22 13:03:20 +00:00
|
|
|
|
|
|
|
} // namespace
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
const CountriesByCode &countriesByCode() {
|
|
|
|
initCountries();
|
|
|
|
return _countriesByCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CountriesByISO2 &countriesByISO2() {
|
|
|
|
initCountries();
|
|
|
|
return _countriesByISO2;
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
QString findValidCode(QString fullCode) {
|
|
|
|
while (fullCode.length()) {
|
2015-04-02 10:33:19 +00:00
|
|
|
CountriesByCode::const_iterator i = _countriesByCode.constFind(fullCode);
|
|
|
|
if (i != _countriesByCode.cend()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
return (*i)->code;
|
|
|
|
}
|
|
|
|
fullCode = fullCode.mid(0, fullCode.length() - 1);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-11-24 19:28:23 +00:00
|
|
|
CountryInput::CountryInput(QWidget *parent, const style::InputField &st) : TWidget(parent)
|
|
|
|
, _st(st)
|
|
|
|
, _text(lang(lng_country_code)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
initCountries();
|
2016-12-09 18:56:01 +00:00
|
|
|
resize(_st.width, _st.heightMin);
|
|
|
|
|
|
|
|
auto availableWidth = width() - _st.textMargins.left() - _st.textMargins.right() - _st.placeholderMargins.left() - _st.placeholderMargins.right() - 1;
|
|
|
|
auto placeholderFont = _st.placeholderFont->f;
|
|
|
|
placeholderFont.setStyleStrategy(QFont::PreferMatch);
|
|
|
|
auto metrics = QFontMetrics(placeholderFont);
|
|
|
|
auto placeholder = QString();// metrics.elidedText(lang(lng_country_fake_ph), Qt::ElideRight, availableWidth);
|
|
|
|
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) {
|
2016-12-13 17:07:56 +00:00
|
|
|
auto box = Ui::show(Box<CountrySelectBox>());
|
2015-10-11 08:37:24 +00:00
|
|
|
connect(box, SIGNAL(countryChosen(const QString&)), this, SLOT(onChooseCountry(const QString&)));
|
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();
|
2014-05-30 08:53:19 +00:00
|
|
|
if (code.length()) {
|
2015-04-02 10:33:19 +00:00
|
|
|
CountriesByCode::const_iterator i = _countriesByCode.constFind(code);
|
|
|
|
if (i != _countriesByCode.cend()) {
|
2014-05-30 08:53:19 +00:00
|
|
|
const CountryInfo *info = *i;
|
|
|
|
lastValidISO = info->iso2;
|
|
|
|
setText(QString::fromUtf8(info->name));
|
|
|
|
} else {
|
|
|
|
setText(lang(lng_bad_country_code));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setText(lang(lng_country_code));
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CountryInput::onChooseCountry(const QString &iso) {
|
2015-12-07 18:09:05 +00:00
|
|
|
Ui::hideLayer();
|
2015-10-11 08:37:24 +00:00
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
CountriesByISO2::const_iterator i = _countriesByISO2.constFind(iso);
|
|
|
|
const CountryInfo *info = (i == _countriesByISO2.cend()) ? 0 : (*i);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
if (info) {
|
|
|
|
lastValidISO = info->iso2;
|
|
|
|
setText(QString::fromUtf8(info->name));
|
|
|
|
emit codeChanged(info->code);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
CountrySelectBox::CountrySelectBox(QWidget*)
|
2017-05-30 15:21:05 +00:00
|
|
|
: _select(this, st::contactsMultiSelect, langFactory(lng_country_ph)) {
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
2016-10-22 13:03:20 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void CountrySelectBox::prepare() {
|
2017-05-30 15:21:05 +00:00
|
|
|
setTitle(langFactory(lng_country_select));
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
_select->resizeToWidth(st::boxWidth);
|
2016-10-22 13:03:20 +00:00
|
|
|
_select->setQueryChangedCallback([this](const QString &query) { onFilterUpdate(query); });
|
|
|
|
_select->setSubmittedCallback([this](bool) { onSubmit(); });
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
_inner = setInnerWidget(object_ptr<Inner>(this), st::countriesScroll, _select->height());
|
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
addButton(langFactory(lng_close), [this] { closeBox(); });
|
2016-12-13 17:07:56 +00:00
|
|
|
|
|
|
|
setDimensions(st::boxWidth, st::boxMaxListHeight);
|
|
|
|
|
|
|
|
connect(_inner, SIGNAL(mustScrollTo(int, int)), this, SLOT(onScrollToY(int, int)));
|
|
|
|
connect(_inner, SIGNAL(countryChosen(const QString&)), this, SIGNAL(countryChosen(const QString&)));
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CountrySelectBox::onSubmit() {
|
|
|
|
_inner->chooseCountry();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountrySelectBox::keyPressEvent(QKeyEvent *e) {
|
|
|
|
if (e->key() == Qt::Key_Down) {
|
|
|
|
_inner->selectSkip(1);
|
|
|
|
} else if (e->key() == Qt::Key_Up) {
|
|
|
|
_inner->selectSkip(-1);
|
|
|
|
} else if (e->key() == Qt::Key_PageDown) {
|
2016-12-13 17:07:56 +00:00
|
|
|
_inner->selectSkipPage(height() - _select->height(), 1);
|
2016-10-20 16:32:15 +00:00
|
|
|
} else if (e->key() == Qt::Key_PageUp) {
|
2016-12-13 17:07:56 +00:00
|
|
|
_inner->selectSkipPage(height() - _select->height(), -1);
|
2016-10-20 16:32:15 +00:00
|
|
|
} else {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::keyPressEvent(e);
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountrySelectBox::resizeEvent(QResizeEvent *e) {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::resizeEvent(e);
|
2016-10-22 13:03:20 +00:00
|
|
|
|
|
|
|
_select->resizeToWidth(width());
|
2016-12-13 17:07:56 +00:00
|
|
|
_select->moveToLeft(0, 0);
|
2016-10-22 13:03:20 +00:00
|
|
|
|
|
|
|
_inner->resizeToWidth(width());
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 13:03:20 +00:00
|
|
|
void CountrySelectBox::onFilterUpdate(const QString &query) {
|
2016-12-13 17:07:56 +00:00
|
|
|
onScrollToY(0);
|
2016-10-22 13:03:20 +00:00
|
|
|
_inner->updateFilter(query);
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void CountrySelectBox::setInnerFocus() {
|
2016-10-22 13:03:20 +00:00
|
|
|
_select->setInnerFocus();
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
CountrySelectBox::Inner::Inner(QWidget *parent) : TWidget(parent)
|
|
|
|
, _rowHeight(st::countryRowHeight) {
|
2015-10-11 08:37:24 +00:00
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent);
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
CountriesByISO2::const_iterator l = _countriesByISO2.constFind(lastValidISO);
|
2014-05-30 08:53:19 +00:00
|
|
|
bool seenLastValid = false;
|
|
|
|
int already = countriesAll.size();
|
|
|
|
|
|
|
|
countriesByLetter.clear();
|
2015-04-02 10:33:19 +00:00
|
|
|
const CountryInfo *lastValid = (l == _countriesByISO2.cend()) ? 0 : (*l);
|
2014-05-30 08:53:19 +00:00
|
|
|
for (int i = 0; i < countriesCount; ++i) {
|
|
|
|
const CountryInfo *ins = lastValid ? (i ? (countries + i - (seenLastValid ? 0 : 1)) : lastValid) : (countries + i);
|
|
|
|
if (lastValid && i && ins == lastValid) {
|
|
|
|
seenLastValid = true;
|
|
|
|
++ins;
|
|
|
|
}
|
|
|
|
if (already > i) {
|
|
|
|
countriesAll[i] = ins;
|
|
|
|
} else {
|
|
|
|
countriesAll.push_back(ins);
|
|
|
|
}
|
2015-10-11 08:37:24 +00:00
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
auto namesList = QString::fromUtf8(ins->name).toLower().split(QRegularExpression("[\\s\\-]"), QString::SkipEmptyParts);
|
|
|
|
auto &names = countriesNames[i];
|
2014-05-30 08:53:19 +00:00
|
|
|
int l = namesList.size();
|
|
|
|
names.resize(0);
|
|
|
|
names.reserve(l);
|
|
|
|
for (int j = 0, l = namesList.size(); j < l; ++j) {
|
2017-10-24 17:11:35 +00:00
|
|
|
auto name = namesList[j].trimmed();
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!name.length()) continue;
|
|
|
|
|
2017-10-24 17:11:35 +00:00
|
|
|
auto ch = name[0];
|
|
|
|
auto &v = countriesByLetter[ch];
|
2014-05-30 08:53:19 +00:00
|
|
|
if (v.isEmpty() || v.back() != i) {
|
|
|
|
v.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
names.push_back(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-11 08:37:24 +00:00
|
|
|
_filter = qsl("a");
|
|
|
|
updateFilter();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void CountrySelectBox::Inner::paintEvent(QPaintEvent *e) {
|
2015-10-11 08:37:24 +00:00
|
|
|
Painter p(this);
|
2014-05-30 08:53:19 +00:00
|
|
|
QRect r(e->rect());
|
2015-10-11 08:37:24 +00:00
|
|
|
p.setClipRect(r);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
auto ms = getms();
|
2014-05-30 08:53:19 +00:00
|
|
|
int l = countriesNow->size();
|
|
|
|
if (l) {
|
2015-10-11 08:37:24 +00:00
|
|
|
if (r.intersects(QRect(0, 0, width(), st::countriesSkip))) {
|
2016-10-31 12:29:26 +00:00
|
|
|
p.fillRect(r.intersected(QRect(0, 0, width(), st::countriesSkip)), st::countryRowBg);
|
2015-10-11 08:37:24 +00:00
|
|
|
}
|
|
|
|
int32 from = floorclamp(r.y() - st::countriesSkip, _rowHeight, 0, l);
|
|
|
|
int32 to = ceilclamp(r.y() + r.height() - st::countriesSkip, _rowHeight, 0, l);
|
|
|
|
for (int32 i = from; i < to; ++i) {
|
2016-12-05 11:01:08 +00:00
|
|
|
auto selected = (i == (_pressed >= 0 ? _pressed : _selected));
|
|
|
|
auto y = st::countriesSkip + i * _rowHeight;
|
|
|
|
|
|
|
|
p.fillRect(0, y, width(), _rowHeight, selected ? st::countryRowBgOver : st::countryRowBg);
|
|
|
|
if (_ripples.size() > i && _ripples[i]) {
|
|
|
|
_ripples[i]->paint(p, 0, y, width(), ms);
|
|
|
|
if (_ripples[i]->empty()) {
|
|
|
|
_ripples[i].reset();
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 08:37:24 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
auto code = QString("+") + (*countriesNow)[i]->code;
|
|
|
|
auto codeWidth = st::countryRowCodeFont->width(code);
|
2015-10-11 08:37:24 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
auto name = QString::fromUtf8((*countriesNow)[i]->name);
|
|
|
|
auto nameWidth = st::countryRowNameFont->width(name);
|
|
|
|
auto availWidth = width() - st::countryRowPadding.left() - st::countryRowPadding.right() - codeWidth - st::boxLayerScroll.width;
|
2015-10-11 08:37:24 +00:00
|
|
|
if (nameWidth > availWidth) {
|
|
|
|
name = st::countryRowNameFont->elided(name, availWidth);
|
|
|
|
nameWidth = st::countryRowNameFont->width(name);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-10-11 08:37:24 +00:00
|
|
|
|
|
|
|
p.setFont(st::countryRowNameFont);
|
2016-10-31 12:29:26 +00:00
|
|
|
p.setPen(st::countryRowNameFg);
|
2015-10-11 08:37:24 +00:00
|
|
|
p.drawTextLeft(st::countryRowPadding.left(), y + st::countryRowPadding.top(), width(), name);
|
2016-10-31 12:29:26 +00:00
|
|
|
|
2015-10-11 08:37:24 +00:00
|
|
|
p.setFont(st::countryRowCodeFont);
|
2016-12-05 11:01:08 +00:00
|
|
|
p.setPen(selected ? st::countryRowCodeFgOver : st::countryRowCodeFg);
|
2015-10-11 08:37:24 +00:00
|
|
|
p.drawTextLeft(st::countryRowPadding.left() + nameWidth + st::countryRowPadding.right(), y + st::countryRowPadding.top(), width(), code);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-10-31 12:29:26 +00:00
|
|
|
p.fillRect(r, st::boxBg);
|
|
|
|
p.setFont(st::noContactsFont);
|
|
|
|
p.setPen(st::noContactsColor);
|
2015-10-11 08:37:24 +00:00
|
|
|
p.drawText(QRect(0, 0, width(), st::noContactsHeight), lang(lng_country_none), style::al_center);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void CountrySelectBox::Inner::enterEventHook(QEvent *e) {
|
2015-10-11 08:37:24 +00:00
|
|
|
setMouseTracking(true);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void CountrySelectBox::Inner::leaveEventHook(QEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
_mouseSelection = false;
|
2015-10-11 08:37:24 +00:00
|
|
|
setMouseTracking(false);
|
2016-12-05 11:01:08 +00:00
|
|
|
if (_selected >= 0) {
|
2015-10-11 08:37:24 +00:00
|
|
|
updateSelectedRow();
|
2016-12-05 11:01:08 +00:00
|
|
|
_selected = -1;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void CountrySelectBox::Inner::mouseMoveEvent(QMouseEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
_mouseSelection = true;
|
|
|
|
updateSelected(e->pos());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void CountrySelectBox::Inner::mousePressEvent(QMouseEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
_mouseSelection = true;
|
|
|
|
updateSelected(e->pos());
|
|
|
|
|
|
|
|
setPressed(_selected);
|
|
|
|
if (_pressed >= 0 && _pressed < countriesNow->size()) {
|
|
|
|
if (_ripples.size() <= _pressed) {
|
|
|
|
_ripples.reserve(_pressed + 1);
|
|
|
|
while (_ripples.size() <= _pressed) {
|
2017-08-13 16:14:00 +00:00
|
|
|
_ripples.push_back(nullptr);
|
2016-12-05 11:01:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!_ripples[_pressed]) {
|
|
|
|
auto mask = Ui::RippleAnimation::rectMask(QSize(width(), _rowHeight));
|
2017-02-21 13:45:56 +00:00
|
|
|
_ripples[_pressed] = std::make_unique<Ui::RippleAnimation>(st::countryRipple, std::move(mask), [this, index = _pressed] {
|
2016-12-05 11:01:08 +00:00
|
|
|
updateRow(index);
|
|
|
|
});
|
|
|
|
_ripples[_pressed]->add(e->pos() - QPoint(0, st::countriesSkip + _pressed * _rowHeight));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountrySelectBox::Inner::mouseReleaseEvent(QMouseEvent *e) {
|
|
|
|
auto pressed = _pressed;
|
|
|
|
setPressed(-1);
|
|
|
|
updateSelectedRow();
|
2015-10-11 08:37:24 +00:00
|
|
|
if (e->button() == Qt::LeftButton) {
|
2016-12-05 11:01:08 +00:00
|
|
|
if ((pressed >= 0) && pressed == _selected) {
|
|
|
|
chooseCountry();
|
|
|
|
}
|
2015-10-11 08:37:24 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void CountrySelectBox::Inner::updateFilter(QString filter) {
|
2017-07-06 11:37:42 +00:00
|
|
|
auto words = TextUtilities::PrepareSearchWords(filter);
|
|
|
|
filter = words.isEmpty() ? QString() : words.join(' ');
|
2015-10-11 08:37:24 +00:00
|
|
|
if (_filter != filter) {
|
|
|
|
_filter = filter;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-10-11 08:37:24 +00:00
|
|
|
if (_filter.isEmpty()) {
|
|
|
|
countriesNow = &countriesAll;
|
|
|
|
} else {
|
|
|
|
QChar first = _filter[0].toLower();
|
|
|
|
CountriesIds &ids(countriesByLetter[first]);
|
|
|
|
|
2017-07-06 11:37:42 +00:00
|
|
|
QStringList::const_iterator fb = words.cbegin(), fe = words.cend(), fi;
|
2015-10-11 08:37:24 +00:00
|
|
|
|
|
|
|
countriesFiltered.clear();
|
|
|
|
for (CountriesIds::const_iterator i = ids.cbegin(), e = ids.cend(); i != e; ++i) {
|
|
|
|
int index = *i;
|
|
|
|
CountryNames &names(countriesNames[index]);
|
|
|
|
CountryNames::const_iterator nb = names.cbegin(), ne = names.cend(), ni;
|
|
|
|
for (fi = fb; fi != fe; ++fi) {
|
|
|
|
QString filterName(*fi);
|
|
|
|
for (ni = nb; ni != ne; ++ni) {
|
|
|
|
if (ni->startsWith(*fi)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ni == ne) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fi == fe) {
|
|
|
|
countriesFiltered.push_back(countriesAll[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
countriesNow = &countriesFiltered;
|
|
|
|
}
|
|
|
|
refresh();
|
2016-12-05 11:01:08 +00:00
|
|
|
_selected = countriesNow->isEmpty() ? -1 : 0;
|
2015-10-11 08:37:24 +00:00
|
|
|
update();
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void CountrySelectBox::Inner::selectSkip(int32 dir) {
|
2016-12-05 11:01:08 +00:00
|
|
|
_mouseSelection = false;
|
2015-10-11 08:37:24 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
int cur = (_selected >= 0) ? _selected : -1;
|
2015-10-11 08:37:24 +00:00
|
|
|
cur += dir;
|
|
|
|
if (cur <= 0) {
|
2016-12-05 11:01:08 +00:00
|
|
|
_selected = countriesNow->isEmpty() ? -1 : 0;
|
2015-10-11 08:37:24 +00:00
|
|
|
} else if (cur >= countriesNow->size()) {
|
2016-12-05 11:01:08 +00:00
|
|
|
_selected = -1;
|
2015-10-11 08:37:24 +00:00
|
|
|
} else {
|
2016-12-05 11:01:08 +00:00
|
|
|
_selected = cur;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-12-05 11:01:08 +00:00
|
|
|
if (_selected >= 0) {
|
|
|
|
emit mustScrollTo(st::countriesSkip + _selected * _rowHeight, st::countriesSkip + (_selected + 1) * _rowHeight);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void CountrySelectBox::Inner::selectSkipPage(int32 h, int32 dir) {
|
2015-10-11 08:37:24 +00:00
|
|
|
int32 points = h / _rowHeight;
|
|
|
|
if (!points) return;
|
|
|
|
selectSkip(points * dir);
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void CountrySelectBox::Inner::chooseCountry() {
|
2015-10-11 08:37:24 +00:00
|
|
|
QString result;
|
|
|
|
if (_filter.isEmpty()) {
|
2016-12-05 11:01:08 +00:00
|
|
|
if (_selected >= 0 && _selected < countriesAll.size()) {
|
|
|
|
result = countriesAll[_selected]->iso2;
|
2015-10-11 08:37:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-12-05 11:01:08 +00:00
|
|
|
if (_selected >= 0 && _selected < countriesFiltered.size()) {
|
|
|
|
result = countriesFiltered[_selected]->iso2;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-11 08:37:24 +00:00
|
|
|
emit countryChosen(result);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void CountrySelectBox::Inner::refresh() {
|
2015-10-11 08:37:24 +00:00
|
|
|
resize(width(), countriesNow->length() ? (countriesNow->length() * _rowHeight + st::countriesSkip) : st::noContactsHeight);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void CountrySelectBox::Inner::updateSelected(QPoint localPos) {
|
|
|
|
if (!_mouseSelection) return;
|
2015-10-11 08:37:24 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
auto in = parentWidget()->rect().contains(parentWidget()->mapFromGlobal(QCursor::pos()));
|
2015-10-11 08:37:24 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
auto selected = (in && localPos.y() >= st::countriesSkip && localPos.y() < st::countriesSkip + countriesNow->size() * _rowHeight) ? ((localPos.y() - st::countriesSkip) / _rowHeight) : -1;
|
|
|
|
if (_selected != selected) {
|
2015-10-11 08:37:24 +00:00
|
|
|
updateSelectedRow();
|
2016-12-05 11:01:08 +00:00
|
|
|
_selected = selected;
|
2015-10-11 08:37:24 +00:00
|
|
|
updateSelectedRow();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void CountrySelectBox::Inner::updateSelectedRow() {
|
2016-12-05 11:01:08 +00:00
|
|
|
updateRow(_selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CountrySelectBox::Inner::updateRow(int index) {
|
|
|
|
if (index >= 0) {
|
|
|
|
update(0, st::countriesSkip + index * _rowHeight, width(), _rowHeight);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-10-11 08:37:24 +00:00
|
|
|
}
|
2016-12-05 11:01:08 +00:00
|
|
|
|
|
|
|
void CountrySelectBox::Inner::setPressed(int pressed) {
|
|
|
|
if (_pressed >= 0 && _pressed < _ripples.size() && _ripples[_pressed]) {
|
|
|
|
_ripples[_pressed]->lastStop();
|
|
|
|
}
|
|
|
|
_pressed = pressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
CountrySelectBox::Inner::~Inner() = default;
|