]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
minor fixes regarding inactive columns
[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 protected slots:
73 virtual void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
74
75 private:
76 /** Used by ColumnWidget::setActive(). */
77 void activate();
78
79 /** Used by ColumnWidget::setActive(). */
80 void deactivate();
81
82 private:
83 bool m_active;
84 bool m_swallowMouseMoveEvents;
85 DolphinColumnView* m_view;
86 KUrl m_url;
87 KUrl m_childUrl; // URL of the next column that is shown
88 QStyleOptionViewItem m_viewOptions;
89
90 bool m_dragging; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
91 QRect m_dropRect; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
92 };
93
94 ColumnWidget::ColumnWidget(QWidget* parent,
95 DolphinColumnView* columnView,
96 const KUrl& url) :
97 QListView(parent),
98 m_active(true),
99 m_swallowMouseMoveEvents(false),
100 m_view(columnView),
101 m_url(url),
102 m_childUrl(),
103 m_dragging(false),
104 m_dropRect()
105 {
106 setMouseTracking(true);
107 viewport()->setAttribute(Qt::WA_Hover);
108
109 // apply the column mode settings to the widget
110 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
111 Q_ASSERT(settings != 0);
112
113 m_viewOptions = QListView::viewOptions();
114
115 QFont font(settings->fontFamily(), settings->fontSize());
116 font.setItalic(settings->italicFont());
117 font.setBold(settings->boldFont());
118 m_viewOptions.font = font;
119
120 const int iconSize = settings->iconSize();
121 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
122
123 activate();
124 }
125
126 ColumnWidget::~ColumnWidget()
127 {
128 }
129
130 void ColumnWidget::setDecorationSize(const QSize& size)
131 {
132 m_viewOptions.decorationSize = size;
133 doItemsLayout();
134 }
135
136 void ColumnWidget::setActive(bool active)
137 {
138 if (m_active == active) {
139 return;
140 }
141
142 m_active = active;
143
144 if (active) {
145 activate();
146 } else {
147 deactivate();
148 }
149 }
150
151 inline bool ColumnWidget::isActive() const
152 {
153 return m_active;
154 }
155
156 const KUrl& ColumnWidget::url() const
157 {
158 return m_url;
159 }
160
161 QStyleOptionViewItem ColumnWidget::viewOptions() const
162 {
163 return m_viewOptions;
164 }
165
166 void ColumnWidget::dragEnterEvent(QDragEnterEvent* event)
167 {
168 if (event->mimeData()->hasUrls()) {
169 event->acceptProposedAction();
170 }
171
172 m_dragging = true;
173 }
174
175 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
176 {
177 QListView::dragLeaveEvent(event);
178
179 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
180 m_dragging = false;
181 setDirtyRegion(m_dropRect);
182 }
183
184 void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
185 {
186 QListView::dragMoveEvent(event);
187
188 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
189 const QModelIndex index = indexAt(event->pos());
190 setDirtyRegion(m_dropRect);
191 m_dropRect = visualRect(index);
192 setDirtyRegion(m_dropRect);
193 }
194
195 void ColumnWidget::dropEvent(QDropEvent* event)
196 {
197 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
198 if (!urls.isEmpty()) {
199 event->acceptProposedAction();
200 m_view->m_controller->indicateDroppedUrls(urls,
201 indexAt(event->pos()),
202 event->source());
203 }
204 QListView::dropEvent(event);
205 m_dragging = false;
206 }
207
208 void ColumnWidget::mousePressEvent(QMouseEvent* event)
209 {
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 viewport()->update();
222 } else {
223 m_view->requestActivation(this);
224 }
225 }
226 } else {
227 // a click on the viewport has been done
228 m_view->requestActivation(this);
229
230 // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
231 // triggers an unwanted loading of directories on hovering folder items.
232 m_swallowMouseMoveEvents = true;
233 }
234
235 QListView::mousePressEvent(event);
236 }
237
238 void ColumnWidget::mouseMoveEvent(QMouseEvent* event)
239 {
240 // see description in ColumnView::mousePressEvent()
241 if (!m_swallowMouseMoveEvents) {
242 QListView::mouseMoveEvent(event);
243 }
244 }
245
246 void ColumnWidget::mouseReleaseEvent(QMouseEvent* event)
247 {
248 QListView::mouseReleaseEvent(event);
249 m_swallowMouseMoveEvents = false;
250 }
251
252
253 void ColumnWidget::paintEvent(QPaintEvent* event)
254 {
255 if (!m_childUrl.isEmpty()) {
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() && !selectionModel()->isSelected(proxyIndex)) {
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
276 QListView::paintEvent(event);
277
278 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
279 if (m_dragging) {
280 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
281 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
282 }
283 }
284
285 void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
286 {
287 if (!m_active) {
288 m_view->requestActivation(this);
289 }
290
291 QListView::contextMenuEvent(event);
292
293 const QModelIndex index = indexAt(event->pos());
294 if (index.isValid() || m_active) {
295 // Only open a context menu above an item or if the mouse is above
296 // the active column.
297 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
298 m_view->m_controller->triggerContextMenuRequest(pos);
299 }
300 }
301
302 void ColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
303 {
304 // inactive views should not have any selection
305 if (!m_active) {
306 clearSelection();
307 }
308 QListView::selectionChanged(selected, deselected);
309 }
310
311 void ColumnWidget::activate()
312 {
313 const QColor bgColor = KColorScheme(KColorScheme::View).background();
314 QPalette palette = viewport()->palette();
315 palette.setColor(viewport()->backgroundRole(), bgColor);
316 viewport()->setPalette(palette);
317
318 update();
319 }
320
321 void ColumnWidget::deactivate()
322 {
323 QColor bgColor = KColorScheme(KColorScheme::View).background();
324 const QColor fgColor = KColorScheme(KColorScheme::View).foreground();
325 bgColor = KColorUtils::mix(bgColor, fgColor, 0.04);
326
327 QPalette palette = viewport()->palette();
328 palette.setColor(viewport()->backgroundRole(), bgColor);
329 viewport()->setPalette(palette);
330
331 update();
332 }
333
334 // ---
335
336 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
337 QColumnView(parent),
338 m_controller(controller)
339 {
340 Q_ASSERT(controller != 0);
341
342 setAcceptDrops(true);
343 setDragDropMode(QAbstractItemView::DragDrop);
344 setDropIndicatorShown(false);
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::zoomIn()
459 {
460 if (isZoomInPossible()) {
461 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
462 // TODO: get rid of K3Icon sizes
463 switch (settings->iconSize()) {
464 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
465 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
466 default: Q_ASSERT(false); break;
467 }
468 updateDecorationSize();
469 }
470 }
471
472 void DolphinColumnView::zoomOut()
473 {
474 if (isZoomOutPossible()) {
475 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
476 // TODO: get rid of K3Icon sizes
477 switch (settings->iconSize()) {
478 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
479 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
480 default: Q_ASSERT(false); break;
481 }
482 updateDecorationSize();
483 }
484 }
485
486 void DolphinColumnView::triggerItem(const QModelIndex& index)
487 {
488 m_controller->triggerItem(index);
489 updateColumnsState(m_controller->url());
490 }
491
492 void DolphinColumnView::updateColumnsState(const KUrl& url)
493 {
494 foreach (QObject* object, viewport()->children()) {
495 if (object->inherits("QListView")) {
496 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
497 widget->setActive(widget->url() == url);
498 }
499 }
500 }
501
502
503 void DolphinColumnView::updateDecorationSize()
504 {
505 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
506 const int iconSize = settings->iconSize();
507
508 foreach (QObject* object, viewport()->children()) {
509 if (object->inherits("QListView")) {
510 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
511 widget->setDecorationSize(QSize(iconSize, iconSize));
512 }
513 }
514
515 m_controller->setZoomInPossible(isZoomInPossible());
516 m_controller->setZoomOutPossible(isZoomOutPossible());
517
518 doItemsLayout();
519 }
520
521 bool DolphinColumnView::isZoomInPossible() const
522 {
523 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
524 return settings->iconSize() < K3Icon::SizeLarge;
525 }
526
527 bool DolphinColumnView::isZoomOutPossible() const
528 {
529 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
530 return settings->iconSize() > K3Icon::SizeSmall;
531 }
532
533 void DolphinColumnView::requestActivation(QWidget* column)
534 {
535 foreach (QObject* object, viewport()->children()) {
536 if (object->inherits("QListView")) {
537 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
538 const bool isActive = (widget == column);
539 widget->setActive(isActive);
540 if (isActive) {
541 m_controller->setUrl(widget->url());
542 }
543 }
544 }
545 }
546
547 #include "dolphincolumnview.moc"