helpBrowser.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 83 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-02-27 17:31:45 +0100 (Fr, 27. Feb 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 /*
00044  * helpBrowser.cc
00045  *
00046  *  Created on: Apr 8, 2009
00047  *      Author: kremer
00048  */
00049 
00050 #include "helpBrowser.hh"
00051 
00052 #include <iostream>
00053 
00054 
00055 HelpBrowser::HelpBrowser(QHelpEngine* _helpEngine, QWidget* parent) :
00056 
00057         QTextBrowser(parent),
00058         helpEngine_(_helpEngine) {
00059 
00060         currentPage_ = 0;
00061 
00062         connect(this, SIGNAL(linkClicked(const QString&)),
00063                         this, SLOT(open(const QString&)));
00064 
00065 }
00066 
00067 HelpBrowser::~HelpBrowser() {
00068 
00069 }
00070 
00071 QVariant HelpBrowser::loadResource (int /*_type*/, const QUrl& _url) {
00072 
00073         if (_url.scheme() == "qthelp") {
00074 
00075                 return QVariant(helpEngine_->fileData(_url));
00076         }
00077         else if (_url.toString().contains("../../")) {
00078 
00079                 // Relative link
00080                 // So convert into qthelp-link
00081 
00082                 QStringList list = _url.toString().split("/");
00083 
00084                 QString base = "";
00085 
00086                 for(int i = 0; i < list.size(); i++) {
00087                         if(list[i].toLower().contains("plugin")) {
00088                                 base = list[i].toLower();
00089                                 break;
00090                         }
00091                 }
00092 
00093                 if(base != "") {
00094 
00095                         // Build new link
00096                         QStringList docDomains = helpEngine_->registeredDocumentations();
00097 
00098                         QString newUrl = "qthelp://";
00099 
00100                         // This gives org.openflipper
00101                         newUrl += docDomains[0].split(".")[0] + "." + docDomains[0].split(".")[1];
00102                         newUrl += "." + base + "/doc/" + list.last();
00103 
00104                         if((helpEngine_->findFile(newUrl)).isValid()) {
00105 
00106                                 return QVariant(helpEngine_->fileData(newUrl));
00107                         }
00108 
00109                         return QVariant();
00110                 }
00111 
00112                 return QVariant();
00113         }
00114         else {
00115 
00116                 QUrl newUrl;
00117 
00118                 QStringList docDomains = helpEngine_->registeredDocumentations();
00119 
00120                 // Search in all namespaces for requested file
00121                 for(int i = 0; i < docDomains.size(); i++) {
00122 
00123                         QString sNewUrl = "qthelp://" + docDomains[i] + "/" + VIRTUAL_FOLDER +
00124                                 "/" + _url.toString();
00125 
00126                         newUrl = helpEngine_->findFile(QUrl(sNewUrl));
00127 
00128                         if(newUrl.isValid()) return QVariant(helpEngine_->fileData(newUrl));
00129                 }
00130 
00131                 // If file has not been found in any of the namespaces
00132                 // return an empty QVariant
00133                 return QVariant();
00134         }
00135 
00136 }
00137 
00138 void HelpBrowser::open(const QString& _url) {
00139 
00140         open(QUrl(_url), "");
00141 }
00142 
00143 void HelpBrowser::open(const QUrl& _url) {
00144 
00145         open(_url, "");
00146 }
00147 
00148 void HelpBrowser::open(const QUrl& _url, const QString& /*_str*/, bool _skipSave) {
00149 
00150         QVariant data = this->loadResource(QTextDocument::HtmlResource, _url);
00151 
00152         QString txt;
00153 
00154         if (data.type() == QVariant::String) {
00155                 txt = data.toString();
00156         } else if (data.type() == QVariant::ByteArray) {
00157 
00158                 QByteArray ba = data.toByteArray();
00159                 QTextCodec *codec = Qt::codecForHtml(ba);
00160                 txt = codec->toUnicode(ba);
00161 
00162                 txt = data.toString();
00163         }
00164 
00165         //std::cerr << txt.toStdString() << std::endl;
00166 
00167         setHtml(txt);
00168 
00169         if(!_skipSave) {
00170                 visitedPages_.push_back(_url.toString());
00171                 currentPage_ = visitedPages_.size()-1;
00172         }
00173 
00174         emit urlChanged ( _url.toString() );
00175 }
00176 
00177 QUrl HelpBrowser::getCurrentDir(const QUrl& _url) {
00178 
00179         QStringList str_list = _url.toString().split("/");
00180         str_list[str_list.size() - 1] = "";
00181 
00182         QString nstr = str_list.join("/");
00183 
00184         return QUrl(nstr);
00185 }
00186 
00187 bool HelpBrowser::isForwardAvailable() {
00188 
00189         return currentPage_ < visitedPages_.size() - 1;
00190 }
00191 
00192 bool HelpBrowser::isBackwardAvailable() {
00193 
00194         return currentPage_ > 0;
00195 }
00196 
00197 void HelpBrowser::backward() {
00198 
00199         if(isBackwardAvailable()) {
00200                 currentPage_--;
00201                 open(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
00202         }
00203 }
00204 
00205 void HelpBrowser::forward() {
00206 
00207         if(isForwardAvailable()) {
00208                 currentPage_++;
00209                 open(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
00210         }
00211 }
00212 

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .