]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
hover fix for the details mode: hovering should only be done on the name column,...
[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 <QtGui/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 setDragDropMode(QAbstractItemView::DragDrop);
47 setDropIndicatorShown(false);
48
49 setMouseTracking(true);
50 viewport()->setAttribute(Qt::WA_Hover);
51
52 const ViewProperties props(controller->url());
53 setSortIndicatorSection(props.sorting());
54 setSortIndicatorOrder(props.sortOrder());
55
56 connect(header(), SIGNAL(sectionClicked(int)),
57 this, SLOT(synchronizeSortingState(int)));
58
59 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
60 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
61 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
62 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
63
64 if (KGlobalSettings::singleClick()) {
65 connect(this, SIGNAL(clicked(const QModelIndex&)),
66 controller, SLOT(triggerItem(const QModelIndex&)));
67 } else {
68 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
69 controller, SLOT(triggerItem(const QModelIndex&)));
70 }
71 connect(this, SIGNAL(activated(const QModelIndex&)),
72 controller, SLOT(triggerItem(const QModelIndex&)));
73 connect(this, SIGNAL(entered(const QModelIndex&)),
74 this, SLOT(slotEntered(const QModelIndex&)));
75 connect(this, SIGNAL(viewportEntered()),
76 controller, SLOT(emitViewportEntered()));
77 connect(controller, SIGNAL(zoomIn()),
78 this, SLOT(zoomIn()));
79 connect(controller, SIGNAL(zoomOut()),
80 this, SLOT(zoomOut()));
81
82 // apply the details mode settings to the widget
83 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
84 Q_ASSERT(settings != 0);
85
86 m_viewOptions = QTreeView::viewOptions();
87
88 QFont font(settings->fontFamily(), settings->fontSize());
89 font.setItalic(settings->italicFont());
90 font.setBold(settings->boldFont());
91 m_viewOptions.font = font;
92
93 updateDecorationSize();
94 }
95
96 DolphinDetailsView::~DolphinDetailsView()
97 {}
98
99 bool DolphinDetailsView::event(QEvent* event)
100 {
101 if (event->type() == QEvent::Polish) {
102 // Assure that by respecting the available width that:
103 // - the 'Name' column is stretched as large as possible
104 // - the remaining columns are as small as possible
105 QHeaderView* headerView = header();
106 headerView->setStretchLastSection(false);
107 headerView->setResizeMode(QHeaderView::ResizeToContents);
108 headerView->setResizeMode(0, QHeaderView::Stretch);
109
110 // hide columns if this is indicated by the settings
111 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
112 Q_ASSERT(settings != 0);
113 if (!settings->showDate()) {
114 hideColumn(KDirModel::ModifiedTime);
115 }
116
117 if (!settings->showPermissions()) {
118 hideColumn(KDirModel::Permissions);
119 }
120
121 if (!settings->showOwner()) {
122 hideColumn(KDirModel::Owner);
123 }
124
125 if (!settings->showGroup()) {
126 hideColumn(KDirModel::Group);
127 }
128
129 if (!settings->showType()) {
130 hideColumn(KDirModel::Type);
131 }
132 }
133
134 return QTreeView::event(event);
135 }
136
137 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
138 {
139 return m_viewOptions;
140 }
141
142 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
143 {
144 QTreeView::contextMenuEvent(event);
145 m_controller->triggerContextMenuRequest(event->pos());
146 }
147
148 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
149 {
150 QTreeView::mouseReleaseEvent(event);
151 m_controller->triggerActivation();
152 }
153
154 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
155 {
156 if (event->mimeData()->hasUrls()) {
157 event->acceptProposedAction();
158 }
159 }
160
161 void DolphinDetailsView::dropEvent(QDropEvent* event)
162 {
163 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
164 if (!urls.isEmpty()) {
165 event->acceptProposedAction();
166 m_controller->indicateDroppedUrls(urls,
167 indexAt(event->pos()),
168 event->source());
169 }
170 QTreeView::dropEvent(event);
171 }
172
173 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
174 {
175 QHeaderView* headerView = header();
176 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
177 }
178
179 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
180 {
181 QHeaderView* headerView = header();
182 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
183 }
184
185 void DolphinDetailsView::synchronizeSortingState(int column)
186 {
187 // The sorting has already been changed in QTreeView if this slot is
188 // invoked, but Dolphin is not informed about this.
189 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
190 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
191 m_controller->indicateSortingChange(sorting);
192 m_controller->indicateSortOrderChange(sortOrder);
193 }
194
195 void DolphinDetailsView::slotEntered(const QModelIndex& index)
196 {
197 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
198 const int nameColumnWidth = header()->sectionSize(KDirModel::Name);
199 if (pos.x() < nameColumnWidth) {
200 m_controller->emitItemEntered(index);
201 }
202 else {
203 m_controller->emitViewportEntered();
204 }
205 }
206
207 void DolphinDetailsView::zoomIn()
208 {
209 if (isZoomInPossible()) {
210 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
211 // TODO: get rid of K3Icon sizes
212 switch (settings->iconSize()) {
213 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
214 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
215 default: Q_ASSERT(false); break;
216 }
217 updateDecorationSize();
218 }
219 }
220
221 void DolphinDetailsView::zoomOut()
222 {
223 if (isZoomOutPossible()) {
224 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
225 // TODO: get rid of K3Icon sizes
226 switch (settings->iconSize()) {
227 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
228 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
229 default: Q_ASSERT(false); break;
230 }
231 updateDecorationSize();
232 }
233 }
234
235 bool DolphinDetailsView::isZoomInPossible() const
236 {
237 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
238 return settings->iconSize() < K3Icon::SizeLarge;
239 }
240
241 bool DolphinDetailsView::isZoomOutPossible() const
242 {
243 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
244 return settings->iconSize() > K3Icon::SizeSmall;
245 }
246
247 void DolphinDetailsView::updateDecorationSize()
248 {
249 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
250 const int iconSize = settings->iconSize();
251 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
252
253 m_controller->setZoomInPossible(isZoomInPossible());
254 m_controller->setZoomOutPossible(isZoomOutPossible());
255
256 doItemsLayout();
257 }
258
259 #include "dolphindetailsview.moc"