moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kvoctrain / kvoctrain / docprop-dialogs / TenseOptPage.cpp
blob1956fa5325178802a23e78c19b9c96e260887456
1 /***************************************************************************
3 user tense options dialog page
5 -----------------------------------------------------------------------
7 begin : Sun May 28 12:14:31 2000
9 copyright : (C) 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
10 (C) 2001 The KDE-EDU team
11 (C) 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 "TenseOptPage.h"
28 #include <qpushbutton.h>
30 #include <kapplication.h>
31 #include <kinputdialog.h>
32 #include <kmessagebox.h>
33 #include <klocale.h>
35 #include <QueryManager.h>
37 #define TENSE_TAG ". "
39 TenseOptPage::TenseOptPage
41 const vector<QString> &tenses,
42 kvoctrainDoc *_doc,
43 QWidget *parent,
44 const char *name
47 TenseOptPageForm( parent, name )
49 connect( tenseList, SIGNAL(highlighted(int)), SLOT(slotTenseChosen(int)) );
50 connect( b_cleanup, SIGNAL(clicked()), SLOT(slotCleanup()) );
51 connect( b_delete, SIGNAL(clicked()), SLOT(slotDeleteTense()) );
52 connect( b_modify, SIGNAL(clicked()), SLOT(slotModifyTense()) );
53 connect( b_new, SIGNAL(clicked()), SLOT(slotNewTense()) );
55 QString str;
56 for (int i = 0; i < (int) tenses.size(); i++) {
57 str.setNum (i+1);
58 if (i <= 9)
59 str.insert (0, " ");
60 tenseList->insertItem (str+TENSE_TAG+tenses[i]);
61 tenseIndex.push_back(i);
64 act_tense = 0;
65 if (tenses.size() != 0)
66 tenseList->setCurrentItem (act_tense);
68 b_modify->setEnabled(tenseList->count() != 0);
69 b_delete->setEnabled(tenseList->count() != 0);
71 tenseList->setFocus();
72 doc = _doc;
76 void TenseOptPage::slotTenseChosen(int index)
78 act_tense = index;
82 void TenseOptPage::slotNewTense()
84 bool ok;
85 QString getTense = KInputDialog::getText(
86 i18n( "Tense Description" ), i18n( "Enter tense description:" ), QString::null, &ok );
87 if( !ok )
88 return;
90 QString str;
91 int i = tenseList->count()+1;
92 str.setNum (i);
93 if (i <= 9)
94 str.insert (0, " ");
95 tenseList->insertItem (str+TENSE_TAG+getTense.stripWhiteSpace());
96 tenseIndex.push_back(-(i-1));
97 act_tense = tenseList->count();
98 tenseList->setCurrentItem (i-1);
99 b_modify->setEnabled(true);
100 b_delete->setEnabled(true);
104 void TenseOptPage::slotModifyTense()
106 if (tenseList->count() != 0 && (int) tenseList->count() > act_tense)
108 QString str = tenseList->text (act_tense);
109 int pos = str.find (TENSE_TAG);
110 str.remove (0, pos+strlen (TENSE_TAG));
111 bool ok;
112 QString getTense = KInputDialog::getText(
113 i18n( "Tense Description" ), i18n( "Enter tense description:" ), str, &ok );
114 if( !ok )
115 return;
116 QString str2;
117 str2.setNum (act_tense+1);
118 if (act_tense <= 9)
119 str2.insert (0, " ");
120 tenseList->changeItem (str2+TENSE_TAG+getTense.stripWhiteSpace(), act_tense);
125 void TenseOptPage::updateListBox(int start)
127 QString str, str2;
128 for (int i = start; i < (int) tenseList->count(); i++)
130 str = tenseList->text (i);
131 int pos = str.find (TENSE_TAG);
132 str.remove (0, pos+strlen (TENSE_TAG));
133 str2.setNum (i+1);
134 if (i <= 9)
135 str2.insert (0, " ");
136 tenseList->changeItem (str2+TENSE_TAG+str, i);
141 void TenseOptPage::slotDeleteTense()
143 int act = act_tense;
144 if (tenseList->count() != 0
145 && (int) tenseList->count() > act) {
147 QString t;
148 t.setNum(tenseIndex[act_tense]+1);
149 t.insert (0, QM_USER_TYPE);
150 for (int ent = 0; ent < doc->numEntries(); ent++) {
151 // FIXME: ProgressDlg here?
152 kvoctrainExpr *exp = doc->getEntry(ent);
153 for (int lang = 0; lang < doc->numLangs(); lang++) {
154 Conjugation conj = exp->getConjugation(lang);
155 for (int con = 0; con < conj.numEntries(); con++ ) {
156 if (conj.getType(con) == t) {
157 KMessageBox::information(this,
158 i18n("This user defined tense could not be deleted\nbecause it is in use."),
159 kapp->makeStdCaption(i18n("Deleting Tense Description")));
160 return;
166 tenseList->removeItem (act);
167 tenseIndex.erase (tenseIndex.begin() + act);
169 if ((int) tenseList->count() <= act)
170 act = tenseList->count()-1;
171 else
172 updateListBox(act); // update items after current
174 if (act >= 0)
175 tenseList->setCurrentItem (act);
177 b_modify->setEnabled(tenseList->count() != 0);
178 b_delete->setEnabled(tenseList->count() != 0);
182 void TenseOptPage::getTenseNames (vector<QString> &ret_tense, vector<int> &ret_Index)
184 QString str; ret_tense.clear();
185 for (int i = 0; i < (int) tenseList->count(); i++)
187 str = tenseList->text(i);
188 int pos = str.find (TENSE_TAG);
189 str.remove (0, pos+strlen (TENSE_TAG));
190 ret_tense.push_back (str);
192 ret_Index = tenseIndex;
196 void TenseOptPage::slotCleanup()
198 vector<bool> used_tense;
199 for (int i = 0; i <= (int) tenseList->count(); i++)
200 used_tense.push_back(false);
202 for (int col = 0; col < doc->numLangs(); col++)
203 for (int i = 0; i < (int) doc->numEntries(); i++) {
204 Conjugation conj = doc->getEntry(i)->getConjugation(col);
205 for (int ci = 0; ci < conj.numEntries(); ci++) {
206 QString t = conj.getType(ci);
207 if (t.left(strlen(UL_USER_TENSE)) == UL_USER_TENSE) {
208 t.remove (0, strlen(UL_USER_TENSE));
209 int idx = t.toInt();
210 if ((int) used_tense.size() < idx)
211 used_tense.resize(idx);
212 if (idx != 0)
213 used_tense[idx-1] = true;
218 for (int i = used_tense.size()-1; i >= 0; i--)
219 if (!used_tense[i]) {
220 for (int u = 0; u < (int) tenseIndex.size() ; u++) {
221 if (tenseIndex[u] == i || tenseIndex[u] < 0) {
222 act_tense = i;
223 slotDeleteTense();
224 break;
229 act_tense = 0;
230 tenseList->setCurrentItem (act_tense);
234 void TenseOptPage::cleanUnused(kvoctrainDoc *doc, const vector<int> &tenseIndex, int old_tenses)
236 vector<int> translate_index;
237 vector<QString> new_tenseStr;
239 /////////////////////////////////////////////////////
240 // translate_index contains new index number for each
241 // old index
242 for (int i = 0; i <= QMAX (old_tenses, (int) tenseIndex.size()); i++)
243 translate_index.push_back(0);
245 // now adjust lesson descriptions to new index
247 for (int i = 0; i < (int) tenseIndex.size(); i++) {
248 if (tenseIndex[i] >= 0)
249 translate_index[tenseIndex[i]+1] = i+1;
252 // only keep remaining tense indices
254 // set tense index to 0 when not needed any more
255 // and translate to new index
257 for (int col = 0; col < doc->numLangs(); col++) {
258 for (int i = 0; i < doc->numEntries(); i++) {
259 Conjugation conj = doc->getEntry(i)->getConjugation (col);
260 bool dirty = false;
261 for (int ci = 0; ci < conj.numEntries(); ci++) {
262 QString old = conj.getType(ci);
263 if (!old.isEmpty() && old.left(strlen(QM_USER_TYPE)) == QM_USER_TYPE) {
264 old.remove (0, 1);
265 int o = old.toInt();
267 dirty = true;
268 QString newtense;
269 if (translate_index[o] != 0) {
270 newtense.setNum (translate_index[o]);
271 newtense.insert (0, QM_USER_TYPE);
272 conj.setType(ci, newtense);
274 else
275 conj.setType(ci, "");
278 if (dirty)
279 doc->getEntry(i)->setConjugation (col, conj);
284 #include "TenseOptPage.moc"