UI: Options: Traffic Graph: Add "Units" option

This commit is contained in:
Nodir Temirkhodjaev 2024-08-26 13:15:19 +05:00
parent 42c420a975
commit 4e0fe5029b
13 changed files with 135 additions and 38 deletions

View File

@ -171,12 +171,12 @@ QString AppGroup::menuLabel() const
if (enabledSpeedLimitIn() != 0) { if (enabledSpeedLimitIn() != 0) {
text += QLatin1Char(' ') + QChar(0x2193) // ↓ text += QLatin1Char(' ') + QChar(0x2193) // ↓
+ FormatUtil::formatSpeed(speedLimitIn() * 1024); + FormatUtil::formatSpeed(speedLimitIn() * 1024LL);
} }
if (enabledSpeedLimitOut() != 0) { if (enabledSpeedLimitOut() != 0) {
text += QLatin1Char(' ') + QChar(0x2191) // ↑ text += QLatin1Char(' ') + QChar(0x2191) // ↑
+ FormatUtil::formatSpeed(speedLimitOut() * 1024); + FormatUtil::formatSpeed(speedLimitOut() * 1024LL);
} }
if (periodEnabled()) { if (periodEnabled()) {

View File

@ -124,6 +124,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 graphWindowTrafUnitDefault() const { return 0; }
int graphWindowTrafUnit() const { return valueInt("graphWindow/trafUnit", 0); }
void setGraphWindowTrafUnit(int v) { setValue("graphWindow/trafUnit", v); }
constexpr QColor graphWindowColorDefault() const { return QColor(255, 255, 255); } constexpr QColor graphWindowColorDefault() const { return QColor(255, 255, 255); }
QColor graphWindowColor() const QColor graphWindowColor() const
{ {

View File

@ -17,5 +17,5 @@ QString AxisTickerSpeed::getTickLabel(
Q_UNUSED(formatChar); Q_UNUSED(formatChar);
Q_UNUSED(precision); Q_UNUSED(precision);
return FormatUtil::formatSpeed(quint32(tick)); return FormatUtil::formatSpeed(qint64(tick), unitFormat());
} }

View File

@ -3,6 +3,8 @@
#include <qcustomplot.h> #include <qcustomplot.h>
#include <util/formatutil.h>
class AxisTickerSpeed : public QCPAxisTicker class AxisTickerSpeed : public QCPAxisTicker
{ {
Q_GADGET Q_GADGET
@ -10,10 +12,16 @@ class AxisTickerSpeed : public QCPAxisTicker
public: public:
explicit AxisTickerSpeed() = default; explicit AxisTickerSpeed() = default;
FormatUtil::SizeFormat unitFormat() const { return m_unitFormat; }
void setUnitFormat(FormatUtil::SizeFormat v) { m_unitFormat = v; }
protected: protected:
double getTickStep(const QCPRange &range) override; double getTickStep(const QCPRange &range) override;
QString getTickLabel( QString getTickLabel(
double tick, const QLocale &locale, QChar formatChar, int precision) override; double tick, const QLocale &locale, QChar formatChar, int precision) override;
private:
FormatUtil::SizeFormat m_unitFormat = FormatUtil::SpeedTraditionalFormat;
}; };
#endif // AXISTICKERSPEED_H #endif // AXISTICKERSPEED_H

View File

@ -8,7 +8,6 @@
#include <conf/firewallconf.h> #include <conf/firewallconf.h>
#include <user/iniuser.h> #include <user/iniuser.h>
#include <util/dateutil.h> #include <util/dateutil.h>
#include <util/formatutil.h>
#include <util/guiutil.h> #include <util/guiutil.h>
#include <util/ioc/ioccontainer.h> #include <util/ioc/ioccontainer.h>
#include <util/window/widgetwindowstatewatcher.h> #include <util/window/widgetwindowstatewatcher.h>
@ -161,8 +160,8 @@ void GraphWindow::setupUi()
axisRect->setMinimumMargins(QMargins(1, 2, 1, 1)); axisRect->setMinimumMargins(QMargins(1, 2, 1, 1));
// Axis Ticker // Axis Ticker
QSharedPointer<AxisTickerSpeed> ticker(new AxisTickerSpeed()); m_ticker.reset(new AxisTickerSpeed());
yAxis->setTicker(ticker); yAxis->setTicker(m_ticker);
// Graph Inbound // Graph Inbound
m_graphIn = new QCPBars(m_plot->xAxis, m_plot->yAxis); m_graphIn = new QCPBars(m_plot->xAxis, m_plot->yAxis);
@ -194,6 +193,7 @@ void GraphWindow::setupFlagsAndColors()
const auto updateFlagsAndColors = [&] { const auto updateFlagsAndColors = [&] {
updateWindowFlags(); updateWindowFlags();
updateColors(); updateColors();
updateFormat();
}; };
updateFlagsAndColors(); updateFlagsAndColors();
@ -241,6 +241,13 @@ void GraphWindow::updateColors()
m_graphOut->setPen(QPen(ini()->graphWindowColorOut())); m_graphOut->setPen(QPen(ini()->graphWindowColorOut()));
} }
void GraphWindow::updateFormat()
{
m_unitFormat = FormatUtil::graphUnitFormat(ini()->graphWindowTrafUnit());
m_ticker->setUnitFormat(m_unitFormat);
}
void GraphWindow::setupTimer() void GraphWindow::setupTimer()
{ {
connect(&m_hoverTimer, &QTimer::timeout, this, &GraphWindow::checkHoverLeave); connect(&m_hoverTimer, &QTimer::timeout, this, &GraphWindow::checkHoverLeave);
@ -422,8 +429,8 @@ void GraphWindow::updateWindowTitleSpeed()
m_graphOut->data()->isEmpty() ? 0 : (m_graphOut->data()->constEnd() - 1)->mainValue(); m_graphOut->data()->isEmpty() ? 0 : (m_graphOut->data()->constEnd() - 1)->mainValue();
setWindowTitle(QChar(0x2193) // ↓ setWindowTitle(QChar(0x2193) // ↓
+ FormatUtil::formatSpeed(quint32(inBits)) + ' ' + QChar(0x2191) // ↑ + FormatUtil::formatSpeed(quint64(inBits), m_unitFormat) + ' ' + QChar(0x2191) // ↑
+ FormatUtil::formatSpeed(quint32(outBits))); + FormatUtil::formatSpeed(quint64(outBits), m_unitFormat));
} }
void GraphWindow::setWindowOpacityPercent(int percent) void GraphWindow::setWindowOpacityPercent(int percent)

