• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kuitformats.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE libraries
00002     Copyright (C) 2007 Chusslove Illich <caslav.ilic@gmx.net>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
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 // Translated from a Pascal implementation provided
00140 // by Youssef Chahibi <chahibi@gmail.com>
00141 QString KuitFormats::toNumberEArab (const QString &numstr)
00142 {
00143     KuitFormatsStaticData *s = staticData;
00144 
00145     const int power = 3;
00146 
00147     // Construct string with proper separators, but Western Arabic digits.
00148     QString sepnum;
00149 
00150     // Find decimal separator in input.
00151     int i = 0;
00152     while ((i < numstr.length()) && (numstr[i] != '.')) {
00153         ++i;
00154     }
00155 
00156     // Add thousand separators to integer part.
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     // Add decimal part.
00165     if (i + 1 < numstr.length()) {
00166         sepnum += s->easternArabicDecSep + numstr.mid(i + 1);
00167     }
00168 
00169     // Add leading 0 if by any chance the input starts with decimal separator.
00170     if (numstr.length() > 1 && numstr[0] == '.') {
00171         sepnum = '0' + sepnum;
00172     }
00173 
00174     // Replace Western with Eastern Arabic digits in the separated string.
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); // find delimiter
00189 
00190     QStringList keys;
00191     if (p < 0) { // single-key shortcut, no delimiter found
00192         keys.append(shstr);
00193     }
00194     else { // multi-key shortcut
00195         QChar oldDelim = shstr[p];
00196         keys = shstr.split(oldDelim, QString::SkipEmptyParts);
00197     }
00198 
00199     for (int i = 0; i < keys.size(); ++i) {
00200         // Normalize key, trim and all lower-case.
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); // find delimiter
00219     if (p < 0) { // single-element path, no delimiter found
00220         return inpstr;
00221     }
00222     else { // multi-element path
00223         QString oldDelim = delRx.capturedTexts().at(0);
00224         QStringList guiels = inpstr.split(oldDelim, QString::SkipEmptyParts);
00225         return guiels.join(delim);
00226     }
00227 }
00228 

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal