Generialize radiobutton options box

This commit is contained in:
Eric Kotato 2020-04-23 17:38:15 +03:00
parent 789ef6e6f8
commit 7189332b74
6 changed files with 194 additions and 127 deletions

View file

@ -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

View file

@ -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<Ui::RadiobuttonGroup>(cNetSpeedBoost());
for (int i = 0; i <= 3; i++) {
const auto button = Ui::CreateChild<Ui::Radiobutton>(
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<QPointer<BoxContent>>();
*box = getDelegate()->show(
Box<ConfirmBox>(
tr::ktg_net_boost_restart_desc(tr::now),
tr::lng_settings_restart_now(tr::now),
tr::lng_cancel(tr::now),
changeBoost));
}

View file

@ -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<Ui::FlatLabel> _description = { nullptr };
std::shared_ptr<Ui::RadiobuttonGroup> _boostGroup;
};

View file

@ -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<int, QString> &options,
Fn<void(int)> 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<int, QString> &options,
Fn<void(int)> 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<Ui::VerticalLayout>(this);
if (!_description.isEmpty()) {
content->add(
object_ptr<Ui::FlatLabel>(this, _description, st::boxLabel),
style::margins(
st::boxPadding.left(),
st::boxPadding.bottom(),
st::boxPadding.right(),
st::boxPadding.bottom()));
}
_group = std::make_shared<Ui::RadiobuttonGroup>(_startValue);
for (auto i = _options.constBegin(); i != _options.constEnd(); ++i) {
content->add(
object_ptr<Ui::Radiobutton>(
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<QPointer<BoxContent>>();
*box = getDelegate()->show(
Box<ConfirmBox>(
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

View file

@ -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<int, QString> &options, Fn<void(int)> saveCallback, bool warnRestart = false);
RadioBox(QWidget* parent, const QString &title, const QString &description, int currentValue, const QMap<int, QString> &options, Fn<void(int)> saveCallback, bool warnRestart = false);
protected:
void prepare() override;
private:
void save();
QString _title;
QString _description;
int _startValue;
QMap<int, QString> _options;
Fn<void(int)> _saveCallback;
bool _warnRestart = false;
std::shared_ptr<Ui::RadiobuttonGroup> _group;
};
} // namespace Kotato

View file

@ -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<Ui::VerticalLayout*> container) {
AddSkip(container);
AddSubsectionTitle(container, tr::ktg_settings_network());
const QMap<int, QString> 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<NetBoostBox>());
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);