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>
36 * Represents one column inside the DolphinColumnView and has been
37 * extended to respect view options and hovering information.
39 class ColumnWidget
: public QListView
42 ColumnWidget(QWidget
* parent
,
43 DolphinColumnView
* columnView
,
45 virtual ~ColumnWidget();
47 /** Sets the size of the icons. */
48 void setDecorationSize(const QSize
& size
);
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.
55 void setActive(bool active
);
56 inline bool isActive() const;
58 inline const KUrl
& url() const;
61 * Updates the selection that the folder gets selected which represents
62 * the URL \a url. If \a url is empty, the selection of the column widget
65 void updateSelection(const KUrl
& url
);
68 virtual QStyleOptionViewItem
viewOptions() const;
69 virtual void dragEnterEvent(QDragEnterEvent
* event
);
70 virtual void dragLeaveEvent(QDragLeaveEvent
* event
);
71 virtual void dragMoveEvent(QDragMoveEvent
* event
);
72 virtual void dropEvent(QDropEvent
* event
);
73 virtual void mousePressEvent(QMouseEvent
* event
);
74 virtual void mouseMoveEvent(QMouseEvent
* event
);
75 virtual void mouseReleaseEvent(QMouseEvent
* event
);
76 virtual void paintEvent(QPaintEvent
* event
);
77 virtual void contextMenuEvent(QContextMenuEvent
* event
);
80 /** Used by ColumnWidget::setActive(). */
83 /** Used by ColumnWidget::setActive(). */
88 bool m_swallowMouseMoveEvents
;;
89 DolphinColumnView
* m_view
;
91 QStyleOptionViewItem m_viewOptions
;
93 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
94 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
97 ColumnWidget::ColumnWidget(QWidget
* parent
,
98 DolphinColumnView
* columnView
,
102 m_swallowMouseMoveEvents(false),
108 setMouseTracking(true);
109 viewport()->setAttribute(Qt::WA_Hover
);
111 // apply the column mode settings to the widget
112 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
113 Q_ASSERT(settings
!= 0);
115 m_viewOptions
= QListView::viewOptions();
117 QFont
font(settings
->fontFamily(), settings
->fontSize());
118 font
.setItalic(settings
->italicFont());
119 font
.setBold(settings
->boldFont());
120 m_viewOptions
.font
= font
;
122 const int iconSize
= settings
->iconSize();
123 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
128 ColumnWidget::~ColumnWidget()
132 void ColumnWidget::setDecorationSize(const QSize
& size
)
134 m_viewOptions
.decorationSize
= size
;
138 void ColumnWidget::setActive(bool active
)
140 if (m_active
== active
) {
153 inline bool ColumnWidget::isActive() const
158 const KUrl
& ColumnWidget::url() const
163 void ColumnWidget::updateSelection(const KUrl
& url
)
165 setSelectionMode(SingleSelection
);
166 QItemSelectionModel
* selModel
= selectionModel();
172 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(m_view
->model());
173 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
174 const QModelIndex dirIndex
= dirModel
->indexForUrl(url
);
175 const QModelIndex proxyIndex
= proxyModel
->mapFromSource(dirIndex
);
177 const QItemSelection selection
= selModel
->selection();
178 const bool isIndexSelected
= selModel
->isSelected(proxyIndex
);
180 if (!m_active
&& ((selection
.count() > 1) || !isIndexSelected
)) {
183 if (!isIndexSelected
) {
184 selModel
->select(proxyIndex
, QItemSelectionModel::Select
);
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
)
238 selectionModel()->clear();
241 QListView::mousePressEvent(event
);
243 const QModelIndex index
= indexAt(event
->pos());
245 bool requestActivation
= false;
246 if (index
.isValid()) {
247 // A click on an item has been done. Only request an activation
248 // if the item is not a directory.
249 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(m_view
->model());
250 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
251 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
252 KFileItem
* item
= dirModel
->itemForIndex(dirIndex
);
253 requestActivation
= (item
!= 0) && !item
->isDir();
255 // a click on the viewport has been done
256 requestActivation
= true;
258 // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
259 // triggers an unwanted loading of directories on hovering folder items.
260 m_swallowMouseMoveEvents
= true;
263 if (requestActivation
) {
264 m_view
->requestActivation(this);
266 m_view
->updateSelections();
270 void ColumnWidget::mouseMoveEvent(QMouseEvent
* event
)
272 // see description in ColumnView::mousePressEvent()
273 if (!m_swallowMouseMoveEvents
) {
274 QListView::mouseMoveEvent(event
);
278 void ColumnWidget::mouseReleaseEvent(QMouseEvent
* event
)
280 QListView::mouseReleaseEvent(event
);
281 m_swallowMouseMoveEvents
= false;
285 void ColumnWidget::paintEvent(QPaintEvent
* event
)
287 QListView::paintEvent(event
);
289 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
291 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
292 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
296 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
299 m_view
->requestActivation(this);
302 QListView::contextMenuEvent(event
);
304 const QModelIndex index
= indexAt(event
->pos());
305 if (index
.isValid() || m_active
) {
306 // Only open a context menu above an item or if the mouse is above
307 // the active column.
308 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
309 m_view
->m_controller
->triggerContextMenuRequest(pos
);
313 void ColumnWidget::activate()
315 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
316 QPalette palette
= viewport()->palette();
317 palette
.setColor(viewport()->backgroundRole(), bgColor
);
318 viewport()->setPalette(palette
);
323 void ColumnWidget::deactivate()
325 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
326 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
327 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
329 QPalette palette
= viewport()->palette();
330 palette
.setColor(viewport()->backgroundRole(), bgColor
);
331 viewport()->setPalette(palette
);
338 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
340 m_controller(controller
)
342 Q_ASSERT(controller
!= 0);
344 setAcceptDrops(true);
345 setDragDropMode(QAbstractItemView::DragDrop
);
346 setDropIndicatorShown(false);
347 setSelectionMode(SingleSelection
);
349 if (KGlobalSettings::singleClick()) {
350 connect(this, SIGNAL(clicked(const QModelIndex
&)),
351 this, SLOT(triggerItem(const QModelIndex
&)));
353 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
354 this, SLOT(triggerItem(const QModelIndex
&)));
356 connect(this, SIGNAL(entered(const QModelIndex
&)),
357 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
358 connect(this, SIGNAL(viewportEntered()),
359 controller
, SLOT(emitViewportEntered()));
360 connect(controller
, SIGNAL(zoomIn()),
361 this, SLOT(zoomIn()));
362 connect(controller
, SIGNAL(zoomOut()),
363 this, SLOT(zoomOut()));
364 connect(controller
, SIGNAL(urlChanged(const KUrl
&)),
365 this, SLOT(updateColumnsState(const KUrl
&)));
367 updateDecorationSize();
370 DolphinColumnView::~DolphinColumnView()
374 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
376 // let the column widget be aware about its URL...
378 if (viewport()->children().count() == 0) {
379 // For the first column widget the directory lister has not been started
380 // yet, hence use the URL from the controller instead.
381 columnUrl
= m_controller
->url();
383 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
384 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
386 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
387 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
389 columnUrl
= fileItem
->url();
393 ColumnWidget
* view
= new ColumnWidget(viewport(), this, columnUrl
);
395 // The following code has been copied 1:1 from QColumnView::createColumn().
396 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
397 // QColumnView::initializeColumn() will be available for this.
399 view
->setFrameShape(QFrame::NoFrame
);
400 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
401 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
402 view
->setMinimumWidth(100);
403 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
405 // copy the 'view' behavior
406 view
->setDragDropMode(dragDropMode());
407 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
408 view
->setDropIndicatorShown(showDropIndicator());
409 view
->setAlternatingRowColors(alternatingRowColors());
410 view
->setAutoScroll(hasAutoScroll());
411 view
->setEditTriggers(editTriggers());
412 view
->setHorizontalScrollMode(horizontalScrollMode());
413 view
->setIconSize(iconSize());
414 view
->setSelectionBehavior(selectionBehavior());
415 view
->setSelectionMode(selectionMode());
416 view
->setTabKeyNavigation(tabKeyNavigation());
417 view
->setTextElideMode(textElideMode());
418 view
->setVerticalScrollMode(verticalScrollMode());
420 view
->setModel(model());
422 // set the delegate to be the columnview delegate
423 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
424 view
->setItemDelegate(itemDelegate());
427 view
->setRootIndex(index
);
429 if (model()->canFetchMore(index
)) {
430 model()->fetchMore(index
);
436 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
438 m_controller
->triggerActivation();
439 QColumnView::mousePressEvent(event
);
442 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
444 if (event
->mimeData()->hasUrls()) {
445 event
->acceptProposedAction();
449 void DolphinColumnView::dropEvent(QDropEvent
* event
)
451 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
452 if (!urls
.isEmpty()) {
453 m_controller
->indicateDroppedUrls(urls
,
454 indexAt(event
->pos()),
456 event
->acceptProposedAction();
458 QColumnView::dropEvent(event
);
461 void DolphinColumnView::showEvent(QShowEvent
* event
)
463 QColumnView::showEvent(event
);
464 if (!event
->spontaneous()) {
465 // QColumnView might clear the selection for folders that are shown in the next column.
466 // As this is not wanted the selection is updated if the directory lister has been completed.
467 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
468 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
469 KDirLister
* dirLister
= dirModel
->dirLister();
470 connect(dirLister
, SIGNAL(completed()),
471 this, SLOT(updateSelections()));
475 void DolphinColumnView::zoomIn()
477 if (isZoomInPossible()) {
478 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
479 // TODO: get rid of K3Icon sizes
480 switch (settings
->iconSize()) {
481 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
482 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
483 default: Q_ASSERT(false); break;
485 updateDecorationSize();
489 void DolphinColumnView::zoomOut()
491 if (isZoomOutPossible()) {
492 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
493 // TODO: get rid of K3Icon sizes
494 switch (settings
->iconSize()) {
495 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
496 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
497 default: Q_ASSERT(false); break;
499 updateDecorationSize();
503 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
505 m_controller
->triggerItem(index
);
506 updateColumnsState(m_controller
->url());
509 void DolphinColumnView::updateColumnsState(const KUrl
& url
)
511 foreach (QObject
* object
, viewport()->children()) {
512 if (object
->inherits("QListView")) {
513 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
514 widget
->setActive(widget
->url() == url
);
520 void DolphinColumnView::updateDecorationSize()
522 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
523 const int iconSize
= settings
->iconSize();
525 foreach (QObject
* object
, viewport()->children()) {
526 if (object
->inherits("QListView")) {
527 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
528 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
532 m_controller
->setZoomInPossible(isZoomInPossible());
533 m_controller
->setZoomOutPossible(isZoomOutPossible());
538 void DolphinColumnView::updateSelections()
540 ColumnWidget
* previousWidget
= 0;
541 foreach (QObject
* object
, viewport()->children()) {
542 if (object
->inherits("QListView")) {
543 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
544 if (previousWidget
!= 0) {
545 previousWidget
->updateSelection(widget
->url());
547 previousWidget
= widget
;
550 if (previousWidget
!= 0) {
551 previousWidget
->updateSelection(KUrl());
555 bool DolphinColumnView::isZoomInPossible() const
557 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
558 return settings
->iconSize() < K3Icon::SizeLarge
;
561 bool DolphinColumnView::isZoomOutPossible() const
563 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
564 return settings
->iconSize() > K3Icon::SizeSmall
;
567 void DolphinColumnView::requestActivation(QWidget
* column
)
569 foreach (QObject
* object
, viewport()->children()) {
570 if (object
->inherits("QListView")) {
571 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
572 const bool isActive
= (widget
== column
);
573 widget
->setActive(isActive
);
575 m_controller
->setUrl(widget
->url());
582 #include "dolphincolumnview.moc"