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 #include "helpBrowser.hh"
43
44 #include <iostream>
45
46 HelpBrowser::HelpBrowser(QHelpEngine* _helpEngine, QWidget* parent) :
47
48 QTextBrowser(parent),
49 helpEngine_(_helpEngine),
50 currentPage_(0),
51 currentVirtualFolder_(""),
52 currentNameSpace_("")
53 {
54 connect(this, SIGNAL(sourceChanged(const QUrl&)), this, SLOT(rememberHistory(const QUrl&)));
55 }
56
57 HelpBrowser::~HelpBrowser() {
58
59 }
60
61
62 void HelpBrowser::updateNameSpaceAndFolder (const QUrl& _url) {
63
64 // Extract the global virtual folder from this link
65 QString link = _url.toString();
66 QStringList linkParts = link.split("/");
67
68 if ( linkParts.size() > 3) {
69 currentVirtualFolder_ = linkParts[3];
70 currentNameSpace_ = QString("org.openflipper.")+ linkParts[3].toLower() ;
71 } else {
72 currentNameSpace_ = "";
73 currentVirtualFolder_ = "";
74 std::cerr << "Unable to detect virtual folder or namespace of this link" << _url.toString().toStdString() << std::endl;
75 }
76 }
77
78 void HelpBrowser::rememberHistory (const QUrl& _url) {
79
80 QUrl newUrl = resolveUrl(_url);
81
82 //if the site is already the current site
83 //don't change the memory stack
84 if ((visitedPages_.size() > 0) && (newUrl == visitedPages_[currentPage_]))
85 return;
86
87 // Delete the visited pages after the current position if they exist
88 if ( currentPage_ < visitedPages_.size()-1 )
89 visitedPages_.erase((visitedPages_.begin()+currentPage_+1),visitedPages_.end());
90
91 visitedPages_.push_back(newUrl);
92 currentPage_ = visitedPages_.size()-1;
93
94 emit historyChanged(_url);
95 }
96
97 QUrl HelpBrowser::resolveUrl(const QUrl &_url)
98 {
99 if (_url.scheme() == "qthelp") {
100
101 updateNameSpaceAndFolder(_url);
102 return _url;
103
104 } else {
105
106 QUrl newUrl;
107
108 if ( _url.toString().startsWith("..") ) {
109
110 // Relative url. We jump to a new context, so we first remove the relative part
111 QUrl tmpURL("qthelp://" + currentNameSpace_ + "/" + currentVirtualFolder_ + "/");
112 newUrl = tmpURL.resolved(_url);
113
114 // Update context
115 updateNameSpaceAndFolder(newUrl);
116 return newUrl;
117
118 } else {
119
120 // Normal URL without relative parts so we can safely combine them
121 // and stay in the current context
122 return "qthelp://" + currentNameSpace_ + "/" + currentVirtualFolder_ + "/" + _url.toString();
123
124 }
125 }
126 }
127
128 QVariant HelpBrowser::loadResource (int /*_type*/, const QUrl& _url) {
129
130 QUrl newUrl = resolveUrl(_url);
131 const QUrl newFileUrl = helpEngine_->findFile(newUrl);
132
133 if(newFileUrl.isValid())
134 return QVariant(helpEngine_->fileData(newFileUrl));
135 else {
136 std::cerr << "Unable to find file at url : " << _url.toString().toStdString() << std::endl;
137 return QVariant("Page not Found.");
138 }
139
140 }
141
142 void HelpBrowser::open(const QString& _url) {
143
144 open(QUrl(_url));
145 }
146
147 void HelpBrowser::open(const QUrl& _url, bool _skipSave) {
148
149 QVariant data = this->loadResource(QTextDocument::HtmlResource, _url);
150
151 QString txt;
152
153 txt = data.toString();
154
155 setHtml(txt);
156
157 //jumps to a reference (Doxygen reference name and not section name)
158 //references are at the end of url after last '#'
159 QStringList Anchor = _url.toString().split("#");
160 if (Anchor.size() > 1)
161 this->scrollToAnchor(Anchor[Anchor.size()-1]);
162
163 if (_skipSave)
164 {
165 disconnect(this,SLOT(rememberHistory(const QUrl&)));
166 emit sourceChanged( _url );
167 connect(this, SIGNAL(sourceChanged(const QUrl&)), this, SLOT(rememberHistory(const QUrl&)));
168 }
169 else
170 emit sourceChanged( _url );
171
172 }
173
174 QUrl HelpBrowser::getCurrentDir(const QUrl& _url) {
175
176 QStringList str_list = _url.toString().split("/");
177
178 if ( str_list.size() > 0 )
179 str_list[str_list.size() - 1] = "";
180 else
181 std::cerr << "Warning, getCurrentDir got invalid input: " << _url.toString().toStdString() << std::endl;
182
183 QString nstr = str_list.join("/");
184
185 return QUrl(nstr);
186 }
187
188 bool HelpBrowser::isForwardAvailable() {
189
190 return currentPage_ < visitedPages_.size() - 1;
191 }
192
193 bool HelpBrowser::isBackwardAvailable() {
194
195 return currentPage_ > 0;
196 }
197
198 void HelpBrowser::backward() {
199
200 if(isBackwardAvailable()) {
201 currentPage_--;
202 open(visitedPages_[currentPage_], true);
203 }
204
205 }
206
207 void HelpBrowser::forward() {
208
209 if(isForwardAvailable()) {
210 currentPage_++;
211 open(visitedPages_[currentPage_], true);
212 }
213
214 }
215
HelpBrowser::updateNameSpaceAndFolder
void updateNameSpaceAndFolder(const QUrl &_url)
updateNameSpaceAndFolder
Definition: helpBrowser.cc:62
HelpBrowser::helpEngine_
QHelpEngine * helpEngine_
The help engine the widget is working on.
Definition: helpBrowser.hh:162
HelpBrowser::currentPage_
int currentPage_
Current position in the history.
Definition: helpBrowser.hh:168
HelpBrowser::visitedPages_
QList< QUrl > visitedPages_
History of the visited pages.
Definition: helpBrowser.hh:165
HelpBrowser::isBackwardAvailable
bool isBackwardAvailable()
Checks if we visited other pages before.
Definition: helpBrowser.cc:193
HelpBrowser::loadResource
QVariant loadResource(int _type, const QUrl &_name)
re implementation of the load resource function of the text browser
Definition: helpBrowser.cc:128
HelpBrowser::currentNameSpace_
QString currentNameSpace_
The currently active namespace.
Definition: helpBrowser.hh:182
HelpBrowser::HelpBrowser
HelpBrowser(QHelpEngine *_helpEngine, QWidget *parent=0)
Constructor.
Definition: helpBrowser.cc:46
HelpBrowser::rememberHistory
void rememberHistory(const QUrl &_url)
Adds a new page to the history.
Definition: helpBrowser.cc:78
HelpBrowser::backward
void backward()
Show last page stored in the history.
Definition: helpBrowser.cc:198
HelpBrowser::isForwardAvailable
bool isForwardAvailable()
Checks if the back button was pressed and we can go forward to the next page.
Definition: helpBrowser.cc:188
HelpBrowser::resolveUrl
QUrl resolveUrl(const QUrl &_url)
resolves relative urls to absolute
Definition: helpBrowser.cc:97
HelpBrowser::currentVirtualFolder_
QString currentVirtualFolder_
The currently active virtual folder.
Definition: helpBrowser.hh:175
HelpBrowser::getCurrentDir
QUrl getCurrentDir(const QUrl &_url)
Extract path from URL.
Definition: helpBrowser.cc:174
HelpBrowser::forward
void forward()
Show next page stored in the history.
Definition: helpBrowser.cc:207
HelpBrowser::~HelpBrowser
virtual ~HelpBrowser()
Destructor.
Definition: helpBrowser.cc:57