kateprojectview.cpp 3.88 KB
Newer Older
1
2
/*  This file is part of the Kate project.
 *
3
 *  SPDX-FileCopyrightText: 2012 Christoph Cullmann <cullmann@kde.org>
4
 *
5
 *  SPDX-License-Identifier: LGPL-2.0-or-later
6
7
8
 */

#include "kateprojectview.h"
9
#include "branchesdialog.h"
10
11
#include "git/gitutils.h"
#include "gitwidget.h"
12
#include "kateprojectfiltermodel.h"
Alexander Lohnau's avatar
Alexander Lohnau committed
13
#include "kateprojectpluginview.h"
14

15
16
#include <KTextEditor/Document>
#include <KTextEditor/View>
17

18
#include <KActionCollection>
Christoph Cullmann's avatar
Christoph Cullmann committed
19
#include <KLineEdit>
20
21
#include <KLocalizedString>

22
#include <QPushButton>
23
#include <QSortFilterProxyModel>
Christoph Cullmann's avatar
Christoph Cullmann committed
24
#include <QTimer>
25
#include <QVBoxLayout>
26

27
KateProjectView::KateProjectView(KateProjectPluginView *pluginView, KateProject *project, KTextEditor::MainWindow *mainWindow)
28
    : m_pluginView(pluginView)
29
30
31
    , m_project(project)
    , m_treeView(new KateProjectViewTree(pluginView, project))
    , m_filter(new KLineEdit())
Dominik Haumann's avatar
Dominik Haumann committed
32
    , m_branchBtn(new QToolButton)
33
{
34
35
36
37
38
39
    /**
     * layout tree view and co.
     */
    QVBoxLayout *layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
Waqar Ahmed's avatar
Waqar Ahmed committed
40
41
    layout->addWidget(m_branchBtn);
    layout->addWidget(m_treeView);
42
43
44
    layout->addWidget(m_filter);
    setLayout(layout);

Dominik Haumann's avatar
Dominik Haumann committed
45
46
47
    m_branchBtn->setAutoRaise(true);
    m_branchBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    m_branchBtn->setSizePolicy(QSizePolicy::Minimum, m_branchBtn->sizePolicy().verticalPolicy());
48
    m_branchBtn->setText(GitUtils::getCurrentBranchName(m_project->baseDir()));
Waqar Ahmed's avatar
Waqar Ahmed committed
49
    m_branchBtn->setIcon(QIcon(QStringLiteral(":/icons/icons/sc-apps-git.svg")));
50

51
52
53
    // let tree get focus for keyboard selection of file to open
    setFocusProxy(m_treeView);

54
    // add to actionCollection so that this is available in Kate Command bar
55
    auto chckbr = pluginView->actionCollection()->addAction(QStringLiteral("checkout_branch"), this, [this] {
56
57
        m_branchBtn->click();
    });
Dominik Haumann's avatar
Dominik Haumann committed
58
    chckbr->setText(i18n("Checkout Git Branch"));
59

60
61
62
    /**
     * setup filter line edit
     */
63
    m_filter->setPlaceholderText(i18n("Filter..."));
64
    m_filter->setClearButtonEnabled(true);
65
    connect(m_filter, &KLineEdit::textChanged, this, &KateProjectView::filterTextChanged);
66
67
68
69
70
71

    /**
     * Setup git checkout stuff
     */
    m_branchBtn->setHidden(true);
    m_branchesDialog = new BranchesDialog(this, mainWindow, m_project->baseDir());
72
73
74
75
76
    connect(m_branchBtn, &QPushButton::clicked, this, [this] {
        m_branchesDialog->openDialog();
    });
    connect(m_branchesDialog, &BranchesDialog::branchChanged, this, [this](const QString &branch) {
        m_branchBtn->setText(branch);
77
        m_project->reload(true);
78
    });
79
80
81
82
    connect(m_project, &KateProject::modelChanged, this, [this] {
        if (GitUtils::isGitRepo(m_project->baseDir())) {
            m_branchBtn->setHidden(false);
            m_branchBtn->setText(GitUtils::getCurrentBranchName(m_project->baseDir()));
83
84
85
            if (m_branchChangedWatcher.files().isEmpty()) {
                m_branchChangedWatcher.addPath(m_project->baseDir() + QStringLiteral("/.git/HEAD"));
            }
86
        } else {
87
88
89
            if (!m_branchChangedWatcher.files().isEmpty()) {
                m_branchChangedWatcher.removePaths(m_branchChangedWatcher.files());
            }
90
91
92
            m_branchBtn->setHidden(true);
        }
    });
93
94
95
    connect(&m_branchChangedWatcher, &QFileSystemWatcher::fileChanged, this, [this] {
        m_project->reload(true);
    });
96
97
}

98
KateProjectView::~KateProjectView()
99
100
101
{
}

102
void KateProjectView::selectFile(const QString &file)
103
{
104
    m_treeView->selectFile(file);
105
106
}

107
void KateProjectView::openSelectedDocument()
108
{
109
    m_treeView->openSelectedDocument();
110
111
}

Laurent Montel's avatar
Laurent Montel committed
112
void KateProjectView::filterTextChanged(const QString &filterText)
Christoph Cullmann's avatar
Christoph Cullmann committed
113
{
114
115
116
    /**
     * filter
     */
Alexander Lohnau's avatar
Alexander Lohnau committed
117
    static_cast<KateProjectFilterProxyModel *>(m_treeView->model())->setFilterString(filterText);
118
119
120
121
122

    /**
     * expand
     */
    if (!filterText.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
123
        QTimer::singleShot(100, m_treeView, &QTreeView::expandAll);
124
    }
Christoph Cullmann's avatar
Christoph Cullmann committed
125
}