]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
fix some drag & drop issues
[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 if (!m_active) {
169 selModel->clear();
170 }
171 } else {
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
189 QStyleOptionViewItem ColumnWidget::viewOptions() const
190 {
191 return m_viewOptions;
192 }
193
194 void ColumnWidget::dragEnterEvent(QDragEnterEvent* event)
195 {
196 if (event->mimeData()->hasUrls()) {
197 event->acceptProposedAction();
198 }
199
200 m_dragging = true;
201 }
202
203 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
204 {
205 QListView::dragLeaveEvent(event);
206
207 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
208 m_dragging = false;
209 setDirtyRegion(m_dropRect);
210 }
211
212 void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
213 {
214 QListView::dragMoveEvent(event);
215
216 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
217 const QModelIndex index = indexAt(event->pos());
218 setDirtyRegion(m_dropRect);
219 m_dropRect = visualRect(index);
220 setDirtyRegion(m_dropRect);
221 }
222
223 void ColumnWidget::dropEvent(QDropEvent* event)
224 {
225 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
226 if (!urls.isEmpty()) {
227 event->acceptProposedAction();
228 m_view->m_controller->indicateDroppedUrls(urls,
229 indexAt(event->pos()),
230 event->source());
231 }
232 QListView::dropEvent(event);
233 m_dragging = false;
234 }
235
236 void ColumnWidget::mousePressEvent(QMouseEvent* event)
237 {
238 QListView::mousePressEvent(event);
239
240 const QModelIndex index = indexAt(event->pos());
241
242 bool requestActivation = false;
243 if (index.isValid()) {
244 // A click on an item has been done. Only request an activation
245 // if the item is not a directory.
246 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
247 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
248 const QModelIndex dirIndex = proxyModel->mapToSource(index);
249 KFileItem* item = dirModel->itemForIndex(dirIndex);
250 requestActivation = (item != 0) && !item->isDir();
251 } else {
252 // a click on the viewport has been done
253 requestActivation = true;
254
255 // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
256 // triggers an unwanted loading of directories on hovering folder items.
257 m_swallowMouseMoveEvents = true;
258 }
259
260 if (requestActivation) {
261 m_view->requestActivation(this);
262 } else {
263 m_view->updateSelections();
264 }
265 }
266
267 void ColumnWidget::mouseMoveEvent(QMouseEvent* event)
268 {
269 // see description in ColumnView::mousePressEvent()
270 if (!m_swallowMouseMoveEvents) {
271 QListView::mouseMoveEvent(event);
272 }
273 }
274
275 void ColumnWidget::mouseReleaseEvent(QMouseEvent* event)
276 {
277 QListView::mouseReleaseEvent(event);
278 m_swallowMouseMoveEvents = false;
279 }
280
281
282 void ColumnWidget::paintEvent(QPaintEvent* event)
283 {
284 QListView::paintEvent(event);
285
286 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
287 if (m_dragging) {
288 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
289 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
290 }
291 }
292
293 void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
294 {
295 if (!m_active) {
296 m_view->requestActivation(this);
297 }
298
299 QListView::contextMenuEvent(event);
300
301 const QModelIndex index = indexAt(event->pos());
302 if (index.isValid() || m_active) {
303 // Only open a context menu above an item or if the mouse is above
304 // the active column.
305 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
306 m_view->m_controller->triggerContextMenuRequest(pos);
307 }
308 }
309
310 void ColumnWidget::activate()
311 {
312 const QColor bgColor = KColorScheme(KColorScheme::View).background();
313 QPalette palette = viewport()->palette();
314 palette.setColor(viewport()->backgroundRole(), bgColor);
315 viewport()->setPalette(palette);
316
317 update();
318 }
319
320 void ColumnWidget::deactivate()
321 {
322 QColor bgColor = KColorScheme(KColorScheme::View).background();
323 const QColor fgColor = KColorScheme(KColorScheme::View).foreground();
324 bgColor = KColorUtils::mix(bgColor, fgColor, 0.04);
325
326 QPalette palette = viewport()->palette();
327 palette.setColor(viewport()->backgroundRole(), bgColor);
328 viewport()->setPalette(palette);
329
330 update();
331 }
332
333 // ---
334
335 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
336 QColumnView(parent),
337 m_controller(controller)
338 {
339 Q_ASSERT(controller != 0);
340
341 setAcceptDrops(true);
342 setDragDropMode(QAbstractItemView::DragDrop);
343 setDropIndicatorShown(false);
344 setSelectionMode(SingleSelection);
345
346 if (KGlobalSettings::singleClick()) {
347 connect(this, SIGNAL(clicked(const QModelIndex&)),
348 this, SLOT(triggerItem(const QModelIndex&)));
349 } else {
350 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
351 this, SLOT(triggerItem(const QModelIndex&)));
352 }
353 connect(this, SIGNAL(entered(const QModelIndex&)),
354 controller, SLOT(emitItemEntered(const QModelIndex&)));
355 connect(this, SIGNAL(viewportEntered()),
356 controller, SLOT(emitViewportEntered()));
357 connect(controller, SIGNAL(zoomIn()),
358 this, SLOT(zoomIn()));
359 connect(controller, SIGNAL(zoomOut()),
360 this, SLOT(zoomOut()));
361 connect(controller, SIGNAL(urlChanged(const KUrl&)),
362 this, SLOT(updateColumnsState(const KUrl&)));
363
364 updateDecorationSize();
365 }
366
367 DolphinColumnView::~DolphinColumnView()
368 {
369 }
370
371 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
372 {
373 // let the column widget be aware about its URL...
374 KUrl columnUrl;
375 if (viewport()->children().count() == 0) {
376 // For the first column widget the directory lister has not been started
377 // yet, hence use the URL from the controller instead.
378 columnUrl = m_controller->url();
379 } else {
380 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
381 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
382
383 const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
384 KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex);
385 if (fileItem != 0) {
386 columnUrl = fileItem->url();
387 }
388 }
389
390 ColumnWidget* view = new ColumnWidget(viewport(), this, columnUrl);
391
392 // The following code has been copied 1:1 from QColumnView::createColumn().
393 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
394 // QColumnView::initializeColumn() will be available for this.
395
396 view->setFrameShape(QFrame::NoFrame);
397 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
398 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
399 view->setMinimumWidth(100);
400 view->setAttribute(Qt::WA_MacShowFocusRect, false);
401
402 // copy the 'view' behavior
403 view->setDragDropMode(dragDropMode());
404 view->setDragDropOverwriteMode(dragDropOverwriteMode());
405 view->setDropIndicatorShown(showDropIndicator());
406 view->setAlternatingRowColors(alternatingRowColors());
407 view->setAutoScroll(hasAutoScroll());
408 view->setEditTriggers(editTriggers());
409 view->setHorizontalScrollMode(horizontalScrollMode());
410 view->setIconSize(iconSize());
411 view->setSelectionBehavior(selectionBehavior());
412 view->setSelectionMode(selectionMode());
413 view->setTabKeyNavigation(tabKeyNavigation());
414 view->setTextElideMode(textElideMode());
415 view->setVerticalScrollMode(verticalScrollMode());
416
417 view->setModel(model());
418
419 // set the delegate to be the columnview delegate
420 QAbstractItemDelegate* delegate = view->itemDelegate();
421 view->setItemDelegate(itemDelegate());
422 delete delegate;
423
424 view->setRootIndex(index);
425
426 if (model()->canFetchMore(index)) {
427 model()->fetchMore(index);
428 }
429
430 return view;
431 }
432
433 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
434 {
435 m_controller->triggerActivation();
436 QColumnView::mousePressEvent(event);
437 }
438
439 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
440 {
441 if (event->mimeData()->hasUrls()) {
442 event->acceptProposedAction();
443 }
444 }
445
446 void DolphinColumnView::dropEvent(QDropEvent* event)
447 {
448 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
449 if (!urls.isEmpty()) {
450 m_controller->indicateDroppedUrls(urls,
451 indexAt(event->pos()),
452 event->source());
453 event->acceptProposedAction();
454 }
455 QColumnView::dropEvent(event);
456 }
457
458 void DolphinColumnView::showEvent(QShowEvent* event)
459 {
460 QColumnView::showEvent(event);
461 if (!event->spontaneous()) {
462 // QColumnView might clear the selection for folders that are shown in the next column.
463 // As this is not wanted the selection is updated if the directory lister has been completed.
464 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
465 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
466 KDirLister* dirLister = dirModel->dirLister();
467 connect(dirLister, SIGNAL(completed()),
468 this, SLOT(updateSelections()));
469 }
470 }
471
472 void DolphinColumnView::zoomIn()
473 {
474 if (isZoomInPossible()) {
475 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
476 // TODO: get rid of K3Icon sizes
477 switch (settings->iconSize()) {
478 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
479 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
480 default: Q_ASSERT(false); break;
481 }
482 updateDecorationSize();
483 }
484 }
485
486 void DolphinColumnView::zoomOut()
487 {
488 if (isZoomOutPossible()) {
489 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
490 // TODO: get rid of K3Icon sizes
491 switch (settings->iconSize()) {
492 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
493 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
494 default: Q_ASSERT(false); break;
495 }
496 updateDecorationSize();
497 }
498 }
499
500 void DolphinColumnView::triggerItem(const QModelIndex& index)
501 {
502 m_controller->triggerItem(index);
503 updateColumnsState(m_controller->url());
504 }
505
506 void DolphinColumnView::updateColumnsState(const KUrl& url)
507 {
508 foreach (QObject* object, viewport()->children()) {
509 if (object->inherits("QListView")) {
510 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
511 widget->setActive(widget->url() == url);
512 }
513 }
514 }
515
516
517 void DolphinColumnView::updateDecorationSize()
518 {
519 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
520 const int iconSize = settings->iconSize();
521
522 foreach (QObject* object, viewport()->children()) {
523 if (object->inherits("QListView")) {
524 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
525 widget->setDecorationSize(QSize(iconSize, iconSize));
526 }
527 }
528
529 m_controller->setZoomInPossible(isZoomInPossible());
530 m_controller->setZoomOutPossible(isZoomOutPossible());
531
532 doItemsLayout();
533 }
534
535 void DolphinColumnView::updateSelections()
536 {
537 ColumnWidget* previousWidget = 0;
538 foreach (QObject* object, viewport()->children()) {
539 if (object->inherits("QListView")) {
540 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
541 if (previousWidget != 0) {
542 previousWidget->updateSelection(widget->url());
543 }
544 previousWidget = widget;
545 }
546 }
547 if (previousWidget != 0) {
548 previousWidget->updateSelection(KUrl());
549 }
550 }
551
552 bool DolphinColumnView::isZoomInPossible() const
553 {
554 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
555 return settings->iconSize() < K3Icon::SizeLarge;
556 }
557
558 bool DolphinColumnView::isZoomOutPossible() const
559 {
560 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
561 return settings->iconSize() > K3Icon::SizeSmall;
562 }
563
564 void DolphinColumnView::requestActivation(QWidget* column)
565 {
566 foreach (QObject* object, viewport()->children()) {
567 if (object->inherits("QListView")) {
568 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
569 const bool isActive = (widget == column);
570 widget->setActive(isActive);
571 if (isActive) {
572 m_controller->setUrl(widget->url());
573 }
574 }
575 }
576 updateSelections();
577 }
578
579 #include "dolphincolumnview.moc"