Diffstat (limited to 'noncore/apps/opie-reader/QTReader.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-reader/QTReader.cpp | 1034 |
1 files changed, 1034 insertions, 0 deletions
diff --git a/noncore/apps/opie-reader/QTReader.cpp b/noncore/apps/opie-reader/QTReader.cpp new file mode 100644 index 0000000..6251812 --- a/dev/null +++ b/noncore/apps/opie-reader/QTReader.cpp | |||
@@ -0,0 +1,1034 @@ | |||
1 | /**************************************************************************** | ||
2 | ** $Id$ | ||
3 | ** | ||
4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. | ||
5 | ** | ||
6 | ** This file is part of an example program for Qt. This example | ||
7 | ** program may be used, distributed and modified without limitation. | ||
8 | ** | ||
9 | *****************************************************************************/ | ||
10 | |||
11 | #include "config.h" | ||
12 | #include "QTReader.h" | ||
13 | #include "QTReaderApp.h" | ||
14 | #include <qpe/qpeapplication.h> | ||
15 | #include <math.h> | ||
16 | #include <ctype.h> | ||
17 | #include <stdio.h> //for sprintf | ||
18 | #include <qpe/config.h> | ||
19 | #include <qpe/applnk.h> | ||
20 | #include <qfontdatabase.h> | ||
21 | #include <qpe/global.h> | ||
22 | #include <qpe/qcopenvelope_qws.h> | ||
23 | |||
24 | #ifdef _UNICODE | ||
25 | const char *QTReader::fonts[] = { "unifont", "Courier", "Times", 0 }; | ||
26 | #else | ||
27 | const char *QTReader::fonts[] = { "Helvetica", "Courier", "Times", 0 }; | ||
28 | #endif | ||
29 | //const int QTReader::fontsizes[] = { 8, 10, 12, 14, 18, 24, 30, 40, 50, 60, 70, 80, 90, 100, 0 }; | ||
30 | |||
31 | //const tchar *QTReader::fonts[] = { "unifont", "fixed", "micro", "smoothtimes", "Courier", "Times", 0 }; | ||
32 | //const int QTReader::fontsizes[] = {10,16,17,22,0}; | ||
33 | //const tchar *QTReader::fonts[] = { "verdana", "Courier", "Times", 0 }; | ||
34 | //const int QTReader::fontsizes[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,0}; | ||
35 | |||
36 | QTReader::QTReader( QWidget *parent=0, const char *name=0, WFlags f = 0) : | ||
37 | QWidget(parent, name, f), | ||
38 | m_delay(100), | ||
39 | m_scrolldy(0), | ||
40 | m_autoScroll(false), | ||
41 | textarray(NULL), | ||
42 | locnarray(NULL), | ||
43 | numlines(0), | ||
44 | m_fontname("unifont"), | ||
45 | m_fm(NULL) | ||
46 | { | ||
47 | m_overlap = 1; | ||
48 | fontsizes = NULL; | ||
49 | // init(); | ||
50 | } | ||
51 | /* | ||
52 | QTReader::QTReader( const QString& filename, QWidget *parent=0, const tchar *name=0, WFlags f = 0) : | ||
53 | QWidget(parent, name, f), | ||
54 | m_textfont(0), | ||
55 | m_textsize(1), | ||
56 | textarray(NULL), | ||
57 | numlines(0), | ||
58 | bstripcr(true), | ||
59 | bunindent(false), | ||
60 | brepara(false), | ||
61 | bdblspce(false), | ||
62 | btight(false), | ||
63 | bindenter(0), | ||
64 | m_fm(NULL) | ||
65 | { | ||
66 | init(); | ||
67 | // qDebug("Load_file(1)"); | ||
68 | load_file((const tchar*)filename); | ||
69 | } | ||
70 | */ | ||
71 | |||
72 | long QTReader::real_delay() | ||
73 | { | ||
74 | return ( 8976 + m_delay ) / ( m_linespacing * m_linespacing ); | ||
75 | } | ||
76 | |||
77 | void QTReader::mouseReleaseEvent( QMouseEvent* _e ) | ||
78 | //void QTReader::mouseDoubleClickEvent( QMouseEvent* _e ) | ||
79 | { | ||
80 | if (textarray != NULL) | ||
81 | { | ||
82 | //printf("(%u, %u)\n", _e->x(), _e->y()); | ||
83 | QString wrd = QString::null; | ||
84 | int lineno = _e->y()/m_linespacing; | ||
85 | if (m_bMonoSpaced) | ||
86 | { | ||
87 | int chno = _e->x()/m_charWidth; | ||
88 | if (chno < ustrlen(textarray[lineno]->data())) | ||
89 | { | ||
90 | wrd[0] = textarray[lineno]->data()[chno]; | ||
91 | } | ||
92 | } | ||
93 | else | ||
94 | { | ||
95 | CBuffer* t = textarray[lineno]; | ||
96 | int first = 0; | ||
97 | while (1) | ||
98 | { | ||
99 | int i = first+1; | ||
100 | // while ((*t)[i] != ' ' && (*t)[i] != 0) i++; | ||
101 | while (QChar((*t)[i]).isLetter() && (*t)[i] != 0) i++; | ||
102 | if (m_fm->width(toQString(t->data()), i) > _e->x()) | ||
103 | { | ||
104 | wrd = toQString(t->data()+first, i - first); | ||
105 | break; | ||
106 | } | ||
107 | // while ((*t)[i] == ' ' && (*t)[i] != 0) i++; | ||
108 | while (!QChar((*t)[i]).isLetter() && (*t)[i] != 0) i++; | ||
109 | if ((*t)[i] == 0) break; | ||
110 | first = i; | ||
111 | } | ||
112 | } | ||
113 | if (!wrd.isEmpty()) | ||
114 | { | ||
115 | QClipboard* cb = QApplication::clipboard(); | ||
116 | cb->setText(wrd); | ||
117 | Global::statusMessage(wrd); | ||
118 | if (!m_targetapp.isEmpty() && !m_targetmsg.isEmpty()) | ||
119 | { | ||
120 | QCopEnvelope e(("QPE/Application/"+m_targetapp).utf8(), (m_targetmsg+"(QString)").utf8()); | ||
121 | e << wrd; | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | |||
127 | void QTReader::focusInEvent(QFocusEvent* e) | ||
128 | { | ||
129 | if (m_autoScroll) timer->start(real_delay(), false); | ||
130 | update(); | ||
131 | } | ||
132 | |||
133 | void QTReader::focusOutEvent(QFocusEvent* e) | ||
134 | { | ||
135 | if (m_autoScroll) | ||
136 | { | ||
137 | timer->stop(); | ||
138 | m_scrolldy = 0; | ||
139 | } | ||
140 | } | ||
141 | |||
142 | #include <qapplication.h> | ||
143 | #include <qdrawutil.h> | ||
144 | #include <unistd.h> | ||
145 | |||
146 | void QTReader::goDown() | ||
147 | { | ||
148 | if (m_bpagemode) | ||
149 | { | ||
150 | dopagedn(); | ||
151 | } | ||
152 | else | ||
153 | { | ||
154 | lineDown(); | ||
155 | } | ||
156 | } | ||
157 | |||
158 | void QTReader::goUp() | ||
159 | { | ||
160 | if (m_bpagemode) | ||
161 | { | ||
162 | dopageup(); | ||
163 | } | ||
164 | else | ||
165 | { | ||
166 | lineUp(); | ||
167 | } | ||
168 | } | ||
169 | |||
170 | void QTReader::keyPressEvent(QKeyEvent* e) | ||
171 | { | ||
172 | switch (e->key()) | ||
173 | { | ||
174 | case Key_Down: | ||
175 | { | ||
176 | e->accept(); | ||
177 | if (m_autoScroll) | ||
178 | { | ||
179 | if (m_delay < 59049) | ||
180 | { | ||
181 | m_delay = (3*m_delay)/2; | ||
182 | timer->changeInterval(real_delay()); | ||
183 | } | ||
184 | else | ||
185 | { | ||
186 | m_delay = 59049; | ||
187 | } | ||
188 | } | ||
189 | else | ||
190 | { | ||
191 | goDown(); | ||
192 | } | ||
193 | } | ||
194 | break; | ||
195 | case Key_Up: | ||
196 | { | ||
197 | e->accept(); | ||
198 | if (m_autoScroll) | ||
199 | { | ||
200 | if (m_delay > 1024) | ||
201 | { | ||
202 | m_delay = (2*m_delay)/3; | ||
203 | timer->changeInterval(real_delay()); | ||
204 | } | ||
205 | else | ||
206 | { | ||
207 | m_delay = 1024; | ||
208 | } | ||
209 | } | ||
210 | else | ||
211 | { | ||
212 | goUp(); | ||
213 | } | ||
214 | } | ||
215 | break; | ||
216 | /* | ||
217 | case Key_Left: | ||
218 | { | ||
219 | e->accept(); | ||
220 | if (m_textfont > 0) | ||
221 | { | ||
222 | m_textfont--; | ||
223 | setfont(NULL); | ||
224 | locate(pagelocate()); | ||
225 | update(); | ||
226 | } | ||
227 | } | ||
228 | break; | ||
229 | case Key_Right: | ||
230 | { | ||
231 | e->accept(); | ||
232 | if (fonts[++m_textfont] == 0) | ||
233 | { | ||
234 | m_textfont--; | ||
235 | } | ||
236 | else | ||
237 | { | ||
238 | setfont(NULL); | ||
239 | locate(pagelocate()); | ||
240 | update(); | ||
241 | } | ||
242 | } | ||
243 | break; | ||
244 | */ | ||
245 | case Key_Right: | ||
246 | { | ||
247 | e->accept(); | ||
248 | if (fontsizes[++m_textsize] == 0) | ||
249 | { | ||
250 | m_textsize--; | ||
251 | } | ||
252 | else | ||
253 | { | ||
254 | bool sc = m_autoScroll; | ||
255 | m_autoScroll = false; | ||
256 | setfont(NULL); | ||
257 | locate(pagelocate()); | ||
258 | update(); | ||
259 | m_autoScroll = sc; | ||
260 | if (m_autoScroll) autoscroll(); | ||
261 | } | ||
262 | } | ||
263 | break; | ||
264 | case Key_Left: | ||
265 | { | ||
266 | e->accept(); | ||
267 | if (m_textsize > 0) | ||
268 | { | ||
269 | bool sc = m_autoScroll; | ||
270 | m_autoScroll = false; | ||
271 | m_textsize--; | ||
272 | setfont(NULL); | ||
273 | locate(pagelocate()); | ||
274 | update(); | ||
275 | m_autoScroll = sc; | ||
276 | if (m_autoScroll) autoscroll(); | ||
277 | } | ||
278 | } | ||
279 | break; | ||
280 | case Key_Space: | ||
281 | //case Key_Enter: | ||
282 | case Key_Return: | ||
283 | { | ||
284 | e->accept(); | ||
285 | setautoscroll(!m_autoScroll); | ||
286 | ((QTReaderApp*)parent()->parent())->setScrollState(m_autoScroll); | ||
287 | } | ||
288 | break; | ||
289 | default: | ||
290 | e->ignore(); | ||
291 | } | ||
292 | } | ||
293 | |||
294 | void QTReader::setautoscroll(bool _sc) | ||
295 | { | ||
296 | if (_sc == m_autoScroll) return; | ||
297 | if (m_autoScroll) | ||
298 | { | ||
299 | m_autoScroll = false; | ||
300 | } | ||
301 | else | ||
302 | { | ||
303 | m_autoScroll = true; | ||
304 | autoscroll(); | ||
305 | } | ||
306 | } | ||
307 | |||
308 | bool QTReader::getline(CBuffer *buff) | ||
309 | { | ||
310 | if (m_bMonoSpaced) | ||
311 | { | ||
312 | return buffdoc.getline(buff ,width(), m_charWidth); | ||
313 | } | ||
314 | else | ||
315 | { | ||
316 | return buffdoc.getline(buff, width()); | ||
317 | } | ||
318 | } | ||
319 | |||
320 | void QTReader::doscroll() | ||
321 | { | ||
322 | if (!m_autoScroll) | ||
323 | { | ||
324 | timer->stop(); | ||
325 | return; | ||
326 | } | ||
327 | // timer->changeInterval(real_delay()); | ||
328 | QPainter p( this ); | ||
329 | QBrush b( white); | ||
330 | bitBlt(this,0,0,this,0,1,width(),-1); | ||
331 | qDrawPlainRect(&p,0,height() - 2,width(),2,white,1,&b); | ||
332 | |||
333 | if (++m_scrolldy == m_linespacing) | ||
334 | { | ||
335 | setfont(&p); | ||
336 | m_scrolldy = 0; | ||
337 | // qDrawPlainRect(&p,0,height() - m_linespacing,width(),m_linespacing,white,1,&b); | ||
338 | pagepos = locnarray[1]; | ||
339 | CBuffer* buff = textarray[0]; | ||
340 | for (int i = 1; i < numlines; i++) | ||
341 | { | ||
342 | textarray[i-1] = textarray[i]; | ||
343 | locnarray[i-1] = locnarray[i]; | ||
344 | } | ||
345 | locnarray[numlines-1] = locate(); | ||
346 | if (getline(buff)) | ||
347 | { | ||
348 | textarray[numlines-1] = buff; | ||
349 | drawText( p, 0, height() - m_descent - 2, buff->data()); | ||
350 | mylastpos = locate(); | ||
351 | } | ||
352 | else | ||
353 | { | ||
354 | // (*buff)[0] = '\0'; | ||
355 | textarray[numlines-1] = buff; | ||
356 | m_autoScroll = false; | ||
357 | ((QTReaderApp*)parent()->parent())->setScrollState(m_autoScroll); | ||
358 | } | ||
359 | } | ||
360 | } | ||
361 | |||
362 | void QTReader::drawText(QPainter& p, int x, int y, tchar* _text) | ||
363 | { | ||
364 | QString text = toQString(_text); | ||
365 | if (m_bMonoSpaced) | ||
366 | { | ||
367 | for (int i = 0; i < text.length(); i++) | ||
368 | { | ||
369 | p.drawText( x+i*m_charWidth, y, QString(text[i]) ); | ||
370 | } | ||
371 | } | ||
372 | else | ||
373 | { | ||
374 | p.drawText( x, y, text ); | ||
375 | } | ||
376 | } | ||
377 | |||
378 | void QTReader::autoscroll() | ||
379 | { | ||
380 | timer->start(real_delay(), false); | ||
381 | } | ||
382 | |||
383 | void QTReader::setfont(QPainter* p) | ||
384 | { | ||
385 | // qDebug("Fontsize = %u",fontsizes[m_textsize]); | ||
386 | // qDebug("SetFont %x",p); | ||
387 | QFont font(m_fontname, fontsizes[m_textsize], (m_bBold) ? QFont::Bold : QFont::Normal ); | ||
388 | m_charWidth = (m_charpc*fontsizes[m_textsize])/100; | ||
389 | if (m_charWidth <= 0) m_charWidth = 1; | ||
390 | // font.setFixedPitch(m_bMonoSpaced); | ||
391 | // qDebug("Raw name = %s", (const char*)font.rawName()); | ||
392 | if (p != NULL) p->setFont( font ); | ||
393 | if (m_fm == NULL) | ||
394 | { | ||
395 | m_fm = new QFontMetrics(font); | ||
396 | buffdoc.setfm(m_fm); | ||
397 | } | ||
398 | else | ||
399 | { | ||
400 | *m_fm = QFontMetrics(font); | ||
401 | } | ||
402 | m_ascent = m_fm->ascent(); | ||
403 | m_descent = m_fm->descent(); | ||
404 | m_linespacing = m_fm->lineSpacing(); | ||
405 | } | ||
406 | |||
407 | void QTReader::drawFonts( QPainter *p ) | ||
408 | { | ||
409 | setfont(p); | ||
410 | if (m_lastwidth != width()) | ||
411 | { | ||
412 | m_lastwidth = width(); | ||
413 | locate(pagepos); | ||
414 | } | ||
415 | else | ||
416 | { | ||
417 | int sl = screenlines(); | ||
418 | if (sl < numlines) | ||
419 | { | ||
420 | //qDebug("df:<%u,%u>",sl,numlines); | ||
421 | |||
422 | size_t newpos = locnarray[sl]; | ||
423 | CBuffer** nta = new CBuffer*[sl]; | ||
424 | size_t* nla = new size_t[sl]; | ||
425 | for (int i = 0; i < sl; i++) | ||
426 | { | ||
427 | nta[i] = textarray[i]; | ||
428 | nla[i] = locnarray[i]; | ||
429 | } | ||
430 | for (int i = sl; i < numlines; i++) delete textarray[i]; | ||
431 | delete [] textarray; | ||
432 | delete [] locnarray; | ||
433 | textarray = nta; | ||
434 | locnarray = nla; | ||
435 | numlines = sl; | ||
436 | jumpto(mylastpos = newpos); | ||
437 | //locate(pagepos); | ||
438 | } | ||
439 | if (sl > numlines) | ||
440 | { | ||
441 | //qDebug("df:<%u,%u>",sl,numlines); | ||
442 | CBuffer** nta = new CBuffer*[sl]; | ||
443 | size_t* nla = new size_t[sl]; | ||
444 | for (int i = 0; i < numlines; i++) | ||
445 | { | ||
446 | nta[i] = textarray[i]; | ||
447 | nla[i] = locnarray[i]; | ||
448 | } | ||
449 | if (locate() != mylastpos) jumpto(mylastpos); | ||
450 | for (int i = numlines; i < sl; i++) | ||
451 | { | ||
452 | nta[i] = new CBuffer; | ||
453 | nla[i] = locate(); | ||
454 | getline(nta[i]); | ||
455 | } | ||
456 | mylastpos = locate(); | ||
457 | delete [] textarray; | ||
458 | delete [] locnarray; | ||
459 | textarray = nta; | ||
460 | locnarray = nla; | ||
461 | numlines = sl; | ||
462 | } | ||
463 | int ypos = (btight) ? 0 : m_ascent-m_linespacing; | ||
464 | // int linespacing = (tight) ? m_ascent : m_ascent+m_descent; | ||
465 | for (int i = 0; i < numlines; i++) | ||
466 | { | ||
467 | drawText( *p, 0, ypos += m_linespacing, textarray[i]->data()); | ||
468 | } | ||
469 | /* | ||
470 | |||
471 | |||
472 | |||
473 | int nlines = height()/(fontmetric.ascent()+fontmetric.descent()); | ||
474 | tchar buffer[1024]; | ||
475 | for (int i = 0; i < nlines; i++) | ||
476 | { | ||
477 | y += fontmetric.ascent(); | ||
478 | sprintf(buffer, "%d:%d:%s[%d]:Lines %d:%s", i+1, m_textfont, fonts[m_textfont], m_fs, nlines, (const tchar*)m_string); | ||
479 | drawText( *p, 0, y, buffer ); | ||
480 | y += fontmetric.descent(); | ||
481 | } | ||
482 | */ | ||
483 | } | ||
484 | m_scrolldy = 0; | ||
485 | } | ||
486 | |||
487 | QString QTReader::firstword() | ||
488 | { | ||
489 | if (m_bMonoSpaced) | ||
490 | { | ||
491 | return toQString(textarray[0]->data()); | ||
492 | } | ||
493 | else | ||
494 | { | ||
495 | int start, end, len, j; | ||
496 | for (j = 0; j < numlines; j++) | ||
497 | { | ||
498 | len = textarray[j]->length(); | ||
499 | for (start = 0; start < len && !isalpha((*textarray[j])[start]); start++); | ||
500 | if (start < len) break; | ||
501 | } | ||
502 | if (j < numlines) | ||
503 | { | ||
504 | QString ret = ""; | ||
505 | for (end = start; end < len && isalpha((*textarray[j])[end]); end++) | ||
506 | ret += (*textarray[j])[end]; | ||
507 | if (ret.isEmpty()) ret = "Current position"; | ||
508 | return ret; | ||
509 | } | ||
510 | else | ||
511 | return "Current position"; | ||
512 | } | ||
513 | } | ||
514 | |||
515 | // | ||
516 | // Construct the QTReader with buttons. | ||
517 | // | ||
518 | |||
519 | void QTReader::ChangeFont(int tgt) | ||
520 | { | ||
521 | |||
522 | QValueList<int>::Iterator it; | ||
523 | |||
524 | // QValueList<int> sizes = QFontDatabase::pointSizes(m_fontname, (m_bBold) ? QFont::Bold : QFont::Normal); | ||
525 | QFontDatabase fdb; | ||
526 | /* | ||
527 | QStringList styles = fdb.styles(m_fontname); | ||
528 | for ( QStringList::Iterator it = styles.begin(); it != styles.end(); ++it ) | ||
529 | { | ||
530 | printf( "%s \n", (*it).latin1() ); | ||
531 | } | ||
532 | */ | ||
533 | QValueList<int> sizes = fdb.pointSizes(m_fontname, (m_bBold) ? QString("Bold") : QString::null); | ||
534 | uint n = sizes.count(); | ||
535 | if (fontsizes != NULL) delete [] fontsizes; | ||
536 | fontsizes = new unsigned int[n+1]; | ||
537 | uint i = 0; | ||
538 | uint best = 0; | ||
539 | for (it = sizes.begin(); it != sizes.end(); it++) | ||
540 | { | ||
541 | fontsizes[i] = (*it)/10; | ||
542 | if (abs(tgt-fontsizes[i]) < abs(tgt-fontsizes[best])) | ||
543 | { | ||
544 | best = i; | ||
545 | } | ||
546 | i++; | ||
547 | } | ||
548 | m_textsize = best; | ||
549 | fontsizes[i] = 0; | ||
550 | setfont(NULL); | ||
551 | QFont font(m_fontname, fontsizes[m_textsize], (m_bBold) ? QFont::Bold : QFont::Normal ); | ||
552 | if (m_fm == NULL) | ||
553 | { | ||
554 | m_fm = new QFontMetrics(font); | ||
555 | buffdoc.setfm(m_fm); | ||
556 | } | ||
557 | } | ||
558 | |||
559 | void QTReader::init() | ||
560 | { | ||
561 | // setCaption( "Qt Draw Demo Application" ); | ||
562 | |||
563 | setBackgroundColor( white ); | ||
564 | // QPainter p(this); | ||
565 | // p.setBackgroundMode( Qt::OpaqueMode ); | ||
566 | buffdoc.setfilter(getfilter()); | ||
567 | ChangeFont(m_textsize); | ||
568 | // setFocusPolicy(QWidget::StrongFocus); | ||
569 | // resize( 240, 320 ); | ||
570 | //setFocus(); | ||
571 | timer = new QTimer(this); | ||
572 | connect(timer, SIGNAL(timeout()), this, SLOT(doscroll())); | ||
573 | // QMessageBox::information(this, "init", m_lastfile, 1); | ||
574 | m_lastwidth = width(); | ||
575 | if (!m_lastfile.isEmpty()) | ||
576 | { | ||
577 | m_string = DocLnk(m_lastfile).name(); | ||
578 | load_file(m_lastfile); | ||
579 | } | ||
580 | } | ||
581 | |||
582 | // | ||
583 | // Clean up | ||
584 | // | ||
585 | QTReader::~QTReader() | ||
586 | { | ||
587 | if (fontsizes != NULL) delete [] fontsizes; | ||
588 | #ifndef QT_NO_PRINTER | ||
589 | // delete printer; | ||
590 | #endif | ||
591 | } | ||
592 | |||
593 | // | ||
594 | // Calls the drawing function as specified by the radio buttons. | ||
595 | // | ||
596 | |||
597 | void QTReader::drawIt( QPainter *p ) | ||
598 | { | ||
599 | drawFonts(p); | ||
600 | } | ||
601 | |||
602 | // | ||
603 | // Called when the print button is clicked. | ||
604 | // | ||
605 | /* | ||
606 | void QTReader::printIt() | ||
607 | { | ||
608 | #ifndef QT_NO_PRINTER | ||
609 | if ( printer->setup( this ) ) { | ||
610 | QPainter paint; | ||
611 | if ( !paint.begin( printer ) ) | ||
612 | return; | ||
613 | drawIt( &paint ); | ||
614 | } | ||
615 | #endif | ||
616 | } | ||
617 | */ | ||
618 | // | ||
619 | // Called when the widget needs to be updated. | ||
620 | // | ||
621 | |||
622 | void QTReader::paintEvent( QPaintEvent * ) | ||
623 | { | ||
624 | QPainter paint( this ); | ||
625 | drawIt( &paint ); | ||
626 | } | ||
627 | |||
628 | // | ||
629 | // Called when the widget has been resized. | ||
630 | // Moves the button group to the upper right corner | ||
631 | // of the widget. | ||
632 | |||
633 | /* | ||
634 | void QTReader::resizeEvent( QResizeEvent * ) | ||
635 | { | ||
636 | // qDebug("resize:(%u,%u)", width(), height()); | ||
637 | // bgroup->move( width()-bgroup->width(), 0 ); | ||
638 | } | ||
639 | */ | ||
640 | |||
641 | // | ||
642 | // Create and display our widget. | ||
643 | // | ||
644 | /* | ||
645 | int main( int argc, tchar **argv ) | ||
646 | { | ||
647 | QApplication app( argc, argv ); | ||
648 | QTReader draw; | ||
649 | app.setMainWidget( &draw ); | ||
650 | draw.setCaption("Qt Example - Drawdemo"); | ||
651 | draw.show(); | ||
652 | return app.exec(); | ||
653 | } | ||
654 | */ | ||
655 | |||
656 | |||
657 | bool QTReader::locate(unsigned long n) { | ||
658 | //printf("Locate\n"); | ||
659 | buffdoc.locate(n); | ||
660 | // qDebug("&buffdoc.located"); | ||
661 | fillbuffer(); | ||
662 | // qDebug("&Buffer filled"); | ||
663 | update(); | ||
664 | // qDebug("&Located"); | ||
665 | return true; | ||
666 | } | ||
667 | |||
668 | unsigned int QTReader::screenlines() | ||
669 | { | ||
670 | // int linespacing = (tight) ? m_ascent : m_ascent+m_descent; | ||
671 | // return (height()-m_descent)/(m_linespacing); | ||
672 | return (height()-2)/(m_linespacing); | ||
673 | }; | ||
674 | |||
675 | bool QTReader::fillbuffer() { | ||
676 | //printf("Fillbuffer\n"); | ||
677 | m_scrolldy = 0; | ||
678 | int ch; | ||
679 | bool ret = false; | ||
680 | int delta = screenlines(); | ||
681 | // qDebug("fillbuffer:%u-%u",delta,numlines); | ||
682 | if (delta != numlines) | ||
683 | { | ||
684 | if (textarray != NULL) | ||
685 | { | ||
686 | for (int i = 0; i < numlines; i++) delete textarray[i]; | ||
687 | delete [] textarray; | ||
688 | delete [] locnarray; | ||
689 | } | ||
690 | numlines = delta; | ||
691 | textarray = new CBuffer*[numlines]; | ||
692 | locnarray = new size_t[numlines]; | ||
693 | for (int i = 0; i < numlines; i++) textarray[i] = new CBuffer; | ||
694 | } | ||
695 | // qDebug("fillbuffer:pagepos:%u",pagepos); | ||
696 | unsigned int oldpagepos = pagepos; | ||
697 | // if (textarray != NULL) | ||
698 | // pagepos = locnarray[0]; | ||
699 | // else | ||
700 | pagepos = locate(); | ||
701 | for (int i = 0; i < delta; i++) | ||
702 | { | ||
703 | locnarray[i] = locate(); | ||
704 | ch = getline(textarray[i]); | ||
705 | // if (ch == EOF) { | ||
706 | if (!ch) | ||
707 | { | ||
708 | if (i == 0) | ||
709 | { | ||
710 | pagepos = oldpagepos; | ||
711 | return false; | ||
712 | } | ||
713 | else | ||
714 | { | ||
715 | ret = true; | ||
716 | for (int j = i+1; j < delta; j++) | ||
717 | { | ||
718 | locnarray[j] = locnarray[j-1]; | ||
719 | (*(textarray[j]))[0] = '\0'; | ||
720 | } | ||
721 | break; | ||
722 | } | ||
723 | } | ||
724 | if (ch == '\012') ret = true; | ||
725 | } | ||
726 | mylastpos = locate(); | ||
727 | // qDebug("fillbuffer:lastpos:%u",mylastpos); | ||
728 | return true; | ||
729 | } | ||
730 | |||
731 | |||
732 | void QTReader::dopagedn() | ||
733 | { | ||
734 | if (m_overlap == 0) | ||
735 | { | ||
736 | if (locate() != mylastpos) jumpto(mylastpos); | ||
737 | } | ||
738 | else | ||
739 | { | ||
740 | if (m_overlap >= screenlines()) m_overlap = screenlines()/2; | ||
741 | jumpto(locnarray[screenlines()-m_overlap]); | ||
742 | } | ||
743 | if (fillbuffer()) | ||
744 | { | ||
745 | update(); | ||
746 | } | ||
747 | } | ||
748 | |||
749 | void QTReader::dopageup() | ||
750 | { | ||
751 | CBuffer** buff = textarray; | ||
752 | unsigned int *loc = new unsigned int[numlines]; | ||
753 | int cbptr = 0; | ||
754 | if (locate() != mylastpos) jumpto(mylastpos); | ||
755 | if (m_overlap >= screenlines()) m_overlap = screenlines()/2; | ||
756 | unsigned int target = locnarray[m_overlap]; | ||
757 | if (buffdoc.hasrandomaccess()) | ||
758 | { | ||
759 | unsigned int delta = locate()-pagelocate(); | ||
760 | if (delta < 64) delta = 64; | ||
761 | if (delta % 2 != 0) delta++; | ||
762 | if (target % 2 != 0) target++; | ||
763 | do | ||
764 | { | ||
765 | delta <<= 1; | ||
766 | if (delta >= target) | ||
767 | { | ||
768 | delta = target; | ||
769 | jumpto(0); | ||
770 | for (int i = 0; i < numlines; i++) | ||
771 | { | ||
772 | loc[i] = locate(); | ||
773 | getline(buff[i]); | ||
774 | } | ||
775 | break; | ||
776 | } | ||
777 | jumpto(target-delta); | ||
778 | do | ||
779 | { | ||
780 | getline(buff[0]); | ||
781 | #ifdef WS | ||
782 | //printf("Trying:%s\n",buff[0]); | ||
783 | #endif | ||
784 | if (locate() > target) continue; | ||
785 | } | ||
786 | while (!buffdoc.iseol()); | ||
787 | for (int i = 0; i < numlines; i++) | ||
788 | { | ||
789 | loc[i] = locate(); | ||
790 | getline(buff[i]); | ||
791 | #ifdef WS | ||
792 | //printf("Filling:%s\n",buff[i]); | ||
793 | #endif | ||
794 | } | ||
795 | } | ||
796 | while (locate() >= target && delta < 4096); | ||
797 | #ifdef WS | ||
798 | //printf("Delta:%u\n",delta); | ||
799 | #endif | ||
800 | } | ||
801 | else | ||
802 | { | ||
803 | jumpto(0); | ||
804 | for (int i = 0; i < numlines; i++) | ||
805 | { | ||
806 | loc[i] = locate(); | ||
807 | getline(buff[i]); | ||
808 | } | ||
809 | } | ||
810 | cbptr = 0; | ||
811 | while (locate() < target) | ||
812 | { | ||
813 | loc[cbptr] = locate(); | ||
814 | getline(buff[cbptr]); | ||
815 | #ifdef WS | ||
816 | //printf("Adding:%s\n",buff[cbptr]->data()); | ||
817 | #endif | ||
818 | cbptr = (cbptr+1) % numlines; | ||
819 | } | ||
820 | pagepos = loc[cbptr]; | ||
821 | textarray = new CBuffer*[numlines]; | ||
822 | for (int i = 0; i < numlines; i++) | ||
823 | { | ||
824 | int j = (cbptr+i)%numlines; | ||
825 | textarray[i] = buff[j]; | ||
826 | locnarray[i] = loc[j]; | ||
827 | } | ||
828 | delete [] buff; | ||
829 | delete [] loc; | ||
830 | mylastpos = locate(); | ||
831 | update(); | ||
832 | } | ||
833 | |||
834 | bool QTReader::load_file(const char *newfile, unsigned int _lcn) | ||
835 | { | ||
836 | // QMessageBox::information(this, "Name", name, 1); | ||
837 | // QMessageBox::information(this, "load_file", newfile, 1); | ||
838 | |||
839 | bool bRC = false; | ||
840 | unsigned int lcn = _lcn; | ||
841 | if (m_lastfile == newfile) | ||
842 | { | ||
843 | lcn = m_lastposn; | ||
844 | } | ||
845 | m_lastfile = newfile; | ||
846 | // QMessageBox::information(0, "Opening...", newfile); | ||
847 | if (buffdoc.openfile(this,newfile) == 0) | ||
848 | { | ||
849 | bRC = true; | ||
850 | // qDebug("buffdoc.openfile done"); | ||
851 | locate(lcn); | ||
852 | // qDebug("buffdoc.locate done"); | ||
853 | } | ||
854 | update(); | ||
855 | // qDebug("Updated"); | ||
856 | return bRC; | ||
857 | } | ||
858 | |||
859 | void QTReader::lineDown() | ||
860 | { | ||
861 | pagepos = locnarray[1]; | ||
862 | CBuffer* buff = textarray[0]; | ||
863 | for (int i = 1; i < numlines; i++) | ||
864 | { | ||
865 | textarray[i-1] = textarray[i]; | ||
866 | locnarray[i-1] = locnarray[i]; | ||
867 | } | ||
868 | locnarray[numlines-1] = locate(); | ||
869 | if (getline(buff)) | ||
870 | { | ||
871 | textarray[numlines-1] = buff; | ||
872 | mylastpos = locate(); | ||
873 | } | ||
874 | else | ||
875 | { | ||
876 | textarray[numlines-1] = buff; | ||
877 | } | ||
878 | update(); | ||
879 | } | ||
880 | /* | ||
881 | void QTReader::lineUp() | ||
882 | { | ||
883 | CBuffer** buff = textarray; | ||
884 | unsigned int *loc = new unsigned int[numlines]; | ||
885 | int cbptr = 0; | ||
886 | if (locate() != mylastpos) jumpto(mylastpos); | ||
887 | unsigned int target = locnarray[numlines-1]; | ||
888 | if (buffdoc.hasrandomaccess()) | ||
889 | { | ||
890 | unsigned int delta = locate()-pagelocate(); | ||
891 | if (delta < 64) delta = 64; | ||
892 | do | ||
893 | { | ||
894 | delta <<= 1; | ||
895 | if (delta >= target) | ||
896 | { | ||
897 | delta = target; | ||
898 | jumpto(0); | ||
899 | for (int i = 0; i < numlines; i++) | ||
900 | { | ||
901 | loc[i] = locate(); | ||
902 | getline(buff[i]); | ||
903 | } | ||
904 | break; | ||
905 | } | ||
906 | jumpto(target-delta); | ||
907 | do | ||
908 | { | ||
909 | buffdoc.getline(buff[0],width()); | ||
910 | #ifdef WS | ||
911 | //printf("Trying:%s\n",buff[0]); | ||
912 | #endif | ||
913 | if (locate() > target) continue; | ||
914 | } | ||
915 | while (!buffdoc.iseol()); | ||
916 | for (int i = 0; i < numlines; i++) | ||
917 | { | ||
918 | loc[i] = locate(); | ||
919 | buffdoc.getline(buff[i],width()); | ||
920 | #ifdef WS | ||
921 | //printf("Filling:%s\n",buff[i]); | ||
922 | #endif | ||
923 | } | ||
924 | } | ||
925 | while (locate() >= target && delta < 4096); | ||
926 | #ifdef WS | ||
927 | //printf("Delta:%u\n",delta); | ||
928 | #endif | ||
929 | } | ||
930 | else | ||
931 | { | ||
932 | jumpto(0); | ||
933 | for (int i = 0; i < numlines; i++) | ||
934 | { | ||
935 | loc[i] = locate(); | ||
936 | buffdoc.getline(buff[i],width()); | ||
937 | } | ||
938 | } | ||
939 | cbptr = 0; | ||
940 | while (locate() < target) | ||
941 | { | ||
942 | loc[cbptr] = locate(); | ||
943 | buffdoc.getline(buff[cbptr], width()); | ||
944 | #ifdef WS | ||
945 | //printf("Adding:%s\n",buff[cbptr]->data()); | ||
946 | #endif | ||
947 | cbptr = (cbptr+1) % numlines; | ||
948 | } | ||
949 | pagepos = loc[cbptr]; | ||
950 | textarray = new CBuffer*[numlines]; | ||
951 | for (int i = 0; i < numlines; i++) | ||
952 | { | ||
953 | int j = (cbptr+i)%numlines; | ||
954 | textarray[i] = buff[j]; | ||
955 | locnarray[i] = loc[j]; | ||
956 | } | ||
957 | delete [] buff; | ||
958 | delete [] loc; | ||
959 | mylastpos = locate(); | ||
960 | update(); | ||
961 | } | ||
962 | */ | ||
963 | void QTReader::lineUp() | ||
964 | { | ||
965 | CBuffer* buff = textarray[numlines-1]; | ||
966 | unsigned int loc; | ||
967 | unsigned int end = locnarray[numlines-1]; | ||
968 | int cbptr = 0; | ||
969 | if (locate() != mylastpos) jumpto(mylastpos); | ||
970 | unsigned int target = locnarray[0]; | ||
971 | if (buffdoc.hasrandomaccess()) | ||
972 | { | ||
973 | unsigned int delta = locate()-pagelocate(); | ||
974 | if (delta < 64) delta = 64; | ||
975 | do | ||
976 | { | ||
977 | delta <<= 1; | ||
978 | if (delta >= target) | ||
979 | { | ||
980 | delta = target; | ||
981 | jumpto(0); | ||
982 | for (int i = 0; i < numlines; i++) | ||
983 | { | ||
984 | loc = locate(); | ||
985 | getline(buff); | ||
986 | } | ||
987 | break; | ||
988 | } | ||
989 | jumpto(target-delta); | ||
990 | do | ||
991 | { | ||
992 | getline(buff); | ||
993 | #ifdef WS | ||
994 | //printf("Trying:%s\n",buff[0]); | ||
995 | #endif | ||
996 | if (locate() > target) continue; | ||
997 | } | ||
998 | while (!buffdoc.iseol()); | ||
999 | loc = locate(); | ||
1000 | getline(buff); | ||
1001 | } | ||
1002 | while (locate() >= target && delta < 4096); | ||
1003 | } | ||
1004 | else | ||
1005 | { | ||
1006 | jumpto(0); | ||
1007 | loc = locate(); | ||
1008 | getline(buff); | ||
1009 | } | ||
1010 | cbptr = 0; | ||
1011 | while (locate() < target) | ||
1012 | { | ||
1013 | loc = locate(); | ||
1014 | getline(buff); | ||
1015 | } | ||
1016 | pagepos = loc; | ||
1017 | for (int i = numlines-1; i > 0; i--) | ||
1018 | { | ||
1019 | textarray[i] = textarray[i-1]; | ||
1020 | locnarray[i] = locnarray[i-1]; | ||
1021 | } | ||
1022 | textarray[0] = buff; | ||
1023 | locnarray[0] = loc; | ||
1024 | // delete [] buff; | ||
1025 | // delete [] loc; | ||
1026 | mylastpos = locate(); | ||
1027 | jumpto(end); | ||
1028 | update(); | ||
1029 | } | ||
1030 | |||
1031 | bool QTReader::empty() | ||
1032 | { | ||
1033 | return buffdoc.empty(); | ||
1034 | } | ||