2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-26 12:27:12 +00:00

177 lines
4.3 KiB
C++
Raw Permalink Normal View History

2020-04-23 17:38:15 +03:00
/*
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
*/
2021-08-25 01:09:37 +03:00
#include "kotato/boxes/kotato_radio_box.h"
2020-04-23 17:38:15 +03:00
2022-01-10 03:16:41 +03:00
#include "kotato/kotato_settings.h"
2020-04-23 17:38:15 +03:00
#include "lang/lang_keys.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/wrap/padding_wrap.h"
#include "ui/wrap/wrap.h"
2020-04-23 17:38:15 +03:00
#include "ui/widgets/checkbox.h"
#include "ui/widgets/labels.h"
2021-12-07 09:25:44 +03:00
#include "ui/boxes/confirm_box.h"
2020-04-23 17:38:15 +03:00
#include "styles/style_layers.h"
#include "styles/style_boxes.h"
#include "core/application.h"
2020-04-23 17:38:15 +03:00
namespace Kotato {
RadioBox::RadioBox(
QWidget*,
const QString &title,
int currentValue,
2021-03-31 12:33:13 +04:00
int valueCount,
Fn<QString(int)> labelGetter,
2020-04-23 17:38:15 +03:00
Fn<void(int)> saveCallback,
bool warnRestart)
: _title(title)
, _startValue(currentValue)
2021-03-31 12:33:13 +04:00
, _valueCount(valueCount)
, _labelGetter(labelGetter)
2020-04-23 17:38:15 +03:00
, _saveCallback(std::move(saveCallback))
, _warnRestart(warnRestart)
, _owned(this)
, _content(_owned.data()) {
2020-04-23 17:38:15 +03:00
}
RadioBox::RadioBox(
QWidget*,
const QString &title,
const QString &description,
int currentValue,
2021-03-31 12:33:13 +04:00
int valueCount,
Fn<QString(int)> labelGetter,
2020-04-23 17:38:15 +03:00
Fn<void(int)> saveCallback,
bool warnRestart)
: _title(title)
, _description(description)
, _startValue(currentValue)
2021-03-31 12:33:13 +04:00
, _valueCount(valueCount)
, _labelGetter(labelGetter)
2020-04-23 17:38:15 +03:00
, _saveCallback(std::move(saveCallback))
, _warnRestart(warnRestart)
, _owned(this)
, _content(_owned.data()) {
2020-04-23 17:38:15 +03:00
}
RadioBox::RadioBox(
QWidget*,
const QString &title,
int currentValue,
int valueCount,
Fn<QString(int)> labelGetter,
Fn<QString(int)> descriptionGetter,
Fn<void(int)> saveCallback,
bool warnRestart)
: _title(title)
, _startValue(currentValue)
, _valueCount(valueCount)
, _labelGetter(labelGetter)
, _descriptionGetter(descriptionGetter)
, _saveCallback(std::move(saveCallback))
, _warnRestart(warnRestart)
, _owned(this)
, _content(_owned.data()) {
}
RadioBox::RadioBox(
QWidget*,
const QString &title,
const QString &description,
int currentValue,
int valueCount,
Fn<QString(int)> labelGetter,
Fn<QString(int)> descriptionGetter,
Fn<void(int)> saveCallback,
bool warnRestart)
: _title(title)
, _description(description)
, _startValue(currentValue)
, _valueCount(valueCount)
, _labelGetter(labelGetter)
, _descriptionGetter(descriptionGetter)
, _saveCallback(std::move(saveCallback))
, _warnRestart(warnRestart)
, _owned(this)
, _content(_owned.data()) {
}
2020-04-23 17:38:15 +03:00
void RadioBox::prepare() {
setTitle(rpl::single(_title));
addButton(tr::lng_settings_save(), [=] { save(); });
addButton(tr::lng_cancel(), [=] { closeBox(); });
if (!_description.isEmpty()) {
_content->add(
object_ptr<Ui::FlatLabel>(_content, _description, st::boxDividerLabel),
2020-04-23 17:38:15 +03:00
style::margins(
st::boxPadding.left(),
0,
2020-04-23 17:38:15 +03:00
st::boxPadding.right(),
st::boxPadding.bottom()));
}
_group = std::make_shared<Ui::RadiobuttonGroup>(_startValue);
2021-03-31 12:33:13 +04:00
for (auto i = 0; i != _valueCount; ++i) {
const auto description = _descriptionGetter
? _descriptionGetter(i)
: QString();
_content->add(
2020-04-23 17:38:15 +03:00
object_ptr<Ui::Radiobutton>(
_content,
2020-04-23 17:38:15 +03:00
_group,
2021-03-31 12:33:13 +04:00
i,
_labelGetter(i),
2020-04-23 17:38:15 +03:00
st::autolockButton),
style::margins(
st::boxPadding.left(),
st::boxPadding.bottom(),
st::boxPadding.right(),
description.isEmpty() ? st::boxPadding.bottom() : 0));
if (!description.isEmpty()) {
_content->add(
object_ptr<Ui::FlatLabel>(_content, description, st::boxDividerLabel),
style::margins(
st::boxPadding.left()
+ st::autolockButton.margin.left()
+ st::autolockButton.margin.right()
+ st::defaultToggle.width
+ st::defaultToggle.border * 2,
0,
st::boxPadding.right(),
st::boxPadding.bottom()));
}
2020-04-23 17:38:15 +03:00
}
auto wrap = object_ptr<Ui::OverrideMargins>(this, std::move(_owned));
setDimensionsToContent(st::boxWidth, wrap.data());
setInnerWidget(std::move(wrap));
2020-04-23 17:38:15 +03:00
}
void RadioBox::save() {
2024-07-01 01:16:47 +04:00
_saveCallback(_group->current());
2020-04-23 17:38:15 +03:00
if (_warnRestart) {
const auto box = std::make_shared<QPointer<BoxContent>>();
*box = getDelegate()->show(
2024-07-01 01:16:47 +04:00
Ui::MakeConfirmBox({
.text = tr::lng_settings_need_restart(),
.confirmed = [] { Core::Restart(); },
.cancelled = crl::guard(this, [=] { closeBox(); box->data()->closeBox(); }),
.confirmText = tr::lng_settings_restart_now(),
.cancelText = tr::lng_settings_restart_later(),
}));
2020-04-23 17:38:15 +03:00
} else {
closeBox();
2020-04-23 17:38:15 +03:00
}
}
} // namespace Kotato