2016-08-19 17:26:31 +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.
|
2016-08-19 17:26:31 +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
|
2016-08-19 17:26:31 +00:00
|
|
|
*/
|
|
|
|
#include "settings/settings_background_widget.h"
|
|
|
|
|
|
|
|
#include "styles/style_settings.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-08-27 04:49:18 +00:00
|
|
|
#include "mainwidget.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/background_box.h"
|
2017-09-13 16:57:44 +00:00
|
|
|
#include "ui/wrap/slide_wrap.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2016-08-27 04:49:18 +00:00
|
|
|
#include "mainwindow.h"
|
2017-02-03 20:07:26 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
2017-02-07 17:39:47 +00:00
|
|
|
#include "window/themes/window_theme_editor.h"
|
2017-02-28 15:43:03 +00:00
|
|
|
#include "core/file_utilities.h"
|
2016-08-19 17:26:31 +00:00
|
|
|
|
|
|
|
namespace Settings {
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
BackgroundRow::BackgroundRow(QWidget *parent) : RpWidget(parent)
|
2016-11-11 13:46:04 +00:00
|
|
|
, _chooseFromGallery(this, lang(lng_settings_bg_from_gallery), st::boxLinkButton)
|
|
|
|
, _chooseFromFile(this, lang(lng_settings_bg_from_file), st::boxLinkButton)
|
2017-02-07 17:39:47 +00:00
|
|
|
, _editTheme(this, lang(lng_settings_bg_edit_theme), st::boxLinkButton)
|
2016-08-27 04:49:18 +00:00
|
|
|
, _radial(animation(this, &BackgroundRow::step_radial)) {
|
|
|
|
updateImage();
|
|
|
|
|
|
|
|
connect(_chooseFromGallery, SIGNAL(clicked()), this, SIGNAL(chooseFromGallery()));
|
|
|
|
connect(_chooseFromFile, SIGNAL(clicked()), this, SIGNAL(chooseFromFile()));
|
2017-02-07 17:39:47 +00:00
|
|
|
connect(_editTheme, SIGNAL(clicked()), this, SIGNAL(editTheme()));
|
2016-12-20 13:03:51 +00:00
|
|
|
checkNonDefaultTheme();
|
|
|
|
subscribe(Window::Theme::Background(), [this](const Window::Theme::BackgroundUpdate &update) {
|
|
|
|
if (update.type == Window::Theme::BackgroundUpdate::Type::ApplyingTheme) {
|
|
|
|
checkNonDefaultTheme();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundRow::checkNonDefaultTheme() {
|
2017-06-29 19:09:10 +00:00
|
|
|
if (Window::Theme::IsNonDefaultUsed()) {
|
2016-12-21 08:38:36 +00:00
|
|
|
if (!_useDefaultTheme) {
|
|
|
|
_useDefaultTheme.create(this, lang(lng_settings_bg_use_default), st::boxLinkButton);
|
|
|
|
_useDefaultTheme->show();
|
|
|
|
connect(_useDefaultTheme, SIGNAL(clicked()), this, SIGNAL(useDefault()));
|
2016-12-20 13:03:51 +00:00
|
|
|
resizeToWidth(width());
|
|
|
|
}
|
2016-12-21 08:38:36 +00:00
|
|
|
} else if (_useDefaultTheme) {
|
|
|
|
_useDefaultTheme.destroy();
|
2016-12-20 13:03:51 +00:00
|
|
|
resizeToWidth(width());
|
|
|
|
}
|
2016-08-27 04:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundRow::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
bool radial = false;
|
|
|
|
float64 radialOpacity = 0;
|
|
|
|
if (_radial.animating()) {
|
|
|
|
_radial.step(getms());
|
|
|
|
radial = _radial.animating();
|
|
|
|
radialOpacity = _radial.opacity();
|
|
|
|
}
|
|
|
|
if (radial) {
|
|
|
|
auto backThumb = App::main() ? App::main()->newBackgroundThumb() : ImagePtr();
|
|
|
|
if (backThumb->isNull()) {
|
|
|
|
p.drawPixmap(0, 0, _background);
|
|
|
|
} else {
|
2016-09-29 11:37:16 +00:00
|
|
|
const QPixmap &pix = App::main()->newBackgroundThumb()->pixBlurred(st::settingsBackgroundSize);
|
2017-01-11 08:16:44 +00:00
|
|
|
p.drawPixmap(0, 0, st::settingsBackgroundSize, st::settingsBackgroundSize, pix, 0, (pix.height() - st::settingsBackgroundSize * cIntRetinaFactor()) / 2, st::settingsBackgroundSize * cIntRetinaFactor(), st::settingsBackgroundSize * cIntRetinaFactor());
|
2016-08-27 04:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto outer = radialRect();
|
|
|
|
QRect inner(QPoint(outer.x() + (outer.width() - st::radialSize.width()) / 2, outer.y() + (outer.height() - st::radialSize.height()) / 2), st::radialSize);
|
|
|
|
p.setPen(Qt::NoPen);
|
2016-10-31 12:29:26 +00:00
|
|
|
p.setOpacity(radialOpacity);
|
|
|
|
p.setBrush(st::radialBg);
|
2016-08-27 04:49:18 +00:00
|
|
|
|
2016-12-03 12:10:35 +00:00
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.drawEllipse(inner);
|
|
|
|
}
|
2016-08-27 04:49:18 +00:00
|
|
|
|
|
|
|
p.setOpacity(1);
|
|
|
|
QRect arc(inner.marginsRemoved(QMargins(st::radialLine, st::radialLine, st::radialLine, st::radialLine)));
|
2016-10-31 12:29:26 +00:00
|
|
|
_radial.draw(p, arc, st::radialLine, st::radialFg);
|
2016-08-27 04:49:18 +00:00
|
|
|
} else {
|
|
|
|
p.drawPixmap(0, 0, _background);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int BackgroundRow::resizeGetHeight(int newWidth) {
|
2016-12-20 13:03:51 +00:00
|
|
|
auto linkTop = 0;
|
|
|
|
auto linkLeft = st::settingsBackgroundSize + st::settingsSmallSkip;
|
|
|
|
auto linkWidth = newWidth - linkLeft;
|
2016-08-27 04:49:18 +00:00
|
|
|
_chooseFromGallery->resizeToWidth(qMin(linkWidth, _chooseFromGallery->naturalWidth()));
|
|
|
|
_chooseFromFile->resizeToWidth(qMin(linkWidth, _chooseFromFile->naturalWidth()));
|
2017-02-07 17:39:47 +00:00
|
|
|
_editTheme->resizeToWidth(qMin(linkWidth, _editTheme->naturalWidth()));
|
2016-12-21 08:38:36 +00:00
|
|
|
if (_useDefaultTheme) {
|
|
|
|
_useDefaultTheme->resizeToWidth(qMin(linkWidth, _useDefaultTheme->naturalWidth()));
|
|
|
|
_useDefaultTheme->moveToLeft(linkLeft, linkTop, newWidth);
|
|
|
|
linkTop += _useDefaultTheme->height() + st::settingsSmallSkip;
|
2016-12-20 13:03:51 +00:00
|
|
|
}
|
|
|
|
_chooseFromGallery->moveToLeft(linkLeft, linkTop, newWidth);
|
|
|
|
linkTop += _chooseFromGallery->height() + st::settingsSmallSkip;
|
|
|
|
_chooseFromFile->moveToLeft(linkLeft, linkTop, newWidth);
|
2017-02-07 17:39:47 +00:00
|
|
|
linkTop += _chooseFromFile->height() + st::settingsSmallSkip;
|
|
|
|
_editTheme->moveToLeft(linkLeft, linkTop, newWidth);
|
2016-08-27 04:49:18 +00:00
|
|
|
|
|
|
|
return st::settingsBackgroundSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
float64 BackgroundRow::radialProgress() const {
|
|
|
|
if (auto m = App::main()) {
|
|
|
|
return m->chatBackgroundProgress();
|
|
|
|
}
|
|
|
|
return 1.;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BackgroundRow::radialLoading() const {
|
|
|
|
if (auto m = App::main()) {
|
|
|
|
if (m->chatBackgroundLoading()) {
|
|
|
|
m->checkChatBackground();
|
|
|
|
if (m->chatBackgroundLoading()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
const_cast<BackgroundRow*>(this)->updateImage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect BackgroundRow::radialRect() const {
|
2016-09-29 11:37:16 +00:00
|
|
|
return QRect(0, 0, st::settingsBackgroundSize, st::settingsBackgroundSize);
|
2016-08-27 04:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundRow::radialStart() {
|
|
|
|
if (radialLoading() && !_radial.animating()) {
|
|
|
|
_radial.start(radialProgress());
|
|
|
|
if (auto shift = radialTimeShift()) {
|
|
|
|
_radial.update(radialProgress(), !radialLoading(), getms() + shift);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
TimeMs BackgroundRow::radialTimeShift() const {
|
2016-08-27 04:49:18 +00:00
|
|
|
return st::radialDuration;
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
void BackgroundRow::step_radial(TimeMs ms, bool timer) {
|
2016-08-27 04:49:18 +00:00
|
|
|
_radial.update(radialProgress(), !radialLoading(), ms + radialTimeShift());
|
|
|
|
if (timer && _radial.animating()) {
|
|
|
|
rtlupdate(radialRect());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundRow::updateImage() {
|
2016-09-29 11:37:16 +00:00
|
|
|
int32 size = st::settingsBackgroundSize * cIntRetinaFactor();
|
2016-08-27 04:49:18 +00:00
|
|
|
QImage back(size, size, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
back.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
{
|
2016-12-03 12:10:35 +00:00
|
|
|
Painter p(&back);
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
2017-01-01 16:45:20 +00:00
|
|
|
auto &pix = Window::Theme::Background()->pixmap();
|
2016-08-27 04:49:18 +00:00
|
|
|
int sx = (pix.width() > pix.height()) ? ((pix.width() - pix.height()) / 2) : 0;
|
|
|
|
int sy = (pix.height() > pix.width()) ? ((pix.height() - pix.width()) / 2) : 0;
|
|
|
|
int s = (pix.width() > pix.height()) ? pix.height() : pix.width();
|
2016-09-29 11:37:16 +00:00
|
|
|
p.drawPixmap(0, 0, st::settingsBackgroundSize, st::settingsBackgroundSize, pix, sx, sy, s, s);
|
2016-08-27 04:49:18 +00:00
|
|
|
}
|
2016-11-28 15:45:07 +00:00
|
|
|
Images::prepareRound(back, ImageRoundRadius::Small);
|
2017-02-21 13:45:56 +00:00
|
|
|
_background = App::pixmapFromImageInPlace(std::move(back));
|
2016-08-27 04:49:18 +00:00
|
|
|
_background.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
|
|
|
|
rtlupdate(radialRect());
|
|
|
|
|
|
|
|
if (radialLoading()) {
|
|
|
|
radialStart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 17:26:31 +00:00
|
|
|
BackgroundWidget::BackgroundWidget(QWidget *parent, UserData *self) : BlockWidget(parent, self, lang(lng_settings_section_background)) {
|
2016-08-27 04:49:18 +00:00
|
|
|
createControls();
|
|
|
|
|
2016-10-28 12:44:28 +00:00
|
|
|
using Update = Window::Theme::BackgroundUpdate;
|
|
|
|
subscribe(Window::Theme::Background(), [this](const Update &update) {
|
2016-08-27 04:49:18 +00:00
|
|
|
if (update.type == Update::Type::New) {
|
|
|
|
_background->updateImage();
|
|
|
|
} else if (update.type == Update::Type::Start) {
|
|
|
|
needBackgroundUpdate(update.tiled);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
subscribe(Adaptive::Changed(), [this]() {
|
2017-10-01 08:15:40 +00:00
|
|
|
_adaptive->toggle(
|
|
|
|
(Global::AdaptiveChatLayout() == Adaptive::ChatLayout::Wide),
|
|
|
|
anim::type::normal);
|
2016-08-27 04:49:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundWidget::createControls() {
|
|
|
|
style::margins margin(0, 0, 0, st::settingsSmallSkip);
|
|
|
|
style::margins slidedPadding(0, margin.bottom() / 2, 0, margin.bottom() - (margin.bottom() / 2));
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
createChildRow(_background, margin);
|
2016-08-27 04:49:18 +00:00
|
|
|
connect(_background, SIGNAL(chooseFromGallery()), this, SLOT(onChooseFromGallery()));
|
|
|
|
connect(_background, SIGNAL(chooseFromFile()), this, SLOT(onChooseFromFile()));
|
2017-02-07 17:39:47 +00:00
|
|
|
connect(_background, SIGNAL(editTheme()), this, SLOT(onEditTheme()));
|
2016-12-21 08:38:36 +00:00
|
|
|
connect(_background, SIGNAL(useDefault()), this, SLOT(onUseDefaultTheme()));
|
2016-08-27 04:49:18 +00:00
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
createChildRow(_tile, margin, lang(lng_settings_bg_tile), [this](bool) { onTile(); }, Window::Theme::Background()->tile());
|
|
|
|
createChildRow(_adaptive, margin, slidedPadding, lang(lng_settings_adaptive_wide), [this](bool) { onAdaptive(); }, Global::AdaptiveForWide());
|
2017-01-14 18:50:16 +00:00
|
|
|
if (Global::AdaptiveChatLayout() != Adaptive::ChatLayout::Wide) {
|
2017-10-01 08:15:40 +00:00
|
|
|
_adaptive->hide(anim::type::instant);
|
2016-08-27 04:49:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundWidget::onChooseFromGallery() {
|
2016-12-13 17:07:56 +00:00
|
|
|
Ui::show(Box<BackgroundBox>());
|
2016-08-27 04:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundWidget::needBackgroundUpdate(bool tile) {
|
|
|
|
_tile->setChecked(tile);
|
|
|
|
_background->updateImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundWidget::onChooseFromFile() {
|
2016-10-28 12:44:28 +00:00
|
|
|
auto imgExtensions = cImgExtensions();
|
2017-02-03 20:07:26 +00:00
|
|
|
auto filters = QStringList(qsl("Theme files (*.tdesktop-theme *.tdesktop-palette *") + imgExtensions.join(qsl(" *")) + qsl(")"));
|
2017-02-28 15:43:03 +00:00
|
|
|
filters.push_back(FileDialog::AllFilesFilter());
|
|
|
|
FileDialog::GetOpenPath(lang(lng_choose_image), filters.join(qsl(";;")), base::lambda_guarded(this, [this](const FileDialog::OpenResult &result) {
|
|
|
|
if (result.paths.isEmpty() && result.remoteContent.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.paths.isEmpty()) {
|
|
|
|
auto filePath = result.paths.front();
|
|
|
|
if (filePath.endsWith(qstr(".tdesktop-theme"), Qt::CaseInsensitive)
|
|
|
|
|| filePath.endsWith(qstr(".tdesktop-palette"), Qt::CaseInsensitive)) {
|
|
|
|
Window::Theme::Apply(filePath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-08-27 04:49:18 +00:00
|
|
|
|
2017-02-28 15:43:03 +00:00
|
|
|
QImage img;
|
|
|
|
if (!result.remoteContent.isEmpty()) {
|
|
|
|
img = App::readImage(result.remoteContent);
|
|
|
|
} else {
|
|
|
|
img = App::readImage(result.paths.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (img.isNull() || img.width() <= 0 || img.height() <= 0) return;
|
|
|
|
|
|
|
|
if (img.width() > 4096 * img.height()) {
|
|
|
|
img = img.copy((img.width() - 4096 * img.height()) / 2, 0, 4096 * img.height(), img.height());
|
|
|
|
} else if (img.height() > 4096 * img.width()) {
|
|
|
|
img = img.copy(0, (img.height() - 4096 * img.width()) / 2, img.width(), 4096 * img.width());
|
|
|
|
}
|
|
|
|
|
|
|
|
Window::Theme::Background()->setImage(Window::Theme::kCustomBackground, std::move(img));
|
|
|
|
_tile->setChecked(false);
|
|
|
|
_background->updateImage();
|
|
|
|
}));
|
2016-08-19 17:26:31 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 17:39:47 +00:00
|
|
|
void BackgroundWidget::onEditTheme() {
|
|
|
|
Window::Theme::Editor::Start();
|
|
|
|
}
|
|
|
|
|
2016-12-21 08:38:36 +00:00
|
|
|
void BackgroundWidget::onUseDefaultTheme() {
|
2016-12-20 13:03:51 +00:00
|
|
|
Window::Theme::ApplyDefault();
|
|
|
|
}
|
|
|
|
|
2016-08-27 04:49:18 +00:00
|
|
|
void BackgroundWidget::onTile() {
|
2016-10-28 12:44:28 +00:00
|
|
|
Window::Theme::Background()->setTile(_tile->checked());
|
2016-08-27 04:49:18 +00:00
|
|
|
}
|
2016-08-19 17:26:31 +00:00
|
|
|
|
2016-08-27 04:49:18 +00:00
|
|
|
void BackgroundWidget::onAdaptive() {
|
|
|
|
if (Global::AdaptiveForWide() != _adaptive->entity()->checked()) {
|
|
|
|
Global::SetAdaptiveForWide(_adaptive->entity()->checked());
|
|
|
|
Adaptive::Changed().notify();
|
|
|
|
Local::writeUserSettings();
|
|
|
|
}
|
2016-08-19 17:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Settings
|