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>
39 * Represents one column inside the DolphinColumnView and has been
40 * extended to respect view options and hovering information.
42 class ColumnWidget
: public QListView
45 ColumnWidget(QWidget
* parent
,
46 DolphinColumnView
* columnView
,
48 virtual ~ColumnWidget();
50 /** Sets the size of the icons. */
51 void setDecorationSize(const QSize
& size
);
54 * An active column is defined as column, which shows the same URL
55 * as indicated by the URL navigator. The active column is usually
56 * drawn in a lighter color. All operations are applied to this column.
58 void setActive(bool active
);
59 inline bool isActive() const;
62 * Sets the directory URL of the child column that is shown next to
63 * this column. This property is only used for a visual indication
64 * of the shown directory, it does not trigger a loading of the model.
66 inline void setChildUrl(const KUrl
& url
);
67 inline const KUrl
& childUrl() const;
70 * Returns the directory URL that is shown inside the column widget.
72 inline const KUrl
& url() const;
75 virtual QStyleOptionViewItem
viewOptions() const;
76 virtual void dragEnterEvent(QDragEnterEvent
* event
);
77 virtual void dragLeaveEvent(QDragLeaveEvent
* event
);
78 virtual void dragMoveEvent(QDragMoveEvent
* event
);
79 virtual void dropEvent(QDropEvent
* event
);
80 virtual void paintEvent(QPaintEvent
* event
);
81 virtual void mousePressEvent(QMouseEvent
* event
);
82 virtual void contextMenuEvent(QContextMenuEvent
* event
);
83 virtual void selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
);
86 /** Used by ColumnWidget::setActive(). */
89 /** Used by ColumnWidget::setActive(). */
94 DolphinColumnView
* m_view
;
95 KUrl m_url
; // URL of the directory that is shown
96 KUrl m_childUrl
; // URL of the next column that is shown
97 QStyleOptionViewItem m_viewOptions
;
99 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
100 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
103 ColumnWidget::ColumnWidget(QWidget
* parent
,
104 DolphinColumnView
* columnView
,
106 QListView(columnView
),
114 setMouseTracking(true);
115 viewport()->setAttribute(Qt::WA_Hover
);
116 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
117 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
118 setSelectionMode(QAbstractItemView::ExtendedSelection
);
120 // apply the column mode settings to the widget
121 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
122 Q_ASSERT(settings
!= 0);
124 m_viewOptions
= QListView::viewOptions();
126 QFont
font(settings
->fontFamily(), settings
->fontSize());
127 font
.setItalic(settings
->italicFont());
128 font
.setBold(settings
->boldFont());
129 m_viewOptions
.font
= font
;
131 const int iconSize
= settings
->iconSize();
132 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
134 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
135 setItemDelegate(delegate
);
139 connect(this, SIGNAL(entered(const QModelIndex
&)),
140 m_view
->m_controller
, SLOT(emitItemEntered(const QModelIndex
&)));
141 connect(this, SIGNAL(viewportEntered()),
142 m_view
->m_controller
, SLOT(emitViewportEntered()));
145 ColumnWidget::~ColumnWidget()
149 void ColumnWidget::setDecorationSize(const QSize
& size
)
151 m_viewOptions
.decorationSize
= size
;
155 void ColumnWidget::setActive(bool active
)
157 if (m_active
== active
) {
170 inline bool ColumnWidget::isActive() const
175 inline void ColumnWidget::setChildUrl(const KUrl
& url
)
180 inline const KUrl
& ColumnWidget::childUrl() const
185 const KUrl
& ColumnWidget::url() const
190 QStyleOptionViewItem
ColumnWidget::viewOptions() const
192 return m_viewOptions
;
195 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
197 if (event
->mimeData()->hasUrls()) {
198 event
->acceptProposedAction();
204 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
206 QListView::dragLeaveEvent(event
);
208 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
210 setDirtyRegion(m_dropRect
);
213 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
215 QListView::dragMoveEvent(event
);
217 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
218 const QModelIndex index
= indexAt(event
->pos());
219 setDirtyRegion(m_dropRect
);
220 m_dropRect
= visualRect(index
);
221 setDirtyRegion(m_dropRect
);
224 void ColumnWidget::dropEvent(QDropEvent
* event
)
226 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
227 if (!urls
.isEmpty()) {
228 event
->acceptProposedAction();
229 m_view
->m_controller
->indicateDroppedUrls(urls
,
230 indexAt(event
->pos()),
233 QListView::dropEvent(event
);
237 void ColumnWidget::paintEvent(QPaintEvent
* event
)
239 if (!m_childUrl
.isEmpty()) {
240 // indicate the shown URL of the next column by highlighting the shown folder item
241 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(m_view
->model());
242 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
243 const QModelIndex dirIndex
= dirModel
->indexForUrl(m_childUrl
);
244 const QModelIndex proxyIndex
= proxyModel
->mapFromSource(dirIndex
);
245 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
246 const QRect itemRect
= visualRect(proxyIndex
);
247 QPainter
painter(viewport());
250 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
252 painter
.setPen(Qt::NoPen
);
253 painter
.setBrush(color
);
254 painter
.drawRect(itemRect
);
260 QListView::paintEvent(event
);
262 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
264 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
265 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
269 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
272 m_view
->requestActivation(this);
275 QListView::mousePressEvent(event
);
278 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
281 m_view
->requestActivation(this);
284 QListView::contextMenuEvent(event
);
286 const QModelIndex index
= indexAt(event
->pos());
287 if (index
.isValid() || m_active
) {
288 // Only open a context menu above an item or if the mouse is above
289 // the active column.
290 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
291 m_view
->m_controller
->triggerContextMenuRequest(pos
);
295 void ColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
297 QListView::selectionChanged(selected
, deselected
);
299 QItemSelectionModel
* selModel
= m_view
->selectionModel();
300 selModel
->select(selected
, QItemSelectionModel::Select
);
301 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
304 void ColumnWidget::activate()
306 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
307 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
308 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
309 if (KGlobalSettings::singleClick()) {
310 connect(this, SIGNAL(clicked(const QModelIndex
&)),
311 m_view
, SLOT(triggerItem(const QModelIndex
&)));
313 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
314 m_view
, SLOT(triggerItem(const QModelIndex
&)));
317 const QColor bgColor
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
318 QPalette palette
= viewport()->palette();
319 palette
.setColor(viewport()->backgroundRole(), bgColor
);
320 viewport()->setPalette(palette
);
325 void ColumnWidget::deactivate()
327 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
328 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
329 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
330 if (KGlobalSettings::singleClick()) {
331 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
332 m_view
, SLOT(triggerItem(const QModelIndex
&)));
334 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
335 m_view
, SLOT(triggerItem(const QModelIndex
&)));
338 QColor bgColor
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
339 const QColor fgColor
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
340 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
342 QPalette palette
= viewport()->palette();
343 palette
.setColor(viewport()->backgroundRole(), bgColor
);
344 viewport()->setPalette(palette
);
346 selectionModel()->clear();
353 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
354 QAbstractItemView(parent
),
355 m_controller(controller
),
361 Q_ASSERT(controller
!= 0);
363 setAcceptDrops(true);
364 setDragDropMode(QAbstractItemView::DragDrop
);
365 setDropIndicatorShown(false);
366 setSelectionMode(ExtendedSelection
);
368 connect(this, SIGNAL(entered(const QModelIndex
&)),
369 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
370 connect(this, SIGNAL(viewportEntered()),
371 controller
, SLOT(emitViewportEntered()));
372 connect(controller
, SIGNAL(zoomIn()),
373 this, SLOT(zoomIn()));
374 connect(controller
, SIGNAL(zoomOut()),
375 this, SLOT(zoomOut()));
376 connect(controller
, SIGNAL(urlChanged(const KUrl
&)),
377 this, SLOT(showColumn(const KUrl
&)));
379 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
380 this, SLOT(moveContentHorizontally(int)));
382 ColumnWidget
* column
= new ColumnWidget(viewport(), this, m_controller
->url());
383 m_columns
.append(column
);
384 setActiveColumnIndex(0);
386 updateDecorationSize();
388 m_animation
= new QTimeLine(500, this);
389 connect(m_animation
, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
392 DolphinColumnView::~DolphinColumnView()
396 QModelIndex
DolphinColumnView::indexAt(const QPoint
& point
) const
398 foreach (ColumnWidget
* column
, m_columns
) {
399 const QPoint topLeft
= column
->frameGeometry().topLeft();
400 const QPoint
adjustedPoint(point
.x() - topLeft
.x(), point
.y() - topLeft
.y());
401 QModelIndex index
= column
->indexAt(adjustedPoint
);
402 if (index
.isValid()) {
406 return activeColumn()->indexAt(point
);
408 return QModelIndex();
411 void DolphinColumnView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
413 activeColumn()->scrollTo(index
, hint
);
416 QRect
DolphinColumnView::visualRect(const QModelIndex
& index
) const
418 return activeColumn()->visualRect(index
);
421 void DolphinColumnView::setModel(QAbstractItemModel
* model
)
423 // TODO: remove all columns
425 activeColumn()->setModel(model
);
426 QAbstractItemView::setModel(model
);
429 bool DolphinColumnView::isIndexHidden(const QModelIndex
& index
) const
431 return false;//activeColumn()->isIndexHidden(index);
434 QModelIndex
DolphinColumnView::moveCursor(CursorAction cursorAction
, Qt::KeyboardModifiers modifiers
)
436 return QModelIndex(); //activeColumn()->moveCursor(cursorAction, modifiers);
439 void DolphinColumnView::setSelection(const QRect
& rect
, QItemSelectionModel::SelectionFlags flags
)
441 //activeColumn()->setSelection(rect, flags);
444 QRegion
DolphinColumnView::visualRegionForSelection(const QItemSelection
& selection
) const
446 return QRegion(); //activeColumn()->visualRegionForSelection(selection);
449 int DolphinColumnView::horizontalOffset() const
454 int DolphinColumnView::verticalOffset() const
456 return 0; // activeColumn()->verticalOffset();
459 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
461 m_controller
->triggerActivation();
462 QAbstractItemView::mousePressEvent(event
);
465 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
467 if (event
->mimeData()->hasUrls()) {
468 event
->acceptProposedAction();
472 void DolphinColumnView::dropEvent(QDropEvent
* event
)
474 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
475 if (!urls
.isEmpty()) {
476 m_controller
->indicateDroppedUrls(urls
,
477 indexAt(event
->pos()),
479 event
->acceptProposedAction();
481 QAbstractItemView::dropEvent(event
);
484 void DolphinColumnView::resizeEvent(QResizeEvent
* event
)
486 QAbstractItemView::resizeEvent(event
);
491 void DolphinColumnView::zoomIn()
493 if (isZoomInPossible()) {
494 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
495 // TODO: get rid of K3Icon sizes
496 switch (settings
->iconSize()) {
497 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
498 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
499 default: Q_ASSERT(false); break;
501 updateDecorationSize();
505 void DolphinColumnView::zoomOut()
507 if (isZoomOutPossible()) {
508 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
509 // TODO: get rid of K3Icon sizes
510 switch (settings
->iconSize()) {
511 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
512 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
513 default: Q_ASSERT(false); break;
515 updateDecorationSize();
519 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
521 m_controller
->triggerItem(index
);
523 const Qt::KeyboardModifiers modifiers
= QApplication::keyboardModifiers();
524 if ((modifiers
& Qt::ControlModifier
) || (modifiers
& Qt::ShiftModifier
)) {
528 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
529 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
530 const KFileItem item
= dirModel
->itemForIndex(proxyModel
->mapToSource(index
));
531 if ((item
.url() != activeColumn()->url()) && item
.isDir()) {
532 deleteInactiveChildColumns();
534 const KUrl
& childUrl
= m_controller
->url();
535 activeColumn()->setChildUrl(childUrl
);
537 ColumnWidget
* column
= new ColumnWidget(viewport(), this, childUrl
);
538 column
->setModel(model());
539 column
->setRootIndex(index
);
541 m_columns
.append(column
);
543 setActiveColumnIndex(m_index
+ 1);
545 // Before invoking layoutColumns() the column must be shown. To prevent
546 // a flickering the initial geometry is set to be invisible.
547 column
->setGeometry(QRect(-1, -1, 1, 1));
552 assureVisibleActiveColumn();
556 void DolphinColumnView::moveContentHorizontally(int x
)
562 void DolphinColumnView::showColumn(const KUrl
& url
)
564 if (!m_columns
[0]->url().isParentOf(url
)) {
565 // the URL is no child URL of the column view, hence do nothing
570 foreach (ColumnWidget
* column
, m_columns
) {
571 if (column
->url() == url
) {
572 // the column represents already the requested URL, hence activate it
573 requestActivation(column
);
575 } else if (!column
->url().isParentOf(url
)) {
576 // the column is no parent of the requested URL, hence it must
577 // be deleted and a new column must be loaded
578 if (columnIndex
> 0) {
579 setActiveColumnIndex(columnIndex
- 1);
580 deleteInactiveChildColumns();
583 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
584 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
585 const QModelIndex dirIndex
= dirModel
->indexForUrl(url
);
586 if (dirIndex
.isValid()) {
587 triggerItem(proxyModel
->mapFromSource(dirIndex
));
595 void DolphinColumnView::updateDecorationSize()
597 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
598 const int iconSize
= settings
->iconSize();
600 foreach (QObject
* object
, viewport()->children()) {
601 if (object
->inherits("QListView")) {
602 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
603 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
607 m_controller
->setZoomInPossible(isZoomInPossible());
608 m_controller
->setZoomOutPossible(isZoomOutPossible());
613 bool DolphinColumnView::isZoomInPossible() const
615 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
616 return settings
->iconSize() < K3Icon::SizeLarge
;
619 bool DolphinColumnView::isZoomOutPossible() const
621 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
622 return settings
->iconSize() > K3Icon::SizeSmall
;
625 void DolphinColumnView::setActiveColumnIndex(int index
)
627 if (m_index
== index
) {
631 const bool hasActiveColumn
= (m_index
>= 0);
632 if (hasActiveColumn
) {
633 m_columns
[m_index
]->setActive(false);
637 m_columns
[m_index
]->setActive(true);
640 void DolphinColumnView::layoutColumns()
643 const int columnWidth
= 250;
644 foreach (ColumnWidget
* column
, m_columns
) {
645 column
->setGeometry(QRect(x
, 0, columnWidth
, viewport()->height()));
650 void DolphinColumnView::updateScrollBar()
652 int contentWidth
= 0;
653 foreach (ColumnWidget
* column
, m_columns
) {
654 contentWidth
+= column
->width();
657 horizontalScrollBar()->setPageStep(contentWidth
);
658 horizontalScrollBar()->setRange(0, contentWidth
- viewport()->width());
661 void DolphinColumnView::assureVisibleActiveColumn()
663 const int viewportWidth
= viewport()->width();
664 const int x
= activeColumn()->x();
665 const int width
= activeColumn()->width();
666 if (x
+ width
> viewportWidth
) {
667 int newContentX
= m_contentX
- x
- width
+ viewportWidth
;
668 if (newContentX
> 0) {
671 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
672 m_animation
->start();
674 const int newContentX
= m_contentX
- x
;
675 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
676 m_animation
->start();
680 void DolphinColumnView::requestActivation(ColumnWidget
* column
)
682 if (column
->isActive()) {
683 assureVisibleActiveColumn();
686 foreach (ColumnWidget
* currColumn
, m_columns
) {
687 if (currColumn
== column
) {
688 setActiveColumnIndex(index
);
689 assureVisibleActiveColumn();
697 void DolphinColumnView::deleteInactiveChildColumns()
699 QList
<ColumnWidget
*>::iterator start
= m_columns
.begin() + m_index
+ 1;
700 QList
<ColumnWidget
*>::iterator end
= m_columns
.end();
701 for (QList
<ColumnWidget
*>::iterator it
= start
; it
!= end
; ++it
) {
702 (*it
)->deleteLater();
704 m_columns
.erase(start
, end
);
707 #include "dolphincolumnview.moc"