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 virtual QStyleOptionViewItem
viewOptions() const;
62 virtual void dragEnterEvent(QDragEnterEvent
* event
);
63 virtual void dragLeaveEvent(QDragLeaveEvent
* event
);
64 virtual void dragMoveEvent(QDragMoveEvent
* event
);
65 virtual void dropEvent(QDropEvent
* event
);
66 virtual void mousePressEvent(QMouseEvent
* event
);
67 virtual void mouseMoveEvent(QMouseEvent
* event
);
68 virtual void mouseReleaseEvent(QMouseEvent
* event
);
69 virtual void paintEvent(QPaintEvent
* event
);
70 virtual void contextMenuEvent(QContextMenuEvent
* event
);
73 /** Used by ColumnWidget::setActive(). */
76 /** Used by ColumnWidget::setActive(). */
81 bool m_swallowMouseMoveEvents
;
82 DolphinColumnView
* m_view
;
84 KUrl m_childUrl
; // URL of the next column that is shown
85 QStyleOptionViewItem m_viewOptions
;
87 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
88 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
91 ColumnWidget::ColumnWidget(QWidget
* parent
,
92 DolphinColumnView
* columnView
,
96 m_swallowMouseMoveEvents(false),
103 setMouseTracking(true);
104 viewport()->setAttribute(Qt::WA_Hover
);
106 // apply the column mode settings to the widget
107 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
108 Q_ASSERT(settings
!= 0);
110 m_viewOptions
= QListView::viewOptions();
112 QFont
font(settings
->fontFamily(), settings
->fontSize());
113 font
.setItalic(settings
->italicFont());
114 font
.setBold(settings
->boldFont());
115 m_viewOptions
.font
= font
;
117 const int iconSize
= settings
->iconSize();
118 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
123 ColumnWidget::~ColumnWidget()
127 void ColumnWidget::setDecorationSize(const QSize
& size
)
129 m_viewOptions
.decorationSize
= size
;
133 void ColumnWidget::setActive(bool active
)
135 if (m_active
== active
) {
148 inline bool ColumnWidget::isActive() const
153 const KUrl
& ColumnWidget::url() const
158 QStyleOptionViewItem
ColumnWidget::viewOptions() const
160 return m_viewOptions
;
163 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
165 if (event
->mimeData()->hasUrls()) {
166 event
->acceptProposedAction();
172 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
174 QListView::dragLeaveEvent(event
);
176 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
178 setDirtyRegion(m_dropRect
);
181 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
183 QListView::dragMoveEvent(event
);
185 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
186 const QModelIndex index
= indexAt(event
->pos());
187 setDirtyRegion(m_dropRect
);
188 m_dropRect
= visualRect(index
);
189 setDirtyRegion(m_dropRect
);
192 void ColumnWidget::dropEvent(QDropEvent
* event
)
194 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
195 if (!urls
.isEmpty()) {
196 event
->acceptProposedAction();
197 m_view
->m_controller
->indicateDroppedUrls(urls
,
198 indexAt(event
->pos()),
201 QListView::dropEvent(event
);
205 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
207 QListView::mousePressEvent(event
);
209 bool requestActivation
= false;
210 const QModelIndex index
= indexAt(event
->pos());
211 if (index
.isValid()) {
212 // A click on an item has been done. Only request an activation
213 // if the item is not a directory.
214 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(m_view
->model());
215 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
216 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
217 KFileItem
* item
= dirModel
->itemForIndex(dirIndex
);
220 m_childUrl
= item
->url();
222 requestActivation
= true;
226 // a click on the viewport has been done
227 requestActivation
= true;
229 // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
230 // triggers an unwanted loading of directories on hovering folder items.
231 m_swallowMouseMoveEvents
= true;
234 if (requestActivation
) {
235 m_view
->requestActivation(this);
239 void ColumnWidget::mouseMoveEvent(QMouseEvent
* event
)
241 // see description in ColumnView::mousePressEvent()
242 if (!m_swallowMouseMoveEvents
) {
243 QListView::mouseMoveEvent(event
);
247 void ColumnWidget::mouseReleaseEvent(QMouseEvent
* event
)
249 QListView::mouseReleaseEvent(event
);
250 m_swallowMouseMoveEvents
= false;
254 void ColumnWidget::paintEvent(QPaintEvent
* event
)
256 // indicate the shown URL of the next column by highlighting the shown folder item
257 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(m_view
->model());
258 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
259 const QModelIndex dirIndex
= dirModel
->indexForUrl(m_childUrl
);
260 const QModelIndex proxyIndex
= proxyModel
->mapFromSource(dirIndex
);
261 if (proxyIndex
.isValid()) {
262 const QRect itemRect
= visualRect(proxyIndex
);
263 QPainter
painter(viewport());
266 QColor color
= KColorScheme(KColorScheme::View
).foreground();
268 painter
.setPen(Qt::NoPen
);
269 painter
.setBrush(color
);
270 painter
.drawRect(itemRect
);
275 QListView::paintEvent(event
);
277 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
279 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
280 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
284 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
287 m_view
->requestActivation(this);
290 QListView::contextMenuEvent(event
);
292 const QModelIndex index
= indexAt(event
->pos());
293 if (index
.isValid() || m_active
) {
294 // Only open a context menu above an item or if the mouse is above
295 // the active column.
296 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
297 m_view
->m_controller
->triggerContextMenuRequest(pos
);
301 void ColumnWidget::activate()
303 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
304 QPalette palette
= viewport()->palette();
305 palette
.setColor(viewport()->backgroundRole(), bgColor
);
306 viewport()->setPalette(palette
);
311 void ColumnWidget::deactivate()
313 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
314 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
315 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
317 QPalette palette
= viewport()->palette();
318 palette
.setColor(viewport()->backgroundRole(), bgColor
);
319 viewport()->setPalette(palette
);
326 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
328 m_controller(controller
)
330 Q_ASSERT(controller
!= 0);
332 setAcceptDrops(true);
333 setDragDropMode(QAbstractItemView::DragDrop
);
334 setDropIndicatorShown(false);
336 if (KGlobalSettings::singleClick()) {
337 connect(this, SIGNAL(clicked(const QModelIndex
&)),
338 this, SLOT(triggerItem(const QModelIndex
&)));
340 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
341 this, SLOT(triggerItem(const QModelIndex
&)));
343 connect(this, SIGNAL(entered(const QModelIndex
&)),
344 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
345 connect(this, SIGNAL(viewportEntered()),
346 controller
, SLOT(emitViewportEntered()));
347 connect(controller
, SIGNAL(zoomIn()),
348 this, SLOT(zoomIn()));
349 connect(controller
, SIGNAL(zoomOut()),
350 this, SLOT(zoomOut()));
351 connect(controller
, SIGNAL(urlChanged(const KUrl
&)),
352 this, SLOT(updateColumnsState(const KUrl
&)));
354 updateDecorationSize();
357 DolphinColumnView::~DolphinColumnView()
361 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
363 // let the column widget be aware about its URL...
365 if (viewport()->children().count() == 0) {
366 // For the first column widget the directory lister has not been started
367 // yet, hence use the URL from the controller instead.
368 columnUrl
= m_controller
->url();
370 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
371 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
373 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
374 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
376 columnUrl
= fileItem
->url();
380 ColumnWidget
* view
= new ColumnWidget(viewport(), this, columnUrl
);
382 // The following code has been copied 1:1 from QColumnView::createColumn().
383 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
384 // QColumnView::initializeColumn() will be available for this.
386 view
->setFrameShape(QFrame::NoFrame
);
387 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
388 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
389 view
->setMinimumWidth(100);
390 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
392 // copy the 'view' behavior
393 view
->setDragDropMode(dragDropMode());
394 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
395 view
->setDropIndicatorShown(showDropIndicator());
396 view
->setAlternatingRowColors(alternatingRowColors());
397 view
->setAutoScroll(hasAutoScroll());
398 view
->setEditTriggers(editTriggers());
399 view
->setHorizontalScrollMode(horizontalScrollMode());
400 view
->setIconSize(iconSize());
401 view
->setSelectionBehavior(selectionBehavior());
402 view
->setSelectionMode(selectionMode());
403 view
->setTabKeyNavigation(tabKeyNavigation());
404 view
->setTextElideMode(textElideMode());
405 view
->setVerticalScrollMode(verticalScrollMode());
407 view
->setModel(model());
409 // set the delegate to be the columnview delegate
410 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
411 view
->setItemDelegate(itemDelegate());
414 view
->setRootIndex(index
);
416 if (model()->canFetchMore(index
)) {
417 model()->fetchMore(index
);
423 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
425 m_controller
->triggerActivation();
426 QColumnView::mousePressEvent(event
);
429 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
431 if (event
->mimeData()->hasUrls()) {
432 event
->acceptProposedAction();
436 void DolphinColumnView::dropEvent(QDropEvent
* event
)
438 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
439 if (!urls
.isEmpty()) {
440 m_controller
->indicateDroppedUrls(urls
,
441 indexAt(event
->pos()),
443 event
->acceptProposedAction();
445 QColumnView::dropEvent(event
);
448 void DolphinColumnView::zoomIn()
450 if (isZoomInPossible()) {
451 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
452 // TODO: get rid of K3Icon sizes
453 switch (settings
->iconSize()) {
454 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
455 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
456 default: Q_ASSERT(false); break;
458 updateDecorationSize();
462 void DolphinColumnView::zoomOut()
464 if (isZoomOutPossible()) {
465 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
466 // TODO: get rid of K3Icon sizes
467 switch (settings
->iconSize()) {
468 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
469 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
470 default: Q_ASSERT(false); break;
472 updateDecorationSize();
476 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
478 m_controller
->triggerItem(index
);
479 updateColumnsState(m_controller
->url());
482 void DolphinColumnView::updateColumnsState(const KUrl
& url
)
484 foreach (QObject
* object
, viewport()->children()) {
485 if (object
->inherits("QListView")) {
486 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
487 widget
->setActive(widget
->url() == url
);
493 void DolphinColumnView::updateDecorationSize()
495 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
496 const int iconSize
= settings
->iconSize();
498 foreach (QObject
* object
, viewport()->children()) {
499 if (object
->inherits("QListView")) {
500 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
501 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
505 m_controller
->setZoomInPossible(isZoomInPossible());
506 m_controller
->setZoomOutPossible(isZoomOutPossible());
511 bool DolphinColumnView::isZoomInPossible() const
513 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
514 return settings
->iconSize() < K3Icon::SizeLarge
;
517 bool DolphinColumnView::isZoomOutPossible() const
519 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
520 return settings
->iconSize() > K3Icon::SizeSmall
;
523 void DolphinColumnView::requestActivation(QWidget
* column
)
525 foreach (QObject
* object
, viewport()->children()) {
526 if (object
->inherits("QListView")) {
527 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
528 const bool isActive
= (widget
== column
);
529 widget
->setActive(isActive
);
531 m_controller
->setUrl(widget
->url());
537 #include "dolphincolumnview.moc"