1
/* This file is part of the Kate project.
3
* Copyright (C) 2012 Christoph Cullmann <cullmann@kde.org>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "kateprojectviewtree.h"
22
#include "kateprojectpluginview.h"
23
#include "kateprojecttreeviewcontextmenu.h"
25
#include <ktexteditor/document.h>
26
#include <ktexteditor/view.h>
28
#include <QContextMenuEvent>
29
#include <krecursivefilterproxymodel.h>
31
KateProjectViewTree::KateProjectViewTree(KateProjectPluginView *pluginView, KateProject *project)
33
, m_pluginView(pluginView)
39
setHeaderHidden(true);
40
setEditTriggers(QAbstractItemView::NoEditTriggers);
43
* attach view => project
44
* do this once, model is stable for whole project life time
45
* kill selection model
46
* create sort proxy model
48
QItemSelectionModel *m = selectionModel();
49
QSortFilterProxyModel *sortModel = new KRecursiveFilterProxyModel(this);
50
//sortModel->setFilterRole(SortFilterRole);
51
//sortModel->setSortRole(SortFilterRole);
52
sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
53
sortModel->setSortCaseSensitivity(Qt::CaseInsensitive);
54
sortModel->setSourceModel(m_project->model());
59
* connect needed signals
61
connect(this, &KateProjectViewTree::clicked, this, &KateProjectViewTree::slotClicked);
62
connect(m_project, &KateProject::modelChanged, this, &KateProjectViewTree::slotModelChanged);
65
* trigger once some slots
70
KateProjectViewTree::~KateProjectViewTree()
74
void KateProjectViewTree::selectFile(const QString &file)
79
QStandardItem *item = m_project->itemForFile(file);
87
QModelIndex index = static_cast<QSortFilterProxyModel *>(model())->mapFromSource(m_project->model()->indexFromItem(item));
88
scrollTo(index, QAbstractItemView::EnsureVisible);
89
selectionModel()->setCurrentIndex(index, QItemSelectionModel::Clear | QItemSelectionModel::Select);
92
void KateProjectViewTree::openSelectedDocument()
97
QModelIndexList selecteStuff = selectedIndexes();
98
if (selecteStuff.isEmpty()) {
103
* open document for first element, if possible
105
QString filePath = selecteStuff[0].data(Qt::UserRole).toString();
106
if (!filePath.isEmpty()) {
107
m_pluginView->mainWindow()->openUrl(QUrl::fromLocalFile(filePath));
111
void KateProjectViewTree::slotClicked(const QModelIndex &index)
114
* open document, if any usable user data
116
QString filePath = index.data(Qt::UserRole).toString();
117
if (!filePath.isEmpty()) {
118
m_pluginView->mainWindow()->openUrl(QUrl::fromLocalFile(filePath));
119
selectionModel()->setCurrentIndex(index, QItemSelectionModel::Clear | QItemSelectionModel::Select);
123
void KateProjectViewTree::slotModelChanged()
127
* perhaps we need to highlight again new file
129
KTextEditor::View *activeView = m_pluginView->mainWindow()->activeView();
130
if (activeView && activeView->document()->url().isLocalFile()) {
131
selectFile(activeView->document()->url().toLocalFile());
135
void KateProjectViewTree::contextMenuEvent(QContextMenuEvent *event)
138
* get path file path or don't do anything
140
QModelIndex index = selectionModel()->currentIndex();
141
QString filePath = index.data(Qt::UserRole).toString();
142
if (filePath.isEmpty()) {
143
QTreeView::contextMenuEvent(event);
147
KateProjectTreeViewContextMenu menu;
148
menu.exec(filePath, viewport()->mapToGlobal(event->pos()), this);