kplato

kptwbsdefinition.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Dag Andersen <danders@get2net.dk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation;
7 version 2 of the License.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kptwbsdefinition.h"
21
22
23#include <tdelocale.h>
24#include <kdebug.h>
25
26#include <tqstring.h>
27#include <tqstringlist.h>
28#include <tqpair.h>
29
30namespace KPlato
31{
32
33
34WBSDefinition::WBSDefinition() {
35 m_levelsEnabled = false;
36
37 m_defaultDef.code = "Number";
38 m_defaultDef.separator = ".";
39
40 m_codeLists.append(qMakePair(TQString("Number"), i18n("Number")));
41 m_codeLists.append(qMakePair(TQString("Roman, upper case"), i18n("Roman, Upper Case")));
42 m_codeLists.append(qMakePair(TQString("Roman, lower case"), i18n("Roman, Lower Case")));
43 m_codeLists.append(qMakePair(TQString("Letter, upper case"), i18n("Letter, Upper Case")));
44 m_codeLists.append(qMakePair(TQString("Letter, lower case"), i18n("Letter, Lower Case")));
45}
46
47WBSDefinition::~WBSDefinition() {
48}
49
50void WBSDefinition::clear() {
51 m_defaultDef.clear();
52 m_levelsDef.clear();
53}
54
55TQString WBSDefinition::wbs(uint index, int level) {
56 if (isLevelsDefEnabled()) {
57 CodeDef def = levelsDef(level);
58 if (!def.isEmpty()) {
59 return code(def, index) + def.separator;
60 }
61 }
62 return code(m_defaultDef, index) + m_defaultDef.separator;
63}
64
65
66TQString WBSDefinition::code(uint index, int level) {
67 if (isLevelsDefEnabled()) {
68 CodeDef def = levelsDef(level);
69 if (!def.isEmpty()) {
70 return code(def, index);
71 }
72 }
73 return code(m_defaultDef, index);
74}
75
76TQString WBSDefinition::separator(int level) {
77 if (isLevelsDefEnabled()) {
78 CodeDef def = levelsDef(level);
79 if (!def.isEmpty()) {
80 return def.separator;
81 }
82 }
83 return m_defaultDef.separator;
84}
85
86void WBSDefinition::setLevelsDef(TQMap<int, CodeDef> def) {
87 m_levelsDef.clear();
88 m_levelsDef = def;
89}
90
91WBSDefinition::CodeDef WBSDefinition::levelsDef(int level) const {
92 return m_levelsDef.contains(level) ? m_levelsDef[level] : CodeDef();
93}
94
95void WBSDefinition::setLevelsDef(int level, CodeDef def) {
96 m_levelsDef.insert(level, def);
97}
98
99void WBSDefinition::setLevelsDef(int level, TQString c, TQString s) {
100 m_levelsDef.insert(level, CodeDef(c, s));
101}
102
103bool WBSDefinition::level0Enabled() {
104 return m_levelsEnabled && !levelsDef(0).isEmpty();
105}
106
107const 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' };
108
109TQString WBSDefinition::code(CodeDef &def, uint index) {
110 if (def.code == "Number") {
111 return TQString("%1").arg(index);
112 }
113 if (def.code == "Roman, lower case") {
114 return TQString("%1").arg(toRoman(index));
115 }
116 if (def.code == "Roman, upper case") {
117 return TQString("%1").arg(toRoman(index, true));
118 }
119 if (def.code == "Letter, lower case") {
120 if (index > 26) {
121 index = 0;
122 }
123 return TQString("%1").arg(Letters[index]);
124 }
125 if (def.code == "Letter, upper case") {
126 if (index > 26) {
127 index = 0;
128 }
129 return TQString("%1").arg(Letters[index].upper());
130 }
131 return TQString();
132}
133
134// Nicked from koparagcounter.cpp
135const TQCString RNUnits[] = {"", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"};
136const TQCString RNTens[] = {"", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"};
137const TQCString RNHundreds[] = {"", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"};
138const TQCString RNThousands[] = {"", "m", "mm", "mmm"};
139
140TQString WBSDefinition::toRoman( int n, bool upper )
141{
142 if ( n >= 0 ) {
143 TQString s = TQString::fromLatin1( RNThousands[ ( n / 1000 ) ] +
144 RNHundreds[ ( n / 100 ) % 10 ] +
145 RNTens[ ( n / 10 ) % 10 ] +
146 RNUnits[ ( n ) % 10 ] );
147 return upper ? s.upper() : s;
148
149 } else { // should never happen, but better not crash if it does
150 kdWarning()<< k_funcinfo << " n=" << n << endl;
151 return TQString::number( n );
152 }
153}
154
155TQStringList WBSDefinition::codeList() {
156 TQStringList cl;
157 TQValueList<TQPair<TQString, TQString> >::Iterator it;
158 for (it = m_codeLists.begin(); it != m_codeLists.end(); ++it) {
159 cl.append((*it).second);
160 }
161 return cl;
162}
163
164int WBSDefinition::defaultCodeIndex() const {
165 TQValueList<TQPair<TQString, TQString> >::const_iterator it;
166 int i = -1;
167 for(it = m_codeLists.begin(); it != m_codeLists.end(); ++it) {
168 ++i;
169 if (m_defaultDef.code == (*it).first)
170 break;
171 }
172 return i;
173}
174
175bool WBSDefinition::setDefaultCode(uint index) {
176 TQValueList<TQPair<TQString, TQString> >::const_iterator it = m_codeLists.at(index);
177 if (it == m_codeLists.end()) {
178 return false;
179 }
180 m_defaultDef.code = (*it).first;
181 return true;
182}
183
184void WBSDefinition::setDefaultSeparator(TQString s) {
185 m_defaultDef.separator = s;
186}
187
188} //namespace KPlato