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
00031 class KuitFormatsStaticData
00032 {
00033 public:
00034
00035 KuitFormatsStaticData ();
00036
00037 QHash<QChar, QChar> westToEastArabicDigit;
00038 QChar easternArabicThSep;
00039 QChar easternArabicDecSep;
00040 };
00041
00042 KuitFormatsStaticData::KuitFormatsStaticData ()
00043 {
00044 #define WEA_ENTRY(a, b) do { \
00045 westToEastArabicDigit[a] = QString::fromUtf8(b)[0]; \
00046 } while (0)
00047 WEA_ENTRY('1', "١");
00048 WEA_ENTRY('2', "٢");
00049 WEA_ENTRY('3', "٣");
00050 WEA_ENTRY('4', "٤");
00051 WEA_ENTRY('5', "٥");
00052 WEA_ENTRY('6', "٦");
00053 WEA_ENTRY('7', "٧");
00054 WEA_ENTRY('8', "٨");
00055 WEA_ENTRY('9', "٩");
00056 WEA_ENTRY('0', "٠");
00057
00058 easternArabicThSep = QString::fromUtf8(".")[0];
00059 easternArabicDecSep = QString::fromUtf8(",")[0];
00060 }
00061
00062 K_GLOBAL_STATIC(KuitFormatsStaticData, staticData)
00063
00064 static QString insertIntegerSeparators (const QString &istr,
00065 const QChar &sep, int ngrp)
00066 {
00067 int len = istr.length();
00068 int flen = (len / ngrp + 1) * (ngrp + 1);
00069 QString fistr(flen, ' ');
00070 for (int i = 0, fi = 0; i < len; ++i, ++fi) {
00071 if (i > 0 && i % 3 == 0) {
00072 fistr[flen - fi - 1] = sep;
00073 ++fi;
00074 }
00075 fistr[flen - fi - 1] = istr[len - i - 1];
00076 }
00077 fistr = fistr.trimmed();
00078 return fistr;
00079 }
00080
00081 static QString toNumberGeneric (const QString &numstr,
00082 const QChar &thosep, const QChar &decsep,
00083 int thosepGE = 0)
00084 {
00085 int len = numstr.length();
00086 int p1 = 0;
00087 while (p1 < len && !numstr[p1].isDigit()) {
00088 ++p1;
00089 }
00090 if (p1 == len) {
00091 return numstr;
00092 }
00093 int p2 = p1 + 1;
00094 while (p2 < len && numstr[p2].isDigit()) {
00095 ++p2;
00096 }
00097 QString intpart = numstr.mid(p1, p2 - p1);
00098 int intval = intpart.toInt();
00099
00100 QString pre = numstr.left(p1);
00101 QString mid = intpart;
00102 if (intval >= thosepGE) {
00103 mid = insertIntegerSeparators(intpart, thosep, 3);
00104 }
00105
00106 QString post = numstr.mid(p2);
00107 if (post.startsWith('.')) {
00108 post[0] = decsep;
00109 }
00110
00111 return pre + mid + post;
00112 }
00113
00114 QString KuitFormats::toNumberSystem (const QString &numstr)
00115 {
00116 return KGlobal::locale()->formatNumber(numstr, false);
00117 }
00118
00119 QString KuitFormats::toNumberUS (const QString &numstr)
00120 {
00121 return toNumberGeneric(numstr, ',', '.');
00122 }
00123
00124 QString KuitFormats::toNumberEuro (const QString &numstr)
00125 {
00126 return toNumberGeneric(numstr, '.', ',');
00127 }
00128
00129 QString KuitFormats::toNumberEuro2 (const QString &numstr)
00130 {
00131 return toNumberGeneric(numstr, ' ', ',');
00132 }
00133
00134 QString KuitFormats::toNumberEuro2ct (const QString &numstr)
00135 {
00136 return toNumberGeneric(numstr, ' ', ',', 10000);
00137 }
00138
00139
00140
00141 QString KuitFormats::toNumberEArab (const QString &numstr)
00142 {
00143 KuitFormatsStaticData *s = staticData;
00144
00145 const int power = 3;
00146
00147
00148 QString sepnum;
00149
00150
00151 int i = 0;
00152 while ((i < numstr.length()) && (numstr[i] != '.')) {
00153 ++i;
00154 }
00155
00156
00157 int j = power;
00158 while (j < i) {
00159 sepnum = s->easternArabicThSep + numstr.mid(i - j, power) + sepnum;
00160 j += power;
00161 }
00162 sepnum = numstr.left(i + power - j) + sepnum;
00163
00164
00165 if (i + 1 < numstr.length()) {
00166 sepnum += s->easternArabicDecSep + numstr.mid(i + 1);
00167 }
00168
00169
00170 if (numstr.length() > 1 && numstr[0] == '.') {
00171 sepnum = '0' + sepnum;
00172 }
00173
00174
00175 QString arnum;
00176 for (int i = 0; i < sepnum.length(); ++i) {
00177 arnum += s->westToEastArabicDigit.value(sepnum[i], sepnum[i]);
00178 }
00179
00180 return arnum;
00181 }
00182
00183 QString KuitFormats::toKeyCombo (const QString &shstr, const QString &delim,
00184 const QHash<QString, QString> &keydict)
00185 {
00186 static QRegExp delRx("[+-]");
00187
00188 int p = delRx.indexIn(shstr);
00189
00190 QStringList keys;
00191 if (p < 0) {
00192 keys.append(shstr);
00193 }
00194 else {
00195 QChar oldDelim = shstr[p];
00196 keys = shstr.split(oldDelim, QString::SkipEmptyParts);
00197 }
00198
00199 for (int i = 0; i < keys.size(); ++i) {
00200
00201 QString nkey = keys[i].trimmed().toLower();
00202 bool isFunctionKey = nkey.length() > 1 && nkey[1].isDigit();
00203 if (!isFunctionKey) {
00204 keys[i] = keydict.contains(nkey) ? keydict[nkey] : keys[i].trimmed();
00205 }
00206 else {
00207 keys[i] = keydict["f%1"].arg(nkey.mid(1));
00208 }
00209 }
00210 return keys.join(delim);
00211 }
00212
00213 QString KuitFormats::toInterfacePath (const QString &inpstr,
00214 const QString &delim)
00215 {
00216 static QRegExp delRx("\\||->");
00217
00218 int p = delRx.indexIn(inpstr);
00219 if (p < 0) {
00220 return inpstr;
00221 }
00222 else {
00223 QString oldDelim = delRx.capturedTexts().at(0);
00224 QStringList guiels = inpstr.split(oldDelim, QString::SkipEmptyParts);
00225 return guiels.join(delim);
00226 }
00227 }
00228