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());
179 void ColumnWidget::releaseSelectionModel()
181 if (selectionModel() == m_view
->selectionModel()) {
182 QItemSelectionModel
* replacementModel
= new QItemSelectionModel(model());
183 setSelectionModel(replacementModel
);
187 QStyleOptionViewItem
ColumnWidget::viewOptions() const
189 return m_viewOptions
;
192 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
194 if (event
->mimeData()->hasUrls()) {
195 event
->acceptProposedAction();
201 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
203 QListView::dragLeaveEvent(event
);
205 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
207 setDirtyRegion(m_dropRect
);
210 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
212 QListView::dragMoveEvent(event
);
214 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
215 const QModelIndex index
= indexAt(event
->pos());
216 setDirtyRegion(m_dropRect
);
217 m_dropRect
= visualRect(index
);
218 setDirtyRegion(m_dropRect
);
221 void ColumnWidget::dropEvent(QDropEvent
* event
)
223 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
224 if (!urls
.isEmpty()) {
225 event
->acceptProposedAction();
226 m_view
->m_controller
->indicateDroppedUrls(urls
,
227 indexAt(event
->pos()),
230 QListView::dropEvent(event
);
234 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
236 m_view
->requestSelectionModel(this);
238 bool swallowMousePressEvent
= false;
239 const QModelIndex index
= indexAt(event
->pos());
240 if (index
.isValid()) {
241 // A click on an item has been done. Only request an activation
242 // if the item is not a directory.
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::Select
);
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 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
433 // let the column widget be aware about its URL...
435 if (viewport()->children().count() == 0) {
436 // For the first column widget the directory lister has not been started
437 // yet, hence use the URL from the controller instead.
438 columnUrl
= m_controller
->url();
440 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
441 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
443 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
444 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
446 columnUrl
= fileItem
->url();
450 ColumnWidget
* view
= new ColumnWidget(viewport(), this, columnUrl
);
452 // The following code has been copied 1:1 from QColumnView::createColumn().
453 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
454 // QColumnView::initializeColumn() will be available for this.
456 view
->setFrameShape(QFrame::NoFrame
);
457 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
458 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
459 view
->setMinimumWidth(100);
460 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
462 // copy the 'view' behavior
463 view
->setDragDropMode(dragDropMode());
464 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
465 view
->setDropIndicatorShown(showDropIndicator());
466 view
->setAlternatingRowColors(alternatingRowColors());
467 view
->setAutoScroll(hasAutoScroll());
468 view
->setEditTriggers(editTriggers());
469 view
->setHorizontalScrollMode(horizontalScrollMode());
470 view
->setIconSize(iconSize());
471 view
->setSelectionBehavior(selectionBehavior());
472 view
->setSelectionMode(selectionMode());
473 view
->setTabKeyNavigation(tabKeyNavigation());
474 view
->setTextElideMode(textElideMode());
475 view
->setVerticalScrollMode(verticalScrollMode());
477 view
->setModel(model());
479 // set the delegate to be the columnview delegate
480 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
481 view
->setItemDelegate(itemDelegate());
484 view
->setRootIndex(index
);
486 if (model()->canFetchMore(index
)) {
487 model()->fetchMore(index
);
493 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
495 m_controller
->triggerActivation();
496 QColumnView::mousePressEvent(event
);
499 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
501 if (event
->mimeData()->hasUrls()) {
502 event
->acceptProposedAction();
506 void DolphinColumnView::dropEvent(QDropEvent
* event
)
508 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
509 if (!urls
.isEmpty()) {
510 m_controller
->indicateDroppedUrls(urls
,
511 indexAt(event
->pos()),
513 event
->acceptProposedAction();
515 QColumnView::dropEvent(event
);
518 void DolphinColumnView::zoomIn()
520 if (isZoomInPossible()) {
521 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
522 // TODO: get rid of K3Icon sizes
523 switch (settings
->iconSize()) {
524 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
525 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
526 default: Q_ASSERT(false); break;
528 updateDecorationSize();
532 void DolphinColumnView::zoomOut()
534 if (isZoomOutPossible()) {
535 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
536 // TODO: get rid of K3Icon sizes
537 switch (settings
->iconSize()) {
538 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
539 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
540 default: Q_ASSERT(false); break;
542 updateDecorationSize();
546 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
548 m_controller
->triggerItem(index
);
549 updateColumnsState(m_controller
->url());
552 void DolphinColumnView::updateColumnsState(const KUrl
& url
)
554 foreach (QObject
* object
, viewport()->children()) {
555 if (object
->inherits("QListView")) {
556 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
557 widget
->setActive(widget
->url() == url
);
563 void DolphinColumnView::updateDecorationSize()
565 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
566 const int iconSize
= settings
->iconSize();
568 foreach (QObject
* object
, viewport()->children()) {
569 if (object
->inherits("QListView")) {
570 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
571 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
575 m_controller
->setZoomInPossible(isZoomInPossible());
576 m_controller
->setZoomOutPossible(isZoomOutPossible());
581 bool DolphinColumnView::isZoomInPossible() const
583 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
584 return settings
->iconSize() < K3Icon::SizeLarge
;
587 bool DolphinColumnView::isZoomOutPossible() const
589 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
590 return settings
->iconSize() > K3Icon::SizeSmall
;
593 void DolphinColumnView::requestActivation(QWidget
* column
)
595 foreach (QObject
* object
, viewport()->children()) {
596 if (object
->inherits("QListView")) {
597 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
598 const bool isActive
= (widget
== column
);
599 widget
->setActive(isActive
);
601 m_controller
->setUrl(widget
->url());
607 void DolphinColumnView::requestSelectionModel(QAbstractItemView
* view
)
609 foreach (QObject
* object
, viewport()->children()) {
610 if (object
->inherits("QListView")) {
611 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
612 if (widget
== view
) {
613 widget
->obtainSelectionModel();
615 widget
->releaseSelectionModel();
621 #include "dolphincolumnview.moc"