File indexing completed on 2024-05-12 16:36:43
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2010 Thorsten Zachmann <zachmann@kde.org> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Library General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2 of the License, or ( at your option ) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Library General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KPrDurationParser.h" 0021 0022 #include <QRegExp> 0023 #include <QStringList> 0024 #include <QGlobalStatic> 0025 0026 // define the regex used as statics 0027 Q_GLOBAL_STATIC_WITH_ARGS(QRegExp, clockRegEx, ("^(?:(\\d+):)?(\\d{2}):(\\d{2}(?:\\.\\d+)?)$")) 0028 Q_GLOBAL_STATIC_WITH_ARGS(QRegExp, timecountRegEx, ("^(\\d+(?:\\.\\d+)?)(h|min|s|ms)?$")) 0029 0030 int KPrDurationParser::durationMs(const QString & duration) 0031 { 0032 int ms = -1; 0033 if (clockRegEx->indexIn(duration) != -1) { 0034 QStringList result(clockRegEx->capturedTexts()); 0035 bool ok = true; 0036 ms = qreal(result[1].toInt(&ok, 10) * 3600 + result[2].toInt(&ok, 10) * 60 + result[3].toDouble(&ok)) * 1000; 0037 } 0038 else if (timecountRegEx->indexIn(duration) != -1) { 0039 QStringList result(timecountRegEx->capturedTexts()); 0040 QString metric(result[2]); 0041 // the default (no metric) is s 0042 qreal factor = 1000; 0043 if (metric != "s") { 0044 if (metric == "min") { 0045 factor = 60 * 1000; 0046 } 0047 else if (metric == "ms") { 0048 factor = 1; 0049 } 0050 else if (metric == "h") { 0051 factor = 3600 * 1000; 0052 } 0053 } 0054 bool ok = true; 0055 ms = qRound(qreal(result[1].toDouble(&ok) * factor)); 0056 } 0057 return ms; 0058 } 0059 0060 QString KPrDurationParser::msToString(const int ms) 0061 { 0062 QString result; 0063 if (ms%1000 == 0) { 0064 result = QString("%1s").arg(ms/1000); 0065 } else { 0066 result = QString("%1ms").arg(ms); 0067 } 0068 return result; 0069 }