2015-02-03 15:02:46 +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.
|
2015-02-03 15:02:46 +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
|
2015-02-03 15:02:46 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/background_box.h"
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2015-02-03 15:02:46 +00:00
|
|
|
#include "mainwidget.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2017-02-03 20:07:26 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
2016-11-21 17:46:29 +00:00
|
|
|
#include "ui/effects/round_checkbox.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2017-03-04 19:36:59 +00:00
|
|
|
#include "auth_session.h"
|
2019-01-03 12:36:01 +00:00
|
|
|
#include "data/data_session.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "styles/style_overview.h"
|
|
|
|
#include "styles/style_boxes.h"
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2019-01-16 12:25:29 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kBackgroundsInRow = 3;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-01-03 14:39:19 +00:00
|
|
|
class BackgroundBox::Inner
|
|
|
|
: public Ui::RpWidget
|
|
|
|
, private MTP::Sender
|
|
|
|
, private base::Subscriber {
|
2017-03-07 14:03:07 +00:00
|
|
|
public:
|
|
|
|
Inner(QWidget *parent);
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void setBackgroundChosenCallback(Fn<void(int index)> callback) {
|
2017-03-07 14:03:07 +00:00
|
|
|
_backgroundChosenCallback = std::move(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Inner();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent *e) override;
|
|
|
|
void mousePressEvent(QMouseEvent *e) override;
|
|
|
|
void mouseReleaseEvent(QMouseEvent *e) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void updateWallpapers();
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(int index)> _backgroundChosenCallback;
|
2017-03-07 14:03:07 +00:00
|
|
|
|
|
|
|
int _over = -1;
|
|
|
|
int _overDown = -1;
|
2019-01-16 12:25:29 +00:00
|
|
|
|
2017-03-07 14:03:07 +00:00
|
|
|
std::unique_ptr<Ui::RoundCheckbox> _check; // this is not a widget
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
BackgroundBox::BackgroundBox(QWidget*) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundBox::prepare() {
|
2017-05-30 15:21:05 +00:00
|
|
|
setTitle(langFactory(lng_backgrounds_header));
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2019-01-03 12:36:01 +00:00
|
|
|
addButton(langFactory(lng_close), [=] { closeBox(); });
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
setDimensions(st::boxWideWidth, st::boxMaxListHeight);
|
|
|
|
|
|
|
|
_inner = setInnerWidget(object_ptr<Inner>(this), st::backgroundScroll);
|
2019-01-03 12:36:01 +00:00
|
|
|
_inner->setBackgroundChosenCallback([=](int index) {
|
|
|
|
backgroundChosen(index);
|
|
|
|
});
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void BackgroundBox::backgroundChosen(int index) {
|
2019-01-16 12:25:29 +00:00
|
|
|
const auto &papers = Auth().data().wallpapers();
|
|
|
|
if (index >= 0 && index < papers.size()) {
|
|
|
|
App::main()->setChatBackground(papers[index]);
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
closeBox();
|
2016-10-20 16:32:15 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:39:19 +00:00
|
|
|
BackgroundBox::Inner::Inner(QWidget *parent) : RpWidget(parent)
|
|
|
|
, _check(std::make_unique<Ui::RoundCheckbox>(st::overviewCheck, [=] { update(); })) {
|
2016-11-21 16:24:23 +00:00
|
|
|
_check->setChecked(true, Ui::RoundCheckbox::SetStyle::Fast);
|
2019-01-16 12:25:29 +00:00
|
|
|
if (Auth().data().wallpapers().empty()) {
|
|
|
|
resize(kBackgroundsInRow * (st::backgroundSize.width() + st::backgroundPadding) + st::backgroundPadding, 2 * (st::backgroundSize.height() + st::backgroundPadding) + st::backgroundPadding);
|
2015-02-03 15:02:46 +00:00
|
|
|
} else {
|
|
|
|
updateWallpapers();
|
|
|
|
}
|
2019-01-16 12:25:29 +00:00
|
|
|
request(MTPaccount_GetWallPapers(
|
|
|
|
MTP_int(Auth().data().wallpapersHash())
|
|
|
|
)).done([=](const MTPaccount_WallPapers &result) {
|
|
|
|
if (Auth().data().updateWallpapers(result)) {
|
|
|
|
updateWallpapers();
|
|
|
|
}
|
|
|
|
}).send();
|
2016-09-26 13:57:08 +00:00
|
|
|
|
2019-01-16 12:25:29 +00:00
|
|
|
subscribe(Auth().downloaderTaskFinished(), [=] { update(); });
|
|
|
|
subscribe(Window::Theme::Background(), [=](const Window::Theme::BackgroundUpdate &update) {
|
2016-12-23 13:21:01 +00:00
|
|
|
if (update.paletteChanged()) {
|
2016-12-20 13:03:51 +00:00
|
|
|
_check->invalidateCache();
|
|
|
|
}
|
|
|
|
});
|
2015-02-03 15:02:46 +00:00
|
|
|
setMouseTracking(true);
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void BackgroundBox::Inner::updateWallpapers() {
|
2019-01-16 12:25:29 +00:00
|
|
|
const auto &papers = Auth().data().wallpapers();
|
|
|
|
const auto count = papers.size();
|
|
|
|
const auto rows = (count / kBackgroundsInRow)
|
|
|
|
+ (count % kBackgroundsInRow ? 1 : 0);
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2019-01-16 12:25:29 +00:00
|
|
|
resize(kBackgroundsInRow * (st::backgroundSize.width() + st::backgroundPadding) + st::backgroundPadding, rows * (st::backgroundSize.height() + st::backgroundPadding) + st::backgroundPadding);
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2019-01-16 12:25:29 +00:00
|
|
|
const auto preload = kBackgroundsInRow * 3;
|
|
|
|
for (const auto &paper : papers | ranges::view::take(preload)) {
|
|
|
|
paper.thumb->load(Data::FileOrigin());
|
2015-02-03 15:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void BackgroundBox::Inner::paintEvent(QPaintEvent *e) {
|
2015-02-03 15:02:46 +00:00
|
|
|
QRect r(e->rect());
|
2016-04-21 17:57:29 +00:00
|
|
|
Painter p(this);
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2019-01-16 12:25:29 +00:00
|
|
|
const auto &papers = Auth().data().wallpapers();
|
|
|
|
if (papers.empty()) {
|
2016-12-13 17:07:56 +00:00
|
|
|
p.setFont(st::noContactsFont);
|
|
|
|
p.setPen(st::noContactsColor);
|
2015-02-03 15:02:46 +00:00
|
|
|
p.drawText(QRect(0, 0, width(), st::noContactsHeight), lang(lng_contacts_loading), style::al_center);
|
2019-01-16 12:25:29 +00:00
|
|
|
return;
|
2015-02-03 15:02:46 +00:00
|
|
|
}
|
2019-01-16 12:25:29 +00:00
|
|
|
auto row = 0;
|
|
|
|
auto column = 0;
|
|
|
|
for (const auto &paper : papers) {
|
|
|
|
const auto increment = gsl::finally([&] {
|
|
|
|
++column;
|
|
|
|
if (column == kBackgroundsInRow) {
|
|
|
|
column = 0;
|
|
|
|
++row;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if ((st::backgroundSize.height() + st::backgroundPadding) * (row + 1) <= r.top()) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2019-01-16 12:25:29 +00:00
|
|
|
paper.thumb->load(Data::FileOrigin());
|
|
|
|
|
|
|
|
int x = st::backgroundPadding + column * (st::backgroundSize.width() + st::backgroundPadding);
|
|
|
|
int y = st::backgroundPadding + row * (st::backgroundSize.height() + st::backgroundPadding);
|
|
|
|
|
|
|
|
const auto &pix = paper.thumb->pix(
|
|
|
|
Data::FileOrigin(),
|
|
|
|
st::backgroundSize.width(),
|
|
|
|
st::backgroundSize.height());
|
|
|
|
p.drawPixmap(x, y, pix);
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2019-01-16 12:25:29 +00:00
|
|
|
if (paper.id == Window::Theme::Background()->id()) {
|
|
|
|
auto checkLeft = x + st::backgroundSize.width() - st::overviewCheckSkip - st::overviewCheck.size;
|
|
|
|
auto checkTop = y + st::backgroundSize.height() - st::overviewCheckSkip - st::overviewCheck.size;
|
|
|
|
_check->paint(p, getms(), checkLeft, checkTop, width());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2019-01-16 12:25:29 +00:00
|
|
|
void BackgroundBox::Inner::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
const auto newOver = [&] {
|
|
|
|
const auto x = e->pos().x();
|
|
|
|
const auto y = e->pos().y();
|
|
|
|
const auto width = st::backgroundSize.width();
|
|
|
|
const auto height = st::backgroundSize.height();
|
|
|
|
const auto skip = st::backgroundPadding;
|
|
|
|
const auto row = int((y - skip) / (height + skip));
|
|
|
|
const auto column = int((x - skip) / (width + skip));
|
|
|
|
if (y - row * (height + skip) > skip + height) {
|
|
|
|
return -1;
|
|
|
|
} else if (x - column * (width + skip) > skip + width) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const auto result = row * kBackgroundsInRow + column;
|
|
|
|
return (result < Auth().data().wallpapers().size()) ? result : -1;
|
|
|
|
}();
|
|
|
|
if (_over != newOver) {
|
2015-02-03 15:02:46 +00:00
|
|
|
_over = newOver;
|
2019-01-16 12:25:29 +00:00
|
|
|
setCursor((_over >= 0 || _overDown >= 0)
|
|
|
|
? style::cur_pointer
|
|
|
|
: style::cur_default);
|
2015-02-03 15:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void BackgroundBox::Inner::mousePressEvent(QMouseEvent *e) {
|
2015-02-03 15:02:46 +00:00
|
|
|
_overDown = _over;
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void BackgroundBox::Inner::mouseReleaseEvent(QMouseEvent *e) {
|
2015-02-03 15:02:46 +00:00
|
|
|
if (_overDown == _over && _over >= 0) {
|
2016-12-13 17:07:56 +00:00
|
|
|
if (_backgroundChosenCallback) {
|
|
|
|
_backgroundChosenCallback(_over);
|
|
|
|
}
|
2015-02-03 15:02:46 +00:00
|
|
|
} else if (_over < 0) {
|
|
|
|
setCursor(style::cur_default);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 16:24:23 +00:00
|
|
|
BackgroundBox::Inner::~Inner() = default;
|