Allow k/m units as input to port pps/bps

Also,
  * Revert to old value if new input is not valid
  * Refactored auto calc and display of rate to XLocale
This commit is contained in:
Srivats P 2021-11-11 21:38:57 +05:30
parent 910fccbfc6
commit f731b48676
2 changed files with 86 additions and 15 deletions

View File

@ -122,9 +122,13 @@ void PortWidget::on_averagePacketsPerSec_editingFinished()
Q_ASSERT(plm->isPort(currentPortIndex_));
bool isOk;
double pps = XLocale().toDouble(averagePacketsPerSec->text(), &isOk);
double pps = XLocale().toPacketsPerSecond(averagePacketsPerSec->text(),
&isOk);
plm->port(currentPortIndex_).setAveragePacketRate(pps);
if (isOk)
plm->port(currentPortIndex_).setAveragePacketRate(pps);
else
updatePortRates();
}
void PortWidget::on_averageBitsPerSec_editingFinished()
@ -132,9 +136,12 @@ void PortWidget::on_averageBitsPerSec_editingFinished()
Q_ASSERT(plm->isPort(currentPortIndex_));
bool isOk;
double bps = XLocale().toDouble(averageBitsPerSec->text(), &isOk);
double bps = XLocale().toBitsPerSecond(averageBitsPerSec->text(), &isOk);
plm->port(currentPortIndex_).setAverageBitRate(bps);
if (isOk)
plm->port(currentPortIndex_).setAverageBitRate(bps);
else
updatePortRates();
}
void PortWidget::updatePortRates()
@ -147,7 +154,7 @@ void PortWidget::updatePortRates()
// XXX: pps/bps input widget is a LineEdit and not a SpinBox
// because we want users to be able to enter values in various
// units e.g. 1.5 Mbps, 1000K, 50 etc.
// units e.g. "1.5 Mbps", "1000K", "50" (bps) etc.
// XXX: It's a considered decision NOT to show frame rate in
// higher units of Kpps and Mpps as most users may not be
@ -158,15 +165,8 @@ void PortWidget::updatePortRates()
averagePacketsPerSec->setText(QString("%L1 pps")
.arg(plm->port(currentPortIndex_).averagePacketRate(), 0, 'f', 4));
double bps = plm->port(currentPortIndex_).averageBitRate();
if (bps > 1e9)
averageBitsPerSec->setText(tr("%L1 Gbps").arg(bps/1e9, 0, 'f', 4));
else if (bps > 1e6)
averageBitsPerSec->setText(tr("%L1 Mbps").arg(bps/1e6, 0, 'f', 4));
else if (bps > 1e3)
averageBitsPerSec->setText(tr("%L1 Kbps").arg(bps/1e3, 0, 'f', 4));
else
averageBitsPerSec->setText(tr("%L1 bps").arg(bps, 0, 'f', 4));
averageBitsPerSec->setText(XLocale().toBitRateString(
plm->port(currentPortIndex_).averageBitRate()));
averageLoadPercent->setValue(
plm->port(currentPortIndex_).averageLoadRate()*100);

View File

@ -21,14 +21,85 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#define _X_LOCALE_H
#include <QLocale>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
class XLocale: public QLocale
{
public:
double toDouble(const QString &s, bool *ok = Q_NULLPTR) const {
double toDouble(const QString &s, bool *ok = Q_NULLPTR) const
{
QString s2 = s;
return QLocale::toDouble(s2.remove(groupSeparator()), ok);
}
double toPacketsPerSecond(const QString &s, bool *ok = Q_NULLPTR) const
{
QString text = s;
double multiplier = 0;
QRegularExpression regex("[a-zA-Z/]+$");
QRegularExpressionMatch match = regex.match(text);
if (match.hasMatch()) {
QString unit = match.captured(0).toCaseFolded();
if ((unit == "mpps") || (unit == "m"))
multiplier = 1e6;
else if ((unit == "kpps") || (unit == "k"))
multiplier = 1e3;
else if (unit == "pps")
multiplier = 1;
if (multiplier)
text.remove(regex);
}
if (multiplier == 0)
multiplier = 1;
return toDouble(text, ok) * multiplier;
}
double toBitsPerSecond(const QString &s, bool *ok = Q_NULLPTR) const
{
QString text = s;
double multiplier = 0;
QRegularExpression regex("[a-zA-Z/]+$");
QRegularExpressionMatch match = regex.match(text);
if (match.hasMatch()) {
QString unit = match.captured(0).toCaseFolded();
if ((unit == "gbps") || (unit == "gb/s") || (unit == "g"))
multiplier = 1e9;
else if ((unit == "mbps") || (unit == "mb/s") || (unit == "m"))
multiplier = 1e6;
else if ((unit == "kbps") || (unit == "kb/s") || (unit == "k"))
multiplier = 1e3;
else if ((unit == "bps") || (unit == "b/s"))
multiplier = 1;
if (multiplier)
text.remove(regex);
}
if (multiplier == 0)
multiplier = 1;
return toDouble(text, ok) * multiplier;
}
QString toBitRateString(double bps) const
{
QString text;
if (bps >= 1e9)
return QObject::tr("%L1 Gbps").arg(bps/1e9, 0, 'f', 4);
if (bps >= 1e6)
return QObject::tr("%L1 Mbps").arg(bps/1e6, 0, 'f', 4);
if (bps >= 1e3)
return QObject::tr("%L1 Kbps").arg(bps/1e3, 0, 'f', 4);
return QObject::tr("%L1 bps").arg(bps, 0, 'f', 4);
}
};
#endif