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