UI: Options: TrafficGraph: Add "Fixed Speed" option

This commit is contained in:
Nodir Temirkhodjaev 2024-11-04 12:49:06 +05:00
parent 9ae935290d
commit 22eb403d46
7 changed files with 67 additions and 13 deletions

View File

@ -9,13 +9,13 @@ QT_FORWARD_DECLARE_CLASS(QHBoxLayout)
QT_FORWARD_DECLARE_CLASS(QComboBox) QT_FORWARD_DECLARE_CLASS(QComboBox)
QT_FORWARD_DECLARE_CLASS(QSpinBox) QT_FORWARD_DECLARE_CLASS(QSpinBox)
using ValuesList = QVector<int>;
class SpinCombo : public QWidget class SpinCombo : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
using ValuesList = QVector<int>;
explicit SpinCombo(QWidget *parent = nullptr); explicit SpinCombo(QWidget *parent = nullptr);
const ValuesList &values() const { return m_values; } const ValuesList &values() const { return m_values; }
@ -61,7 +61,7 @@ private:
}; };
template<size_t N> template<size_t N>
ValuesList SpinCombo::makeValuesList(const std::array<int, N> &arr) SpinCombo::ValuesList SpinCombo::makeValuesList(const std::array<int, N> &arr)
{ {
return ValuesList(arr.begin(), arr.end()); return ValuesList(arr.begin(), arr.end());
} }

View File

