1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include "interval.hh"
#include <QRegularExpression>
QString
Interval::format(Format f) const {
if (isNull())
return "0";
if (isInfinite())
return "infinite";
switch (f) {
case Format::Automatic:
if (0 == _duration % 60000UL)
return format(Format::Minutes);
else if (0 == _duration % 1000UL)
return format(Format::Seconds);
return format(Format::Milliseconds);
case Format::Minutes:
return QString("%1 min").arg(_duration/60000UL);
case Format::Seconds:
return QString("%1 s").arg(_duration/1000UL);
case Format::Milliseconds:
return QString("%1 ms").arg(_duration);
}
return QString("%1 ms").arg(_duration);
}
bool
Interval::parse(const QString &value) {
if (value.toLower().startsWith("inf")) {
_duration = std::numeric_limits<unsigned long long>::max();
return true;
}
QRegularExpression ex(R"(\s*([0-9]+)\s*(min|s|ms|)\s*)");
QRegularExpressionMatch match = ex.match(value);
if (! match.hasMatch())
return false;
bool hasUnit = match.capturedLength(2);
QString unit = match.captured(2);
QString dur = match.captured(1);
if (hasUnit && ("s"==unit))
_duration = dur.toULongLong()*1000ULL;
else if (hasUnit && ("min"==unit))
_duration = dur.toULongLong()*60000ULL;
else
_duration = dur.toULongLong();
return true;
}
|