1 /*  This file is part of the KDevelop PHP Documentation Plugin
2 
3     SPDX-FileCopyrightText: 2012 Milian Wolff <mail@milianw.de>
4 
5     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
6 */
7 
8 #include "phpdocumentationwidget.h"
9 
10 #include <QProgressBar>
11 #include <QLabel>
12 #include <QVBoxLayout>
13 #include <QTemporaryFile>
14 #include <QTextStream>
15 
16 #include <KLocalizedString>
17 
18 #include "phpdocsplugin.h"
19 #include <documentation/standarddocumentationview.h>
20 
createStyleSheet(QObject * parent)21 QTemporaryFile* createStyleSheet(QObject* parent)
22 {
23     QTemporaryFile* file = new QTemporaryFile(parent);
24     bool ret = file->open();
25     Q_ASSERT(ret);
26     Q_UNUSED(ret);
27 
28     QTextStream ts(file);
29     ts << ".page-tools { float: none !important; } body { background: white !important; };";
30     return file;
31 }
32 
PhpDocumentationWidget(KDevelop::DocumentationFindWidget * find,const QUrl & url,PhpDocsPlugin * provider,QWidget * parent)33 PhpDocumentationWidget::PhpDocumentationWidget(KDevelop::DocumentationFindWidget* find, const QUrl &url,
34                                                PhpDocsPlugin* provider, QWidget* parent)
35 : QStackedWidget(parent)
36 , m_loading(new QWidget(this))
37 , m_styleSheet(createStyleSheet(this))
38 , m_provider(provider)
39 {
40     m_part = new KDevelop::StandardDocumentationView(find, this);
41     m_part->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
42     addWidget(m_part);
43     addWidget(m_loading);
44 
45     QProgressBar* progressbar = new QProgressBar;
46     progressbar->setValue(0);
47     progressbar->setMinimum(0);
48     progressbar->setMaximum(100);
49     progressbar->setAlignment(Qt::AlignCenter);
50 
51     connect( m_part, &KDevelop::StandardDocumentationView::loadProgress,
52              progressbar, &QProgressBar::setValue );
53 
54     QVBoxLayout* layout = new QVBoxLayout;
55     layout->addStretch();
56     QLabel* label = new QLabel(i18n("...loading documentation..."));
57     label->setAlignment(Qt::AlignCenter);
58     layout->addWidget(label);
59     layout->addWidget(progressbar);
60     layout->addStretch();
61     m_loading->setLayout(layout);
62     setCurrentWidget(m_loading);
63 
64 
65     connect(m_part, &KDevelop::StandardDocumentationView::linkClicked, this, &PhpDocumentationWidget::linkClicked);
66     connect(m_part, &KDevelop::StandardDocumentationView::loadFinished, this, &PhpDocumentationWidget::documentLoaded);
67 
68     m_part->load( url );
69 }
70 
~PhpDocumentationWidget()71 PhpDocumentationWidget::~PhpDocumentationWidget()
72 {
73     // make sure we don't get called by any of the m_part signals on shutdown, see also:
74     // https://codereview.qt-project.org/#/c/83800/
75     disconnect(m_part, 0, this, 0);
76 }
77 
linkClicked(const QUrl & url)78 void PhpDocumentationWidget::linkClicked(const QUrl& url)
79 {
80     m_part->load(url);
81     m_provider->addToHistory(url);
82 }
83 
documentLoaded()84 void PhpDocumentationWidget::documentLoaded()
85 {
86     m_part->settings()->setUserStyleSheetUrl(QUrl::fromLocalFile(m_styleSheet->fileName()));
87 
88     setCurrentWidget(m_part);
89     removeWidget(m_loading);
90     delete m_loading;
91     m_loading = 0;
92 }
93