Cppcheck
helpdialog.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "helpdialog.h"
20 
21 #include "common.h"
22 
23 #include "ui_helpdialog.h"
24 
25 #include <QApplication>
26 #include <QFileInfo>
27 #include <QHelpEngine>
28 #include <QHelpContentWidget>
29 #include <QHelpIndexWidget>
30 #include <QMessageBox>
31 #include <QStringList>
32 #include <QUrl>
33 #include <QVBoxLayout>
34 
35 class QWidget;
36 
37 void HelpBrowser::setHelpEngine(QHelpEngine *helpEngine)
38 {
39  mHelpEngine = helpEngine;
40 }
41 
42 QVariant HelpBrowser::loadResource(int type, const QUrl &name)
43 {
44  if (name.scheme() == "qthelp") {
45  QString url(name.toString());
46  while (url.indexOf("/./") > 0)
47  url.remove(url.indexOf("/./"), 2);
48  return QVariant(mHelpEngine->fileData(QUrl(url)));
49  }
50  return QTextBrowser::loadResource(type, name);
51 }
52 
53 static QString getHelpFile()
54 {
55  const QString datadir = getDataDir();
56 
57  QStringList paths;
58  paths << (datadir + "/help")
59  << datadir
60  << (QApplication::applicationDirPath() + "/help")
61  << QApplication::applicationDirPath();
62 #ifdef FILESDIR
63  const QString filesdir = FILESDIR;
64  paths << (filesdir + "/help")
65  << filesdir;
66 #endif
67  for (const QString &p: paths) {
68  QString filename = p + "/online-help.qhc";
69  if (QFileInfo::exists(filename))
70  return filename;
71  }
72  return QString();
73 }
74 
75 HelpDialog::HelpDialog(QWidget *parent) :
76  QDialog(parent),
77  mUi(new Ui::HelpDialog)
78 {
79  mUi->setupUi(this);
80 
81  QString helpFile = getHelpFile();
82  if (helpFile.isEmpty()) {
83  const QString msg = tr("Helpfile '%1' was not found").arg("online-help.qhc");
84  QMessageBox msgBox(QMessageBox::Warning,
85  tr("Cppcheck"),
86  msg,
87  QMessageBox::Ok,
88  this);
89  msgBox.exec();
90  mHelpEngine = nullptr;
91  return;
92  }
93 
94  mHelpEngine = new QHelpEngine(helpFile);
95  // Disable the timestamp check of online-help.qhc by setting _q_readonly
96  mHelpEngine->setProperty("_q_readonly", QVariant::fromValue<bool>(true));
97  mHelpEngine->setupData();
98 
99  mUi->contents->addWidget(mHelpEngine->contentWidget());
100  mUi->index->addWidget(mHelpEngine->indexWidget());
101 
102  mUi->textBrowser->setHelpEngine(mHelpEngine);
103 
104  mUi->textBrowser->setSource(QUrl("qthelp://cppcheck.sourceforge.io/doc/index.html"));
105  connect(mHelpEngine->contentWidget(),
106  SIGNAL(linkActivated(QUrl)),
107  mUi->textBrowser,
108  SLOT(setSource(QUrl)));
109 
110  connect(mHelpEngine->indexWidget(),
111  SIGNAL(linkActivated(QUrl,QString)),
112  mUi->textBrowser,
113  SLOT(setSource(QUrl)));
114 }
115 
117 {
118  delete mUi;
119  delete mHelpEngine;
120 }
void setHelpEngine(QHelpEngine *helpEngine)
Definition: helpdialog.cpp:37
QVariant loadResource(int type, const QUrl &name) override
Definition: helpdialog.cpp:42
QHelpEngine * mHelpEngine
Definition: helpdialog.h:45
~HelpDialog() override
Definition: helpdialog.cpp:116
QHelpEngine * mHelpEngine
Definition: helpdialog.h:57
Ui::HelpDialog * mUi
Definition: helpdialog.h:56
HelpDialog(QWidget *parent=nullptr)
Definition: helpdialog.cpp:75
QString getDataDir()
Get configured data dir.
Definition: common.cpp:75
static QString getHelpFile()
Definition: helpdialog.cpp:53
Definition: aboutdialog.h:27