helpBrowser.cc 2.2 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, const QUrl& _basePath, QWidget* parent) :
14

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

	currentPage_ = 0;

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

}

HelpBrowser::~HelpBrowser() {
27

28 29
}

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

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

34 35 36
		return QVariant(helpEngine_->fileData(_url));
	}
	else {
37

38 39
		// Set basePath_ as prefix of resource file
		QUrl newUrl = QUrl(basePath_.toString() + _url.toString());
40

41 42
		return QVariant(helpEngine_->fileData(newUrl));
	}
43 44 45

}

46
void HelpBrowser::open(const QString& _url) {
47

48 49
	open(QUrl(_url), "");
}
50

51
void HelpBrowser::open(const QUrl& _url) {
52

53 54
	open(_url, "");
}
55

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

58
	QVariant data = this->loadResource(QTextDocument::HtmlResource, _url);
59 60 61 62 63 64 65 66 67 68 69 70 71 72

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

73 74 75
	//std::cerr << txt.toStdString() << std::endl;

	setHtml(txt);
76 77

	if(!_skipSave) {
78
		visitedPages_.push_back(_url.toString());
79 80 81
		currentPage_ = visitedPages_.size()-1;
	}

82
	emit urlChanged ( _url.toString() );
83 84
}

85
QUrl HelpBrowser::getCurrentDir(const QUrl& _url) {
86

87 88
	QStringList str_list = _url.toString().split("/");
	str_list[str_list.size() - 1] = "";
89

90
	QString nstr = str_list.join("/");
91

92
	return QUrl(nstr);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

bool HelpBrowser::isForwardAvailable() {

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

bool HelpBrowser::isBackwardAvailable() {

	return currentPage_ > 0;
}

void HelpBrowser::backward() {

	if(isBackwardAvailable()) {
		currentPage_--;
109
		open(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
110 111 112 113 114 115 116
	}
}

void HelpBrowser::forward() {

	if(isForwardAvailable()) {
		currentPage_++;
117
		open(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
118 119 120
	}
}