helpBrowser.cc 3.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * helpBrowser.cc
 *
 *  Created on: Apr 8, 2009
 *      Author: kremer
 */

#include "helpBrowser.hh"

#include <iostream>

12

13
HelpBrowser::HelpBrowser(QHelpEngine* _helpEngine, QWidget* parent) :
14

15
	QTextBrowser(parent),
16
	helpEngine_(_helpEngine) {
17
18
19

	currentPage_ = 0;

20
21
	connect(this, SIGNAL(linkClicked(const QString&)),
			this, SLOT(open(const QString&)));
22
23
24
25

}

HelpBrowser::~HelpBrowser() {
26

27
28
}

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

31
	if (_url.scheme() == "qthelp") {
32

33
34
		return QVariant(helpEngine_->fileData(_url));
	}
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	else if (_url.toString().contains("../../")) {

		// Relative link
		// So convert into qthelp-link

		QStringList list = _url.toString().split("/");

		QString base = "";

		for(int i = 0; i < list.size(); i++) {
			if(list[i].toLower().contains("plugin")) {
				base = list[i].toLower();
				break;
			}
		}

		if(base != "") {

			// Build new link
			QStringList docDomains = helpEngine_->registeredDocumentations();

			QString newUrl = "qthelp://";

			// This gives org.openflipper
			newUrl += docDomains[0].split(".")[0] + "." + docDomains[0].split(".")[1];
			newUrl += "." + base + "/doc/" + list.last();

			if((helpEngine_->findFile(newUrl)).isValid()) {

				return QVariant(helpEngine_->fileData(newUrl));
			}

			return QVariant();
		}

		return QVariant();
	}
72
	else {
73

74
		QUrl newUrl;
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
		QStringList docDomains = helpEngine_->registeredDocumentations();

		// Search in all namespaces for requested file
		for(int i = 0; i < docDomains.size(); i++) {

			QString sNewUrl = "qthelp://" + docDomains[i] + "/" + VIRTUAL_FOLDER +
				"/" + _url.toString();

			newUrl = helpEngine_->findFile(QUrl(sNewUrl));

			if(newUrl.isValid()) return QVariant(helpEngine_->fileData(newUrl));
		}

		// If file has not been found in any of the namespaces
		// return an empty QVariant
		return QVariant();
92
	}
93
94
95

}

96
void HelpBrowser::open(const QString& _url) {
97

98
99
	open(QUrl(_url), "");
}
100

101
void HelpBrowser::open(const QUrl& _url) {
102

103
104
	open(_url, "");
}
105

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

108
	QVariant data = this->loadResource(QTextDocument::HtmlResource, _url);
109
110
111
112
113
114
115
116
117
118
119
120
121
122

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

123
124
125
	//std::cerr << txt.toStdString() << std::endl;

	setHtml(txt);
126
127

	if(!_skipSave) {
128
		visitedPages_.push_back(_url.toString());
129
130
131
		currentPage_ = visitedPages_.size()-1;
	}

132
	emit urlChanged ( _url.toString() );
133
134
}

135
QUrl HelpBrowser::getCurrentDir(const QUrl& _url) {
136

137
138
	QStringList str_list = _url.toString().split("/");
	str_list[str_list.size() - 1] = "";
139

140
	QString nstr = str_list.join("/");
141

142
	return QUrl(nstr);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
}

bool HelpBrowser::isForwardAvailable() {

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

bool HelpBrowser::isBackwardAvailable() {

	return currentPage_ > 0;
}

void HelpBrowser::backward() {

	if(isBackwardAvailable()) {
		currentPage_--;
159
		open(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
160
161
162
163
164
165
166
	}
}

void HelpBrowser::forward() {

	if(isForwardAvailable()) {
		currentPage_++;
167
		open(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
168
169
170
	}
}