helpBrowser.cc 2.51 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 35
		return QVariant(helpEngine_->fileData(_url));
	}
	else {
36

37
		QUrl newUrl;
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
		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();
55
	}
56 57 58

}

59
void HelpBrowser::open(const QString& _url) {
60

61 62
	open(QUrl(_url), "");
}
63

64
void HelpBrowser::open(const QUrl& _url) {
65

66 67
	open(_url, "");
}
68

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

71
	QVariant data = this->loadResource(QTextDocument::HtmlResource, _url);
72 73 74 75 76 77 78 79 80 81 82 83 84 85

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

86 87 88
	//std::cerr << txt.toStdString() << std::endl;

	setHtml(txt);
89 90

	if(!_skipSave) {
91
		visitedPages_.push_back(_url.toString());
92 93 94
		currentPage_ = visitedPages_.size()-1;
	}

95
	emit urlChanged ( _url.toString() );
96 97
}

98
QUrl HelpBrowser::getCurrentDir(const QUrl& _url) {
99

100 101
	QStringList str_list = _url.toString().split("/");
	str_list[str_list.size() - 1] = "";
102

103
	QString nstr = str_list.join("/");
104

105
	return QUrl(nstr);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

bool HelpBrowser::isForwardAvailable() {

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

bool HelpBrowser::isBackwardAvailable() {

	return currentPage_ > 0;
}

void HelpBrowser::backward() {

	if(isBackwardAvailable()) {
		currentPage_--;
122
		open(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
123 124 125 126 127 128 129
	}
}

void HelpBrowser::forward() {

	if(isForwardAvailable()) {
		currentPage_++;
130
		open(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
131 132 133
	}
}