documentationpanelwidget.cpp 5.4 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
54
55
56
57
        qWarning() << "Couldn't setup QtHelp Engine";
        delete m_engine;
        delete m_textBrowser;
        delete m_tabWidget;
        delete m_splitter;
58
    }
59

60
61
    loadDocumentation();

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

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

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

86
87
    //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"));
88

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

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

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

    setSession(session);
}

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

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

    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";
119
120
}

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

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

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

    loadDocumentation();
136
137
}

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

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

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