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 virtual void selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
);
76 /** Used by ColumnWidget::setActive(). */
79 /** Used by ColumnWidget::setActive(). */
84 bool m_swallowMouseMoveEvents
;
85 DolphinColumnView
* m_view
;
87 KUrl m_childUrl
; // URL of the next column that is shown
88 QStyleOptionViewItem m_viewOptions
;
90 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
91 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
94 ColumnWidget::ColumnWidget(QWidget
* parent
,
95 DolphinColumnView
* columnView
,
99 m_swallowMouseMoveEvents(false),
106 setMouseTracking(true);
107 viewport()->setAttribute(Qt::WA_Hover
);
109 // apply the column mode settings to the widget
110 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
111 Q_ASSERT(settings
!= 0);
113 m_viewOptions
= QListView::viewOptions();
115 QFont
font(settings
->fontFamily(), settings
->fontSize());
116 font
.setItalic(settings
->italicFont());
117 font
.setBold(settings
->boldFont());
118 m_viewOptions
.font
= font
;
120 const int iconSize
= settings
->iconSize();
121 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
126 ColumnWidget::~ColumnWidget()
130 void ColumnWidget::setDecorationSize(const QSize
& size
)
132 m_viewOptions
.decorationSize
= size
;
136 void ColumnWidget::setActive(bool active
)
138 if (m_active
== active
) {
151 inline bool ColumnWidget::isActive() const
156 const KUrl
& ColumnWidget::url() const
161 QStyleOptionViewItem
ColumnWidget::viewOptions() const
163 return m_viewOptions
;
166 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
168 if (event
->mimeData()->hasUrls()) {
169 event
->acceptProposedAction();
175 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
177 QListView::dragLeaveEvent(event
);
179 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
181 setDirtyRegion(m_dropRect
);
184 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
186 QListView::dragMoveEvent(event
);
188 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
189 const QModelIndex index
= indexAt(event
->pos());
190 setDirtyRegion(m_dropRect
);
191 m_dropRect
= visualRect(index
);
192 setDirtyRegion(m_dropRect
);
195 void ColumnWidget::dropEvent(QDropEvent
* event
)
197 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
198 if (!urls
.isEmpty()) {
199 event
->acceptProposedAction();
200 m_view
->m_controller
->indicateDroppedUrls(urls
,
201 indexAt(event
->pos()),
204 QListView::dropEvent(event
);
208 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
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();
221 viewport()->update();
223 m_view
->requestActivation(this);
227 // a click on the viewport has been done
228 m_view
->requestActivation(this);
230 // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
231 // triggers an unwanted loading of directories on hovering folder items.
232 m_swallowMouseMoveEvents
= true;
235 QListView::mousePressEvent(event
);
238 void ColumnWidget::mouseMoveEvent(QMouseEvent
* event
)
240 // see description in ColumnView::mousePressEvent()
241 if (!m_swallowMouseMoveEvents
) {
242 QListView::mouseMoveEvent(event
);
246 void ColumnWidget::mouseReleaseEvent(QMouseEvent
* event
)
248 QListView::mouseReleaseEvent(event
);
249 m_swallowMouseMoveEvents
= false;
253 void ColumnWidget::paintEvent(QPaintEvent
* event
)
255 if (!m_childUrl
.isEmpty()) {
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() && !selectionModel()->isSelected(proxyIndex
)) {
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
);
276 QListView::paintEvent(event
);
278 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
280 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
281 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
285 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
288 m_view
->requestActivation(this);
291 QListView::contextMenuEvent(event
);
293 const QModelIndex index
= indexAt(event
->pos());
294 if (index
.isValid() || m_active
) {
295 // Only open a context menu above an item or if the mouse is above
296 // the active column.
297 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
298 m_view
->m_controller
->triggerContextMenuRequest(pos
);
302 void ColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
304 // inactive views should not have any selection
308 QListView::selectionChanged(selected
, deselected
);
311 void ColumnWidget::activate()
313 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
314 QPalette palette
= viewport()->palette();
315 palette
.setColor(viewport()->backgroundRole(), bgColor
);
316 viewport()->setPalette(palette
);
321 void ColumnWidget::deactivate()
323 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
324 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
325 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
327 QPalette palette
= viewport()->palette();
328 palette
.setColor(viewport()->backgroundRole(), bgColor
);
329 viewport()->setPalette(palette
);
336 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
338 m_controller(controller
)
340 Q_ASSERT(controller
!= 0);
342 setAcceptDrops(true);
343 setDragDropMode(QAbstractItemView::DragDrop
);
344 setDropIndicatorShown(false);
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::zoomIn()
460 if (isZoomInPossible()) {
461 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
462 // TODO: get rid of K3Icon sizes
463 switch (settings
->iconSize()) {
464 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
465 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
466 default: Q_ASSERT(false); break;
468 updateDecorationSize();
472 void DolphinColumnView::zoomOut()
474 if (isZoomOutPossible()) {
475 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
476 // TODO: get rid of K3Icon sizes
477 switch (settings
->iconSize()) {
478 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
479 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
480 default: Q_ASSERT(false); break;
482 updateDecorationSize();
486 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
488 m_controller
->triggerItem(index
);
489 updateColumnsState(m_controller
->url());
492 void DolphinColumnView::updateColumnsState(const KUrl
& url
)
494 foreach (QObject
* object
, viewport()->children()) {
495 if (object
->inherits("QListView")) {
496 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
497 widget
->setActive(widget
->url() == url
);
503 void DolphinColumnView::updateDecorationSize()
505 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
506 const int iconSize
= settings
->iconSize();
508 foreach (QObject
* object
, viewport()->children()) {
509 if (object
->inherits("QListView")) {
510 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
511 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
515 m_controller
->setZoomInPossible(isZoomInPossible());
516 m_controller
->setZoomOutPossible(isZoomOutPossible());
521 bool DolphinColumnView::isZoomInPossible() const
523 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
524 return settings
->iconSize() < K3Icon::SizeLarge
;
527 bool DolphinColumnView::isZoomOutPossible() const
529 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
530 return settings
->iconSize() > K3Icon::SizeSmall
;
533 void DolphinColumnView::requestActivation(QWidget
* column
)
535 foreach (QObject
* object
, viewport()->children()) {
536 if (object
->inherits("QListView")) {
537 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
538 const bool isActive
= (widget
== column
);
539 widget
->setActive(isActive
);
541 m_controller
->setUrl(widget
->url());
547 #include "dolphincolumnview.moc"