From 22eb403d46e15a63707e516205d3ee8436d10258 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Mon, 4 Nov 2024 12:49:06 +0500 Subject: [PATCH] UI: Options: TrafficGraph: Add "Fixed Speed" option --- src/ui/form/controls/spincombo.h | 6 ++-- src/ui/form/graph/graphwindow.cpp | 9 +++-- src/ui/form/graph/graphwindow.h | 3 -- src/ui/form/opt/pages/applicationspage.cpp | 13 ++++--- src/ui/form/opt/pages/graphpage.cpp | 40 ++++++++++++++++++++++ src/ui/form/opt/pages/graphpage.h | 5 +++ src/ui/user/iniuser.h | 4 +++ 7 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/ui/form/controls/spincombo.h b/src/ui/form/controls/spincombo.h index e37b6de9..3d307683 100644 --- a/src/ui/form/controls/spincombo.h +++ b/src/ui/form/controls/spincombo.h @@ -9,13 +9,13 @@ QT_FORWARD_DECLARE_CLASS(QHBoxLayout) QT_FORWARD_DECLARE_CLASS(QComboBox) QT_FORWARD_DECLARE_CLASS(QSpinBox) -using ValuesList = QVector; - class SpinCombo : public QWidget { Q_OBJECT public: + using ValuesList = QVector; + explicit SpinCombo(QWidget *parent = nullptr); const ValuesList &values() const { return m_values; } @@ -61,7 +61,7 @@ private: }; template -ValuesList SpinCombo::makeValuesList(const std::array &arr) +SpinCombo::ValuesList SpinCombo::makeValuesList(const std::array &arr) { return ValuesList(arr.begin(), arr.end()); } diff --git a/src/ui/form/graph/graphwindow.cpp b/src/ui/form/graph/graphwindow.cpp index 7708d79e..0f2c32e8 100644 --- a/src/ui/form/graph/graphwindow.cpp +++ b/src/ui/form/graph/graphwindow.cpp @@ -410,11 +410,16 @@ void GraphWindow::addTraffic(qint64 unixTime, quint32 inBytes, quint32 outBytes) 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(); } -void GraphWindow::setupByIniUser(const IniUser &ini, bool onlyFlags) { } - void GraphWindow::addEmptyTraffic() { addTraffic(DateUtil::getUnixTime(), 0, 0); diff --git a/src/ui/form/graph/graphwindow.h b/src/ui/form/graph/graphwindow.h index 3ae02138..d0d063c2 100644 --- a/src/ui/form/graph/graphwindow.h +++ b/src/ui/form/graph/graphwindow.h @@ -35,9 +35,6 @@ signals: public slots: void addTraffic(qint64 unixTime, quint32 inBytes, quint32 outBytes); -protected slots: - void setupByIniUser(const IniUser &ini, bool onlyFlags); - private slots: void checkHoverLeave(); diff --git a/src/ui/form/opt/pages/applicationspage.cpp b/src/ui/form/opt/pages/applicationspage.cpp index d8ede921..90a57d88 100644 --- a/src/ui/form/opt/pages/applicationspage.cpp +++ b/src/ui/form/opt/pages/applicationspage.cpp @@ -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, 20 * 1024, 30 * 1024, 50 * 1024 }; -CheckSpinCombo *createGroupLimit() +CheckSpinCombo *createSpeedLimitCombo() { auto c = new CheckSpinCombo(); - c->spinBox()->setRange(0, 999999); - c->spinBox()->setSuffix(" Kb/s"); c->setValues(speedLimitValues); c->setDisabledIndex(speedLimitDisabledIndex); + + auto spinBox = c->spinBox(); + spinBox->setRange(0, 9999999); + spinBox->setSuffix(" Kb/s"); + return c; } @@ -456,7 +459,7 @@ void ApplicationsPage::setupGroupLog() void ApplicationsPage::setupGroupLimitIn() { - m_cscLimitIn = createGroupLimit(); + m_cscLimitIn = createSpeedLimitCombo(); connect(m_cscLimitIn->checkBox(), &QCheckBox::toggled, this, [&](bool checked) { pageAppGroupSetChecked(this, &AppGroup::setLimitInEnabled, checked); @@ -469,7 +472,7 @@ void ApplicationsPage::setupGroupLimitIn() void ApplicationsPage::setupGroupLimitOut() { - m_cscLimitOut = createGroupLimit(); + m_cscLimitOut = createSpeedLimitCombo(); connect(m_cscLimitOut->checkBox(), &QCheckBox::toggled, this, [&](bool checked) { pageAppGroupSetChecked(this, &AppGroup::setLimitOutEnabled, checked); diff --git a/src/ui/form/opt/pages/graphpage.cpp b/src/ui/form/opt/pages/graphpage.cpp index c3d4233d..9db4afab 100644 --- a/src/ui/form/opt/pages/graphpage.cpp +++ b/src/ui/form/opt/pages/graphpage.cpp @@ -13,8 +13,10 @@ #include
#include #include +#include #include #include +#include #include GraphPage::GraphPage(OptionsController *ctrl, QWidget *parent) : OptBasePage(ctrl, parent) @@ -33,6 +35,7 @@ void GraphPage::onResetToDefault() m_graphOpacity->spinBox()->setValue(iniUser()->graphWindowOpacityDefault()); m_graphHoverOpacity->spinBox()->setValue(iniUser()->graphWindowHoverOpacityDefault()); m_graphMaxSeconds->spinBox()->setValue(iniUser()->graphWindowMaxSecondsDefault()); + m_graphFixedSpeed->spinBox()->setValue(iniUser()->graphWindowFixedSpeedDefault()); m_comboTrafUnit->setCurrentIndex(iniUser()->graphWindowTrafUnitDefault()); m_graphColor->setColor(iniUser()->graphWindowColorDefault()); @@ -66,6 +69,8 @@ void GraphPage::onRetranslateUi() m_graphOpacity->label()->setText(tr("Opacity:")); m_graphHoverOpacity->label()->setText(tr("Hover opacity:")); m_graphMaxSeconds->label()->setText(tr("Max seconds:")); + m_graphFixedSpeed->label()->setText(tr("Fixed speed:")); + retranslateFixedSpeedCombo(); m_traphUnits->setText(tr("Units:")); m_graphColor->label()->setText(tr("Background:")); @@ -77,6 +82,16 @@ void GraphPage::onRetranslateUi() 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() { auto layout = new QVBoxLayout(); @@ -151,6 +166,7 @@ void GraphPage::setupGraphBox() layout->addWidget(m_graphOpacity); layout->addWidget(m_graphHoverOpacity); layout->addWidget(m_graphMaxSeconds); + layout->addWidget(m_graphFixedSpeed); layout->addWidget(ControlUtil::createSeparator()); layout->addLayout(trafUnitsLayout); @@ -222,6 +238,30 @@ void GraphPage::setupGraphOptions() 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() diff --git a/src/ui/form/opt/pages/graphpage.h b/src/ui/form/opt/pages/graphpage.h index 6ff65c97..2add4287 100644 --- a/src/ui/form/opt/pages/graphpage.h +++ b/src/ui/form/opt/pages/graphpage.h @@ -5,6 +5,7 @@ class LabelColor; class LabelSpin; +class LabelSpinCombo; class GraphPage : public OptBasePage { @@ -20,6 +21,8 @@ protected slots: void onRetranslateUi() override; private: + void retranslateFixedSpeedCombo(); + void setupUi(); QLayout *setupColumns(); QLayout *setupColumn1(); @@ -28,6 +31,7 @@ private: void setupGraphBox(); void setupGraphCheckboxes(); void setupGraphOptions(); + void setupGraphFixedSpeed(); QLayout *setupTrafUnitsLayout(); void setupColorsBox(); void setupGraphColors(); @@ -46,6 +50,7 @@ private: LabelSpin *m_graphOpacity = nullptr; LabelSpin *m_graphHoverOpacity = nullptr; LabelSpin *m_graphMaxSeconds = nullptr; + LabelSpinCombo *m_graphFixedSpeed = nullptr; QLabel *m_traphUnits = nullptr; QComboBox *m_comboTrafUnit = nullptr; diff --git a/src/ui/user/iniuser.h b/src/ui/user/iniuser.h index 21809266..e0a14239 100644 --- a/src/ui/user/iniuser.h +++ b/src/ui/user/iniuser.h @@ -265,6 +265,10 @@ public: int graphWindowMaxSeconds() const { return valueInt("graphWindow/maxSeconds", 500); } 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; } int graphWindowTrafUnit() const { return valueInt("graphWindow/trafUnit", 0); } void setGraphWindowTrafUnit(int v) { setValue("graphWindow/trafUnit", v); }