lib/HelpWindow.cpp

Go to the documentation of this file.
00001 /*
00002  *  Qtstalker stock charter
00003  * 
00004  *  Copyright (C) 2001-2007 Stefan S. Stratigakos
00005  * 
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
00019  *  USA.
00020  */
00021 
00022 #include "HelpWindow.h"
00023 #include "RcFile.h"
00024 #include "../pics/next.xpm"
00025 #include "../pics/home.xpm"
00026 #include "../pics/previous.xpm"
00027 #include "../pics/disable.xpm"
00028 #include <qlayout.h>
00029 #include <qdir.h>
00030 #include <qfile.h>
00031 
00032 HelpWindow::HelpWindow (QWidget *w, QString &fn) : QDialog (w, "HelpWindow", FALSE, WDestructiveClose)
00033 {
00034   RcFile rcfile;
00035   rcfile.loadData(RcFile::HelpFilePath, homePath);
00036   homePath.append("/");
00037   tocPath = homePath + "toc.html";
00038 
00039   QVBoxLayout *vbox = new QVBoxLayout (this);
00040   vbox->setSpacing(5);
00041   vbox->setMargin(5);
00042   
00043   toolbar = new Toolbar(this, Toolbar::Horizontal);
00044   vbox->addWidget(toolbar);
00045 
00046   QString s("home");
00047   QString s2(tr("Home"));
00048   toolbar->addButton(s, home, s2);
00049   QObject::connect(toolbar->getButton(s), SIGNAL(clicked()), this, SLOT(goHome()));
00050   
00051   s = "previous";
00052   s2 = tr("Previous");
00053   toolbar->addButton(s, previous, s2);
00054   QObject::connect(toolbar->getButton(s), SIGNAL(clicked()), this, SLOT(goPrevious()));
00055 
00056   s = "next";
00057   s2 = tr("Next");
00058   toolbar->addButton(s, next, s2);
00059   QObject::connect(toolbar->getButton(s), SIGNAL(clicked()), this, SLOT(goNext()));
00060   
00061   s = "exit";
00062   s2 = tr("Close");
00063   toolbar->addButton(s, disable, s2);
00064   QObject::connect(toolbar->getButton(s), SIGNAL(clicked()), this, SLOT(exit()));
00065   
00066   text = new QTextBrowser(this);
00067   
00068   QObject::connect(text, SIGNAL(backwardAvailable(bool)), this, SLOT(previousStatus(bool)));
00069   QObject::connect(text, SIGNAL(forwardAvailable(bool)), this, SLOT(nextStatus(bool)));
00070   QObject::connect(text, SIGNAL(linkClicked(const QString &)), this, SLOT(slotLoadFile(const QString &)));
00071   
00072   vbox->addWidget (text);
00073   
00074   s = homePath + fn;
00075   QDir dir;
00076   // If the doc exists at fn (perhaps full pathname) then use it
00077   // otherwise try relative to HelpFilePath, otherwise use the default
00078   if (dir.exists(fn) && s.contains(".html"))
00079     text->setSource(fn);
00080   else if (dir.exists(s) && s.contains(".html"))
00081     text->setSource(s);
00082   else
00083   {
00084     qDebug("Document not found: " + fn);
00085     text->setSource(tocPath);
00086   }
00087   
00088   setCaption(text->documentTitle());
00089   
00090   QSize sz;
00091   rcfile.loadSize(RcFile::HelpWindowSize, sz);
00092   resize(sz);
00093 }
00094 
00095 HelpWindow::~HelpWindow ()
00096 {
00097 }
00098 
00099 void HelpWindow::goHome ()
00100 {
00101   text->setSource(tocPath);
00102 }
00103 
00104 void HelpWindow::goPrevious ()
00105 {
00106   text->backward();
00107 }
00108 
00109 void HelpWindow::goNext ()
00110 {
00111   text->forward();
00112 }
00113 
00114 void HelpWindow::previousStatus (bool d)
00115 {
00116   QString s("previous");
00117   toolbar->setButtonStatus(s, d);
00118 }
00119 
00120 void HelpWindow::nextStatus (bool d)
00121 {
00122   QString s("next");
00123   toolbar->setButtonStatus(s, d);
00124 }
00125 
00126 void HelpWindow::exit ()
00127 {  
00128   RcFile rcfile;
00129   rcfile.saveSize(RcFile::HelpWindowSize, size());
00130   done(0);
00131 }
00132 
00133 void HelpWindow::slotLoadFile(const QString &fileName)
00134 {  
00135   if (fileName.contains(".html"))
00136   {
00137       text->setSource(fileName);
00138       //setCaption(text->documentTitle());
00139   }
00140   else
00141   {
00142       // In case to view a file without known mime source extension, e.g. CHANGELOG.0.3X
00143       QFile file(QFile::encodeName(fileName));
00144       QString s(file.name());
00145       
00146       if ( file.open( IO_ReadOnly ) ) 
00147       {
00148         s = (QString)file.readAll();
00149         file.close();
00150         s.replace( QChar('<'), "&lt;" );
00151         s.replace( QChar('>'), "&gt;" );
00152         s.prepend("<html><title>CHANGELOG</title<body><pre>");
00153         // FIXME: generate <title> out of fileName
00154         // FIXME: add a "back" link or a complete list of all available CHANGLOG files
00155         //        possible in form of a left hand placed table
00156         s.append("</pre></body></html>");
00157       }
00158       else
00159       {
00160         s = file.errorString ();
00161       }
00162       text->setText(s);
00163       //setCaption(text->documentTitle());
00164   }
00165   setCaption(text->documentTitle());
00166 }