documentationpanelwidget.cpp 5.44 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
#include "session.h"
24
25
26
27
28
29

#include <KLocalizedString>

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

45
DocumentationPanelWidget::DocumentationPanelWidget(Cantor::Session* session, QWidget* parent) :QWidget(parent), m_session(nullptr), m_engine(nullptr), m_textBrowser(nullptr), m_tabWidget(nullptr), m_splitter(nullptr), m_backend(QString())
46
{
47
48
    m_backend = session->backend()->name();
    const QString fileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QLatin1String("documentation/") + m_backend + QLatin1String("/help.qhc"));
49
    m_engine = new QHelpEngine(fileName, this);
50

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

61
62
    loadDocumentation();

63
64
65
66
67
68
69
70
71
72
    // 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
73
74
75
76
    m_tabWidget = new QTabWidget(this);
    m_tabWidget->setMovable(true);
    m_tabWidget->setElideMode(Qt::ElideRight);

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

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

87
88
    //const QByteArray contents = m_engine->fileData(QUrl(QLatin1String("qthelp://org.kde.cantor/doc/figures/plotting1.png")));
    //m_textBrowser->setContent(contents, QLatin1String("image/png;charset=UTF-8"));
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

    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)));
100
101
102
103
104
105
106

    setSession(session);
}

void DocumentationPanelWidget::setSession(Cantor::Session* session)
{
    m_session = session;
107
108
}

109
110
void DocumentationPanelWidget::displayHelp(const QUrl& url)
{
111
    const QByteArray contents = m_engine->fileData(url);
112
113
    m_textBrowser->setContent(contents, QLatin1String("text/html;charset=UTF-8"));
    m_textBrowser->show();
114
115
116
117
118
119

    qDebug() << url;
    //display the actual keyword contents, not the header topic
    const QModelIndex index = m_engine->indexWidget()->currentIndex();
    const QString indexText = index.data(Qt::DisplayRole).toString();
    qDebug() << indexText << "index pressed";
120
121
}

122
123
124
void DocumentationPanelWidget::doSearch(const QString& str)
{
    // perform searching of the string passed
Shubham  .'s avatar
Shubham . committed
125
    Q_UNUSED(str)
126
127
}

128
129
void DocumentationPanelWidget::contextSensitiveHelp(const QString& keyword)
{
Shubham  .'s avatar
Shubham . committed
130
131
132
133
    qDebug() << "Context sensitive help for " << keyword;

    QHelpIndexWidget* const index = m_engine->indexWidget();
    index->filterIndices(keyword); // filter exactly, no wildcards
134
    index->activateCurrentItem(); // this internally emitts the QHelpIndexWidget::linkActivated signal
Shubham  .'s avatar
Shubham . committed
135
136

    loadDocumentation();
137
138
}

139
140
void DocumentationPanelWidget::loadDocumentation()
{
141
142
    const QString backend = backendName();
    const QString fileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, QLatin1String("documentation/") + backend + QLatin1String("/help.qch"));
143
    m_engine->registerDocumentation(fileName);
144
145
}

146
147
148
void DocumentationPanelWidget::unloadDocumentation()
{
    //Call this function when the user changes the current backend
Shubham  .'s avatar
Shubham . committed
149
    m_engine->unregisterDocumentation(QLatin1String("org.kde.cantor"));
150
151
}

152
QString DocumentationPanelWidget::backendName() const
153
{
154
    return m_backend;
155
}