1 #include <QtGui/QApplication>
2 #include <QtGui/QFrame>
3 #include <Qt3Support/q3multilineedit.h>
8 #include <QtGui/QLabel>
11 #include <QtGui/QPushButton>
12 #include <QtGui/QRadioButton>
13 #include <QtCore/QList>
14 #include <QtCore/qfile.h>
23 #include <QtCore/qfile.h>
24 #include <QtCore/QTextStream>
38 #define TAG_BOLD_OFF 5
39 #define TAG_ITALIC_ON 6
40 #define TAG_ITALIC_OFF 7
43 #define TAG_LINK_END 10
44 #define TAG_LIST_ON 11
45 #define TAG_LIST_OFF 12
46 #define TAG_LIST_ITEM 13
48 #define PIX_HEIGHT 2000
50 char names[14][40] = {
51 "text", "other", "link", "anchor", "bold on",
52 "bold off", "italic on", "italic off", "break",
53 "line", "end link", "list on", "list off", "list item"
60 strcpy(tag_label, "");
82 void AnalyzeTag(QString*);
85 Tag *CheckTag(int, int);
86 Tag *FindAnchor(char*);
89 HTMLAnalyzer::HTMLAnalyzer()
95 bool HTMLAnalyzer::LoadFile(char *fname)
104 if (!f.open(IO_ReadOnly))
110 poms = fs.readLine();
111 while (poms.length()>0) {
115 poms1 = poms.left(i);
117 pomt->tag_type = TAG_TEXT;
119 poms1 = poms1.simplifyWhiteSpace();
122 sprintf(pomt->tag_text, " %s",
126 poms = poms.right(poms.length() - i);
130 poms1 = poms.mid(1, i - 1);
132 poms = poms.right(poms.length() - i -1);
139 pomt->tag_type = TAG_TEXT;
141 poms=poms.simplifyWhiteSpace();
143 sprintf(pomt->tag_text, " %s", poms.data());
150 pomt->tag_type = TAG_BREAK;
158 void HTMLAnalyzer::AnalyzeTag(QString *t)
164 *t = t->simplifyWhiteSpace();
167 if ((t->data()[0] != 'A') && (t->data()[0] != 'a')) {
169 if (strcmp(t->data(), "B") ==0 ) {
170 pom->tag_type = TAG_BOLD_ON;
172 else if (strcmp(t->data(), "/B") == 0) {
173 pom->tag_type = TAG_BOLD_OFF;
175 else if (strcmp(t->data(), "I") == 0) {
176 pom->tag_type = TAG_ITALIC_ON;
178 else if (strcmp(t->data(), "/I") == 0) {
179 pom->tag_type = TAG_ITALIC_OFF;
181 else if (strcmp(t->data(), "BR") == 0) {
182 pom->tag_type = TAG_BREAK;
184 else if (strcmp(t->data(), "HR") == 0) {
185 pom->tag_type = TAG_LINE;
187 else if (strcmp(t->data(), "/A") == 0) {
188 pom->tag_type = TAG_LINK_END;
190 else if (strcmp(t->data(), "UL") == 0) {
191 pom->tag_type = TAG_LIST_ON;
193 else if (strcmp(t->data(), "/UL") == 0) {
194 pom->tag_type = TAG_LIST_OFF;
196 else if (strcmp(t->data(), "LI") == 0) {
197 pom->tag_type = TAG_LIST_ITEM;
199 else if (strcmp(t->data(), "PRE") == 0) {
202 else if (strcmp(t->data(),"/PRE") == 0) {
212 poms = t->mid(2, i - 2);
213 poms = poms.simplifyWhiteSpace();
215 poms1 = t->right(t->length() - i - 1);
216 poms1 = poms1.simplifyWhiteSpace();
218 if (strcmp(poms.data(),"HREF") == 0) {
219 pom->tag_type = TAG_LINK;
220 strcpy(pom->tag_link,poms1.data());
222 else if (strcmp(poms.data(), "NAME") == 0) {
223 pom->tag_type = TAG_ANCHOR;
224 if (poms1.data()[0] == '"')
225 poms1=poms1.right(poms1.length() - 1);
226 if (poms1.data()[poms1.length() - 1] == '"')
227 poms1=poms1.left(poms1.length() - 1);
228 strcpy(pom->tag_label, poms1.data());
235 void HTMLAnalyzer::DumpList()
239 while (pom != NULL) {
240 fprintf(stderr, "%s:%s,%s,%s\n", names[pom->tag_type],
241 pom->tag_text, pom->tag_link, pom->tag_label);
246 void HTMLAnalyzer::PackLinks()
253 if ((pom->tag_type == TAG_LINK) ||
254 (pom->tag_type == TAG_ANCHOR)) {
257 while ((pom1 != NULL) &&
258 (pom1->tag_type != TAG_LINK_END)) {
259 if (pom1->tag_type == TAG_TEXT)
260 strcat(s,pom1->tag_text);
262 pom1 = tags.current();
264 strcpy(pom->tag_text,s);
266 pom = tags.current();
274 Tag *HTMLAnalyzer::CheckTag(int x,int y)
278 while (pom != NULL) {
279 if (pom->tag_type==TAG_LINK)
280 if ((x >= pom->x) && (x <= pom->x + pom->w) &&
281 (y >= pom->y) && (y <= pom->y + pom->h)) {
289 Tag *HTMLAnalyzer::FindAnchor(char *name)
293 while (pom != NULL) {
294 if ((pom->tag_type == TAG_ANCHOR) &&
295 (strcmp(pom->tag_label, name) == 0)) {
302 /********************************/
304 class QHTML: public QFrame {
309 HTMLAnalyzer *analyzer;
311 int cx,cy,oy,lstep,pstep;
313 QFont *normal,*bold,*italic,*bold_italic,*actual_font;
318 virtual void paintEvent(QPaintEvent *ev);
319 virtual void resizeEvent(QResizeEvent *ev);
320 virtual void mousePressEvent(QMouseEvent *ev);
333 QHTML::QHTML(char *d)
338 QFont f("Helvetica", 12, QFont::Bold);
339 normal = new QFont("Helvetica", 12, QFont::Normal);
340 bold = new QFont("Helvetica", 12, QFont::Bold);
341 italic = new QFont("Helvetica", 12, QFont::Normal, TRUE);
342 bold_italic = new QFont("Helvetica", 12, QFont::Bold, TRUE);
345 actual_font = normal;
346 bar = new QMenuBar(this);
348 pp->insertItem("Index", this, SLOT(contents()));
349 pp->insertItem("User Guide", this, SLOT(user_guide()));
350 pp->insertItem("Language reference", this, SLOT(lang_guide()));
352 /*pp->setStyle(WindowsStyle);*/
353 bar->insertItem("File", this, SLOT(load()));
354 bar->insertItem("Contents", pp);
355 bar->insertItem("Quit", app, SLOT(quit()));
357 setCaption("LOGLAN Help");
358 setBackgroundColor(gray);
359 analyzer = new HTMLAnalyzer;
360 map = new QPixmap(500, PIX_HEIGHT);
361 map->fill(backgroundColor());
362 resize(500, height());
363 setFixedSize(width(), height());
366 pstep = height() - bar->height();
367 vscroll = new QScrollBar(0, PIX_HEIGHT, lstep, pstep, 0,
368 QScrollBar::Vertical, this);
369 vscroll->setTracking(TRUE);
370 vscroll->setGeometry(width() - 16, bar->height(), 16,
371 height()-bar->height());
372 connect(vscroll, SIGNAL(valueChanged(int)), this, SLOT(vscrolled(int)));
373 sprintf(s, "%s/index.html", homedir);
374 analyzer->LoadFile(s);
375 analyzer->PackLinks();
379 void QHTML::vscrolled(int v)
387 QString s(QFileDialog::getOpenFileName(homedir, "*", this));
389 vscroll->setValue(0);
390 analyzer->LoadFile((char*)s.ascii());
391 analyzer->PackLinks();
396 void QHTML::contents()
399 sprintf(ss, "%s/index.html", homedir);
400 analyzer->LoadFile(ss);
401 analyzer->PackLinks();
405 void QHTML::user_guide()
408 sprintf(ss, "%s/userg.html", homedir);
409 analyzer->LoadFile(ss);
410 analyzer->PackLinks();
414 void QHTML::lang_guide()
417 sprintf(ss, "%s/langg.html", homedir);
418 analyzer->LoadFile(ss);
419 analyzer->PackLinks();
423 void QHTML::paintEvent(QPaintEvent *ev)
426 bitBlt(this, 0, bar->height(), map, 0, oy,
427 width() - 16, height() - 16);
431 void QHTML::resizeEvent(QResizeEvent *ev)
436 void QHTML::mousePressEvent(QMouseEvent *ev)
442 pom = analyzer->CheckTag(ev->x(), ev->y() + oy);
444 poms.sprintf(pom->tag_link);
445 if (poms.data()[0] == '"')
446 poms = poms.right(poms.length() - 1);
447 if (poms.data()[poms.length() - 1] == '"')
448 poms=poms.left(poms.length() - 1);
449 if (poms.data()[0] == '#') {
450 poms = poms.right(poms.length() - 1);
451 pom1 = analyzer->FindAnchor((char*)poms.ascii());
453 vscroll->setValue(pom1->y);
456 sprintf(ss, "%s/%s", homedir, poms.data());
457 analyzer->LoadFile(ss);
458 analyzer->PackLinks();
465 * @attention Currently not in use
471 void QHTML::DrawList()
476 if (!analyzer->tags.isEmpty()) {
479 map->fill(backgroundColor());
482 pom = analyzer->tags.first();
483 while (pom != NULL) {
484 switch(pom->tag_type) {
486 p.setFont(*actual_font);
487 if (cx + p.fontMetrics().width(pom->tag_text) >
490 cy = cy + p.fontMetrics().height();
492 p.drawText(cx, cy, pom->tag_text);
493 cx = cx + p.fontMetrics().width(pom->tag_text);
496 p.setFont(*actual_font);
497 cy = cy + p.fontMetrics().height();
501 if (actual_font == bold)
502 actual_font = bold_italic;
504 actual_font = italic;
508 if (actual_font == bold_italic)
511 actual_font = normal;
515 if (actual_font == italic)
516 actual_font = bold_italic;
522 if (actual_font == bold_italic)
523 actual_font = italic;
525 actual_font = normal;
529 p.setFont(*actual_font);
530 if (cx + p.fontMetrics().width(pom->tag_text) >
533 cy = cy + p.fontMetrics().height();
535 p.setPen(QColor(255, 0, 0));
536 p.drawText(cx, cy, pom->tag_text);
538 pom->y = cy + p.fontMetrics().height();
539 pom->w = p.fontMetrics().width(pom->tag_text);
540 pom->h = p.fontMetrics().height();
541 p.setPen(QColor(0, 0, 0));
542 cx = cx + p.fontMetrics().width(pom->tag_text);
545 p.setFont(*actual_font);
546 if (cx + p.fontMetrics().width(pom->tag_text) >
549 cy = cy + p.fontMetrics().height();
551 //p.setPen(QColor(0,255,0));
552 p.drawText(cx, cy, pom->tag_text);
554 pom->y = cy - p.fontMetrics().height();
555 pom->w = p.fontMetrics().width(pom->tag_text);
556 pom->h = p.fontMetrics().height();
557 p.setPen(QColor(0, 0, 0));
558 cx=cx + p.fontMetrics().width(pom->tag_text);
562 p.setFont(*actual_font);
564 cy = cy + p.fontMetrics().height();
566 case TAG_LIST_ITEM:p.setFont(*actual_font);
567 p.setBrush(QBrush(QColor(0, 0, 255)));
569 cy = cy + p.fontMetrics().height();
570 p.drawPie(cx, cy - 5, 5, 5, 0, 5760);
574 p.setFont(*actual_font);
576 cy = cy + p.fontMetrics().height();
578 (int)(p.fontMetrics().height() / 2),
579 width() - 16 - 5, cy -
580 (int)(p.fontMetrics().height() / 2));
582 cy = cy + p.fontMetrics().height();
585 pom = analyzer->tags.next();
594 int main( int argc, char **argv )
598 app = new QApplication(argc, argv);
603 QHTML cfg((char*)ps.ascii());
604 //app->setStyle(WindowsStyle);
605 app->setMainWidget(&cfg);