documentationpanelwidget.cpp 5.25 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
/*
    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"
23
24
#include "../../worksheet.h"
#include "../../worksheettextitem.h"
25
26
27
28
29
30

#include <KLocalizedString>

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

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

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

62
63
    loadDocumentation();

64
65
66
    m_tabWidget = new QTabWidget(this);
    m_tabWidget->setMovable(true);
    m_tabWidget->setElideMode(Qt::ElideRight);
67
68
69
70
71
72
73
74
75
76
77
78

    // 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);

    // Add different tabs to the widget
79
    m_tabWidget->addTab(m_engine->contentWidget(), i18n("Contents"));
80
81
    m_tabWidget->addTab(m_engine->indexWidget(), i18n("Index"));
    m_tabWidget->addTab(container, i18n("Search"));
82

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

88
89
90
91
92
93
94
    connect(m_engine->contentWidget(), &QHelpContentWidget::linkActivated, this, &DocumentationPanelWidget::displayHelp);
    connect(m_engine->indexWidget(), &QHelpIndexWidget::linkActivated, this, &DocumentationPanelWidget::displayHelp);

    Worksheet* worksheet = new Worksheet(Cantor::Backend::getBackend(QLatin1String("maxima")), parent);
    WorksheetTextItem* textItem = worksheet->currentTextItem();
    connect(textItem, &WorksheetTextItem::requestDocumentation, this, &DocumentationPanelWidget::contextSensitiveHelp);
    //connect(search, SIGNAL(clicked(bool)), this, SLOT(doSearch(QString)));
95

96
97
98
    m_splitter = new QSplitter(Qt::Horizontal, this);
    m_splitter->addWidget(m_tabWidget);
    m_splitter->addWidget(m_textBrowser);
99

100
    QHBoxLayout* layout = new QHBoxLayout(this);
101
    layout->addWidget(m_splitter);
102
103
}

104
105
void DocumentationPanelWidget::displayHelp(const QUrl& url)
{
106
    const QByteArray contents = m_engine->fileData(url);
107
108
    m_textBrowser->setContent(contents, QLatin1String("text/html;charset=UTF-8"));
    m_textBrowser->show();
109
110
}

111
112
113
114
115
void DocumentationPanelWidget::doSearch(const QString& str)
{
    // perform searching of the string passed
}

116
117
118
119
120
void DocumentationPanelWidget::contextSensitiveHelp(const QString& keyword)
{

}

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

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

131
132
133
134
135
136
137
138
139
140
141
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);
}

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

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