1 /***************************************************************************
2 * Copyright (C) 2007-2009 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 "dolphinmodel.h"
23 #include "dolphincolumnviewcontainer.h"
24 #include "dolphinviewcontroller.h"
25 #include "dolphindirlister.h"
26 #include "dolphinfileitemdelegate.h"
27 #include "dolphinsortfilterproxymodel.h"
28 #include "settings/dolphinsettings.h"
29 #include "dolphinviewautoscroller.h"
30 #include "dolphin_columnmodesettings.h"
31 #include "dolphin_generalsettings.h"
32 #include "draganddrophelper.h"
33 #include "folderexpander.h"
34 #include "tooltips/tooltipmanager.h"
35 #include "viewextensionsfactory.h"
36 #include "viewmodecontroller.h"
37 #include "zoomlevelinfo.h"
39 #include <kcolorscheme.h>
40 #include <kdirlister.h>
41 #include <kfileitem.h>
42 #include <kio/previewjob.h>
43 #include <kiconeffect.h>
45 #include <konqmimedata.h>
47 #include <QApplication>
49 #include <QHeaderView>
54 DolphinColumnView::DolphinColumnView(QWidget
* parent
,
55 DolphinColumnViewContainer
* container
,
57 DolphinTreeView(parent
),
59 m_container(container
),
60 m_extensionsFactory(0),
69 setMouseTracking(true);
71 setUniformRowHeights(true);
72 setSelectionBehavior(SelectItems
);
73 setSelectionMode(QAbstractItemView::ExtendedSelection
);
74 setDragDropMode(QAbstractItemView::DragDrop
);
75 setDropIndicatorShown(false);
76 setRootIsDecorated(false);
77 setItemsExpandable(false);
78 setEditTriggers(QAbstractItemView::NoEditTriggers
);
80 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
81 Q_ASSERT(settings
!= 0);
83 if (settings
->useSystemFont()) {
84 m_font
= KGlobalSettings::generalFont();
86 m_font
= QFont(settings
->fontFamily(),
87 qRound(settings
->fontSize()),
88 settings
->fontWeight(),
89 settings
->italicFont());
90 m_font
.setPointSizeF(settings
->fontSize());
93 connect(this, SIGNAL(viewportEntered()),
94 m_container
->m_dolphinViewController
, SLOT(emitViewportEntered()));
95 connect(this, SIGNAL(entered(const QModelIndex
&)),
96 this, SLOT(slotEntered(const QModelIndex
&)));
98 const DolphinView
* dolphinView
= m_container
->m_dolphinViewController
->view();
99 connect(dolphinView
, SIGNAL(showPreviewChanged()),
100 this, SLOT(slotShowPreviewChanged()));
102 m_dirLister
= new DolphinDirLister();
103 m_dirLister
->setAutoUpdate(true);
104 m_dirLister
->setMainWindow(window());
105 m_dirLister
->setDelayedMimeTypes(true);
106 const bool showHiddenFiles
= m_container
->m_dolphinViewController
->view()->showHiddenFiles();
107 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
109 m_dolphinModel
= new DolphinModel(this);
110 m_dolphinModel
->setDirLister(m_dirLister
);
111 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
113 m_proxyModel
= new DolphinSortFilterProxyModel(this);
114 m_proxyModel
->setSourceModel(m_dolphinModel
);
115 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
117 m_proxyModel
->setSorting(dolphinView
->sorting());
118 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
119 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
121 setModel(m_proxyModel
);
123 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
124 this, SLOT(updateFont()));
126 const ViewModeController
* viewModeController
= m_container
->m_viewModeController
;
127 connect(viewModeController
, SIGNAL(zoomLevelChanged(int)),
128 this, SLOT(setZoomLevel(int)));
129 const QString nameFilter
= viewModeController
->nameFilter();
130 if (!nameFilter
.isEmpty()) {
131 m_proxyModel
->setFilterFixedString(nameFilter
);
134 updateDecorationSize(dolphinView
->showPreview());
137 DolphinViewController
* dolphinViewController
= m_container
->m_dolphinViewController
;
138 m_extensionsFactory
= new ViewExtensionsFactory(this, dolphinViewController
, viewModeController
);
139 m_extensionsFactory
->fileItemDelegate()->setMinimizedNameColumn(true);
141 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
144 DolphinColumnView::~DolphinColumnView()
148 delete m_dolphinModel
;
150 m_dirLister
= 0; // deleted by m_dolphinModel
153 void DolphinColumnView::setActive(bool active
)
155 if (m_active
!= active
) {
166 void DolphinColumnView::updateBackground()
168 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
169 // cleaning up the cut-indication of DolphinColumnView with the code from
170 // DolphinView a common helper-class should be available which can be shared
171 // by all view implementations -> no hardcoded value anymore
172 const QPalette::ColorRole role
= viewport()->backgroundRole();
173 QColor color
= viewport()->palette().color(role
);
174 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
176 QPalette palette
= viewport()->palette();
177 palette
.setColor(role
, color
);
178 viewport()->setPalette(palette
);
183 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
186 const QModelIndex index
= indexAt(pos
);
187 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
188 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
189 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
194 void DolphinColumnView::setSelectionModel(QItemSelectionModel
* model
)
196 // If a change of the selection is done although the view is not active
197 // (e. g. by the selection markers), the column must be activated. This
198 // is done by listening to the current selectionChanged() signal.
199 if (selectionModel() != 0) {
200 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
201 this, SLOT(requestActivation()));
204 DolphinTreeView::setSelectionModel(model
);
206 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
207 this, SLOT(requestActivation()));
210 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
212 QStyleOptionViewItem viewOptions
= DolphinTreeView::viewOptions();
213 viewOptions
.font
= m_font
;
214 viewOptions
.fontMetrics
= QFontMetrics(m_font
);
215 viewOptions
.decorationSize
= m_decorationSize
;
216 viewOptions
.showDecorationSelected
= true;
220 bool DolphinColumnView::event(QEvent
* event
)
222 if (event
->type() == QEvent::Polish
) {
223 // Hide all columns except of the 'Name' column
224 for (int i
= DolphinModel::Name
+ 1; i
< DolphinModel::ExtraColumnCount
; ++i
) {
230 return DolphinTreeView::event(event
);
233 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
235 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_dolphinViewController
);
236 DolphinTreeView::startDrag(supportedActions
);
239 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
241 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
242 event
->acceptProposedAction();
245 DolphinTreeView::dragEnterEvent(event
);
248 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
250 DolphinTreeView::dragMoveEvent(event
);
252 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
253 // accept url drops, independently from the destination item
254 event
->acceptProposedAction();
258 void DolphinColumnView::dropEvent(QDropEvent
* event
)
260 const QModelIndex index
= indexAt(event
->pos());
261 m_container
->m_dolphinViewController
->setItemView(this);
262 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
263 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
264 m_container
->m_dolphinViewController
->indicateDroppedUrls(item
, url(), event
);
265 DolphinTreeView::dropEvent(event
);
268 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
270 if (!m_childUrl
.isEmpty()) {
271 // Indicate the shown URL of the next column by highlighting the shown folder item
272 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
273 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
274 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
275 QPainter
painter(viewport());
277 QStyleOptionViewItemV4 option
;
278 option
.initFrom(this);
279 option
.rect
= visualRect(proxyIndex
);
280 option
.state
= QStyle::State_Enabled
| QStyle::State_HasFocus
;
281 option
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
282 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &option
, &painter
, this);
286 DolphinTreeView::paintEvent(event
);
289 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
292 if (!indexAt(event
->pos()).isValid() && (QApplication::mouseButtons() & Qt::MidButton
)) {
293 m_container
->m_dolphinViewController
->replaceUrlByClipboard();
296 DolphinTreeView::mousePressEvent(event
);
299 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
301 DolphinTreeView::keyPressEvent(event
);
303 DolphinViewController
* controller
= m_container
->m_dolphinViewController
;
304 controller
->handleKeyPressEvent(event
);
305 switch (event
->key()) {
306 case Qt::Key_Right
: {
307 // Special key handling for the column: A Key_Right should
308 // open a new column for the currently selected folder.
309 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(currentIndex());
310 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
311 if (!item
.isNull() && item
.isDir()) {
312 controller
->emitItemTriggered(item
);
318 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
319 QItemSelectionModel::Current
|
320 QItemSelectionModel::Clear
);
328 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
331 DolphinTreeView::contextMenuEvent(event
);
332 m_container
->m_dolphinViewController
->triggerContextMenuRequest(event
->pos());
335 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
337 const int step
= m_decorationSize
.height();
338 verticalScrollBar()->setSingleStep(step
);
339 DolphinTreeView::wheelEvent(event
);
342 void DolphinColumnView::leaveEvent(QEvent
* event
)
344 DolphinTreeView::leaveEvent(event
);
345 // if the mouse is above an item and moved very fast outside the widget,
346 // no viewportEntered() signal might be emitted although the mouse has been moved
347 // above the viewport
348 m_container
->m_dolphinViewController
->emitViewportEntered();
351 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
353 DolphinTreeView::currentChanged(current
, previous
);
354 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
357 QRect
DolphinColumnView::visualRect(const QModelIndex
& index
) const
359 QRect rect
= DolphinTreeView::visualRect(index
);
361 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
362 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
363 if (!item
.isNull()) {
364 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.text(), viewOptions());
365 rect
.setWidth(width
);
371 bool DolphinColumnView::acceptsDrop(const QModelIndex
& index
) const
373 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
374 // Accept drops above directories
375 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
376 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
377 return !item
.isNull() && item
.isDir();
383 void DolphinColumnView::setZoomLevel(int level
)
385 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
386 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
388 const bool showPreview
= m_container
->m_dolphinViewController
->view()->showPreview();
390 settings
->setPreviewSize(size
);
392 settings
->setIconSize(size
);
395 updateDecorationSize(showPreview
);
398 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
400 m_container
->m_dolphinViewController
->setItemView(this);
401 m_container
->m_dolphinViewController
->emitItemEntered(index
);
404 void DolphinColumnView::requestActivation()
406 m_container
->m_dolphinViewController
->requestActivation();
408 m_container
->requestActivation(this);
409 selectionModel()->clear();
413 void DolphinColumnView::updateFont()
415 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
416 Q_ASSERT(settings
!= 0);
418 if (settings
->useSystemFont()) {
419 m_font
= KGlobalSettings::generalFont();
423 void DolphinColumnView::slotShowPreviewChanged()
425 const DolphinView
* view
= m_container
->m_dolphinViewController
->view();
426 updateDecorationSize(view
->showPreview());
429 void DolphinColumnView::activate()
431 setFocus(Qt::OtherFocusReason
);
433 if (KGlobalSettings::singleClick()) {
434 connect(this, SIGNAL(clicked(const QModelIndex
&)),
435 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
437 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
438 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
441 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
442 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
448 void DolphinColumnView::deactivate()
451 if (KGlobalSettings::singleClick()) {
452 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
453 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
455 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
456 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
459 // It is important to disconnect the connection to requestActivation() temporary, otherwise the internal
460 // clearing of the selection would result in activating the column again.
461 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
462 this, SLOT(requestActivation()));
463 const QModelIndex current
= selectionModel()->currentIndex();
464 selectionModel()->clear();
465 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
466 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
467 this, SLOT(requestActivation()));
472 void DolphinColumnView::updateDecorationSize(bool showPreview
)
474 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
475 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
476 const QSize
size(iconSize
, iconSize
);
479 m_decorationSize
= size
;
484 #include "dolphincolumnview.moc"