helpBrowser.cc 6.12 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

	currentPage_ = 0;

64
65
	connect(this, SIGNAL(linkClicked(const QString&)),
			this, SLOT(open(const QString&)));
66
67
68
69

}

HelpBrowser::~HelpBrowser() {
70

71
72
}

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

75
	if (_url.scheme() == "qthelp") {
76

77
78
		// Extract the global virtual folder from this link
		QString link = _url.toString();
79

80
		QStringList linkParts = link.split("/");
81

82
83
84
85
86
87
88
		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;
89
90
		}

91
92
		return QVariant(helpEngine_->fileData(_url));
	}	else {
93

94
95
		const QString sNewUrl = "qthelp://" + currentNameSpace_ + "/" + currentVirtualFolder_ + "/" + _url.toString();
		const QUrl newUrl = helpEngine_->findFile(QUrl(sNewUrl));
96

97
98
99
100
		if(newUrl.isValid())
			return QVariant(helpEngine_->fileData(newUrl));
		else {
			std::cerr << "Unable to find file at url : " << sNewUrl.toStdString() << std::endl;
101
102
103
			return QVariant();
		}

104
	}
105
106
107

}

108
void HelpBrowser::open(const QString& _url) {
109

110
111
	open(QUrl(_url), "");
}
112

113
void HelpBrowser::open(const QUrl& _url) {
114

115
116
	open(_url, "");
}
117

118
void HelpBrowser::open(const QUrl& _url, const QString& /*_str*/, bool _skipSave) {
119

120
	QVariant data = this->loadResource(QTextDocument::HtmlResource, _url);
121
122
123
124
125
126
127
128
129
130
131
132
133
134

	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();
	}

135
136
137
	//std::cerr << txt.toStdString() << std::endl;

	setHtml(txt);
138
139

	if(!_skipSave) {
140
		visitedPages_.push_back(_url.toString());
141
142
143
		currentPage_ = visitedPages_.size()-1;
	}

144
145
146
147
148
149
  //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]);

150
	emit urlChanged ( _url.toString() );
151
152
}

153
QUrl HelpBrowser::getCurrentDir(const QUrl& _url) {
154

155
156
	QStringList str_list = _url.toString().split("/");
	str_list[str_list.size() - 1] = "";
157

158
	QString nstr = str_list.join("/");
159

160
	return QUrl(nstr);
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
}

bool HelpBrowser::isForwardAvailable() {

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

bool HelpBrowser::isBackwardAvailable() {

	return currentPage_ > 0;
}

void HelpBrowser::backward() {

	if(isBackwardAvailable()) {
		currentPage_--;
177
		open(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
178
179
180
181
182
183
184
	}
}

void HelpBrowser::forward() {

	if(isForwardAvailable()) {
		currentPage_++;
185
		open(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
186
187
188
	}
}