NCL Composer  0.1.5
 All Classes Functions Variables Pages
ComposerHelpWidget.cpp
1 /* Copyright (c) 2011 Telemidia/PUC-Rio.
2  * All rights reserved. This program and the accompanying materials
3  * are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors:
8  * Telemidia/PUC-Rio - initial API and implementation
9  */
10 #include "ComposerHelpWidget.h"
11 
12 #include <QBoxLayout>
13 #include <QSplitter>
14 #include <QDebug>
15 
16 #include <QApplication>
17 
18 namespace composer {
19 namespace gui {
20 
21 /************************* HelpBrowser class **********************************/
22 HelpBrowser::HelpBrowser(QHelpEngine &helpEngine, QWidget *parent)
23  : QWebView(parent), helpEngine(helpEngine)
24 {
25 
26  /* In the future, we must change to only use the .qch files:
27 
28  settings()->setAttribute(QWebSettings::PluginsEnabled, false);
29  settings()->setAttribute(QWebSettings::JavaEnabled, false);
30 
31  page()->setNetworkAccessManager(
32  new HelpNetworkAccessManager(&helpEngine, this) );
33  */
34 }
35 
36 void HelpBrowser::setSource(const QUrl &url)
37 {
38  QUrl baseUrl = QUrl("file://" + QApplication::applicationDirPath()+"/doc/");
39 
40  // This should be: load(url);
41  setContent(helpEngine.fileData(url), "text/html", baseUrl);
42 }
43 
44 ComposerHelpWidget::ComposerHelpWidget(QWidget *parent)
45  : helpEngine(QApplication::applicationDirPath() + "/doc/nclcomposer.qhc",
46  parent),
47  helpBrowser(helpEngine, parent)
48 {
49  helpEngine.setupData();
50 
51  QSplitter *splitter = new QSplitter(this);
52  splitter->addWidget(helpEngine.contentWidget());
53  splitter->addWidget(&helpBrowser);
54 
55  QHBoxLayout *boxLayout = new QHBoxLayout(this);
56  boxLayout->addWidget(splitter);
57 
58  setLayout(boxLayout);
59  helpEngine.contentWidget()->expandAll();
60 
61  connect(helpEngine.contentWidget(), SIGNAL(linkActivated(const QUrl &)),
62  &helpBrowser, SLOT(setSource(const QUrl &)));
63 
64  setMinimumSize(800, 600);
65  retranslateUi();
66 }
67 
68 void ComposerHelpWidget::retranslateUi()
69 {
70  setWindowTitle(tr("NCL Composer Help"));
71 }
72 
73 
74 /*
75 /todo
76 The current implementation is not the best one. Today, we have to distribute
77 the .qch and the .html, .css, etc. files. If we reimplement QNetworkReply
78 we can use only the .qch file. The following commented code was a not
79 successful attempt to do this.
80  */
81 /*********************** HelpNetworkReply class *******************************/
82 /*
83  * The HelpNetworkReply will handle the
84  *
85 class HelpNetworkReply : public QNetworkReply
86 {
87 public:
88  HelpNetworkReply(const QNetworkRequest &request, const QByteArray &fileData,
89  const QString &mimeType);
90 
91  virtual void abort();
92 
93  virtual qint64 bytesAvailable() const
94  { return data.length() + QNetworkReply::bytesAvailable(); }
95 
96 protected:
97  virtual qint64 readData(char *data, qint64 maxlen);
98 
99 private:
100  QByteArray data;
101  qint64 origLen;
102 };
103 
104 HelpNetworkReply::HelpNetworkReply(const QNetworkRequest &request,
105  const QByteArray &fileData,
106  const QString &mimeType)
107 
108  : data(fileData), origLen(fileData.length())
109 {
110  setRequest(request);
111  setOpenMode(QIODevice::ReadOnly);
112 
113  setHeader(QNetworkRequest::ContentTypeHeader, mimeType);
114  setHeader(QNetworkRequest::ContentLengthHeader, QByteArray::number(origLen));
115 
116  QTimer::singleShot(0, this, SIGNAL(metaDataChanged()));
117  QTimer::singleShot(0, this, SIGNAL(readyRead()));
118 }
119 
120 void HelpNetworkReply::abort()
121 {
122  // nothing to do
123 }
124 
125 qint64 HelpNetworkReply::readData(char *buffer, qint64 maxlen)
126 {
127  qint64 len = qMin(qint64(data.length()), maxlen);
128  if (len)
129  {
130  qMemCopy(buffer, data.constData(), len);
131  data.remove(0, len);
132  }
133 
134  if (!data.length())
135  QTimer::singleShot(0, this, SIGNAL(finished()));
136 
137  return len;
138 }*/
139 
140 /******************* HelpNetworkAccessManager class ***************************/
141 /*
142 class HelpNetworkAccessManager : public QNetworkAccessManager
143 {
144 public:
145  HelpNetworkAccessManager(QHelpEngine *engine, QObject *parent);
146 
147 protected:
148  virtual QNetworkReply *createRequest(Operation op,
149  const QNetworkRequest &request,
150  QIODevice *outgoingData = 0);
151 
152 private:
153  QHelpEngine *helpEngine;
154 };
155 HelpNetworkAccessManager::HelpNetworkAccessManager(QHelpEngine *engine,
156  QObject *parent)
157  : QNetworkAccessManager(parent), helpEngine(engine)
158 {
159 }
160 
161 QNetworkReply *HelpNetworkAccessManager::createRequest(Operation op,
162  const QNetworkRequest &request,
163  QIODevice *outgoingData)
164 {
165  const QString& scheme = request.url().scheme();
166  if (scheme == QLatin1String("qthelp") || scheme == QLatin1String("about"))
167  {
168  qDebug() << request.url();
169  const QUrl& url = request.url();
170  QString mimeType = url.toString();
171  if (mimeType.endsWith(QLatin1String(".svg"))
172  || mimeType.endsWith(QLatin1String(".svgz")))
173  {
174  mimeType = QLatin1String("image/svg+xml");
175  }
176  else if (mimeType.endsWith(QLatin1String(".css")))
177  {
178  mimeType = QLatin1String("text/css");
179  }
180  else if (mimeType.endsWith(QLatin1String(".png")))
181  {
182  mimeType = QLatin1String("image/png");
183  }
184  else if (mimeType.endsWith(QLatin1String(".js")))
185  {
186  mimeType = QLatin1String("text/javascript");
187  }
188  else
189  {
190  mimeType = QLatin1String("text/html");
191  }
192  return new HelpNetworkReply(request, helpEngine->fileData(url), mimeType);
193  }
194 
195  return QNetworkAccessManager::createRequest(op, request, outgoingData);
196 }*/
197 
198 
199 } } // end namespace