helpBrowser.cc 7.26 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
	helpEngine_(_helpEngine),
58
	currentPage_(0),
59
60
61
	currentVirtualFolder_(""),
	currentNameSpace_("")
{
62
  connect(this, SIGNAL(sourceChanged(const QUrl&)), this, SLOT(rememberHistory(const QUrl&)));
63
64
65
}

HelpBrowser::~HelpBrowser() {
66

67
68
}

Jan Möbius's avatar
Jan Möbius committed
69
70
71
72
73
74
75
76
77

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

  // Extract the global virtual folder from this link
  QString link = _url.toString();
  QStringList linkParts = link.split("/");

  if ( linkParts.size() > 3) {
    currentVirtualFolder_ = linkParts[3];
78
    currentNameSpace_     = QString("org.openflipper.")+ linkParts[3].toLower() ;
Jan Möbius's avatar
Jan Möbius committed
79
80
81
82
83
84
85
86
87
  } else {
    currentNameSpace_ = "";
    currentVirtualFolder_ = "";
    std::cerr << "Unable to detect virtual folder or namespace of this link" << _url.toString().toStdString() << std::endl;
  }
}

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

88
  QUrl newUrl = resolveUrl(_url);
Jan Möbius's avatar
Jan Möbius committed
89
90
91
92
  // Delete the visited pages after the current position if they exist
  if ( currentPage_ < visitedPages_.size()-1 )
    visitedPages_.erase((visitedPages_.begin()+currentPage_),visitedPages_.end());

93
  visitedPages_.push_back(newUrl);
Jan Möbius's avatar
Jan Möbius committed
94
95
96
97
  currentPage_ = visitedPages_.size()-1;

}

98
99
100
QUrl HelpBrowser::resolveUrl(const QUrl &_url)
{
  if (_url.scheme() == "qthelp") {
101

102
103
    updateNameSpaceAndFolder(_url);
    return _url;
Jan Möbius's avatar
Jan Möbius committed
104

105
  } else {
Jan Möbius's avatar
Jan Möbius committed
106

107
    QUrl newUrl;
Jan Möbius's avatar
Jan Möbius committed
108

109
    if ( _url.toString().startsWith("..") ) {
110

111
112
113
      // Relative url. We jump to a new context, so we first remove the relative part
      QUrl tmpURL("qthelp://" + currentNameSpace_ + "/" + currentVirtualFolder_ + "/");
      newUrl = tmpURL.resolved(_url);
Jan Möbius's avatar
Jan Möbius committed
114

115
116
117
      // Update context
      updateNameSpaceAndFolder(newUrl);
      return newUrl;
118

119
    } else {
120

121
122
123
      // Normal URL without relative parts so we can safely combine them
      // and stay in the current context
      return "qthelp://" + currentNameSpace_ + "/" + currentVirtualFolder_ + "/" + _url.toString();
124

125
126
127
    }
  }
}
Jan Möbius's avatar
Jan Möbius committed
128

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

131
132
  QUrl newUrl = resolveUrl(_url);
  const QUrl newFileUrl = helpEngine_->findFile(newUrl);
133

134
135
136
137
138
139
  if(newFileUrl.isValid())
    return QVariant(helpEngine_->fileData(newFileUrl));
  else {
    std::cerr << "Unable to find file at url : " << _url.toString().toStdString() << std::endl;
    return QVariant("Page not Found.");
  }
140
141
142

}

143
void HelpBrowser::open(const QString& _url) {
144

Jan Möbius's avatar
Jan Möbius committed
145
	open(QUrl(_url));
146
}emit sourceChanged( _url );
147

Jan Möbius's avatar
Jan Möbius committed
148
void HelpBrowser::open(const QUrl& _url, bool _skipSave) {
149

150
	QVariant data = this->loadResource(QTextDocument::HtmlResource, _url);
151
152
153
154
155
156
157
158
159
160
161
162
163
164

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

165
	setHtml(txt);
166

167
168
169
170
171
172
  //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]);

173
174
175
176
177
178
179
180
181
  if (_skipSave)
  {
    disconnect(this,SLOT(rememberHistory(const QUrl&)));
    emit sourceChanged( _url );
    connect(this, SIGNAL(sourceChanged(const QUrl&)), this, SLOT(rememberHistory(const QUrl&)));
  }
  else
    emit sourceChanged( _url );

182
183
}

184
QUrl HelpBrowser::getCurrentDir(const QUrl& _url) {
185

186
	QStringList str_list = _url.toString().split("/");
Jan Möbius's avatar
Jan Möbius committed
187
188
189
190
191

	if ( str_list.size() > 0  )
	  str_list[str_list.size() - 1] = "";
	else
	  std::cerr << "Warning, getCurrentDir got invalid input: " << _url.toString().toStdString() << std::endl;
192

193
	QString nstr = str_list.join("/");
194

195
	return QUrl(nstr);
196
197
198
199
200
201
202
203
204
}

bool HelpBrowser::isForwardAvailable() {

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

bool HelpBrowser::isBackwardAvailable() {

205
	return currentPage_ > 1;
206
207
208
209
210
211
}

void HelpBrowser::backward() {

	if(isBackwardAvailable()) {
		currentPage_--;
212
		open(visitedPages_[currentPage_], true);
213
	}
Jan Möbius's avatar
Jan Möbius committed
214

215
216
217
218
219
220
}

void HelpBrowser::forward() {

	if(isForwardAvailable()) {
		currentPage_++;
Jan Möbius's avatar
Jan Möbius committed
221
		open(visitedPages_[currentPage_], true);
222
	}
Jan Möbius's avatar
Jan Möbius committed
223

224
225
}