]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
new approach: don't fight against the selection behavior of QColumnView, but improve...
[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 protected:
61 virtual QStyleOptionViewItem viewOptions() const;
62 virtual void dragEnterEvent(QDragEnterEvent* event);
63 virtual void dragLeaveEvent(QDragLeaveEvent* event);
64 virtual void dragMoveEvent(QDragMoveEvent* event);
65 virtual void dropEvent(QDropEvent* event);
66 virtual void mousePressEvent(QMouseEvent* event);
67 virtual void mouseMoveEvent(QMouseEvent* event);
68 virtual void mouseReleaseEvent(QMouseEvent* event);
69 virtual void paintEvent(QPaintEvent* event);
70 virtual void contextMenuEvent(QContextMenuEvent* event);
71
72 private:
73 /** Used by ColumnWidget::setActive(). */
74 void activate();
75
76 /** Used by ColumnWidget::setActive(). */
77 void deactivate();
78
79 private:
80 bool m_active;
81 bool m_swallowMouseMoveEvents;
82 DolphinColumnView* m_view;
83 KUrl m_url;
84 KUrl m_childUrl; // URL of the next column that is shown
85 QStyleOptionViewItem m_viewOptions;
86
87 bool m_dragging; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
88 QRect m_dropRect; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
89 };
90
91 ColumnWidget::ColumnWidget(QWidget* parent,
92 DolphinColumnView* columnView,
93 const KUrl& url) :
94 QListView(parent),
95 m_active(true),
96 m_swallowMouseMoveEvents(false),
97 m_view(columnView),
98 m_url(url),
99 m_childUrl(),
100 m_dragging(false),
101 m_dropRect()
102 {
103 setMouseTracking(true);
104 viewport()->setAttribute(Qt::WA_Hover);
105
106 // apply the column mode settings to the widget
107 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
108 Q_ASSERT(settings != 0);
109
110 m_viewOptions = QListView::viewOptions();
111
112 QFont font(settings->fontFamily(), settings->fontSize());
113 font.setItalic(settings->italicFont());
114 font.setBold(settings->boldFont());
115 m_viewOptions.font = font;
116
117 const int iconSize = settings->iconSize();
118 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
119
120 activate();
121 }
122
123 ColumnWidget::~ColumnWidget()
124 {
125 }
126
127 void ColumnWidget::setDecorationSize(const QSize& size)
128 {
129 m_viewOptions.decorationSize = size;
130 doItemsLayout();
131 }
132
133 void ColumnWidget::setActive(bool active)
134 {
135 if (m_active == active) {
136 return;
137 }
138
139 m_active = active;
140
141 if (active) {
142 activate();
143 } else {
144 deactivate();
145 }
146 }
147
148 inline bool ColumnWidget::isActive() const
149 {
150 return m_active;
151 }
152
153 const KUrl& ColumnWidget::url() const
154 {
155 return m_url;
156 }
157
158 QStyleOptionViewItem ColumnWidget::viewOptions() const
159 {
160 return m_viewOptions;
161 }
162
163 void ColumnWidget::dragEnterEvent(QDragEnterEvent* event)
164 {
165 if (event->mimeData()->hasUrls()) {
166 event->acceptProposedAction();
167 }
168
169 m_dragging = true;
170 }
171
172 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
173 {
174 QListView::dragLeaveEvent(event);
175
176 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
177 m_dragging = false;
178 setDirtyRegion(m_dropRect);
179 }
180
181 void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
182 {
183 QListView::dragMoveEvent(event);
184
185 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
186 const QModelIndex index = indexAt(event->pos());
187 setDirtyRegion(m_dropRect);
188 m_dropRect = visualRect(index);
189 setDirtyRegion(m_dropRect);
190 }
191
192 void ColumnWidget::dropEvent(QDropEvent* event)
193 {
194 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
195 if (!urls.isEmpty()) {
196 event->acceptProposedAction();
197 m_view->m_controller->indicateDroppedUrls(urls,
198 indexAt(event->pos()),
199 event->source());
200 }
201 QListView::dropEvent(event);
202 m_dragging = false;
203 }
204
205 void ColumnWidget::mousePressEvent(QMouseEvent* event)
206 {
207 QListView::mousePressEvent(event);
208
209 bool requestActivation = false;
210 const QModelIndex index = indexAt(event->pos());
211 if (index.isValid()) {
212 // A click on an item has been done. Only request an activation
213 // if the item is not a directory.
214 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
215 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
216 const QModelIndex dirIndex = proxyModel->mapToSource(index);
217 KFileItem* item = dirModel->itemForIndex(dirIndex);
218 if (item != 0) {
219 if (item->isDir()) {
220 m_childUrl = item->url();
221 } else {
222 requestActivation = true;
223 }
224 }
225 } else {
226 // a click on the viewport has been done
227 requestActivation = true;
228
229 // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
230 // triggers an unwanted loading of directories on hovering folder items.
231 m_swallowMouseMoveEvents = true;
232 }
233
234 if (requestActivation) {
235 m_view->requestActivation(this);
236 }
237 }
238
239 void ColumnWidget::mouseMoveEvent(QMouseEvent* event)
240 {
241 // see description in ColumnView::mousePressEvent()
242 if (!m_swallowMouseMoveEvents) {
243 QListView::mouseMoveEvent(event);
244 }
245 }
246
247 void ColumnWidget::mouseReleaseEvent(QMouseEvent* event)
248 {
249 QListView::mouseReleaseEvent(event);
250 m_swallowMouseMoveEvents = false;
251 }
252
253
254 void ColumnWidget::paintEvent(QPaintEvent* event)
255 {
256 // indicate the shown URL of the next column by highlighting the shown folder item
257 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
258 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
259 const QModelIndex dirIndex = dirModel->indexForUrl(m_childUrl);
260 const QModelIndex proxyIndex = proxyModel->mapFromSource(dirIndex);
261 if (proxyIndex.isValid()) {
262 const QRect itemRect = visualRect(proxyIndex);
263 QPainter painter(viewport());
264 painter.save();
265
266 QColor color = KColorScheme(KColorScheme::View).foreground();
267 color.setAlpha(32);
268 painter.setPen(Qt::NoPen);
269 painter.setBrush(color);
270 painter.drawRect(itemRect);
271
272 painter.restore();
273 }
274
275 QListView::paintEvent(event);
276
277 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
278 if (m_dragging) {
279 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
280 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
281 }
282 }
283
284 void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
285 {
286 if (!m_active) {
287 m_view->requestActivation(this);
288 }
289
290 QListView::contextMenuEvent(event);
291
292 const QModelIndex index = indexAt(event->pos());
293 if (index.isValid() || m_active) {
294 // Only open a context menu above an item or if the mouse is above
295 // the active column.
296 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
297 m_view->m_controller->triggerContextMenuRequest(pos);
298 }
299 }
300
301 void ColumnWidget::activate()
302 {
303 const QColor bgColor = KColorScheme(KColorScheme::View).background();
304 QPalette palette = viewport()->palette();
305 palette.setColor(viewport()->backgroundRole(), bgColor);
306 viewport()->setPalette(palette);
307
308 update();
309 }
310
311 void ColumnWidget::deactivate()
312 {
313 QColor bgColor = KColorScheme(KColorScheme::View).background();
314 const QColor fgColor = KColorScheme(KColorScheme::View).foreground();
315 bgColor = KColorUtils::mix(bgColor, fgColor, 0.04);
316
317 QPalette palette = viewport()->palette();
318 palette.setColor(viewport()->backgroundRole(), bgColor);
319 viewport()->setPalette(palette);
320
321 update();
322 }
323
324 // ---
325
326 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
327 QColumnView(parent),
328 m_controller(controller)
329 {
330 Q_ASSERT(controller != 0);
331
332 setAcceptDrops(true);
333 setDragDropMode(QAbstractItemView::DragDrop);
334 setDropIndicatorShown(false);
335
336 if (KGlobalSettings::singleClick()) {
337 connect(this, SIGNAL(clicked(const QModelIndex&)),
338 this, SLOT(triggerItem(const QModelIndex&)));
339 } else {
340 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
341 this, SLOT(triggerItem(const QModelIndex&)));
342 }
343 connect(this, SIGNAL(entered(const QModelIndex&)),
344 controller, SLOT(emitItemEntered(const QModelIndex&)));
345 connect(this, SIGNAL(viewportEntered()),
346 controller, SLOT(emitViewportEntered()));
347 connect(controller, SIGNAL(zoomIn()),
348 this, SLOT(zoomIn()));
349 connect(controller, SIGNAL(zoomOut()),
350 this, SLOT(zoomOut()));
351 connect(controller, SIGNAL(urlChanged(const KUrl&)),
352 this, SLOT(updateColumnsState(const KUrl&)));
353
354 updateDecorationSize();
355 }
356
357 DolphinColumnView::~DolphinColumnView()
358 {
359 }
360
361 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
362 {
363 // let the column widget be aware about its URL...
364 KUrl columnUrl;
365 if (viewport()->children().count() == 0) {
366 // For the first column widget the directory lister has not been started
367 // yet, hence use the URL from the controller instead.
368 columnUrl = m_controller->url();
369 } else {
370 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
371 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
372
373 const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
374 KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex);
375 if (fileItem != 0) {
376 columnUrl = fileItem->url();
377 }
378 }
379
380 ColumnWidget* view = new ColumnWidget(viewport(), this, columnUrl);
381
382 // The following code has been copied 1:1 from QColumnView::createColumn().
383 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
384 // QColumnView::initializeColumn() will be available for this.
385
386 view->setFrameShape(QFrame::NoFrame);
387 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
388 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
389 view->setMinimumWidth(100);
390 view->setAttribute(Qt::WA_MacShowFocusRect, false);
391
392 // copy the 'view' behavior
393 view->setDragDropMode(dragDropMode());
394 view->setDragDropOverwriteMode(dragDropOverwriteMode());
395 view->setDropIndicatorShown(showDropIndicator());
396 view->setAlternatingRowColors(alternatingRowColors());
397 view->setAutoScroll(hasAutoScroll());
398 view->setEditTriggers(editTriggers());
399 view->setHorizontalScrollMode(horizontalScrollMode());
400 view->setIconSize(iconSize());
401 view->setSelectionBehavior(selectionBehavior());
402 view->setSelectionMode(selectionMode());
403 view->setTabKeyNavigation(tabKeyNavigation());
404 view->setTextElideMode(textElideMode());
405 view->setVerticalScrollMode(verticalScrollMode());
406
407 view->setModel(model());
408
409 // set the delegate to be the columnview delegate
410 QAbstractItemDelegate* delegate = view->itemDelegate();
411 view->setItemDelegate(itemDelegate());
412 delete delegate;
413
414 view->setRootIndex(index);
415
416 if (model()->canFetchMore(index)) {
417 model()->fetchMore(index);
418 }
419
420 return view;
421 }
422
423 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
424 {
425 m_controller->triggerActivation();
426 QColumnView::mousePressEvent(event);
427 }
428
429 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
430 {
431 if (event->mimeData()->hasUrls()) {
432 event->acceptProposedAction();
433 }
434 }
435
436 void DolphinColumnView::dropEvent(QDropEvent* event)
437 {
438 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
439 if (!urls.isEmpty()) {
440 m_controller->indicateDroppedUrls(urls,
441 indexAt(event->pos()),
442 event->source());
443 event->acceptProposedAction();
444 }
445 QColumnView::dropEvent(event);
446 }
447
448 void DolphinColumnView::zoomIn()
449 {
450 if (isZoomInPossible()) {
451 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
452 // TODO: get rid of K3Icon sizes
453 switch (settings->iconSize()) {
454 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
455 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
456 default: Q_ASSERT(false); break;
457 }
458 updateDecorationSize();
459 }
460 }
461
462 void DolphinColumnView::zoomOut()
463 {
464 if (isZoomOutPossible()) {
465 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
466 // TODO: get rid of K3Icon sizes
467 switch (settings->iconSize()) {
468 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
469 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
470 default: Q_ASSERT(false); break;
471 }
472 updateDecorationSize();
473 }
474 }
475
476 void DolphinColumnView::triggerItem(const QModelIndex& index)
477 {
478 m_controller->triggerItem(index);
479 updateColumnsState(m_controller->url());
480 }
481
482 void DolphinColumnView::updateColumnsState(const KUrl& url)
483 {
484 foreach (QObject* object, viewport()->children()) {
485 if (object->inherits("QListView")) {
486 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
487 widget->setActive(widget->url() == url);
488 }
489 }
490 }
491
492
493 void DolphinColumnView::updateDecorationSize()
494 {
495 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
496 const int iconSize = settings->iconSize();
497
498 foreach (QObject* object, viewport()->children()) {
499 if (object->inherits("QListView")) {
500 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
501 widget->setDecorationSize(QSize(iconSize, iconSize));
502 }
503 }
504
505 m_controller->setZoomInPossible(isZoomInPossible());
506 m_controller->setZoomOutPossible(isZoomOutPossible());
507
508 doItemsLayout();
509 }
510
511 bool DolphinColumnView::isZoomInPossible() const
512 {
513 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
514 return settings->iconSize() < K3Icon::SizeLarge;
515 }
516
517 bool DolphinColumnView::isZoomOutPossible() const
518 {
519 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
520 return settings->iconSize() > K3Icon::SizeSmall;
521 }
522
523 void DolphinColumnView::requestActivation(QWidget* column)
524 {
525 foreach (QObject* object, viewport()->children()) {
526 if (object->inherits("QListView")) {
527 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
528 const bool isActive = (widget == column);
529 widget->setActive(isActive);
530 if (isActive) {
531 m_controller->setUrl(widget->url());
532 }
533 }
534 }
535 }
536
537 #include "dolphincolumnview.moc"