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 "dolphincontroller.h"
25 #include "dolphindirlister.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "settings/dolphinsettings.h"
28 #include "dolphinviewautoscroller.h"
29 #include "dolphin_columnmodesettings.h"
30 #include "dolphin_generalsettings.h"
31 #include "draganddrophelper.h"
32 #include "folderexpander.h"
33 #include "tooltips/tooltipmanager.h"
34 #include "versioncontrolobserver.h"
35 #include "viewextensionsfactory.h"
36 #include "zoomlevelinfo.h"
38 #include <kcolorscheme.h>
39 #include <kdirlister.h>
40 #include <kfileitem.h>
41 #include <kio/previewjob.h>
42 #include <kiconeffect.h>
44 #include <konqmimedata.h>
46 #include <QApplication>
52 DolphinColumnView::DolphinColumnView(QWidget
* parent
,
53 DolphinColumnViewContainer
* container
,
57 m_container(container
),
58 m_extensionsFactory(0),
68 setMouseTracking(true);
69 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
70 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
71 setSelectionBehavior(SelectItems
);
72 setSelectionMode(QAbstractItemView::ExtendedSelection
);
73 setDragDropMode(QAbstractItemView::DragDrop
);
74 setDropIndicatorShown(false);
75 setSelectionRectVisible(true);
76 setEditTriggers(QAbstractItemView::NoEditTriggers
);
78 setVerticalScrollMode(QListView::ScrollPerPixel
);
79 setHorizontalScrollMode(QListView::ScrollPerPixel
);
81 // apply the column mode settings to the widget
82 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
83 Q_ASSERT(settings
!= 0);
85 if (settings
->useSystemFont()) {
86 m_font
= KGlobalSettings::generalFont();
88 m_font
= QFont(settings
->fontFamily(),
90 settings
->fontWeight(),
91 settings
->italicFont());
96 connect(this, SIGNAL(viewportEntered()),
97 m_container
->m_controller
, SLOT(emitViewportEntered()));
98 connect(this, SIGNAL(entered(const QModelIndex
&)),
99 this, SLOT(slotEntered(const QModelIndex
&)));
101 const DolphinView
* dolphinView
= m_container
->m_controller
->dolphinView();
102 connect(dolphinView
, SIGNAL(showPreviewChanged()),
103 this, SLOT(slotShowPreviewChanged()));
105 m_dirLister
= new DolphinDirLister();
106 m_dirLister
->setAutoUpdate(true);
107 m_dirLister
->setMainWindow(window());
108 m_dirLister
->setDelayedMimeTypes(true);
109 const bool showHiddenFiles
= m_container
->m_controller
->dolphinView()->showHiddenFiles();
110 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
112 m_dolphinModel
= new DolphinModel(this);
113 m_dolphinModel
->setDirLister(m_dirLister
);
114 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
116 m_proxyModel
= new DolphinSortFilterProxyModel(this);
117 m_proxyModel
->setSourceModel(m_dolphinModel
);
118 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
120 m_proxyModel
->setSorting(dolphinView
->sorting());
121 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
122 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
124 setModel(m_proxyModel
);
126 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
127 this, SLOT(updateFont()));
129 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
130 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
131 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
132 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
134 new VersionControlObserver(this);*/
136 DolphinController
* controller
= m_container
->m_controller
;
137 connect(controller
, SIGNAL(zoomLevelChanged(int)),
138 this, SLOT(setZoomLevel(int)));
140 const QString nameFilter
= controller
->nameFilter();
141 if (!nameFilter
.isEmpty()) {
142 m_proxyModel
->setFilterRegExp(nameFilter
);
145 updateDecorationSize(dolphinView
->showPreview());
146 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
149 DolphinColumnView::~DolphinColumnView()
153 delete m_dolphinModel
;
155 m_dirLister
= 0; // deleted by m_dolphinModel
158 void DolphinColumnView::setActive(bool active
)
160 if (active
&& (m_container
->focusProxy() != this)) {
161 m_container
->setFocusProxy(this);
164 if (m_active
!= active
) {
175 void DolphinColumnView::updateBackground()
177 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
178 // cleaning up the cut-indication of DolphinColumnView with the code from
179 // DolphinView a common helper-class should be available which can be shared
180 // by all view implementations -> no hardcoded value anymore
181 const QPalette::ColorRole role
= viewport()->backgroundRole();
182 QColor color
= viewport()->palette().color(role
);
183 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
185 QPalette palette
= viewport()->palette();
186 palette
.setColor(role
, color
);
187 viewport()->setPalette(palette
);
192 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
195 const QModelIndex index
= indexAt(pos
);
196 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
197 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
198 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
203 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
205 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
206 viewOptions
.font
= m_font
;
207 viewOptions
.decorationSize
= m_decorationSize
;
208 viewOptions
.showDecorationSelected
= true;
212 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
214 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_controller
);
217 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
219 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
220 event
->acceptProposedAction();
225 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
227 QListView::dragLeaveEvent(event
);
228 setDirtyRegion(m_dropRect
);
231 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
233 QListView::dragMoveEvent(event
);
235 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
236 const QModelIndex index
= indexAt(event
->pos());
237 setDirtyRegion(m_dropRect
);
239 m_dropRect
.setSize(QSize()); // set as invalid
240 if (index
.isValid()) {
241 m_container
->m_controller
->setItemView(this);
242 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
243 if (!item
.isNull() && item
.isDir()) {
244 m_dropRect
= visualRect(index
);
247 setDirtyRegion(m_dropRect
);
249 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
250 // accept url drops, independently from the destination item
251 event
->acceptProposedAction();
255 void DolphinColumnView::dropEvent(QDropEvent
* event
)
257 const QModelIndex index
= indexAt(event
->pos());
258 m_container
->m_controller
->setItemView(this);
259 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
260 m_container
->m_controller
->indicateDroppedUrls(item
, url(), event
);
261 QListView::dropEvent(event
);
264 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
266 if (!m_childUrl
.isEmpty()) {
267 // indicate the shown URL of the next column by highlighting the shown folder item
268 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
269 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
270 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
271 const QRect itemRect
= visualRect(proxyIndex
);
272 QPainter
painter(viewport());
273 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
275 painter
.setPen(Qt::NoPen
);
276 painter
.setBrush(color
);
277 painter
.drawRect(itemRect
);
281 QListView::paintEvent(event
);
284 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
287 if (!indexAt(event
->pos()).isValid()) {
288 if (QApplication::mouseButtons() & Qt::MidButton
) {
289 m_container
->m_controller
->replaceUrlByClipboard();
291 } else if (event
->button() == Qt::LeftButton
) {
292 // TODO: see comment in DolphinIconsView::mousePressEvent()
293 setState(QAbstractItemView::DraggingState
);
295 QListView::mousePressEvent(event
);
298 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
300 QListView::keyPressEvent(event
);
303 DolphinController
* controller
= m_container
->m_controller
;
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 index
= currentIndex();
310 const KFileItem item
= controller
->itemForIndex(index
);
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 m_container
->requestActivation(this);
332 Q_ASSERT(m_container
->m_controller
->itemView() == this);
333 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
337 QListView::contextMenuEvent(event
);
338 m_container
->m_controller
->triggerContextMenuRequest(event
->pos());
341 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
343 const int height
= m_decorationSize
.height();
344 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
345 verticalScrollBar()->setSingleStep(step
);
347 QListView::wheelEvent(event
);
350 void DolphinColumnView::leaveEvent(QEvent
* event
)
352 QListView::leaveEvent(event
);
353 // if the mouse is above an item and moved very fast outside the widget,
354 // no viewportEntered() signal might be emitted although the mouse has been moved
355 // above the viewport
356 m_container
->m_controller
->emitViewportEntered();
359 void DolphinColumnView::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
361 QListView::selectionChanged(selected
, deselected
);
363 //QItemSelectionModel* selModel = m_container->selectionModel();
364 //selModel->select(selected, QItemSelectionModel::Select);
365 //selModel->select(deselected, QItemSelectionModel::Deselect);
368 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
370 QListView::currentChanged(current
, previous
);
371 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
374 void DolphinColumnView::setZoomLevel(int level
)
376 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
377 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
379 const bool showPreview
= m_container
->m_controller
->dolphinView()->showPreview();
381 settings
->setPreviewSize(size
);
383 settings
->setIconSize(size
);
386 updateDecorationSize(showPreview
);
389 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
391 m_container
->m_controller
->setItemView(this);
392 m_container
->m_controller
->emitItemEntered(index
);
395 void DolphinColumnView::requestActivation()
397 m_container
->m_controller
->setItemView(this);
398 m_container
->m_controller
->requestActivation();
400 m_container
->requestActivation(this);
401 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
402 selectionModel()->clear();
406 void DolphinColumnView::updateFont()
408 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
409 Q_ASSERT(settings
!= 0);
411 if (settings
->useSystemFont()) {
412 m_font
= KGlobalSettings::generalFont();
416 void DolphinColumnView::slotShowPreviewChanged()
418 const DolphinView
* view
= m_container
->m_controller
->dolphinView();
419 updateDecorationSize(view
->showPreview());
422 void DolphinColumnView::activate()
424 setFocus(Qt::OtherFocusReason
);
426 if (KGlobalSettings::singleClick()) {
427 connect(this, SIGNAL(clicked(const QModelIndex
&)),
428 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
430 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
431 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
434 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
435 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
441 void DolphinColumnView::deactivate()
444 if (KGlobalSettings::singleClick()) {
445 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
446 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
448 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
449 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
452 const QModelIndex current
= selectionModel()->currentIndex();
453 selectionModel()->clear();
454 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
458 void DolphinColumnView::updateDecorationSize(bool showPreview
)
460 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
461 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
462 const QSize
size(iconSize
, iconSize
);
465 m_decorationSize
= size
;
470 #include "dolphincolumnview.moc"