Diffstat (limited to 'noncore/apps/opie-reader/QTReader.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/apps/opie-reader/QTReader.cpp | 632 |
1 files changed, 353 insertions, 279 deletions
diff --git a/noncore/apps/opie-reader/QTReader.cpp b/noncore/apps/opie-reader/QTReader.cpp index 6335ea2..e7bfc28 100644 --- a/noncore/apps/opie-reader/QTReader.cpp +++ b/noncore/apps/opie-reader/QTReader.cpp | |||
@@ -8,9 +8,11 @@ | |||
8 | ** | 8 | ** |
9 | *****************************************************************************/ | 9 | *****************************************************************************/ |
10 | 10 | ||
11 | #include <qpainter.h> | ||
11 | #include "config.h" | 12 | #include "config.h" |
12 | #include "QTReader.h" | 13 | #include "QTReader.h" |
13 | #include "QTReaderApp.h" | 14 | #include "QTReaderApp.h" |
15 | #include "CDrawBuffer.h" | ||
14 | #include <qpe/qpeapplication.h> | 16 | #include <qpe/qpeapplication.h> |
15 | #include <math.h> | 17 | #include <math.h> |
16 | #include <ctype.h> | 18 | #include <ctype.h> |
@@ -36,16 +38,19 @@ const char *QTReader::fonts[] = { "Helvetica", "Courier", "Times", 0 }; | |||
36 | QTReader::QTReader( QWidget *parent=0, const char *name=0, WFlags f = 0) : | 38 | QTReader::QTReader( QWidget *parent=0, const char *name=0, WFlags f = 0) : |
37 | QWidget(parent, name, f), | 39 | QWidget(parent, name, f), |
38 | m_delay(100), | 40 | m_delay(100), |
39 | m_scrolldy(0), | 41 | m_scrolldy1(0), |
42 | m_scrolldy2(0), | ||
40 | m_autoScroll(false), | 43 | m_autoScroll(false), |
41 | textarray(NULL), | 44 | //textarray(NULL), |
42 | locnarray(NULL), | 45 | //locnarray(NULL), |
43 | numlines(0), | 46 | numlines(0), |
44 | m_fontname("unifont"), | 47 | m_fontname("unifont"), |
45 | m_fm(NULL) | 48 | m_fm(NULL), |
49 | mouseUpOn(true), | ||
50 | m_twotouch(true), | ||
51 | m_touchone(true) | ||
46 | { | 52 | { |
47 | m_overlap = 1; | 53 | m_overlap = 1; |
48 | fontsizes = NULL; | ||
49 | // init(); | 54 | // init(); |
50 | } | 55 | } |
51 | /* | 56 | /* |
@@ -74,16 +79,129 @@ long QTReader::real_delay() | |||
74 | return ( 8976 + m_delay ) / ( m_linespacing * m_linespacing ); | 79 | return ( 8976 + m_delay ) / ( m_linespacing * m_linespacing ); |
75 | } | 80 | } |
76 | 81 | ||
77 | void QTReader::mouseReleaseEvent( QMouseEvent* _e ) | 82 | void QTReader::mousePressEvent( QMouseEvent* _e ) |
78 | //void QTReader::mouseDoubleClickEvent( QMouseEvent* _e ) | 83 | { |
84 | if (_e->button() == RightButton) | ||
85 | { | ||
86 | mouseUpOn = false; | ||
87 | if (_e->y() > height()/2) | ||
88 | { | ||
89 | goDown(); | ||
90 | } | ||
91 | else | ||
92 | { | ||
93 | goUp(); | ||
94 | } | ||
95 | } | ||
96 | } | ||
97 | |||
98 | bool QTReader::getcurrentpos(int x, int y, size_t& start, size_t& offset, size_t& tgt) | ||
79 | { | 99 | { |
80 | if (textarray != NULL) | 100 | int lineno = 0; |
101 | int ht = textarray[0]->lineSpacing(); | ||
102 | while ((ht < y) && (lineno < numlines)) | ||
81 | { | 103 | { |
82 | // printf("(%u, %u)\n", _e->x(), _e->y()); | 104 | ht += textarray[++lineno]->lineSpacing(); |
83 | QString wrd = QString::null; | 105 | } |
84 | int lineno = _e->y()/m_linespacing; | 106 | start = locnarray[lineno]; |
85 | if (m_bMonoSpaced) | 107 | if (m_bMonoSpaced) |
86 | { | 108 | { |
109 | offset = x/m_charWidth; | ||
110 | } | ||
111 | else | ||
112 | { | ||
113 | int i; | ||
114 | CDrawBuffer* t = textarray[lineno]; | ||
115 | for (i = t->length(); t->width(i) > x; i--); | ||
116 | offset = i; | ||
117 | } | ||
118 | return textarray[lineno]->isLink(offset, tgt); | ||
119 | } | ||
120 | |||
121 | void QTReader::setTwoTouch(bool _b) | ||
122 | { | ||
123 | setBackgroundColor( white ); | ||
124 | m_twotouch = m_touchone = _b; | ||
125 | } | ||
126 | |||
127 | void QTReader::mouseReleaseEvent( QMouseEvent* _e ) | ||
128 | { | ||
129 | if (_e->button() == LeftButton) | ||
130 | { | ||
131 | if (mouseUpOn) | ||
132 | { | ||
133 | if (textarray[0] != NULL) | ||
134 | { | ||
135 | QString wrd, line; | ||
136 | // int lineno = _e->y()/m_linespacing; | ||
137 | int lineno = 0; | ||
138 | int ht = textarray[0]->lineSpacing(); | ||
139 | while ((ht < _e->y()) && (lineno < numlines)) | ||
140 | { | ||
141 | ht += textarray[++lineno]->lineSpacing(); | ||
142 | } | ||
143 | size_t startpos, startoffset, tgt; | ||
144 | if (getcurrentpos(_e->x(), _e->y(), startpos, startoffset, tgt)) | ||
145 | { | ||
146 | if (buffdoc.hyperlink(tgt)) | ||
147 | { | ||
148 | fillbuffer(); | ||
149 | update(); | ||
150 | } | ||
151 | else | ||
152 | { | ||
153 | locate(pagelocate()); | ||
154 | } | ||
155 | return; | ||
156 | } | ||
157 | if (m_twotouch) | ||
158 | { | ||
159 | if (m_touchone) | ||
160 | { | ||
161 | m_touchone = false; | ||
162 | m_startpos = startpos; | ||
163 | m_startoffset = startoffset; | ||
164 | setBackgroundColor( lightGray ); | ||
165 | } | ||
166 | else | ||
167 | { | ||
168 | m_touchone = true; | ||
169 | setBackgroundColor( white ); | ||
170 | size_t endpos, endoffset; | ||
171 | endpos = startpos; | ||
172 | endoffset = startoffset; | ||
173 | size_t currentpos = locate(); | ||
174 | if (endpos >= m_startpos) | ||
175 | { | ||
176 | jumpto(m_startpos); | ||
177 | for (int i = 0; i < m_startoffset; i++) | ||
178 | { | ||
179 | getch(); | ||
180 | } | ||
181 | if (m_startpos == endpos) | ||
182 | { | ||
183 | for (int i = m_startoffset; i <= endoffset; i++) | ||
184 | { | ||
185 | wrd += QChar(getch()); | ||
186 | } | ||
187 | } | ||
188 | else | ||
189 | { | ||
190 | while (buffdoc.explocate() <= endpos) | ||
191 | { | ||
192 | wrd += QChar(getch()); | ||
193 | } | ||
194 | for (int i = 0; i < endoffset; i++) | ||
195 | { | ||
196 | wrd += QChar(getch()); | ||
197 | } | ||
198 | } | ||
199 | jumpto(currentpos); | ||
200 | } | ||
201 | } | ||
202 | } | ||
203 | else if (m_bMonoSpaced) | ||
204 | { | ||
87 | int chno = _e->x()/m_charWidth; | 205 | int chno = _e->x()/m_charWidth; |
88 | if (chno < ustrlen(textarray[lineno]->data())) | 206 | if (chno < ustrlen(textarray[lineno]->data())) |
89 | { | 207 | { |
@@ -92,19 +210,17 @@ void QTReader::mouseReleaseEvent( QMouseEvent* _e ) | |||
92 | } | 210 | } |
93 | else | 211 | else |
94 | { | 212 | { |
95 | CBuffer* t = textarray[lineno]; | 213 | CDrawBuffer* t = textarray[lineno]; |
96 | int first = 0; | 214 | int first = 0; |
97 | while (1) | 215 | while (1) |
98 | { | 216 | { |
99 | int i = first+1; | 217 | int i = first+1; |
100 | // while ((*t)[i] != ' ' && (*t)[i] != 0) i++; | ||
101 | while (QChar((*t)[i]).isLetter() && (*t)[i] != 0) i++; | 218 | while (QChar((*t)[i]).isLetter() && (*t)[i] != 0) i++; |
102 | if (m_fm->width(toQString(t->data()), i) > _e->x()) | 219 | if (t->width(i) > _e->x()) |
103 | { | 220 | { |
104 | wrd = toQString(t->data()+first, i - first); | 221 | wrd = toQString(t->data()+first, i - first); |
105 | break; | 222 | break; |
106 | } | 223 | } |
107 | // while ((*t)[i] == ' ' && (*t)[i] != 0) i++; | ||
108 | while (!QChar((*t)[i]).isLetter() && (*t)[i] != 0) i++; | 224 | while (!QChar((*t)[i]).isLetter() && (*t)[i] != 0) i++; |
109 | if ((*t)[i] == 0) break; | 225 | if ((*t)[i] == 0) break; |
110 | first = i; | 226 | first = i; |
@@ -112,14 +228,13 @@ void QTReader::mouseReleaseEvent( QMouseEvent* _e ) | |||
112 | } | 228 | } |
113 | if (!wrd.isEmpty()) | 229 | if (!wrd.isEmpty()) |
114 | { | 230 | { |
115 | QClipboard* cb = QApplication::clipboard(); | 231 | emit OnWordSelected(wrd, locnarray[lineno], (m_twotouch) ? wrd : toQString(textarray[lineno]->data())); |
116 | cb->setText(wrd); | 232 | } |
117 | Global::statusMessage(wrd); | 233 | } |
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 | } | 234 | } |
235 | else | ||
236 | { | ||
237 | mouseUpOn = true; | ||
123 | } | 238 | } |
124 | } | 239 | } |
125 | } | 240 | } |
@@ -135,7 +250,7 @@ void QTReader::focusOutEvent(QFocusEvent* e) | |||
135 | if (m_autoScroll) | 250 | if (m_autoScroll) |
136 | { | 251 | { |
137 | timer->stop(); | 252 | timer->stop(); |
138 | m_scrolldy = 0; | 253 | m_scrolldy1 = m_scrolldy2 = 0; |
139 | } | 254 | } |
140 | } | 255 | } |
141 | 256 | ||
@@ -245,15 +360,11 @@ void QTReader::keyPressEvent(QKeyEvent* e) | |||
245 | case Key_Right: | 360 | case Key_Right: |
246 | { | 361 | { |
247 | e->accept(); | 362 | e->accept(); |
248 | if (fontsizes[++m_textsize] == 0) | 363 | if (m_fontControl.increasesize()) |
249 | { | ||
250 | m_textsize--; | ||
251 | } | ||
252 | else | ||
253 | { | 364 | { |
254 | bool sc = m_autoScroll; | 365 | bool sc = m_autoScroll; |
366 | setfont(); | ||
255 | m_autoScroll = false; | 367 | m_autoScroll = false; |
256 | setfont(NULL); | ||
257 | locate(pagelocate()); | 368 | locate(pagelocate()); |
258 | update(); | 369 | update(); |
259 | m_autoScroll = sc; | 370 | m_autoScroll = sc; |
@@ -264,12 +375,11 @@ void QTReader::keyPressEvent(QKeyEvent* e) | |||
264 | case Key_Left: | 375 | case Key_Left: |
265 | { | 376 | { |
266 | e->accept(); | 377 | e->accept(); |
267 | if (m_textsize > 0) | 378 | if (m_fontControl.decreasesize()) |
268 | { | 379 | { |
269 | bool sc = m_autoScroll; | 380 | bool sc = m_autoScroll; |
270 | m_autoScroll = false; | 381 | m_autoScroll = false; |
271 | m_textsize--; | 382 | setfont(); |
272 | setfont(NULL); | ||
273 | locate(pagelocate()); | 383 | locate(pagelocate()); |
274 | update(); | 384 | update(); |
275 | m_autoScroll = sc; | 385 | m_autoScroll = sc; |
@@ -278,12 +388,10 @@ void QTReader::keyPressEvent(QKeyEvent* e) | |||
278 | } | 388 | } |
279 | break; | 389 | break; |
280 | case Key_Space: | 390 | case Key_Space: |
281 | // case Key_Enter: | ||
282 | case Key_Return: | 391 | case Key_Return: |
283 | { | 392 | { |
284 | e->accept(); | 393 | e->accept(); |
285 | setautoscroll(!m_autoScroll); | 394 | emit OnActionPressed(); |
286 | ((QTReaderApp*)parent()->parent())->setScrollState(m_autoScroll); | ||
287 | } | 395 | } |
288 | break; | 396 | break; |
289 | default: | 397 | default: |
@@ -297,15 +405,17 @@ void QTReader::setautoscroll(bool _sc) | |||
297 | if (m_autoScroll) | 405 | if (m_autoScroll) |
298 | { | 406 | { |
299 | m_autoScroll = false; | 407 | m_autoScroll = false; |
408 | QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; | ||
300 | } | 409 | } |
301 | else | 410 | else |
302 | { | 411 | { |
303 | m_autoScroll = true; | 412 | m_autoScroll = true; |
304 | autoscroll(); | 413 | autoscroll(); |
414 | QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Disable; // light is even not dimmed | ||
305 | } | 415 | } |
306 | } | 416 | } |
307 | 417 | ||
308 | bool QTReader::getline(CBuffer *buff) | 418 | bool QTReader::getline(CDrawBuffer *buff) |
309 | { | 419 | { |
310 | if (m_bMonoSpaced) | 420 | if (m_bMonoSpaced) |
311 | { | 421 | { |
@@ -330,48 +440,38 @@ void QTReader::doscroll() | |||
330 | bitBlt(this,0,0,this,0,1,width(),-1); | 440 | bitBlt(this,0,0,this,0,1,width(),-1); |
331 | qDrawPlainRect(&p,0,height() - 2,width(),2,white,1,&b); | 441 | qDrawPlainRect(&p,0,height() - 2,width(),2,white,1,&b); |
332 | 442 | ||
333 | if (++m_scrolldy == m_linespacing) | 443 | if (++m_scrolldy1 == textarray[0]->lineSpacing()) |
334 | { | 444 | { |
335 | setfont(&p); | 445 | CDrawBuffer* buff = textarray[0]; |
336 | m_scrolldy = 0; | 446 | for (int i = 1; i <= numlines; i++) |
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 | { | 447 | { |
342 | textarray[i-1] = textarray[i]; | 448 | textarray[i-1] = textarray[i]; |
343 | locnarray[i-1] = locnarray[i]; | 449 | locnarray[i-1] = locnarray[i]; |
344 | } | 450 | } |
345 | locnarray[numlines-1] = locate(); | 451 | textarray[numlines] = buff; |
346 | if (getline(buff)) | 452 | --numlines; |
347 | { | 453 | m_scrolldy1 = 0; |
348 | textarray[numlines-1] = buff; | ||
349 | drawText( p, 0, height() - m_descent - 2, buff->data()); | ||
350 | mylastpos = locate(); | ||
351 | } | 454 | } |
352 | else | 455 | if (++m_scrolldy2 == textarray[numlines]->lineSpacing()) |
353 | { | 456 | { |
354 | // (*buff)[0] = '\0'; | 457 | m_scrolldy2 = 0; |
355 | textarray[numlines-1] = buff; | 458 | numlines++; |
356 | m_autoScroll = false; | ||
357 | ((QTReaderApp*)parent()->parent())->setScrollState(m_autoScroll); | ||
358 | } | ||
359 | } | ||
360 | } | ||
361 | 459 | ||
362 | void QTReader::drawText(QPainter& p, int x, int y, tchar* _text) | 460 | if (textarray[numlines] == NULL) |
363 | { | ||
364 | QString text = toQString(_text); | ||
365 | if (m_bMonoSpaced) | ||
366 | { | ||
367 | for (int i = 0; i < text.length(); i++) | ||
368 | { | 461 | { |
369 | p.drawText( x+i*m_charWidth, y, QString(text[i]) ); | 462 | textarray[numlines] = new CDrawBuffer; |
370 | } | 463 | } |
371 | } | 464 | locnarray[numlines] = locate(); |
372 | else | 465 | int ch = getline(textarray[numlines]); |
466 | textarray[numlines-1]->render(&p, height() - textarray[numlines]->descent() - 2, m_bMonoSpaced, m_charWidth, width()); | ||
467 | mylastpos = locate(); | ||
468 | if (!ch) | ||
373 | { | 469 | { |
374 | p.drawText( x, y, text ); | 470 | m_autoScroll = false; |
471 | ((QTReaderApp*)parent()->parent())->setScrollState(m_autoScroll); | ||
472 | emit OnRedraw(); | ||
473 | } | ||
474 | emit OnRedraw(); | ||
375 | } | 475 | } |
376 | } | 476 | } |
377 | 477 | ||
@@ -380,47 +480,36 @@ void QTReader::autoscroll() | |||
380 | timer->start(real_delay(), false); | 480 | timer->start(real_delay(), false); |
381 | } | 481 | } |
382 | 482 | ||
383 | void QTReader::setfont(QPainter* p) | 483 | void QTReader::setfont() |
384 | { | 484 | { |
385 | // qDebug("Fontsize = %u",fontsizes[m_textsize]); | 485 | // m_fontControl.Change |
386 | // qDebug("SetFont %x",p); | 486 | m_charWidth = (m_charpc*m_fontControl.currentsize())/100; |
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; | 487 | if (m_charWidth <= 0) m_charWidth = 1; |
390 | // font.setFixedPitch(m_bMonoSpaced); | 488 | m_ascent = m_fontControl.ascent(); |
391 | // qDebug("Raw name = %s", (const char*)font.rawName()); | 489 | m_descent = m_fontControl.descent(); |
392 | if (p != NULL) p->setFont( font ); | 490 | m_linespacing = m_fontControl.lineSpacing(); |
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 | } | 491 | } |
406 | 492 | ||
407 | void QTReader::drawFonts( QPainter *p ) | 493 | void QTReader::drawFonts( QPainter *p ) |
408 | { | 494 | { |
409 | setfont(p); | 495 | setfont(); |
410 | if (m_lastwidth != width()) | 496 | if (m_lastwidth != width() || m_lastheight != height()) |
411 | { | 497 | { |
412 | m_lastwidth = width(); | 498 | m_lastwidth = width(); |
413 | locate(pagepos); | 499 | m_lastheight = height(); |
500 | locate(pagelocate()); | ||
414 | } | 501 | } |
415 | else | 502 | else |
416 | { | 503 | { |
504 | |||
505 | /* | ||
417 | int sl = screenlines(); | 506 | int sl = screenlines(); |
418 | if (sl < numlines) | 507 | if (sl < numlines) |
419 | { | 508 | { |
420 | // qDebug("df:<%u,%u>",sl,numlines); | 509 | // qDebug("df:<%u,%u>",sl,numlines); |
421 | 510 | ||
422 | size_t newpos = locnarray[sl]; | 511 | size_t newpos = locnarray[sl]; |
423 | CBuffer** nta = new CBuffer*[sl]; | 512 | CDrawBuffer** nta = new CDrawBuffer*[sl]; |
424 | size_t* nla = new size_t[sl]; | 513 | size_t* nla = new size_t[sl]; |
425 | for (int i = 0; i < sl; i++) | 514 | for (int i = 0; i < sl; i++) |
426 | { | 515 | { |
@@ -428,18 +517,17 @@ void QTReader::drawFonts( QPainter *p ) | |||
428 | nla[i] = locnarray[i]; | 517 | nla[i] = locnarray[i]; |
429 | } | 518 | } |
430 | for (int i = sl; i < numlines; i++) delete textarray[i]; | 519 | for (int i = sl; i < numlines; i++) delete textarray[i]; |
431 | delete [] textarray; | ||
432 | delete [] locnarray; | 520 | delete [] locnarray; |
521 | delete [] textarray; | ||
433 | textarray = nta; | 522 | textarray = nta; |
434 | locnarray = nla; | 523 | locnarray = nla; |
435 | numlines = sl; | 524 | numlines = sl; |
436 | jumpto(mylastpos = newpos); | 525 | jumpto(mylastpos = newpos); |
437 | // locate(pagepos); | ||
438 | } | 526 | } |
439 | if (sl > numlines) | 527 | if (sl > numlines) |
440 | { | 528 | { |
441 | // qDebug("df:<%u,%u>",sl,numlines); | 529 | // qDebug("df:<%u,%u>",sl,numlines); |
442 | CBuffer** nta = new CBuffer*[sl]; | 530 | CDrawBuffer** nta = new CDrawBuffer*[sl]; |
443 | size_t* nla = new size_t[sl]; | 531 | size_t* nla = new size_t[sl]; |
444 | for (int i = 0; i < numlines; i++) | 532 | for (int i = 0; i < numlines; i++) |
445 | { | 533 | { |
@@ -449,39 +537,33 @@ void QTReader::drawFonts( QPainter *p ) | |||
449 | if (locate() != mylastpos) jumpto(mylastpos); | 537 | if (locate() != mylastpos) jumpto(mylastpos); |
450 | for (int i = numlines; i < sl; i++) | 538 | for (int i = numlines; i < sl; i++) |
451 | { | 539 | { |
452 | nta[i] = new CBuffer; | 540 | nta[i] = new CDrawBuffer(&m_fontControl); |
453 | nla[i] = locate(); | 541 | nla[i] = locate(); |
454 | getline(nta[i]); | 542 | getline(nta[i]); |
455 | } | 543 | } |
456 | mylastpos = locate(); | 544 | mylastpos = locate(); |
457 | delete [] textarray; | ||
458 | delete [] locnarray; | 545 | delete [] locnarray; |
546 | delete [] textarray; | ||
459 | textarray = nta; | 547 | textarray = nta; |
460 | locnarray = nla; | 548 | locnarray = nla; |
461 | numlines = sl; | 549 | numlines = sl; |
462 | } | 550 | } |
463 | int ypos = (btight) ? 0 : m_ascent-m_linespacing; | 551 | */ |
464 | // int linespacing = (tight) ? m_ascent : m_ascent+m_descent; | 552 | if (numlines > 0) |
465 | for (int i = 0; i < numlines; i++) | ||
466 | { | 553 | { |
467 | drawText( *p, 0, ypos += m_linespacing, textarray[i]->data()); | 554 | int ypos = textarray[0]->ascent(); |
468 | } | 555 | textarray[0]->render( p, ypos, m_bMonoSpaced, m_charWidth, width()); |
469 | /* | 556 | for (int i = 1; i < numlines; i++) |
470 | |||
471 | |||
472 | |||
473 | int nlines = height()/(fontmetric.ascent()+fontmetric.descent()); | ||
474 | tchar buffer[1024]; | ||
475 | for (int i = 0; i < nlines; i++) | ||
476 | { | 557 | { |
477 | y += fontmetric.ascent(); | 558 | ypos += (textarray[i-1]->lineSpacing() + textarray[i]->lineSpacing())/2; |
478 | sprintf(buffer, "%d:%d:%s[%d]:Lines %d:%s", i+1, m_textfont, fonts[m_textfont], m_fs, nlines, (const tchar*)m_string); | 559 | textarray[i]->render( p, ypos, m_bMonoSpaced, m_charWidth, width()); |
479 | drawText( *p, 0, y, buffer ); | 560 | |
480 | y += fontmetric.descent(); | 561 | } |
562 | // mylastpos = locate(); | ||
481 | } | 563 | } |
482 | */ | ||
483 | } | 564 | } |
484 | m_scrolldy = 0; | 565 | m_scrolldy1 = m_scrolldy2 = 0; |
566 | emit OnRedraw(); | ||
485 | } | 567 | } |
486 | 568 | ||
487 | QString QTReader::firstword() | 569 | QString QTReader::firstword() |
@@ -516,44 +598,9 @@ QString QTReader::firstword() | |||
516 | // Construct the QTReader with buttons. | 598 | // Construct the QTReader with buttons. |
517 | // | 599 | // |
518 | 600 | ||
519 | void QTReader::ChangeFont(int tgt) | 601 | bool QTReader::ChangeFont(int tgt) |
520 | { | 602 | { |
521 | 603 | return m_fontControl.ChangeFont(m_fontname, tgt); | |
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 | } | 604 | } |
558 | 605 | ||
559 | void QTReader::init() | 606 | void QTReader::init() |
@@ -565,13 +612,15 @@ void QTReader::init() | |||
565 | // p.setBackgroundMode( Qt::OpaqueMode ); | 612 | // p.setBackgroundMode( Qt::OpaqueMode ); |
566 | buffdoc.setfilter(getfilter()); | 613 | buffdoc.setfilter(getfilter()); |
567 | ChangeFont(m_textsize); | 614 | ChangeFont(m_textsize); |
568 | // setFocusPolicy(QWidget::StrongFocus); | 615 | setFocusPolicy(QWidget::StrongFocus); |
569 | // resize( 240, 320 ); | 616 | // resize( 240, 320 ); |
570 | //setFocus(); | 617 | //setFocus(); |
571 | timer = new QTimer(this); | 618 | timer = new QTimer(this); |
572 | connect(timer, SIGNAL(timeout()), this, SLOT(doscroll())); | 619 | connect(timer, SIGNAL(timeout()), this, SLOT(doscroll())); |
573 | // QMessageBox::information(this, "init", m_lastfile, 1); | 620 | // QMessageBox::information(this, "init", m_lastfile, 1); |
574 | m_lastwidth = width(); | 621 | m_lastwidth = width(); |
622 | m_lastheight = height(); | ||
623 | setfont(); | ||
575 | if (!m_lastfile.isEmpty()) | 624 | if (!m_lastfile.isEmpty()) |
576 | { | 625 | { |
577 | m_string = DocLnk(m_lastfile).name(); | 626 | m_string = DocLnk(m_lastfile).name(); |
@@ -584,10 +633,6 @@ void QTReader::init() | |||
584 | // | 633 | // |
585 | QTReader::~QTReader() | 634 | QTReader::~QTReader() |
586 | { | 635 | { |
587 | if (fontsizes != NULL) delete [] fontsizes; | ||
588 | #ifndef QT_NO_PRINTER | ||
589 | // delete printer; | ||
590 | #endif | ||
591 | } | 636 | } |
592 | 637 | ||
593 | // | 638 | // |
@@ -672,76 +717,73 @@ unsigned int QTReader::screenlines() | |||
672 | return (height()-2)/(m_linespacing); | 717 | return (height()-2)/(m_linespacing); |
673 | }; | 718 | }; |
674 | 719 | ||
675 | bool QTReader::fillbuffer() { | 720 | bool QTReader::fillbuffer(int reuse, int ht) |
721 | { | ||
676 | if (buffdoc.empty()) return false; | 722 | if (buffdoc.empty()) return false; |
677 | //printf("Fillbuffer\n"); | 723 | m_scrolldy1 = m_scrolldy2 = 0; |
678 | m_scrolldy = 0; | ||
679 | int ch; | 724 | int ch; |
680 | bool ret = false; | 725 | bool ret = false; |
681 | int delta = screenlines(); | 726 | unsigned int oldpagepos = locnarray[reuse]; |
682 | // qDebug("fillbuffer:%u-%u",delta,numlines); | 727 | int ypos = ht; |
683 | if (delta != numlines) | 728 | numlines = reuse; |
729 | while (ypos < height()) | ||
684 | { | 730 | { |
685 | if (textarray != NULL) | 731 | if (textarray[numlines] == NULL) |
686 | { | 732 | { |
687 | for (int i = 0; i < numlines; i++) delete textarray[i]; | 733 | textarray[numlines] = new CDrawBuffer(&m_fontControl); |
688 | delete [] textarray; | ||
689 | delete [] locnarray; | ||
690 | } | 734 | } |
691 | numlines = delta; | 735 | locnarray[numlines] = locate(); |
692 | textarray = new CBuffer*[numlines]; | 736 | int ch = getline(textarray[numlines]); |
693 | locnarray = new size_t[numlines]; | 737 | ypos += textarray[numlines]->lineSpacing(); |
694 | for (int i = 0; i < numlines; i++) textarray[i] = new CBuffer; | 738 | numlines++; |
695 | } | ||
696 | // qDebug("fillbuffer:pagepos:%u",pagepos); | ||
697 | unsigned int oldpagepos = pagepos; | ||
698 | // if (textarray != NULL) | ||
699 | // pagepos = locnarray[0]; | ||
700 | // else | ||
701 | pagepos = locate(); | ||
702 | for (int i = 0; i < delta; i++) | ||
703 | { | ||
704 | locnarray[i] = locate(); | ||
705 | ch = getline(textarray[i]); | ||
706 | // if (ch == EOF) { | ||
707 | if (!ch) | 739 | if (!ch) |
708 | { | 740 | { |
709 | if (i == 0) | 741 | if (numlines - reuse == 1/* && locnarray[0] == buffdoc.locate()*/) |
710 | { | 742 | { |
711 | locate(oldpagepos); | 743 | locate(oldpagepos); |
712 | return false; | 744 | return false; |
713 | } | 745 | } |
714 | else | 746 | else |
715 | { | 747 | { |
716 | ret = true; | 748 | --numlines; |
717 | for (int j = i+1; j < delta; j++) | 749 | mylastpos = locate(); |
718 | { | 750 | return true; |
719 | locnarray[j] = locnarray[j-1]; | ||
720 | (*(textarray[j]))[0] = '\0'; | ||
721 | } | ||
722 | break; | ||
723 | } | 751 | } |
724 | } | 752 | } |
725 | if (ch == '\012') ret = true; | ||
726 | } | 753 | } |
754 | |||
755 | --numlines; | ||
727 | mylastpos = locate(); | 756 | mylastpos = locate(); |
728 | // qDebug("fillbuffer:lastpos:%u",mylastpos); | 757 | |
729 | return true; | 758 | return true; |
730 | } | 759 | } |
731 | 760 | ||
732 | 761 | ||
733 | void QTReader::dopagedn() | 762 | void QTReader::dopagedn() |
734 | { | 763 | { |
735 | if (m_overlap == 0) | 764 | int skip = 0, ypos = 0; |
765 | if (locate() != mylastpos) | ||
736 | { | 766 | { |
737 | if (locate() != mylastpos) jumpto(mylastpos); | 767 | //qDebug("Jumping to %u", mylastpos); |
768 | jumpto(mylastpos); | ||
738 | } | 769 | } |
739 | else | 770 | CDrawBuffer* reusebuffer = textarray[numlines]; |
771 | if (reusebuffer != NULL) | ||
772 | { | ||
773 | for (int i = 0; i <= m_overlap; i++) | ||
740 | { | 774 | { |
741 | if (m_overlap >= screenlines()) m_overlap = screenlines()/2; | 775 | int offset = numlines - m_overlap + i; |
742 | jumpto(locnarray[screenlines()-m_overlap]); | 776 | reusebuffer = textarray[offset]; |
777 | size_t reuselocn = locnarray[offset]; | ||
778 | textarray[offset] = textarray[i]; | ||
779 | textarray[i] = reusebuffer; | ||
780 | locnarray[offset] = locnarray[i]; | ||
781 | locnarray[i] = reuselocn; | ||
782 | ypos += textarray[i]->lineSpacing(); | ||
783 | skip++; | ||
743 | } | 784 | } |
744 | if (fillbuffer()) | 785 | } |
786 | if (fillbuffer(skip, ypos)) | ||
745 | { | 787 | { |
746 | update(); | 788 | update(); |
747 | } | 789 | } |
@@ -749,86 +791,82 @@ void QTReader::dopagedn() | |||
749 | 791 | ||
750 | void QTReader::dopageup() | 792 | void QTReader::dopageup() |
751 | { | 793 | { |
752 | CBuffer** buff = textarray; | 794 | CBufferFace<CDrawBuffer*> buff; |
753 | unsigned int *loc = new unsigned int[numlines]; | 795 | CBufferFace<size_t> loc; |
754 | int cbptr = 0; | 796 | unsigned int target = locnarray[(m_overlap < numlines) ? m_overlap : numlines/2]; |
755 | if (locate() != mylastpos) jumpto(mylastpos); | 797 | |
756 | if (m_overlap >= screenlines()) m_overlap = screenlines()/2; | 798 | size_t delta; |
757 | unsigned int target = locnarray[m_overlap]; | 799 | if (target < 2048) |
758 | if (buffdoc.hasrandomaccess()) | ||
759 | { | ||
760 | unsigned int delta = locate()-pagelocate(); | ||
761 | if (delta < 64) delta = 64; | ||
762 | if (delta % 2 != 0) delta++; | ||
763 | if (target % 2 != 0) target++; | ||
764 | do | ||
765 | { | ||
766 | delta <<= 1; | ||
767 | if (delta >= target) | ||
768 | { | 800 | { |
769 | delta = target; | 801 | delta = target; |
770 | jumpto(0); | 802 | jumpto(0); |
771 | for (int i = 0; i < numlines; i++) | ||
772 | { | ||
773 | loc[i] = locate(); | ||
774 | getline(buff[i]); | ||
775 | } | ||
776 | break; | ||
777 | } | 803 | } |
804 | else | ||
805 | { | ||
806 | delta = 2048; | ||
807 | |||
778 | jumpto(target-delta); | 808 | jumpto(target-delta); |
809 | |||
810 | buff[0] = new CDrawBuffer(&m_fontControl); | ||
811 | |||
779 | do | 812 | do |
780 | { | 813 | { |
781 | getline(buff[0]); | 814 | |
782 | #ifdef WS | 815 | if (!getline(buff[0])) break; |
783 | //printf("Trying:%s\n",buff[0]); | 816 | |
784 | #endif | ||
785 | if (locate() > target) continue; | 817 | if (locate() > target) continue; |
786 | } | 818 | } |
787 | while (!buffdoc.iseol()); | 819 | while (!buffdoc.iseol()); |
788 | for (int i = 0; i < numlines; i++) | ||
789 | { | ||
790 | loc[i] = locate(); | ||
791 | getline(buff[i]); | ||
792 | #ifdef WS | ||
793 | //printf("Filling:%s\n",buff[i]); | ||
794 | #endif | ||
795 | } | ||
796 | } | 820 | } |
797 | while (locate() >= target && delta < 4096); | 821 | int nbfl = 0; |
798 | #ifdef WS | 822 | |
799 | //printf("Delta:%u\n",delta); | 823 | bool ch = true; |
800 | #endif | 824 | int ypos = 0; |
825 | while (locate() < target) | ||
826 | { | ||
827 | if (buff[nbfl] == NULL) buff[nbfl] = new CDrawBuffer(&m_fontControl); | ||
828 | loc[nbfl] = locate(); | ||
829 | ch = getline(buff[nbfl]); | ||
830 | ypos += buff[nbfl]->lineSpacing(); | ||
831 | nbfl++; | ||
832 | if (!ch) break; | ||
801 | } | 833 | } |
802 | else | 834 | if (ypos < height()) |
803 | { | 835 | { |
804 | jumpto(0); | 836 | locate(0); |
805 | for (int i = 0; i < numlines; i++) | 837 | return; |
838 | } | ||
839 | if (ch) | ||
806 | { | 840 | { |
807 | loc[i] = locate(); | 841 | if (buff[nbfl] == NULL) buff[nbfl] = new CDrawBuffer(&m_fontControl); |
808 | getline(buff[i]); | 842 | loc[nbfl] = locate(); |
843 | int ch = getline(buff[nbfl]); | ||
844 | nbfl++; | ||
809 | } | 845 | } |
846 | ypos = 0; | ||
847 | numlines = 0; | ||
848 | while (ypos < height() && numlines <= nbfl-2) | ||
849 | { | ||
850 | ypos += buff[nbfl - numlines - 2]->lineSpacing(); | ||
851 | numlines++; | ||
810 | } | 852 | } |
811 | cbptr = 0; | 853 | --numlines; |
812 | while (locate() < target) | 854 | int offset = nbfl; |
855 | offset -= numlines+1; | ||
856 | for (int i = 0; i <= numlines; i++) | ||
813 | { | 857 | { |
814 | loc[cbptr] = locate(); | 858 | delete textarray[i]; |
815 | getline(buff[cbptr]); | 859 | textarray[i] = buff[offset+i]; |
816 | #ifdef WS | 860 | locnarray[i] = loc[offset + i]; |
817 | //printf("Adding:%s\n",buff[cbptr]->data()); | ||
818 | #endif | ||
819 | cbptr = (cbptr+1) % numlines; | ||
820 | } | 861 | } |
821 | pagepos = loc[cbptr]; | 862 | for (int i = 0; i < nbfl - numlines - 1; i++) |
822 | textarray = new CBuffer*[numlines]; | ||
823 | for (int i = 0; i < numlines; i++) | ||
824 | { | 863 | { |
825 | int j = (cbptr+i)%numlines; | 864 | delete buff[i]; |
826 | textarray[i] = buff[j]; | ||
827 | locnarray[i] = loc[j]; | ||
828 | } | 865 | } |
829 | delete [] buff; | 866 | |
830 | delete [] loc; | 867 | // --numlines; |
831 | mylastpos = locate(); | 868 | mylastpos = locate(); |
869 | |||
832 | update(); | 870 | update(); |
833 | } | 871 | } |
834 | 872 | ||
@@ -852,6 +890,7 @@ bool QTReader::load_file(const char *newfile, unsigned int _lcn) | |||
852 | locate(lcn); | 890 | locate(lcn); |
853 | // qDebug("buffdoc.locate done"); | 891 | // qDebug("buffdoc.locate done"); |
854 | } | 892 | } |
893 | setfilter(getfilter()); | ||
855 | update(); | 894 | update(); |
856 | // qDebug("Updated"); | 895 | // qDebug("Updated"); |
857 | return bRC; | 896 | return bRC; |
@@ -859,23 +898,33 @@ bool QTReader::load_file(const char *newfile, unsigned int _lcn) | |||
859 | 898 | ||
860 | void QTReader::lineDown() | 899 | void QTReader::lineDown() |
861 | { | 900 | { |
862 | pagepos = locnarray[1]; | 901 | int ypos = 0; |
863 | CBuffer* buff = textarray[0]; | 902 | int offset = numlines; |
864 | for (int i = 1; i < numlines; i++) | 903 | |
904 | for (int i = 0; i <= numlines; i++) | ||
865 | { | 905 | { |
866 | textarray[i-1] = textarray[i]; | 906 | if ((ypos += textarray[numlines-i]->lineSpacing()) > height()) |
867 | locnarray[i-1] = locnarray[i]; | 907 | { |
908 | offset = i-1; | ||
909 | break; | ||
910 | } | ||
868 | } | 911 | } |
869 | locnarray[numlines-1] = locate(); | 912 | offset = numlines - offset; |
870 | if (getline(buff)) | 913 | for (int i = offset; i <= numlines; i++) |
871 | { | 914 | { |
872 | textarray[numlines-1] = buff; | 915 | CDrawBuffer* buff = textarray[i-offset]; |
873 | mylastpos = locate(); | 916 | textarray[i-offset] = textarray[i]; |
917 | locnarray[i-offset] = locnarray[i]; | ||
918 | textarray[i] = buff; | ||
874 | } | 919 | } |
875 | else | 920 | numlines = numlines - offset + 1; |
921 | locnarray[numlines] = locate(); | ||
922 | if (textarray[numlines] == NULL) | ||
876 | { | 923 | { |
877 | textarray[numlines-1] = buff; | 924 | textarray[numlines] = new CDrawBuffer(&m_fontControl); |
878 | } | 925 | } |
926 | getline(textarray[numlines]); | ||
927 | mylastpos = locate(); | ||
879 | update(); | 928 | update(); |
880 | } | 929 | } |
881 | /* | 930 | /* |
@@ -963,12 +1012,13 @@ void QTReader::lineUp() | |||
963 | */ | 1012 | */ |
964 | void QTReader::lineUp() | 1013 | void QTReader::lineUp() |
965 | { | 1014 | { |
966 | CBuffer* buff = textarray[numlines-1]; | 1015 | CDrawBuffer* buff = textarray[numlines]; |
967 | unsigned int loc; | 1016 | unsigned int loc; |
968 | unsigned int end = locnarray[numlines-1]; | 1017 | unsigned int end = locnarray[numlines]; |
969 | int cbptr = 0; | 1018 | int cbptr = 0; |
970 | if (locate() != mylastpos) jumpto(mylastpos); | 1019 | if (locate() != mylastpos) jumpto(mylastpos); |
971 | unsigned int target = locnarray[0]; | 1020 | unsigned int target = locnarray[0]; |
1021 | if (target == 0) return; | ||
972 | if (buffdoc.hasrandomaccess()) | 1022 | if (buffdoc.hasrandomaccess()) |
973 | { | 1023 | { |
974 | unsigned int delta = locate()-pagelocate(); | 1024 | unsigned int delta = locate()-pagelocate(); |
@@ -980,11 +1030,8 @@ void QTReader::lineUp() | |||
980 | { | 1030 | { |
981 | delta = target; | 1031 | delta = target; |
982 | jumpto(0); | 1032 | jumpto(0); |
983 | for (int i = 0; i < numlines; i++) | ||
984 | { | ||
985 | loc = locate(); | 1033 | loc = locate(); |
986 | getline(buff); | 1034 | getline(buff); |
987 | } | ||
988 | break; | 1035 | break; |
989 | } | 1036 | } |
990 | jumpto(target-delta); | 1037 | jumpto(target-delta); |
@@ -1014,18 +1061,27 @@ void QTReader::lineUp() | |||
1014 | loc = locate(); | 1061 | loc = locate(); |
1015 | getline(buff); | 1062 | getline(buff); |
1016 | } | 1063 | } |
1017 | pagepos = loc; | 1064 | for (int i = numlines; i > 0; i--) |
1018 | for (int i = numlines-1; i > 0; i--) | ||
1019 | { | 1065 | { |
1020 | textarray[i] = textarray[i-1]; | 1066 | textarray[i] = textarray[i-1]; |
1021 | locnarray[i] = locnarray[i-1]; | 1067 | locnarray[i] = locnarray[i-1]; |
1022 | } | 1068 | } |
1023 | textarray[0] = buff; | 1069 | textarray[0] = buff; |
1024 | locnarray[0] = loc; | 1070 | locnarray[0] = loc; |
1025 | // delete [] buff; | 1071 | int start = numlines; |
1026 | // delete [] loc; | 1072 | int ypos = 0; |
1027 | mylastpos = locate(); | 1073 | for (int i = 0; i <= numlines; i++) |
1028 | jumpto(end); | 1074 | { |
1075 | ypos += textarray[i]->lineSpacing(); | ||
1076 | if (ypos > height()) | ||
1077 | { | ||
1078 | start = i; | ||
1079 | ypos -= textarray[i]->lineSpacing(); | ||
1080 | break; | ||
1081 | } | ||
1082 | } | ||
1083 | jumpto(locnarray[start]); | ||
1084 | fillbuffer(start, ypos); | ||
1029 | update(); | 1085 | update(); |
1030 | } | 1086 | } |
1031 | 1087 | ||
@@ -1033,3 +1089,21 @@ bool QTReader::empty() | |||
1033 | { | 1089 | { |
1034 | return buffdoc.empty(); | 1090 | return buffdoc.empty(); |
1035 | } | 1091 | } |
1092 | |||
1093 | MarkupType QTReader::PreferredMarkup() | ||
1094 | { | ||
1095 | MarkupType m = buffdoc.PreferredMarkup(); | ||
1096 | if (m == cTEXT) | ||
1097 | { | ||
1098 | int ext = m_lastfile.findRev('.'); | ||
1099 | if (ext >= 0) | ||
1100 | { | ||
1101 | QString ft = m_lastfile.right(m_lastfile.length()-ext-1).upper(); | ||
1102 | if (ft.left(3) == "HTM") | ||
1103 | { | ||
1104 | m = cHTML; | ||
1105 | } | ||
1106 | } | ||
1107 | } | ||
1108 | return m; | ||
1109 | } | ||