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 const QRect itemRect
= visualRect(proxyIndex
);
276 QPainter
painter(viewport());
277 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
279 painter
.setPen(Qt::NoPen
);
280 painter
.setBrush(color
);
281 painter
.drawRect(itemRect
);
285 DolphinTreeView::paintEvent(event
);
288 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
291 if (!indexAt(event
->pos()).isValid() && (QApplication::mouseButtons() & Qt::MidButton
)) {
292 m_container
->m_dolphinViewController
->replaceUrlByClipboard();
295 DolphinTreeView::mousePressEvent(event
);
298 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
300 DolphinTreeView::keyPressEvent(event
);
302 DolphinViewController
* controller
= m_container
->m_dolphinViewController
;
303 controller
->handleKeyPressEvent(event
);
304 switch (event
->key()) {
305 case Qt::Key_Right
: {
306 // Special key handling for the column: A Key_Right should
307 // open a new column for the currently selected folder.
308 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(currentIndex());
309 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
310 if (!item
.isNull() && item
.isDir()) {
311 controller
->emitItemTriggered(item
);
317 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
318 QItemSelectionModel::Current
|
319 QItemSelectionModel::Clear
);
327 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
330 DolphinTreeView::contextMenuEvent(event
);
331 m_container
->m_dolphinViewController
->triggerContextMenuRequest(event
->pos());
334 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
336 const int step
= m_decorationSize
.height();
337 verticalScrollBar()->setSingleStep(step
);
338 DolphinTreeView::wheelEvent(event
);
341 void DolphinColumnView::leaveEvent(QEvent
* event
)
343 DolphinTreeView::leaveEvent(event
);
344 // if the mouse is above an item and moved very fast outside the widget,
345 // no viewportEntered() signal might be emitted although the mouse has been moved
346 // above the viewport
347 m_container
->m_dolphinViewController
->emitViewportEntered();
350 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
352 DolphinTreeView::currentChanged(current
, previous
);
353 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
356 QRect
DolphinColumnView::visualRect(const QModelIndex
& index
) const
358 QRect rect
= DolphinTreeView::visualRect(index
);
360 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
361 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
362 if (!item
.isNull()) {
363 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.text(), viewOptions());
364 rect
.setWidth(width
);
370 bool DolphinColumnView::acceptsDrop(const QModelIndex
& index
) const
372 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
373 // Accept drops above directories
374 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
375 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
376 return !item
.isNull() && item
.isDir();
382 void DolphinColumnView::setZoomLevel(int level
)
384 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
385 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
387 const bool showPreview
= m_container
->m_dolphinViewController
->view()->showPreview();
389 settings
->setPreviewSize(size
);
391 settings
->setIconSize(size
);
394 updateDecorationSize(showPreview
);
397 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
399 m_container
->m_dolphinViewController
->setItemView(this);
400 m_container
->m_dolphinViewController
->emitItemEntered(index
);
403 void DolphinColumnView::requestActivation()
405 m_container
->m_dolphinViewController
->requestActivation();
407 m_container
->requestActivation(this);
408 selectionModel()->clear();
412 void DolphinColumnView::updateFont()
414 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
415 Q_ASSERT(settings
!= 0);
417 if (settings
->useSystemFont()) {
418 m_font
= KGlobalSettings::generalFont();
422 void DolphinColumnView::slotShowPreviewChanged()
424 const DolphinView
* view
= m_container
->m_dolphinViewController
->view();
425 updateDecorationSize(view
->showPreview());
428 void DolphinColumnView::activate()
430 setFocus(Qt::OtherFocusReason
);
432 if (KGlobalSettings::singleClick()) {
433 connect(this, SIGNAL(clicked(const QModelIndex
&)),
434 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
436 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
437 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
440 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
441 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
447 void DolphinColumnView::deactivate()
450 if (KGlobalSettings::singleClick()) {
451 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
452 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
454 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
455 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
458 // It is important to disconnect the connection to requestActivation() temporary, otherwise the internal
459 // clearing of the selection would result in activating the column again.
460 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
461 this, SLOT(requestActivation()));
462 const QModelIndex current
= selectionModel()->currentIndex();
463 selectionModel()->clear();
464 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
465 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
466 this, SLOT(requestActivation()));
471 void DolphinColumnView::updateDecorationSize(bool showPreview
)
473 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
474 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
475 const QSize
size(iconSize
, iconSize
);
478 m_decorationSize
= size
;
483 #include "dolphincolumnview.moc"