From cdc7051e5692e97effffe2051f6e88525479ac4f Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 25 Aug 2019 01:06:00 +0300 Subject: [PATCH] Considerably refactored code of Usercard. --- src/singletons/Settings.hpp | 8 +- src/widgets/dialogs/UserInfoPopup.cpp | 19 +-- src/widgets/settingspages/AdvancedPage.cpp | 171 ++++++++------------- src/widgets/settingspages/AdvancedPage.hpp | 11 +- 4 files changed, 80 insertions(+), 129 deletions(-) diff --git a/src/singletons/Settings.hpp b/src/singletons/Settings.hpp index 3e04b2904..a079648e3 100644 --- a/src/singletons/Settings.hpp +++ b/src/singletons/Settings.hpp @@ -9,6 +9,8 @@ #include #include +using TimeoutButton = std::pair; + namespace chatterino { class Settings : public ABSettings @@ -197,8 +199,10 @@ public: /// Timeout buttons - ChatterinoSetting> timeoutDurationsPerUnit = { "/timeouts/timeoutDurationsPerUnit", { "1", "30", "1", "5", "30", "1", "1", "1" }}; - ChatterinoSetting> timeoutDurationUnits = { "/timeouts/timeoutDurationUnits", { "s", "s", "m", "m", "m", "h", "d", "w" }}; + ChatterinoSetting> timeoutButtons = { + "/timeouts/timeoutButtons", + { { "s", 1 }, { "s", 30 }, { "m", 1 }, { "m", 5 }, + { "m", 30 }, { "h", 1 }, { "d", 1 }, { "w", 1 } } }; private: void updateModerationActions(); diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index 36764cd31..d99070f94 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -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 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 durationsPerUnit = - getSettings()->timeoutDurationsPerUnit; - - std::vector durationUnits = getSettings()->timeoutDurationUnits; + const auto timeoutButtons = getSettings()->timeoutButtons.getValue(); std::vector> t(8); // Timeouts. auto i = 0; - std::generate(t.begin(), t.end(), [&] { + const auto tButton = timeoutButtons[i]; std::pair 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); diff --git a/src/widgets/settingspages/AdvancedPage.cpp b/src/widgets/settingspages/AdvancedPage.cpp index 69db906cb..35a1b70b3 100644 --- a/src/widgets/settingspages/AdvancedPage.cpp +++ b/src/widgets/settingspages/AdvancedPage.cpp @@ -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 #include -#include - 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 durationsPerUnit = - getSettings()->timeoutDurationsPerUnit; - std::vector::iterator itDurationPerUnit; - itDurationPerUnit = durationsPerUnit.begin(); - - std::vector durationUnits = - getSettings()->timeoutDurationUnits; - std::vector::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().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().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(); - 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().withoutMargin(); + + auto buttonLabel = timeout.emplace(); + 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 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 durationUnits = getSettings()->timeoutDurationUnits; - durationUnits[index] = newUnit; - getSettings()->timeoutDurationUnits = durationUnits; -} - } // namespace chatterino diff --git a/src/widgets/settingspages/AdvancedPage.hpp b/src/widgets/settingspages/AdvancedPage.hpp index bc556fb0d..6a36c2fa6 100644 --- a/src/widgets/settingspages/AdvancedPage.hpp +++ b/src/widgets/settingspages/AdvancedPage.hpp @@ -11,16 +11,9 @@ public: private: // list needed for dynamic timeout settings - QList durationInputs_; - QList unitInputs_; + std::vector durationInputs_; + std::vector unitInputs_; - // iterators used in dynamic timeout settings - QList::iterator itDurationInput_; - QList::iterator itUnitInput_; - -private slots: - void timeoutDurationChanged(const QString &newDuration); - void timeoutUnitChanged(const QString &newUnit); }; } // namespace chatterino