KDECore
kuitformats.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <kuitformats_p.h>
00021
00022 #include <config.h>
00023 #include <kglobal.h>
00024 #include <klocale.h>
00025
00026 #include <QStringList>
00027 #include <QRegExp>
00028
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031
00032 static QString insertIntegerSeparators (const QString &istr,
00033 const QChar &sep, int ngrp)
00034 {
00035 int len = istr.length();
00036 int flen = (len / ngrp + 1) * (ngrp + 1);
00037 QString fistr(flen, ' ');
00038 for (int i = 0, fi = 0; i < len; ++i, ++fi) {
00039 if (i > 0 && i % 3 == 0) {
00040 fistr[flen - fi - 1] = sep;
00041 ++fi;
00042 }
00043 fistr[flen - fi - 1] = istr[len - i - 1];
00044 }
00045 fistr = fistr.trimmed();
00046 return fistr;
00047 }
00048
00049 static QString toNumberGeneric (const QString &numstr,
00050 const QChar &thosep, const QChar &decsep,
00051 int thosepGE = 0)
00052 {
00053 int len = numstr.length();
00054 int p1 = 0;
00055 while (p1 < len && !numstr[p1].isDigit()) {
00056 ++p1;
00057 }
00058 if (p1 == len) {
00059 return numstr;
00060 }
00061 int p2 = p1 + 1;
00062 while (p2 < len && numstr[p2].isDigit()) {
00063 ++p2;
00064 }
00065 QString intpart = numstr.mid(p1, p2 - p1);
00066 int intval = intpart.toInt();
00067
00068 QString pre = numstr.left(p1);
00069 QString mid = intpart;
00070 if (intval >= thosepGE) {
00071 mid = insertIntegerSeparators(intpart, thosep, 3);
00072 }
00073
00074 QString post = numstr.mid(p2);
00075 if (post.startsWith('.')) {
00076 post[0] = decsep;
00077 }
00078
00079 return pre + mid + post;
00080 }
00081
00082 QString KuitFormats::toNumberSystem (const QString &numstr)
00083 {
00084 return KGlobal::locale()->formatNumber(numstr, false);
00085 }
00086
00087 QString KuitFormats::toNumberUS (const QString &numstr)
00088 {
00089 return toNumberGeneric(numstr, ',', '.');
00090 }
00091
00092 QString KuitFormats::toNumberEuro (const QString &numstr)
00093 {
00094 return toNumberGeneric(numstr, '.', ',');
00095 }
00096
00097 QString KuitFormats::toNumberEuro2 (const QString &numstr)
00098 {
00099 return toNumberGeneric(numstr, ' ', ',');
00100 }
00101
00102 QString KuitFormats::toNumberEuro2ct (const QString &numstr)
00103 {
00104 return toNumberGeneric(numstr, ' ', ',', 10000);
00105 }
00106
00107 QString KuitFormats::toKeyCombo (const QString &shstr, const QString &delim,
00108 const QHash<QString, QString> &keydict)
00109 {
00110 static QRegExp delRx("[+-]");
00111
00112 int p = delRx.indexIn(shstr);
00113
00114 QStringList keys;
00115 if (p < 0) {
00116 keys.append(shstr);
00117 }
00118 else {
00119 QChar oldDelim = shstr[p];
00120 keys = shstr.split(oldDelim, QString::SkipEmptyParts);
00121 }
00122
00123 for (int i = 0; i < keys.size(); ++i) {
00124
00125 QString nkey = keys[i].trimmed().toLower();
00126 bool isFunctionKey = nkey.length() > 1 && nkey[1].isDigit();
00127 if (!isFunctionKey) {
00128 keys[i] = keydict.contains(nkey) ? keydict[nkey] : keys[i].trimmed();
00129 }
00130 else {
00131 keys[i] = keydict["f%1"].arg(nkey.mid(1));
00132 }
00133 }
00134 return keys.join(delim);
00135 }
00136
00137 QString KuitFormats::toInterfacePath (const QString &inpstr,
00138 const QString &delim)
00139 {
00140 static QRegExp delRx("\\||->");
00141
00142 int p = delRx.indexIn(inpstr);
00143 if (p < 0) {
00144 return inpstr;
00145 }
00146 else {
00147 QString oldDelim = delRx.capturedTexts().at(0);
00148 QStringList guiels = inpstr.split(oldDelim, QString::SkipEmptyParts);
00149 return guiels.join(delim);
00150 }
00151 }
00152