1 /***************************************************************************
2               Utils.cpp  -  some commonly used utility functions
3                              -------------------
4     begin                : Sun Mar 27 2011
5     copyright            : (C) 2011 by Thomas Eschenbacher
6     email                : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <math.h>
21 #include <pthread.h>
22 #include <string.h>
23 
24 #include <QDate>
25 #include <QDateTime>
26 #include <QDir>
27 #include <QLatin1Char>
28 #include <QLocale>
29 #include <QString>
30 #include <QThread>
31 #include <QtGlobal>
32 
33 #include <KLocalizedString>
34 
35 #include "libkwave/String.h"
36 #include "libkwave/Utils.h"
37 
38 //***************************************************************************
yield()39 void Kwave::yield()
40 {
41     pthread_testcancel();
42     QThread::yieldCurrentThread();
43 }
44 
45 //***************************************************************************
zoom2string(double percent)46 QString Kwave::zoom2string(double percent)
47 {
48     QString result;
49 
50     if (percent < 1000.0) {
51 	int digits;
52 
53 	if (percent < 1.0)
54 	    digits = Kwave::toInt(ceil(1.0 - log10(percent)));
55 	else if (percent < 10.0)
56 	    digits = 1;
57 	else
58 	    digits = 0;
59 	result.setNum(percent, 'f', digits);
60 	result += _(" %");
61     } else {
62 	result.setNum(Kwave::toInt(rint(percent / 100.0)));
63 	result.prepend(_("x "));
64     }
65     return result;
66 }
67 
68 //***************************************************************************
ms2string(double ms,int precision)69 QString Kwave::ms2string(double ms, int precision)
70 {
71     QString result;
72 
73     if (ms < 1.0) {
74 	// limit precision, use 0.0 for exact zero
75 	int digits = (ms != 0.0) ? Kwave::toInt(ceil(1.0 - log10(ms))) : 1;
76 	if ( (digits < 0) || (digits > precision)) digits = precision;
77 
78 	result = _("%1 ms").arg(ms, 0, 'f', digits);
79     } else if (ms < 1000.0) {
80 	result = _("%1 ms").arg(ms, 0, 'f', 1);
81     } else {
82 	int s = Kwave::toInt(round(ms / 1000.0));
83 	int m = Kwave::toInt(floor(s / 60.0));
84 
85 	if (m < 1) {
86 	    int digits = Kwave::toInt(
87 		ceil(static_cast<double>(precision + 1) - log10(ms)));
88 	    result = _("%1 s").arg(
89 		static_cast<double>(ms) / 1000.0, 0, 'f', digits);
90 	} else {
91 	    result = _("%1:%2 min").arg(
92 		m,      2, 10, QLatin1Char('0')).arg(
93 		s % 60, 2, 10, QLatin1Char('0'));
94 	}
95     }
96 
97     return result;
98 }
99 
100 //***************************************************************************
samples2string(sample_index_t samples)101 QString Kwave::samples2string(sample_index_t samples)
102 {
103     return QLocale().toString(Kwave::toUint(samples));
104 }
105 
106 //***************************************************************************
ms2hms(double ms)107 QString Kwave::ms2hms(double ms)
108 {
109     unsigned int t, h, m, s, tms;
110     t = static_cast<unsigned int>(rint(ms * 10.0));
111     tms = t % 10000;
112     t /= 10000;
113     s = t % 60;
114     t /= 60;
115     m = t % 60;
116     t /= 60;
117     h = t;
118 
119     return i18nc(
120 	"time of label tooltip, %1=hour, %2=minute, %3=second, %4=milliseconds",
121 	"%1:%2:%3.%4",
122 	QString::asprintf("%02u", h),
123 	QString::asprintf("%02u", m),
124 	QString::asprintf("%02u", s),
125 	QString::asprintf("%04u", tms)
126     );
127 }
128 
129 //***************************************************************************
string2date(const QString & str)130 QString Kwave::string2date(const QString &str)
131 {
132     const Qt::DateFormat formats[] = {
133 	Qt::ISODate,
134 	Qt::TextDate,
135 	Qt::SystemLocaleShortDate,
136 	Qt::SystemLocaleLongDate,
137 	Qt::DefaultLocaleShortDate,
138 	Qt::DefaultLocaleLongDate
139     };
140     QString s;
141     const unsigned int fmt_count =
142 	sizeof(formats) / sizeof(formats[0]);
143     QDateTime dt;
144 
145     // try ID3 full date/time
146     dt = QDateTime::fromString(str, _("yyyy-MM-ddThh:mm:ss"));
147     if (dt.isValid())
148 	return str; // already in complete date/time format
149 
150     // type ID3 date without time
151     dt = QDateTime::fromString(str, _("yyyy-MM-dd"));
152     if (dt.isValid())
153 	return str; // already a valid date
154 
155     // try all date/time formats supported by Qt
156     for (unsigned int i = 0; i < fmt_count; i++) {
157 	Qt::DateFormat fmt = formats[i];
158 	s = QString();
159 
160 	dt = QDateTime::fromString(str, fmt);
161 
162 	if (dt.isValid()) {
163 	    // full timestamp, including time
164 	    s = dt.toString(_("yyyy-MM-ddThh:mm:ss"));
165 	}
166 	if (!s.length()) {
167 	    // date only, without time
168 	    dt = QDateTime(QDate::fromString(str), QTime(0,0));
169 	    s = dt.toString(_("yyyy-MM-dd"));
170 	}
171 
172 	if (s.length()) {
173 	    return s;
174 	}
175     }
176 
177     return QString();
178 }
179 
180 //***************************************************************************
URLfromUserInput(const QString & path)181 QUrl Kwave::URLfromUserInput(const QString &path)
182 {
183     return QUrl::fromUserInput(path, QDir::currentPath(),
184                                QUrl::AssumeLocalFile);
185 }
186 
187 //***************************************************************************
urlScheme()188 QString Kwave::urlScheme()
189 {
190     return _("kwave");
191 }
192 
193 //***************************************************************************
undoLimit()194 quint64 Kwave::undoLimit()
195 {
196     return 2048;
197 }
198 
199 //***************************************************************************
200 //***************************************************************************
201