tdesktop/Telegram/SourceFiles/boxes/single_choice_box.cpp

61 lines
1.6 KiB
C++
Raw Normal View History

2019-01-05 11:08:02 +00:00
/*
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 "boxes/single_choice_box.h"
#include "lang/lang_keys.h"
#include "storage/localstorage.h"
#include "mainwindow.h"
#include "ui/widgets/checkbox.h"
#include "styles/style_boxes.h"
2019-01-11 10:07:56 +00:00
SingleChoiceBox::SingleChoiceBox(
QWidget*,
LangKey title,
const std::vector<QString> &optionTexts,
int initialSelection,
Fn<void(int)> callback)
: _title(title)
, _optionTexts(optionTexts)
, _initialSelection(initialSelection)
, _callback(callback) {
}
2019-01-05 11:08:02 +00:00
void SingleChoiceBox::prepare() {
setTitle(langFactory(_title));
2019-01-11 10:07:56 +00:00
addButton(langFactory(lng_box_ok), [=] { closeBox(); });
2019-01-05 11:08:02 +00:00
2019-01-11 10:07:56 +00:00
const auto group = std::make_shared<Ui::RadiobuttonGroup>(_initialSelection);
auto y = st::boxOptionListPadding.top()
+ st::autolockButton.margin.top();
_options.reserve(_optionTexts.size());
2019-01-05 11:08:02 +00:00
auto i = 0;
for (const auto &text : _optionTexts) {
_options.emplace_back(this, group, i, text, st::autolockButton);
2019-01-11 10:07:56 +00:00
_options.back()->moveToLeft(
st::boxPadding.left() + st::boxOptionListPadding.left(),
y);
2019-01-05 11:08:02 +00:00
y += _options.back()->heightNoMargins() + st::boxOptionListSkip;
i++;
}
2019-01-11 10:07:56 +00:00
group->setChangedCallback([=](int value) {
const auto weak = make_weak(this);
2019-01-05 11:08:02 +00:00
_callback(value);
2019-01-11 10:07:56 +00:00
if (weak) {
closeBox();
}
2019-01-05 11:08:02 +00:00
});
2019-01-11 10:07:56 +00:00
const auto height = y
- st::boxOptionListSkip
+ st::boxOptionListPadding.bottom()
+ st::boxPadding.bottom();
setDimensions(st::autolockWidth, height);
2019-01-05 11:08:02 +00:00
}