]> cloud.milkyroute.net Git - dolphin.git/blob - src/sidebartreeview.cpp
allow Konqueror to open also files inside a new tab, not only directories
[dolphin.git] / src / sidebartreeview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "sidebartreeview.h"
21
22 #include "dolphincontroller.h"
23 #include "dolphinmodel.h"
24 #include "draganddrophelper.h"
25
26 #include <kfileitemdelegate.h>
27 #include <QKeyEvent>
28 #include <QPainter>
29 #include <QHeaderView>
30 #include <QScrollBar>
31
32 SidebarTreeView::SidebarTreeView(QWidget* parent) :
33 KTreeView(parent)
34 {
35 setAcceptDrops(true);
36 setUniformRowHeights(true);
37 setSelectionMode(QAbstractItemView::SingleSelection);
38 setEditTriggers(QAbstractItemView::NoEditTriggers);
39 setSortingEnabled(true);
40 setFrameStyle(QFrame::NoFrame);
41 setDragDropMode(QAbstractItemView::DragDrop);
42 setDropIndicatorShown(false);
43 setAutoExpandDelay(300);
44
45 setVerticalScrollMode(QListView::ScrollPerPixel);
46 setHorizontalScrollMode(QListView::ScrollPerPixel);
47
48 viewport()->setAttribute(Qt::WA_Hover);
49
50 QPalette palette = viewport()->palette();
51 palette.setColor(viewport()->backgroundRole(), Qt::transparent);
52 viewport()->setPalette(palette);
53
54 KFileItemDelegate* delegate = new KFileItemDelegate(this);
55 setItemDelegate(delegate);
56 }
57
58 SidebarTreeView::~SidebarTreeView()
59 {
60 }
61
62 bool SidebarTreeView::event(QEvent* event)
63 {
64 if (event->type() == QEvent::Polish) {
65 // hide all columns except of the 'Name' column
66 hideColumn(DolphinModel::Size);
67 hideColumn(DolphinModel::ModifiedTime);
68 hideColumn(DolphinModel::Permissions);
69 hideColumn(DolphinModel::Owner);
70 hideColumn(DolphinModel::Group);
71 hideColumn(DolphinModel::Type);
72 hideColumn(DolphinModel::Rating);
73 hideColumn(DolphinModel::Tags);
74 header()->hide();
75 }
76 else if (event->type() == QEvent::UpdateRequest) {
77 // a wheel movement will scroll 1 item
78 if (model()->rowCount() > 0) {
79 verticalScrollBar()->setSingleStep(sizeHintForRow(0) / 3);
80 }
81 }
82
83 return QTreeView::event(event);
84 }
85
86 void SidebarTreeView::startDrag(Qt::DropActions supportedActions)
87 {
88 DragAndDropHelper::startDrag(this, supportedActions);
89 }
90
91 void SidebarTreeView::dragEnterEvent(QDragEnterEvent* event)
92 {
93 QTreeView::dragEnterEvent(event);
94 if (event->mimeData()->hasUrls()) {
95 event->acceptProposedAction();
96 }
97 }
98
99 void SidebarTreeView::dragLeaveEvent(QDragLeaveEvent* event)
100 {
101 QTreeView::dragLeaveEvent(event);
102 setDirtyRegion(m_dropRect);
103 }
104
105 void SidebarTreeView::dragMoveEvent(QDragMoveEvent* event)
106 {
107 QTreeView::dragMoveEvent(event);
108
109 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
110 const QModelIndex index = indexAt(event->pos());
111 setDirtyRegion(m_dropRect);
112 m_dropRect = visualRect(index);
113 setDirtyRegion(m_dropRect);
114
115 if (event->mimeData()->hasUrls()) {
116 // accept url drops, independently from the destination item
117 event->acceptProposedAction();
118 }
119 }
120
121 void SidebarTreeView::dropEvent(QDropEvent* event)
122 {
123 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
124 if (urls.isEmpty()) {
125 QTreeView::dropEvent(event);
126 } else {
127 event->acceptProposedAction();
128 const QModelIndex index = indexAt(event->pos());
129 if (index.isValid()) {
130 emit urlsDropped(urls, index);
131 }
132 }
133 }
134
135 #include "sidebartreeview.moc"