]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
SVN_SILENT made messages (.desktop file)
[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 }
177 }
178
179 void ColumnWidget::releaseSelectionModel()
180 {
181 if (selectionModel() == m_view->selectionModel()) {
182 QItemSelectionModel* replacementModel = new QItemSelectionModel(model());
183 setSelectionModel(replacementModel);
184 }
185 }
186
187 QStyleOptionViewItem ColumnWidget::viewOptions() const
188 {
189 return m_viewOptions;
190 }
191
192 void ColumnWidget::dragEnterEvent(QDragEnterEvent* event)
193 {
194 if (event->mimeData()->hasUrls()) {
195 event->acceptProposedAction();
196 }
197
198 m_dragging = true;
199 }
200
201 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
202 {
203 QListView::dragLeaveEvent(event);
204
205 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
206 m_dragging = false;
207 setDirtyRegion(m_dropRect);
208 }
209
210 void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
211 {
212 QListView::dragMoveEvent(event);
213
214 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
215 const QModelIndex index = indexAt(event->pos());
216 setDirtyRegion(m_dropRect);
217 m_dropRect = visualRect(index);
218 setDirtyRegion(m_dropRect);
219 }
220
221 void ColumnWidget::dropEvent(QDropEvent* event)
222 {
223 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
224 if (!urls.isEmpty()) {
225 event->acceptProposedAction();
226 m_view->m_controller->indicateDroppedUrls(urls,
227 indexAt(event->pos()),
228 event->source());
229 }
230 QListView::dropEvent(event);
231 m_dragging = false;
232 }
233
234 void ColumnWidget::mousePressEvent(QMouseEvent* event)
235 {
236 m_view->requestSelectionModel(this);
237
238 bool swallowMousePressEvent = false;
239 const QModelIndex index = indexAt(event->pos());
240 if (index.isValid()) {
241 // A click on an item has been done. Only request an activation
242 // if the item is not a directory.
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::Select);
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 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
432 {
433 // let the column widget be aware about its URL...
434 KUrl columnUrl;
435 if (viewport()->children().count() == 0) {
436 // For the first column widget the directory lister has not been started
437 // yet, hence use the URL from the controller instead.
438 columnUrl = m_controller->url();
439 } else {
440 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
441 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
442
443 const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
444 KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex);
445 if (fileItem != 0) {
446 columnUrl = fileItem->url();
447 }
448 }
449
450 ColumnWidget* view = new ColumnWidget(viewport(), this, columnUrl);
451
452 // The following code has been copied 1:1 from QColumnView::createColumn().
453 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
454 // QColumnView::initializeColumn() will be available for this.
455
456 view->setFrameShape(QFrame::NoFrame);
457 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
458 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
459 view->setMinimumWidth(100);
460 view->setAttribute(Qt::WA_MacShowFocusRect, false);
461
462 // copy the 'view' behavior
463 view->setDragDropMode(dragDropMode());
464 view->setDragDropOverwriteMode(dragDropOverwriteMode());
465 view->setDropIndicatorShown(showDropIndicator());
466 view->setAlternatingRowColors(alternatingRowColors());
467 view->setAutoScroll(hasAutoScroll());
468 view->setEditTriggers(editTriggers());
469 view->setHorizontalScrollMode(horizontalScrollMode());
470 view->setIconSize(iconSize());
471 view->setSelectionBehavior(selectionBehavior());
472 view->setSelectionMode(selectionMode());
473 view->setTabKeyNavigation(tabKeyNavigation());
474 view->setTextElideMode(textElideMode());
475 view->setVerticalScrollMode(verticalScrollMode());
476
477 view->setModel(model());
478
479 // set the delegate to be the columnview delegate
480 QAbstractItemDelegate* delegate = view->itemDelegate();
481 view->setItemDelegate(itemDelegate());
482 delete delegate;
483
484 view->setRootIndex(index);
485
486 if (model()->canFetchMore(index)) {
487 model()->fetchMore(index);
488 }
489
490 return view;
491 }
492
493 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
494 {
495 m_controller->triggerActivation();
496 QColumnView::mousePressEvent(event);
497 }
498
499 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
500 {
501 if (event->mimeData()->hasUrls()) {
502 event->acceptProposedAction();
503 }
504 }
505
506 void DolphinColumnView::dropEvent(QDropEvent* event)
507 {
508 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
509 if (!urls.isEmpty()) {
510 m_controller->indicateDroppedUrls(urls,
511 indexAt(event->pos()),
512 event->source());
513 event->acceptProposedAction();
514 }
515 QColumnView::dropEvent(event);
516 }
517
518 void DolphinColumnView::zoomIn()
519 {
520 if (isZoomInPossible()) {
521 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
522 // TODO: get rid of K3Icon sizes
523 switch (settings->iconSize()) {
524 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
525 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
526 default: Q_ASSERT(false); break;
527 }
528 updateDecorationSize();
529 }
530 }
531
532 void DolphinColumnView::zoomOut()
533 {
534 if (isZoomOutPossible()) {
535 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
536 // TODO: get rid of K3Icon sizes
537 switch (settings->iconSize()) {
538 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
539 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
540 default: Q_ASSERT(false); break;
541 }
542 updateDecorationSize();
543 }
544 }
545
546 void DolphinColumnView::triggerItem(const QModelIndex& index)
547 {
548 m_controller->triggerItem(index);
549 updateColumnsState(m_controller->url());
550 }
551
552 void DolphinColumnView::updateColumnsState(const KUrl& url)
553 {
554 foreach (QObject* object, viewport()->children()) {
555 if (object->inherits("QListView")) {
556 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
557 widget->setActive(widget->url() == url);
558 }
559 }
560 }
561
562
563 void DolphinColumnView::updateDecorationSize()
564 {
565 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
566 const int iconSize = settings->iconSize();
567
568 foreach (QObject* object, viewport()->children()) {
569 if (object->inherits("QListView")) {
570 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
571 widget->setDecorationSize(QSize(iconSize, iconSize));
572 }
573 }
574
575 m_controller->setZoomInPossible(isZoomInPossible());
576 m_controller->setZoomOutPossible(isZoomOutPossible());
577
578 doItemsLayout();
579 }
580
581 bool DolphinColumnView::isZoomInPossible() const
582 {
583 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
584 return settings->iconSize() < K3Icon::SizeLarge;
585 }
586
587 bool DolphinColumnView::isZoomOutPossible() const
588 {
589 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
590 return settings->iconSize() > K3Icon::SizeSmall;
591 }
592
593 void DolphinColumnView::requestActivation(QWidget* column)
594 {
595 foreach (QObject* object, viewport()->children()) {
596 if (object->inherits("QListView")) {
597 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
598 const bool isActive = (widget == column);
599 widget->setActive(isActive);
600 if (isActive) {
601 m_controller->setUrl(widget->url());
602 }
603 }
604 }
605 }
606
607 void DolphinColumnView::requestSelectionModel(QAbstractItemView* view)
608 {
609 foreach (QObject* object, viewport()->children()) {
610 if (object->inherits("QListView")) {
611 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
612 if (widget == view) {
613 widget->obtainSelectionModel();
614 } else {
615 widget->releaseSelectionModel();
616 }
617 }
618 }
619 }
620
621 #include "dolphincolumnview.moc"