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 "dolphinmodel.h"
23 #include "dolphincontroller.h"
24 #include "dolphinsettings.h"
26 #include "dolphin_columnmodesettings.h"
28 #include <kcolorutils.h>
29 #include <kcolorscheme.h>
30 #include <kdirlister.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 DolphinModel
* dolphinModel
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
243 const QModelIndex dirIndex
= dolphinModel
->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 activeColumn()->setModel(model
);
424 QAbstractItemView::setModel(model
);
427 bool DolphinColumnView::isIndexHidden(const QModelIndex
& index
) const
429 return false;//activeColumn()->isIndexHidden(index);
432 QModelIndex
DolphinColumnView::moveCursor(CursorAction cursorAction
, Qt::KeyboardModifiers modifiers
)
434 return QModelIndex(); //activeColumn()->moveCursor(cursorAction, modifiers);
437 void DolphinColumnView::setSelection(const QRect
& rect
, QItemSelectionModel::SelectionFlags flags
)
439 //activeColumn()->setSelection(rect, flags);
442 QRegion
DolphinColumnView::visualRegionForSelection(const QItemSelection
& selection
) const
444 return QRegion(); //activeColumn()->visualRegionForSelection(selection);
447 int DolphinColumnView::horizontalOffset() const
452 int DolphinColumnView::verticalOffset() const
454 return 0; // activeColumn()->verticalOffset();
457 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
459 m_controller
->triggerActivation();
460 QAbstractItemView::mousePressEvent(event
);
463 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
465 if (event
->mimeData()->hasUrls()) {
466 event
->acceptProposedAction();
470 void DolphinColumnView::dropEvent(QDropEvent
* event
)
472 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
473 if (!urls
.isEmpty()) {
474 m_controller
->indicateDroppedUrls(urls
,
475 indexAt(event
->pos()),
477 event
->acceptProposedAction();
479 QAbstractItemView::dropEvent(event
);
482 void DolphinColumnView::resizeEvent(QResizeEvent
* event
)
484 QAbstractItemView::resizeEvent(event
);
489 void DolphinColumnView::zoomIn()
491 if (isZoomInPossible()) {
492 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
493 // TODO: get rid of K3Icon sizes
494 switch (settings
->iconSize()) {
495 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
496 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
497 default: Q_ASSERT(false); break;
499 updateDecorationSize();
503 void DolphinColumnView::zoomOut()
505 if (isZoomOutPossible()) {
506 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
507 // TODO: get rid of K3Icon sizes
508 switch (settings
->iconSize()) {
509 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
510 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
511 default: Q_ASSERT(false); break;
513 updateDecorationSize();
517 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
519 m_controller
->triggerItem(index
);
521 const Qt::KeyboardModifiers modifiers
= QApplication::keyboardModifiers();
522 if ((modifiers
& Qt::ControlModifier
) || (modifiers
& Qt::ShiftModifier
)) {
526 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
527 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
528 const KFileItem item
= dirModel
->itemForIndex(proxyModel
->mapToSource(index
));
529 if ((item
.url() != activeColumn()->url()) && item
.isDir()) {
530 deleteInactiveChildColumns();
532 const KUrl
& childUrl
= m_controller
->url();
533 activeColumn()->setChildUrl(childUrl
);
535 ColumnWidget
* column
= new ColumnWidget(viewport(), this, childUrl
);
536 column
->setModel(model());
537 column
->setRootIndex(index
);
539 m_columns
.append(column
);
541 setActiveColumnIndex(m_index
+ 1);
543 // Before invoking layoutColumns() the column must be shown. To prevent
544 // a flickering the initial geometry is set to be invisible.
545 column
->setGeometry(QRect(-1, -1, 1, 1));
550 assureVisibleActiveColumn();
554 void DolphinColumnView::moveContentHorizontally(int x
)
560 void DolphinColumnView::showColumn(const KUrl
& url
)
562 if (!m_columns
[0]->url().isParentOf(url
)) {
563 // the URL is no child URL of the column view, hence do nothing
568 foreach (ColumnWidget
* column
, m_columns
) {
569 if (column
->url() == url
) {
570 // the column represents already the requested URL, hence activate it
571 requestActivation(column
);
573 } else if (!column
->url().isParentOf(url
)) {
574 // the column is no parent of the requested URL, hence it must
575 // be deleted and a new column must be loaded
576 if (columnIndex
> 0) {
577 setActiveColumnIndex(columnIndex
- 1);
578 deleteInactiveChildColumns();
581 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
582 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
583 const QModelIndex dirIndex
= dirModel
->indexForUrl(url
);
584 if (dirIndex
.isValid()) {
585 triggerItem(proxyModel
->mapFromSource(dirIndex
));
593 void DolphinColumnView::updateDecorationSize()
595 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
596 const int iconSize
= settings
->iconSize();
598 foreach (QObject
* object
, viewport()->children()) {
599 if (object
->inherits("QListView")) {
600 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
601 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
605 m_controller
->setZoomInPossible(isZoomInPossible());
606 m_controller
->setZoomOutPossible(isZoomOutPossible());
611 bool DolphinColumnView::isZoomInPossible() const
613 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
614 return settings
->iconSize() < K3Icon::SizeLarge
;
617 bool DolphinColumnView::isZoomOutPossible() const
619 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
620 return settings
->iconSize() > K3Icon::SizeSmall
;
623 void DolphinColumnView::setActiveColumnIndex(int index
)
625 if (m_index
== index
) {
629 const bool hasActiveColumn
= (m_index
>= 0);
630 if (hasActiveColumn
) {
631 m_columns
[m_index
]->setActive(false);
635 m_columns
[m_index
]->setActive(true);
638 void DolphinColumnView::layoutColumns()
641 const int columnWidth
= 250;
642 foreach (ColumnWidget
* column
, m_columns
) {
643 column
->setGeometry(QRect(x
, 0, columnWidth
, viewport()->height()));
648 void DolphinColumnView::updateScrollBar()
650 int contentWidth
= 0;
651 foreach (ColumnWidget
* column
, m_columns
) {
652 contentWidth
+= column
->width();
655 horizontalScrollBar()->setPageStep(contentWidth
);
656 horizontalScrollBar()->setRange(0, contentWidth
- viewport()->width());
659 void DolphinColumnView::assureVisibleActiveColumn()
661 const int viewportWidth
= viewport()->width();
662 const int x
= activeColumn()->x();
663 const int width
= activeColumn()->width();
664 if (x
+ width
> viewportWidth
) {
665 int newContentX
= m_contentX
- x
- width
+ viewportWidth
;
666 if (newContentX
> 0) {
669 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
670 m_animation
->start();
672 const int newContentX
= m_contentX
- x
;
673 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
674 m_animation
->start();
678 void DolphinColumnView::requestActivation(ColumnWidget
* column
)
680 if (column
->isActive()) {
681 assureVisibleActiveColumn();
684 foreach (ColumnWidget
* currColumn
, m_columns
) {
685 if (currColumn
== column
) {
686 setActiveColumnIndex(index
);
687 assureVisibleActiveColumn();
695 void DolphinColumnView::deleteInactiveChildColumns()
697 QList
<ColumnWidget
*>::iterator start
= m_columns
.begin() + m_index
+ 1;
698 QList
<ColumnWidget
*>::iterator end
= m_columns
.end();
699 for (QList
<ColumnWidget
*>::iterator it
= start
; it
!= end
; ++it
) {
700 (*it
)->deleteLater();
702 m_columns
.erase(start
, end
);
705 #include "dolphincolumnview.moc"