View File

@ -4,6 +4,9 @@
#include <QTimer> #include <QTimer>
#include <form/controls/formwindow.h> #include <form/controls/formwindow.h>
#include <util/formatutil.h>
class AxisTickerSpeed;
class ConfManager; class ConfManager;
class FirewallConf; class FirewallConf;
@ -55,6 +58,7 @@ private:
void setupFlagsAndColors(); void setupFlagsAndColors();
void updateWindowFlags(); void updateWindowFlags();
void updateColors(); void updateColors();
void updateFormat();
void setupTimer(); void setupTimer();
@ -76,9 +80,12 @@ protected:
private: private:
bool m_mouseDragResize = false; bool m_mouseDragResize = false;
FormatUtil::SizeFormat m_unitFormat = FormatUtil::SpeedTraditionalFormat;
qint64 m_lastUnixTime = 0; qint64 m_lastUnixTime = 0;
GraphPlot *m_plot = nullptr; GraphPlot *m_plot = nullptr;
QSharedPointer<AxisTickerSpeed> m_ticker;
QCPBars *m_graphIn = nullptr; QCPBars *m_graphIn = nullptr;
QCPBars *m_graphOut = nullptr; QCPBars *m_graphOut = nullptr;

View File

@ -52,7 +52,7 @@ CheckSpinCombo *createGroupLimit()
QString formatSpeed(int kbits) QString formatSpeed(int kbits)
{ {
return FormatUtil::formatSpeed(quint32(kbits * 1024)); return FormatUtil::formatSpeed(kbits * 1024LL);
} }
void pageAppGroupCheckEdited(ApplicationsPage *page, AppGroup *appGroup, bool isFlag = false) void pageAppGroupCheckEdited(ApplicationsPage *page, AppGroup *appGroup, bool isFlag = false)

