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 paintEvent(QPaintEvent
* event
);
75 virtual void contextMenuEvent(QContextMenuEvent
* event
);
78 /** Used by ColumnWidget::setActive(). */
81 /** Used by ColumnWidget::setActive(). */
86 DolphinColumnView
* m_view
;
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
,
104 setMouseTracking(true);
105 viewport()->setAttribute(Qt::WA_Hover
);
107 // apply the column mode settings to the widget
108 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
109 Q_ASSERT(settings
!= 0);
111 m_viewOptions
= QListView::viewOptions();
113 QFont
font(settings
->fontFamily(), settings
->fontSize());
114 font
.setItalic(settings
->italicFont());
115 font
.setBold(settings
->boldFont());
116 m_viewOptions
.font
= font
;
118 const int iconSize
= settings
->iconSize();
119 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
124 ColumnWidget::~ColumnWidget()
128 void ColumnWidget::setDecorationSize(const QSize
& size
)
130 m_viewOptions
.decorationSize
= size
;
134 void ColumnWidget::setActive(bool active
)
136 if (m_active
== active
) {
149 inline bool ColumnWidget::isActive() const
154 const KUrl
& ColumnWidget::url() const
159 void ColumnWidget::updateSelection(const KUrl
& url
)
161 setSelectionMode(SingleSelection
);
162 QItemSelectionModel
* selModel
= selectionModel();
168 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(m_view
->model());
169 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
170 const QModelIndex dirIndex
= dirModel
->indexForUrl(url
);
171 const QModelIndex proxyIndex
= proxyModel
->mapFromSource(dirIndex
);
173 const QItemSelection selection
= selModel
->selection();
174 const bool isIndexSelected
= selModel
->isSelected(proxyIndex
);
176 if (!m_active
&& ((selection
.count() > 1) || !isIndexSelected
)) {
179 if (!isIndexSelected
) {
180 selModel
->select(proxyIndex
, QItemSelectionModel::Select
);
184 QStyleOptionViewItem
ColumnWidget::viewOptions() const
186 return m_viewOptions
;
189 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
191 if (event
->mimeData()->hasUrls()) {
192 event
->acceptProposedAction();
198 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
200 QListView::dragLeaveEvent(event
);
202 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
204 setDirtyRegion(m_dropRect
);
207 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
209 QListView::dragMoveEvent(event
);
211 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
212 const QModelIndex index
= indexAt(event
->pos());
213 setDirtyRegion(m_dropRect
);
214 m_dropRect
= visualRect(index
);
215 setDirtyRegion(m_dropRect
);
218 void ColumnWidget::dropEvent(QDropEvent
* event
)
220 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
221 if (!urls
.isEmpty()) {
222 event
->acceptProposedAction();
223 m_view
->m_controller
->indicateDroppedUrls(urls
,
224 indexAt(event
->pos()),
227 QListView::dropEvent(event
);
231 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
234 selectionModel()->clear();
235 QListView::mousePressEvent(event
);
239 QListView::mousePressEvent(event
);
241 const QModelIndex index
= indexAt(event
->pos());
243 bool requestActivation
= false;
244 if (index
.isValid()) {
245 // A click on an item has been done. Only request an activation
246 // if the item is not a directory.
247 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(m_view
->model());
248 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
249 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
250 KFileItem
* item
= dirModel
->itemForIndex(dirIndex
);
251 requestActivation
= (item
!= 0) && !item
->isDir();
253 // a click on the viewport has been done
254 requestActivation
= true;
257 if (requestActivation
) {
258 m_view
->requestActivation(this);
260 m_view
->updateSelections();
264 void ColumnWidget::paintEvent(QPaintEvent
* event
)
266 QListView::paintEvent(event
);
268 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
270 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
271 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
275 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
278 m_view
->requestActivation(this);
281 QListView::contextMenuEvent(event
);
283 const QModelIndex index
= indexAt(event
->pos());
284 if (index
.isValid() || m_active
) {
285 // Only open a context menu above an item or if the mouse is above
286 // the active column.
287 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
288 m_view
->m_controller
->triggerContextMenuRequest(pos
);
292 void ColumnWidget::activate()
294 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
295 QPalette palette
= viewport()->palette();
296 palette
.setColor(viewport()->backgroundRole(), bgColor
);
297 viewport()->setPalette(palette
);
302 void ColumnWidget::deactivate()
304 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
305 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
306 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
308 QPalette palette
= viewport()->palette();
309 palette
.setColor(viewport()->backgroundRole(), bgColor
);
310 viewport()->setPalette(palette
);
317 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
319 m_controller(controller
)
321 Q_ASSERT(controller
!= 0);
323 setAcceptDrops(true);
324 setDragDropMode(QAbstractItemView::DragDrop
);
325 setDropIndicatorShown(false);
326 setSelectionMode(SingleSelection
);
328 if (KGlobalSettings::singleClick()) {
329 connect(this, SIGNAL(clicked(const QModelIndex
&)),
330 this, SLOT(triggerItem(const QModelIndex
&)));
332 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
333 this, SLOT(triggerItem(const QModelIndex
&)));
335 connect(this, SIGNAL(entered(const QModelIndex
&)),
336 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
337 connect(this, SIGNAL(viewportEntered()),
338 controller
, SLOT(emitViewportEntered()));
339 connect(controller
, SIGNAL(zoomIn()),
340 this, SLOT(zoomIn()));
341 connect(controller
, SIGNAL(zoomOut()),
342 this, SLOT(zoomOut()));
343 connect(controller
, SIGNAL(urlChanged(const KUrl
&)),
344 this, SLOT(updateColumnsState(const KUrl
&)));
346 updateDecorationSize();
349 DolphinColumnView::~DolphinColumnView()
353 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
355 // let the column widget be aware about its URL...
357 if (viewport()->children().count() == 0) {
358 // For the first column widget the directory lister has not been started
359 // yet, hence use the URL from the controller instead.
360 columnUrl
= m_controller
->url();
362 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
363 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
365 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
366 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
368 columnUrl
= fileItem
->url();
372 ColumnWidget
* view
= new ColumnWidget(viewport(), this, columnUrl
);
374 // The following code has been copied 1:1 from QColumnView::createColumn().
375 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
376 // QColumnView::initializeColumn() will be available for this.
378 view
->setFrameShape(QFrame::NoFrame
);
379 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
380 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
381 view
->setMinimumWidth(100);
382 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
384 // copy the 'view' behavior
385 view
->setDragDropMode(dragDropMode());
386 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
387 view
->setDropIndicatorShown(showDropIndicator());
388 view
->setAlternatingRowColors(alternatingRowColors());
389 view
->setAutoScroll(hasAutoScroll());
390 view
->setEditTriggers(editTriggers());
391 view
->setHorizontalScrollMode(horizontalScrollMode());
392 view
->setIconSize(iconSize());
393 view
->setSelectionBehavior(selectionBehavior());
394 view
->setSelectionMode(selectionMode());
395 view
->setTabKeyNavigation(tabKeyNavigation());
396 view
->setTextElideMode(textElideMode());
397 view
->setVerticalScrollMode(verticalScrollMode());
399 view
->setModel(model());
401 // set the delegate to be the columnview delegate
402 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
403 view
->setItemDelegate(itemDelegate());
406 view
->setRootIndex(index
);
408 if (model()->canFetchMore(index
)) {
409 model()->fetchMore(index
);
415 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
417 m_controller
->triggerActivation();
418 QColumnView::mousePressEvent(event
);
421 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
423 if (event
->mimeData()->hasUrls()) {
424 event
->acceptProposedAction();
428 void DolphinColumnView::dropEvent(QDropEvent
* event
)
430 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
431 if (!urls
.isEmpty()) {
432 m_controller
->indicateDroppedUrls(urls
,
433 indexAt(event
->pos()),
435 event
->acceptProposedAction();
437 QColumnView::dropEvent(event
);
440 void DolphinColumnView::showEvent(QShowEvent
* event
)
442 QColumnView::showEvent(event
);
443 if (!event
->spontaneous()) {
444 // QColumnView might clear the selection for folders that are shown in the next column.
445 // As this is not wanted the selection is updated if the directory lister has been completed.
446 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
447 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
448 KDirLister
* dirLister
= dirModel
->dirLister();
449 connect(dirLister
, SIGNAL(completed()),
450 this, SLOT(updateSelections()));
454 void DolphinColumnView::zoomIn()
456 if (isZoomInPossible()) {
457 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
458 // TODO: get rid of K3Icon sizes
459 switch (settings
->iconSize()) {
460 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
461 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
462 default: Q_ASSERT(false); break;
464 updateDecorationSize();
468 void DolphinColumnView::zoomOut()
470 if (isZoomOutPossible()) {
471 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
472 // TODO: get rid of K3Icon sizes
473 switch (settings
->iconSize()) {
474 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
475 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
476 default: Q_ASSERT(false); break;
478 updateDecorationSize();
482 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
484 m_controller
->triggerItem(index
);
485 updateColumnsState(m_controller
->url());
488 void DolphinColumnView::updateColumnsState(const KUrl
& url
)
490 foreach (QObject
* object
, viewport()->children()) {
491 if (object
->inherits("QListView")) {
492 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
493 widget
->setActive(widget
->url() == url
);
499 void DolphinColumnView::updateDecorationSize()
501 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
502 const int iconSize
= settings
->iconSize();
504 foreach (QObject
* object
, viewport()->children()) {
505 if (object
->inherits("QListView")) {
506 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
507 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
511 m_controller
->setZoomInPossible(isZoomInPossible());
512 m_controller
->setZoomOutPossible(isZoomOutPossible());
517 void DolphinColumnView::updateSelections()
519 ColumnWidget
* previousWidget
= 0;
520 foreach (QObject
* object
, viewport()->children()) {
521 if (object
->inherits("QListView")) {
522 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
523 if (previousWidget
!= 0) {
524 previousWidget
->updateSelection(widget
->url());
526 previousWidget
= widget
;
529 if (previousWidget
!= 0) {
530 previousWidget
->updateSelection(KUrl());
534 bool DolphinColumnView::isZoomInPossible() const
536 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
537 return settings
->iconSize() < K3Icon::SizeLarge
;
540 bool DolphinColumnView::isZoomOutPossible() const
542 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
543 return settings
->iconSize() > K3Icon::SizeSmall
;
546 void DolphinColumnView::requestActivation(QWidget
* column
)
548 foreach (QObject
* object
, viewport()->children()) {
549 if (object
->inherits("QListView")) {
550 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
551 const bool isActive
= (widget
== column
);
552 widget
->setActive(isActive
);
554 m_controller
->setUrl(widget
->url());
561 #include "dolphincolumnview.moc"