~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to addons/project/kateprojectviewtree.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  This file is part of the Kate project.
 
2
 *
 
3
 *  Copyright (C) 2012 Christoph Cullmann <cullmann@kde.org>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 */
 
20
 
 
21
#include "kateprojectviewtree.h"
 
22
#include "kateprojectpluginview.h"
 
23
#include "kateprojecttreeviewcontextmenu.h"
 
24
 
 
25
#include <ktexteditor/document.h>
 
26
#include <ktexteditor/view.h>
 
27
 
 
28
#include <QContextMenuEvent>
 
29
#include <krecursivefilterproxymodel.h>
 
30
 
 
31
KateProjectViewTree::KateProjectViewTree(KateProjectPluginView *pluginView, KateProject *project)
 
32
    : QTreeView()
 
33
    , m_pluginView(pluginView)
 
34
    , m_project(project)
 
35
{
 
36
    /**
 
37
     * default style
 
38
     */
 
39
    setHeaderHidden(true);
 
40
    setEditTriggers(QAbstractItemView::NoEditTriggers);
 
41
 
 
42
    /**
 
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
 
47
     */
 
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());
 
55
    setModel(sortModel);
 
56
    delete m;
 
57
 
 
58
    /**
 
59
     * connect needed signals
 
60
     */
 
61
    connect(this, &KateProjectViewTree::clicked, this, &KateProjectViewTree::slotClicked);
 
62
    connect(m_project, &KateProject::modelChanged, this, &KateProjectViewTree::slotModelChanged);
 
63
 
 
64
    /**
 
65
     * trigger once some slots
 
66
     */
 
67
    slotModelChanged();
 
68
}
 
69
 
 
70
KateProjectViewTree::~KateProjectViewTree()
 
71
{
 
72
}
 
73
 
 
74
void KateProjectViewTree::selectFile(const QString &file)
 
75
{
 
76
    /**
 
77
     * get item if any
 
78
     */
 
79
    QStandardItem *item = m_project->itemForFile(file);
 
80
    if (!item) {
 
81
        return;
 
82
    }
 
83
 
 
84
    /**
 
85
     * select it
 
86
     */
 
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);
 
90
}
 
91
 
 
92
void KateProjectViewTree::openSelectedDocument()
 
93
{
 
94
    /**
 
95
     * anything selected?
 
96
     */
 
97
    QModelIndexList selecteStuff = selectedIndexes();
 
98
    if (selecteStuff.isEmpty()) {
 
99
        return;
 
100
    }
 
101
 
 
102
    /**
 
103
     * open document for first element, if possible
 
104
     */
 
105
    QString filePath = selecteStuff[0].data(Qt::UserRole).toString();
 
106
    if (!filePath.isEmpty()) {
 
107
        m_pluginView->mainWindow()->openUrl(QUrl::fromLocalFile(filePath));
 
108
    }
 
109
}
 
110
 
 
111
void KateProjectViewTree::slotClicked(const QModelIndex &index)
 
112
{
 
113
    /**
 
114
     * open document, if any usable user data
 
115
     */
 
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);
 
120
    }
 
121
}
 
122
 
 
123
void KateProjectViewTree::slotModelChanged()
 
124
{
 
125
    /**
 
126
     * model was updated
 
127
     * perhaps we need to highlight again new file
 
128
     */
 
129
    KTextEditor::View *activeView = m_pluginView->mainWindow()->activeView();
 
130
    if (activeView && activeView->document()->url().isLocalFile()) {
 
131
        selectFile(activeView->document()->url().toLocalFile());
 
132
    }
 
133
}
 
134
 
 
135
void KateProjectViewTree::contextMenuEvent(QContextMenuEvent *event)
 
136
{
 
137
    /**
 
138
     * get path file path or don't do anything
 
139
     */
 
140
    QModelIndex index = selectionModel()->currentIndex();
 
141
    QString filePath = index.data(Qt::UserRole).toString();
 
142
    if (filePath.isEmpty()) {
 
143
        QTreeView::contextMenuEvent(event);
 
144
        return;
 
145
    }
 
146
 
 
147
    KateProjectTreeViewContextMenu menu;
 
148
    menu.exec(filePath, viewport()->mapToGlobal(event->pos()), this);
 
149
 
 
150
    event->accept();
 
151
}