]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
* toggle selection when using CTRL modifier
[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 <QApplication>
34 #include <QPoint>
35
36 /**
37 * Represents one column inside the DolphinColumnView and has been
38 * extended to respect view options and hovering information.
39 */
40 class ColumnWidget : public QListView
41 {
42 public:
43 ColumnWidget(QWidget* parent,
44 DolphinColumnView* columnView,
45 const KUrl& url);
46 virtual ~ColumnWidget();
47
48 /** Sets the size of the icons. */
49 void setDecorationSize(const QSize& size);
50
51 /**
52 * An active column is defined as column, which shows the same URL
53 * as indicated by the URL navigator. The active column is usually
54 * drawn in a lighter color. All operations are applied to this column.
55 */
56 void setActive(bool active);
57 inline bool isActive() const;
58
59 inline const KUrl& url() const;
60
61 void obtainSelectionModel();
62 void releaseSelectionModel();
63
64 protected:
65 virtual QStyleOptionViewItem viewOptions() const;
66 virtual void dragEnterEvent(QDragEnterEvent* event);
67 virtual void dragLeaveEvent(QDragLeaveEvent* event);
68 virtual void dragMoveEvent(QDragMoveEvent* event);
69 virtual void dropEvent(QDropEvent* event);
70 virtual void mousePressEvent(QMouseEvent* event);
71 virtual void mouseMoveEvent(QMouseEvent* event);
72 virtual void mouseReleaseEvent(QMouseEvent* event);
73 virtual void paintEvent(QPaintEvent* event);
74 virtual void contextMenuEvent(QContextMenuEvent* event);
75
76 protected slots:
77 virtual void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
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 KUrl m_childUrl; // URL of the next column that is shown
92 QStyleOptionViewItem m_viewOptions;
93
94 bool m_dragging; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
95 QRect m_dropRect; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
96 };
97
98 ColumnWidget::ColumnWidget(QWidget* parent,
99 DolphinColumnView* columnView,
100 const KUrl& url) :
101 QListView(parent),
102 m_active(true),
103 m_swallowMouseMoveEvents(false),
104 m_view(columnView),
105 m_url(url),
106 m_childUrl(),
107 m_dragging(false),
108 m_dropRect()
109 {
110 setMouseTracking(true);
111 viewport()->setAttribute(Qt::WA_Hover);
112
113 // apply the column mode settings to the widget
114 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
115 Q_ASSERT(settings != 0);
116
117 m_viewOptions = QListView::viewOptions();
118
119 QFont font(settings->fontFamily(), settings->fontSize());
120 font.setItalic(settings->italicFont());
121 font.setBold(settings->boldFont());
122 m_viewOptions.font = font;
123
124 const int iconSize = settings->iconSize();
125 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
126
127 activate();
128 }
129
130 ColumnWidget::~ColumnWidget()
131 {
132 }
133
134 void ColumnWidget::setDecorationSize(const QSize& size)
135 {
136 m_viewOptions.decorationSize = size;
137 doItemsLayout();
138 }
139
140 void ColumnWidget::setActive(bool active)
141 {
142 if (active) {
143 obtainSelectionModel();
144 } else {
145 releaseSelectionModel();
146 }
147
148 if (m_active == active) {
149 return;
150 }
151
152 m_active = active;
153
154 if (active) {
155 activate();
156 } else {
157 deactivate();
158 }
159 }
160
161 inline bool ColumnWidget::isActive() const
162 {
163 return m_active;
164 }
165
166 const KUrl& ColumnWidget::url() const
167 {
168 return m_url;
169 }
170
171 void ColumnWidget::obtainSelectionModel()
172 {
173 if (selectionModel() != m_view->selectionModel()) {
174 selectionModel()->deleteLater();
175 setSelectionModel(m_view->selectionModel());
176 clearSelection();
177 }
178 }
179
180 void ColumnWidget::releaseSelectionModel()
181 {
182 if (selectionModel() == m_view->selectionModel()) {
183 QItemSelectionModel* replacementModel = new QItemSelectionModel(model());
184 setSelectionModel(replacementModel);
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 m_view->requestSelectionModel(this);
238
239 bool swallowMousePressEvent = false;
240 const QModelIndex index = indexAt(event->pos());
241 if (index.isValid()) {
242 // a click on an item has been done
243 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
244 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
245 const QModelIndex dirIndex = proxyModel->mapToSource(index);
246 KFileItem* item = dirModel->itemForIndex(dirIndex);
247 if (item != 0) {
248 QItemSelectionModel* selModel = selectionModel();
249
250 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
251 if (modifier & Qt::ControlModifier) {
252 m_view->requestActivation(this);
253 selModel->select(index, QItemSelectionModel::Toggle);
254 swallowMousePressEvent = true;
255 } else if (item->isDir()) {
256 m_childUrl = item->url();
257 viewport()->update();
258
259 // Only request the activation if not the left button is pressed.
260 // The left button on a directory opens a new column, hence requesting
261 // an activation is useless as the new column will request the activation
262 // afterwards.
263 if (event->button() != Qt::LeftButton) {
264 m_view->requestActivation(this);
265 }
266 } else {
267 m_view->requestActivation(this);
268 }
269
270 // TODO: check behavior with ShiftModifier
271 //if (modifier & Qt::ShiftModifier)
272
273 // TODO: is the assumption OK that Qt::RightButton always represents the context menu button?
274 if (event->button() == Qt::RightButton) {
275 swallowMousePressEvent = true;
276 if (!selModel->isSelected(index)) {
277 clearSelection();
278 }
279 selModel->select(index, QItemSelectionModel::Select);
280 }
281 }
282 } else {
283 // a click on the viewport has been done
284 m_view->requestActivation(this);
285
286 // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
287 // triggers an unwanted loading of directories on hovering folder items.
288 m_swallowMouseMoveEvents = true;
289 clearSelection();
290 }
291
292 if (!swallowMousePressEvent) {
293 QListView::mousePressEvent(event);
294 }
295 }
296
297 void ColumnWidget::mouseMoveEvent(QMouseEvent* event)
298 {
299 // see description in ColumnView::mousePressEvent()
300 if (!m_swallowMouseMoveEvents) {
301 QListView::mouseMoveEvent(event);
302 }
303 }
304
305 void ColumnWidget::mouseReleaseEvent(QMouseEvent* event)
306 {
307 QListView::mouseReleaseEvent(event);
308 m_swallowMouseMoveEvents = false;
309 }
310
311
312 void ColumnWidget::paintEvent(QPaintEvent* event)
313 {
314 if (!m_childUrl.isEmpty()) {
315 // indicate the shown URL of the next column by highlighting the shown folder item
316 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
317 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
318 const QModelIndex dirIndex = dirModel->indexForUrl(m_childUrl);
319 const QModelIndex proxyIndex = proxyModel->mapFromSource(dirIndex);
320 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
321 const QRect itemRect = visualRect(proxyIndex);
322 QPainter painter(viewport());
323 painter.save();
324
325 QColor color = KColorScheme(KColorScheme::View).foreground();
326 color.setAlpha(32);
327 painter.setPen(Qt::NoPen);
328 painter.setBrush(color);
329 painter.drawRect(itemRect);
330
331 painter.restore();
332 }
333 }
334
335 QListView::paintEvent(event);
336
337 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
338 if (m_dragging) {
339 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
340 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
341 }
342 }
343
344 void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
345 {
346 if (!m_active) {
347 m_view->requestActivation(this);
348 }
349
350 QListView::contextMenuEvent(event);
351
352 const QModelIndex index = indexAt(event->pos());
353 if (index.isValid() || m_active) {
354 // Only open a context menu above an item or if the mouse is above
355 // the active column.
356 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
357 m_view->m_controller->triggerContextMenuRequest(pos);
358 }
359 }
360
361 void ColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
362 {
363 // inactive views should not have any selection
364 if (!m_active) {
365 clearSelection();
366 }
367 QListView::selectionChanged(selected, deselected);
368 }
369
370 void ColumnWidget::activate()
371 {
372 const QColor bgColor = KColorScheme(KColorScheme::View).background();
373 QPalette palette = viewport()->palette();
374 palette.setColor(viewport()->backgroundRole(), bgColor);
375 viewport()->setPalette(palette);
376
377 update();
378 }
379
380 void ColumnWidget::deactivate()
381 {
382 QColor bgColor = KColorScheme(KColorScheme::View).background();
383 const QColor fgColor = KColorScheme(KColorScheme::View).foreground();
384 bgColor = KColorUtils::mix(bgColor, fgColor, 0.04);
385
386 QPalette palette = viewport()->palette();
387 palette.setColor(viewport()->backgroundRole(), bgColor);
388 viewport()->setPalette(palette);
389
390 update();
391 }
392
393 // ---
394
395 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
396 QColumnView(parent),
397 m_controller(controller)
398 {
399 Q_ASSERT(controller != 0);
400
401 setAcceptDrops(true);
402 setDragDropMode(QAbstractItemView::DragDrop);
403 setDropIndicatorShown(false);
404 setSelectionMode(ExtendedSelection);
405
406 if (KGlobalSettings::singleClick()) {
407 connect(this, SIGNAL(clicked(const QModelIndex&)),
408 this, SLOT(triggerItem(const QModelIndex&)));
409 } else {
410 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
411 this, SLOT(triggerItem(const QModelIndex&)));
412 }
413 connect(this, SIGNAL(entered(const QModelIndex&)),
414 controller, SLOT(emitItemEntered(const QModelIndex&)));
415 connect(this, SIGNAL(viewportEntered()),
416 controller, SLOT(emitViewportEntered()));
417 connect(controller, SIGNAL(zoomIn()),
418 this, SLOT(zoomIn()));
419 connect(controller, SIGNAL(zoomOut()),
420 this, SLOT(zoomOut()));
421 connect(controller, SIGNAL(urlChanged(const KUrl&)),
422 this, SLOT(updateColumnsState(const KUrl&)));
423
424 updateDecorationSize();
425 }
426
427 DolphinColumnView::~DolphinColumnView()
428 {
429 }
430
431 void DolphinColumnView::invertSelection()
432 {
433 selectActiveColumn(QItemSelectionModel::Toggle);
434 }
435
436 void DolphinColumnView::selectAll()
437 {
438 selectActiveColumn(QItemSelectionModel::Select);
439 }
440
441 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
442 {
443 // let the column widget be aware about its URL...
444 KUrl columnUrl;
445 if (viewport()->children().count() == 0) {
446 // For the first column widget the directory lister has not been started
447 // yet, hence use the URL from the controller instead.
448 columnUrl = m_controller->url();
449 } else {
450 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
451 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
452
453 const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
454 KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex);
455 if (fileItem != 0) {
456 columnUrl = fileItem->url();
457 }
458 }
459
460 ColumnWidget* view = new ColumnWidget(viewport(), this, columnUrl);
461
462 // The following code has been copied 1:1 from QColumnView::createColumn().
463 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
464 // QColumnView::initializeColumn() will be available for this.
465
466 view->setFrameShape(QFrame::NoFrame);
467 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
468 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
469 view->setMinimumWidth(100);
470 view->setAttribute(Qt::WA_MacShowFocusRect, false);
471
472 // copy the 'view' behavior
473 view->setDragDropMode(dragDropMode());
474 view->setDragDropOverwriteMode(dragDropOverwriteMode());
475 view->setDropIndicatorShown(showDropIndicator());
476 view->setAlternatingRowColors(alternatingRowColors());
477 view->setAutoScroll(hasAutoScroll());
478 view->setEditTriggers(editTriggers());
479 view->setHorizontalScrollMode(horizontalScrollMode());
480 view->setIconSize(iconSize());
481 view->setSelectionBehavior(selectionBehavior());
482 view->setSelectionMode(selectionMode());
483 view->setTabKeyNavigation(tabKeyNavigation());
484 view->setTextElideMode(textElideMode());
485 view->setVerticalScrollMode(verticalScrollMode());
486
487 view->setModel(model());
488
489 // set the delegate to be the columnview delegate
490 QAbstractItemDelegate* delegate = view->itemDelegate();
491 view->setItemDelegate(itemDelegate());
492 delete delegate;
493
494 view->setRootIndex(index);
495
496 if (model()->canFetchMore(index)) {
497 model()->fetchMore(index);
498 }
499
500 return view;
501 }
502
503 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
504 {
505 m_controller->triggerActivation();
506 QColumnView::mousePressEvent(event);
507 }
508
509 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
510 {
511 if (event->mimeData()->hasUrls()) {
512 event->acceptProposedAction();
513 }
514 }
515
516 void DolphinColumnView::dropEvent(QDropEvent* event)
517 {
518 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
519 if (!urls.isEmpty()) {
520 m_controller->indicateDroppedUrls(urls,
521 indexAt(event->pos()),
522 event->source());
523 event->acceptProposedAction();
524 }
525 QColumnView::dropEvent(event);
526 }
527
528 void DolphinColumnView::zoomIn()
529 {
530 if (isZoomInPossible()) {
531 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
532 // TODO: get rid of K3Icon sizes
533 switch (settings->iconSize()) {
534 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
535 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
536 default: Q_ASSERT(false); break;
537 }
538 updateDecorationSize();
539 }
540 }
541
542 void DolphinColumnView::zoomOut()
543 {
544 if (isZoomOutPossible()) {
545 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
546 // TODO: get rid of K3Icon sizes
547 switch (settings->iconSize()) {
548 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
549 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
550 default: Q_ASSERT(false); break;
551 }
552 updateDecorationSize();
553 }
554 }
555
556 void DolphinColumnView::triggerItem(const QModelIndex& index)
557 {
558 m_controller->triggerItem(index);
559 updateColumnsState(m_controller->url());
560 }
561
562 void DolphinColumnView::updateColumnsState(const KUrl& url)
563 {
564 foreach (QObject* object, viewport()->children()) {
565 if (object->inherits("QListView")) {
566 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
567 widget->setActive(widget->url() == url);
568 }
569 }
570 }
571
572
573 void DolphinColumnView::updateDecorationSize()
574 {
575 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
576 const int iconSize = settings->iconSize();
577
578 foreach (QObject* object, viewport()->children()) {
579 if (object->inherits("QListView")) {
580 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
581 widget->setDecorationSize(QSize(iconSize, iconSize));
582 }
583 }
584
585 m_controller->setZoomInPossible(isZoomInPossible());
586 m_controller->setZoomOutPossible(isZoomOutPossible());
587
588 doItemsLayout();
589 }
590
591 bool DolphinColumnView::isZoomInPossible() const
592 {
593 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
594 return settings->iconSize() < K3Icon::SizeLarge;
595 }
596
597 bool DolphinColumnView::isZoomOutPossible() const
598 {
599 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
600 return settings->iconSize() > K3Icon::SizeSmall;
601 }
602
603 void DolphinColumnView::requestActivation(QWidget* column)
604 {
605 foreach (QObject* object, viewport()->children()) {
606 if (object->inherits("QListView")) {
607 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
608 const bool isActive = (widget == column);
609 widget->setActive(isActive);
610 if (isActive) {
611 m_controller->setUrl(widget->url());
612 }
613 }
614 }
615 }
616
617 void DolphinColumnView::requestSelectionModel(QAbstractItemView* view)
618 {
619 foreach (QObject* object, viewport()->children()) {
620 if (object->inherits("QListView")) {
621 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
622 if (widget == view) {
623 widget->obtainSelectionModel();
624 } else {
625 widget->releaseSelectionModel();
626 }
627 }
628 }
629 }
630
631 void DolphinColumnView::selectActiveColumn(QItemSelectionModel::SelectionFlags flags)
632 {
633 // TODO: this approach of selecting the active column is very slow. It should be
634 // possible to speedup the implementation by using QItemSelection, but all adempts
635 // have failed yet...
636
637 // assure that the selection model of the active column is set properly, otherwise
638 // no visual update of the selections is done
639 const KUrl& activeUrl = m_controller->url();
640 foreach (QObject* object, viewport()->children()) {
641 if (object->inherits("QListView")) {
642 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
643 if (widget->url() == activeUrl) {
644 widget->obtainSelectionModel();
645 } else {
646 widget->releaseSelectionModel();
647 }
648 }
649 }
650
651 QItemSelectionModel* selModel = selectionModel();
652
653 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
654 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
655 KDirLister* dirLister = dirModel->dirLister();
656
657 const KFileItemList list = dirLister->itemsForDir(activeUrl);
658 foreach (KFileItem* item, list) {
659 const QModelIndex index = dirModel->indexForUrl(item->url());
660 selModel->select(proxyModel->mapFromSource(index), flags);
661 }
662 }
663
664 #include "dolphincolumnview.moc"