kplato

kptwbsdefinition.cc

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