F:/KPlato/koffice/kplato/kptwbsdefinition.cc

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Dag Andersen <danders@get2net.dk>
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;
00007    version 2 of the License.
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 "kptwbsdefinition.h"
00021 
00022 
00023 #include <klocale.h>
00024 #include <kdebug.h>
00025 
00026 #include <QList>
00027 #include <QString>
00028 #include <QStringList>
00029 #include <QPair>
00030 
00031 namespace KPlato
00032 {
00033 
00034 
00035 WBSDefinition::WBSDefinition() {
00036     m_levelsEnabled = false;
00037     
00038     m_defaultDef.code = "Number";
00039     m_defaultDef.separator = ".";
00040     
00041     m_codeLists.append(qMakePair(QString("Number"), i18n("Number")));
00042     m_codeLists.append(qMakePair(QString("Roman, upper case"), i18n("Roman, upper case")));
00043     m_codeLists.append(qMakePair(QString("Roman, lower case"), i18n("Roman, lower case")));
00044     m_codeLists.append(qMakePair(QString("Letter, upper case"), i18n("Letter, upper case")));
00045     m_codeLists.append(qMakePair(QString("Letter, lower case"), i18n("Letter, lower case")));
00046 }
00047 
00048 WBSDefinition::~WBSDefinition() {
00049 }
00050 
00051 void WBSDefinition::clear() {
00052     m_defaultDef.clear();
00053     m_levelsDef.clear();
00054 }
00055     
00056 QString WBSDefinition::wbs(uint index, int level) {
00057     if (isLevelsDefEnabled()) {
00058         CodeDef def = levelsDef(level);
00059         if (!def.isEmpty()) {
00060             return code(def, index) + def.separator;
00061         }
00062     }
00063     return code(m_defaultDef, index) + m_defaultDef.separator;
00064 }
00065 
00066 
00067 QString WBSDefinition::code(uint index, int level) {
00068     if (isLevelsDefEnabled()) {
00069         CodeDef def = levelsDef(level);
00070         if (!def.isEmpty()) {
00071             return code(def, index);
00072         }
00073     }
00074     return code(m_defaultDef, index);
00075 }
00076 
00077 QString WBSDefinition::separator(int level) {
00078     if (isLevelsDefEnabled()) {
00079         CodeDef def = levelsDef(level);
00080         if (!def.isEmpty()) {
00081             return def.separator;
00082         }
00083     }
00084     return m_defaultDef.separator;
00085 }
00086 
00087 void WBSDefinition::setLevelsDef(QMap<int, CodeDef> def) { 
00088     m_levelsDef.clear();
00089     m_levelsDef = def; 
00090 }
00091 
00092 WBSDefinition::CodeDef WBSDefinition::levelsDef(int level) const { 
00093     return m_levelsDef.contains(level) ? m_levelsDef[level] : CodeDef(); 
00094 }
00095     
00096 void WBSDefinition::setLevelsDef(int level, CodeDef def) {
00097     m_levelsDef.insert(level, def);
00098 }
00099 
00100 void WBSDefinition::setLevelsDef(int level, QString c, QString s) {
00101     m_levelsDef.insert(level, CodeDef(c, s));
00102 }
00103 
00104 bool WBSDefinition::level0Enabled() {
00105     return m_levelsEnabled && !levelsDef(0).isEmpty();
00106 }
00107 
00108 const QChar Letters[] = { '?','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
00109 
00110 QString WBSDefinition::code(CodeDef &def, uint index) {
00111     if (def.code == "Number") {
00112         return QString("%1").arg(index);
00113     }
00114     if (def.code == "Roman, lower case") {
00115         return QString("%1").arg(toRoman(index));
00116     }
00117     if (def.code == "Roman, upper case") {
00118         return QString("%1").arg(toRoman(index, true));
00119     }
00120     if (def.code == "Letter, lower case") {
00121         if (index > 26) {
00122             index = 0;
00123         }
00124         return QString("%1").arg(Letters[index]);
00125     }
00126     if (def.code == "Letter, upper case") {
00127         if (index > 26) {
00128             index = 0;
00129         }
00130         return QString("%1").arg(Letters[index].toUpper());
00131     }
00132     return QString();
00133 }
00134 
00135 // Nicked from koparagcounter.cc
00136 const QByteArray RNUnits[] = {"", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"};
00137 const QByteArray RNTens[] = {"", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"};
00138 const QByteArray RNHundreds[] = {"", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"};
00139 const QByteArray RNThousands[] = {"", "m", "mm", "mmm"};
00140 
00141 QString WBSDefinition::toRoman( int n, bool upper )
00142 {
00143     if ( n >= 0 ) {
00144         QString s = QString::fromLatin1( RNThousands[ ( n / 1000 ) ] +
00145                                          RNHundreds[ ( n / 100 ) % 10 ] +
00146                                          RNTens[ ( n / 10 ) % 10 ] +
00147                                          RNUnits[ ( n ) % 10 ] );
00148         return upper ? s.toUpper() : s;
00149         
00150     } else { // should never happen, but better not crash if it does
00151         kWarning()<< k_funcinfo << " n=" << n << endl;
00152         return QString::number( n );
00153     }
00154 }
00155 
00156 QStringList WBSDefinition::codeList() {
00157     QStringList cl;
00158     QList<QPair<QString, QString> >::Iterator it;
00159     for (it = m_codeLists.begin(); it != m_codeLists.end(); ++it) {
00160         cl.append((*it).second);
00161     }
00162     return cl;
00163 }
00164 
00165 int WBSDefinition::defaultCodeIndex() const {
00166     int index = -1;
00167     for(int i = 0; i < m_codeLists.count(); ++i) {
00168         if (m_defaultDef.code == m_codeLists.at(i).first) {
00169             index = i;
00170             break;
00171         }
00172     }
00173     return index;
00174 }
00175 
00176 bool WBSDefinition::setDefaultCode(uint index) {
00177     if (index >= m_codeLists.size()) {
00178         return false;
00179     }
00180     m_defaultDef.code = m_codeLists[index].first;
00181     return true;
00182 }
00183 
00184 void WBSDefinition::setDefaultSeparator(QString s) {
00185     m_defaultDef.separator = s;
00186 }
00187 
00188 } //namespace KPlato

Généré le Wed Nov 22 23:40:58 2006 pour KPlato par  doxygen 1.5.1-p1