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()
236 QListIterator<Tag *> tagsIterator(tags);
238 while (tagsIterator.hasNext()) {
239 pom = tagsIterator.next();
240 fprintf(stderr, "%s:%s,%s,%s\n", names[pom->tag_type],
241 pom->tag_text, pom->tag_link, pom->tag_label);
245 void HTMLAnalyzer::PackLinks()
249 QListIterator<Tag *> tagsIterator(tags);
251 while (tagsIterator.hasNext()) {
252 pom = tagsIterator.next();
253 if ((pom->tag_type == TAG_LINK) ||
254 (pom->tag_type == TAG_ANCHOR)) {
255 pom1 = tagsIterator.next();
257 while ((pom1 != NULL) &&
258 (pom1->tag_type != TAG_LINK_END)) {
259 if (pom1->tag_type == TAG_TEXT)
260 strcat(s,pom1->tag_text);
261 tags.removeOne(pom1);
262 pom1 = tags.current();
264 strcpy(pom->tag_text,s);
265 tags.removeOne(pom1);
266 pom = tags.current();
268 pom = tagsIterator.next();
274 Tag *HTMLAnalyzer::CheckTag(int x,int y)
277 QListIterator<Tag *> tagsIterator(tags);
279 while (tagsIterator.hasNext()) {
280 pom = tagsIterator.next();
281 if (pom->tag_type==TAG_LINK)
282 if ((x >= pom->x) && (x <= pom->x + pom->w) &&
283 (y >= pom->y) && (y <= pom->y + pom->h)) {
290 Tag *HTMLAnalyzer::FindAnchor(char *name)
293 QListIterator<Tag *> tagsIterator(tags);
295 while (tagsIterator.hasNext()) {
296 pom = tagsIterator.next();
297 if ((pom->tag_type == TAG_ANCHOR) &&
298 (strcmp(pom->tag_label, name) == 0)) {
304 /********************************/
306 class QHTML: public QMainWindow {
311 HTMLAnalyzer *analyzer;
313 int cx,cy,oy,lstep,pstep;
315 QFont *normal,*bold,*italic,*bold_italic,*actual_font;
320 virtual void paintEvent(QPaintEvent *ev);
321 virtual void resizeEvent(QResizeEvent *ev);
322 virtual void mousePressEvent(QMouseEvent *ev);
335 QHTML::QHTML(char *d)
339 QFont f("Helvetica", 12, QFont::Bold);
340 normal = new QFont("Helvetica", 12, QFont::Normal);
341 bold = new QFont("Helvetica", 12, QFont::Bold);
342 italic = new QFont("Helvetica", 12, QFont::Normal, TRUE);
343 bold_italic = new QFont("Helvetica", 12, QFont::Bold, TRUE);
346 actual_font = normal;
348 setWindowTitle("LOGLAN Help");
350 QMenu * contentsMenu = NULL;
351 menuBar()->addAction("File", this, SLOT(load()));
352 contentsMenu = menuBar()->addMenu("&Contents");
353 contentsMenu->addAction("Index", this, SLOT(contents()));
354 contentsMenu->addAction("User Guide", this, SLOT(user_guide()));
355 contentsMenu->addAction("Language reference", this, SLOT(lang_guide()));
357 menuBar()->addAction("Quit", app, SLOT(quit()));
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, QScrollBar::Vertical, this);
368 vscroll->setTracking(TRUE);
369 vscroll->setGeometry(width() - 16, bar->height(), 16, height()-bar->height());
370 connect(vscroll, SIGNAL(valueChanged(int)), this, SLOT(vscrolled(int)));
371 sprintf(s, "%s/index.html", homedir);
372 analyzer->LoadFile(s);
373 analyzer->PackLinks();
377 void QHTML::vscrolled(int v)
385 QString s(QFileDialog::getOpenFileName(homedir, "*", this));
387 vscroll->setValue(0);
388 analyzer->LoadFile((char*)s.ascii());
389 analyzer->PackLinks();
394 void QHTML::contents()
397 sprintf(ss, "%s/index.html", homedir);
398 analyzer->LoadFile(ss);
399 analyzer->PackLinks();
403 void QHTML::user_guide()
406 sprintf(ss, "%s/userg.html", homedir);
407 analyzer->LoadFile(ss);
408 analyzer->PackLinks();
412 void QHTML::lang_guide()
415 sprintf(ss, "%s/langg.html", homedir);
416 analyzer->LoadFile(ss);
417 analyzer->PackLinks();
421 void QHTML::paintEvent(QPaintEvent *ev)
424 bitBlt(this, 0, bar->height(), map, 0, oy,
425 width() - 16, height() - 16);
429 void QHTML::resizeEvent(QResizeEvent *ev)
434 void QHTML::mousePressEvent(QMouseEvent *ev)
440 pom = analyzer->CheckTag(ev->x(), ev->y() + oy);
442 poms.sprintf(pom->tag_link);
444 poms = poms.right(poms.length() - 1);
445 if (poms[poms.length() - 1] == '"')
446 poms=poms.left(poms.length() - 1);
447 if (poms[0] == '#') {
448 poms = poms.right(poms.length() - 1);
449 pom1 = analyzer->FindAnchor((char*)poms.ascii());
451 vscroll->setValue(pom1->y);
454 sprintf(ss, "%s/%s", homedir, poms.toStdString().c_str());
455 analyzer->LoadFile(ss);
456 analyzer->PackLinks();
463 * @attention Currently not in use
469 void QHTML::DrawList()
474 if (!analyzer->tags.isEmpty()) {
477 map->fill(backgroundColor());
480 QListIterator<Tag *> tagsIterator(tags);
482 while (tagsIterator.hasNext()) {
483 pom = tagsIterator.next();
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();
593 int main( int argc, char **argv )
597 app = new QApplication(argc, argv);
602 QHTML cfg((char*)ps.ascii());
603 //app->setStyle(WindowsStyle);
604 // app->setMainWidget(&cfg);