1 #include <QtGui/QApplication>
2 #include <QtGui/QMainWindow>
3 #include <Qt3Support/q3multilineedit.h>
4 #include <QtGui/QMenuBar>
5 // #include <qpopmenu.h>
6 #include <QtGui/QDialog>
7 #include <QtGui/QButtonGroup>
8 #include <QtGui/QLabel>
9 #include <QtGui/QLineEdit>
10 #include <Qt3Support/Q3ListBox>
11 #include <QtGui/QPushButton>
12 #include <QtGui/QRadioButton>
13 #include <QtCore/QList>
14 #include <QtCore/qfile.h>
15 #include <QtGui/qcombobox.h>
16 #include <QtGui/QToolTip>
17 #include <QtGui/QFont>
18 #include <QtGui/QPixmap>
19 #include <QtGui/QMessageBox>
23 #include <QtCore/QTextStream>
24 #include <QtCore/qstring.h>
25 #include <QtGui/QFileDialog>
26 #include <QtGui/QFontMetrics>
27 #include <QtGui/QPainter>
28 #include <QtGui/QScrollBar>
37 #define TAG_BOLD_OFF 5
38 #define TAG_ITALIC_ON 6
39 #define TAG_ITALIC_OFF 7
42 #define TAG_LINK_END 10
43 #define TAG_LIST_ON 11
44 #define TAG_LIST_OFF 12
45 #define TAG_LIST_ITEM 13
47 #define PIX_HEIGHT 2000
49 char names[14][40] = {
50 "text", "other", "link", "anchor", "bold on",
51 "bold off", "italic on", "italic off", "break",
52 "line", "end link", "list on", "list off", "list item"
59 strcpy(tag_label, "");
81 void AnalyzeTag(QString*);
84 Tag *CheckTag(int, int);
85 Tag *FindAnchor(char*);
88 HTMLAnalyzer::HTMLAnalyzer()
94 bool HTMLAnalyzer::LoadFile(char *fname)
103 if (!f.open(IO_ReadOnly))
108 while (!fs.atEnd()) {
109 poms = fs.readLine();
110 while (poms.length()>0) {
114 poms1 = poms.left(i);
116 pomt->tag_type = TAG_TEXT;
118 poms1 = poms1.simplifyWhiteSpace();
121 sprintf(pomt->tag_text, " %s", poms1.toStdString().c_str());
124 poms = poms.right(poms.length() - i);
128 poms1 = poms.mid(1, i - 1);
130 poms = poms.right(poms.length() - i -1);
137 pomt->tag_type = TAG_TEXT;
139 poms=poms.simplifyWhiteSpace();
141 sprintf(pomt->tag_text, " %s", poms.toStdString().c_str());
148 pomt->tag_type = TAG_BREAK;
156 void HTMLAnalyzer::AnalyzeTag(QString *t)
162 *t = t->simplifyWhiteSpace();
165 if ((t->toStdString().c_str()[0] != 'A') && (t->toStdString().c_str()[0] != 'a')) {
167 if (strcmp(t->toStdString().c_str(), "B") ==0 ) {
168 pom->tag_type = TAG_BOLD_ON;
170 else if (strcmp(t->toStdString().c_str(), "/B") == 0) {
171 pom->tag_type = TAG_BOLD_OFF;
173 else if (strcmp(t->toStdString().c_str(), "I") == 0) {
174 pom->tag_type = TAG_ITALIC_ON;
176 else if (strcmp(t->toStdString().c_str(), "/I") == 0) {
177 pom->tag_type = TAG_ITALIC_OFF;
179 else if (strcmp(t->toStdString().c_str(), "BR") == 0) {
180 pom->tag_type = TAG_BREAK;
182 else if (strcmp(t->toStdString().c_str(), "HR") == 0) {
183 pom->tag_type = TAG_LINE;
185 else if (strcmp(t->toStdString().c_str(), "/A") == 0) {
186 pom->tag_type = TAG_LINK_END;
188 else if (strcmp(t->toStdString().c_str(), "UL") == 0) {
189 pom->tag_type = TAG_LIST_ON;
191 else if (strcmp(t->toStdString().c_str(), "/UL") == 0) {
192 pom->tag_type = TAG_LIST_OFF;
194 else if (strcmp(t->toStdString().c_str(), "LI") == 0) {
195 pom->tag_type = TAG_LIST_ITEM;
197 else if (strcmp(t->toStdString().c_str(), "PRE") == 0) {
200 else if (strcmp(t->toStdString().c_str(),"/PRE") == 0) {
210 poms = t->mid(2, i - 2);
211 poms = poms.simplifyWhiteSpace();
213 poms1 = t->right(t->length() - i - 1);
214 poms1 = poms1.simplifyWhiteSpace();
216 if (poms == "HREF") {
217 pom->tag_type = TAG_LINK;
218 strcpy(pom->tag_link,poms1.toStdString().c_str());
220 else if (poms == "NAME") {
221 pom->tag_type = TAG_ANCHOR;
223 poms1=poms1.right(poms1.length() - 1);
224 if (poms1[poms1.length() - 1] == '"')
225 poms1=poms1.left(poms1.length() - 1);
226 strcpy(pom->tag_label, poms1.toStdString().c_str());
233 void HTMLAnalyzer::DumpList()
237 while (pom != NULL) {
238 fprintf(stderr, "%s:%s,%s,%s\n", names[pom->tag_type],
239 pom->tag_text, pom->tag_link, pom->tag_label);
240 pom=tags.takeFirst();
244 void HTMLAnalyzer::PackLinks()
251 if ((pom->tag_type == TAG_LINK) ||
252 (pom->tag_type == TAG_ANCHOR)) {
253 pom1 = tags.takeFirst();
255 while ((pom1 != NULL) &&
256 (pom1->tag_type != TAG_LINK_END)) {
257 if (pom1->tag_type == TAG_TEXT)
258 strcat(s,pom1->tag_text);
259 tags.removeOne(pom1);
260 pom1 = tags.current();
262 strcpy(pom->tag_text,s);
263 tags.removeOne(pom1);
264 pom = tags.current();
266 pom = tags.takeFirst();
272 Tag *HTMLAnalyzer::CheckTag(int x,int y)
276 while (pom != NULL) {
277 if (pom->tag_type==TAG_LINK)
278 if ((x >= pom->x) && (x <= pom->x + pom->w) &&
279 (y >= pom->y) && (y <= pom->y + pom->h)) {
282 pom=tags.takeFirst();
287 Tag *HTMLAnalyzer::FindAnchor(char *name)
291 while (pom != NULL) {
292 if ((pom->tag_type == TAG_ANCHOR) &&
293 (strcmp(pom->tag_label, name) == 0)) {
296 pom=tags.takeFirst();
300 /********************************/
302 class QHTML: public QMainWindow {
307 HTMLAnalyzer *analyzer;
309 int cx,cy,oy,lstep,pstep;
311 QFont *normal,*bold,*italic,*bold_italic,*actual_font;
316 virtual void paintEvent(QPaintEvent *ev);
317 virtual void resizeEvent(QResizeEvent *ev);
318 virtual void mousePressEvent(QMouseEvent *ev);
331 QHTML::QHTML(char *d)
335 QFont f("Helvetica", 12, QFont::Bold);
336 normal = new QFont("Helvetica", 12, QFont::Normal);
337 bold = new QFont("Helvetica", 12, QFont::Bold);
338 italic = new QFont("Helvetica", 12, QFont::Normal, TRUE);
339 bold_italic = new QFont("Helvetica", 12, QFont::Bold, TRUE);
342 actual_font = normal;
344 setWindowTitle("LOGLAN Help");
346 QMenu * contentsMenu = NULL;
347 menuBar()->addAction("File", this, SLOT(load()));
348 contentsMenu = menuBar()->addMenu("&Contents");
349 contentsMenu->addAction("Index", this, SLOT(contents()));
350 contentsMenu->addAction("User Guide", this, SLOT(user_guide()));
351 contentsMenu->addAction("Language reference", this, SLOT(lang_guide()));
353 menuBar()->addAction("Quit", app, SLOT(quit()));
355 analyzer = new HTMLAnalyzer;
356 map = new QPixmap(500, PIX_HEIGHT);
357 map->fill(backgroundColor());
358 resize(500, height());
359 setFixedSize(width(), height());
362 pstep = height() - bar->height();
363 vscroll = new QScrollBar(0, PIX_HEIGHT, lstep, pstep, 0, QScrollBar::Vertical, this);
364 vscroll->setTracking(TRUE);
365 vscroll->setGeometry(width() - 16, bar->height(), 16, height()-bar->height());
366 connect(vscroll, SIGNAL(valueChanged(int)), this, SLOT(vscrolled(int)));
367 sprintf(s, "%s/index.html", homedir);
368 analyzer->LoadFile(s);
369 analyzer->PackLinks();
373 void QHTML::vscrolled(int v)
381 QString s(QFileDialog::getOpenFileName(homedir, "*", this));
383 vscroll->setValue(0);
384 analyzer->LoadFile((char*)s.ascii());
385 analyzer->PackLinks();
390 void QHTML::contents()
393 sprintf(ss, "%s/index.html", homedir);
394 analyzer->LoadFile(ss);
395 analyzer->PackLinks();
399 void QHTML::user_guide()
402 sprintf(ss, "%s/userg.html", homedir);
403 analyzer->LoadFile(ss);
404 analyzer->PackLinks();
408 void QHTML::lang_guide()
411 sprintf(ss, "%s/langg.html", homedir);
412 analyzer->LoadFile(ss);
413 analyzer->PackLinks();
417 void QHTML::paintEvent(QPaintEvent *ev)
420 bitBlt(this, 0, bar->height(), map, 0, oy,
421 width() - 16, height() - 16);
425 void QHTML::resizeEvent(QResizeEvent *ev)
430 void QHTML::mousePressEvent(QMouseEvent *ev)
436 pom = analyzer->CheckTag(ev->x(), ev->y() + oy);
438 poms.sprintf(pom->tag_link);
440 poms = poms.right(poms.length() - 1);
441 if (poms[poms.length() - 1] == '"')
442 poms=poms.left(poms.length() - 1);
443 if (poms[0] == '#') {
444 poms = poms.right(poms.length() - 1);
445 pom1 = analyzer->FindAnchor((char*)poms.ascii());
447 vscroll->setValue(pom1->y);
450 sprintf(ss, "%s/%s", homedir, poms.toStdString().c_str());
451 analyzer->LoadFile(ss);
452 analyzer->PackLinks();
459 * @attention Currently not in use
465 void QHTML::DrawList()
470 if (!analyzer->tags.isEmpty()) {
473 map->fill(backgroundColor());
476 pom = analyzer->tags.first();
477 while (pom != NULL) {
478 switch(pom->tag_type) {
480 p.setFont(*actual_font);
481 if (cx + p.fontMetrics().width(pom->tag_text) >
484 cy = cy + p.fontMetrics().height();
486 p.drawText(cx, cy, pom->tag_text);
487 cx = cx + p.fontMetrics().width(pom->tag_text);
490 p.setFont(*actual_font);
491 cy = cy + p.fontMetrics().height();
495 if (actual_font == bold)
496 actual_font = bold_italic;
498 actual_font = italic;
502 if (actual_font == bold_italic)
505 actual_font = normal;
509 if (actual_font == italic)
510 actual_font = bold_italic;
516 if (actual_font == bold_italic)
517 actual_font = italic;
519 actual_font = normal;
523 p.setFont(*actual_font);
524 if (cx + p.fontMetrics().width(pom->tag_text) >
527 cy = cy + p.fontMetrics().height();
529 p.setPen(QColor(255, 0, 0));
530 p.drawText(cx, cy, pom->tag_text);
532 pom->y = cy + p.fontMetrics().height();
533 pom->w = p.fontMetrics().width(pom->tag_text);
534 pom->h = p.fontMetrics().height();
535 p.setPen(QColor(0, 0, 0));
536 cx = cx + p.fontMetrics().width(pom->tag_text);
539 p.setFont(*actual_font);
540 if (cx + p.fontMetrics().width(pom->tag_text) >
543 cy = cy + p.fontMetrics().height();
545 //p.setPen(QColor(0,255,0));
546 p.drawText(cx, cy, pom->tag_text);
548 pom->y = cy - p.fontMetrics().height();
549 pom->w = p.fontMetrics().width(pom->tag_text);
550 pom->h = p.fontMetrics().height();
551 p.setPen(QColor(0, 0, 0));
552 cx=cx + p.fontMetrics().width(pom->tag_text);
556 p.setFont(*actual_font);
558 cy = cy + p.fontMetrics().height();
560 case TAG_LIST_ITEM:p.setFont(*actual_font);
561 p.setBrush(QBrush(QColor(0, 0, 255)));
563 cy = cy + p.fontMetrics().height();
564 p.drawPie(cx, cy - 5, 5, 5, 0, 5760);
568 p.setFont(*actual_font);
570 cy = cy + p.fontMetrics().height();
572 (int)(p.fontMetrics().height() / 2),
573 width() - 16 - 5, cy -
574 (int)(p.fontMetrics().height() / 2));
576 cy = cy + p.fontMetrics().height();
579 pom = analyzer->tags.takeFirst();
588 int main( int argc, char **argv )
592 app = new QApplication(argc, argv);
597 QHTML cfg((char*)ps.ascii());
598 //app->setStyle(WindowsStyle);
599 // app->setMainWidget(&cfg);