documentationpanelwidget.cpp 5.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1301, USA.

    ---
    Copyright (C) 2020 Shubham <aryan100jangid@gmail.com>
 */

#include "cantor_macros.h"
#include "documentationpanelplugin.h"

#include <KLocalizedString>

#include <QApplication>
#include <QByteArray>
#include <QDebug>
29
#include <QHBoxLayout>
30
31
32
33
#include <QHelpContentWidget>
#include <QHelpEngine>
#include <QHelpIndexWidget>
#include <QIcon>
34
#include <QLineEdit>
35
#include <QPointer>
36
#include <QPushButton>
37
#include <QSplitter>
38
#include <QStandardPaths>
39
40
#include <QTabWidget>
#include <QUrl>
41
#include <QWebEngineView>
42
#include <QWidget>
43

44
DocumentationPanelWidget::DocumentationPanelWidget(QWidget* parent) :QWidget(parent), m_engine(nullptr), m_textBrowser(nullptr), m_tabWidget(nullptr), m_splitter(nullptr)
45
{
46
47
48
    const QString backendName = QLatin1String("maxima");
    const QString fileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QLatin1String("documentation/") + backendName + QLatin1String("/help.qhc"));
    m_engine = new QHelpEngine(fileName, this);
49

50
    if(!m_engine->setupData())
51
    {
52
53
54
55
56
        qWarning() << "Couldn't setup QtHelp Engine";
        delete m_engine;
        delete m_textBrowser;
        delete m_tabWidget;
        delete m_splitter;
57
    }
58

59
60
    loadDocumentation();

61
62
63
64
65
66
67
68
69
70
    // create  a container for Search tab
    QWidget* container = new QWidget(this);
    QHBoxLayout* clayout = new QHBoxLayout(this);
    container->setLayout(clayout);

    QLineEdit* input = new QLineEdit(this);
    QPushButton* search = new QPushButton(i18n("Search"), this);
    clayout->addWidget(input);
    clayout->addWidget(search);

Shubham  .'s avatar
Shubham . committed
71
72
73
74
    m_tabWidget = new QTabWidget(this);
    m_tabWidget->setMovable(true);
    m_tabWidget->setElideMode(Qt::ElideRight);

75
    // Add different tabs to the widget
76
    m_tabWidget->addTab(m_engine->contentWidget(), i18n("Contents"));
77
78
    m_tabWidget->addTab(m_engine->indexWidget(), i18n("Index"));
    m_tabWidget->addTab(container, i18n("Search"));
79

80
    m_textBrowser = new QWebEngineView(this);
81
    const QByteArray contents = m_engine->fileData(QUrl(QLatin1String("qthelp://org.kde.cantor/doc/maxima.html#SEC_Top"))); // set initial page contents
82
83
    m_textBrowser->setContent(contents, QLatin1String("text/html;charset=UTF-8"));
    m_textBrowser->show();
84

85
86
87
88
    connect(m_engine->contentWidget(), &QHelpContentWidget::linkActivated, this, &DocumentationPanelWidget::displayHelp);
    connect(m_engine->indexWidget(), &QHelpIndexWidget::linkActivated, this, &DocumentationPanelWidget::displayHelp);

    //connect(search, SIGNAL(clicked(bool)), this, SLOT(doSearch(QString)));
89

90
91
92
    m_splitter = new QSplitter(Qt::Horizontal, this);
    m_splitter->addWidget(m_tabWidget);
    m_splitter->addWidget(m_textBrowser);
93

94
    QHBoxLayout* layout = new QHBoxLayout(this);
95
    layout->addWidget(m_splitter);
96
97
}

98
99
void DocumentationPanelWidget::displayHelp(const QUrl& url)
{
100
    const QByteArray contents = m_engine->fileData(url);
101
102
    m_textBrowser->setContent(contents, QLatin1String("text/html;charset=UTF-8"));
    m_textBrowser->show();
103
104
}

105
106
107
void DocumentationPanelWidget::doSearch(const QString& str)
{
    // perform searching of the string passed
Shubham  .'s avatar
Shubham . committed
108
    Q_UNUSED(str)
109
110
}

111
112
void DocumentationPanelWidget::contextSensitiveHelp(const QString& keyword)
{
Shubham  .'s avatar
Shubham . committed
113
114
115
116
117
118
119
120
    qDebug() << "Context sensitive help for " << keyword;

    // get the index widget
    QHelpIndexWidget* const index = m_engine->indexWidget();
    index->filterIndices(keyword); // filter exactly, no wildcards
    index->activateCurrentItem(); // this internally emitts the QHelpContentWidget::linkActivated signal

    loadDocumentation();
121
122
}

123
124
void DocumentationPanelWidget::loadDocumentation()
{
125
126
127
    //1. Get the backend name
    //2. Load their documentation

128
129
130
    const QString backendName = QLatin1String("maxima");
    const QString fileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QLatin1String("documentation/") + backendName + QLatin1String("/help.qch"));
    m_engine->registerDocumentation(fileName);
131
132
}

133
134
135
136
137
138
139
140
141
142
143
void DocumentationPanelWidget::unloadDocumentation()
{
    //1. Get the backend name
    //2. Unload their documentation
    //Call this function when the user changes the current backend

    const QString backendName = QLatin1String("maxima");
    const QString fileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QLatin1String("documentation/") + backendName + QLatin1String("/help.qch"));
    m_engine->unregisterDocumentation(fileName);
}

144
145
QIcon DocumentationPanelWidget::icon() const
{
146
147
    // return backend's icon
    return QIcon();
148
149
150
151
}

QString DocumentationPanelWidget::name() const
{
152
153
    // return backend's name
    return QString(QLatin1String("maxima"));
154
}