1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "dolphincolumnview.h"
22 #include "dolphincontroller.h"
23 #include "dolphinsettings.h"
25 #include "dolphin_columnmodesettings.h"
27 #include <kcolorutils.h>
28 #include <kcolorscheme.h>
29 #include <kdirlister.h>
30 #include <kdirmodel.h>
32 #include <QAbstractProxyModel>
33 #include <QApplication>
37 * Represents one column inside the DolphinColumnView and has been
38 * extended to respect view options and hovering information.
40 class ColumnWidget
: public QListView
43 ColumnWidget(QWidget
* parent
,
44 DolphinColumnView
* columnView
,
46 virtual ~ColumnWidget();
48 /** Sets the size of the icons. */
49 void setDecorationSize(const QSize
& size
);
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.
56 void setActive(bool active
);
57 inline bool isActive() const;
59 inline const KUrl
& url() const;
61 void obtainSelectionModel();
62 void releaseSelectionModel();
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
);
77 virtual void selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
);
80 /** Used by ColumnWidget::setActive(). */
83 /** Used by ColumnWidget::setActive(). */
88 bool m_swallowMouseMoveEvents
;
89 DolphinColumnView
* m_view
;
91 KUrl m_childUrl
; // URL of the next column that is shown
92 QStyleOptionViewItem m_viewOptions
;
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
98 ColumnWidget::ColumnWidget(QWidget
* parent
,
99 DolphinColumnView
* columnView
,
103 m_swallowMouseMoveEvents(false),
110 setMouseTracking(true);
111 viewport()->setAttribute(Qt::WA_Hover
);
113 // apply the column mode settings to the widget
114 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
115 Q_ASSERT(settings
!= 0);
117 m_viewOptions
= QListView::viewOptions();
119 QFont
font(settings
->fontFamily(), settings
->fontSize());
120 font
.setItalic(settings
->italicFont());
121 font
.setBold(settings
->boldFont());
122 m_viewOptions
.font
= font
;
124 const int iconSize
= settings
->iconSize();
125 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
130 ColumnWidget::~ColumnWidget()
134 void ColumnWidget::setDecorationSize(const QSize
& size
)
136 m_viewOptions
.decorationSize
= size
;
140 void ColumnWidget::setActive(bool active
)
143 obtainSelectionModel();
145 releaseSelectionModel();
148 if (m_active
== active
) {
161 inline bool ColumnWidget::isActive() const
166 const KUrl
& ColumnWidget::url() const
171 void ColumnWidget::obtainSelectionModel()
173 if (selectionModel() != m_view
->selectionModel()) {
174 selectionModel()->deleteLater();
175 setSelectionModel(m_view
->selectionModel());
180 void ColumnWidget::releaseSelectionModel()
182 if (selectionModel() == m_view
->selectionModel()) {
183 QItemSelectionModel
* replacementModel
= new QItemSelectionModel(model());
184 setSelectionModel(replacementModel
);
188 QStyleOptionViewItem
ColumnWidget::viewOptions() const
190 return m_viewOptions
;
193 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
195 if (event
->mimeData()->hasUrls()) {
196 event
->acceptProposedAction();
202 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
204 QListView::dragLeaveEvent(event
);
206 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
208 setDirtyRegion(m_dropRect
);
211 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
213 QListView::dragMoveEvent(event
);
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
);
222 void ColumnWidget::dropEvent(QDropEvent
* event
)
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()),
231 QListView::dropEvent(event
);
235 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
237 m_view
->requestSelectionModel(this);
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
);
248 QItemSelectionModel
* selModel
= selectionModel();
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();
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
263 if (event
->button() != Qt::LeftButton
) {
264 m_view
->requestActivation(this);
267 m_view
->requestActivation(this);
270 // TODO: check behavior with ShiftModifier
271 //if (modifier & Qt::ShiftModifier)
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
)) {
279 selModel
->select(index
, QItemSelectionModel::Select
);
283 // a click on the viewport has been done
284 m_view
->requestActivation(this);
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;
292 if (!swallowMousePressEvent
) {
293 QListView::mousePressEvent(event
);
297 void ColumnWidget::mouseMoveEvent(QMouseEvent
* event
)
299 // see description in ColumnView::mousePressEvent()
300 if (!m_swallowMouseMoveEvents
) {
301 QListView::mouseMoveEvent(event
);
305 void ColumnWidget::mouseReleaseEvent(QMouseEvent
* event
)
307 QListView::mouseReleaseEvent(event
);
308 m_swallowMouseMoveEvents
= false;
312 void ColumnWidget::paintEvent(QPaintEvent
* event
)
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());
325 QColor color
= KColorScheme(KColorScheme::View
).foreground();
327 painter
.setPen(Qt::NoPen
);
328 painter
.setBrush(color
);
329 painter
.drawRect(itemRect
);
335 QListView::paintEvent(event
);
337 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
339 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
340 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
344 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
347 m_view
->requestActivation(this);
350 QListView::contextMenuEvent(event
);
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
);
361 void ColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
363 // inactive views should not have any selection
367 QListView::selectionChanged(selected
, deselected
);
370 void ColumnWidget::activate()
372 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
373 QPalette palette
= viewport()->palette();
374 palette
.setColor(viewport()->backgroundRole(), bgColor
);
375 viewport()->setPalette(palette
);
380 void ColumnWidget::deactivate()
382 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
383 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
384 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
386 QPalette palette
= viewport()->palette();
387 palette
.setColor(viewport()->backgroundRole(), bgColor
);
388 viewport()->setPalette(palette
);
395 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
397 m_controller(controller
)
399 Q_ASSERT(controller
!= 0);
401 setAcceptDrops(true);
402 setDragDropMode(QAbstractItemView::DragDrop
);
403 setDropIndicatorShown(false);
404 setSelectionMode(ExtendedSelection
);
406 if (KGlobalSettings::singleClick()) {
407 connect(this, SIGNAL(clicked(const QModelIndex
&)),
408 this, SLOT(triggerItem(const QModelIndex
&)));
410 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
411 this, SLOT(triggerItem(const QModelIndex
&)));
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
&)));
424 updateDecorationSize();
427 DolphinColumnView::~DolphinColumnView()
431 void DolphinColumnView::invertSelection()
433 selectActiveColumn(QItemSelectionModel::Toggle
);
436 void DolphinColumnView::selectAll()
438 selectActiveColumn(QItemSelectionModel::Select
);
441 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
443 // let the column widget be aware about its URL...
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();
450 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
451 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
453 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
454 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
456 columnUrl
= fileItem
->url();
460 ColumnWidget
* view
= new ColumnWidget(viewport(), this, columnUrl
);
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.
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);
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());
487 view
->setModel(model());
489 // set the delegate to be the columnview delegate
490 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
491 view
->setItemDelegate(itemDelegate());
494 view
->setRootIndex(index
);
496 if (model()->canFetchMore(index
)) {
497 model()->fetchMore(index
);
503 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
505 m_controller
->triggerActivation();
506 QColumnView::mousePressEvent(event
);
509 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
511 if (event
->mimeData()->hasUrls()) {
512 event
->acceptProposedAction();
516 void DolphinColumnView::dropEvent(QDropEvent
* event
)
518 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
519 if (!urls
.isEmpty()) {
520 m_controller
->indicateDroppedUrls(urls
,
521 indexAt(event
->pos()),
523 event
->acceptProposedAction();
525 QColumnView::dropEvent(event
);
528 void DolphinColumnView::zoomIn()
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;
538 updateDecorationSize();
542 void DolphinColumnView::zoomOut()
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;
552 updateDecorationSize();
556 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
558 m_controller
->triggerItem(index
);
559 updateColumnsState(m_controller
->url());
562 void DolphinColumnView::updateColumnsState(const KUrl
& url
)
564 foreach (QObject
* object
, viewport()->children()) {
565 if (object
->inherits("QListView")) {
566 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
567 widget
->setActive(widget
->url() == url
);
573 void DolphinColumnView::updateDecorationSize()
575 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
576 const int iconSize
= settings
->iconSize();
578 foreach (QObject
* object
, viewport()->children()) {
579 if (object
->inherits("QListView")) {
580 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
581 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
585 m_controller
->setZoomInPossible(isZoomInPossible());
586 m_controller
->setZoomOutPossible(isZoomOutPossible());
591 bool DolphinColumnView::isZoomInPossible() const
593 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
594 return settings
->iconSize() < K3Icon::SizeLarge
;
597 bool DolphinColumnView::isZoomOutPossible() const
599 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
600 return settings
->iconSize() > K3Icon::SizeSmall
;
603 void DolphinColumnView::requestActivation(QWidget
* column
)
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
);
611 m_controller
->setUrl(widget
->url());
617 void DolphinColumnView::requestSelectionModel(QAbstractItemView
* view
)
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();
625 widget
->releaseSelectionModel();
631 void DolphinColumnView::selectActiveColumn(QItemSelectionModel::SelectionFlags flags
)
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...
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();
646 widget
->releaseSelectionModel();
651 QItemSelectionModel
* selModel
= selectionModel();
653 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
654 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
655 KDirLister
* dirLister
= dirModel
->dirLister();
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
);
664 #include "dolphincolumnview.moc"