summaryrefslogtreecommitdiff
path: root/inputmethods/pickboard/pickboardcfg.cpp
Unidiff
Diffstat (limited to 'inputmethods/pickboard/pickboardcfg.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--inputmethods/pickboard/pickboardcfg.cpp731
1 files changed, 731 insertions, 0 deletions
diff --git a/inputmethods/pickboard/pickboardcfg.cpp b/inputmethods/pickboard/pickboardcfg.cpp
new file mode 100644
index 0000000..e8b47cb
--- a/dev/null
+++ b/inputmethods/pickboard/pickboardcfg.cpp
@@ -0,0 +1,731 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "pickboardcfg.h"
22#include "pickboardpicks.h"
23
24#include <qpe/global.h>
25
26#include <qpainter.h>
27#include <qlist.h>
28#include <qbitmap.h>
29#include <qlayout.h>
30#include <qvbox.h>
31#include <qdialog.h>
32#include <qscrollview.h>
33#include <qpopupmenu.h>
34#include <qhbuttongroup.h>
35#include <qpushbutton.h>
36#include <qmessagebox.h>
37#include <qwindowsystem_qws.h>
38
39static const char * pickboard_help =
40 "<h1>The Pickboard</h1>"
41 "<i>The smallest and fastest way to type.</i>"
42 "<p>"
43 "Enter a word by tapping letter-groups and picking the word."
44 "<br>Enter spaces with \"Space\", or other keys through \"KEY\"."
45 "<br>Use \"Shift\" to capitalize words that are not normally capitalized."
46 "<br>Press \"Shift\" twice for an all-capitals word."
47 "<br>Add custom words by picking them, then selecting \"Add...\" from the menu on the right."
48 ;
49
50const int intermatchmargin=5;
51
52
53PickboardConfig::~PickboardConfig() { }
54
55void PickboardConfig::updateRows(int from, int to)
56{
57 if ( from != to ) { // (all)
58 parent->update();
59 } else {
60 QFontMetrics fm = parent->fontMetrics();
61 parent->update(QRect(0,1+fm.descent() + from * fm.lineSpacing(), parent->width(),
62 fm.lineSpacing()));
63 }
64}
65
66void PickboardConfig::updateItem(int r, int)
67{
68 updateRows(r,r);
69}
70
71void PickboardConfig::changeMode(int m)
72{
73 parent->setMode(m);
74}
75void PickboardConfig::generateText(const QString& s)
76{
77#if defined(Q_WS_QWS) || defined(_WS_QWS_)
78 for (int i=0; i<(int)s.length(); i++) {
79 parent->emitKey(s[i].unicode(), 0, 0, true, false);
80 parent->emitKey(s[i].unicode(), 0, 0, false, false);
81 }
82#endif
83}
84void PickboardConfig::generateKey( int k )
85{
86#if defined(Q_WS_QWS) || defined(_WS_QWS_)
87 parent->emitKey(0, k, 0, true, false);
88 parent->emitKey(0, k, 0, false, false);
89#endif
90}
91
92void PickboardConfig::pickPoint(const QPoint& p, bool press)
93{
94 if ( press ) {
95 int ls=parent->height()/nrows;
96 int y=0;
97 pressx = -1;
98 for (int r=0; r<nrows; r++) {
99 if ( p.y() >= y && p.y() < y+ls ) {
100 pressrow = r;
101 pressx = p.x();
102 pickInRow( pressrow, pressx, TRUE );
103 return;
104 }
105 y += ls;
106 }
107 } else if ( pressx >= 0 ) {
108 pickInRow( pressrow, pressx, FALSE );
109 pressx = -1;
110 }
111}
112
113void PickboardConfig::fillMenu(QPopupMenu& menu)
114{
115 menu.insertItem("Reset",100);
116 menu.insertSeparator();
117 menu.insertItem("Help",1);
118}
119
120void PickboardConfig::doMenu(int i)
121{
122 switch (i) {
123 case 100:
124 if ( parent->currentMode() ) {
125 changeMode(0);
126 updateRows(0,1);
127 }
128 break;
129 case 1: {
130 QMessageBox help("Pickboard Help", pickboard_help,
131 QMessageBox::NoIcon, 1, 0, 0);
132 help.showMaximized();
133 help.exec();
134 }
135 }
136}
137
138void StringConfig::draw(QPainter* p)
139{
140 QFontMetrics fm = p->fontMetrics();
141
142 for (int r=0; r<nrows; r++) {
143 p->translate(0,fm.lineSpacing());
144 p->setPen(rowColor(r));
145
146 int tw=0;
147 QString s;
148 int i=0;
149 for (; !(s=text(r,i)).isNull(); ++i) {
150 int w = fm.width(s);
151 tw += w;
152 }
153 bool spread = spreadRow(r);// && parent->width() > tw;
154 int xw = spread ? (parent->width()-tw)/(i-1) : 3;
155 int x = spread ? (parent->width()-tw-xw*(i-1))/2 : 2;
156
157 i=0;
158 for (; !(s=text(r,i)).isNull(); ++i) {
159 int w = fm.width(s)+xw;
160 if ( highlight(r,i) ) {
161 p->fillRect(x-xw/2,1+fm.descent()-fm.lineSpacing(),w,fm.lineSpacing(),Qt::black);
162 p->setPen(Qt::white);
163 }else{
164 p->setPen(Qt::black);
165 }
166 p->drawText(x,-fm.descent()-1,s);
167 x += w;
168 }
169 }
170}
171
172void StringConfig::pickInRow(int r, int xpos, bool press)
173{
174 QFontMetrics fm = parent->fontMetrics();
175
176 int tw=0;
177 QString s;
178 int i=0;
179 for (; !(s=text(r,i)).isNull(); ++i) {
180 int w = fm.width(s);
181 tw += w;
182 }
183 bool spread = spreadRow(r) && parent->width() > tw;
184 int xw = spread ? (parent->width()-tw)/(i-1) : 3;
185 int x = spread ? (parent->width()-tw-xw*(i-1))/2 : 2;
186
187 i=0;
188 for (; !(s=text(r,i)).isNull(); ++i) {
189 int x2 = x + fm.width(s)+xw;
190 if ( xpos >= x && xpos < x2 ) {
191 pick(press, r, i);
192 return;
193 }
194 x = x2;
195 }
196}
197
198void StringConfig::updateItem(int r, int item)
199{
200 QFontMetrics fm = parent->fontMetrics();
201
202 int y = r * fm.lineSpacing();
203
204 int tw=0;
205 QString s;
206 int i=0;
207 for (; !(s=text(r,i)).isNull(); ++i) {
208 int w = fm.width(s);
209 tw += w;
210 }
211 bool spread = spreadRow(r) && parent->width() > tw;
212 int xw = spread ? (parent->width()-tw)/(i-1) : 3;
213 int x = spread ? (parent->width()-tw-xw*(i-1))/2 : 2;
214
215 i=0;
216 for (; !(s=text(r,i)).isNull(); ++i) {
217 int w = fm.width(s)+xw;
218 if ( i == item ) {
219 parent->update(QRect(x-xw/2,y+1+fm.descent(),w,fm.lineSpacing()));
220 return;
221 }
222 x += w;
223 }
224}
225
226bool StringConfig::highlight(int,int) const
227{
228 return FALSE;
229}
230
231LetterButton::LetterButton(const QChar& letter, QWidget* parent) :
232 QPushButton(letter,parent)
233{
234 setToggleButton(TRUE);
235 setAutoDefault(FALSE);
236 connect(this,SIGNAL(clicked()),this,SLOT(toggleCase()));
237 skip=TRUE;
238}
239
240void LetterButton::toggleCase()
241{
242 if ( skip ) {
243 // Don't toggle case the first time
244 skip=FALSE;
245 return;
246 }
247
248 QChar ch = text()[0];
249 QChar nch = ch.lower();
250 if ( ch == nch )
251 nch = ch.upper();
252 setText(nch);
253}
254
255LetterChoice::LetterChoice(QWidget* parent, const QString& set) :
256 QButtonGroup(parent)
257{
258 QHBoxLayout *l = new QHBoxLayout(this);
259 setFrameStyle(0);
260 setExclusive(TRUE);
261 for (int i=0; i<(int)set.length(); i++) {
262 LetterButton* b = new LetterButton(set[i],this);
263 l->addWidget(b,1,AlignCenter);
264 connect(b,SIGNAL(clicked()),this,SLOT(change()));
265 }
266}
267
268void LetterChoice::change()
269{
270 LetterButton* b = (LetterButton*)sender();
271 ch = b->text()[0];
272 emit changed();
273}
274
275
276PickboardAdd::PickboardAdd(QWidget* owner, const QStringList& setlist) :
277 QDialog( owner, 0, TRUE )
278{
279 QVBoxLayout* l = new QVBoxLayout(this);
280 l->setAutoAdd(TRUE);
281
282 QScrollView *sv = new QScrollView(this);
283 sv->setResizePolicy(QScrollView::AutoOneFit);
284 setMaximumHeight(200); // ### QDialog shouldn't allow us to be bigger than the screen
285 QVBox *letters = new QVBox(sv);
286 letters->setSpacing(0);
287 lc = new LetterChoice*[setlist.count()];
288 nlc = (int)setlist.count();
289 for (int i=0; i<nlc; i++) {
290 lc[i] = new LetterChoice(letters,setlist[i]);
291 connect(lc[i],SIGNAL(changed()),this,SLOT(checkAllDone()));
292 }
293 sv->addChild(letters);
294 QHBox* hb = new QHBox(this);
295 hb->setSpacing(0);
296 yes = new QPushButton("OK",hb);
297 yes->setEnabled(FALSE);
298 QPushButton *no = new QPushButton("Cancel",hb);
299 connect(yes, SIGNAL(clicked()), this, SLOT(accept()));
300 connect(no, SIGNAL(clicked()), this, SLOT(reject()));
301}
302
303PickboardAdd::~PickboardAdd()
304{
305 delete [] lc;
306}
307
308QString PickboardAdd::word() const
309{
310 QString str;
311 for (int i=0; i<nlc; i++) {
312 str += lc[i]->choice();
313 }
314 return str;
315}
316
317bool PickboardAdd::exec()
318{
319 QPoint pos = parentWidget()->mapToGlobal(QPoint(0,0));
320 pos.ry() -= height();
321 if ( QDialog::exec() ) {
322 Global::addWords(QStringList(word()));
323 return TRUE;
324 } else {
325 return FALSE;
326 }
327}
328
329void PickboardAdd::checkAllDone()
330{
331 if ( !yes->isEnabled() ) {
332 for (int i=0; i<nlc; i++) {
333 if ( lc[i]->choice().isNull() )
334 return;
335 }
336 yes->setEnabled(TRUE);
337 }
338}
339
340
341void DictFilterConfig::doMenu(int i)
342{
343 switch (i) {
344 case 300:
345 if ( input.count() == 0 ) {
346 QMessageBox::information(0, "Adding Words",
347 "To add words, pick the letters,\nthen "
348 "open the Add dialog. In that\ndialog, tap "
349 "the correct letters\nfrom the list "
350 "(tap twice for\ncapitals).");
351 } else {
352 PickboardAdd add(parent,capitalize(input));
353 if ( add.exec() )
354 generateText(add.word());
355 input.clear();
356 matches.clear();
357 updateRows(0,0);
358 }
359 break;
360 case 100:
361 if ( !input.isEmpty() ) {
362 input.clear();
363 matches.clear();
364 StringConfig::doMenu(i);
365 updateRows(0,1);
366 break;
367 } // else fall through
368 default:
369 StringConfig::doMenu(i);
370 }
371 shift = 0;
372 lit0 = -1;
373}
374
375QString DictFilterConfig::text(int r, int i)
376{
377 QStringList l = r ? sets : input.isEmpty() ? othermodes : matches;
378 return i < (int)l.count() ?
379 (input.isEmpty() ? l[i] : capitalize(l[i]))
380 : QString::null;
381}
382
383bool DictFilterConfig::spreadRow(int r)
384{
385 return r ? TRUE : input.isEmpty() ? TRUE : FALSE;
386}
387
388QStringList DictFilterConfig::capitalize(const QStringList& l)
389{
390 switch ( shift ) {
391 case 1: {
392 QStringList r;
393 QStringList::ConstIterator it = l.begin();
394 r.append((*it).upper());
395 for (++it; it != l.end(); ++it)
396 r.append(*it);
397 return r;
398 } case 2: {
399 QStringList r;
400 for (QStringList::ConstIterator it = l.begin(); it != l.end(); ++it)
401 r.append((*it).upper());
402 return r;
403 }
404 }
405 return l;
406}
407
408QString DictFilterConfig::capitalize(const QString& s)
409{
410 switch ( shift ) {
411 case 1: {
412 QString u = s;
413 u[0] = u[0].upper();
414 return u;
415 break;
416 } case 2:
417 return s.upper();
418 break;
419 }
420 return s;
421}
422
423void DictFilterConfig::pick(bool press, int row, int item)
424{
425 if ( row == 0 ) {
426 if ( press ) {
427 if ( input.isEmpty() ) {
428 lit0 = item;
429 if ( othermodes[item] == "Space" ) {
430 updateItem(row,item);
431 generateText(" ");
432 } else if ( othermodes[item] == "Back" ) {
433 updateItem(row,item);
434 generateKey(Qt::Key_Backspace);
435 } else if ( othermodes[item] == "Enter" ) {
436 updateItem(row,item);
437 generateKey(Qt::Key_Return);
438 } else if ( othermodes[item] == "Shift" ) {
439 updateItem(row,item);
440 shift = (shift+1)%3;
441 }
442 }
443 } else {
444 if ( !input.isEmpty() ) {
445 input.clear();
446 if ( item>=0 ) {
447 generateText(capitalize(matches[item]));
448 }
449 shift = 0;
450 matches.clear();
451 updateRows(0,0);
452 } else if ( item < 3 ) {
453 lit0 = -1;
454 changeMode(item+1); // I'm mode 0! ####
455 updateRows(0,1);
456 }
457 if ( lit0 >= 0 ) {
458 if ( !shift || othermodes[lit0] != "Shift" ) {
459 updateItem(0,lit0);
460 lit0 = -1;
461 }
462 }
463 }
464 } else {
465 lit0 = -1;
466 if ( press && item >= 0 ) {
467 lit1 = item;
468 add(sets[item]);
469 updateItem(1,item);
470 updateRows(0,0);
471 } else {
472 updateItem(1,lit1);
473 lit1 = -1;
474 }
475 }
476}
477
478bool DictFilterConfig::scanMatch(const QString& set, const QChar& l) const
479{
480 return set == "?" || set == "*" || set.contains(l);
481}
482
483//static int visit=0;
484//static int lvisit=0;
485
486void DictFilterConfig::scan(const QDawg::Node* n, int ipos, const QString& str, int length, bool extend)
487{
488 if ( n ) {
489 do {
490//visit++;
491 bool pastend = ipos >= (int)input.count();
492 if ( pastend && extend || !pastend && scanMatch(input[ipos],n->letter().lower()) ) {
493 if ( length>1 ) {
494 if ( !pastend && input[ipos] == "*" ) {
495 scan(n->jump(),ipos+1,str+n->letter(),length-1,FALSE);
496 scan(n->jump(),ipos,str+n->letter(),length,FALSE);
497 } else {
498 scan(n->jump(),ipos+1,str+n->letter(),length-1,extend);
499 }
500 } else {
501 if ( n->isWord() ) {
502 matches.append(str+n->letter());
503 }
504 }
505 }
506 n = n->next();
507 } while (n);
508 }
509}
510
511void DictFilterConfig::scanLengths(const QDawg::Node* n, int ipos, int& length_bitarray)
512{
513 if ( n ) {
514 do {
515//lvisit++;
516 bool pastend = ipos >= (int)input.count();
517 if ( pastend || scanMatch(input[ipos],n->letter().lower()) ) {
518 scanLengths(n->jump(),ipos+1,length_bitarray);
519 if ( n->isWord() )
520 length_bitarray |= (1<<(ipos+1));
521 }
522 n = n->next();
523 } while (n);
524 }
525}
526
527void DictFilterConfig::add(const QString& set)
528{
529 QFontMetrics fm = parent->fontMetrics();
530 input.append(set.lower());
531 matches.clear();
532//visit=0;
533//lvisit=0;
534 int length_bitarray = 0;
535 if ( input.count() > 4 ) {
536 scanLengths(Global::addedDawg().root(),0,length_bitarray);
537 scanLengths(Global::fixedDawg().root(),0,length_bitarray);
538 } else {
539 length_bitarray = 0xffffffff;
540 }
541 for (int len=input.count(); len<22 /* 32 */; ++len) {
542 if ( length_bitarray & (1<<len) ) {
543 scan(Global::addedDawg().root(),0,"",len,TRUE);
544 scan(Global::fixedDawg().root(),0,"",len,TRUE);
545 int x = 2;
546 for (QStringList::Iterator it=matches.begin(); it!=matches.end(); ++it) {
547 x += fm.width(*it)+intermatchmargin;
548 if ( x >= parent->width() ) {
549//qDebug("%d+%d visits",lvisit,visit);
550 return; // RETURN - No point finding more
551 }
552 }
553 }
554 if ( len == 1 && input.count() == 1 ) {
555 // Allow all single-characters to show as "matches"
556 for ( int i=0; i<(int)set.length(); i++ ) {
557 QChar ch = set[i].lower();
558 matches.append(ch);
559 }
560 }
561 }
562//qDebug("%d+%d visits",lvisit,visit);
563}
564
565bool DictFilterConfig::highlight(int r,int c) const
566{
567 return r == 0 ? c == lit0 : c == lit1;
568}
569
570
571void DictFilterConfig::addSet(const QString& s)
572{
573 sets.append(s);
574}
575
576void DictFilterConfig::addMode(const QString& s)
577{
578 othermodes.append(s);
579}
580
581void DictFilterConfig::fillMenu(QPopupMenu& menu)
582{
583 menu.insertItem("Add...",300);
584 StringConfig::fillMenu(menu);
585}
586
587QValueList<QPixmap> KeycodeConfig::row(int i)
588{
589 return i ? keypm2 : keypm1;
590}
591
592void KeycodeConfig::pickInRow(int r, int xpos, bool press)
593{
594 QValueList<QPixmap> pl = row(r);
595 QValueList<QPixmap>::Iterator it;
596 int item=0;
597 int x=xmarg;
598 for (it=pl.begin(); it!=pl.end(); ++it) {
599 int x2 = x + (*it).width();
600 if ( (*it).height() > 1 )
601 x2 += xw;
602 if ( xpos >= x && xpos < x2 ) {
603 pick(press, r, item);
604 return;
605 }
606 x = x2;
607 item++;
608 }
609}
610
611void KeycodeConfig::pick(bool press, int row, int item)
612{
613 if ( !press ) {
614 if ( item >= 0 ) {
615 int k = row == 0 ? keys1[item] : keys2[item];
616 if ( k )
617 generateKey(k);
618 }
619 changeMode(0);
620 updateRows(0,1);
621 }
622}
623
624void KeycodeConfig::draw(QPainter* p)
625{
626 int y=3;
627 QValueList<QPixmap>::Iterator it;
628 for (int r=0; r<nrows; r++) {
629 QValueList<QPixmap> pl = row(r);
630 int x = xmarg;
631 for (it=pl.begin(); it!=pl.end(); ++it) {
632 if ( (*it).height() == 1 ) {
633 // just a gap
634 x += (*it).width();
635 } else {
636 p->drawPixmap(x,y,*it);
637 x += (*it).width()+xw;
638 }
639 }
640 y += parent->height()/nrows;
641 }
642}
643
644
645void KeycodeConfig::addKey(int r, const QPixmap& pm, int code)
646{
647 if ( r == 0 ) {
648 keypm1.append(pm);
649 keys1.append(code);
650 } else {
651 keypm2.append(pm);
652 keys2.append(code);
653 }
654}
655void KeycodeConfig::addGap(int r, int w)
656{
657 QBitmap pm(w,1); // ick.
658 addKey(r,pm,0);
659}
660
661QString CharConfig::text(int r, int i)
662{
663 QStringList l = r ? chars2 : chars1;
664 return i < (int)l.count() ? l[i] : QString::null;
665}
666bool CharConfig::spreadRow(int)
667{
668 return TRUE;
669}
670
671void CharConfig::pick(bool press, int row, int item)
672{
673 if ( !press ) {
674 if ( item >= 0 ) {
675 generateText(row == 0 ? chars1[item] : chars2[item]);
676 }
677 changeMode(0);
678 updateRows(0,1);
679 }
680}
681
682void CharConfig::addChar(int r, const QString& s)
683{
684 if ( r ) chars2.append(s); else chars1.append(s);
685}
686
687QString CharStringConfig::text(int r, int i)
688{
689 QStringList l = r ? chars : QStringList(input);
690 return i < (int)l.count() ? l[i] : QString::null;
691}
692
693bool CharStringConfig::spreadRow(int i)
694{
695 return i ? TRUE : FALSE;
696}
697
698void CharStringConfig::pick(bool press, int row, int item)
699{
700 if ( row == 0 ) {
701 if ( !press ) {
702 if ( item>=0 ) {
703 generateText(input);
704 }
705 input = "";
706 changeMode(0);
707 updateRows(0,1);
708 }
709 } else {
710 if ( press && item >= 0 ) {
711 input.append(chars[item]);
712 updateRows(0,0);
713 }
714 }
715}
716
717void CharStringConfig::addChar(const QString& s)
718{
719 chars.append(s);
720}
721
722void CharStringConfig::doMenu(int i)
723{
724 if ( i == 100 ) {
725 input = "";
726 updateRows(0,0);
727 }
728
729 StringConfig::doMenu(i);
730}
731