helpBrowser.cc 6.79 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
41
42
\*===========================================================================*/

43
44
45
46
47
48
49
50
51
52
53
/*
 * helpBrowser.cc
 *
 *  Created on: Apr 8, 2009
 *      Author: kremer
 */

#include "helpBrowser.hh"

#include <iostream>

54
HelpBrowser::HelpBrowser(QHelpEngine* _helpEngine, QWidget* parent) :
55

56
	QTextBrowser(parent),
57
58
59
60
	helpEngine_(_helpEngine),
	currentVirtualFolder_(""),
	currentNameSpace_("")
{
61
62
63
64
65
66

	currentPage_ = 0;

}

HelpBrowser::~HelpBrowser() {
67

68
69
}

Jan Möbius's avatar
Jan Möbius committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

void HelpBrowser::updateNameSpaceAndFolder (const QUrl& _url) {

  // Extract the global virtual folder from this link
  QString link = _url.toString();
  QStringList linkParts = link.split("/");

  if ( linkParts.size() > 3) {
    currentNameSpace_     = linkParts[2];
    currentVirtualFolder_ = linkParts[3];
  } else {
    currentNameSpace_ = "";
    currentVirtualFolder_ = "";
    std::cerr << "Unable to detect virtual folder or namespace of this link" << _url.toString().toStdString() << std::endl;
  }
}

void HelpBrowser::rememberHistory (const QUrl& _url) {

  // Delete the visited pages after the current position if they exist
  if ( currentPage_ < visitedPages_.size()-1 )
    visitedPages_.erase((visitedPages_.begin()+currentPage_),visitedPages_.end());

  visitedPages_.push_back(_url);
  currentPage_ = visitedPages_.size()-1;

}

98
QVariant HelpBrowser::loadResource (int /*_type*/, const QUrl& _url) {
99

100
	if (_url.scheme() == "qthelp") {
101

Jan Möbius's avatar
Jan Möbius committed
102
103
104
105
106
107
108
109
	  updateNameSpaceAndFolder(_url);
		return QVariant(helpEngine_->fileData(_url));

	}	else {

		QUrl newUrl;

		if ( _url.toString().startsWith("..") ) {
110

Jan Möbius's avatar
Jan Möbius committed
111
112
113
114
115
116
		  // Relative url. We jump to a new context, so we first remove the relative part
		  QUrl tmpURL("qthelp://" + currentNameSpace_ + "/" + currentVirtualFolder_ + "/");
		  newUrl = tmpURL.resolved(_url);

		  // Update context
		  updateNameSpaceAndFolder(newUrl);
117

118
		} else {
119

Jan Möbius's avatar
Jan Möbius committed
120
121
122
		  // Normal URL without relative parts so we can safely combine them
		  // and stay in the current context
		  newUrl = "qthelp://" + currentNameSpace_ + "/" + currentVirtualFolder_ + "/" + _url.toString();
123

Jan Möbius's avatar
Jan Möbius committed
124
125
126
		}

		const QUrl newFileUrl = helpEngine_->findFile(newUrl);
127

Jan Möbius's avatar
Jan Möbius committed
128
129
		if(newFileUrl.isValid())
			return QVariant(helpEngine_->fileData(newFileUrl));
130
		else {
Jan Möbius's avatar
Jan Möbius committed
131
			std::cerr << "Unable to find file at url : " << newUrl.toString().toStdString() << std::endl;
132
133
134
			return QVariant();
		}

135
	}
136
137
138

}

139
void HelpBrowser::open(const QString& _url) {
140

Jan Möbius's avatar
Jan Möbius committed
141
	open(QUrl(_url));
142
}
143

Jan Möbius's avatar
Jan Möbius committed
144
void HelpBrowser::open(const QUrl& _url, bool _skipSave) {
145

146
	QVariant data = this->loadResource(QTextDocument::HtmlResource, _url);
147
148
149
150
151
152
153
154
155
156
157
158
159
160

	QString txt;

	if (data.type() == QVariant::String) {
		txt = data.toString();
	} else if (data.type() == QVariant::ByteArray) {

		QByteArray ba = data.toByteArray();
		QTextCodec *codec = Qt::codecForHtml(ba);
		txt = codec->toUnicode(ba);

		txt = data.toString();
	}

161
	setHtml(txt);
162

Jan Möbius's avatar
Jan Möbius committed
163
164
	if(!_skipSave)
	  rememberHistory(_url);
165

166
167
168
169
170
171
  //jumps to a reference (Doxygen reference name and not section name)
  //references are at the end of url after last '#'
  QStringList Anchor = _url.toString().split("#");
  if (Anchor.size() > 1)
    this->scrollToAnchor(Anchor[Anchor.size()-1]);

172
	emit urlChanged ( _url.toString() );
173
174
}

175
QUrl HelpBrowser::getCurrentDir(const QUrl& _url) {
176

177
	QStringList str_list = _url.toString().split("/");
Jan Möbius's avatar
Jan Möbius committed
178
179
180
181
182

	if ( str_list.size() > 0  )
	  str_list[str_list.size() - 1] = "";
	else
	  std::cerr << "Warning, getCurrentDir got invalid input: " << _url.toString().toStdString() << std::endl;
183

184
	QString nstr = str_list.join("/");
185

186
	return QUrl(nstr);
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
}

bool HelpBrowser::isForwardAvailable() {

	return currentPage_ < visitedPages_.size() - 1;
}

bool HelpBrowser::isBackwardAvailable() {

	return currentPage_ > 0;
}

void HelpBrowser::backward() {

	if(isBackwardAvailable()) {
		currentPage_--;
Jan Möbius's avatar
Jan Möbius committed
203
		open(visitedPages_[currentPage_], true);
204
	}
Jan Möbius's avatar
Jan Möbius committed
205

206
207
208
209
210
211
}

void HelpBrowser::forward() {

	if(isForwardAvailable()) {
		currentPage_++;
Jan Möbius's avatar
Jan Möbius committed
212
		open(visitedPages_[currentPage_], true);
213
	}
Jan Möbius's avatar
Jan Möbius committed
214

215
216
}