]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
support return key for navigation (thanks to Luca Gugelmann for the patch!)
[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 "dolphincontroller.h"
24 #include "dolphinsettings.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "viewproperties.h"
27
28 #include "dolphin_detailsmodesettings.h"
29
30 #include <kdirmodel.h>
31 #include <kfileitemdelegate.h>
32
33 #include <QHeaderView>
34
35 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
36 QTreeView(parent),
37 m_controller(controller)
38 {
39 Q_ASSERT(controller != 0);
40
41 setAcceptDrops(true);
42 setRootIsDecorated(false);
43 setSortingEnabled(true);
44 setUniformRowHeights(true);
45 setSelectionBehavior(SelectItems);
46
47 viewport()->setAttribute(Qt::WA_Hover);
48
49 const ViewProperties props(controller->url());
50 setSortIndicatorSection(props.sorting());
51 setSortIndicatorOrder(props.sortOrder());
52
53 connect(header(), SIGNAL(sectionClicked(int)),
54 this, SLOT(synchronizeSortingState(int)));
55
56 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
57 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
58 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
59 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
60
61 connect(this, SIGNAL(clicked(const QModelIndex&)),
62 controller, SLOT(triggerItem(const QModelIndex&)));
63 connect(this, SIGNAL(activated(const QModelIndex&)),
64 controller, SLOT(triggerItem(const QModelIndex&)));
65
66 connect(controller, SIGNAL(zoomIn()),
67 this, SLOT(zoomIn()));
68 connect(controller, SIGNAL(zoomOut()),
69 this, SLOT(zoomOut()));
70
71 // apply the details mode settings to the widget
72 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
73 Q_ASSERT(settings != 0);
74
75 m_viewOptions = QTreeView::viewOptions();
76 m_viewOptions.font = QFont(settings->fontFamily(), settings->fontSize());
77 updateDecorationSize();
78
79 KFileItemDelegate* delegate = new KFileItemDelegate(parent);
80 setItemDelegate(delegate);
81 }
82
83 DolphinDetailsView::~DolphinDetailsView()
84 {
85 }
86
87 bool DolphinDetailsView::event(QEvent* event)
88 {
89 if (event->type() == QEvent::Polish) {
90 // Assure that by respecting the available width that:
91 // - the 'Name' column is stretched as large as possible
92 // - the remaining columns are as small as possible
93 QHeaderView* headerView = header();
94 headerView->setStretchLastSection(false);
95 headerView->setResizeMode(QHeaderView::ResizeToContents);
96 headerView->setResizeMode(0, QHeaderView::Stretch);
97
98 // hide columns if this is indicated by the settings
99 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
100 Q_ASSERT(settings != 0);
101 if (!settings->showDate()) {
102 hideColumn(KDirModel::ModifiedTime);
103 }
104
105 if (!settings->showPermissions()) {
106 hideColumn(KDirModel::Permissions);
107 }
108
109 if (!settings->showOwner()) {
110 hideColumn(KDirModel::Owner);
111 }
112
113 if (!settings->showGroup()) {
114 hideColumn(KDirModel::Group);
115 }
116 }
117
118 return QTreeView::event(event);
119 }
120 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
121 {
122 return m_viewOptions;
123 }
124
125 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
126 {
127 QTreeView::contextMenuEvent(event);
128 m_controller->triggerContextMenuRequest(event->pos());
129 }
130
131 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
132 {
133 QTreeView::mouseReleaseEvent(event);
134 m_controller->triggerActivation();
135 }
136
137 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
138 {
139 if (event->mimeData()->hasUrls()) {
140 event->acceptProposedAction();
141 }
142 }
143
144 void DolphinDetailsView::dropEvent(QDropEvent* event)
145 {
146 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
147 if (urls.isEmpty() || (event->source() == this)) {
148 QTreeView::dropEvent(event);
149 }
150 else {
151 event->acceptProposedAction();
152 m_controller->indicateDroppedUrls(urls, event->pos());
153 }
154 }
155
156 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
157 {
158 QHeaderView* headerView = header();
159 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
160 }
161
162 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
163 {
164 QHeaderView* headerView = header();
165 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
166 }
167
168 void DolphinDetailsView::synchronizeSortingState(int column)
169 {
170 // The sorting has already been changed in QTreeView if this slot is
171 // invoked, but Dolphin is not informed about this.
172 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
173 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
174 m_controller->indicateSortingChange(sorting);
175 m_controller->indicateSortOrderChange(sortOrder);
176 }
177
178 void DolphinDetailsView::zoomIn()
179 {
180 if (isZoomInPossible()) {
181 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
182 // TODO: get rid of K3Icon sizes
183 switch (settings->iconSize()) {
184 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
185 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
186 default: Q_ASSERT(false); break;
187 }
188 updateDecorationSize();
189 }
190 }
191
192 void DolphinDetailsView::zoomOut()
193 {
194 if (isZoomOutPossible()) {
195 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
196 // TODO: get rid of K3Icon sizes
197 switch (settings->iconSize()) {
198 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
199 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
200 default: Q_ASSERT(false); break;
201 }
202 updateDecorationSize();
203 }
204 }
205
206 bool DolphinDetailsView::isZoomInPossible() const
207 {
208 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
209 return settings->iconSize() < K3Icon::SizeLarge;
210 }
211
212 bool DolphinDetailsView::isZoomOutPossible() const
213 {
214 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
215 return settings->iconSize() > K3Icon::SizeSmall;
216 }
217
218 void DolphinDetailsView::updateDecorationSize()
219 {
220 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
221 const int iconSize = settings->iconSize();
222 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
223
224 m_controller->setZoomInPossible(isZoomInPossible());
225 m_controller->setZoomOutPossible(isZoomOutPossible());
226
227 doItemsLayout();
228 }
229
230 #include "dolphindetailsview.moc"