moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kvoctrain / kvoctrain / kvt-core / GrammerManager.cpp
blobed69b3ee59fd9deb31e9670e318eb73ee21140e5
1 /***************************************************************************
3 manage grammar parts (articles, conjugation)
5 -----------------------------------------------------------------------
7 begin : Sat Nov 27 09:50:53 MET 1999
9 copyright : (C) 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
10 (C) 2001 The KDE-EDU team
11 (C) 2004-2005 Peter Hedlund <peter@peterandlinda.com>
13 -----------------------------------------------------------------------
15 ***************************************************************************/
17 /***************************************************************************
18 * *
19 * This program is free software; you can redistribute it and/or modify *
20 * it under the terms of the GNU General Public License as published by *
21 * the Free Software Foundation; either version 2 of the License, or *
22 * (at your option) any later version. *
23 * *
24 ***************************************************************************/
26 #include "GrammerManager.h"
28 #include <klocale.h>
30 Conjugation::conjug_name_t
31 Conjugation::names [] =
33 { CONJ_SIMPLE_PRESENT, I18N_NOOP("Simple Preset") },
34 { CONJ_PRESENT_PROGR, I18N_NOOP("Preset Progressive") },
35 { CONJ_PRESENT_PERFECT, I18N_NOOP("Preset Perfect") },
37 { CONJ_SIMPLE_PAST, I18N_NOOP("Simple Past") },
38 { CONJ_PAST_PROGR, I18N_NOOP("Past Progressive") },
39 { CONJ_PAST_PARTICIPLE, I18N_NOOP("Past Participle") },
41 { CONJ_FUTURE, I18N_NOOP("Future") }
45 vector<QString> Conjugation::userTenses;
48 //================================================================
50 Comparison::Comparison (
51 const QString &l1,
52 const QString &l2,
53 const QString &l3
56 setL1 (l1);
57 setL2 (l2);
58 setL3 (l3);
62 bool Comparison::isEmpty() const
64 return ls1.stripWhiteSpace().isEmpty()
65 && ls2.stripWhiteSpace().isEmpty()
66 && ls3.stripWhiteSpace().isEmpty();
70 void Comparison::clear()
72 ls1 = "";
73 ls2 = "";
74 ls3 = "";
78 //=================================================================
81 Article::Article
82 (const QString &fem_def, const QString &fem_indef,
83 const QString &mal_def, const QString &mal_indef,
84 const QString &nat_def, const QString &nat_indef
87 setFemale (fem_def, fem_indef);
88 setMale (mal_def, mal_indef);
89 setNatural (nat_def, nat_indef);
93 void Article::setFemale
94 (const QString &def, const QString &indef)
96 fem_def = def;
97 fem_indef = indef;
101 void Article::setMale
102 (const QString &def, const QString &indef)
104 mal_def = def;
105 mal_indef = indef;
109 void Article::setNatural
110 (const QString &def, const QString &indef)
112 nat_def = def;
113 nat_indef = indef;
117 void Article::female
118 (QString &def, QString &indef) const
120 def = fem_def;
121 indef = fem_indef;
125 void Article::male
126 (QString &def, QString &indef) const
128 def = mal_def;
129 indef = mal_indef;
133 void Article::natural
134 (QString &def, QString &indef) const
136 def = nat_def;
137 indef = nat_indef;
142 //==============================================================
145 int Conjugation::numEntries() const
147 return conjugations.size();
151 vector<TenseRelation> Conjugation::getRelation ()
153 vector<TenseRelation> vec;
155 for (int i = 0; i < numInternalNames(); i++) {
156 vec.push_back(TenseRelation(names[i].abbrev,
157 i18n(names[i].name)));
160 for (int i = 0; i < (int) userTenses.size(); i++) {
161 QString s;
162 s.setNum(i+1);
163 s.insert(0, UL_USER_TENSE);
164 vec.push_back(TenseRelation(s, userTenses[i]));
167 return vec;
171 void Conjugation::setTenseNames (vector<QString> names)
173 userTenses = names;
177 QString Conjugation::getName (const QString &abbrev)
179 if (abbrev.length() >= 2 && abbrev[0] == QString(UL_USER_TENSE)) {
180 QString s = abbrev;
181 s.remove(0, 1);
182 int i = s.toInt() - 1;
184 if (i < (int) userTenses.size() )
185 return userTenses[i];
186 else
187 return "";
189 else {
190 for (int i = 0; i < (int) numInternalNames(); i++)
191 if (names[i].abbrev == abbrev) {
192 return i18n(names[i].name);
196 return "";
200 QString Conjugation::getName (int idx)
202 if (idx < numInternalNames() )
203 return i18n(names[idx].name);
205 else if (idx < numTenses() )
206 return userTenses[idx-numInternalNames()];
208 else
209 return "";
213 QString Conjugation::getAbbrev (const QString &name)
215 for (int i = 0; i < (int) userTenses.size(); i++)
216 if (userTenses[i] == name) {
217 QString s;
218 s.setNum(i+1);
219 s.insert(0, UL_USER_TENSE);
220 return s;
223 for (int i = 0; i < (int) numInternalNames(); i++)
224 if (names[i].name == name)
225 return names[i].abbrev;
227 return "";
231 QString Conjugation::getAbbrev (int idx)
233 if (idx < numInternalNames() )
234 return names[idx].abbrev;
236 else if (idx < numTenses() ) {
237 QString s;
238 s.setNum(idx-numInternalNames()+1);
239 s.insert(0, UL_USER_TENSE);
240 return s;
243 else
244 return "";
248 int Conjugation::numInternalNames()
250 return sizeof(names) / sizeof(names[0]);
254 int Conjugation::numTenses()
256 return numInternalNames()+userTenses.size();
260 QString Conjugation::getType (int idx)
262 if (idx >= (int) conjugations.size() )
263 return "";
265 return conjugations[idx].type;
269 void Conjugation::setType (int idx, const QString & type)
271 if (idx >= (int) conjugations.size() )
272 return;
274 conjugations[idx].type = type;
278 void Conjugation::cleanUp ()
280 for (int i = (int)conjugations.size()-1; i >= 0; i--) {
281 const conjug_t *ctp = &conjugations[i];
282 if ( ctp->pers1_sing.stripWhiteSpace().isEmpty()
283 && ctp->pers2_sing.stripWhiteSpace().isEmpty()
284 && ctp->pers3_m_sing.stripWhiteSpace().isEmpty()
285 && ctp->pers3_f_sing.stripWhiteSpace().isEmpty()
286 && ctp->pers3_n_sing.stripWhiteSpace().isEmpty()
287 && ctp->pers1_plur.stripWhiteSpace().isEmpty()
288 && ctp->pers2_plur.stripWhiteSpace().isEmpty()
289 && ctp->pers3_m_plur.stripWhiteSpace().isEmpty()
290 && ctp->pers3_f_plur.stripWhiteSpace().isEmpty()
291 && ctp->pers3_n_plur.stripWhiteSpace().isEmpty()
293 conjugations.erase(conjugations.begin() + i);
298 bool Conjugation::isEmpty (int idx)
300 if (idx < (int) conjugations.size()) {
301 const conjug_t *ctp = &conjugations[idx];
302 return ctp->pers1_sing.stripWhiteSpace().isEmpty()
303 && ctp->pers2_sing.stripWhiteSpace().isEmpty()
304 && ctp->pers3_m_sing.stripWhiteSpace().isEmpty()
305 && ctp->pers3_f_sing.stripWhiteSpace().isEmpty()
306 && ctp->pers3_n_sing.stripWhiteSpace().isEmpty()
307 && ctp->pers1_plur.stripWhiteSpace().isEmpty()
308 && ctp->pers2_plur.stripWhiteSpace().isEmpty()
309 && ctp->pers3_m_plur.stripWhiteSpace().isEmpty()
310 && ctp->pers3_f_plur.stripWhiteSpace().isEmpty()
311 && ctp->pers3_n_plur.stripWhiteSpace().isEmpty();
313 return true;
317 #define _GET_CON_(elem, type, default) \
318 for (int i = 0; i < (int) conjugations.size(); i++) \
319 if (conjugations[i].type == type) \
320 return conjugations[i].elem; \
321 return default;
324 bool Conjugation::pers3SingularCommon(const QString &type) const
326 _GET_CON_(s3common, type, false);
330 bool Conjugation::pers3PluralCommon(const QString &type) const
332 _GET_CON_(p3common, type, false);
336 QString Conjugation::pers1Singular
337 (const QString &type) const
339 _GET_CON_(pers1_sing, type, "");
343 QString Conjugation::pers2Singular
344 (const QString &type) const
346 _GET_CON_(pers2_sing, type, "");
350 QString Conjugation::pers3FemaleSingular
351 (const QString &type) const
353 _GET_CON_(pers3_f_sing, type, "");
357 QString Conjugation::pers3MaleSingular
358 (const QString &type) const
360 _GET_CON_(pers3_m_sing, type, "");
364 QString Conjugation::pers3NaturalSingular
365 (const QString &type) const
367 _GET_CON_(pers3_n_sing, type, "");
371 QString Conjugation::pers1Plural
372 (const QString &type) const
374 _GET_CON_(pers1_plur, type, "");
378 QString Conjugation::pers2Plural
379 (const QString &type) const
381 _GET_CON_(pers2_plur, type, "");
385 QString Conjugation::pers3FemalePlural
386 (const QString &type) const
388 _GET_CON_(pers3_f_plur, type, "");
392 QString Conjugation::pers3MalePlural
393 (const QString &type) const
395 _GET_CON_(pers3_m_plur, type, "");
399 QString Conjugation::pers3NaturalPlural
400 (const QString &type) const
402 _GET_CON_(pers3_n_plur, type, "");
406 #undef _GET_CON_
409 #define _SET_CON_(elem, type, str) \
410 for (int i = 0; i < (int) conjugations.size(); i++) \
411 if (conjugations[i].type == type) { \
412 conjugations[i].elem = str; \
413 return; \
415 conjug_t ct; \
416 ct.type = type; \
417 ct.elem = str; \
418 conjugations.push_back(ct);
421 void Conjugation::setPers3PluralCommon(const QString &type, bool f)
423 _SET_CON_(p3common, type, f);
427 void Conjugation::setPers3SingularCommon(const QString &type, bool f)
429 _SET_CON_(s3common, type, f);
433 void Conjugation::setPers1Singular
434 (const QString &type, const QString &str)
436 _SET_CON_(pers1_sing, type, str);
440 void Conjugation::setPers2Singular
441 (const QString &type, const QString &str)
443 _SET_CON_(pers2_sing, type, str);
447 void Conjugation::setPers3FemaleSingular
448 (const QString &type, const QString &str)
450 _SET_CON_(pers3_f_sing, type, str);
454 void Conjugation::setPers3MaleSingular
455 (const QString &type, const QString &str)
457 _SET_CON_(pers3_m_sing, type, str);
461 void Conjugation::setPers3NaturalSingular
462 (const QString &type, const QString &str)
464 _SET_CON_(pers3_n_sing, type, str);
468 void Conjugation::setPers1Plural
469 (const QString &type, const QString &str)
471 _SET_CON_(pers1_plur, type, str);
475 void Conjugation::setPers2Plural
476 (const QString &type, const QString &str)
478 _SET_CON_(pers2_plur, type, str);
482 void Conjugation::setPers3FemalePlural
483 (const QString &type, const QString &str)
485 _SET_CON_(pers3_f_plur, type, str);
489 void Conjugation::setPers3MalePlural
490 (const QString &type, const QString &str)
492 _SET_CON_(pers3_m_plur, type, str);
496 void Conjugation::setPers3NaturalPlural
497 (const QString &type, const QString &str)
499 _SET_CON_(pers3_n_plur, type, str);
502 #undef _SET_CON_