Slightly refactored code of Usercard.

This commit is contained in:
23rd 2019-08-24 16:42:08 +03:00 committed by TranRed
parent 0ce5e7f529
commit 25871bdc2f
4 changed files with 55 additions and 99 deletions

View file

@ -24,10 +24,28 @@
#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: "
namespace {
const auto kBorderColor = QColor(255, 255, 255, 80);
int calculateTimeoutDuration(const QString &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];
}
} // namespace
namespace chatterino { namespace chatterino {
UserInfoPopup::UserInfoPopup() UserInfoPopup::UserInfoPopup()
@ -90,9 +108,8 @@ UserInfoPopup::UserInfoPopup()
auto usercard = user.emplace<EffectLabel2>(this); auto usercard = user.emplace<EffectLabel2>(this);
usercard->getLabel().setText("Usercard"); usercard->getLabel().setText("Usercard");
QColor borderColor(255, 255, 255, 80); usercard->setBorderColor(kBorderColor);
usercard->setBorderColor(borderColor); viewLogs->setBorderColor(kBorderColor);
viewLogs->setBorderColor(borderColor);
auto mod = user.emplace<Button>(this); auto mod = user.emplace<Button>(this);
mod->setPixmap(app->resources->buttons.mod); mod->setPixmap(app->resources->buttons.mod);
@ -474,8 +491,6 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
.setLayoutType<QHBoxLayout>() .setLayoutType<QHBoxLayout>()
.withoutMargin(); .withoutMargin();
QColor borderColor(255, 255, 255, 80);
int buttonWidth = 40; int buttonWidth = 40;
int buttonHeight = 32; int buttonHeight = 32;
@ -528,7 +543,7 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
a->getLabel().setText(std::get<0>(item)); a->getLabel().setText(std::get<0>(item));
a->setScaleIndependantSize(buttonWidth, buttonHeight); a->setScaleIndependantSize(buttonWidth, buttonHeight);
a->setBorderColor(borderColor); a->setBorderColor(kBorderColor);
QObject::connect(a.getElement(), &EffectLabel2::leftClicked, QObject::connect(a.getElement(), &EffectLabel2::leftClicked,
[this, timeout = std::get<1>(item)] { [this, timeout = std::get<1>(item)] {
@ -546,32 +561,14 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
std::vector<QString> durationUnits = getSettings()->timeoutDurationUnits; std::vector<QString> durationUnits = getSettings()->timeoutDurationUnits;
addTimeouts( std::vector<std::pair<QString, int>> t(8); // Timeouts.
"Timeouts", auto i = 0;
{{durationsPerUnit[0] + durationUnits[0], std::generate(t.begin(), t.end(), [&] {
calculateTimeoutDuration(durationsPerUnit[0], durationUnits[0])}, return std::make_pair(durationsPerUnit[i] + durationUnits[i],
calculateTimeoutDuration(durationsPerUnit[i], durationUnits[i++]));
{durationsPerUnit[1] + durationUnits[1], });
calculateTimeoutDuration(durationsPerUnit[1], durationUnits[1])},
{durationsPerUnit[2] + durationUnits[2],
calculateTimeoutDuration(durationsPerUnit[2], durationUnits[2])},
{durationsPerUnit[3] + durationUnits[3],
calculateTimeoutDuration(durationsPerUnit[3], durationUnits[3])},
{durationsPerUnit[4] + durationUnits[4],
calculateTimeoutDuration(durationsPerUnit[4], durationUnits[4])},
{durationsPerUnit[5] + durationUnits[5],
calculateTimeoutDuration(durationsPerUnit[5], durationUnits[5])},
{durationsPerUnit[6] + durationUnits[6],
calculateTimeoutDuration(durationsPerUnit[6], durationUnits[6])},
{durationsPerUnit[7] + durationUnits[7],
calculateTimeoutDuration(durationsPerUnit[7], durationUnits[7])}});
addTimeouts("Timeouts", t);
addButton(Ban, "ban", getApp()->resources->buttons.ban); addButton(Ban, "ban", getApp()->resources->buttons.ban);
} }
@ -604,34 +601,4 @@ void UserInfoPopup::fillLatestMessages()
this->latestMessages_->setChannel(channelPtr); this->latestMessages_->setChannel(channelPtr);
} }
int UserInfoPopup::calculateTimeoutDuration(const QString &durationPerUnit,
const QString &unit)
{
int valueInUnit = durationPerUnit.toInt();
int valueInSec = 0;
if (unit == "s")
{
valueInSec = valueInUnit;
}
else if (unit == "m")
{
valueInSec = valueInUnit * 60;
}
else if (unit == "h")
{
valueInSec = valueInUnit * 60 * 60;
}
else if (unit == "d")
{
valueInSec = valueInUnit * 24 * 60 * 60;
}
else if (unit == "w")
{
valueInSec = valueInUnit * 7 * 24 * 60 * 60;
}
return valueInSec;
}
} // namespace chatterino } // namespace chatterino

View file

@ -34,8 +34,6 @@ private:
bool isMod_; bool isMod_;
bool isBroadcaster_; bool isBroadcaster_;
static int calculateTimeoutDuration(const QString &durationPerUnit, const QString &unit);
QString userName_; QString userName_;
QString userId_; QString userId_;
ChannelPtr channel_; ChannelPtr channel_;

View file

@ -21,6 +21,8 @@
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
#include <algorithm>
namespace chatterino { namespace chatterino {
AdvancedPage::AdvancedPage() AdvancedPage::AdvancedPage()
@ -74,12 +76,7 @@ AdvancedPage::AdvancedPage()
// Logs end // Logs end
// Timeoutbuttons // Timeoutbuttons
QStringList unitsForDropdown = QStringList(); const auto unitsForDropdown = QStringList{ "s", "m", "h", "d", "w" };
unitsForDropdown.append("s");
unitsForDropdown.append("m");
unitsForDropdown.append("h");
unitsForDropdown.append("d");
unitsForDropdown.append("w");
std::vector<QString> durationsPerUnit = std::vector<QString> durationsPerUnit =
getSettings()->timeoutDurationsPerUnit; getSettings()->timeoutDurationsPerUnit;
@ -93,11 +90,11 @@ AdvancedPage::AdvancedPage()
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
{ {
durationInputs.append(new QLineEdit()); this->durationInputs_.append(new QLineEdit());
unitInputs.append(new QComboBox()); this->unitInputs_.append(new QComboBox());
} }
itDurationInput = durationInputs.begin(); this->itDurationInput_ = this->durationInputs_.begin();
itUnitInput = unitInputs.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();
@ -124,7 +121,7 @@ AdvancedPage::AdvancedPage()
auto buttonLabel = timeout.emplace<QLabel>(); auto buttonLabel = timeout.emplace<QLabel>();
buttonLabel->setText("Button " + QString::number(i + 1) + ": "); buttonLabel->setText("Button " + QString::number(i + 1) + ": ");
QLineEdit *lineEditDurationInput = *itDurationInput; QLineEdit *lineEditDurationInput = *this->itDurationInput_;
lineEditDurationInput->setObjectName(QString::number(i)); lineEditDurationInput->setObjectName(QString::number(i));
lineEditDurationInput->setValidator( lineEditDurationInput->setValidator(
new QIntValidator(1, 99, this)); new QIntValidator(1, 99, this));
@ -133,7 +130,7 @@ AdvancedPage::AdvancedPage()
lineEditDurationInput->setMaximumWidth(30); lineEditDurationInput->setMaximumWidth(30);
timeout.append(lineEditDurationInput); timeout.append(lineEditDurationInput);
QComboBox *timeoutDurationUnit = *itUnitInput; QComboBox *timeoutDurationUnit = *this->itUnitInput_;
timeoutDurationUnit->setObjectName(QString::number(i)); timeoutDurationUnit->setObjectName(QString::number(i));
timeoutDurationUnit->addItems(unitsForDropdown); timeoutDurationUnit->addItems(unitsForDropdown);
timeoutDurationUnit->setCurrentText(*itUnit); timeoutDurationUnit->setCurrentText(*itUnit);
@ -153,8 +150,8 @@ AdvancedPage::AdvancedPage()
++itDurationPerUnit; ++itDurationPerUnit;
++itUnit; ++itUnit;
++itDurationInput; ++this->itDurationInput_;
++itUnitInput; ++this->itUnitInput_;
} }
timeoutLayout->addStretch(); timeoutLayout->addStretch();
} }
@ -166,32 +163,26 @@ void AdvancedPage::timeoutDurationChanged(const QString &newDuration)
QObject *sender = QObject::sender(); QObject *sender = QObject::sender();
int index = sender->objectName().toInt(); int index = sender->objectName().toInt();
itDurationInput = durationInputs.begin() + index; this->itDurationInput_ = this->durationInputs_.begin() + index;
QLineEdit *durationPerUnit = *itDurationInput; QLineEdit *durationPerUnit = *this->itDurationInput_;
itUnitInput = unitInputs.begin() + index; this->itUnitInput_ = this->unitInputs_.begin() + index;
QComboBox *cbUnit = *itUnitInput; QComboBox *cbUnit = *this->itUnitInput_;
QString unit = cbUnit->currentText(); QString unit = cbUnit->currentText();
int valueInUnit = newDuration.toInt(); int valueInUnit = newDuration.toInt();
// safety mechanism for setting days and weeks // safety mechanism for setting days and weeks
if (unit == "d") if (unit == "d" && valueInUnit > 14)
{
if (valueInUnit > 14)
{ {
durationPerUnit->setText("14"); durationPerUnit->setText("14");
return; return;
} }
} else if (unit == "w" && valueInUnit > 2)
else if (unit == "w")
{
if (valueInUnit > 2)
{ {
durationPerUnit->setText("2"); durationPerUnit->setText("2");
return; return;
} }
}
std::vector<QString> durationsPerUnit = std::vector<QString> durationsPerUnit =
getSettings()->timeoutDurationsPerUnit; getSettings()->timeoutDurationsPerUnit;
@ -204,8 +195,8 @@ void AdvancedPage::timeoutUnitChanged(const QString &newUnit)
QObject *sender = QObject::sender(); QObject *sender = QObject::sender();
int index = sender->objectName().toInt(); int index = sender->objectName().toInt();
itDurationInput = durationInputs.begin() + index; this->itDurationInput_ = this->durationInputs_.begin() + index;
QLineEdit *durationPerUnit = *itDurationInput; QLineEdit *durationPerUnit = *this->itDurationInput_;
// safety mechanism for changing units (i.e. to days or weeks) // safety mechanism for changing units (i.e. to days or weeks)
AdvancedPage::timeoutDurationChanged(durationPerUnit->text()); AdvancedPage::timeoutDurationChanged(durationPerUnit->text());

View file

@ -11,12 +11,12 @@ public:
private: private:
// list needed for dynamic timeout settings // list needed for dynamic timeout settings
QList<QLineEdit *> durationInputs; QList<QLineEdit *> durationInputs_;
QList<QComboBox *> unitInputs; QList<QComboBox *> unitInputs_;
// iterators used in dynamic timeout settings // iterators used in dynamic timeout settings
QList<QLineEdit *>::iterator itDurationInput; QList<QLineEdit *>::iterator itDurationInput_;
QList<QComboBox *>::iterator itUnitInput; QList<QComboBox *>::iterator itUnitInput_;
private slots: private slots:
void timeoutDurationChanged(const QString &newDuration); void timeoutDurationChanged(const QString &newDuration);