helpBrowser.cc 2.5 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
#include <QWebFrame>

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

16 17
	QWebView(parent),
	helpEngine_(_helpEngine) {
18 19 20

	currentPage_ = 0;

21 22 23 24
	page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
	// Link has been clicked
    connect(this->page(), SIGNAL(linkClicked(const QUrl&)),
            this, SLOT(load(const QUrl&)));
25 26 27 28

}

HelpBrowser::~HelpBrowser() {
29

30 31
}

32
QVariant HelpBrowser::loadBytes(const QUrl &url) {
33

34
	QByteArray bytes = helpEngine_->fileData(url);
35

36 37 38 39 40 41
	return QVariant(bytes);
}

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

	load(_url, "");
42 43
}

44 45 46 47 48 49 50 51
void HelpBrowser::load(const QUrl& _url) {

	load(_url, "");
}

void HelpBrowser::load(const QUrl& _url, const QString& /*_s_url*/, bool _skipSave) {

	QUrl newUrl;
52

53
	if(_url.scheme() != "qthelp") {
54 55 56
		// Convert link to be within qthelp domain namespace
		QString new_file = visitedPages_[currentPage_];
		QStringList comp = new_file.split("/");
57
		comp[comp.size()-1] = _url.toString();
58

59
		newUrl = QUrl(comp.join("/"));
60

61
		if(!helpEngine_->findFile(newUrl).isValid()) {
62 63
			QMessageBox::warning( this, "OpenFlipper Help",
				"Unable to find specified file within documentation.");
64 65
			return;
		}
66 67

	} else {
68
		newUrl = _url;
69 70
	}

71
	QVariant data = this->loadBytes(newUrl);
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
	page()->mainFrame()->setHtml(txt, getCurrentDir(newUrl));
87 88

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

93
	emit urlChanged ( _url.toString() );
94 95
}

96
QUrl HelpBrowser::getCurrentDir(const QUrl& _url) {
97

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

101
	QString nstr = str_list.join("/");
102

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

bool HelpBrowser::isForwardAvailable() {

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

bool HelpBrowser::isBackwardAvailable() {

	return currentPage_ > 0;
}

void HelpBrowser::backward() {

	if(isBackwardAvailable()) {
		currentPage_--;
120
		load(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
121 122 123 124 125 126 127
	}
}

void HelpBrowser::forward() {

	if(isForwardAvailable()) {
		currentPage_++;
128
		load(QUrl(visitedPages_[currentPage_]), visitedPages_[currentPage_], true);
129 130 131
	}
}