kwave  18.07.70
Utils.cpp
Go to the documentation of this file.
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 <stdio.h>
23 #include <string.h>
24 
25 #include <QDate>
26 #include <QDateTime>
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 //***************************************************************************
40 {
41  pthread_testcancel();
42  QThread::yieldCurrentThread();
43 }
44 
45 //***************************************************************************
46 QString Kwave::zoom2string(double percent)
47 {
48  QString result;
49 
50  if (percent < 1.0) {
51  int digits = Kwave::toInt(ceil(1.0 - log10(percent)));
52  QString format = _("%0.") + QString::number(digits) + _("f %%");
53  result = format.sprintf(UTF8(format), percent);
54  } else if (percent < 10.0) {
55  result = result.sprintf("%0.1f %%", percent);
56  } else if (percent < 1000.0) {
57  result = result.sprintf("%0.0f %%", percent);
58  } else {
59  result = result.sprintf("x %d",
60  Kwave::toInt(rint(percent / 100.0)));
61  }
62  return result;
63 }
64 
65 //***************************************************************************
66 QString Kwave::ms2string(double ms, int precision)
67 {
68  QString result;
69 
70  if (ms < 1.0) {
71  // limit precision, use 0.0 for exact zero
72  int digits = (ms != 0.0) ? Kwave::toInt(ceil(1.0 - log10(ms))) : 1;
73  if ( (digits < 0) || (digits > precision)) digits = precision;
74 
75  result = _("%1 ms").arg(ms, 0, 'f', digits);
76  } else if (ms < 1000.0) {
77  result = _("%1 ms").arg(ms, 0, 'f', 1);
78  } else {
79  int s = Kwave::toInt(round(ms / 1000.0));
80  int m = Kwave::toInt(floor(s / 60.0));
81 
82  if (m < 1) {
83  int digits = Kwave::toInt(
84  ceil(static_cast<double>(precision + 1) - log10(ms)));
85  result = _("%1 s").arg(
86  static_cast<double>(ms) / 1000.0, 0, 'f', digits);
87  } else {
88  result = _("%1:%2 min").arg(
89  m, 2, 10, QLatin1Char('0')).arg(
90  s % 60, 2, 10, QLatin1Char('0'));
91  }
92  }
93 
94  return result;
95 }
96 
97 //***************************************************************************
99 {
100  return QLocale().toString(Kwave::toUint(samples));
101 }
102 
103 //***************************************************************************
104 QString Kwave::ms2hms(double ms)
105 {
106  unsigned int t, h, m, s, tms;
107  t = static_cast<unsigned int>(rint(ms * 10.0));
108  tms = t % 10000;
109  t /= 10000;
110  s = t % 60;
111  t /= 60;
112  m = t % 60;
113  t /= 60;
114  h = t;
115 
116  QString hms_format = i18nc(
117  "time of label tooltip, %1=hour, %2=minute, %3=second, %4=milliseconds",
118  "%02u:%02u:%02u.%04u");
119  QString hms;
120  hms.sprintf(UTF8(hms_format), h, m, s, tms);
121 
122  return hms;
123 }
124 
125 //***************************************************************************
126 QString Kwave::string2date(const QString &str)
127 {
128  const Qt::DateFormat formats[] = {
129  Qt::ISODate,
130  Qt::TextDate,
131  Qt::SystemLocaleShortDate,
132  Qt::SystemLocaleLongDate,
133  Qt::DefaultLocaleShortDate,
134  Qt::DefaultLocaleLongDate
135  };
136  QString s;
137  const unsigned int fmt_count =
138  sizeof(formats) / sizeof(formats[0]);
139  QDateTime dt;
140 
141  // try ID3 full date/time
142  dt = QDateTime::fromString(str, _("yyyy-MM-ddThh:mm:ss"));
143  if (dt.isValid())
144  return str; // already in complete date/time format
145 
146  // type ID3 date without time
147  dt = QDateTime::fromString(str, _("yyyy-MM-dd"));
148  if (dt.isValid())
149  return str; // already a valid date
150 
151  // try all date/time formats supported by Qt
152  for (unsigned int i = 0; i < fmt_count; i++) {
153  Qt::DateFormat fmt = formats[i];
154  s = QString();
155 
156  dt = QDateTime::fromString(str, fmt);
157 
158  if (dt.isValid()) {
159  // full timestamp, including time
160  s = dt.toString(_("yyyy-MM-ddThh:mm:ss"));
161  }
162  if (!s.length()) {
163  // date only, without time
164  dt = QDateTime(QDate::fromString(str), QTime(0,0));
165  s = dt.toString(_("yyyy-MM-dd"));
166  }
167 
168  if (s.length()) {
169  return s;
170  }
171  }
172 
173  return QString();
174 }
175 
176 //***************************************************************************
178 {
179  return _("kwave");
180 }
181 
182 //***************************************************************************
183 //***************************************************************************
QString Q_DECL_EXPORT ms2string(double ms, int precision=6)
Definition: Utils.cpp:66
QString Q_DECL_EXPORT samples2string(sample_index_t samples)
Definition: Utils.cpp:98
QString Q_DECL_EXPORT ms2hms(double ms)
Definition: Utils.cpp:104
quint64 sample_index_t
Definition: Sample.h:28
QString Q_DECL_EXPORT zoom2string(double percent)
Definition: Utils.cpp:46
QString Q_DECL_EXPORT string2date(const QString &s)
Definition: Utils.cpp:126
int toInt(T x)
Definition: Utils.h:127
#define _(m)
Definition: memcpy.c:66
void Q_DECL_EXPORT yield()
Definition: Utils.cpp:39
unsigned int toUint(T x)
Definition: Utils.h:109
QString Q_DECL_EXPORT urlScheme()
Definition: Utils.cpp:177
#define UTF8(qs)
Definition: String.h:48