• Skip to content
  • Skip to link menu
KDE 4.1 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 #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); // find delimiter
00113 
00114     QStringList keys;
00115     if (p < 0) { // single-key shortcut, no delimiter found
00116         keys.append(shstr);
00117     }
00118     else { // multi-key shortcut
00119         QChar oldDelim = shstr[p];
00120         keys = shstr.split(oldDelim, QString::SkipEmptyParts);
00121     }
00122 
00123     for (int i = 0; i < keys.size(); ++i) {
00124         // Normalize key, trim and all lower-case.
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); // find delimiter
00143     if (p < 0) { // single-element path, no delimiter found
00144         return inpstr;
00145     }
00146     else { // multi-element path
00147         QString oldDelim = delRx.capturedTexts().at(0);
00148         QStringList guiels = inpstr.split(oldDelim, QString::SkipEmptyParts);
00149         return guiels.join(delim);
00150     }
00151 }
00152 

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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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