View File

@ -1,6 +1,7 @@
#include "graphpage.h" #include "graphpage.h"
#include <QCheckBox> #include <QCheckBox>
#include <QComboBox>
#include <QGroupBox> #include <QGroupBox>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
@ -32,6 +33,7 @@ void GraphPage::onResetToDefault()
m_graphOpacity->spinBox()->setValue(ini()->graphWindowOpacityDefault()); m_graphOpacity->spinBox()->setValue(ini()->graphWindowOpacityDefault());
m_graphHoverOpacity->spinBox()->setValue(ini()->graphWindowHoverOpacityDefault()); m_graphHoverOpacity->spinBox()->setValue(ini()->graphWindowHoverOpacityDefault());
m_graphMaxSeconds->spinBox()->setValue(ini()->graphWindowMaxSecondsDefault()); m_graphMaxSeconds->spinBox()->setValue(ini()->graphWindowMaxSecondsDefault());
m_comboTrafUnit->setCurrentIndex(ini()->graphWindowTrafUnitDefault());
m_graphColor->setColor(ini()->graphWindowColorDefault()); m_graphColor->setColor(ini()->graphWindowColorDefault());
m_graphColorIn->setColor(ini()->graphWindowColorInDefault()); m_graphColorIn->setColor(ini()->graphWindowColorInDefault());
@ -56,6 +58,7 @@ 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_traphUnits->setText(tr("Units:"));
m_graphColor->label()->setText(tr("Background:")); m_graphColor->label()->setText(tr("Background:"));
m_graphColorIn->label()->setText(tr("Download:")); m_graphColorIn->label()->setText(tr("Download:"));
@ -97,13 +100,25 @@ QLayout *GraphPage::setupColumns()
QLayout *GraphPage::setupColumn1() QLayout *GraphPage::setupColumn1()
{ {
auto layout = new QVBoxLayout();
layout->setSpacing(10);
// Graph Group Box // Graph Group Box
setupGraphBox(); setupGraphBox();
layout->addWidget(m_gbGraph);
auto layout = new QVBoxLayout();
layout->setSpacing(10);
layout->addWidget(m_gbGraph);
layout->addStretch();
return layout;
}
QLayout *GraphPage::setupColumn2()
{
// Graph Colors Group Box
setupColorsBox();
auto layout = new QVBoxLayout();
layout->setSpacing(10);
layout->addWidget(m_gbColors);
layout->addStretch(); layout->addStretch();
return layout; return layout;
@ -114,10 +129,22 @@ void GraphPage::setupGraphBox()
setupGraphCheckboxes(); setupGraphCheckboxes();
setupGraphOptions(); setupGraphOptions();
auto layout = ControlUtil::createVLayoutByWidgets({ m_cbGraphHideOnClose, // Traffic Units
ControlUtil::createSeparator(), m_cbGraphAlwaysOnTop, m_cbGraphFrameless, auto trafUnitsLayout = setupTrafUnitsLayout();
m_cbGraphClickThrough, m_cbGraphHideOnHover, ControlUtil::createSeparator(),
m_graphOpacity, m_graphHoverOpacity, m_graphMaxSeconds }); auto layout = new QVBoxLayout();
layout->addWidget(m_cbGraphHideOnClose);
layout->addWidget(ControlUtil::createSeparator());
layout->addWidget(m_cbGraphAlwaysOnTop);
layout->addWidget(m_cbGraphFrameless);
layout->addWidget(m_cbGraphClickThrough);
layout->addWidget(m_cbGraphHideOnHover);
layout->addWidget(ControlUtil::createSeparator());
layout->addWidget(m_graphOpacity);
layout->addWidget(m_graphHoverOpacity);
layout->addWidget(m_graphMaxSeconds);
layout->addWidget(ControlUtil::createSeparator());
layout->addLayout(trafUnitsLayout);
m_gbGraph = new QGroupBox(); m_gbGraph = new QGroupBox();
m_gbGraph->setLayout(layout); m_gbGraph->setLayout(layout);
@ -188,18 +215,23 @@ void GraphPage::setupGraphOptions()
}); });
} }
QLayout *GraphPage::setupColumn2() QLayout *GraphPage::setupTrafUnitsLayout()
{ {
auto layout = new QVBoxLayout(); const QStringList list = { "b/s", "B/s", "ib/s", "iB/s" };
layout->setSpacing(10);
// Graph Colors Group Box m_traphUnits = ControlUtil::createLabel();
setupColorsBox();
layout->addWidget(m_gbColors);
layout->addStretch(); m_comboTrafUnit = ControlUtil::createComboBox(list, [&](int index) {
if (ini()->graphWindowTrafUnit() != index) {
ini()->setGraphWindowTrafUnit(index);
ctrl()->setIniEdited();
}
});
m_comboTrafUnit->setFixedWidth(110);
return layout; m_comboTrafUnit->setCurrentIndex(ini()->graphWindowTrafUnit());
return ControlUtil::createRowLayout(m_traphUnits, m_comboTrafUnit);
} }
void GraphPage::setupColorsBox() void GraphPage::setupColorsBox()

View File

@ -23,10 +23,12 @@ private:
void setupUi(); void setupUi();
QLayout *setupColumns(); QLayout *setupColumns();
QLayout *setupColumn1(); QLayout *setupColumn1();
QLayout *setupColumn2();
void setupGraphBox(); void setupGraphBox();
void setupGraphCheckboxes(); void setupGraphCheckboxes();
void setupGraphOptions(); void setupGraphOptions();
QLayout *setupColumn2(); QLayout *setupTrafUnitsLayout();
void setupColorsBox(); void setupColorsBox();
void setupGraphColors(); void setupGraphColors();
@ -42,6 +44,8 @@ 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;
QLabel *m_traphUnits = nullptr;
QComboBox *m_comboTrafUnit = nullptr;
LabelColor *m_graphColor = nullptr; LabelColor *m_graphColor = nullptr;
LabelColor *m_graphColorIn = nullptr; LabelColor *m_graphColorIn = nullptr;

View File

@ -144,8 +144,7 @@ void IfacePage::retranslateComboTheme()
ControlUtil::setComboBoxTexts(m_comboTheme, list); ControlUtil::setComboBoxTexts(m_comboTheme, list);
const auto colorScheme = IniUser::colorSchemeByName(iniUser()->theme()); updateTheme();
m_comboTheme->setCurrentIndex(colorScheme);
} }
void IfacePage::retranslateComboHotKey() void IfacePage::retranslateComboHotKey()
@ -558,3 +557,9 @@ void IfacePage::setupConfirmationsBox()
m_gbConfirmations = new QGroupBox(); m_gbConfirmations = new QGroupBox();
m_gbConfirmations->setLayout(layout); m_gbConfirmations->setLayout(layout);
} }
void IfacePage::updateTheme()
{
const auto colorScheme = IniUser::colorSchemeByName(iniUser()->theme());
m_comboTheme->setCurrentIndex(colorScheme);
}

View File

@ -56,6 +56,8 @@ private:
QLayout *setupTrayActionLayout(); QLayout *setupTrayActionLayout();
void setupConfirmationsBox(); void setupConfirmationsBox();
void updateTheme();
private: private:
bool m_explorerEdited : 1 = false; bool m_explorerEdited : 1 = false;
bool m_languageEdited : 1 = false; bool m_languageEdited : 1 = false;

View File

