moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kwordquiz / src / kwordquizview.cpp
blob46796fc7975e5959f55aa6b16b3671f1da3fbd6b
1 /***************************************************************************
2 kwordquizview.cpp - description
3 -------------------
4 begin : Wed Jul 24 20:12:30 PDT 2002
5 copyright : (C) 2002 by Peter Hedlund
6 email : peter@peterandlinda.com
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 // include files for Qt
19 #include <qpainter.h>
20 #include <qtable.h>
21 #include <qclipboard.h>
22 #include <qlineedit.h>
24 // include files for KDE
25 #include <klocale.h> //i18n
26 #include <kmessagebox.h> // always useful
27 #include <krandomsequence.h>
28 #include <kglobalsettings.h>
29 #include <knotifyclient.h>
31 // application specific includes
32 #include "kwordquizview.h"
33 #include "kwordquizdoc.h"
34 #include "kwordquiz.h"
35 #include "dlgsort.h"
36 #include "prefs.h"
37 #include "dlgrc.h"
38 #include "dlgspecchar.h"
40 QValueList<WQUndo> *KWordQuizView::m_undoList = 0L;
42 KWordQuizView::KWordQuizView(QWidget *parent, const char *name) : QTable(parent, name)
44 if(!m_undoList)
45 m_undoList = new QValueList<WQUndo>();
47 setNumCols(2);
48 setNumRows(50);
49 setColumnWidth(1, 250);
50 setColumnWidth(0, 250);
51 setSelectionMode(QTable::Single);
52 horizontalHeader()->setLabel(0, i18n("Column 1"));
53 horizontalHeader()->setLabel(1, i18n("Column 2"));
54 setMinimumSize(0, 0); //This seems to improve resizing of main window
55 dlgSpecChar = 0;
58 KWordQuizView::~KWordQuizView()
62 KWordQuizDoc *KWordQuizView::getDocument() const
64 KWordQuizApp *theApp=(KWordQuizApp *) parentWidget();
65 return theApp->getDocument();
68 void KWordQuizView::print(KPrinter *pPrinter)
70 QPainter painter;
72 //type 0 Vocabulary list
73 //type 1 Vocabulary exam
74 //type 2 Flashcards
75 int type = pPrinter->option("kde-kwordquiz-type").toInt();
77 //I think working with screen resolution is enough for our purposes
78 int res = pPrinter->resolution();
79 int pad = 2;
80 int marg = res;
81 int card_marg = res / 2;
82 int card_width = 5 * res;
83 int card_height = 3 * res;
84 int card_text_marg = res /5;
85 int card_line_top = 30;
87 if (type == 2)
88 pPrinter->setOrientation(KPrinter::Landscape);
90 painter.begin(pPrinter);
92 int pageNum = 1;
94 int cw0 = verticalHeader()->width();
95 int cw1 = columnWidth(0);
96 int cw2 = columnWidth(1);
97 int cw3 = 0;
101 if (type == 1)
102 cw3 = 50;
104 int gridWidth = cw0 + cw1 + cw2 + cw3;
105 int lPos = marg;
106 int tPos = marg + horizontalHeader()->height();
108 QRect w = painter.window();
110 doNewPage(painter, res, type);
112 if (type == 2)
114 tPos = card_marg;
115 for (int rc = 0; rc < numRows(); ++rc)
118 //draw rectangle 2 cards wide
119 painter.drawRect(card_marg, tPos, 2 * card_width, card_height);
120 //divide into 2 cards with line
121 painter.drawLine(card_marg + card_width, tPos, card_marg + card_width, tPos + card_height);
122 //draw line inside card
123 painter.drawLine(card_marg + card_text_marg, tPos + card_line_top,
124 card_marg + card_width - card_text_marg, tPos + card_line_top);
125 painter.drawLine(card_marg + card_width + card_text_marg, tPos + card_line_top,
126 card_marg + card_width + card_width - card_text_marg, tPos + card_line_top);
127 //draw headers
128 painter.setFont(KGlobalSettings::generalFont());
129 painter.drawText(card_marg + card_text_marg, tPos, card_width, card_line_top, AlignAuto | AlignVCenter, horizontalHeader()->label(0));
130 painter.drawText(card_marg + card_width + card_text_marg, tPos, card_width, card_line_top, AlignAuto | AlignVCenter, horizontalHeader()->label(1));
131 //draw text
132 painter.setFont(font());
133 painter.drawText(card_marg + card_text_marg, tPos + card_line_top, card_width - (2 * card_text_marg), card_height - card_line_top, AlignHCenter | AlignVCenter, text(rc, 0));
134 painter.drawText(card_marg + card_width + card_text_marg, tPos + card_line_top, card_width - (2 * card_text_marg), card_height - card_line_top, AlignHCenter | AlignVCenter, text(rc, 1));
136 tPos = tPos + card_height + card_line_top;
138 if (tPos + card_height + card_line_top > w.height() - card_marg)
140 doEndOfPage(painter, tPos, pageNum++, res, type);
141 tPos = card_marg;
142 pPrinter->newPage();
143 doNewPage(painter, res, type);
148 else
151 for (int rc = 0; rc < numRows(); ++rc)
153 painter.drawLine(lPos, tPos, lPos + gridWidth, tPos);
154 painter.setFont(KGlobalSettings::generalFont());
156 painter.drawText(lPos, tPos, cw0 - pad, rowHeight(rc), AlignRight | AlignVCenter, QString::number(rc + 1));
158 painter.setFont(font());
159 painter.drawText(lPos + cw0 + pad, tPos, cw1, rowHeight(rc), AlignAuto | AlignVCenter, text(rc, 0));
161 if (type == 0)
162 painter.drawText(lPos + cw0 + cw1 + pad, tPos, cw2, rowHeight(rc), AlignAuto | AlignVCenter, text(rc, 1));
164 tPos = tPos + rowHeight(rc);
166 if (tPos + rowHeight(rc + 1) > w.height() - marg)
168 doEndOfPage(painter, tPos, pageNum++, res, type);
169 tPos = marg + horizontalHeader()->height();
170 pPrinter->newPage();
171 doNewPage(painter, res, type);
175 doEndOfPage(painter, tPos, pageNum++, res, type);
176 painter.end();
179 void KWordQuizView::doNewPage( QPainter & painter, int res, int type )
181 int cw0 = verticalHeader()->width();
182 int cw1 = columnWidth(0);
183 int cw2 = columnWidth(1);
184 int cw3 = 0;
185 int marg = res;
186 int card_marg = res / 2;
187 int pad = 2;
189 if (type == 1)
190 cw3 = 50;
192 QRect w = painter.window();
194 painter.setFont(KGlobalSettings::generalFont());
196 if (type == 2)
198 painter.drawText(card_marg, card_marg - 20, i18n("KWordQuiz - %1").arg(getDocument()->URL().fileName()));
199 return;
201 painter.drawLine(marg, marg, marg + cw0 + cw1 + cw2 + cw3, marg);
203 painter.drawText(marg, marg - 20, i18n("KWordQuiz - %1").arg(getDocument()->URL().fileName()));
205 if (type == 1)
207 QString score = i18n("Name:_____________________________ Date:__________");
208 QRect r = painter.boundingRect(0, 0, 0, 0, AlignAuto, score);
209 painter.drawText(w.width() - r.width() - marg, marg - 20, score);
212 painter.drawText(marg, marg, cw0, horizontalHeader()->height(), AlignRight | AlignVCenter, "");
214 painter.drawText(marg + cw0 + pad, marg, cw1, horizontalHeader()->height(), AlignAuto | AlignVCenter, horizontalHeader()->label(0));
215 painter.drawText(marg + cw0 + cw1 + pad, marg, cw2, horizontalHeader()->height(), AlignAuto | AlignVCenter, horizontalHeader()->label(1));
217 if (type == 1)
218 painter.drawText(marg + cw0 + cw1 + cw2 + pad, marg, cw3, horizontalHeader()->height(), AlignAuto | AlignVCenter, i18n("Score"));
222 void KWordQuizView::doEndOfPage( QPainter & painter, int vPos, int pageNum, int res, int type )
224 int marg = res;
225 painter.setFont(KGlobalSettings::generalFont());
226 QRect w = painter.window();
227 QRect r = painter.boundingRect(0, 0, 0, 0, AlignAuto, QString::number(pageNum));
228 painter.drawText((w.width()/2) - (r.width()/2), w.height() - marg + 20, QString::number(pageNum));
230 if (type == 2)
231 return;
233 int cw0 = verticalHeader()->width();
234 int cw1 = columnWidth(0);
235 int cw2 = columnWidth(1);
236 int cw3 = 0;
238 if (type == 1)
239 cw3 = 50;
241 //Last horizontal line
242 painter.drawLine(marg, vPos, marg + cw0 + cw1 + cw2 + cw3, vPos);
243 //Four vertical lines
244 painter.drawLine(marg, marg, marg, vPos);
245 painter.drawLine(marg + cw0, marg, marg + cw0, vPos);
246 painter.drawLine(marg + cw0 + cw1, marg, marg + cw0 + cw1, vPos);
247 painter.drawLine(marg + cw0 + cw1 + cw2, marg, marg + cw0 + cw1 + cw2, vPos);
249 if (type == 1)
250 painter.drawLine(marg + cw0 + cw1 + cw2 + cw3, marg, marg + cw0 + cw1 + cw2 + cw3, vPos);
257 \fn KWordQuizView::gridIsEmpty()
259 bool KWordQuizView::gridIsEmpty()
261 for(int i = 0; i < numRows(); i++){
262 for(int j = 0; j < 2; j++){
263 if(!text(i, j).isEmpty()){
264 return false;
268 return true;
271 QWidget * KWordQuizView::beginEdit( int row, int col, bool replace )
273 m_currentText = text(row, col);
274 cellEditor = QTable::beginEdit(row, col, replace);
275 cellEditor->installEventFilter(this);
276 return cellEditor;
279 void KWordQuizView::endEdit( int row, int col, bool accept, bool replace )
281 // this code gets called after enter and arrow keys, now we
282 // only process if editing really has been done
284 if (cellWidget(row, col) != 0) //if edited the cellWidget still exists
286 if (((QLineEdit *) cellWidget(row, col))->text() != m_currentText)
287 addUndo(i18n("&Undo Entry"));
288 QTable::endEdit(row, col, accept, replace); //this will destroy the cellWidget
289 if (!text(row, col).isEmpty())
291 QTableItem* itm;
292 itm = item(row, col);
293 itm->setWordWrap(true);
294 adjustRow(row);
295 getDocument() -> setModified(true);
296 if (Prefs::enableBlanks())
297 if (!checkForBlank(text(row, col), true))
298 KNotifyClient::event(winId(), "SyntaxError", i18n("There is an error with the Fill-in-the-blank brackets"));
303 void KWordQuizView::adjustRow( int row )
305 // we want to make the row high enough to display content, but
306 // if the user already made it even higher we keep that height
307 int rh = rowHeight(row);
308 QTable::adjustRow(row);
309 if (rh > rowHeight(row))
310 setRowHeight(row, rh);
313 void KWordQuizView::saveCurrentSelection(bool clear = true)
316 m_currentRow = currentRow();
317 m_currentCol = currentColumn();
319 if (numSelections() > 0)
321 m_currentSel = selection(0);
322 if (clear)
323 clearSelection();
325 else
327 QTableSelection* ts;
328 ts = new QTableSelection(m_currentRow, m_currentCol, m_currentRow, m_currentCol);
329 m_currentSel = *ts;
333 void KWordQuizView::doEditUndo( )
335 if (isEditing())
337 if (((QLineEdit *) cellWidget(currentRow(), currentColumn()))->isUndoAvailable())
338 ((QLineEdit *) cellWidget(currentRow(), currentColumn()))->undo();
340 else
342 WQUndo undo;
343 if (m_undoList->count() > 0)
345 setUpdatesEnabled(false);
346 undo = m_undoList->first();
347 setFont(undo.font());
348 verticalHeader()->setMinimumWidth(undo.colWidth0());
349 setColumnWidth(0, undo.colWidth1());
350 setColumnWidth(1, undo.colWidth2());
351 setNumRows(0);
352 setNumRows(undo.numRows());
353 setCurrentCell(undo.currentRow(), undo.currentCol());
354 addSelection(undo.selection());
356 QString s;
357 int i = 0;
358 KWqlDataItemList dataList = undo.list();
359 KWqlDataItemList::ConstIterator end(dataList.end());
360 for(KWqlDataItemList::ConstIterator dataIt = dataList.begin(); dataIt != end; ++dataIt)
362 s = (*dataIt).frontText();
363 if (!s.isEmpty())
364 setText(i, 0, s); //calling setText only when there actually is text helps with sorting
365 s = (*dataIt).backText();
366 if (!s.isEmpty())
367 setText(i, 1, s);
368 setRowHeight(i, (*dataIt).rowHeight());
369 i++;
372 m_undoList->remove(m_undoList->begin());
373 setUpdatesEnabled(true);
376 if (m_undoList->count() > 0)
378 undo = m_undoList->first();
379 emit undoChange(undo.text(), true);
381 else
382 emit undoChange(i18n("Cannot &Undo"), false);
386 void KWordQuizView::doEditCut( )
388 if (isEditing())
390 ((QLineEdit *) cellWidget(currentRow(), currentColumn()))->cut();
392 else
394 addUndo(i18n("&Undo Cut"));
395 doEditCopy();
396 saveCurrentSelection();
397 for (int r = m_currentSel.topRow(); r <= m_currentSel.bottomRow(); ++r)
398 for(int c = m_currentSel.leftCol(); c <= m_currentSel.rightCol(); ++c)
399 clearCell(r, c);
401 getDocument()->setModified(true);
404 void KWordQuizView::doEditCopy( )
406 bool mod = getDocument()->isModified();
407 if (isEditing())
409 ((QLineEdit *) cellWidget(currentRow(), currentColumn()))->copy();
411 else
413 saveCurrentSelection(false);
414 QString s;
415 for (int r = m_currentSel.topRow(); r <= m_currentSel.bottomRow(); ++r)
417 for (int c = m_currentSel.leftCol(); c <= m_currentSel.rightCol(); ++c)
418 s = s + text(r, c) + "\t";
419 s = s + "\n";
421 kapp->clipboard()->setText(s);
423 getDocument()->setModified(mod);
426 void KWordQuizView::doEditPaste( )
428 if (isEditing())
430 ((QLineEdit *) cellWidget(currentRow(), currentColumn()))->paste();
432 else
434 addUndo(i18n("&Undo Paste"));
435 saveCurrentSelection();
436 int tr = m_currentSel.topRow();
437 int br = m_currentSel.bottomRow();
438 int lc = m_currentSel.leftCol();
439 int rc = m_currentSel.rightCol();
441 if (lc == rc && tr == br) //one cell selected
443 QString s = kapp->clipboard()->text();
444 QStringList sl;
445 sl = QStringList::split("\n", s);
447 uint r = numRows() - tr;
449 if (sl.count() > r) //do we need to add rows?
451 setNumRows(numRows() + (sl.count() - r));
452 br = numRows();
454 else
455 br= br + sl.count() - 1;
457 if (lc == 0) //left col?
458 if (sl[0].find("\t") < ((int) sl[0].length() - 1))
459 rc = 1; //expand to second column;
461 uint i = 0;
462 int ar = tr;
463 QStringList sr;
464 while(i < sl.count() && br <= numRows() )
466 int ac = lc;
468 sr = QStringList::split("\t", sl[i]);
469 int c = 0;
470 while (ac <= rc)
472 setText(ar, ac, sr[c]);
473 ac++;
474 c++;
476 ar++;
477 i++;
482 else
484 QString s = kapp->clipboard()->text();
485 QStringList sl;
486 sl = QStringList::split("\n", s);
487 uint i = 0;
488 int ar = tr;
489 QStringList sr;
490 while(i < sl.count() && ar <= br )
492 int ac = lc;
494 sr = QStringList::split("\t", sl[i]);
495 int c = 0;
496 while (ac <= rc)
498 setText(ar, ac, sr[c]);
499 ac++;
500 c++;
502 ar++;
503 i++;
507 //restore selection
508 addSelection(QTableSelection(tr, lc, br, rc));
509 setCurrentCell(m_currentRow, m_currentCol);
511 getDocument()->setModified(true);
514 void KWordQuizView::doEditClear( )
516 if (isEditing())
518 ((QLineEdit *) cellWidget(currentRow(), currentColumn()))->clear();
520 else
522 addUndo(i18n("&Undo Clear"));
523 saveCurrentSelection(false);
524 for (int r = m_currentSel.topRow(); r <= m_currentSel.bottomRow(); ++r)
525 for(int c = m_currentSel.leftCol(); c <= m_currentSel.rightCol(); ++c)
526 clearCell(r, c);
528 getDocument()->setModified(true);
531 void KWordQuizView::doEditInsert( )
533 addUndo(i18n("&Undo Insert"));
534 setUpdatesEnabled(false);
535 saveCurrentSelection();
536 insertRows(m_currentSel.topRow(), m_currentSel.bottomRow() - m_currentSel.topRow() + 1);
538 addSelection(QTableSelection(m_currentSel.topRow(), m_currentSel.leftCol(), m_currentSel.bottomRow(), m_currentSel.rightCol()));
539 setCurrentCell(m_currentRow, m_currentCol);
540 setUpdatesEnabled(true);
541 repaintContents();
542 getDocument()->setModified(true);
545 void KWordQuizView::doEditDelete( )
547 addUndo(i18n("&Undo Delete"));
548 //retrieve current selection
549 saveCurrentSelection();
551 int tr = m_currentSel.topRow();
552 int br = m_currentSel.bottomRow();
554 if (tr == 0 && br == numRows() - 1)
555 br--; //leave one row if all rows are selected
557 for (int r = br; r >= tr; --r)
558 removeRow(r);
560 if (br > numRows())
561 br = numRows(); //adjust for new numRows
563 //restore selection as much as possible
564 addSelection(QTableSelection(tr, m_currentSel.leftCol(), br, m_currentSel.rightCol()));
565 setCurrentCell(m_currentRow, m_currentCol);
566 getDocument()->setModified(true);
569 const char delim_start = '[';
570 const char delim_end = ']';
573 bool KWordQuizView::checkForBlank( const QString & s, bool blank )
575 if (!blank)
576 return true;
578 bool result = false;
579 int openCount = 0;
580 int closeCount = 0;
581 QMemArray<int> openPos(0);
582 QMemArray<int> closePos(0);
584 for (uint i = 0; i<= s.length(); ++i)
586 if (s[i] == delim_start)
588 openCount++;
589 openPos.resize(openCount);
590 openPos[openCount] = i;
593 if (s[i] == delim_end)
595 closeCount++;
596 closePos.resize(closeCount);
597 closePos[closeCount] = i;
601 if (openCount == 0 && closeCount == 0)
602 return true;
604 if (openCount > 0 && closeCount > 0)
605 if (openPos.size() == closePos.size())
606 for (uint i = 0; i <= openPos.size(); ++i)
607 result = (openPos[i] < closePos[i]);
609 return result;
612 void KWordQuizView::doEditMarkBlank( )
614 if (isEditing())
616 addUndo(i18n("&Undo Mark Blank"));
617 QLineEdit * l = (QLineEdit *) cellWidget(currentRow(), currentColumn());
618 if (l->text().length() > 0)
620 QString s = l->text();
621 int cp = l->cursorPosition();
622 if (!l->hasSelectedText())
624 if (!s[cp].isSpace() && !s[cp - 1].isSpace())
626 l->cursorWordBackward(false);
627 int cp1 = l->cursorPosition();
628 l->cursorWordForward(false);
629 if (l->cursorPosition() != (int) s.length())
630 { while(l->text()[l->cursorPosition()].isSpace())
631 l->cursorBackward(false, 1);
633 int cp2 = l->cursorPosition();
634 if (cp2 == (int) s.length())
635 l->setSelection(cp1, cp2 - cp1);
636 else
637 l->setSelection(cp1, cp2 - cp1 - 1);
640 else
641 return;
645 if (l->hasSelectedText())
647 QString st = l->selectedText();
648 int len = st.length();
649 st = st.prepend(delim_start);
650 st = st.append(delim_end);
651 int ss = l->selectionStart();
652 s = s.replace(ss, len, st);
653 l->setText(s);
654 l->setSelection(ss, st.length());
660 void KWordQuizView::doEditUnmarkBlank( )
662 addUndo(i18n("&Undo Unmark Blank"));
663 QString s;
665 if (isEditing())
667 QLineEdit * l = (QLineEdit *) cellWidget(currentRow(), currentColumn());
669 if (l->hasSelectedText())
671 QString ls = l->text();
672 s = l->selectedText();
673 int len = s.length();
674 s.remove(delim_start);
675 s.remove(delim_end);
676 int ss = l->selectionStart();
677 ls = ls.replace(ss, len, s);
678 l->setText(ls);
679 l->setSelection(ss, s.length());
681 else
683 if (l->text().length() > 0)
685 s = l->text();
686 int cs = l->cursorPosition();
688 int fr = s.findRev(delim_start, cs);
689 if (fr > 0)
691 s = s.replace(fr, 1, "");
692 cs--;
694 int ff = s.find(delim_end, cs);
695 if (ff > 0)
696 s = s.replace(ff, 1, "");
698 l->setText(s);
699 l->setCursorPosition(cs);
703 else
705 saveCurrentSelection(false);
706 for (int r = m_currentSel.topRow(); r <= m_currentSel.bottomRow(); ++r)
707 for(int c = m_currentSel.leftCol(); c <= m_currentSel.rightCol(); ++c)
709 s = text(r, c);
710 s = s.remove(delim_start);
711 s = s.remove(delim_end);
712 setText(r, c, s);
717 bool KWordQuizView::checkSyntax(bool all, bool blanks)
719 int errorCount = 0;
720 int r1, r2, c1 ,c2;
722 if (all)
724 r1 = 0;
725 r2 = numRows() - 1;
726 c1 = 0;
727 c2 = 1;
729 else
731 saveCurrentSelection(false);
732 r1 = m_currentSel.topRow();
733 r2 = m_currentSel.bottomRow();
734 c1 = m_currentSel.leftCol();
735 c2 = m_currentSel.rightCol();
738 for (int r = r1; r <= r2; ++r)
739 for(int c = c1 ; c <= c2 ; ++c )
741 QString s = text(r, c);
742 if (s.length() > 0)
743 for (uint i = 0; i <= s.length(); ++i)
744 if (s[i] == delim_start || s[i] == delim_end)
745 if (!checkForBlank(s, blanks))
746 errorCount++;
748 return (errorCount == 0);
751 void KWordQuizView::doVocabSort( )
753 saveCurrentSelection();
754 DlgSort* dlg;
755 dlg = new DlgSort(this, "dlg_sort", true);
756 dlg->setLanguage(1, horizontalHeader()->label(0));
757 dlg->setLanguage(2, horizontalHeader()->label(1));
758 dlg->disableResize();
759 if (dlg->exec() == KDialogBase::Accepted)
761 addUndo(i18n("&Undo Sort"));
762 if (dlg->base())
763 sortColumn(0, dlg->ascending(), true);
764 else
765 sortColumn(1, dlg->ascending(), true);
766 getDocument()->setModified(true);
768 //restore selection
769 addSelection(QTableSelection(m_currentSel.topRow(), m_currentSel.leftCol(), m_currentSel.bottomRow(), m_currentSel.rightCol()));
770 setCurrentCell(m_currentRow, m_currentCol);
773 void KWordQuizView::doVocabShuffle( )
775 //@todo handle empty rows
776 addUndo(i18n("&Undo Shuffle"));
777 saveCurrentSelection();
778 KRandomSequence* rs;
779 rs = new KRandomSequence();
780 int count = numRows();
781 setUpdatesEnabled(false);
782 while (count > 0)
784 swapRows(count-1, rs->getLong(count));
785 count--;
787 //restore selection
788 addSelection(QTableSelection(m_currentSel.topRow(), m_currentSel.leftCol(), m_currentSel.bottomRow(), m_currentSel.rightCol()));
789 setCurrentCell(m_currentRow, m_currentCol);
790 setUpdatesEnabled(true);
791 repaintContents();
792 getDocument()->setModified(true);
795 void KWordQuizView::doVocabRC( )
797 saveCurrentSelection();
798 DlgRC* dlg;
799 dlg = new DlgRC(this, "dlg_rc", true);
800 //dlg->setInitialSize(QSize(225, 230), true);
801 dlg->setNumRows(numRows());
802 dlg->setRowHeight(rowHeight(m_currentRow));
803 dlg->setColWidth(columnWidth(m_currentCol));
804 dlg->disableResize();
805 if (dlg->exec() == KDialogBase::Accepted)
807 if (dlg->numRows() < 1)
808 setNumRows(1);
809 else
810 setNumRows(dlg->numRows());
811 for (int i = m_currentSel.topRow(); i <= m_currentSel.bottomRow(); ++i)
812 setRowHeight(i, dlg->rowHeight());
813 for (int i = m_currentSel.leftCol(); i <= m_currentSel.rightCol(); ++i)
814 setColumnWidth(i, dlg->colWidth());
815 getDocument()->setModified(true);
817 addSelection(QTableSelection(m_currentSel.topRow(), m_currentSel.leftCol(), m_currentSel.bottomRow(), m_currentSel.rightCol()));
818 setCurrentCell(m_currentRow, m_currentCol);
821 void KWordQuizView::doVocabSpecChar( )
823 QString f = font().family();
824 QChar c=' ';
825 if (dlgSpecChar==0)
827 dlgSpecChar = new DlgSpecChar( this, "insert special char", f, c, false );
828 connect( dlgSpecChar, SIGNAL(insertChar(QChar)), this, SLOT(slotSpecChar(QChar)));
829 connect( dlgSpecChar, SIGNAL( finished() ), this, SLOT( slotDlgSpecCharClosed() ) );
831 dlgSpecChar->show();
834 void KWordQuizView::slotDlgSpecCharClosed( )
836 if ( dlgSpecChar )
838 disconnect( dlgSpecChar, SIGNAL(insertChar(QChar)), this, SLOT(slotSpecChar(QChar)));
839 disconnect( dlgSpecChar, SIGNAL( finished() ), this, SLOT( slotDlgSpecCharClosed() ) );
840 dlgSpecChar->deleteLater();
841 dlgSpecChar = 0L;
845 void KWordQuizView::slotSpecChar(const QChar & c)
847 if (isEditing())
849 QLineEdit * l = (QLineEdit *) cellWidget(currentRow(), currentColumn());
850 if (l->hasSelectedText())
852 QString ls = l->text();
853 QString s = l->selectedText();
854 int len = s.length();
855 int ss = l->selectionStart();
856 ls = ls.replace(ss, len, c);
857 l->setText(ls);
858 l->setSelection(ss, 1);
860 else
862 QString s = l->text();
863 int i = l->cursorPosition();
864 s.insert(i, c);
865 l->setText(s);
866 l->setCursorPosition(i + 1);
869 else
871 editCell(currentRow(), currentColumn(), true);
872 ((QLineEdit *) cellWidget(currentRow(), currentColumn()))->setText(c);
873 ((QLineEdit *) cellWidget(currentRow(), currentColumn()))->setCursorPosition(1);
877 void KWordQuizView::activateNextCell( )
879 saveCurrentSelection(false);
880 int tr = m_currentSel.topRow();
881 int br = m_currentSel.bottomRow();
882 int lc = m_currentSel.leftCol();
883 int rc = m_currentSel.rightCol();
885 if (lc == rc && tr == br) //one cell selected
887 clearSelection();
888 switch(Prefs::enterMove())
890 case 0:
891 if (m_currentRow == (numRows() - 1))
892 setNumRows(numRows() + 1);
893 setCurrentCell(m_currentRow + 1, m_currentCol);
894 break;
895 case 1:
896 if (m_currentCol == 0)
897 setCurrentCell(m_currentRow, m_currentCol + 1);
898 else
900 if (m_currentRow == (numRows() - 1))
901 setNumRows(numRows() + 1);
902 setCurrentCell(m_currentRow + 1, m_currentCol - 1);
904 break;
905 case 2:
906 setCurrentCell(m_currentRow, m_currentCol);
907 break;
910 else //a larger selection, move within it
912 //addSelection(QTableSelection(m_currentSel.topRow(), m_currentSel.leftCol(), m_currentSel.bottomRow(), m_currentSel.rightCol()));
913 switch(Prefs::enterMove())
915 case 0:
916 if (m_currentRow == br)
918 if (m_currentCol < rc)
919 setCurrentCell(tr, rc);
920 else
921 setCurrentCell(tr, lc);
923 else
924 if (m_currentRow < br)
925 setCurrentCell(m_currentRow + 1, m_currentCol);
926 break;
927 case 1:
928 if (m_currentCol == rc)
930 if (m_currentRow < br)
931 setCurrentCell(m_currentRow + 1, lc);
932 else
933 setCurrentCell(tr, lc);
935 else
936 setCurrentCell(m_currentRow, m_currentCol + 1);
937 break;
938 case 2:
939 setCurrentCell(m_currentRow, m_currentCol);
940 break;
945 void KWordQuizView::addUndo( const QString & caption )
947 while (m_undoList->count() > 10)
949 m_undoList->remove(m_undoList->begin());
952 WQUndo* undo = new WQUndo();
953 undo->setText(caption);
954 undo->setFont(font());
955 undo->setColWidth0(verticalHeader()->width());
956 undo->setColWidth1(columnWidth(0));
957 undo->setColWidth2(columnWidth(1));
958 undo->setNumRows(numRows());
959 undo->setCurrentRow(currentRow());
960 undo->setCurrentCol(currentColumn());
961 undo->setSelection(selection(0));
963 KWqlDataItemList list;
964 for(int i = 0; i < numRows(); i++)
966 KWqlDataItem item(text(i, 0), text(i, 1), rowHeight(i));
967 list.append(item);
970 undo->setList(list);
972 m_undoList->prepend(*undo);
974 getDocument()->setModified(true);
976 emit undoChange(caption, true);
979 void KWordQuizView::setFont( const QFont & font)
981 QTable::setFont(font);
982 horizontalHeader()->setFont(KGlobalSettings::generalFont());
983 verticalHeader()->setFont(KGlobalSettings::generalFont());
984 for (int i = 0; i < numRows(); ++i)
985 adjustRow(i); //setRowHeight(i, fontMetrics().lineSpacing() );
988 void KWordQuizView::paintCell( QPainter * p, int row, int col, const QRect & cr, bool selected, const QColorGroup & cg )
990 QColorGroup g (cg);
992 if (Prefs::enableBlanks())
993 if (!checkForBlank(text(row, col), true))
994 g.setColor(QColorGroup::Text, Qt::red);
996 QTable::paintCell (p, row, col, cr, selected, g );
999 void KWordQuizView::keyPressEvent( QKeyEvent * e)
1001 /*if (isEditing())
1002 if (e->key() == Key_Tab)
1004 endEdit(currentRow(), currentColumn(), true, true);
1005 activateNextCell();
1006 return;
1008 else
1009 return;
1011 if (e->key() == Key_Tab)
1013 activateNextCell();
1014 return;
1016 QTable::keyPressEvent(e);
1019 void KWordQuizView::slotCheckedAnswer( int i )
1021 if (i == -1)
1023 clearSelection();
1024 addSelection(QTableSelection(m_currentSel.topRow(), m_currentSel.leftCol(), m_currentSel.bottomRow(), m_currentSel.rightCol()));
1025 setCurrentCell(m_currentRow, m_currentCol);
1027 else
1029 clearSelection();
1030 setCurrentCell(i, 0);
1031 selectRow(i);
1035 bool KWordQuizView::eventFilter( QObject * o, QEvent * e )
1037 if (o == cellEditor)
1039 if ( e->type() == QEvent::KeyPress )
1041 QKeyEvent *k = (QKeyEvent *)e;
1042 if (k->key() == Key_Tab)
1044 endEdit(currentRow(), currentColumn(), true, true);
1045 activateNextCell();
1046 return true;
1050 return QTable::eventFilter(o, e);
1053 #include "kwordquizview.moc"