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 /***************************************************************************
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. *
24 ***************************************************************************/
26 #include "UsageOptPage.h"
28 #include <kapplication.h>
29 #include <kinputdialog.h>
30 #include <kmessagebox.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
,
48 UsageOptPageForm( parent
, name
),
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)) );
58 for (int i
= 0; i
< (int) usages
.size(); i
++) {
62 usageList
->insertItem (str
+USAGE_TAG
+usages
[i
]);
63 usageIndex
.push_back(i
);
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
)
83 void UsageOptPage::slotNewUsage()
86 QString getUsage
= KInputDialog::getText(
87 i18n( "usage (area) of an expression", "Usage Description" ), i18n( "Enter usage description:" ), QString::null
, &ok
);
91 int i
= usageList
->count()+1;
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
));
113 QString getUsage
= KInputDialog::getText(
114 i18n( "usage (area) of an expression", "Usage Description" ), i18n( "Enter usage description:" ), str
, &ok
);
118 str2
.setNum (act_usage
+1);
120 str2
.insert (0, " ");
121 usageList
->changeItem (str2
+USAGE_TAG
+getUsage
.stripWhiteSpace(), act_usage
);
126 void UsageOptPage::updateListBox(int start
)
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
));
136 str2
.insert (0, " ");
137 usageList
->changeItem (str2
+USAGE_TAG
+str
, i
);
142 void UsageOptPage::slotDeleteUsage()
145 if (usageList
->count() != 0 && (int) usageList
->count() > act
)
148 t
.setNum(usageIndex
[act_usage
]+1);
149 t
.insert (0, UL_USER_USAGE
);
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")));
168 usageList
->removeItem (act
);
169 usageIndex
.erase (usageIndex
.begin() + act
);
171 if ((int) usageList
->count() <= act
)
172 act
= usageList
->count()-1;
174 updateListBox(act
); // update items after current
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
);
208 while (t
.left(strlen(UL_USER_USAGE
)) == UL_USER_USAGE
) {
212 if ((next
= t
.find(UL_USAGE_DIV
)) >= 0) {
214 t
.remove (0, next
+1);
222 if ((int) used_usage
.size() < idx
)
223 used_usage
.resize(idx
);
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) {
242 usageList
->setCurrentItem (act_usage
);
246 void UsageOptPage::cleanUnused(kvoctrainDoc
*doc
,
247 const vector
<int> &usageIndex
,
250 vector
<int> translate_index
;
251 vector
<QString
> new_usageStr
;
253 /////////////////////////////////////////////////////
254 // translate_index contains new index number for each
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
) {
276 while (t
.left(strlen(UL_USER_USAGE
)) == UL_USER_USAGE
) {
280 if ((next
= t
.find(UL_USAGE_DIV
)) >= 0) {
282 t
.remove (0, next
+1);
291 if (translate_index
[o
] != 0) {
292 newusage
.setNum (translate_index
[o
]);
293 newusage
.insert (0, UL_USER_USAGE
);
296 if (tg
.length() == 0)
299 if (newusage
.length() )
300 tg
+= UL_USAGE_DIV
+ newusage
;
303 if (tg
.length() == 0)
305 else if (t
.length() != 0)
306 tg
+= UL_USAGE_DIV
+ t
;
308 doc
->getEntry(i
)->setUsageLabel (col
, tg
);
314 #include "UsageOptPage.moc"