]> cloud.milkyroute.net Git - dolphin.git/blob - src/sidebartreeview.cpp
* implement the DragAndDropHelper as singleton derived from QObject, so that emitting...
[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
44 setVerticalScrollMode(QListView::ScrollPerPixel);
45 setHorizontalScrollMode(QListView::ScrollPerPixel);
46
47 viewport()->setAttribute(Qt::WA_Hover);
48
49 // make the background transparent and apply the window-text color
50 // to the text color, so that enough contrast is given for all color
51 // schemes
52 QPalette p = palette();
53 p.setColor(QPalette::Active, QPalette::Text, p.color(QPalette::Active, QPalette::WindowText));
54 p.setColor(QPalette::Inactive, QPalette::Text, p.color(QPalette::Inactive, QPalette::WindowText));
55 p.setColor(QPalette::Disabled, QPalette::Text, p.color(QPalette::Disabled, QPalette::WindowText));
56 setPalette(p);
57 viewport()->setAutoFillBackground(false);
58
59 KFileItemDelegate* delegate = new KFileItemDelegate(this);
60 setItemDelegate(delegate);
61 }
62
63 SidebarTreeView::~SidebarTreeView()
64 {
65 }
66
67 bool SidebarTreeView::event(QEvent* event)
68 {
69 if (event->type() == QEvent::Polish) {
70 // hide all columns except of the 'Name' column
71 hideColumn(DolphinModel::Size);
72 hideColumn(DolphinModel::ModifiedTime);
73 hideColumn(DolphinModel::Permissions);
74 hideColumn(DolphinModel::Owner);
75 hideColumn(DolphinModel::Group);
76 hideColumn(DolphinModel::Type);
77 hideColumn(DolphinModel::Rating);
78 hideColumn(DolphinModel::Tags);
79 header()->hide();
80 }
81 else if (event->type() == QEvent::UpdateRequest) {
82 // a wheel movement will scroll 1 item
83 if (model()->rowCount() > 0) {
84 verticalScrollBar()->setSingleStep(sizeHintForRow(0) / 3);
85 }
86 }
87
88 return KTreeView::event(event);
89 }
90
91 void SidebarTreeView::startDrag(Qt::DropActions supportedActions)
92 {
93 DragAndDropHelper::instance().startDrag(this, supportedActions);
94 }
95
96 void SidebarTreeView::dragEnterEvent(QDragEnterEvent* event)
97 {
98 KTreeView::dragEnterEvent(event);
99 if (event->mimeData()->hasUrls()) {
100 event->acceptProposedAction();
101 }
102 }
103
104 void SidebarTreeView::dragLeaveEvent(QDragLeaveEvent* event)
105 {
106 KTreeView::dragLeaveEvent(event);
107 setDirtyRegion(m_dropRect);
108 }
109
110 void SidebarTreeView::dragMoveEvent(QDragMoveEvent* event)
111 {
112 KTreeView::dragMoveEvent(event);
113
114 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
115 const QModelIndex index = indexAt(event->pos());
116 setDirtyRegion(m_dropRect);
117 m_dropRect = visualRect(index);
118 setDirtyRegion(m_dropRect);
119
120 if (event->mimeData()->hasUrls()) {
121 // accept url drops, independently from the destination item
122 event->acceptProposedAction();
123 }
124 }
125
126 void SidebarTreeView::dropEvent(QDropEvent* event)
127 {
128 const QModelIndex index = indexAt(event->pos());
129 if (index.isValid()) {
130 emit urlsDropped(index, event);
131 }
132 KTreeView::dropEvent(event);
133 }
134
135 #include "sidebartreeview.moc"