Developer Documentation
helpBrowser.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 /*
51  * helpBrowser.cc
52  *
53  * Created on: Apr 8, 2009
54  * Author: kremer
55  */
56 
57 #include "helpBrowser.hh"
58 
59 #include <iostream>
60 
61 HelpBrowser::HelpBrowser(QHelpEngine* _helpEngine, QWidget* parent) :
62 
63  QTextBrowser(parent),
64  helpEngine_(_helpEngine),
65  currentPage_(0),
66  currentVirtualFolder_(""),
67  currentNameSpace_("")
68 {
69  connect(this, SIGNAL(sourceChanged(const QUrl&)), this, SLOT(rememberHistory(const QUrl&)));
70 }
71 
73 
74 }
75 
76 
77 void HelpBrowser::updateNameSpaceAndFolder (const QUrl& _url) {
78 
79  // Extract the global virtual folder from this link
80  QString link = _url.toString();
81  QStringList linkParts = link.split("/");
82 
83  if ( linkParts.size() > 3) {
84  currentVirtualFolder_ = linkParts[3];
85  currentNameSpace_ = QString("org.openflipper.")+ linkParts[3].toLower() ;
86  } else {
87  currentNameSpace_ = "";
89  std::cerr << "Unable to detect virtual folder or namespace of this link" << _url.toString().toStdString() << std::endl;
90  }
91 }
92 
93 void HelpBrowser::rememberHistory (const QUrl& _url) {
94 
95  QUrl newUrl = resolveUrl(_url);
96 
97  //if the site is already the current site
98  //don't change the memory stack
99  if ((visitedPages_.size() > 0) && (newUrl == visitedPages_[currentPage_]))
100  return;
101 
102  // Delete the visited pages after the current position if they exist
103  if ( currentPage_ < visitedPages_.size()-1 )
104  visitedPages_.erase((visitedPages_.begin()+currentPage_+1),visitedPages_.end());
105 
106  visitedPages_.push_back(newUrl);
107  currentPage_ = visitedPages_.size()-1;
108 
109  emit historyChanged(_url);
110 }
111 
112 QUrl HelpBrowser::resolveUrl(const QUrl &_url)
113 {
114  if (_url.scheme() == "qthelp") {
115 
117  return _url;
118 
119  } else {
120 
121  QUrl newUrl;
122 
123  if ( _url.toString().startsWith("..") ) {
124 
125  // Relative url. We jump to a new context, so we first remove the relative part
126  QUrl tmpURL("qthelp://" + currentNameSpace_ + "/" + currentVirtualFolder_ + "/");
127  newUrl = tmpURL.resolved(_url);
128 
129  // Update context
130  updateNameSpaceAndFolder(newUrl);
131  return newUrl;
132 
133  } else {
134 
135  // Normal URL without relative parts so we can safely combine them
136  // and stay in the current context
137  return "qthelp://" + currentNameSpace_ + "/" + currentVirtualFolder_ + "/" + _url.toString();
138 
139  }
140  }
141 }
142 
143 QVariant HelpBrowser::loadResource (int /*_type*/, const QUrl& _url) {
144 
145  QUrl newUrl = resolveUrl(_url);
146  const QUrl newFileUrl = helpEngine_->findFile(newUrl);
147 
148  if(newFileUrl.isValid())
149  return QVariant(helpEngine_->fileData(newFileUrl));
150  else {
151  std::cerr << "Unable to find file at url : " << _url.toString().toStdString() << std::endl;
152  return QVariant("Page not Found.");
153  }
154 
155 }
156 
157 void HelpBrowser::open(const QString& _url) {
158 
159  open(QUrl(_url));
160 }
161 
162 void HelpBrowser::open(const QUrl& _url, bool _skipSave) {
163 
164  QVariant data = this->loadResource(QTextDocument::HtmlResource, _url);
165 
166  QString txt;
167 
168  txt = data.toString();
169 
170  setHtml(txt);
171 
172  //jumps to a reference (Doxygen reference name and not section name)
173  //references are at the end of url after last '#'
174  QStringList Anchor = _url.toString().split("#");
175  if (Anchor.size() > 1)
176  this->scrollToAnchor(Anchor[Anchor.size()-1]);
177 
178  if (_skipSave)
179  {
180  disconnect(this,SLOT(rememberHistory(const QUrl&)));
181  emit sourceChanged( _url );
182  connect(this, SIGNAL(sourceChanged(const QUrl&)), this, SLOT(rememberHistory(const QUrl&)));
183  }
184  else
185  emit sourceChanged( _url );
186 
187 }
188 
189 QUrl HelpBrowser::getCurrentDir(const QUrl& _url) {
190 
191  QStringList str_list = _url.toString().split("/");
192 
193  if ( str_list.size() > 0 )
194  str_list[str_list.size() - 1] = "";
195  else
196  std::cerr << "Warning, getCurrentDir got invalid input: " << _url.toString().toStdString() << std::endl;
197 
198  QString nstr = str_list.join("/");
199 
200  return QUrl(nstr);
201 }
202 
204 
205  return currentPage_ < visitedPages_.size() - 1;
206 }
207 
209 
210  return currentPage_ > 0;
211 }
212 
214 
215  if(isBackwardAvailable()) {
216  currentPage_--;
217  open(visitedPages_[currentPage_], true);
218  }
219 
220 }
221 
223 
224  if(isForwardAvailable()) {
225  currentPage_++;
226  open(visitedPages_[currentPage_], true);
227  }
228 
229 }
230 
void rememberHistory(const QUrl &_url)
Adds a new page to the history.
Definition: helpBrowser.cc:93
bool isForwardAvailable()
Checks if the back button was pressed and we can go forward to the next page.
Definition: helpBrowser.cc:203
void backward()
Show last page stored in the history.
Definition: helpBrowser.cc:213
QUrl getCurrentDir(const QUrl &_url)
Extract path from URL.
Definition: helpBrowser.cc:189
QHelpEngine * helpEngine_
The help engine the widget is working on.
Definition: helpBrowser.hh:183
QUrl resolveUrl(const QUrl &_url)
resolves relative urls to absolute
Definition: helpBrowser.cc:112
virtual ~HelpBrowser()
Destructor.
Definition: helpBrowser.cc:72
QString currentNameSpace_
The currently active namespace.
Definition: helpBrowser.hh:203
void updateNameSpaceAndFolder(const QUrl &_url)
updateNameSpaceAndFolder
Definition: helpBrowser.cc:77
QList< QUrl > visitedPages_
History of the visited pages.
Definition: helpBrowser.hh:186
QVariant loadResource(int _type, const QUrl &_name)
re implementation of the load resource function of the text browser
Definition: helpBrowser.cc:143
bool isBackwardAvailable()
Checks if we visited other pages before.
Definition: helpBrowser.cc:208
HelpBrowser(QHelpEngine *_helpEngine, QWidget *parent=0)
Constructor.
Definition: helpBrowser.cc:61
int currentPage_
Current position in the history.
Definition: helpBrowser.hh:189
QString currentVirtualFolder_
The currently active virtual folder.
Definition: helpBrowser.hh:196
void forward()
Show next page stored in the history.
Definition: helpBrowser.cc:222