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

View file

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

View file

@ -5,6 +5,7 @@
#include "controllers/taggedusers/TaggedUsersModel.hpp" #include "controllers/taggedusers/TaggedUsersModel.hpp"
#include "singletons/Logging.hpp" #include "singletons/Logging.hpp"
#include "singletons/Paths.hpp" #include "singletons/Paths.hpp"
#include "singletons/Settings.hpp"
#include "util/Helpers.hpp" #include "util/Helpers.hpp"
#include "util/LayoutCreator.hpp" #include "util/LayoutCreator.hpp"
@ -21,8 +22,6 @@
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
#include <algorithm>
namespace chatterino { namespace chatterino {
AdvancedPage::AdvancedPage() AdvancedPage::AdvancedPage()
@ -72,30 +71,12 @@ AdvancedPage::AdvancedPage()
[]() mutable { []() mutable {
getSettings()->cachePath = ""; // getSettings()->cachePath = ""; //
}); });
}
// Logs end // Logs end
// Timeoutbuttons // 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 timeoutLayout = tabs.appendTab(new QVBoxLayout, "Timeouts");
auto texts = timeoutLayout.emplace<QVBoxLayout>().withoutMargin(); auto texts = timeoutLayout.emplace<QVBoxLayout>().withoutMargin();
{ {
@ -113,97 +94,75 @@ AdvancedPage::AdvancedPage()
texts->setContentsMargins(0, 0, 0, 15); texts->setContentsMargins(0, 0, 0, 15);
texts->setSizeConstraint(QLayout::SetMaximumSize); texts->setSizeConstraint(QLayout::SetMaximumSize);
// build one line for each customizable button const auto valueChanged = [=] {
for (int i = 0; i < 8; i++) const auto index = QObject::sender()->objectName().toInt();
{
auto timeout = timeoutLayout.emplace<QHBoxLayout>().withoutMargin(); 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>(); line->setText("14");
buttonLabel->setText("Button " + QString::number(i + 1) + ": "); return;
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();
} }
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->setContentsMargins(40, 0, 0, 0);
timeout->setSizeConstraint(QLayout::SetMaximumSize); timeout->setSizeConstraint(QLayout::SetMaximumSize);
++itDurationPerUnit;
++itUnit;
++this->itDurationInput_;
++this->itUnitInput_;
} }
timeoutLayout->addStretch(); timeoutLayout->addStretch();
} }
// Timeoutbuttons end // 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 } // namespace chatterino

View file

@ -11,16 +11,9 @@ public:
private: private:
// list needed for dynamic timeout settings // list needed for dynamic timeout settings
QList<QLineEdit *> durationInputs_; std::vector<QLineEdit *> durationInputs_;
QList<QComboBox *> unitInputs_; 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 } // namespace chatterino