@ -410,11 +410,16 @@ void GraphWindow::addTraffic(qint64 unixTime, quint32 inBytes, quint32 outBytes)
m_plot->yAxis->setRange(yRange); m_plot->yAxis->setRange(yRange);
} }
const qint64 yRangeMax = iniUser()->graphWindowFixedSpeed() * 1024LL;
if (yRangeMax > 0) {
yRange.upper = yRangeMax;
m_plot->yAxis->setRange(yRange);
}
m_plot->replot(); m_plot->replot();
} }
void GraphWindow::setupByIniUser(const IniUser &ini, bool onlyFlags) { }
void GraphWindow::addEmptyTraffic() void GraphWindow::addEmptyTraffic()
{ {
addTraffic(DateUtil::getUnixTime(), 0, 0); addTraffic(DateUtil::getUnixTime(), 0, 0);

View File

@ -35,9 +35,6 @@ signals:
public slots: public slots:
void addTraffic(qint64 unixTime, quint32 inBytes, quint32 outBytes); void addTraffic(qint64 unixTime, quint32 inBytes, quint32 outBytes);
protected slots:
void setupByIniUser(const IniUser &ini, bool onlyFlags);
private slots: private slots:
void checkHoverLeave(); void checkHoverLeave();

View File

@ -40,13 +40,16 @@ const std::array speedLimitValues = { 10, 0, 20, 30, 50, 75, 100, 150, 200, 300,
qRound(1.5 * 1024), 2 * 1024, 3 * 1024, 5 * 1024, qRound(7.5 * 1024), 10 * 1024, 15 * 1024, qRound(1.5 * 1024), 2 * 1024, 3 * 1024, 5 * 1024, qRound(7.5 * 1024), 10 * 1024, 15 * 1024,
20 * 1024, 30 * 1024, 50 * 1024 }; 20 * 1024, 30 * 1024, 50 * 1024 };
CheckSpinCombo *createGroupLimit() CheckSpinCombo *createSpeedLimitCombo()
{ {
auto c = new CheckSpinCombo(); auto c = new CheckSpinCombo();
c->spinBox()->setRange(0, 999999);
c->spinBox()->setSuffix(" Kb/s");
c->setValues(speedLimitValues); c->setValues(speedLimitValues);
c->setDisabledIndex(speedLimitDisabledIndex); c->setDisabledIndex(speedLimitDisabledIndex);
auto spinBox = c->spinBox();
spinBox->setRange(0, 9999999);
spinBox->setSuffix(" Kb/s");
return c; return c;
} }
@ -456,7 +459,7 @@ void ApplicationsPage::setupGroupLog()
void ApplicationsPage::setupGroupLimitIn() void ApplicationsPage::setupGroupLimitIn()
{ {
m_cscLimitIn = createGroupLimit(); m_cscLimitIn = createSpeedLimitCombo();
connect(m_cscLimitIn->checkBox(), &QCheckBox::toggled, this, [&](bool checked) { connect(m_cscLimitIn->checkBox(), &QCheckBox::toggled, this, [&](bool checked) {
pageAppGroupSetChecked(this, &AppGroup::setLimitInEnabled, checked); pageAppGroupSetChecked(this, &AppGroup::setLimitInEnabled, checked);
@ -469,7 +472,7 @@ void ApplicationsPage::setupGroupLimitIn()
void ApplicationsPage::setupGroupLimitOut() void ApplicationsPage::setupGroupLimitOut()
{ {
m_cscLimitOut = createGroupLimit(); m_cscLimitOut = createSpeedLimitCombo();
connect(m_cscLimitOut->checkBox(), &QCheckBox::toggled, this, [&](bool checked) { connect(m_cscLimitOut->checkBox(), &QCheckBox::toggled, this, [&](bool checked) {
pageAppGroupSetChecked(this, &AppGroup::setLimitOutEnabled, checked); pageAppGroupSetChecked(this, &AppGroup::setLimitOutEnabled, checked);

View File

@ -13,8 +13,10 @@
#include <form/controls/controlutil.h> #include <form/controls/controlutil.h>
#include <form/controls/labelcolor.h> #include <form/controls/labelcolor.h>
#include <form/controls/labelspin.h> #include <form/controls/labelspin.h>
#include <form/controls/labelspincombo.h>
#include <form/opt/optionscontroller.h> #include <form/opt/optionscontroller.h>
#include <user/iniuser.h> #include <user/iniuser.h>
#include <util/formatutil.h>
#include <util/iconcache.h> #include <util/iconcache.h>
GraphPage::GraphPage(OptionsController *ctrl, QWidget *parent) : OptBasePage(ctrl, parent) GraphPage::GraphPage(OptionsController *ctrl, QWidget *parent) : OptBasePage(ctrl, parent)
@ -33,6 +35,7 @@ void GraphPage::onResetToDefault()
m_graphOpacity->spinBox()->setValue(iniUser()->graphWindowOpacityDefault()); m_graphOpacity->spinBox()->setValue(iniUser()->graphWindowOpacityDefault());
m_graphHoverOpacity->spinBox()->setValue(iniUser()->graphWindowHoverOpacityDefault()); m_graphHoverOpacity->spinBox()->setValue(iniUser()->graphWindowHoverOpacityDefault());
m_graphMaxSeconds->spinBox()->setValue(iniUser()->graphWindowMaxSecondsDefault()); m_graphMaxSeconds->spinBox()->setValue(iniUser()->graphWindowMaxSecondsDefault());
m_graphFixedSpeed->spinBox()->setValue(iniUser()->graphWindowFixedSpeedDefault());
m_comboTrafUnit->setCurrentIndex(iniUser()->graphWindowTrafUnitDefault()); m_comboTrafUnit->setCurrentIndex(iniUser()->graphWindowTrafUnitDefault());
m_graphColor->setColor(iniUser()->graphWindowColorDefault()); m_graphColor->setColor(iniUser()->graphWindowColorDefault());
@ -66,6 +69,8 @@ void GraphPage::onRetranslateUi()
m_graphOpacity->label()->setText(tr("Opacity:")); m_graphOpacity->label()->setText(tr("Opacity:"));
m_graphHoverOpacity->label()->setText(tr("Hover opacity:")); m_graphHoverOpacity->label()->setText(tr("Hover opacity:"));
m_graphMaxSeconds->label()->setText(tr("Max seconds:")); m_graphMaxSeconds->label()->setText(tr("Max seconds:"));
m_graphFixedSpeed->label()->setText(tr("Fixed speed:"));
retranslateFixedSpeedCombo();
m_traphUnits->setText(tr("Units:")); m_traphUnits->setText(tr("Units:"));
m_graphColor->label()->setText(tr("Background:")); m_graphColor->label()->setText(tr("Background:"));
@ -77,6 +82,16 @@ void GraphPage::onRetranslateUi()
m_graphGridColor->label()->setText(tr("Grid:")); m_graphGridColor->label()->setText(tr("Grid:"));
} }
void GraphPage::retranslateFixedSpeedCombo()
{
auto names = m_graphFixedSpeed->names();
names.replace(0, tr("Custom"));
names.replace(1, tr("Auto-scale"));
m_graphFixedSpeed->setNames(names);
}
void GraphPage::setupUi() void GraphPage::setupUi()
{ {
auto layout = new QVBoxLayout(); auto layout = new QVBoxLayout();
@ -151,6 +166,7 @@ void GraphPage::setupGraphBox()
layout->addWidget(m_graphOpacity); layout->addWidget(m_graphOpacity);
layout->addWidget(m_graphHoverOpacity); layout->addWidget(m_graphHoverOpacity);
layout->addWidget(m_graphMaxSeconds); layout->addWidget(m_graphMaxSeconds);
layout->addWidget(m_graphFixedSpeed);
layout->addWidget(ControlUtil::createSeparator()); layout->addWidget(ControlUtil::createSeparator());
layout->addLayout(trafUnitsLayout); layout->addLayout(trafUnitsLayout);
@ -222,6 +238,30 @@ void GraphPage::setupGraphOptions()
ctrl()->setIniUserEdited(); ctrl()->setIniUserEdited();
} }
}); });
setupGraphFixedSpeed();
}
void GraphPage::setupGraphFixedSpeed()
{
const std::array speedValues = { 100, 0, 500, 1024, 3 * 1024, 5 * 1024, 10 * 1024, 20 * 1024,
50 * 1024 };
QStringList speedNames;
for (const int kbits : speedValues) {
const auto name = FormatUtil::formatSpeed(kbits * 1024LL);
speedNames.append(name);
}
const auto speedValuesList = SpinCombo::makeValuesList(speedValues);
m_graphFixedSpeed = ControlUtil::createSpinCombo(iniUser()->graphWindowFixedSpeed(), 0, 9999999,
speedValuesList, " Kb/s", [&](int value) {
if (iniUser()->graphWindowFixedSpeed() != value) {
iniUser()->setGraphWindowFixedSpeed(value);
ctrl()->setIniUserEdited();
}
});
m_graphFixedSpeed->setNames(speedNames);
} }
QLayout *GraphPage::setupTrafUnitsLayout() QLayout *GraphPage::setupTrafUnitsLayout()

View File

@ -5,6 +5,7 @@
class LabelColor; class LabelColor;
class LabelSpin; class LabelSpin;
class LabelSpinCombo;
class GraphPage : public OptBasePage class GraphPage : public OptBasePage
{ {
@ -20,6 +21,8 @@ protected slots:
void onRetranslateUi() override; void onRetranslateUi() override;
private: private:
void retranslateFixedSpeedCombo();
void setupUi(); void setupUi();
QLayout *setupColumns(); QLayout *setupColumns();
QLayout *setupColumn1(); QLayout *setupColumn1();
@ -28,6 +31,7 @@ private:
void setupGraphBox(); void setupGraphBox();
void setupGraphCheckboxes(); void setupGraphCheckboxes();
void setupGraphOptions(); void setupGraphOptions();
void setupGraphFixedSpeed();
QLayout *setupTrafUnitsLayout(); QLayout *setupTrafUnitsLayout();
void setupColorsBox(); void setupColorsBox();
void setupGraphColors(); void setupGraphColors();
@ -46,6 +50,7 @@ private:
LabelSpin *m_graphOpacity = nullptr; LabelSpin *m_graphOpacity = nullptr;
LabelSpin *m_graphHoverOpacity = nullptr; LabelSpin *m_graphHoverOpacity = nullptr;
LabelSpin *m_graphMaxSeconds = nullptr; LabelSpin *m_graphMaxSeconds = nullptr;
LabelSpinCombo *m_graphFixedSpeed = nullptr;
QLabel *m_traphUnits = nullptr; QLabel *m_traphUnits = nullptr;
QComboBox *m_comboTrafUnit = nullptr; QComboBox *m_comboTrafUnit = nullptr;

View File

@ -265,6 +265,10 @@ public:
int graphWindowMaxSeconds() const { return valueInt("graphWindow/maxSeconds", 500); } int graphWindowMaxSeconds() const { return valueInt("graphWindow/maxSeconds", 500); }
void setGraphWindowMaxSeconds(int v) { setValue("graphWindow/maxSeconds", v); } void setGraphWindowMaxSeconds(int v) { setValue("graphWindow/maxSeconds", v); }
constexpr int graphWindowFixedSpeedDefault() const { return 0; }
int graphWindowFixedSpeed() const { return valueInt("graphWindow/fixedSpeed"); }
void setGraphWindowFixedSpeed(int v) { setValue("graphWindow/fixedSpeed", v); }
constexpr int graphWindowTrafUnitDefault() const { return 0; } constexpr int graphWindowTrafUnitDefault() const { return 0; }
int graphWindowTrafUnit() const { return valueInt("graphWindow/trafUnit", 0); } int graphWindowTrafUnit() const { return valueInt("graphWindow/trafUnit", 0); }
void setGraphWindowTrafUnit(int v) { setValue("graphWindow/trafUnit", v); } void setGraphWindowTrafUnit(int v) { setValue("graphWindow/trafUnit", v); }