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
);
189 QStyleOptionViewItem
ColumnWidget::viewOptions() const
191 return m_viewOptions
;
194 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
196 if (event
->mimeData()->hasUrls()) {
197 event
->acceptProposedAction();
203 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
205 QListView::dragLeaveEvent(event
);
207 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
209 setDirtyRegion(m_dropRect
);
212 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
214 QListView::dragMoveEvent(event
);
216 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
217 const QModelIndex index
= indexAt(event
->pos());
218 setDirtyRegion(m_dropRect
);
219 m_dropRect
= visualRect(index
);
220 setDirtyRegion(m_dropRect
);
223 void ColumnWidget::dropEvent(QDropEvent
* event
)
225 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
226 if (!urls
.isEmpty()) {
227 event
->acceptProposedAction();
228 m_view
->m_controller
->indicateDroppedUrls(urls
,
229 indexAt(event
->pos()),
232 QListView::dropEvent(event
);
236 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
238 QListView::mousePressEvent(event
);
240 const QModelIndex index
= indexAt(event
->pos());
242 bool requestActivation
= false;
243 if (index
.isValid()) {
244 // A click on an item has been done. Only request an activation
245 // if the item is not a directory.
246 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(m_view
->model());
247 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
248 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
249 KFileItem
* item
= dirModel
->itemForIndex(dirIndex
);
250 requestActivation
= (item
!= 0) && !item
->isDir();
252 // a click on the viewport has been done
253 requestActivation
= true;
255 // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
256 // triggers an unwanted loading of directories on hovering folder items.
257 m_swallowMouseMoveEvents
= true;
260 if (requestActivation
) {
261 m_view
->requestActivation(this);
263 m_view
->updateSelections();
267 void ColumnWidget::mouseMoveEvent(QMouseEvent
* event
)
269 // see description in ColumnView::mousePressEvent()
270 if (!m_swallowMouseMoveEvents
) {
271 QListView::mouseMoveEvent(event
);
275 void ColumnWidget::mouseReleaseEvent(QMouseEvent
* event
)
277 QListView::mouseReleaseEvent(event
);
278 m_swallowMouseMoveEvents
= false;
282 void ColumnWidget::paintEvent(QPaintEvent
* event
)
284 QListView::paintEvent(event
);
286 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
288 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
289 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
293 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
296 m_view
->requestActivation(this);
299 QListView::contextMenuEvent(event
);
301 const QModelIndex index
= indexAt(event
->pos());
302 if (index
.isValid() || m_active
) {
303 // Only open a context menu above an item or if the mouse is above
304 // the active column.
305 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
306 m_view
->m_controller
->triggerContextMenuRequest(pos
);
310 void ColumnWidget::activate()
312 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
313 QPalette palette
= viewport()->palette();
314 palette
.setColor(viewport()->backgroundRole(), bgColor
);
315 viewport()->setPalette(palette
);
320 void ColumnWidget::deactivate()
322 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
323 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
324 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
326 QPalette palette
= viewport()->palette();
327 palette
.setColor(viewport()->backgroundRole(), bgColor
);
328 viewport()->setPalette(palette
);
335 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
337 m_controller(controller
)
339 Q_ASSERT(controller
!= 0);
341 setAcceptDrops(true);
342 setDragDropMode(QAbstractItemView::DragDrop
);
343 setDropIndicatorShown(false);
344 setSelectionMode(SingleSelection
);
346 if (KGlobalSettings::singleClick()) {
347 connect(this, SIGNAL(clicked(const QModelIndex
&)),
348 this, SLOT(triggerItem(const QModelIndex
&)));
350 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
351 this, SLOT(triggerItem(const QModelIndex
&)));
353 connect(this, SIGNAL(entered(const QModelIndex
&)),
354 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
355 connect(this, SIGNAL(viewportEntered()),
356 controller
, SLOT(emitViewportEntered()));
357 connect(controller
, SIGNAL(zoomIn()),
358 this, SLOT(zoomIn()));
359 connect(controller
, SIGNAL(zoomOut()),
360 this, SLOT(zoomOut()));
361 connect(controller
, SIGNAL(urlChanged(const KUrl
&)),
362 this, SLOT(updateColumnsState(const KUrl
&)));
364 updateDecorationSize();
367 DolphinColumnView::~DolphinColumnView()
371 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
373 // let the column widget be aware about its URL...
375 if (viewport()->children().count() == 0) {
376 // For the first column widget the directory lister has not been started
377 // yet, hence use the URL from the controller instead.
378 columnUrl
= m_controller
->url();
380 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
381 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
383 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
384 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
386 columnUrl
= fileItem
->url();
390 ColumnWidget
* view
= new ColumnWidget(viewport(), this, columnUrl
);
392 // The following code has been copied 1:1 from QColumnView::createColumn().
393 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
394 // QColumnView::initializeColumn() will be available for this.
396 view
->setFrameShape(QFrame::NoFrame
);
397 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
398 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
399 view
->setMinimumWidth(100);
400 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
402 // copy the 'view' behavior
403 view
->setDragDropMode(dragDropMode());
404 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
405 view
->setDropIndicatorShown(showDropIndicator());
406 view
->setAlternatingRowColors(alternatingRowColors());
407 view
->setAutoScroll(hasAutoScroll());
408 view
->setEditTriggers(editTriggers());
409 view
->setHorizontalScrollMode(horizontalScrollMode());
410 view
->setIconSize(iconSize());
411 view
->setSelectionBehavior(selectionBehavior());
412 view
->setSelectionMode(selectionMode());
413 view
->setTabKeyNavigation(tabKeyNavigation());
414 view
->setTextElideMode(textElideMode());
415 view
->setVerticalScrollMode(verticalScrollMode());
417 view
->setModel(model());
419 // set the delegate to be the columnview delegate
420 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
421 view
->setItemDelegate(itemDelegate());
424 view
->setRootIndex(index
);
426 if (model()->canFetchMore(index
)) {
427 model()->fetchMore(index
);
433 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
435 m_controller
->triggerActivation();
436 QColumnView::mousePressEvent(event
);
439 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
441 if (event
->mimeData()->hasUrls()) {
442 event
->acceptProposedAction();
446 void DolphinColumnView::dropEvent(QDropEvent
* event
)
448 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
449 if (!urls
.isEmpty()) {
450 m_controller
->indicateDroppedUrls(urls
,
451 indexAt(event
->pos()),
453 event
->acceptProposedAction();
455 QColumnView::dropEvent(event
);
458 void DolphinColumnView::showEvent(QShowEvent
* event
)
460 QColumnView::showEvent(event
);
461 if (!event
->spontaneous()) {
462 // QColumnView might clear the selection for folders that are shown in the next column.
463 // As this is not wanted the selection is updated if the directory lister has been completed.
464 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
465 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
466 KDirLister
* dirLister
= dirModel
->dirLister();
467 connect(dirLister
, SIGNAL(completed()),
468 this, SLOT(updateSelections()));
472 void DolphinColumnView::zoomIn()
474 if (isZoomInPossible()) {
475 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
476 // TODO: get rid of K3Icon sizes
477 switch (settings
->iconSize()) {
478 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
479 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
480 default: Q_ASSERT(false); break;
482 updateDecorationSize();
486 void DolphinColumnView::zoomOut()
488 if (isZoomOutPossible()) {
489 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
490 // TODO: get rid of K3Icon sizes
491 switch (settings
->iconSize()) {
492 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
493 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
494 default: Q_ASSERT(false); break;
496 updateDecorationSize();
500 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
502 m_controller
->triggerItem(index
);
503 updateColumnsState(m_controller
->url());
506 void DolphinColumnView::updateColumnsState(const KUrl
& url
)
508 foreach (QObject
* object
, viewport()->children()) {
509 if (object
->inherits("QListView")) {
510 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
511 widget
->setActive(widget
->url() == url
);
517 void DolphinColumnView::updateDecorationSize()
519 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
520 const int iconSize
= settings
->iconSize();
522 foreach (QObject
* object
, viewport()->children()) {
523 if (object
->inherits("QListView")) {
524 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
525 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
529 m_controller
->setZoomInPossible(isZoomInPossible());
530 m_controller
->setZoomOutPossible(isZoomOutPossible());
535 void DolphinColumnView::updateSelections()
537 ColumnWidget
* previousWidget
= 0;
538 foreach (QObject
* object
, viewport()->children()) {
539 if (object
->inherits("QListView")) {
540 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
541 if (previousWidget
!= 0) {
542 previousWidget
->updateSelection(widget
->url());
544 previousWidget
= widget
;
547 if (previousWidget
!= 0) {
548 previousWidget
->updateSelection(KUrl());
552 bool DolphinColumnView::isZoomInPossible() const
554 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
555 return settings
->iconSize() < K3Icon::SizeLarge
;
558 bool DolphinColumnView::isZoomOutPossible() const
560 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
561 return settings
->iconSize() > K3Icon::SizeSmall
;
564 void DolphinColumnView::requestActivation(QWidget
* column
)
566 foreach (QObject
* object
, viewport()->children()) {
567 if (object
->inherits("QListView")) {
568 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
569 const bool isActive
= (widget
== column
);
570 widget
->setActive(isActive
);
572 m_controller
->setUrl(widget
->url());
579 #include "dolphincolumnview.moc"