Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[3e4acc]: libkwave / Utils.cpp Maximize Restore History

Download this file

Utils.cpp    135 lines (115 with data), 4.2 kB

  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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/***************************************************************************
Utils.cpp - some commonly used utility functions
-------------------
begin : Sun Mar 27 2011
copyright : (C) 2011 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <QString>
#include <QThread>
#include <kapplication.h>
#include <kglobal.h>
#include <klocale.h>
#include "Utils.h"
//***************************************************************************
void Kwave::yield()
{
pthread_testcancel();
QThread::yieldCurrentThread();
}
//***************************************************************************
QString Kwave::zoom2string(double percent)
{
QString result = "";
if (percent < 1.0) {
int digits = static_cast<int>(ceil(1.0 - log10(percent)));
QString format;
format = "%0."+format.setNum(digits)+"f %%";
result = format.sprintf(format.toUtf8(), percent);
} else if (percent < 10.0) {
result = result.sprintf("%0.1f %%", percent);
} else if (percent < 1000.0) {
result = result.sprintf("%0.0f %%", percent);
} else {
result = result.sprintf("x %d",
static_cast<int>(rint(percent / 100.0)));
}
return result;
}
//***************************************************************************
QString Kwave::ms2string(double ms, int precision)
{
char buf[128];
int bufsize = 128;
if (ms < 1.0) {
char format[128];
// limit precision, use 0.0 for exact zero
int digits = (ms != 0.0) ? static_cast<int>(ceil(1.0 - log10(ms))) : 1;
if ( (digits < 0) || (digits > precision)) digits = precision;
snprintf(format, sizeof(format), "%%0.%df ms", digits);
snprintf(buf, bufsize, format, ms);
} else if (ms < 1000.0) {
snprintf(buf, bufsize, "%0.1f ms", ms);
} else {
int s = static_cast<int>(round(ms / 1000.0));
int m = static_cast<int>(floor(s / 60.0));
if (m < 1) {
char format[128];
int digits = static_cast<int>(
ceil(static_cast<double>(precision+1) - log10(ms)));
snprintf(format, sizeof(format), "%%0.%df s", digits);
snprintf(buf, bufsize, format, ms / 1000.0);
} else {
snprintf(buf, bufsize, "%02d:%02d min", m, s % 60);
}
}
QString result(buf);
return result;
}
//***************************************************************************
QString Kwave::ms2hms(double ms)
{
unsigned int t, h, m, s, tms;
t = static_cast<unsigned int>(rint(ms * 10.0));
tms = t % 10000;
t /= 10000;
s = t % 60;
t /= 60;
m = t % 60;
t /= 60;
h = t;
QString hms_format = i18nc(
"time of label tooltip, %1=hour, %2=minute, %3=second, %4=milliseconds",
"%02u:%02u:%02u.%04u");
QString hms;
hms.sprintf(hms_format.toUtf8().data(), h, m, s, tms);
return hms;
}
//***************************************************************************
QString Kwave::dottedNumber(unsigned int number)
{
const QString num = QString::number(number);
QString dotted = "";
const QString dot = KGlobal::locale()->thousandsSeparator();
const int len = num.length();
for (int i=len-1; i >= 0; i--) {
if ((i != len-1) && !((len-i-1) % 3)) dotted = dot + dotted;
dotted = num.at(i) + dotted;
}
return dotted;
}
//***************************************************************************
//***************************************************************************