Refactored UserInfoPopup::TimeoutWidget.

This commit is contained in:
23rd 2019-08-25 13:05:24 +03:00 committed by TranRed
parent d63438c351
commit a893be796c

View file

@ -24,8 +24,6 @@
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply> #include <QNetworkReply>
#include <algorithm>
#define TEXT_FOLLOWERS "Followers: " #define TEXT_FOLLOWERS "Followers: "
#define TEXT_VIEWS "Views: " #define TEXT_VIEWS "Views: "
#define TEXT_CREATED "Created: " #define TEXT_CREATED "Created: "
@ -494,21 +492,23 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
layout->setSpacing(16); layout->setSpacing(16);
auto addButton = [&](Action action, const QString &text, const auto addLayout = [&](const QString &text) {
const QPixmap &pixmap) {
auto vbox = layout.emplace<QVBoxLayout>().withoutMargin(); auto vbox = layout.emplace<QVBoxLayout>().withoutMargin();
{
auto title = vbox.emplace<QHBoxLayout>().withoutMargin(); auto title = vbox.emplace<QHBoxLayout>().withoutMargin();
title->addStretch(1); title->addStretch(1);
auto label = title.emplace<Label>(text); auto label = title.emplace<Label>(text);
label->setHasOffset(false);
label->setStyleSheet("color: #BBB"); label->setStyleSheet("color: #BBB");
label->setHasOffset(false);
title->addStretch(1); title->addStretch(1);
auto hbox = vbox.emplace<QHBoxLayout>().withoutMargin(); auto hbox = vbox.emplace<QHBoxLayout>().withoutMargin();
hbox->setSpacing(0); hbox->setSpacing(0);
{ return hbox;
auto button = hbox.emplace<Button>(nullptr); };
const auto addButton = [&](Action action, const QString &title,
const QPixmap &pixmap) {
auto button = addLayout(title).emplace<Button>(nullptr);
button->setPixmap(pixmap); button->setPixmap(pixmap);
button->setScaleIndependantSize(buttonHeight, buttonHeight); button->setScaleIndependantSize(buttonHeight, buttonHeight);
button->setBorderColor(QColor(255, 255, 255, 127)); button->setBorderColor(QColor(255, 255, 255, 127));
@ -517,57 +517,33 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
button.getElement(), &Button::leftClicked, [this, action] { button.getElement(), &Button::leftClicked, [this, action] {
this->buttonClicked.invoke(std::make_pair(action, -1)); this->buttonClicked.invoke(std::make_pair(action, -1));
}); });
}
}
}; };
auto addTimeouts = [&](const QString &title_, const auto addTimeouts = [&](const QString &title) {
const std::vector<std::pair<QString, int>> &items) { auto hbox = addLayout(title);
auto vbox = layout.emplace<QVBoxLayout>().withoutMargin();
{
auto title = vbox.emplace<QHBoxLayout>().withoutMargin();
title->addStretch(1);
auto label = title.emplace<Label>(title_);
label->setStyleSheet("color: #BBB");
label->setHasOffset(false);
title->addStretch(1);
auto hbox = vbox.emplace<QHBoxLayout>().withoutMargin(); for (const auto &item : getSettings()->timeoutButtons.getValue())
hbox->setSpacing(0);
for (const auto &item : items)
{ {
auto a = hbox.emplace<EffectLabel2>(); auto a = hbox.emplace<EffectLabel2>();
a->getLabel().setText(std::get<0>(item)); a->getLabel().setText(QString::number(item.second) + item.first);
a->setScaleIndependantSize(buttonWidth, buttonHeight); a->setScaleIndependantSize(buttonWidth, buttonHeight);
a->setBorderColor(kBorderColor); a->setBorderColor(kBorderColor);
QObject::connect(a.getElement(), &EffectLabel2::leftClicked, const auto pair = std::make_pair(
[this, timeout = std::get<1>(item)] { Action::Timeout,
this->buttonClicked.invoke(std::make_pair( calculateTimeoutDuration(item.second, item.first));
Action::Timeout, timeout));
QObject::connect(
a.getElement(), &EffectLabel2::leftClicked, [this, pair] {
this->buttonClicked.invoke(pair);
}); });
} }
}
}; };
addButton(Unban, "unban", getApp()->resources->buttons.unban); addButton(Unban, "Unban", getApp()->resources->buttons.unban);
addTimeouts("Timeouts");
const auto timeoutButtons = getSettings()->timeoutButtons.getValue(); addButton(Ban, "Ban", getApp()->resources->buttons.ban);
std::vector<TimeoutButton> t(8); // Timeouts.
auto i = 0;
std::generate(t.begin(), t.end(), [&] {
const auto tButton = timeoutButtons[i];
const auto pair = std::make_pair(
QString::number(tButton.second) + tButton.first,
calculateTimeoutDuration(tButton.second, tButton.first));
i++;
return pair;
});
addTimeouts("Timeouts", t);
addButton(Ban, "ban", getApp()->resources->buttons.ban);
} }
void UserInfoPopup::TimeoutWidget::paintEvent(QPaintEvent *) void UserInfoPopup::TimeoutWidget::paintEvent(QPaintEvent *)