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 <QNetworkReply>
#include <algorithm>
#define TEXT_FOLLOWERS "Followers: "
#define TEXT_VIEWS "Views: "
#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 {
UserInfoPopup::UserInfoPopup()
@ -90,9 +108,8 @@ UserInfoPopup::UserInfoPopup()
auto usercard = user.emplace<EffectLabel2>(this);
usercard->getLabel().setText("Usercard");
QColor borderColor(255, 255, 255, 80);
usercard->setBorderColor(borderColor);
viewLogs->setBorderColor(borderColor);
usercard->setBorderColor(kBorderColor);
viewLogs->setBorderColor(kBorderColor);
auto mod = user.emplace<Button>(this);
mod->setPixmap(app->resources->buttons.mod);
@ -474,8 +491,6 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
.setLayoutType<QHBoxLayout>()
.withoutMargin();
QColor borderColor(255, 255, 255, 80);
int buttonWidth = 40;
int buttonHeight = 32;
@ -528,7 +543,7 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
a->getLabel().setText(std::get<0>(item));
a->setScaleIndependantSize(buttonWidth, buttonHeight);
a->setBorderColor(borderColor);
a->setBorderColor(kBorderColor);
QObject::connect(a.getElement(), &EffectLabel2::leftClicked,
[this, timeout = std::get<1>(item)] {
@ -546,32 +561,14 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
std::vector<QString> durationUnits = getSettings()->timeoutDurationUnits;
addTimeouts(
"Timeouts",
{{durationsPerUnit[0] + durationUnits[0],
calculateTimeoutDuration(durationsPerUnit[0], durationUnits[0])},
{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])}});
std::vector<std::pair<QString, int>> t(8); // Timeouts.
auto i = 0;
std::generate(t.begin(), t.end(), [&] {
return std::make_pair(durationsPerUnit[i] + durationUnits[i],
calculateTimeoutDuration(durationsPerUnit[i], durationUnits[i++]));
});
addTimeouts("Timeouts", t);
addButton(Ban, "ban", getApp()->resources->buttons.ban);
}
@ -604,34 +601,4 @@ void UserInfoPopup::fillLatestMessages()
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

View file

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

View file

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

View file

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