PageRenderTime 70ms CodeModel.GetById 20ms RepoModel.GetById 5ms app.codeStats 0ms

/gui/docu_dock.cpp

https://gitlab.com/ismaelgv/morpheus
C++ | 196 lines | 154 code | 36 blank | 6 comment | 33 complexity | 839e4aa9cd89bb1489626cf2d2f7f18e MD5 | raw file
  1. #include "docu_dock.h"
  2. #include "QtNetwork/QNetworkAccessManager"
  3. #include "QtNetwork/QNetworkReply"
  4. class HelpNetworkReply : public QNetworkReply
  5. {
  6. public:
  7. HelpNetworkReply(const QNetworkRequest &request, const QByteArray &fileData, const QString &mimeType);
  8. virtual void abort() override {}
  9. virtual qint64 bytesAvailable() const override { return data.length() + QNetworkReply::bytesAvailable(); }
  10. protected:
  11. virtual qint64 readData(char *data, qint64 maxlen) override;
  12. private:
  13. QByteArray data;
  14. qint64 origLen;
  15. };
  16. class HelpNetworkAccessManager : public QNetworkAccessManager
  17. {
  18. public:
  19. HelpNetworkAccessManager(QHelpEngineCore *engine, QObject *parent)
  20. : QNetworkAccessManager(parent), m_helpEngine(engine) {}
  21. protected:
  22. virtual QNetworkReply *createRequest(Operation op,
  23. const QNetworkRequest &request, QIODevice *outgoingData = 0) override;
  24. private:
  25. QHelpEngineCore *m_helpEngine;
  26. };
  27. HelpNetworkReply::HelpNetworkReply(const QNetworkRequest &request,
  28. const QByteArray &fileData, const QString &mimeType)
  29. : data(fileData), origLen(fileData.length())
  30. {
  31. setRequest(request);
  32. setOpenMode(QIODevice::ReadOnly);
  33. setHeader(QNetworkRequest::ContentTypeHeader, mimeType);
  34. setHeader(QNetworkRequest::ContentLengthHeader, QByteArray::number(origLen));
  35. QTimer::singleShot(0, this, SIGNAL(metaDataChanged()));
  36. QTimer::singleShot(0, this, SIGNAL(readyRead()));
  37. }
  38. qint64 HelpNetworkReply::readData(char *buffer, qint64 maxlen)
  39. {
  40. qint64 len = qMin(qint64(data.length()), maxlen);
  41. if (len) {
  42. memcpy(buffer, data.constData(), len);
  43. data.remove(0, len);
  44. }
  45. if (!data.length())
  46. QTimer::singleShot(0, this, SIGNAL(finished()));
  47. return len;
  48. }
  49. QNetworkReply *HelpNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
  50. {
  51. // qDebug() << "Creating Help File Request for URL " << request.url();
  52. QString scheme = request.url().scheme();
  53. if (scheme == QLatin1String("qthelp") || scheme == QLatin1String("about")) {
  54. QString mimeType/* = QMimeDatabase().mimeTypeForUrl(request.url()).name()*/;
  55. if (request.url().path().endsWith(".png"))
  56. mimeType = "image/png";
  57. else if (request.url().path().endsWith(".html"))
  58. mimeType = "text/html";
  59. else if (request.url().path().endsWith(".js"))
  60. mimeType = "text/javascript";
  61. else if (request.url().path().endsWith(".css"))
  62. mimeType = "text/css";
  63. auto data = m_helpEngine->fileData(request.url());
  64. if (data.isEmpty())
  65. qDebug() << "Empty URL" << request.url().path() << " / " << mimeType << " of " << data.size() << " bytes";
  66. return new HelpNetworkReply(request, data , mimeType);
  67. }
  68. else {
  69. QDesktopServices::openUrl(request.url());
  70. return new HelpNetworkReply(request, QByteArray() , "text/html");
  71. }
  72. return QNetworkAccessManager::createRequest(op, request, outgoingData);
  73. }
  74. DocuDock::DocuDock(QWidget* parent) : QDockWidget("Documentation", parent)
  75. {
  76. timer = NULL;
  77. splitter = new QSplitter(Qt::Horizontal, this);
  78. help_engine = config::getHelpEngine();
  79. connect(help_engine,SIGNAL(setupFinished()),this,SLOT(setRootOfHelpIndex()));
  80. help_engine->setupData();
  81. HelpNetworkAccessManager* hnam = new HelpNetworkAccessManager(help_engine,this);
  82. help_view = new QWebView();
  83. help_view->page()->setNetworkAccessManager(hnam);
  84. // help_view->page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
  85. help_view->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
  86. help_view->page()->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, false);
  87. help_view->page()->settings()->setAttribute(QWebSettings::LocalContentCanAccessFileUrls, true);
  88. splitter->addWidget(help_engine->contentWidget());
  89. help_engine->contentWidget()->setRootIsDecorated(true);
  90. root_reset = false;
  91. splitter->addWidget(help_view);
  92. help_view->show();
  93. connect(help_engine->contentWidget(),SIGNAL(linkActivated(const QUrl&)), this, SLOT(setCurrentURL(const QUrl&)));
  94. this->setWidget(splitter);
  95. }
  96. void DocuDock::setCurrentNode(nodeController* node)
  97. {
  98. QMap <QString, QUrl > identifiers;
  99. while (node && identifiers.empty()) {
  100. identifiers = help_engine->linksForIdentifier(node->getName());
  101. if (! identifiers.empty()) {
  102. setCurrentURL(identifiers.begin().value());
  103. break;
  104. }
  105. node = node->getParent();
  106. }
  107. }
  108. void DocuDock::setCurrentElement(QString name) {
  109. QMap <QString, QUrl > identifiers = help_engine->linksForIdentifier(name);
  110. // qDebug() << "Searching for help for " << name;
  111. if (!identifiers.empty()) {
  112. setCurrentURL(identifiers.begin().value());
  113. }
  114. }
  115. void DocuDock::setCurrentURL(const QUrl& url) {
  116. if (help_view->url() != url) {
  117. help_view->setUrl(url);
  118. // qDebug() << url;
  119. }
  120. }
  121. void DocuDock::resizeEvent(QResizeEvent* event)
  122. {
  123. if (double(event->size().height()) / double(event->size().width()) > 1) {
  124. if (splitter->orientation() != Qt::Vertical)
  125. splitter->setOrientation(Qt::Vertical);
  126. }
  127. else {
  128. if (splitter->orientation() != Qt::Horizontal)
  129. splitter->setOrientation(Qt::Horizontal);
  130. }
  131. QDockWidget::resizeEvent(event);
  132. }
  133. void DocuDock::setRootOfHelpIndex()
  134. {
  135. QHelpContentWidget* help_index = help_engine->contentWidget();
  136. QHelpContentModel* model = help_engine->contentModel();
  137. if (model->isCreatingContents()) {
  138. if ( ! timer) {
  139. timer = new QTimer(this);
  140. timer->setSingleShot(true);
  141. connect(timer,SIGNAL(timeout()),this, SLOT(setRootOfHelpIndex()));
  142. }
  143. timer->start(500);
  144. return;
  145. }
  146. QModelIndex root = model->index(0,0);
  147. int root_rows = model->rowCount(root);
  148. // qDebug() << "I am getting the Docu " <<root_rows ;
  149. int modules_row = -1;
  150. for (uint row=0; row<root_rows; row++) {
  151. // qDebug() << row << model->data(model->index(row,0,root),0);
  152. if ( model->data(model->index(row,0,root),0).toString() == "Modules" )
  153. modules_row = row;
  154. }
  155. if (modules_row>=0) {
  156. help_index->setRootIndex(model->index(modules_row,0,root));
  157. help_index->setExpanded(model->index(modules_row,0,root),true);
  158. root_reset = true;
  159. }
  160. }