]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
Clicking on items in dolphin part finally implemented.
[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
32 #include <QApplication>
33 #include <QHeaderView>
34 #include <QRubberBand>
35 #include <QPainter>
36 #include <QScrollBar>
37
38 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
39 QTreeView(parent),
40 m_controller(controller),
41 m_dragging(false),
42 m_showElasticBand(false),
43 m_elasticBandOrigin(),
44 m_elasticBandDestination()
45 {
46 Q_ASSERT(controller != 0);
47
48 setAcceptDrops(true);
49 setRootIsDecorated(false);
50 setSortingEnabled(true);
51 setUniformRowHeights(true);
52 setSelectionBehavior(SelectItems);
53 setDragDropMode(QAbstractItemView::DragDrop);
54 setDropIndicatorShown(false);
55 setAlternatingRowColors(true);
56
57 setMouseTracking(true);
58 viewport()->setAttribute(Qt::WA_Hover);
59
60 const ViewProperties props(controller->url());
61 setSortIndicatorSection(props.sorting());
62 setSortIndicatorOrder(props.sortOrder());
63
64 connect(header(), SIGNAL(sectionClicked(int)),
65 this, SLOT(synchronizeSortingState(int)));
66
67 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
68 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
69 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
70 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
71
72 if (KGlobalSettings::singleClick()) {
73 connect(this, SIGNAL(clicked(const QModelIndex&)),
74 this, SLOT(slotItemActivated(const QModelIndex&)));
75 } else {
76 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
77 this, SLOT(slotItemActivated(const QModelIndex&)));
78 }
79 connect(this, SIGNAL(entered(const QModelIndex&)),
80 this, SLOT(slotEntered(const QModelIndex&)));
81 connect(this, SIGNAL(viewportEntered()),
82 controller, SLOT(emitViewportEntered()));
83 connect(controller, SIGNAL(zoomIn()),
84 this, SLOT(zoomIn()));
85 connect(controller, SIGNAL(zoomOut()),
86 this, SLOT(zoomOut()));
87
88 // apply the details mode settings to the widget
89 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
90 Q_ASSERT(settings != 0);
91
92 m_viewOptions = QTreeView::viewOptions();
93
94 QFont font(settings->fontFamily(), settings->fontSize());
95 font.setItalic(settings->italicFont());
96 font.setBold(settings->boldFont());
97 m_viewOptions.font = font;
98
99 updateDecorationSize();
100 }
101
102 DolphinDetailsView::~DolphinDetailsView()
103 {
104 }
105
106 bool DolphinDetailsView::event(QEvent* event)
107 {
108 if (event->type() == QEvent::Polish) {
109 // Assure that by respecting the available width that:
110 // - the 'Name' column is stretched as large as possible
111 // - the remaining columns are as small as possible
112 QHeaderView* headerView = header();
113 headerView->setStretchLastSection(false);
114 headerView->setResizeMode(QHeaderView::ResizeToContents);
115 headerView->setResizeMode(0, QHeaderView::Stretch);
116
117 // hide columns if this is indicated by the settings
118 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
119 Q_ASSERT(settings != 0);
120 if (!settings->showDate()) {
121 hideColumn(KDirModel::ModifiedTime);
122 }
123
124 if (!settings->showPermissions()) {
125 hideColumn(KDirModel::Permissions);
126 }
127
128 if (!settings->showOwner()) {
129 hideColumn(KDirModel::Owner);
130 }
131
132 if (!settings->showGroup()) {
133 hideColumn(KDirModel::Group);
134 }
135
136 if (!settings->showType()) {
137 hideColumn(KDirModel::Type);
138 }
139 }
140
141 return QTreeView::event(event);
142 }
143
144 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
145 {
146 return m_viewOptions;
147 }
148
149 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
150 {
151 QTreeView::contextMenuEvent(event);
152 m_controller->triggerContextMenuRequest(event->pos());
153 }
154
155 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
156 {
157 m_controller->triggerActivation();
158
159 QTreeView::mousePressEvent(event);
160
161 const QModelIndex index = indexAt(event->pos());
162 if (!index.isValid() || (index.column() != KDirModel::Name)) {
163 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
164 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
165 clearSelection();
166 }
167 }
168
169 if (event->button() == Qt::LeftButton) {
170 m_showElasticBand = true;
171
172 const QPoint pos(contentsPos());
173 m_elasticBandOrigin = event->pos();
174 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
175 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
176 m_elasticBandDestination = event->pos();
177 }
178 }
179
180 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
181 {
182 QTreeView::mouseMoveEvent(event);
183 if (m_showElasticBand) {
184 updateElasticBand();
185 }
186 }
187
188 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
189 {
190 QTreeView::mouseReleaseEvent(event);
191 if (m_showElasticBand) {
192 updateElasticBand();
193 m_showElasticBand = false;
194 }
195 }
196
197 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
198 {
199 if (event->mimeData()->hasUrls()) {
200 event->acceptProposedAction();
201 }
202
203 if (m_showElasticBand) {
204 updateElasticBand();
205 m_showElasticBand = false;
206 }
207 m_dragging = true;
208 }
209
210 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
211 {
212 QTreeView::dragLeaveEvent(event);
213
214 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
215 m_dragging = false;
216 setDirtyRegion(m_dropRect);
217 }
218
219 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
220 {
221 QTreeView::dragMoveEvent(event);
222
223 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
224 setDirtyRegion(m_dropRect);
225 const QModelIndex index = indexAt(event->pos());
226 if (!index.isValid() || (index.column() != KDirModel::Name)) {
227 m_dragging = false;
228 } else {
229 m_dragging = true;
230 m_dropRect = visualRect(index);
231 setDirtyRegion(m_dropRect);
232 }
233 }
234
235 void DolphinDetailsView::dropEvent(QDropEvent* event)
236 {
237 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
238 if (!urls.isEmpty()) {
239 event->acceptProposedAction();
240 m_controller->indicateDroppedUrls(urls,
241 indexAt(event->pos()),
242 event->source());
243 }
244 QTreeView::dropEvent(event);
245 m_dragging = false;
246 }
247
248 void DolphinDetailsView::paintEvent(QPaintEvent* event)
249 {
250 QTreeView::paintEvent(event);
251 if (m_showElasticBand) {
252 // The following code has been taken from QListView
253 // and adapted to DolphinDetailsView.
254 // (C) 1992-2007 Trolltech ASA
255 QStyleOptionRubberBand opt;
256 opt.initFrom(this);
257 opt.shape = QRubberBand::Rectangle;
258 opt.opaque = false;
259 opt.rect = elasticBandRect();
260
261 QPainter painter(viewport());
262 painter.save();
263 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
264 painter.restore();
265 }
266
267 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
268 if (m_dragging) {
269 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
270 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
271 }
272 }
273
274 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
275 {
276 QHeaderView* headerView = header();
277 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
278 }
279
280 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
281 {
282 QHeaderView* headerView = header();
283 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
284 }
285
286 void DolphinDetailsView::synchronizeSortingState(int column)
287 {
288 // The sorting has already been changed in QTreeView if this slot is
289 // invoked, but Dolphin is not informed about this.
290 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
291 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
292 m_controller->indicateSortingChange(sorting);
293 m_controller->indicateSortOrderChange(sortOrder);
294 }
295
296 void DolphinDetailsView::slotEntered(const QModelIndex& index)
297 {
298 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
299 const int nameColumnWidth = header()->sectionSize(KDirModel::Name);
300 if (pos.x() < nameColumnWidth) {
301 m_controller->emitItemEntered(index);
302 }
303 else {
304 m_controller->emitViewportEntered();
305 }
306 }
307
308 void DolphinDetailsView::updateElasticBand()
309 {
310 Q_ASSERT(m_showElasticBand);
311 QRect dirtyRegion(elasticBandRect());
312 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
313 dirtyRegion = dirtyRegion.united(elasticBandRect());
314 setDirtyRegion(dirtyRegion);
315 }
316
317 void DolphinDetailsView::zoomIn()
318 {
319 if (isZoomInPossible()) {
320 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
321 // TODO: get rid of K3Icon sizes
322 switch (settings->iconSize()) {
323 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
324 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
325 default: Q_ASSERT(false); break;
326 }
327 updateDecorationSize();
328 }
329 }
330
331 void DolphinDetailsView::zoomOut()
332 {
333 if (isZoomOutPossible()) {
334 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
335 // TODO: get rid of K3Icon sizes
336 switch (settings->iconSize()) {
337 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
338 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
339 default: Q_ASSERT(false); break;
340 }
341 updateDecorationSize();
342 }
343 }
344
345 bool DolphinDetailsView::isZoomInPossible() const
346 {
347 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
348 return settings->iconSize() < K3Icon::SizeLarge;
349 }
350
351 bool DolphinDetailsView::isZoomOutPossible() const
352 {
353 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
354 return settings->iconSize() > K3Icon::SizeSmall;
355 }
356
357 void DolphinDetailsView::updateDecorationSize()
358 {
359 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
360 const int iconSize = settings->iconSize();
361 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
362
363 m_controller->setZoomInPossible(isZoomInPossible());
364 m_controller->setZoomOutPossible(isZoomOutPossible());
365
366 doItemsLayout();
367 }
368
369 QPoint DolphinDetailsView::contentsPos() const
370 {
371 // implementation note: the horizonal position is ignored currently, as no
372 // horizontal scrolling is done anyway during a selection
373 const QScrollBar* scrollbar = verticalScrollBar();
374 Q_ASSERT(scrollbar != 0);
375
376 const int maxHeight = maximumViewportSize().height();
377 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
378 const int visibleHeight = model()->rowCount() + 1 - height;
379 if (visibleHeight <= 0) {
380 return QPoint(0, 0);
381 }
382
383 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
384 return QPoint(0, y);
385 }
386
387 QRect DolphinDetailsView::elasticBandRect() const
388 {
389 const QPoint pos(contentsPos());
390 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
391 return QRect(topLeft, m_elasticBandDestination).normalized();
392 }
393
394 static bool isValidNameIndex(const QModelIndex& index)
395 {
396 return index.isValid() && (index.column() == KDirModel::Name);
397 }
398
399 void DolphinDetailsView::slotItemActivated(const QModelIndex& index)
400 {
401 if (!isValidNameIndex(index)) {
402 clearSelection();
403 m_controller->emitItemEntered(index);
404 } else {
405 m_controller->triggerItem(index);
406 }
407 }
408
409 #include "dolphindetailsview.moc"