summaryrefslogtreecommitdiffstats
path: root/kplato/kptwbsdefinition.cpp
blob: 4c1465334f770da96299d39dd9eb787b8937705b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* This file is part of the KDE project
   Copyright (C) 2005 Dag Andersen <danders@get2net.dk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation;
   version 2 of the License.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kptwbsdefinition.h"


#include <tdelocale.h>
#include <kdebug.h>

#include <tqstring.h>
#include <tqstringlist.h>
#include <tqpair.h>

namespace KPlato
{


WBSDefinition::WBSDefinition() {
    m_levelsEnabled = false;
    
    m_defaultDef.code = "Number";
    m_defaultDef.separator = ".";
    
    m_codeLists.append(tqMakePair(TQString("Number"), i18n("Number")));
    m_codeLists.append(tqMakePair(TQString("Roman, upper case"), i18n("Roman, Upper Case")));
    m_codeLists.append(tqMakePair(TQString("Roman, lower case"), i18n("Roman, Lower Case")));
    m_codeLists.append(tqMakePair(TQString("Letter, upper case"), i18n("Letter, Upper Case")));
    m_codeLists.append(tqMakePair(TQString("Letter, lower case"), i18n("Letter, Lower Case")));
}

WBSDefinition::~WBSDefinition() {
}

void WBSDefinition::clear() {
    m_defaultDef.clear();
    m_levelsDef.clear();
}
    
TQString WBSDefinition::wbs(uint index, int level) {
    if (isLevelsDefEnabled()) {
        CodeDef def = levelsDef(level);
        if (!def.isEmpty()) {
            return code(def, index) + def.separator;
        }
    }
    return code(m_defaultDef, index) + m_defaultDef.separator;
}


TQString WBSDefinition::code(uint index, int level) {
    if (isLevelsDefEnabled()) {
        CodeDef def = levelsDef(level);
        if (!def.isEmpty()) {
            return code(def, index);
        }
    }
    return code(m_defaultDef, index);
}

TQString WBSDefinition::separator(int level) {
    if (isLevelsDefEnabled()) {
        CodeDef def = levelsDef(level);
        if (!def.isEmpty()) {
            return def.separator;
        }
    }
    return m_defaultDef.separator;
}

void WBSDefinition::setLevelsDef(TQMap<int, CodeDef> def) { 
    m_levelsDef.clear();
    m_levelsDef = def; 
}

WBSDefinition::CodeDef WBSDefinition::levelsDef(int level) const { 
    return m_levelsDef.contains(level) ? m_levelsDef[level] : CodeDef(); 
}
    
void WBSDefinition::setLevelsDef(int level, CodeDef def) {
    m_levelsDef.insert(level, def);
}

void WBSDefinition::setLevelsDef(int level, TQString c, TQString s) {
    m_levelsDef.insert(level, CodeDef(c, s));
}

bool WBSDefinition::level0Enabled() {
    return m_levelsEnabled && !levelsDef(0).isEmpty();
}

const TQChar 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' };

TQString WBSDefinition::code(CodeDef &def, uint index) {
    if (def.code == "Number") {
        return TQString("%1").arg(index);
    }
    if (def.code == "Roman, lower case") {
        return TQString("%1").arg(toRoman(index));
    }
    if (def.code == "Roman, upper case") {
        return TQString("%1").arg(toRoman(index, true));
    }
    if (def.code == "Letter, lower case") {
        if (index > 26) {
            index = 0;
        }
        return TQString("%1").arg(Letters[index]);
    }
    if (def.code == "Letter, upper case") {
        if (index > 26) {
            index = 0;
        }
        return TQString("%1").arg(Letters[index].upper());
    }
    return TQString();
}

// Nicked from koparagcounter.cpp
const TQCString RNUnits[] = {"", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"};
const TQCString RNTens[] = {"", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"};
const TQCString RNHundreds[] = {"", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"};
const TQCString RNThousands[] = {"", "m", "mm", "mmm"};

TQString WBSDefinition::toRoman( int n, bool upper )
{
    if ( n >= 0 ) {
        TQString s = TQString::fromLatin1( RNThousands[ ( n / 1000 ) ] +
                                         RNHundreds[ ( n / 100 ) % 10 ] +
                                         RNTens[ ( n / 10 ) % 10 ] +
                                         RNUnits[ ( n ) % 10 ] );
        return upper ? s.upper() : s;
        
    } else { // should never happen, but better not crash if it does
        kdWarning()<< k_funcinfo << " n=" << n << endl;
        return TQString::number( n );
    }
}

TQStringList WBSDefinition::codeList() {
    TQStringList cl;
    TQValueList<TQPair<TQString, TQString> >::Iterator it;
    for (it = m_codeLists.begin(); it != m_codeLists.end(); ++it) {
        cl.append((*it).second);
    }
    return cl;
}

int WBSDefinition::defaultCodeIndex() const {
    TQValueList<TQPair<TQString, TQString> >::const_iterator it;
    int i = -1;
    for(it = m_codeLists.begin(); it != m_codeLists.end(); ++it) {
        ++i;
        if (m_defaultDef.code == (*it).first)
            break;
    }
    return i;
}

bool WBSDefinition::setDefaultCode(uint index) {
    TQValueList<TQPair<TQString, TQString> >::const_iterator it = m_codeLists.at(index);
    if (it == m_codeLists.end()) {
        return false;
    }
    m_defaultDef.code = (*it).first;
    return true;
}

void WBSDefinition::setDefaultSeparator(TQString s) {
    m_defaultDef.separator = s;
}

} //namespace KPlato