]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
get libkonq from kdebase
[dolphin.git] / src / dolphindetailsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphindetailsview.h"
22
23 #include "dolphinmainwindow.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "viewproperties.h"
27
28 #include <assert.h>
29 #include <kdirmodel.h>
30 #include <QHeaderView>
31
32 DolphinDetailsView::DolphinDetailsView(DolphinView* parent) :
33 QTreeView(parent),
34 m_dolphinView(parent)
35 {
36 assert(parent != 0);
37
38 setAcceptDrops(true);
39 setRootIsDecorated(false);
40 setSortingEnabled(true);
41 setUniformRowHeights(true);
42
43 const ViewProperties props(parent->url());
44 setSortIndicatorSection(props.sorting());
45 setSortIndicatorOrder(props.sortOrder());
46
47 connect(header(), SIGNAL(sectionClicked(int)),
48 this, SLOT(synchronizeSortingState(int)));
49
50 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
51 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
52 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
53 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
54 }
55
56 DolphinDetailsView::~DolphinDetailsView()
57 {
58 }
59
60 bool DolphinDetailsView::event(QEvent* event)
61 {
62 if (event->type() == QEvent::Polish) {
63 // Assure that by respecting the available width that:
64 // - the 'Name' column is stretched as large as possible
65 // - the remaining columns are as small as possible
66 QHeaderView* headerView = header();
67 headerView->setStretchLastSection(false);
68 headerView->setResizeMode(QHeaderView::ResizeToContents);
69 headerView->setResizeMode(0, QHeaderView::Stretch);
70 }
71
72 return QTreeView::event(event);
73 }
74 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
75 {
76 return QTreeView::viewOptions();
77
78 // TODO: the view options should been read from the settings;
79 // the following code is just for testing...
80 //QStyleOptionViewItem options = QTreeView::viewOptions();
81 //options.decorationAlignment = Qt::AlignRight;
82 //options.decorationPosition = QStyleOptionViewItem::Right;
83 //options.decorationSize = QSize(100, 100);
84 //options.showDecorationSelected = true;
85 //options.state = QStyle::State_MouseOver;
86 //return options;
87 }
88
89 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
90 {
91 QTreeView::contextMenuEvent(event);
92
93 KFileItem* item = 0;
94
95 const QModelIndex index = indexAt(event->pos());
96 if (index.isValid()) {
97 item = m_dolphinView->fileItem(index);
98 }
99
100 m_dolphinView->openContextMenu(item, event->globalPos());
101 }
102
103 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
104 {
105 QTreeView::mouseReleaseEvent(event);
106 m_dolphinView->declareViewActive();
107 }
108
109 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
110 {
111 if (event->mimeData()->hasUrls()) {
112 event->acceptProposedAction();
113 }
114 }
115
116 void DolphinDetailsView::dropEvent(QDropEvent* event)
117 {
118 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
119 if (!urls.isEmpty()) {
120 event->acceptProposedAction();
121
122 // TODO: handle dropping above a directory
123
124 const KUrl& destination = m_dolphinView->url();
125 m_dolphinView->mainWindow()->dropUrls(urls, destination);
126 }
127 }
128
129 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
130 {
131 QHeaderView* headerView = header();
132 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
133 }
134
135 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
136 {
137 QHeaderView* headerView = header();
138 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
139 }
140
141 void DolphinDetailsView::synchronizeSortingState(int column)
142 {
143 // The sorting has already been changed in QTreeView if this slot is
144 // invoked, but Dolphin was not informed about this. This is bypassed by changing
145 // the sorting and sort order to a temporary other value and readjust it again.
146 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
147 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
148
149 // temporary adjust the sorting and sort order to different values...
150 const DolphinView::Sorting tempSorting = (sorting == DolphinView::SortByName) ?
151 DolphinView::SortBySize :
152 DolphinView::SortByName;
153 m_dolphinView->setSorting(tempSorting);
154 const Qt::SortOrder tempSortOrder = (sortOrder == Qt::Ascending) ?
155 Qt::Descending : Qt::Ascending;
156 m_dolphinView->setSortOrder(tempSortOrder);
157
158 // ... so that setting them again results in storing the new setting.
159 m_dolphinView->setSorting(sorting);
160 m_dolphinView->setSortOrder(sortOrder);
161 }
162
163 #include "dolphindetailsview.moc"