moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kvoctrain / kvoctrain / docprop-dialogs / UsageOptPage.cpp
blob872517146c8f422cb237220f816a8ee7005a45c4
1 /***************************************************************************
3 usage labels dialog page
5 -----------------------------------------------------------------------
7 begin : Thu Mar 30 20:38:31 1999
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 "UsageOptPage.h"
28 #include <kapplication.h>
29 #include <kinputdialog.h>
30 #include <kmessagebox.h>
31 #include <klocale.h>
33 #include <qgroupbox.h>
34 #include <qpushbutton.h>
36 #include <QueryManager.h>
38 #define USAGE_TAG ". "
40 UsageOptPage::UsageOptPage
42 const vector<QString> &usages,
43 kvoctrainDoc *_doc,
44 QWidget *parent,
45 const char *name
48 UsageOptPageForm( parent, name ),
49 doc(_doc)
51 connect( b_cleanup, SIGNAL(clicked()), SLOT(slotCleanup()) );
52 connect( b_delete, SIGNAL(clicked()), SLOT(slotDeleteUsage()) );
53 connect( b_modify, SIGNAL(clicked()), SLOT(slotModifyUsage()) );
54 connect( b_new, SIGNAL(clicked()), SLOT(slotNewUsage()) );
55 connect( usageList, SIGNAL(highlighted(int)), SLOT(slotUsageChosen(int)) );
57 QString str;
58 for (int i = 0; i < (int) usages.size(); i++) {
59 str.setNum (i+1);
60 if (i <= 9)
61 str.insert (0, " ");
62 usageList->insertItem (str+USAGE_TAG+usages[i]);
63 usageIndex.push_back(i);
66 act_usage = 0;
67 if (usages.size() != 0)
68 usageList->setCurrentItem (act_usage);
70 b_modify->setEnabled(usageList->count() != 0);
71 b_delete->setEnabled(usageList->count() != 0);
73 usageList->setFocus();
77 void UsageOptPage::slotUsageChosen(int index)
79 act_usage = index;
83 void UsageOptPage::slotNewUsage()
85 bool ok;
86 QString getUsage = KInputDialog::getText(
87 i18n( "usage (area) of an expression", "Usage Description" ), i18n( "Enter usage description:" ), QString::null, &ok );
88 if( !ok )
89 return;
90 QString str;
91 int i = usageList->count()+1;
92 str.setNum (i);
93 if (i <= 9)
94 str.insert (0, " ");
95 usageList->insertItem (str+USAGE_TAG+getUsage.stripWhiteSpace());
96 usageIndex.push_back(-(i-1));
97 act_usage = usageList->count();
98 usageList->setCurrentItem (i-1);
99 b_modify->setEnabled(true);
100 b_delete->setEnabled(true);
104 void UsageOptPage::slotModifyUsage()
106 if (usageList->count() != 0
107 && (int) usageList->count() > act_usage) {
108 QString str = usageList->text (act_usage);
109 int pos = str.find (USAGE_TAG);
110 str.remove (0, pos+strlen (USAGE_TAG));
112 bool ok;
113 QString getUsage = KInputDialog::getText(
114 i18n( "usage (area) of an expression", "Usage Description" ), i18n( "Enter usage description:" ), str, &ok );
115 if( !ok )
116 return;
117 QString str2;
118 str2.setNum (act_usage+1);
119 if (act_usage <= 9)
120 str2.insert (0, " ");
121 usageList->changeItem (str2+USAGE_TAG+getUsage.stripWhiteSpace(), act_usage);
126 void UsageOptPage::updateListBox(int start)
128 QString str, str2;
129 for (int i = start; i < (int) usageList->count(); i++)
131 str = usageList->text (i);
132 int pos = str.find (USAGE_TAG);
133 str.remove (0, pos+strlen (USAGE_TAG));
134 str2.setNum (i+1);
135 if (i <= 9)
136 str2.insert (0, " ");
137 usageList->changeItem (str2+USAGE_TAG+str, i);
142 void UsageOptPage::slotDeleteUsage()
144 int act = act_usage;
145 if (usageList->count() != 0 && (int) usageList->count() > act)
147 QString t;
148 t.setNum(usageIndex[act_usage]+1);
149 t.insert (0, UL_USER_USAGE);
150 t += UL_USAGE_DIV;
151 for (int ent = 0; ent < doc->numEntries(); ent++) {
152 // FIXME: ProgressDlg here?
153 kvoctrainExpr *exp = doc->getEntry(ent);
154 for (int lang = 0; lang < doc->numLangs(); lang++) {
155 QString ul = exp->getUsageLabel(lang) + UL_USAGE_DIV;
156 if (ul.find(t) >= 0 ) {
157 KMessageBox::information(this,
158 i18n("usage (area) of an expression",
159 "This user-defined usage label could not be deleted "
160 "because it is in use."),
161 kapp->makeStdCaption(i18n("usage (area) of an expression",
162 "Deleting Usage Label")));
163 return;
168 usageList->removeItem (act);
169 usageIndex.erase (usageIndex.begin() + act);
171 if ((int) usageList->count() <= act)
172 act = usageList->count()-1;
173 else
174 updateListBox(act); // update items after current
176 if (act >= 0)
177 usageList->setCurrentItem (act);
179 b_modify->setEnabled(usageList->count() != 0);
180 b_delete->setEnabled(usageList->count() != 0);
184 void UsageOptPage::getUsageLabels (vector<QString> &ret_usage,
185 vector<int> &ret_Index)
187 QString str; ret_usage.clear();
188 for (int i = 0; i < (int) usageList->count(); i++) {
189 str = usageList->text(i);
190 int pos = str.find (USAGE_TAG);
191 str.remove (0, pos+strlen (USAGE_TAG));
192 ret_usage.push_back (str);
194 ret_Index = usageIndex;
198 void UsageOptPage::slotCleanup()
200 vector<bool> used_usage;
201 for (int i = 0; i <= (int) usageList->count(); i++)
202 used_usage.push_back(false);
204 for (int col = 0; col < doc->numLangs(); col++)
205 for (int i = 0; i < (int) doc->numEntries(); i++) {
206 QString t = doc->getEntry(i)->getUsageLabel(col);
207 QString n;
208 while (t.left(strlen(UL_USER_USAGE)) == UL_USER_USAGE) {
209 t.remove (0, 1);
211 int next;
212 if ((next = t.find(UL_USAGE_DIV)) >= 0) {
213 n = t.left(next);
214 t.remove (0, next+1);
216 else {
217 n = t;
218 t = "";
221 int idx = n.toInt();
222 if ((int) used_usage.size() < idx)
223 used_usage.resize(idx);
224 if (idx != 0)
225 used_usage[idx-1] = true ;
229 for (int i = used_usage.size()-1; i >= 0; i--) {
230 if (!used_usage[i]) {
231 for (int u = 0; u < (int) usageIndex.size() ; u++) {
232 if (usageIndex[u] == i || usageIndex[u] < 0) {
233 act_usage = i;
234 slotDeleteUsage();
235 break;
241 act_usage = 0;
242 usageList->setCurrentItem (act_usage);
246 void UsageOptPage::cleanUnused(kvoctrainDoc *doc,
247 const vector<int> &usageIndex,
248 int old_usages)
250 vector<int> translate_index;
251 vector<QString> new_usageStr;
253 /////////////////////////////////////////////////////
254 // translate_index contains new index number for each
255 // old index
256 for (int i = 0; i <= QMAX (old_usages, (int) usageIndex.size()); i++)
257 translate_index.push_back(0);
259 // now adjust lesson descriptions to new index
261 for (int i = 0; i < (int) usageIndex.size(); i++) {
262 if (usageIndex[i] >= 0)
263 translate_index[usageIndex[i]+1] = i+1;
266 // only keep remaining usage indices
268 // set usage index to 0 when not needed any more
269 // and translate to new index
271 for (int col = 0; col < doc->numLangs(); col++) {
272 for (int i = 0; i < doc->numEntries(); i++) {
273 QString t = doc->getEntry(i)->getUsageLabel (col);
274 if (!t.isEmpty() && t.left(strlen(UL_USER_USAGE)) == UL_USER_USAGE) {
275 QString tg;
276 while (t.left(strlen(UL_USER_USAGE)) == UL_USER_USAGE) {
277 QString n;
278 t.remove (0, 1);
279 int next;
280 if ((next = t.find(UL_USAGE_DIV)) >= 0) {
281 n = t.left(next);
282 t.remove (0, next+1);
284 else {
285 n = t;
286 t = "";
289 QString newusage;
290 int o = n.toInt();
291 if (translate_index[o] != 0) {
292 newusage.setNum (translate_index[o]);
293 newusage.insert (0, UL_USER_USAGE);
296 if (tg.length() == 0)
297 tg = newusage;
298 else
299 if (newusage.length() )
300 tg += UL_USAGE_DIV + newusage;
303 if (tg.length() == 0)
304 tg = t;
305 else if (t.length() != 0)
306 tg += UL_USAGE_DIV + t;
308 doc->getEntry(i)->setUsageLabel (col, tg);
314 #include "UsageOptPage.moc"