2015-02-03 15:02:46 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2015-02-03 15:02:46 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2015-02-03 15:02:46 +00:00
|
|
|
*/
|
|
|
|
#include "stdafx.h"
|
2016-11-08 14:43:10 +00:00
|
|
|
#include "boxes/backgroundbox.h"
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2016-11-08 14:43:10 +00:00
|
|
|
#include "lang.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-04-21 17:57:29 +00:00
|
|
|
#include "styles/style_overview.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2016-11-21 17:46:29 +00:00
|
|
|
#include "ui/effects/round_checkbox.h"
|
2015-02-03 15:02:46 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
BackgroundBox::BackgroundBox(QWidget*) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundBox::prepare() {
|
|
|
|
setTitle(lang(lng_backgrounds_header));
|
2016-10-20 16:32:15 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
addButton(lang(lng_close), [this] { 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);
|
|
|
|
_inner->setBackgroundChosenCallback([this](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) {
|
2016-10-20 16:32:15 +00:00
|
|
|
if (index >= 0 && index < App::cServerBackgrounds().size()) {
|
2016-12-13 17:07:56 +00:00
|
|
|
auto &paper = App::cServerBackgrounds()[index];
|
2016-10-20 16:32:15 +00:00
|
|
|
if (App::main()) App::main()->setChatBackground(paper);
|
|
|
|
|
2016-10-28 12:44:28 +00:00
|
|
|
using Update = Window::Theme::BackgroundUpdate;
|
|
|
|
Window::Theme::Background()->notify(Update(Update::Type::Start, !paper.id));
|
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
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
BackgroundBox::Inner::Inner(QWidget *parent) : TWidget(parent)
|
2016-11-21 16:24:23 +00:00
|
|
|
, _check(std_::make_unique<Ui::RoundCheckbox>(st::overviewCheck, [this] { update(); })) {
|
|
|
|
_check->setChecked(true, Ui::RoundCheckbox::SetStyle::Fast);
|
2015-02-03 15:02:46 +00:00
|
|
|
if (App::cServerBackgrounds().isEmpty()) {
|
|
|
|
resize(BackgroundsInRow * (st::backgroundSize.width() + st::backgroundPadding) + st::backgroundPadding, 2 * (st::backgroundSize.height() + st::backgroundPadding) + st::backgroundPadding);
|
2016-10-20 16:32:15 +00:00
|
|
|
MTP::send(MTPaccount_GetWallPapers(), rpcDone(&Inner::gotWallpapers));
|
2015-02-03 15:02:46 +00:00
|
|
|
} else {
|
|
|
|
updateWallpapers();
|
|
|
|
}
|
2016-09-26 13:57:08 +00:00
|
|
|
|
|
|
|
subscribe(FileDownload::ImageLoaded(), [this] { update(); });
|
2016-12-31 13:34:41 +00:00
|
|
|
subscribe(Window::Theme::Background(), [this](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::gotWallpapers(const MTPVector<MTPWallPaper> &result) {
|
2015-02-03 15:02:46 +00:00
|
|
|
App::WallPapers wallpapers;
|
|
|
|
|
2017-01-17 08:50:22 +00:00
|
|
|
auto oldBackground = ImagePtr(qsl(":/gui/art/bg_initial.jpg"));
|
2016-11-07 15:24:28 +00:00
|
|
|
wallpapers.push_back(App::WallPaper(Window::Theme::kInitialBackground, oldBackground, oldBackground));
|
|
|
|
auto &v = result.c_vector().v;
|
|
|
|
for_const (auto &w, v) {
|
2015-02-03 15:02:46 +00:00
|
|
|
switch (w.type()) {
|
|
|
|
case mtpc_wallPaper: {
|
2016-11-07 15:24:28 +00:00
|
|
|
auto &d = w.c_wallPaper();
|
|
|
|
auto &sizes = d.vsizes.c_vector().v;
|
2015-02-03 15:02:46 +00:00
|
|
|
const MTPPhotoSize *thumb = 0, *full = 0;
|
|
|
|
int32 thumbLevel = -1, fullLevel = -1;
|
|
|
|
for (QVector<MTPPhotoSize>::const_iterator j = sizes.cbegin(), e = sizes.cend(); j != e; ++j) {
|
|
|
|
char size = 0;
|
|
|
|
int32 w = 0, h = 0;
|
|
|
|
switch (j->type()) {
|
|
|
|
case mtpc_photoSize: {
|
2016-11-07 15:24:28 +00:00
|
|
|
auto &s = j->c_photoSize().vtype.c_string().v;
|
2015-02-03 15:02:46 +00:00
|
|
|
if (s.size()) size = s[0];
|
|
|
|
w = j->c_photoSize().vw.v;
|
|
|
|
h = j->c_photoSize().vh.v;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_photoCachedSize: {
|
2016-11-07 15:24:28 +00:00
|
|
|
auto &s = j->c_photoCachedSize().vtype.c_string().v;
|
2015-02-03 15:02:46 +00:00
|
|
|
if (s.size()) size = s[0];
|
|
|
|
w = j->c_photoCachedSize().vw.v;
|
|
|
|
h = j->c_photoCachedSize().vh.v;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
if (!size || !w || !h) continue;
|
|
|
|
|
2015-02-03 16:44:54 +00:00
|
|
|
int32 newThumbLevel = qAbs((st::backgroundSize.width() * cIntRetinaFactor()) - w), newFullLevel = qAbs(2560 - w);
|
2015-02-03 15:02:46 +00:00
|
|
|
if (thumbLevel < 0 || newThumbLevel < thumbLevel) {
|
|
|
|
thumbLevel = newThumbLevel;
|
|
|
|
thumb = &(*j);
|
|
|
|
}
|
|
|
|
if (fullLevel < 0 || newFullLevel < fullLevel) {
|
|
|
|
fullLevel = newFullLevel;
|
|
|
|
full = &(*j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (thumb && full && full->type() != mtpc_photoSizeEmpty) {
|
2015-04-16 11:14:25 +00:00
|
|
|
wallpapers.push_back(App::WallPaper(d.vid.v ? d.vid.v : INT_MAX, App::image(*thumb), App::image(*full)));
|
2015-02-03 15:02:46 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_wallPaperSolid: {
|
2016-11-07 15:24:28 +00:00
|
|
|
auto &d = w.c_wallPaperSolid();
|
2015-02-03 15:02:46 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
App::cSetServerBackgrounds(wallpapers);
|
|
|
|
updateWallpapers();
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void BackgroundBox::Inner::updateWallpapers() {
|
2015-02-03 15:02:46 +00:00
|
|
|
_bgCount = App::cServerBackgrounds().size();
|
|
|
|
_rows = _bgCount / BackgroundsInRow;
|
|
|
|
if (_bgCount % BackgroundsInRow) ++_rows;
|
|
|
|
|
|
|
|
resize(BackgroundsInRow * (st::backgroundSize.width() + st::backgroundPadding) + st::backgroundPadding, _rows * (st::backgroundSize.height() + st::backgroundPadding) + st::backgroundPadding);
|
|
|
|
for (int i = 0; i < BackgroundsInRow * 3; ++i) {
|
|
|
|
if (i >= _bgCount) break;
|
|
|
|
|
|
|
|
App::cServerBackgrounds().at(i).thumb->load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if (_rows) {
|
|
|
|
for (int i = 0; i < _rows; ++i) {
|
|
|
|
if ((st::backgroundSize.height() + st::backgroundPadding) * (i + 1) <= r.top()) continue;
|
|
|
|
for (int j = 0; j < BackgroundsInRow; ++j) {
|
|
|
|
int index = i * BackgroundsInRow + j;
|
|
|
|
if (index >= _bgCount) break;
|
|
|
|
|
|
|
|
const App::WallPaper &paper(App::cServerBackgrounds().at(index));
|
|
|
|
paper.thumb->load();
|
|
|
|
|
|
|
|
int x = st::backgroundPadding + j * (st::backgroundSize.width() + st::backgroundPadding);
|
|
|
|
int y = st::backgroundPadding + i * (st::backgroundSize.height() + st::backgroundPadding);
|
|
|
|
|
|
|
|
const QPixmap &pix(paper.thumb->pix(st::backgroundSize.width(), st::backgroundSize.height()));
|
|
|
|
p.drawPixmap(x, y, pix);
|
|
|
|
|
2016-10-28 12:44:28 +00:00
|
|
|
if (paper.id == Window::Theme::Background()->id()) {
|
2016-11-21 16:24:23 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:32:15 +00:00
|
|
|
void BackgroundBox::Inner::mouseMoveEvent(QMouseEvent *e) {
|
2015-02-03 15:02:46 +00:00
|
|
|
int x = e->pos().x(), y = e->pos().y();
|
|
|
|
int row = int((y - st::backgroundPadding) / (st::backgroundSize.height() + st::backgroundPadding));
|
|
|
|
if (y - row * (st::backgroundSize.height() + st::backgroundPadding) > st::backgroundPadding + st::backgroundSize.height()) row = _rows + 1;
|
|
|
|
|
|
|
|
int col = int((x - st::backgroundPadding) / (st::backgroundSize.width() + st::backgroundPadding));
|
|
|
|
if (x - col * (st::backgroundSize.width() + st::backgroundPadding) > st::backgroundPadding + st::backgroundSize.width()) row = _rows + 1;
|
|
|
|
|
|
|
|
int newOver = row * BackgroundsInRow + col;
|
|
|
|
if (newOver >= _bgCount) newOver = -1;
|
|
|
|
if (newOver != _over) {
|
|
|
|
_over = newOver;
|
|
|
|
setCursor((_over >= 0 || _overDown >= 0) ? style::cur_pointer : style::cur_default);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|