@ -25,7 +25,7 @@ int FormatUtil::getPower(qint64 value, SizeFormat format)
} }
if (isBase1000(format)) { if (isBase1000(format)) {
return int(qLn(qAbs(value)) / 3); return int(std::log10(qAbs(value)) / 3);
} }
// Compute log2(value) / 10 // Compute log2(value) / 10
@ -42,8 +42,8 @@ QString FormatUtil::formatSize(qint64 value, int power, int precision, SizeForma
return QLocale().toString(value); return QLocale().toString(value);
} }
const int base = isBase1000(format) ? 1000 : 1024; const qreal base = isBase1000(format) ? 1000 : 1024;
const qreal powerValue = qPow(qreal(base), power); const qreal powerValue = qPow(base, power);
return QLocale().toString(value / powerValue, 'f', precision); return QLocale().toString(value / powerValue, 'f', precision);
} }
@ -72,10 +72,32 @@ QString FormatUtil::formatDataSize(qint64 bytes, int precision, SizeFormat forma
return sizeStr + ' ' + unitStr; return sizeStr + ' ' + unitStr;
} }
QString FormatUtil::formatSpeed(quint32 bitsPerSecond) QString FormatUtil::formatSpeed(qint64 bitsPerSecond, SizeFormat format)
{ {
const auto format = SizeFormat(SizeTraditionalFormat | SizeBits); if (!isBits(format)) {
bitsPerSecond /= 8;
}
const auto text = formatDataSize(bitsPerSecond, /*precision=*/0, format); const auto text = formatDataSize(bitsPerSecond, /*precision=*/0, format);
return text + "/s"; return text + "/s";
} }
QStringList FormatUtil::graphUnitNames()
{
static const QStringList list = { "b/s", "B/s", "ib/s", "iB/s" };
return list;
}
FormatUtil::SizeFormat FormatUtil::graphUnitFormat(int index)
{
static const SizeFormat list[] = { SpeedTraditionalFormat, SizeTraditionalFormat,
SpeedIecFormat, SizeIecFormat };
if (index < 0 || index >= std::size(list)) {
index = 0;
}
return list[index];
}

View File

@ -9,7 +9,7 @@ class FormatUtil : public QObject
Q_OBJECT Q_OBJECT
public: public:
enum SizeFormat { enum SizeFormat : quint8 {
SizeBase1000 = 0x01, // use factors of 1000 SizeBase1000 = 0x01, // use factors of 1000
SizeBase1024 = 0x02, // use SI quantifiers SizeBase1024 = 0x02, // use SI quantifiers
@ -18,9 +18,12 @@ public:
SizeBits = 0x10, // b, Kb, Mb, Gb, ... SizeBits = 0x10, // b, Kb, Mb, Gb, ...
SizeIecFormat = (SizeBase1024 | SizeFormatIec), // base 1024, KiB, MiB, GiB, ... SizeIecFormat = (SizeBase1000 | SizeFormatIec), // base 1000, KiB, MiB, GiB, ...
SizeTraditionalFormat = (SizeBase1024 | SizeFormatSi), // base 1024, kB, MB, GB, ... SizeTraditionalFormat = (SizeBase1024 | SizeFormatSi), // base 1024, KB, MB, GB, ...
SizeSIFormat = (SizeBase1000 | SizeFormatSi) // base 1000, kB, MB, GB, ... SizeSIFormat = (SizeBase1000 | SizeFormatSi), // base 1000, KB, MB, GB, ...
SpeedTraditionalFormat = (SizeTraditionalFormat | SizeBits), // Kb/s, Mb/s, ...
SpeedIecFormat = (SizeIecFormat | SizeBits), // Kib/s, Mib/s, ...
}; };
static int getPower(qint64 value, SizeFormat format = SizeTraditionalFormat); static int getPower(qint64 value, SizeFormat format = SizeTraditionalFormat);
@ -33,7 +36,10 @@ public:
static QString formatDataSize( static QString formatDataSize(
qint64 bytes, int precision = 2, SizeFormat format = SizeTraditionalFormat); qint64 bytes, int precision = 2, SizeFormat format = SizeTraditionalFormat);
static QString formatSpeed(quint32 bitsPerSecond); static QString formatSpeed(qint64 bitsPerSecond, SizeFormat format = SpeedTraditionalFormat);
static QStringList graphUnitNames();
static FormatUtil::SizeFormat graphUnitFormat(int index);
}; };
#endif // FORMATUTIL_H #endif // FORMATUTIL_H