1 /***************************************************************************
2 kwordquizview.cpp - description
4 begin : Wed Jul 24 20:12:30 PDT 2002
5 copyright : (C) 2002 by Peter Hedlund
6 email : peter@peterandlinda.com
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 // include files for Qt
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"
38 #include "dlgspecchar.h"
40 QValueList
<WQUndo
> *KWordQuizView::m_undoList
= 0L;
42 KWordQuizView::KWordQuizView(QWidget
*parent
, const char *name
) : QTable(parent
, name
)
45 m_undoList
= new QValueList
<WQUndo
>();
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
58 KWordQuizView::~KWordQuizView()
62 KWordQuizDoc
*KWordQuizView::getDocument() const
64 KWordQuizApp
*theApp
=(KWordQuizApp
*) parentWidget();
65 return theApp
->getDocument();
68 void KWordQuizView::print(KPrinter
*pPrinter
)
72 //type 0 Vocabulary list
73 //type 1 Vocabulary exam
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();
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;
88 pPrinter
->setOrientation(KPrinter::Landscape
);
90 painter
.begin(pPrinter
);
94 int cw0
= verticalHeader()->width();
95 int cw1
= columnWidth(0);
96 int cw2
= columnWidth(1);
104 int gridWidth
= cw0
+ cw1
+ cw2
+ cw3
;
106 int tPos
= marg
+ horizontalHeader()->height();
108 QRect w
= painter
.window();
110 doNewPage(painter
, res
, type
);
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
);
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));
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
);
143 doNewPage(painter
, res
, type
);
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));
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();
171 doNewPage(painter
, res
, type
);
175 doEndOfPage(painter
, tPos
, pageNum
++, res
, type
);
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);
186 int card_marg
= res
/ 2;
192 QRect w
= painter
.window();
194 painter
.setFont(KGlobalSettings::generalFont());
198 painter
.drawText(card_marg
, card_marg
- 20, i18n("KWordQuiz - %1").arg(getDocument()->URL().fileName()));
201 painter
.drawLine(marg
, marg
, marg
+ cw0
+ cw1
+ cw2
+ cw3
, marg
);
203 painter
.drawText(marg
, marg
- 20, i18n("KWordQuiz - %1").arg(getDocument()->URL().fileName()));
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));
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
)
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
));
233 int cw0
= verticalHeader()->width();
234 int cw1
= columnWidth(0);
235 int cw2
= columnWidth(1);
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
);
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()){
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);
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())
292 itm
= item(row
, col
);
293 itm
->setWordWrap(true);
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);
328 ts
= new QTableSelection(m_currentRow
, m_currentCol
, m_currentRow
, m_currentCol
);
333 void KWordQuizView::doEditUndo( )
337 if (((QLineEdit
*) cellWidget(currentRow(), currentColumn()))->isUndoAvailable())
338 ((QLineEdit
*) cellWidget(currentRow(), currentColumn()))->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());
352 setNumRows(undo
.numRows());
353 setCurrentCell(undo
.currentRow(), undo
.currentCol());
354 addSelection(undo
.selection());
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();
364 setText(i
, 0, s
); //calling setText only when there actually is text helps with sorting
365 s
= (*dataIt
).backText();
368 setRowHeight(i
, (*dataIt
).rowHeight());
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);
382 emit
undoChange(i18n("Cannot &Undo"), false);
386 void KWordQuizView::doEditCut( )
390 ((QLineEdit
*) cellWidget(currentRow(), currentColumn()))->cut();
394 addUndo(i18n("&Undo Cut"));
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
)
401 getDocument()->setModified(true);
404 void KWordQuizView::doEditCopy( )
406 bool mod
= getDocument()->isModified();
409 ((QLineEdit
*) cellWidget(currentRow(), currentColumn()))->copy();
413 saveCurrentSelection(false);
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";
421 kapp
->clipboard()->setText(s
);
423 getDocument()->setModified(mod
);
426 void KWordQuizView::doEditPaste( )
430 ((QLineEdit
*) cellWidget(currentRow(), currentColumn()))->paste();
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();
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
));
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;
464 while(i
< sl
.count() && br
<= numRows() )
468 sr
= QStringList::split("\t", sl
[i
]);
472 setText(ar
, ac
, sr
[c
]);
484 QString s
= kapp
->clipboard()->text();
486 sl
= QStringList::split("\n", s
);
490 while(i
< sl
.count() && ar
<= br
)
494 sr
= QStringList::split("\t", sl
[i
]);
498 setText(ar
, ac
, sr
[c
]);
508 addSelection(QTableSelection(tr
, lc
, br
, rc
));
509 setCurrentCell(m_currentRow
, m_currentCol
);
511 getDocument()->setModified(true);
514 void KWordQuizView::doEditClear( )
518 ((QLineEdit
*) cellWidget(currentRow(), currentColumn()))->clear();
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
)
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);
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
)
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
)
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
)
589 openPos
.resize(openCount
);
590 openPos
[openCount
] = i
;
593 if (s
[i
] == delim_end
)
596 closePos
.resize(closeCount
);
597 closePos
[closeCount
] = i
;
601 if (openCount
== 0 && closeCount
== 0)
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
]);
612 void KWordQuizView::doEditMarkBlank( )
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
);
637 l
->setSelection(cp1
, cp2
- cp1
- 1);
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
);
654 l
->setSelection(ss
, st
.length());
660 void KWordQuizView::doEditUnmarkBlank( )
662 addUndo(i18n("&Undo Unmark Blank"));
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
);
676 int ss
= l
->selectionStart();
677 ls
= ls
.replace(ss
, len
, s
);
679 l
->setSelection(ss
, s
.length());
683 if (l
->text().length() > 0)
686 int cs
= l
->cursorPosition();
688 int fr
= s
.findRev(delim_start
, cs
);
691 s
= s
.replace(fr
, 1, "");
694 int ff
= s
.find(delim_end
, cs
);
696 s
= s
.replace(ff
, 1, "");
699 l
->setCursorPosition(cs
);
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
)
710 s
= s
.remove(delim_start
);
711 s
= s
.remove(delim_end
);
717 bool KWordQuizView::checkSyntax(bool all
, bool blanks
)
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
);
743 for (uint i
= 0; i
<= s
.length(); ++i
)
744 if (s
[i
] == delim_start
|| s
[i
] == delim_end
)
745 if (!checkForBlank(s
, blanks
))
748 return (errorCount
== 0);
751 void KWordQuizView::doVocabSort( )
753 saveCurrentSelection();
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"));
763 sortColumn(0, dlg
->ascending(), true);
765 sortColumn(1, dlg
->ascending(), true);
766 getDocument()->setModified(true);
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();
779 rs
= new KRandomSequence();
780 int count
= numRows();
781 setUpdatesEnabled(false);
784 swapRows(count
-1, rs
->getLong(count
));
788 addSelection(QTableSelection(m_currentSel
.topRow(), m_currentSel
.leftCol(), m_currentSel
.bottomRow(), m_currentSel
.rightCol()));
789 setCurrentCell(m_currentRow
, m_currentCol
);
790 setUpdatesEnabled(true);
792 getDocument()->setModified(true);
795 void KWordQuizView::doVocabRC( )
797 saveCurrentSelection();
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)
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();
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() ) );
834 void KWordQuizView::slotDlgSpecCharClosed( )
838 disconnect( dlgSpecChar
, SIGNAL(insertChar(QChar
)), this, SLOT(slotSpecChar(QChar
)));
839 disconnect( dlgSpecChar
, SIGNAL( finished() ), this, SLOT( slotDlgSpecCharClosed() ) );
840 dlgSpecChar
->deleteLater();
845 void KWordQuizView::slotSpecChar(const QChar
& c
)
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
);
858 l
->setSelection(ss
, 1);
862 QString s
= l
->text();
863 int i
= l
->cursorPosition();
866 l
->setCursorPosition(i
+ 1);
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
888 switch(Prefs::enterMove())
891 if (m_currentRow
== (numRows() - 1))
892 setNumRows(numRows() + 1);
893 setCurrentCell(m_currentRow
+ 1, m_currentCol
);
896 if (m_currentCol
== 0)
897 setCurrentCell(m_currentRow
, m_currentCol
+ 1);
900 if (m_currentRow
== (numRows() - 1))
901 setNumRows(numRows() + 1);
902 setCurrentCell(m_currentRow
+ 1, m_currentCol
- 1);
906 setCurrentCell(m_currentRow
, m_currentCol
);
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())
916 if (m_currentRow
== br
)
918 if (m_currentCol
< rc
)
919 setCurrentCell(tr
, rc
);
921 setCurrentCell(tr
, lc
);
924 if (m_currentRow
< br
)
925 setCurrentCell(m_currentRow
+ 1, m_currentCol
);
928 if (m_currentCol
== rc
)
930 if (m_currentRow
< br
)
931 setCurrentCell(m_currentRow
+ 1, lc
);
933 setCurrentCell(tr
, lc
);
936 setCurrentCell(m_currentRow
, m_currentCol
+ 1);
939 setCurrentCell(m_currentRow
, m_currentCol
);
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
));
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
)
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
)
1002 if (e->key() == Key_Tab)
1004 endEdit(currentRow(), currentColumn(), true, true);
1011 if (e
->key() == Key_Tab
)
1016 QTable::keyPressEvent(e
);
1019 void KWordQuizView::slotCheckedAnswer( int i
)
1024 addSelection(QTableSelection(m_currentSel
.topRow(), m_currentSel
.leftCol(), m_currentSel
.bottomRow(), m_currentSel
.rightCol()));
1025 setCurrentCell(m_currentRow
, m_currentCol
);
1030 setCurrentCell(i
, 0);
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);
1050 return QTable::eventFilter(o
, e
);
1053 #include "kwordquizview.moc"