From 7189332b744cbfc1335ca73bc1eace1b28a341f1 Mon Sep 17 00:00:00 2001 From: RadRussianRus Date: Thu, 23 Apr 2020 17:38:15 +0300 Subject: [PATCH] Generialize radiobutton options box --- Telegram/CMakeLists.txt | 4 +- .../kotato/boxes/net_boost_box.cpp | 89 -------------- .../SourceFiles/kotato/boxes/net_boost_box.h | 33 ------ .../SourceFiles/kotato/boxes/radio_box.cpp | 109 ++++++++++++++++++ Telegram/SourceFiles/kotato/boxes/radio_box.h | 40 +++++++ Telegram/SourceFiles/kotato/settings_menu.cpp | 46 +++++++- 6 files changed, 194 insertions(+), 127 deletions(-) delete mode 100644 Telegram/SourceFiles/kotato/boxes/net_boost_box.cpp delete mode 100644 Telegram/SourceFiles/kotato/boxes/net_boost_box.h create mode 100644 Telegram/SourceFiles/kotato/boxes/radio_box.cpp create mode 100644 Telegram/SourceFiles/kotato/boxes/radio_box.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 3b6a5a5a4..f2405ccba 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -617,8 +617,8 @@ PRIVATE kotato/boxes/confirm_box.h kotato/boxes/fonts_box.cpp kotato/boxes/fonts_box.h - kotato/boxes/net_boost_box.cpp - kotato/boxes/net_boost_box.h + kotato/boxes/radio_box.cpp + kotato/boxes/radio_box.h kotato/boxes/unpin_box.cpp kotato/boxes/unpin_box.h kotato/json_settings.cpp diff --git a/Telegram/SourceFiles/kotato/boxes/net_boost_box.cpp b/Telegram/SourceFiles/kotato/boxes/net_boost_box.cpp deleted file mode 100644 index 0ebaad12b..000000000 --- a/Telegram/SourceFiles/kotato/boxes/net_boost_box.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* -This file is part of Telegram Desktop, -the official desktop application for the Telegram messaging service. - -For license and copyright information please follow this link: -https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL -*/ -#include "kotato/boxes/net_boost_box.h" - -#include "lang/lang_keys.h" -#include "ui/widgets/checkbox.h" -#include "ui/widgets/labels.h" -#include "styles/style_layers.h" -#include "styles/style_boxes.h" -#include "boxes/confirm_box.h" -#include "kotato/json_settings.h" -#include "app.h" - -NetBoostBox::NetBoostBox(QWidget* parent) -{ -} - -void NetBoostBox::prepare() { - setTitle(tr::ktg_net_speed_boost_title()); - - addButton(tr::lng_settings_save(), [=] { save(); }); - addButton(tr::lng_cancel(), [=] { closeBox(); }); - - auto y = st::boxOptionListPadding.top(); - _description.create( - this, - tr::ktg_net_speed_boost_desc(tr::now), - st::boxLabel); - _description->moveToLeft(st::boxPadding.left(), y); - - y += _description->height() + st::boxMediumSkip; - - _boostGroup = std::make_shared(cNetSpeedBoost()); - - for (int i = 0; i <= 3; i++) { - const auto button = Ui::CreateChild( - this, - _boostGroup, - i, - BoostLabel(i), - st::autolockButton); - button->moveToLeft(st::boxPadding.left(), y); - y += button->heightNoMargins() + st::boxOptionListSkip; - } - showChildren(); - setDimensions(st::boxWidth, y); -} - -QString NetBoostBox::BoostLabel(int boost) { - switch (boost) { - case 0: - return tr::ktg_net_speed_boost_default(tr::now); - - case 1: - return tr::ktg_net_speed_boost_slight(tr::now); - - case 2: - return tr::ktg_net_speed_boost_medium(tr::now); - - case 3: - return tr::ktg_net_speed_boost_big(tr::now); - - default: - Unexpected("Boost in NetBoostBox::BoostLabel."); - } - return QString(); -} - -void NetBoostBox::save() { - const auto changeBoost = [=] { - SetNetworkBoost(_boostGroup->value()); - Kotato::JsonSettings::Write(); - App::restart(); - }; - - const auto box = std::make_shared>(); - - *box = getDelegate()->show( - Box( - tr::ktg_net_boost_restart_desc(tr::now), - tr::lng_settings_restart_now(tr::now), - tr::lng_cancel(tr::now), - changeBoost)); -} \ No newline at end of file diff --git a/Telegram/SourceFiles/kotato/boxes/net_boost_box.h b/Telegram/SourceFiles/kotato/boxes/net_boost_box.h deleted file mode 100644 index d6a16fbd5..000000000 --- a/Telegram/SourceFiles/kotato/boxes/net_boost_box.h +++ /dev/null @@ -1,33 +0,0 @@ -/* -This file is part of Kotatogram Desktop, -the unofficial app based on Telegram Desktop. - -For license and copyright information please follow this link: -https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL -*/ -#pragma once - -#include "boxes/abstract_box.h" - -namespace Ui { -class RadiobuttonGroup; -class Radiobutton; -class FlatLabel; -} // namespace Ui - -class NetBoostBox : public Ui::BoxContent { -public: - NetBoostBox(QWidget* parent); - - static QString BoostLabel(int boost); - -protected: - void prepare() override; - -private: - void save(); - - object_ptr _description = { nullptr }; - std::shared_ptr _boostGroup; - -}; \ No newline at end of file diff --git a/Telegram/SourceFiles/kotato/boxes/radio_box.cpp b/Telegram/SourceFiles/kotato/boxes/radio_box.cpp new file mode 100644 index 000000000..2d3cb7a91 --- /dev/null +++ b/Telegram/SourceFiles/kotato/boxes/radio_box.cpp @@ -0,0 +1,109 @@ +/* +This file is part of Kotatogram Desktop, +the unofficial app based on Telegram Desktop. + +For license and copyright information please follow this link: +https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL +*/ +#include "kotato/boxes/radio_box.h" + +#include "lang/lang_keys.h" +#include "ui/wrap/vertical_layout.h" +#include "ui/widgets/checkbox.h" +#include "ui/widgets/labels.h" +#include "boxes/confirm_box.h" +#include "styles/style_layers.h" +#include "styles/style_boxes.h" +#include "kotato/json_settings.h" +#include "app.h" + +namespace Kotato { + +RadioBox::RadioBox( + QWidget*, + const QString &title, + int currentValue, + const QMap &options, + Fn saveCallback, + bool warnRestart) +: _title(title) +, _startValue(currentValue) +, _options(options) +, _saveCallback(std::move(saveCallback)) +, _warnRestart(warnRestart) { +} + +RadioBox::RadioBox( + QWidget*, + const QString &title, + const QString &description, + int currentValue, + const QMap &options, + Fn saveCallback, + bool warnRestart) +: _title(title) +, _description(description) +, _startValue(currentValue) +, _options(options) +, _saveCallback(std::move(saveCallback)) +, _warnRestart(warnRestart) { +} + +void RadioBox::prepare() { + setTitle(rpl::single(_title)); + + addButton(tr::lng_settings_save(), [=] { save(); }); + addButton(tr::lng_cancel(), [=] { closeBox(); }); + + const auto content = Ui::CreateChild(this); + + if (!_description.isEmpty()) { + content->add( + object_ptr(this, _description, st::boxLabel), + style::margins( + st::boxPadding.left(), + st::boxPadding.bottom(), + st::boxPadding.right(), + st::boxPadding.bottom())); + } + + _group = std::make_shared(_startValue); + + for (auto i = _options.constBegin(); i != _options.constEnd(); ++i) { + content->add( + object_ptr( + this, + _group, + i.key(), + i.value(), + st::autolockButton), + style::margins( + st::boxPadding.left(), + st::boxPadding.bottom(), + st::boxPadding.right(), + st::boxPadding.bottom())); + } + + setDimensionsToContent(st::boxWidth, content); +} + +void RadioBox::save() { + if (_warnRestart) { + const auto saveAfterWarn = [=] { + _saveCallback(_group->value()); + }; + + const auto box = std::make_shared>(); + + *box = getDelegate()->show( + Box( + tr::lng_settings_need_restart(tr::now), + tr::lng_settings_restart_now(tr::now), + tr::lng_cancel(tr::now), + saveAfterWarn)); + } else { + _saveCallback(_group->value()); + } +} + +} // namespace Kotato \ No newline at end of file diff --git a/Telegram/SourceFiles/kotato/boxes/radio_box.h b/Telegram/SourceFiles/kotato/boxes/radio_box.h new file mode 100644 index 000000000..50f7bdf57 --- /dev/null +++ b/Telegram/SourceFiles/kotato/boxes/radio_box.h @@ -0,0 +1,40 @@ +/* +This file is part of Kotatogram Desktop, +the unofficial app based on Telegram Desktop. + +For license and copyright information please follow this link: +https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL +*/ +#pragma once + +#include "boxes/abstract_box.h" + +namespace Ui { +class RadiobuttonGroup; +class Radiobutton; +class FlatLabel; +} // namespace Ui + +namespace Kotato { + +class RadioBox : public Ui::BoxContent { +public: + RadioBox(QWidget* parent, const QString &title, int currentValue, const QMap &options, Fn saveCallback, bool warnRestart = false); + RadioBox(QWidget* parent, const QString &title, const QString &description, int currentValue, const QMap &options, Fn saveCallback, bool warnRestart = false); + +protected: + void prepare() override; + +private: + void save(); + + QString _title; + QString _description; + int _startValue; + QMap _options; + Fn _saveCallback; + bool _warnRestart = false; + std::shared_ptr _group; +}; + +} // namespace Kotato diff --git a/Telegram/SourceFiles/kotato/settings_menu.cpp b/Telegram/SourceFiles/kotato/settings_menu.cpp index 1503f8360..f491ac8ad 100644 --- a/Telegram/SourceFiles/kotato/settings_menu.cpp +++ b/Telegram/SourceFiles/kotato/settings_menu.cpp @@ -18,7 +18,7 @@ https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL #include "ui/text/text_utilities.h" // Ui::Text::ToUpper #include "boxes/connection_box.h" #include "kotato/boxes/fonts_box.h" -#include "kotato/boxes/net_boost_box.h" +#include "kotato/boxes/radio_box.h" #include "boxes/about_box.h" #include "boxes/confirm_box.h" #include "platform/platform_specific.h" @@ -38,6 +38,30 @@ https://github.com/kotatogram/kotatogram-desktop/blob/dev/LEGAL namespace Settings { +namespace { + +QString NetBoostLabel(int boost) { + switch (boost) { + case 0: + return tr::ktg_net_speed_boost_default(tr::now); + + case 1: + return tr::ktg_net_speed_boost_slight(tr::now); + + case 2: + return tr::ktg_net_speed_boost_medium(tr::now); + + case 3: + return tr::ktg_net_speed_boost_big(tr::now); + + default: + Unexpected("Boost in Settings::NetBoostLabel."); + } + return QString(); +} + +} // namespace + #define SettingsMenuСSwitch(LangKey, Option) AddButton( \ container, \ tr::LangKey(), \ @@ -192,13 +216,29 @@ void SetupKotatoNetwork(not_null container) { AddSkip(container); AddSubsectionTitle(container, tr::ktg_settings_network()); + const QMap netBoostOptions = { + { 0, NetBoostLabel(0) }, + { 1, NetBoostLabel(1) }, + { 2, NetBoostLabel(2) }, + { 3, NetBoostLabel(3) } + }; + AddButtonWithLabel( container, tr::ktg_settings_net_speed_boost(), - rpl::single(NetBoostBox::BoostLabel(cNetSpeedBoost())), + rpl::single(NetBoostLabel(cNetSpeedBoost())), st::settingsButton )->addClickHandler([=] { - Ui::show(Box()); + Ui::show(Box<::Kotato::RadioBox>( + tr::ktg_net_speed_boost_title(tr::now), + tr::ktg_net_speed_boost_desc(tr::now), + cNetSpeedBoost(), + netBoostOptions, + [=] (int value) { + SetNetworkBoost(value); + ::Kotato::JsonSettings::Write(); + App::restart(); + }, true)); }); AddSkip(container);