Considerably refactored code of Usercard.

This commit is contained in:
23rd 2019-08-25 01:06:00 +03:00 committed by TranRed
parent f7fa000266
commit cdc7051e56
4 changed files with 80 additions and 129 deletions

View file

@ -9,6 +9,8 @@
#include <pajlada/settings/setting.hpp>
#include <pajlada/settings/settinglistener.hpp>
using TimeoutButton = std::pair<QString, int>;
namespace chatterino {
class Settings : public ABSettings
@ -197,8 +199,10 @@ public:
/// Timeout buttons
ChatterinoSetting<std::vector<QString>> timeoutDurationsPerUnit = { "/timeouts/timeoutDurationsPerUnit", { "1", "30", "1", "5", "30", "1", "1", "1" }};
ChatterinoSetting<std::vector<QString>> timeoutDurationUnits = { "/timeouts/timeoutDurationUnits", { "s", "s", "m", "m", "m", "h", "d", "w" }};
ChatterinoSetting<std::vector<TimeoutButton>> timeoutButtons = {
"/timeouts/timeoutButtons",
{ { "s", 1 }, { "s", 30 }, { "m", 1 }, { "m", 5 },
{ "m", 30 }, { "h", 1 }, { "d", 1 }, { "w", 1 } } };
private:
void updateModerationActions();

View file

@ -34,13 +34,12 @@ namespace {
const auto kBorderColor = QColor(255, 255, 255, 80);
int calculateTimeoutDuration(const QString &durationPerUnit,
const QString &unit)
int calculateTimeoutDuration(int durationPerUnit, const QString &unit)
{
static const QMap<QString, int> durations{
{"s", 1}, {"m", 60}, {"h", 3600}, {"d", 86400}, {"w", 604800},
};
return durationPerUnit.toInt() * durations[unit];
return durationPerUnit * durations[unit];
}
} // namespace
@ -555,18 +554,15 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
addButton(Unban, "unban", getApp()->resources->buttons.unban);
std::vector<QString> durationsPerUnit =
getSettings()->timeoutDurationsPerUnit;
std::vector<QString> durationUnits = getSettings()->timeoutDurationUnits;
const auto timeoutButtons = getSettings()->timeoutButtons.getValue();
std::vector<std::pair<QString, int>> t(8); // Timeouts.
auto i = 0;
std::generate(t.begin(), t.end(), [&] {
const auto tButton = timeoutButtons[i];
std::pair<QString, int> pair = std::make_pair(
durationsPerUnit[i] + durationUnits[i],
calculateTimeoutDuration(durationsPerUnit[i], durationUnits[i]));
QString::number(tButton.second) + tButton.first,
calculateTimeoutDuration(tButton.second, tButton.first));
i++;
return pair;
});
@ -593,8 +589,7 @@ void UserInfoPopup::fillLatestMessages()
for (size_t i = 0; i < snapshot.size(); i++)
{
MessagePtr message = snapshot[i];
if (message->loginName.compare(this->userName_, Qt::CaseInsensitive) ==
0 &&
if (!message->loginName.compare(this->userName_, Qt::CaseInsensitive) &&
!message->flags.has(MessageFlag::Whisper))
{
channelPtr->addMessage(message);

View file

@ -5,6 +5,7 @@
#include "controllers/taggedusers/TaggedUsersModel.hpp"
#include "singletons/Logging.hpp"
#include "singletons/Paths.hpp"
#include "singletons/Settings.hpp"
#include "util/Helpers.hpp"
#include "util/LayoutCreator.hpp"
@ -21,8 +22,6 @@
#include <QVBoxLayout>
#include <QtConcurrent/QtConcurrent>
#include <algorithm>
namespace chatterino {
AdvancedPage::AdvancedPage()
@ -72,30 +71,12 @@ AdvancedPage::AdvancedPage()
[]() mutable {
getSettings()->cachePath = ""; //
});
}
// Logs end
// Timeoutbuttons
const auto unitsForDropdown = QStringList{ "s", "m", "h", "d", "w" };
std::vector<QString> durationsPerUnit =
getSettings()->timeoutDurationsPerUnit;
std::vector<QString>::iterator itDurationPerUnit;
itDurationPerUnit = durationsPerUnit.begin();
std::vector<QString> durationUnits =
getSettings()->timeoutDurationUnits;
std::vector<QString>::iterator itUnit;
itUnit = durationUnits.begin();
for (int i = 0; i < 8; i++)
{
this->durationInputs_.append(new QLineEdit());
this->unitInputs_.append(new QComboBox());
}
this->itDurationInput_ = this->durationInputs_.begin();
this->itUnitInput_ = this->unitInputs_.begin();
{
auto timeoutLayout = tabs.appendTab(new QVBoxLayout, "Timeouts");
auto texts = timeoutLayout.emplace<QVBoxLayout>().withoutMargin();
{
@ -113,97 +94,75 @@ AdvancedPage::AdvancedPage()
texts->setContentsMargins(0, 0, 0, 15);
texts->setSizeConstraint(QLayout::SetMaximumSize);
// build one line for each customizable button
for (int i = 0; i < 8; i++)
{
auto timeout = timeoutLayout.emplace<QHBoxLayout>().withoutMargin();
const auto valueChanged = [=] {
const auto index = QObject::sender()->objectName().toInt();
const auto line = this->durationInputs_[index];
const auto duration = line->text().toInt();
const auto unit = this->unitInputs_[index]->currentText();
// safety mechanism for setting days and weeks
if (unit == "d" && duration > 14)
{
auto buttonLabel = timeout.emplace<QLabel>();
buttonLabel->setText("Button " + QString::number(i + 1) + ": ");
QLineEdit *lineEditDurationInput = *this->itDurationInput_;
lineEditDurationInput->setObjectName(QString::number(i));
lineEditDurationInput->setValidator(
new QIntValidator(1, 99, this));
lineEditDurationInput->setText(*itDurationPerUnit);
lineEditDurationInput->setAlignment(Qt::AlignRight);
lineEditDurationInput->setMaximumWidth(30);
timeout.append(lineEditDurationInput);
QComboBox *timeoutDurationUnit = *this->itUnitInput_;
timeoutDurationUnit->setObjectName(QString::number(i));
timeoutDurationUnit->addItems(unitsForDropdown);
timeoutDurationUnit->setCurrentText(*itUnit);
timeout.append(timeoutDurationUnit);
QObject::connect(lineEditDurationInput, &QLineEdit::textChanged,
this, &AdvancedPage::timeoutDurationChanged);
QObject::connect(timeoutDurationUnit,
&QComboBox::currentTextChanged, this,
&AdvancedPage::timeoutUnitChanged);
timeout->addStretch();
line->setText("14");
return;
}
else if (unit == "w" && duration > 2)
{
line->setText("2");
return;
}
auto timeouts = getSettings()->timeoutButtons.getValue();
timeouts[index] = TimeoutButton{ unit, duration };
getSettings()->timeoutButtons.setValue(timeouts);
};
// build one line for each customizable button
auto i = 0;
for (const auto tButton : getSettings()->timeoutButtons.getValue())
{
const auto buttonNumber = QString::number(i);
auto timeout = timeoutLayout.emplace<QHBoxLayout>().withoutMargin();
auto buttonLabel = timeout.emplace<QLabel>();
buttonLabel->setText(QString("Button %1: ").arg(++i));
auto *lineEditDurationInput = new QLineEdit();
lineEditDurationInput->setObjectName(buttonNumber);
lineEditDurationInput->setValidator(
new QIntValidator(1, 99, this));
lineEditDurationInput->setText(
QString::number(tButton.second));
lineEditDurationInput->setAlignment(Qt::AlignRight);
lineEditDurationInput->setMaximumWidth(30);
timeout.append(lineEditDurationInput);
auto *timeoutDurationUnit = new QComboBox();
timeoutDurationUnit->setObjectName(buttonNumber);
timeoutDurationUnit->addItems({ "s", "m", "h", "d", "w" });
timeoutDurationUnit->setCurrentText(tButton.first);
timeout.append(timeoutDurationUnit);
QObject::connect(lineEditDurationInput,
&QLineEdit::textChanged, this,
valueChanged);
QObject::connect(timeoutDurationUnit,
&QComboBox::currentTextChanged, this,
valueChanged);
timeout->addStretch();
this->durationInputs_.push_back(lineEditDurationInput);
this->unitInputs_.push_back(timeoutDurationUnit);
timeout->setContentsMargins(40, 0, 0, 0);
timeout->setSizeConstraint(QLayout::SetMaximumSize);
++itDurationPerUnit;
++itUnit;
++this->itDurationInput_;
++this->itUnitInput_;
}
timeoutLayout->addStretch();
}
// Timeoutbuttons end
}
void AdvancedPage::timeoutDurationChanged(const QString &newDuration)
{
QObject *sender = QObject::sender();
int index = sender->objectName().toInt();
this->itDurationInput_ = this->durationInputs_.begin() + index;
QLineEdit *durationPerUnit = *this->itDurationInput_;
this->itUnitInput_ = this->unitInputs_.begin() + index;
QComboBox *cbUnit = *this->itUnitInput_;
QString unit = cbUnit->currentText();
int valueInUnit = newDuration.toInt();
// safety mechanism for setting days and weeks
if (unit == "d" && valueInUnit > 14)
{
durationPerUnit->setText("14");
return;
}
else if (unit == "w" && valueInUnit > 2)
{
durationPerUnit->setText("2");
return;
}
std::vector<QString> durationsPerUnit =
getSettings()->timeoutDurationsPerUnit;
durationsPerUnit[index] = newDuration;
getSettings()->timeoutDurationsPerUnit = durationsPerUnit;
}
void AdvancedPage::timeoutUnitChanged(const QString &newUnit)
{
QObject *sender = QObject::sender();
int index = sender->objectName().toInt();
this->itDurationInput_ = this->durationInputs_.begin() + index;
QLineEdit *durationPerUnit = *this->itDurationInput_;
// safety mechanism for changing units (i.e. to days or weeks)
AdvancedPage::timeoutDurationChanged(durationPerUnit->text());
std::vector<QString> durationUnits = getSettings()->timeoutDurationUnits;
durationUnits[index] = newUnit;
getSettings()->timeoutDurationUnits = durationUnits;
}
} // namespace chatterino

View file

@ -11,16 +11,9 @@ public:
private:
// list needed for dynamic timeout settings
QList<QLineEdit *> durationInputs_;
QList<QComboBox *> unitInputs_;
std::vector<QLineEdit *> durationInputs_;
std::vector<QComboBox *> unitInputs_;
// iterators used in dynamic timeout settings
QList<QLineEdit *>::iterator itDurationInput_;
QList<QComboBox *>::iterator itUnitInput_;
private slots:
void timeoutDurationChanged(const QString &newDuration);
void timeoutUnitChanged(const QString &newUnit);
};
} // namespace chatterino