]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
If the user changes the sorting by clicking on a header section, the resulting sortin...
[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 "dolphinview.h"
25 #include "viewproperties.h"
26
27 #include <assert.h>
28 #include <kdirmodel.h>
29 #include <QHeaderView>
30
31 DolphinDetailsView::DolphinDetailsView(DolphinView* parent) :
32 QTreeView(parent),
33 m_dolphinView(parent)
34 {
35 assert(parent != 0);
36
37 setAcceptDrops(true);
38 setRootIsDecorated(false);
39 setSortingEnabled(true);
40 setUniformRowHeights(true);
41
42 const ViewProperties props(parent->url());
43 setSortIndicatorSection(props.sorting());
44 setSortIndicatorOrder(props.sortOrder());
45
46 connect(header(), SIGNAL(sectionClicked(int)),
47 this, SLOT(synchronizeSortingState(int)));
48
49 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
50 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
51 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
52 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
53 }
54
55 DolphinDetailsView::~DolphinDetailsView()
56 {
57 }
58
59 bool DolphinDetailsView::event(QEvent* event)
60 {
61 if (event->type() == QEvent::Polish) {
62 // Assure that by respecting the available width that:
63 // - the 'Name' column is stretched as large as possible
64 // - the remaining columns are as small as possible
65 QHeaderView* headerView = header();
66 headerView->setStretchLastSection(false);
67 headerView->setResizeMode(QHeaderView::ResizeToContents);
68 headerView->setResizeMode(0, QHeaderView::Stretch);
69 }
70
71 return QTreeView::event(event);
72 }
73 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
74 {
75 return QTreeView::viewOptions();
76
77 // TODO: the view options should been read from the settings;
78 // the following code is just for testing...
79 //QStyleOptionViewItem options = QTreeView::viewOptions();
80 //options.decorationAlignment = Qt::AlignRight;
81 //options.decorationPosition = QStyleOptionViewItem::Right;
82 //options.decorationSize = QSize(100, 100);
83 //options.showDecorationSelected = true;
84 //options.state = QStyle::State_MouseOver;
85 //return options;
86 }
87
88 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
89 {
90 QTreeView::contextMenuEvent(event);
91
92 KFileItem* item = 0;
93
94 const QModelIndex index = indexAt(event->pos());
95 if (index.isValid()) {
96 item = m_dolphinView->fileItem(index);
97 }
98
99 m_dolphinView->openContextMenu(item, event->globalPos());
100 }
101
102 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
103 {
104 QTreeView::mouseReleaseEvent(event);
105 m_dolphinView->declareViewActive();
106 }
107
108 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
109 {
110 if (event->mimeData()->hasUrls()) {
111 event->acceptProposedAction();
112 }
113 }
114
115 void DolphinDetailsView::dropEvent(QDropEvent* event)
116 {
117 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
118 if (!urls.isEmpty()) {
119 event->acceptProposedAction();
120
121 // TODO: handle dropping above a directory
122
123 const KUrl& destination = m_dolphinView->url();
124 m_dolphinView->mainWindow()->dropUrls(urls, destination);
125 }
126 }
127
128 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
129 {
130 QHeaderView* headerView = header();
131 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
132 }
133
134 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
135 {
136 QHeaderView* headerView = header();
137 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
138 }
139
140 void DolphinDetailsView::synchronizeSortingState(int column)
141 {
142 // The sorting has already been changed in QTreeView if this slot is
143 // invoked, but Dolphin was not informed about this. This is bypassed by changing
144 // the sorting and sort order to a temporary other value and readjust it again.
145 const bool update = (column == KDirModel::Name) || (column == KDirModel::Size) ||
146 (column == KDirModel::ModifiedTime);
147 if (update) {
148 DolphinView::Sorting sorting = DolphinView::SortByName;
149 switch (column) {
150 case KDirModel::Size: sorting = DolphinView::SortBySize; break;
151 case KDirModel::ModifiedTime: sorting = DolphinView::SortByDate; break;
152 case KDirModel::Name:
153 default: break;
154 }
155
156 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
157
158 // temporary adjust the sorting and sort order to different values...
159 const DolphinView::Sorting tempSorting = (sorting == DolphinView::SortByName) ?
160 DolphinView::SortBySize :
161 DolphinView::SortByName;
162 m_dolphinView->setSorting(tempSorting);
163 const Qt::SortOrder tempSortOrder = (sortOrder == Qt::Ascending) ?
164 Qt::Descending : Qt::Ascending;
165 m_dolphinView->setSortOrder(tempSortOrder);
166
167 // ... so that setting them again results in storing the new setting.
168 m_dolphinView->setSorting(sorting);
169 m_dolphinView->setSortOrder(sortOrder);
170 }
171
172 }
173
174 #include "dolphindetailsview.moc"