1 /* This file is part of KWordQuiz
2 Copyright (C) 2003 Peter Hedlund <peter@peterandlinda.com>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA.
19 #include <krandomsequence.h>
26 QPtrList
<WQListItem
> *WQQuiz::m_list
= 0L;
27 QPtrList
<WQListItem
> *WQQuiz::m_errorList
= 0L;
28 QPtrList
<WQListItem
> *WQQuiz::m_quizList
= 0L;
30 WQQuiz::WQQuiz(KWordQuizView
* parent
, const char *name
) : QObject(parent
, name
)
34 m_list
= new QPtrList
<WQListItem
>();
35 m_errorList
= new QPtrList
<WQListItem
>();
36 m_quizList
= new QPtrList
<WQListItem
>();
43 void WQQuiz::activateErrorList()
48 for (l
= m_errorList
->first(); l
; l
= m_errorList
->next())
54 m_questionCount
= m_list
->count();
58 void WQQuiz::activateBaseList()
68 for (l
= m_quizList
->first(); l
; l
= m_quizList
->next())
71 m_questionCount
= m_list
->count();
75 void WQQuiz::addToList(int aCol
, int bCol
)
77 //build a list of row numbers containing text in both columns
79 typedef QValueList
<int> IntList
;
81 for (int current
= 0; current
< m_table
->numRows(); ++current
)
83 if (!m_table
->text(current
, 0).isEmpty() && !m_table
->text(current
, 1).isEmpty())
85 tempList
.append(current
);
89 KRandomSequence
*rs
= new KRandomSequence(0);
91 int count
= tempList
.count();
93 IntList::ConstIterator it
;
94 for ( it
= tempList
.begin(); it
!= tempList
.end(); ++it
)
97 li
= new WQListItem();
98 li
->setQuestion(aCol
);
107 a
= rs
->getLong(count
); //rand() % count;
113 b
= rs
->getLong(count
); //rand() % count;
114 while(b
== *it
|| b
== a
/*|| b < 0*/);
119 m_quizList
->append(li
);
129 if (Prefs::enableBlanks())
131 result
= m_table
->checkSyntax(true, true);
171 addToList(aCol
, bCol
);
173 //check if enough in list
180 result
= (m_quizList
-> count() > 0);
183 result
= (m_quizList
-> count() > 0);
186 result
= (m_quizList
-> count() > 2);
199 addToList(aCol
, bCol
);
202 //Prepare final lists
207 void WQQuiz::listRandom()
209 KRandomSequence
*rs
= new KRandomSequence(0);
210 rs
->randomize(m_quizList
);
213 bool WQQuiz::checkAnswer(int i
, const QString
& a
)
216 WQListItem
*li
= m_list
->at(i
);
218 if (li
->question() == 0)
227 QString tTemp
= m_table
-> text(li
->oneOp(), j
);
228 tTemp
= tTemp
.stripWhiteSpace();
229 ans
= ans
.stripWhiteSpace();
231 if (m_quizType
== qtQA
)
233 if (QString(m_correctBlank
).length() > 0)
236 if (ans
.find(";") > 0)
237 ls
= QStringList::split(";", ans
);
241 if (m_correctBlank
.find(";") > 0)
242 la
= QStringList::split(";", m_correctBlank
);
244 la
.append(m_correctBlank
);
246 result
= (ls
.count() == la
.count());
249 for (uint counter
= 0; counter
< la
.count(); counter
++)
251 result
= (ls
[counter
].stripWhiteSpace() == la
[counter
].stripWhiteSpace());
259 result
= (ans
== tTemp
);
264 if (m_quizType
== qtMultiple
)
266 if (Prefs::enableBlanks())
271 result
= (ans
== tTemp
);
276 result
= (ans
== tTemp
);
283 m_errorList
-> append(li
);
289 QStringList
WQQuiz::multiOptions(int i
)
293 WQListItem
*li
= m_list
->at(i
);
295 typedef QPtrList
<QString
> LS
;
297 ls
= new QPtrList
<QString
>();
300 if (li
->question() == 0)
309 s
= new QString(m_table
->text(li
->oneOp(), j
));
310 if (Prefs::enableBlanks())
317 s
= new QString(m_table
->text(li
->twoOp(), j
));
318 if (Prefs::enableBlanks())
325 s
= new QString(m_table
->text(li
->threeOp(), j
));
326 if (Prefs::enableBlanks())
333 KRandomSequence
*rs
= new KRandomSequence(0);
338 Result
.append(*ls
->first());
344 QString
WQQuiz::quizIcon(int i
, QuizIcon ico
)
347 WQListItem
*li
= m_list
->at(i
);
348 if (ico
== qiLeftCol
)
350 if (li
->question() == 0)
356 if (ico
== qiRightCol
)
358 if (li
->question() == 0)
366 QString
WQQuiz::yourAnswer(int i
, const QString
& s
)
370 if (QString(m_answerBlank
).length() > 0)
375 ls
= QStringList::split(";", s
, true);
379 result
= m_answerBlank
.replace("..........", "<u></u>");
381 int offset
= 0, counter
= 0;
384 offset
= result
.find("<u>", offset
);
387 result
.insert(offset
+ 3, ls
[counter
]);
392 result
.append("</qt>");
393 result
.prepend("<qt>");
402 QString
WQQuiz::hint(int i
)
404 if (QString(m_correctBlank
).length() > 0)
406 return m_correctBlank
;
414 void WQQuiz::setQuizType(QuizType qt
)
419 void WQQuiz::setQuizMode(int qm
)
425 QString
WQQuiz::question(int i
)
427 WQListItem
*li
= m_list
->at(i
);
428 QString s
= m_table
->text(li
->oneOp(), li
->question());
429 if (Prefs::enableBlanks())
434 if (m_quizType
!= qtFlash
&& i
> 0)
436 WQListItem
*li2
= m_list
->at(i
- 1);
437 emit
checkingAnswer(li2
->oneOp());
440 emit
checkingAnswer(li
->oneOp());
445 QString
WQQuiz::blankAnswer(int i
)
453 if (m_quizType
== qtQA
&& Prefs::enableBlanks())
455 WQListItem
*li
= m_list
->at(i
);
457 if (li
->question() == 0)
465 tTemp
= m_table
->text(li
->oneOp(), j
);
469 rx
.setPattern("\\[.*\\]");
471 r
.replace(rx
, "..........");
479 offset
= rx
.search(tTemp
, offset
);
482 if (m_correctBlank
.length() > 0)
483 m_correctBlank
= m_correctBlank
+ ";" + " " + tTemp
.mid(offset
+ 1, tTemp
.find(']', offset
) - offset
- 1);
485 m_correctBlank
= tTemp
.mid(offset
+ 1, tTemp
.find(']', offset
) - offset
- 1);
491 return m_answerBlank
;
494 QString
WQQuiz::answer(int i
)
497 WQListItem
*li
= m_list
->at(i
);
499 if (li
->question() == 0)
509 if (m_quizType
== qtQA
)
511 s
= m_table
->text(li
->oneOp(), j
);
512 if (Prefs::enableBlanks())
514 s
.replace("[", "<u>");
515 s
.replace("]", "</u>");
522 s
= m_table
->text(li
->oneOp(), j
);
523 if (Prefs::enableBlanks())
532 QString
WQQuiz::langQuestion(int i
)
534 WQListItem
*li
= m_list
->at(i
);
535 return m_table
->horizontalHeader()->label(li
->question());
538 QString
WQQuiz::langAnswer(int i
)
541 WQListItem
*li
= m_list
->at(i
);
544 if (li
->question() == 0)
553 return m_table
->horizontalHeader()->label(j
);
556 int WQQuiz::kbAnswer(int i
)
558 /* WQListItem *li = m_list->at(i);
559 if (li->question() == 0)
561 //@todo return m_table ->layoutLeft();
565 //@todo return m_table -> layoutRight();
570 int WQQuiz::questionCount()
572 return m_questionCount
;
575 void WQQuiz::finish()
577 emit
checkingAnswer(-1);
580 #include "wqquiz.moc"