kateprojectview.cpp 4.66 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())
32
    , m_branchBtn(new QPushButton)
33
34
    , m_gitBtn(new QPushButton)
    , m_stackWidget(new QStackedWidget)
Waqar Ahmed's avatar
Waqar Ahmed committed
35
    , m_gitWidget(new GitWidget(project, this, mainWindow))
36
{
37
38
39
40
    /**
     * layout tree view and co.
     */
    QVBoxLayout *layout = new QVBoxLayout();
41
    QHBoxLayout *btnLayout = new QHBoxLayout();
42
43
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
44
45
46
47
    layout->addLayout(btnLayout);
    layout->addWidget(m_gitBtn);
    layout->addWidget(m_stackWidget);
    //    layout->addWidget(m_treeView);
48
49
50
    layout->addWidget(m_filter);
    setLayout(layout);

51
52
53
54
55
56
57
58
    btnLayout->addWidget(m_branchBtn);
    btnLayout->setStretch(0, 2);
    btnLayout->addWidget(m_gitBtn);

    m_stackWidget->addWidget(m_treeView);
    m_stackWidget->addWidget(m_gitWidget);

    connect(m_gitBtn, &QPushButton::clicked, this, [this] {
59
        m_gitWidget->getStatus();
60
61
62
        m_stackWidget->setCurrentWidget(m_gitWidget);
    });

63
    m_branchBtn->setText(GitUtils::getCurrentBranchName(m_project->baseDir()));
Waqar Ahmed's avatar
Waqar Ahmed committed
64
65
    m_branchBtn->setIcon(QIcon(QStringLiteral(":/icons/icons/sc-apps-git.svg")));
    m_gitBtn->setIcon(QIcon(QStringLiteral(":/icons/icons/sc-apps-git.svg")));
66

67
68
69
    // let tree get focus for keyboard selection of file to open
    setFocusProxy(m_treeView);

70
    // add to actionCollection so that this is available in Kate Command bar
71
    auto chckbr = pluginView->actionCollection()->addAction(QStringLiteral("checkout_branch"), this, [this] {
72
73
        m_branchBtn->click();
    });
74
    chckbr->setText(QStringLiteral("Checkout Git Branch"));
75

76
77
78
    /**
     * setup filter line edit
     */
79
    m_filter->setPlaceholderText(i18n("Filter..."));
80
    m_filter->setClearButtonEnabled(true);
81
    connect(m_filter, &KLineEdit::textChanged, this, &KateProjectView::filterTextChanged);
82
83
84
85
86
87

    /**
     * Setup git checkout stuff
     */
    m_branchBtn->setHidden(true);
    m_branchesDialog = new BranchesDialog(this, mainWindow, m_project->baseDir());
88
    connect(m_branchBtn, &QPushButton::clicked, this, [this] {
89
90
91
92
        if (m_stackWidget->currentWidget() != m_treeView) {
            m_stackWidget->setCurrentWidget(m_treeView);
            return;
        }
93
94
95
96
        m_branchesDialog->openDialog();
    });
    connect(m_branchesDialog, &BranchesDialog::branchChanged, this, [this](const QString &branch) {
        m_branchBtn->setText(branch);
97
        m_project->reload(true);
98
    });
99
100
101
102
    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()));
103
104
105
            if (m_branchChangedWatcher.files().isEmpty()) {
                m_branchChangedWatcher.addPath(m_project->baseDir() + QStringLiteral("/.git/HEAD"));
            }
106
            m_gitWidget->getStatus();
107
        } else {
108
109
110
            if (!m_branchChangedWatcher.files().isEmpty()) {
                m_branchChangedWatcher.removePaths(m_branchChangedWatcher.files());
            }
111
112
113
            m_branchBtn->setHidden(true);
        }
    });
114
115
116
    connect(&m_branchChangedWatcher, &QFileSystemWatcher::fileChanged, this, [this] {
        m_project->reload(true);
    });
117
118
119
    connect(m_gitWidget, &GitWidget::checkoutBranch, this, [this] {
        m_branchesDialog->openDialog();
    });
120
121
}

122
KateProjectView::~KateProjectView()
123
124
125
{
}

126
void KateProjectView::selectFile(const QString &file)
127
{
128
    m_treeView->selectFile(file);
129
130
}

131
void KateProjectView::openSelectedDocument()
132
{
133
    m_treeView->openSelectedDocument();
134
135
}

Laurent Montel's avatar
Laurent Montel committed
136
void KateProjectView::filterTextChanged(const QString &filterText)
Christoph Cullmann's avatar
Christoph Cullmann committed
137
{
138
139
140
    /**
     * filter
     */
Alexander Lohnau's avatar
Alexander Lohnau committed
141
    static_cast<KateProjectFilterProxyModel *>(m_treeView->model())->setFilterString(filterText);
142
143
144
145
146

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