1 /***************************************************************************
3 lesson properties dialog page
5 -----------------------------------------------------------------------
7 begin : Thu Jun 3 22:03:50 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 "LessOptPage.h"
28 #include <kapplication.h>
29 #include <kinputdialog.h>
31 #include <kmessagebox.h>
33 #include <qcombobox.h>
34 #include <qpushbutton.h>
37 #include <kvoctraindoc.h>
41 LessOptPage::LessOptPage
49 LessOptPageForm( parent
, name
)
51 connect( lessonList
, SIGNAL(highlighted(int)), SLOT(slotLessonChosen(int)) );
52 connect( b_new
, SIGNAL(clicked()), SLOT(slotNewLesson()) );
53 connect( b_modify
, SIGNAL(clicked()), SLOT(slotModifyLesson()) );
54 connect( b_delete
, SIGNAL(clicked()), SLOT(slotDeleteLesson()) );
55 connect( b_cleanup
, SIGNAL(clicked()), SLOT(slotCleanup()) );
60 for (int i
= 1; i
< lessons
->count(); i
++) {
64 lessonList
->insertItem (str
+LESS_TAG
+lessons
->text(i
));
65 lessonIndex
.push_back(i
-1);
69 if (lessonList
->count() != 0)
70 lessonList
->setCurrentItem (act_lesson
);
71 lessonList
->setFocus();
73 b_modify
->setEnabled(lessonList
->count() != 0);
74 b_delete
->setEnabled(lessonList
->count() != 0);
78 void LessOptPage::slotLessonChosen(int index
)
84 void LessOptPage::slotNewLesson()
87 QString getLesson
= KInputDialog::getText(
88 i18n( "Lesson Description" ), i18n( "Enter lesson description:" ), QString::null
, &ok
);
92 int i
= lessonList
->count()+1;
96 lessonList
->insertItem (str
+LESS_TAG
+getLesson
.stripWhiteSpace());
97 lessonIndex
.push_back(-(i
-1));
98 act_lesson
= lessonList
->count();
99 lessonList
->setCurrentItem (i
-1);
100 b_modify
->setEnabled(true);
101 b_delete
->setEnabled(true);
105 void LessOptPage::slotModifyLesson()
107 if (lessonList
->count() != 0 && (int) lessonList
->count() > act_lesson
)
109 QString str
= lessonList
->text (act_lesson
);
110 int pos
= str
.find (LESS_TAG
);
111 str
.remove (0, pos
+strlen (LESS_TAG
));
113 QString getLesson
= KInputDialog::getText(
114 i18n( "Lesson Description" ), i18n( "Enter lesson description:" ), str
, &ok
);
118 str2
.setNum (act_lesson
+1);
120 str2
.insert (0, " ");
121 lessonList
->changeItem (str2
+LESS_TAG
+getLesson
.stripWhiteSpace(), act_lesson
);
126 void LessOptPage::updateListBox(int start
)
129 for (int i
= start
; i
< (int) lessonList
->count(); i
++)
131 str
= lessonList
->text (i
);
132 int pos
= str
.find (LESS_TAG
);
133 str
.remove (0, pos
+strlen (LESS_TAG
));
136 str2
.insert (0, " ");
137 lessonList
->changeItem (str2
+LESS_TAG
+str
, i
);
142 void LessOptPage::slotDeleteLesson()
144 int act
= act_lesson
;
145 if (lessonList
->count() != 0
146 && (int) lessonList
->count() > act
) {
148 for (int ent
= 0; ent
< doc
->numEntries(); ent
++) {
149 // FIXME: ProgressDlg here?
150 if (doc
->getEntry(ent
)->getLesson() == lessonIndex
[act_lesson
]+1) {
151 KMessageBox::information(this,
152 i18n("This lesson could not be deleted\nbecause it is in use."),
153 kapp
->makeStdCaption(i18n("Deleting Lesson")));
158 lessonList
->removeItem (act
);
159 lessonIndex
.erase (lessonIndex
.begin() + act
);
161 if ((int) lessonList
->count() <= act
)
162 act
= lessonList
->count()-1;
164 updateListBox(act
); // update items after current
167 lessonList
->setCurrentItem (act
);
169 b_modify
->setEnabled(lessonList
->count() != 0);
170 b_delete
->setEnabled(lessonList
->count() != 0);
174 void LessOptPage::getLesson (QComboBox
*ret_lesson
, vector
<int> &ret_Index
)
176 while (ret_lesson
->count() > 1) /* first entry is "no lesson" */
177 ret_lesson
->removeItem (1);
180 for (int i
= 0; i
< (int) lessonList
->count(); i
++) {
181 str
= lessonList
->text(i
);
182 int pos
= str
.find (LESS_TAG
);
183 str
.remove (0, pos
+strlen (LESS_TAG
));
184 ret_lesson
->insertItem (str
);
186 ret_Index
= lessonIndex
;
190 void LessOptPage::slotCleanup()
192 vector
<bool> used_lesson
;
193 for (int i
= 0; i
< (int) lessonList
->count(); i
++)
194 used_lesson
.push_back(false);
196 for (int i
= 0; i
< (int) doc
->numEntries(); i
++) {
197 int idx
= doc
->getEntry(i
)->getLesson();
198 if ((int) used_lesson
.size() < idx
)
199 used_lesson
.resize(idx
);
201 used_lesson
[idx
-1] = true ;
204 for (int i
= used_lesson
.size()-1; i
>= 0; i
--)
205 if (!used_lesson
[i
]) {
206 for (int u
= 0; u
< (int) lessonIndex
.size() ; u
++) {
207 if (lessonIndex
[u
] == i
|| lessonIndex
[u
] < 0) {
216 lessonList
->setCurrentItem (act_lesson
);
220 void LessOptPage::cleanUnused (kvoctrainDoc
*doc
,
221 const QComboBox
* /*lessons*/,
222 const vector
<int> &lessonIndex
,
224 vector
<int> &lessons_in_query
)
226 vector
<int> translate_index
;
228 /////////////////////////////////////////////////////
229 // translate_index contains new index number for each
231 for (int i
= 0; i
<= QMAX (old_lessons
, (int) lessonIndex
.size()); i
++)
232 translate_index
.push_back(0);
234 // now adjust lesson descriptions to new index
235 for (int i
= 0; i
< (int) lessonIndex
.size(); i
++) {
236 if (lessonIndex
[i
] >= 0)
237 translate_index
[lessonIndex
[i
]+1] = i
+1;
240 // now adjust for query selected lessons to new index
241 for (int i
= (int) lessons_in_query
.size()-1; i
>= 0; i
--) {
242 if (translate_index
[lessons_in_query
[i
]] > 0)
243 lessons_in_query
[i
] = translate_index
[lessons_in_query
[i
]];
245 lessons_in_query
.erase(lessons_in_query
.begin() + i
);
248 // only keep remaining lesson member indices
250 // set lesson index to 0 when not needed any more
251 // and translate to new index
252 for (int i
= 0; i
< doc
->numEntries(); i
++) {
253 if (doc
->getEntry(i
)->getLesson () != 0)
254 doc
->getEntry(i
)->setLesson (translate_index
[doc
->getEntry(i
)->getLesson ()]);
257 #include "LessOptPage.moc"