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