]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
don't load directories when hovering folders during moving the mouse...
[dolphin.git] / src / dolphincolumnview.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphincolumnview.h"
21
22 #include "dolphincontroller.h"
23 #include "dolphinsettings.h"
24
25 #include "dolphin_columnmodesettings.h"
26
27 #include <kcolorutils.h>
28 #include <kcolorscheme.h>
29 #include <kdirlister.h>
30 #include <kdirmodel.h>
31
32 #include <QAbstractProxyModel>
33 #include <QPoint>
34
35 /**
36 * Represents one column inside the DolphinColumnView and has been
37 * extended to respect view options and hovering information.
38 */
39 class ColumnWidget : public QListView
40 {
41 public:
42 ColumnWidget(QWidget* parent,
43 DolphinColumnView* columnView,
44 const KUrl& url);
45 virtual ~ColumnWidget();
46
47 /** Sets the size of the icons. */
48 void setDecorationSize(const QSize& size);
49
50 /**
51 * An active column is defined as column, which shows the same URL
52 * as indicated by the URL navigator. The active column is usually
53 * drawn in a lighter color. All operations are applied to this column.
54 */
55 void setActive(bool active);
56 inline bool isActive() const;
57
58 inline const KUrl& url() const;
59
60 /**
61 * Updates the selection that the folder gets selected which represents
62 * the URL \a url. If \a url is empty, the selection of the column widget
63 * gets cleared.
64 */
65 void updateSelection(const KUrl& url);
66
67 protected:
68 virtual QStyleOptionViewItem viewOptions() const;
69 virtual void dragEnterEvent(QDragEnterEvent* event);
70 virtual void dragLeaveEvent(QDragLeaveEvent* event);
71 virtual void dragMoveEvent(QDragMoveEvent* event);
72 virtual void dropEvent(QDropEvent* event);
73 virtual void mousePressEvent(QMouseEvent* event);
74 virtual void mouseMoveEvent(QMouseEvent* event);
75 virtual void mouseReleaseEvent(QMouseEvent* event);
76 virtual void paintEvent(QPaintEvent* event);
77 virtual void contextMenuEvent(QContextMenuEvent* event);
78
79 private:
80 /** Used by ColumnWidget::setActive(). */
81 void activate();
82
83 /** Used by ColumnWidget::setActive(). */
84 void deactivate();
85
86 private:
87 bool m_active;
88 bool m_swallowMouseMoveEvents;;
89 DolphinColumnView* m_view;
90 KUrl m_url;
91 QStyleOptionViewItem m_viewOptions;
92
93 bool m_dragging; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
94 QRect m_dropRect; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
95 };
96
97 ColumnWidget::ColumnWidget(QWidget* parent,
98 DolphinColumnView* columnView,
99 const KUrl& url) :
100 QListView(parent),
101 m_active(true),
102 m_swallowMouseMoveEvents(false),
103 m_view(columnView),
104 m_url(url),
105 m_dragging(false),
106 m_dropRect()
107 {
108 setMouseTracking(true);
109 viewport()->setAttribute(Qt::WA_Hover);
110
111 // apply the column mode settings to the widget
112 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
113 Q_ASSERT(settings != 0);
114
115 m_viewOptions = QListView::viewOptions();
116
117 QFont font(settings->fontFamily(), settings->fontSize());
118 font.setItalic(settings->italicFont());
119 font.setBold(settings->boldFont());
120 m_viewOptions.font = font;
121
122 const int iconSize = settings->iconSize();
123 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
124
125 activate();
126 }
127
128 ColumnWidget::~ColumnWidget()
129 {
130 }
131
132 void ColumnWidget::setDecorationSize(const QSize& size)
133 {
134 m_viewOptions.decorationSize = size;
135 doItemsLayout();
136 }
137
138 void ColumnWidget::setActive(bool active)
139 {
140 if (m_active == active) {
141 return;
142 }
143
144 m_active = active;
145
146 if (active) {
147 activate();
148 } else {
149 deactivate();
150 }
151 }
152
153 inline bool ColumnWidget::isActive() const
154 {
155 return m_active;
156 }
157
158 const KUrl& ColumnWidget::url() const
159 {
160 return m_url;
161 }
162
163 void ColumnWidget::updateSelection(const KUrl& url)
164 {
165 setSelectionMode(SingleSelection);
166 QItemSelectionModel* selModel = selectionModel();
167 if (url.isEmpty()) {
168 selModel->clear();
169 return;
170 }
171
172 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
173 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
174 const QModelIndex dirIndex = dirModel->indexForUrl(url);
175 const QModelIndex proxyIndex = proxyModel->mapFromSource(dirIndex);
176
177 const QItemSelection selection = selModel->selection();
178 const bool isIndexSelected = selModel->isSelected(proxyIndex);
179
180 if (!m_active && ((selection.count() > 1) || !isIndexSelected)) {
181 selModel->clear();
182 }
183 if (!isIndexSelected) {
184 selModel->select(proxyIndex, QItemSelectionModel::Select);
185 }
186 }
187
188 QStyleOptionViewItem ColumnWidget::viewOptions() const
189 {
190 return m_viewOptions;
191 }
192
193 void ColumnWidget::dragEnterEvent(QDragEnterEvent* event)
194 {
195 if (event->mimeData()->hasUrls()) {
196 event->acceptProposedAction();
197 }
198
199 m_dragging = true;
200 }
201
202 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
203 {
204 QListView::dragLeaveEvent(event);
205
206 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
207 m_dragging = false;
208 setDirtyRegion(m_dropRect);
209 }
210
211 void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
212 {
213 QListView::dragMoveEvent(event);
214
215 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
216 const QModelIndex index = indexAt(event->pos());
217 setDirtyRegion(m_dropRect);
218 m_dropRect = visualRect(index);
219 setDirtyRegion(m_dropRect);
220 }
221
222 void ColumnWidget::dropEvent(QDropEvent* event)
223 {
224 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
225 if (!urls.isEmpty()) {
226 event->acceptProposedAction();
227 m_view->m_controller->indicateDroppedUrls(urls,
228 indexAt(event->pos()),
229 event->source());
230 }
231 QListView::dropEvent(event);
232 m_dragging = false;
233 }
234
235 void ColumnWidget::mousePressEvent(QMouseEvent* event)
236 {
237 if (m_active) {
238 selectionModel()->clear();
239 }
240
241 QListView::mousePressEvent(event);
242
243 const QModelIndex index = indexAt(event->pos());
244
245 bool requestActivation = false;
246 if (index.isValid()) {
247 // A click on an item has been done. Only request an activation
248 // if the item is not a directory.
249 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
250 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
251 const QModelIndex dirIndex = proxyModel->mapToSource(index);
252 KFileItem* item = dirModel->itemForIndex(dirIndex);
253 requestActivation = (item != 0) && !item->isDir();
254 } else {
255 // a click on the viewport has been done
256 requestActivation = true;
257
258 // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
259 // triggers an unwanted loading of directories on hovering folder items.
260 m_swallowMouseMoveEvents = true;
261 }
262
263 if (requestActivation) {
264 m_view->requestActivation(this);
265 } else {
266 m_view->updateSelections();
267 }
268 }
269
270 void ColumnWidget::mouseMoveEvent(QMouseEvent* event)
271 {
272 // see description in ColumnView::mousePressEvent()
273 if (!m_swallowMouseMoveEvents) {
274 QListView::mouseMoveEvent(event);
275 }
276 }
277
278 void ColumnWidget::mouseReleaseEvent(QMouseEvent* event)
279 {
280 QListView::mouseReleaseEvent(event);
281 m_swallowMouseMoveEvents = false;
282 }
283
284
285 void ColumnWidget::paintEvent(QPaintEvent* event)
286 {
287 QListView::paintEvent(event);
288
289 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
290 if (m_dragging) {
291 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
292 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
293 }
294 }
295
296 void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
297 {
298 if (!m_active) {
299 m_view->requestActivation(this);
300 }
301
302 QListView::contextMenuEvent(event);
303
304 const QModelIndex index = indexAt(event->pos());
305 if (index.isValid() || m_active) {
306 // Only open a context menu above an item or if the mouse is above
307 // the active column.
308 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
309 m_view->m_controller->triggerContextMenuRequest(pos);
310 }
311 }
312
313 void ColumnWidget::activate()
314 {
315 const QColor bgColor = KColorScheme(KColorScheme::View).background();
316 QPalette palette = viewport()->palette();
317 palette.setColor(viewport()->backgroundRole(), bgColor);
318 viewport()->setPalette(palette);
319
320 update();
321 }
322
323 void ColumnWidget::deactivate()
324 {
325 QColor bgColor = KColorScheme(KColorScheme::View).background();
326 const QColor fgColor = KColorScheme(KColorScheme::View).foreground();
327 bgColor = KColorUtils::mix(bgColor, fgColor, 0.04);
328
329 QPalette palette = viewport()->palette();
330 palette.setColor(viewport()->backgroundRole(), bgColor);
331 viewport()->setPalette(palette);
332
333 update();
334 }
335
336 // ---
337
338 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
339 QColumnView(parent),
340 m_controller(controller)
341 {
342 Q_ASSERT(controller != 0);
343
344 setAcceptDrops(true);
345 setDragDropMode(QAbstractItemView::DragDrop);
346 setDropIndicatorShown(false);
347 setSelectionMode(SingleSelection);
348
349 if (KGlobalSettings::singleClick()) {
350 connect(this, SIGNAL(clicked(const QModelIndex&)),
351 this, SLOT(triggerItem(const QModelIndex&)));
352 } else {
353 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
354 this, SLOT(triggerItem(const QModelIndex&)));
355 }
356 connect(this, SIGNAL(entered(const QModelIndex&)),
357 controller, SLOT(emitItemEntered(const QModelIndex&)));
358 connect(this, SIGNAL(viewportEntered()),
359 controller, SLOT(emitViewportEntered()));
360 connect(controller, SIGNAL(zoomIn()),
361 this, SLOT(zoomIn()));
362 connect(controller, SIGNAL(zoomOut()),
363 this, SLOT(zoomOut()));
364 connect(controller, SIGNAL(urlChanged(const KUrl&)),
365 this, SLOT(updateColumnsState(const KUrl&)));
366
367 updateDecorationSize();
368 }
369
370 DolphinColumnView::~DolphinColumnView()
371 {
372 }
373
374 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
375 {
376 // let the column widget be aware about its URL...
377 KUrl columnUrl;
378 if (viewport()->children().count() == 0) {
379 // For the first column widget the directory lister has not been started
380 // yet, hence use the URL from the controller instead.
381 columnUrl = m_controller->url();
382 } else {
383 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
384 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
385
386 const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
387 KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex);
388 if (fileItem != 0) {
389 columnUrl = fileItem->url();
390 }
391 }
392
393 ColumnWidget* view = new ColumnWidget(viewport(), this, columnUrl);
394
395 // The following code has been copied 1:1 from QColumnView::createColumn().
396 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
397 // QColumnView::initializeColumn() will be available for this.
398
399 view->setFrameShape(QFrame::NoFrame);
400 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
401 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
402 view->setMinimumWidth(100);
403 view->setAttribute(Qt::WA_MacShowFocusRect, false);
404
405 // copy the 'view' behavior
406 view->setDragDropMode(dragDropMode());
407 view->setDragDropOverwriteMode(dragDropOverwriteMode());
408 view->setDropIndicatorShown(showDropIndicator());
409 view->setAlternatingRowColors(alternatingRowColors());
410 view->setAutoScroll(hasAutoScroll());
411 view->setEditTriggers(editTriggers());
412 view->setHorizontalScrollMode(horizontalScrollMode());
413 view->setIconSize(iconSize());
414 view->setSelectionBehavior(selectionBehavior());
415 view->setSelectionMode(selectionMode());
416 view->setTabKeyNavigation(tabKeyNavigation());
417 view->setTextElideMode(textElideMode());
418 view->setVerticalScrollMode(verticalScrollMode());
419
420 view->setModel(model());
421
422 // set the delegate to be the columnview delegate
423 QAbstractItemDelegate* delegate = view->itemDelegate();
424 view->setItemDelegate(itemDelegate());
425 delete delegate;
426
427 view->setRootIndex(index);
428
429 if (model()->canFetchMore(index)) {
430 model()->fetchMore(index);
431 }
432
433 return view;
434 }
435
436 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
437 {
438 m_controller->triggerActivation();
439 QColumnView::mousePressEvent(event);
440 }
441
442 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
443 {
444 if (event->mimeData()->hasUrls()) {
445 event->acceptProposedAction();
446 }
447 }
448
449 void DolphinColumnView::dropEvent(QDropEvent* event)
450 {
451 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
452 if (!urls.isEmpty()) {
453 m_controller->indicateDroppedUrls(urls,
454 indexAt(event->pos()),
455 event->source());
456 event->acceptProposedAction();
457 }
458 QColumnView::dropEvent(event);
459 }
460
461 void DolphinColumnView::showEvent(QShowEvent* event)
462 {
463 QColumnView::showEvent(event);
464 if (!event->spontaneous()) {
465 // QColumnView might clear the selection for folders that are shown in the next column.
466 // As this is not wanted the selection is updated if the directory lister has been completed.
467 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
468 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
469 KDirLister* dirLister = dirModel->dirLister();
470 connect(dirLister, SIGNAL(completed()),
471 this, SLOT(updateSelections()));
472 }
473 }
474
475 void DolphinColumnView::zoomIn()
476 {
477 if (isZoomInPossible()) {
478 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
479 // TODO: get rid of K3Icon sizes
480 switch (settings->iconSize()) {
481 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
482 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
483 default: Q_ASSERT(false); break;
484 }
485 updateDecorationSize();
486 }
487 }
488
489 void DolphinColumnView::zoomOut()
490 {
491 if (isZoomOutPossible()) {
492 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
493 // TODO: get rid of K3Icon sizes
494 switch (settings->iconSize()) {
495 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
496 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
497 default: Q_ASSERT(false); break;
498 }
499 updateDecorationSize();
500 }
501 }
502
503 void DolphinColumnView::triggerItem(const QModelIndex& index)
504 {
505 m_controller->triggerItem(index);
506 updateColumnsState(m_controller->url());
507 }
508
509 void DolphinColumnView::updateColumnsState(const KUrl& url)
510 {
511 foreach (QObject* object, viewport()->children()) {
512 if (object->inherits("QListView")) {
513 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
514 widget->setActive(widget->url() == url);
515 }
516 }
517 }
518
519
520 void DolphinColumnView::updateDecorationSize()
521 {
522 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
523 const int iconSize = settings->iconSize();
524
525 foreach (QObject* object, viewport()->children()) {
526 if (object->inherits("QListView")) {
527 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
528 widget->setDecorationSize(QSize(iconSize, iconSize));
529 }
530 }
531
532 m_controller->setZoomInPossible(isZoomInPossible());
533 m_controller->setZoomOutPossible(isZoomOutPossible());
534
535 doItemsLayout();
536 }
537
538 void DolphinColumnView::updateSelections()
539 {
540 ColumnWidget* previousWidget = 0;
541 foreach (QObject* object, viewport()->children()) {
542 if (object->inherits("QListView")) {
543 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
544 if (previousWidget != 0) {
545 previousWidget->updateSelection(widget->url());
546 }
547 previousWidget = widget;
548 }
549 }
550 if (previousWidget != 0) {
551 previousWidget->updateSelection(KUrl());
552 }
553 }
554
555 bool DolphinColumnView::isZoomInPossible() const
556 {
557 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
558 return settings->iconSize() < K3Icon::SizeLarge;
559 }
560
561 bool DolphinColumnView::isZoomOutPossible() const
562 {
563 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
564 return settings->iconSize() > K3Icon::SizeSmall;
565 }
566
567 void DolphinColumnView::requestActivation(QWidget* column)
568 {
569 foreach (QObject* object, viewport()->children()) {
570 if (object->inherits("QListView")) {
571 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
572 const bool isActive = (widget == column);
573 widget->setActive(isActive);
574 if (isActive) {
575 m_controller->setUrl(widget->url());
576 }
577 }
578 }
579 updateSelections();
580 }
581
582 #include "dolphincolumnview.moc"