]> cloud.milkyroute.net Git - dolphin.git/blob - src/sidebartreeview.cpp
Our beloved qt-copy also has the patch applied
[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
25 #include <kfileitemdelegate.h>
26 #include <QKeyEvent>
27 #include <QPainter>
28 #include <QHeaderView>
29 #include <QScrollBar>
30
31 SidebarTreeView::SidebarTreeView(QWidget* parent) :
32 QTreeView(parent),
33 m_dragging(false)
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 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
46 // check avoids a division by zero happening on versions before 4.3.1.
47 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
48 // ereslibre
49 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
50 setVerticalScrollMode(QListView::ScrollPerPixel);
51 setHorizontalScrollMode(QListView::ScrollPerPixel);
52 #endif
53
54 viewport()->setAttribute(Qt::WA_Hover);
55
56 QPalette palette = viewport()->palette();
57 palette.setColor(viewport()->backgroundRole(), Qt::transparent);
58 viewport()->setPalette(palette);
59
60 KFileItemDelegate* delegate = new KFileItemDelegate(this);
61 setItemDelegate(delegate);
62 }
63
64 SidebarTreeView::~SidebarTreeView()
65 {
66 }
67
68 bool SidebarTreeView::event(QEvent* event)
69 {
70 if (event->type() == QEvent::Polish) {
71 // hide all columns except of the 'Name' column
72 hideColumn(DolphinModel::Size);
73 hideColumn(DolphinModel::ModifiedTime);
74 hideColumn(DolphinModel::Permissions);
75 hideColumn(DolphinModel::Owner);
76 hideColumn(DolphinModel::Group);
77 hideColumn(DolphinModel::Type);
78 hideColumn(DolphinModel::Rating);
79 hideColumn(DolphinModel::Tags);
80 header()->hide();
81 }
82 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
83 // check avoids a division by zero happening on versions before 4.3.1.
84 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
85 // ereslibre
86 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
87 else if (event->type() == QEvent::UpdateRequest) {
88 // a wheel movement will scroll 1 item
89 if (model()->rowCount() > 0) {
90 verticalScrollBar()->setSingleStep(sizeHintForRow(0) / 3);
91 }
92 }
93 #endif
94
95 return QTreeView::event(event);
96 }
97
98 void SidebarTreeView::dragEnterEvent(QDragEnterEvent* event)
99 {
100 if (event->mimeData()->hasUrls()) {
101 event->acceptProposedAction();
102 }
103 QTreeView::dragEnterEvent(event);
104 m_dragging = true;
105 }
106
107 void SidebarTreeView::dragLeaveEvent(QDragLeaveEvent* event)
108 {
109 QTreeView::dragLeaveEvent(event);
110
111 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
112 m_dragging = false;
113 setDirtyRegion(m_dropRect);
114 }
115
116 void SidebarTreeView::dragMoveEvent(QDragMoveEvent* event)
117 {
118 QTreeView::dragMoveEvent(event);
119
120 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
121 const QModelIndex index = indexAt(event->pos());
122 setDirtyRegion(m_dropRect);
123 m_dropRect = visualRect(index);
124 setDirtyRegion(m_dropRect);
125 }
126
127 void SidebarTreeView::dropEvent(QDropEvent* event)
128 {
129 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
130 if (urls.isEmpty()) {
131 QTreeView::dropEvent(event);
132 } else {
133 event->acceptProposedAction();
134 const QModelIndex index = indexAt(event->pos());
135 if (index.isValid()) {
136 emit urlsDropped(urls, index);
137 }
138 }
139 m_dragging = false;
140 }
141
142 void SidebarTreeView::paintEvent(QPaintEvent* event)
143 {
144 QTreeView::paintEvent(event);
145
146 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
147 if (m_dragging) {
148 const QBrush& brush = palette().brush(QPalette::Normal, QPalette::Highlight);
149 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
150 }
151 }
152
153 #include "sidebartreeview.moc"