]> cloud.milkyroute.net Git - dolphin.git/blob - src/sidebartreeview.cpp
Provide horizontal auto scrolling for the tree view (implemented by Harald Hvaal...
[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 else if (event->type() == QEvent::MetaCall) {
83 resizeColumnToContents(DolphinModel::Name);
84 }
85
86 return QTreeView::event(event);
87 }
88
89 void SidebarTreeView::startDrag(Qt::DropActions supportedActions)
90 {
91 DragAndDropHelper::startDrag(this, supportedActions);
92 }
93
94 void SidebarTreeView::dragEnterEvent(QDragEnterEvent* event)
95 {
96 QTreeView::dragEnterEvent(event);
97 if (event->mimeData()->hasUrls()) {
98 event->acceptProposedAction();
99 }
100 }
101
102 void SidebarTreeView::dragLeaveEvent(QDragLeaveEvent* event)
103 {
104 QTreeView::dragLeaveEvent(event);
105 setDirtyRegion(m_dropRect);
106 }
107
108 void SidebarTreeView::dragMoveEvent(QDragMoveEvent* event)
109 {
110 QTreeView::dragMoveEvent(event);
111
112 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
113 const QModelIndex index = indexAt(event->pos());
114 setDirtyRegion(m_dropRect);
115 m_dropRect = visualRect(index);
116 setDirtyRegion(m_dropRect);
117
118 if (event->mimeData()->hasUrls()) {
119 // accept url drops, independently from the destination item
120 event->acceptProposedAction();
121 }
122 }
123
124 void SidebarTreeView::dropEvent(QDropEvent* event)
125 {
126 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
127 if (urls.isEmpty()) {
128 QTreeView::dropEvent(event);
129 } else {
130 event->acceptProposedAction();
131 const QModelIndex index = indexAt(event->pos());
132 if (index.isValid()) {
133 emit urlsDropped(urls, index);
134 }
135 }
136 }
137
138 #include "sidebartreeview.moc"