archiveview.cpp 5.15 KB
Newer Older
1
2
3
/*
 * ark -- archiver for the KDE project
 *
4
 * Copyright (C) 2008-2009 Harald Hvaal <haraldhv (at@at) stud.ntnu.no>
5
 * Copyright (c) 2016 Vladyslav Batyrenko <mvlabat@gmail.com>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "archiveview.h"
24
#include "ark_debug.h"
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
25

26
#include <QHeaderView>
Bhushan Shah's avatar
Bhushan Shah committed
27
#include <QMimeData>
28
29
#include <QDragEnterEvent>
#include <QDragMoveEvent>
Harald Hvaal's avatar
Harald Hvaal committed
30
#include <QMouseEvent>
31
#include <QLineEdit>
32
33

ArchiveView::ArchiveView(QWidget *parent)
Lukáš Tinkl's avatar
Lukáš Tinkl committed
34
    : QTreeView(parent)
35
{
36
37
38
39
40
41
42
43
    setSelectionMode(QAbstractItemView::ExtendedSelection);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setAlternatingRowColors(true);
    setAnimated(true);
    setAllColumnsShowFocus(true);
    setSortingEnabled(true);
    setDragEnabled(true);
    setDropIndicatorShown(true);
44
45
    // #368807: drops must be initially disabled, otherwise they will override the MainWindow's ones.
    // They will be enabled in Part::slotLoadingFinished().
46
    setDropsEnabled(false);
47
    header()->setSectionResizeMode(QHeaderView::ResizeToContents);
48
49
}

50
void ArchiveView::startDrag(Qt::DropActions supportedActions)
51
{
52
53
    //only start the drag if it's over the filename column. this allows dragging selection in
    //tree/detail view
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
54
55
56
    if (currentIndex().column() != 0) {
        return;
    }
57

58
    QTreeView::startDrag(supportedActions);
59
60
}

61
62
63
64
65
66
67
void ArchiveView::expandIfSingleFolder()
{
    if (model()->rowCount() == 1) {
        expandToDepth(0);
    }
}

68
69
70
void ArchiveView::setDropsEnabled(bool enabled)
{
    setAcceptDrops(enabled);
71
    setDragDropMode(enabled ? QAbstractItemView::DragDrop : QAbstractItemView::DragOnly);
72
}
73

74
void ArchiveView::dragEnterEvent(QDragEnterEvent * event)
75
{
76
    //TODO: if no model, trigger some mechanism to create one automatically!
77
    qCDebug(ARK) << event;
78

79
80
81
82
    if (event->source() == this) {
        //we don't support internal drops yet.
        return;
    }
83

84
    QTreeView::dragEnterEvent(event);
85
86
}

87
void ArchiveView::dropEvent(QDropEvent * event)
88
{
89
    qCDebug(ARK) << event;
90

91
92
93
94
    if (event->source() == this) {
        //we don't support internal drops yet.
        return;
    }
95

96
    QTreeView::dropEvent(event);
97
98
}

99
void ArchiveView::dragMoveEvent(QDragMoveEvent * event)
100
{
101
    qCDebug(ARK) << event;
102

103
104
105
106
107
108
    if (event->source() == this) {
        //we don't support internal drops yet.
        return;
    }

    QTreeView::dragMoveEvent(event);
Lukáš Tinkl's avatar
Lukáš Tinkl committed
109
    if (event->mimeData()->hasFormat(QStringLiteral("text/uri-list"))) {
110
111
112
113
114
115
116
        const auto urls = event->mimeData()->urls();
        for (const auto &url : urls) {
            if (!url.isLocalFile()) {
                return;
            }
        }

117
        event->acceptProposedAction();
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
118
    }
119
}
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

bool ArchiveView::eventFilter(QObject *object, QEvent *event)
{
    if (object == m_entryEditor && event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if (keyEvent->key() == Qt::Key_Escape) {
            closeEntryEditor();
            return true;
        }
    }
    return false;
}

void ArchiveView::mouseReleaseEvent(QMouseEvent *event)
{
    if (m_editorIndex.isValid()) {
        closeEntryEditor();
Elvis Angelaccio's avatar
Elvis Angelaccio committed
137
    } else {
138
139
140
141
142
143
144
145
        QTreeView::mouseReleaseEvent(event);
    }
}

void ArchiveView::keyPressEvent(QKeyEvent *event)
{
    if (m_editorIndex.isValid()) {
        switch (event->key()) {
Elvis Angelaccio's avatar
Elvis Angelaccio committed
146
147
148
149
150
151
        case Qt::Key_Return:
        case Qt::Key_Enter: {
            QLineEdit* editor = static_cast<QLineEdit*>(indexWidget(m_editorIndex));
            emit entryChanged(editor->text());
            closeEntryEditor();
            break;
152
        }
Elvis Angelaccio's avatar
Elvis Angelaccio committed
153
154
155
156
        default:
            QTreeView::keyPressEvent(event);
        }
    } else {
157
158
159
160
        QTreeView::keyPressEvent(event);
    }
}

161
162
163
164
165
166
167
168
169
void ArchiveView::renameSelectedEntry()
{
    QModelIndex currentIndex = selectionModel()->currentIndex();
    currentIndex = (currentIndex.parent().isValid())
                   ? currentIndex.parent().child(currentIndex.row(), 0)
                   : model()->index(currentIndex.row(), 0);
    openEntryEditor(currentIndex);
}

170
void ArchiveView::openEntryEditor(const QModelIndex &index)
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
    m_editorIndex = index;
    openPersistentEditor(index);
    m_entryEditor = static_cast<QLineEdit*>(indexWidget(m_editorIndex));
    m_entryEditor->installEventFilter(this);
    m_entryEditor->setText(index.data().toString());
    m_entryEditor->setFocus(Qt::OtherFocusReason);
    m_entryEditor->selectAll();
}

void ArchiveView::closeEntryEditor()
{
    m_entryEditor->removeEventFilter(this);
    closePersistentEditor(m_editorIndex);
    m_editorIndex = QModelIndex();
}