• 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 
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); // find delimiter
00106 
00107     QStringList keys;
00108     if (p < 0) { // single-key shortcut, no delimiter found
00109         keys.append(shstr);
00110     }
00111     else { // multi-key shortcut
00112         QChar oldDelim = shstr[p];
00113         keys = shstr.split(oldDelim, QString::SkipEmptyParts);
00114     }
00115 
00116     for (int i = 0; i < keys.size(); ++i) {
00117         // Normalize key, trim and all lower-case.
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); // find delimiter
00136     if (p < 0) { // single-element path, no delimiter found
00137         return inpstr;
00138     }
00139     else { // multi-element path
00140         QString oldDelim = delRx.capturedTexts().at(0);
00141         QStringList guiels = inpstr.split(oldDelim, QString::SkipEmptyParts);
00142         return guiels.join(delim);
00143     }
00144 }
00145 

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.6
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