readmepanel.cpp 2.62 KB
Newer Older
Hadi Charara's avatar
Hadi Charara committed
1
2
3
4
5
6
7
#include "readmepanel.h"
#include <qevent.h>
#include <kservice.h>
#include <KParts/ReadOnlyPart>
#include <QVBoxLayout>
#include <QFileInfo>
#include <QtWidgets>
Hadi Charara's avatar
Hadi Charara committed
8
#include <KColorScheme>
Hadi Charara's avatar
Hadi Charara committed
9

Hadi Charara's avatar
Hadi Charara committed
10
11
#include "dolphin_readmepanelsettings.h"

Hadi Charara's avatar
Hadi Charara committed
12
13
ReadmePanel::ReadmePanel ( QWidget* parent ) :
    Panel(parent),
Hadi Charara's avatar
Hadi Charara committed
14
    toggleViewAction(QSharedPointer<QAction>(new QAction(this))),
Hadi Charara's avatar
Hadi Charara committed
15
16
17
    m_layout(nullptr),
    m_readmeWidget(nullptr),
    m_markdownPart(nullptr),
Hadi Charara's avatar
Hadi Charara committed
18
19
    m_readmeUrl(),
    m_textBrowser(nullptr)
Hadi Charara's avatar
Hadi Charara committed
20
21
22
{
    m_layout = new QVBoxLayout(this);
    m_layout->setContentsMargins(0, 0, 0, 0);
Hadi Charara's avatar
Hadi Charara committed
23
24
25
26
27
28
29
30
31

    toggleViewAction->setCheckable(true);
    toggleViewAction->setMenuRole(QAction::NoRole);
    toggleViewAction->setText("README.md Preview");
    toggleViewAction->setChecked(ReadmePanelSettings::enabled());
    connect(toggleViewAction.get(), SIGNAL(triggered(bool)),
            this, SLOT(toggleView(bool)));
}

Hadi Charara's avatar
Hadi Charara committed
32
33
34
35
36
37
ReadmePanel::~ReadmePanel()
{
    ReadmePanelSettings::self()->save();
}


Hadi Charara's avatar
Hadi Charara committed
38
39
40
41
42
43
44
void ReadmePanel::toggleView(bool b)
{
    ReadmePanelSettings::setEnabled(b);
    if (b)
        urlChanged();
    else
        hide();
Hadi Charara's avatar
Hadi Charara committed
45
46
}

Hadi Charara's avatar
Hadi Charara committed
47
void ReadmePanel::show()
Hadi Charara's avatar
Hadi Charara committed
48
49
50
51
52
53
54
55
56
{
    if (!m_markdownPart) {
        KPluginFactory* factory = nullptr;
        KService::Ptr service = KService::serviceByDesktopName(QStringLiteral("markdownpart"));
        if (service) {
            factory = KPluginLoader(service->library()).factory();
        }
        m_markdownPart = factory ? (factory->create<KParts::ReadOnlyPart>(this)) : nullptr;
        m_readmeWidget = m_markdownPart->widget();
Hadi Charara's avatar
Hadi Charara committed
57
        configureTextBrowser();
Hadi Charara's avatar
Hadi Charara committed
58
59
60
61
62
        setFocusProxy(m_readmeWidget);
        m_layout->addWidget(m_readmeWidget);
    }
}

Hadi Charara's avatar
Hadi Charara committed
63
inline void ReadmePanel::configureTextBrowser()
Hadi Charara's avatar
Hadi Charara committed
64
{
Hadi Charara's avatar
Hadi Charara committed
65
66
67
68
69
    if(!m_textBrowser)
        m_textBrowser = m_readmeWidget->findChild<QTextBrowser *>();
    if(!m_textBrowser) return;
    m_textBrowser->setFrameStyle(0);
    m_textBrowser->setStyleSheet("QTextEdit { background-color: transparent; }");
Hadi Charara's avatar
Hadi Charara committed
70
    m_textBrowser->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Hadi Charara's avatar
Hadi Charara committed
71
    m_textBrowser->setTextColor(KColorScheme::ForegroundRole::NormalText);
Hadi Charara's avatar
Hadi Charara committed
72
73
74
}


Hadi Charara's avatar
Hadi Charara committed
75
void ReadmePanel::hide() {
Hadi Charara's avatar
Hadi Charara committed
76
77
    if(m_markdownPart)
        m_markdownPart->closeUrl();
Hadi Charara's avatar
Hadi Charara committed
78
    this->parentWidget()->hide();
Hadi Charara's avatar
Hadi Charara committed
79
80
81
82
}

bool ReadmePanel::urlChanged()
{
83
    m_readmeUrl.setUrl((url().toString().append("/README.md")));
Hadi Charara's avatar
Hadi Charara committed
84
    QFileInfo check_file(m_readmeUrl.path());
85
86
87
88
89
90

    if (!m_readmeUrl.isValid()
        || !check_file.exists()
        || !check_file.isReadable()
        || !toggleViewAction->isChecked()) {

Hadi Charara's avatar
Hadi Charara committed
91
        hide();
92
        return true;
Hadi Charara's avatar
Hadi Charara committed
93
    }
Hadi Charara's avatar
Hadi Charara committed
94
    show();
Hadi Charara's avatar
Hadi Charara committed
95
    this->parentWidget()->show();
Hadi Charara's avatar
Hadi Charara committed
96
97
98
    m_markdownPart->openUrl(m_readmeUrl);
    return true;
}