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());
94 // KFileItemDelegate* delegate = new KFileItemDelegate(this);
95 // delegate->setShowToolTipWhenElided(false);
96 // setItemDelegate(delegate);
100 connect(this, SIGNAL(viewportEntered()),
101 m_container
->m_controller
, SLOT(emitViewportEntered()));
102 connect(this, SIGNAL(entered(const QModelIndex
&)),
103 this, SLOT(slotEntered(const QModelIndex
&)));
105 const DolphinView
* dolphinView
= m_container
->m_controller
->dolphinView();
106 connect(dolphinView
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
107 this, SLOT(slotSortingChanged(DolphinView::Sorting
)));
108 connect(dolphinView
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
109 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
110 connect(dolphinView
, SIGNAL(sortFoldersFirstChanged(bool)),
111 this, SLOT(slotSortFoldersFirstChanged(bool)));
112 connect(dolphinView
, SIGNAL(showHiddenFilesChanged()),
113 this, SLOT(slotShowHiddenFilesChanged()));
114 connect(dolphinView
, SIGNAL(showPreviewChanged()),
115 this, SLOT(slotShowPreviewChanged()));
117 m_dirLister
= new DolphinDirLister();
118 m_dirLister
->setAutoUpdate(true);
119 m_dirLister
->setMainWindow(window());
120 m_dirLister
->setDelayedMimeTypes(true);
121 const bool showHiddenFiles
= m_container
->m_controller
->dolphinView()->showHiddenFiles();
122 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
124 m_dolphinModel
= new DolphinModel(this);
125 m_dolphinModel
->setDirLister(m_dirLister
);
126 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
128 m_proxyModel
= new DolphinSortFilterProxyModel(this);
129 m_proxyModel
->setSourceModel(m_dolphinModel
);
130 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
132 m_proxyModel
->setSorting(dolphinView
->sorting());
133 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
134 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
136 setModel(m_proxyModel
);
138 //m_dirLister->openUrl(url, KDirLister::NoFlags);
140 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
141 this, SLOT(updateFont()));
143 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
144 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
145 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
146 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
148 new VersionControlObserver(this);*/
150 DolphinController
* controller
= m_container
->m_controller
;
151 connect(controller
, SIGNAL(zoomLevelChanged(int)),
152 this, SLOT(setZoomLevel(int)));
154 const QString nameFilter
= controller
->nameFilter();
155 if (!nameFilter
.isEmpty()) {
156 m_proxyModel
->setFilterRegExp(nameFilter
);
158 connect(controller
, SIGNAL(nameFilterChanged(const QString
&)),
159 this, SLOT(setNameFilter(const QString
&)));
161 updateDecorationSize(dolphinView
->showPreview());
163 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
166 DolphinColumnView::~DolphinColumnView()
170 delete m_dolphinModel
;
172 m_dirLister
= 0; // deleted by m_dolphinModel
175 void DolphinColumnView::setActive(bool active
)
177 if (active
&& (m_container
->focusProxy() != this)) {
178 m_container
->setFocusProxy(this);
181 if (m_active
!= active
) {
192 /*void DolphinColumnView::setSorting(DolphinView::Sorting sorting)
194 m_proxyModel->setSorting(sorting);
197 void DolphinColumnView::setSortOrder(Qt::SortOrder order)
199 m_proxyModel->setSortOrder(order);
202 void DolphinColumnView::setSortFoldersFirst(bool foldersFirst)
204 m_proxyModel->setSortFoldersFirst(foldersFirst);
207 void DolphinColumnView::setShowHiddenFiles(bool show)
209 if (show != m_dirLister->showingDotFiles()) {
210 m_dirLister->setShowingDotFiles(show);
212 m_dirLister->openUrl(m_url, KDirLister::Reload);
216 void DolphinColumnView::setShowPreview(bool show)
218 m_previewGenerator->setPreviewShown(show);
221 m_dirLister->openUrl(m_url, KDirLister::Reload);
224 void DolphinColumnView::updateBackground()
226 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
227 // cleaning up the cut-indication of DolphinColumnView with the code from
228 // DolphinView a common helper-class should be available which can be shared
229 // by all view implementations -> no hardcoded value anymore
230 const QPalette::ColorRole role
= viewport()->backgroundRole();
231 QColor color
= viewport()->palette().color(role
);
232 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
234 QPalette palette
= viewport()->palette();
235 palette
.setColor(role
, color
);
236 viewport()->setPalette(palette
);
241 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
244 const QModelIndex index
= indexAt(pos
);
245 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
246 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
247 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
252 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
254 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
255 viewOptions
.font
= m_font
;
256 viewOptions
.decorationSize
= m_decorationSize
;
257 viewOptions
.showDecorationSelected
= true;
261 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
263 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_controller
);
266 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
268 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
269 event
->acceptProposedAction();
274 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
276 QListView::dragLeaveEvent(event
);
277 setDirtyRegion(m_dropRect
);
280 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
282 QListView::dragMoveEvent(event
);
284 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
285 const QModelIndex index
= indexAt(event
->pos());
286 setDirtyRegion(m_dropRect
);
288 m_dropRect
.setSize(QSize()); // set as invalid
289 if (index
.isValid()) {
290 m_container
->m_controller
->setItemView(this);
291 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
292 if (!item
.isNull() && item
.isDir()) {
293 m_dropRect
= visualRect(index
);
296 setDirtyRegion(m_dropRect
);
298 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
299 // accept url drops, independently from the destination item
300 event
->acceptProposedAction();
304 void DolphinColumnView::dropEvent(QDropEvent
* event
)
306 const QModelIndex index
= indexAt(event
->pos());
307 m_container
->m_controller
->setItemView(this);
308 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
309 m_container
->m_controller
->indicateDroppedUrls(item
, url(), event
);
310 QListView::dropEvent(event
);
313 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
315 if (!m_childUrl
.isEmpty()) {
316 // indicate the shown URL of the next column by highlighting the shown folder item
317 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
318 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
319 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
320 const QRect itemRect
= visualRect(proxyIndex
);
321 QPainter
painter(viewport());
322 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
324 painter
.setPen(Qt::NoPen
);
325 painter
.setBrush(color
);
326 painter
.drawRect(itemRect
);
330 QListView::paintEvent(event
);
333 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
336 if (!indexAt(event
->pos()).isValid()) {
337 if (QApplication::mouseButtons() & Qt::MidButton
) {
338 m_container
->m_controller
->replaceUrlByClipboard();
340 } else if (event
->button() == Qt::LeftButton
) {
341 // TODO: see comment in DolphinIconsView::mousePressEvent()
342 setState(QAbstractItemView::DraggingState
);
344 QListView::mousePressEvent(event
);
347 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
349 QListView::keyPressEvent(event
);
352 DolphinController
* controller
= m_container
->m_controller
;
353 controller
->handleKeyPressEvent(event
);
354 switch (event
->key()) {
355 case Qt::Key_Right
: {
356 // Special key handling for the column: A Key_Right should
357 // open a new column for the currently selected folder.
358 const QModelIndex index
= currentIndex();
359 const KFileItem item
= controller
->itemForIndex(index
);
360 if (!item
.isNull() && item
.isDir()) {
361 controller
->emitItemTriggered(item
);
367 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
368 QItemSelectionModel::Current
|
369 QItemSelectionModel::Clear
);
377 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
380 m_container
->requestActivation(this);
381 Q_ASSERT(m_container
->m_controller
->itemView() == this);
382 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
386 QListView::contextMenuEvent(event
);
388 const QModelIndex index
= indexAt(event
->pos());
389 if (!index
.isValid()) {
393 const QPoint pos
= m_container
->viewport()->mapFromGlobal(event
->globalPos());
394 Q_ASSERT(m_container
->m_controller
->itemView() == this);
395 m_container
->m_controller
->triggerContextMenuRequest(pos
);
398 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
400 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
401 if (event
->modifiers() & Qt::ControlModifier
) {
406 const int height
= m_decorationSize
.height();
407 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
408 verticalScrollBar()->setSingleStep(step
);
410 QListView::wheelEvent(event
);
413 void DolphinColumnView::leaveEvent(QEvent
* event
)
415 QListView::leaveEvent(event
);
416 // if the mouse is above an item and moved very fast outside the widget,
417 // no viewportEntered() signal might be emitted although the mouse has been moved
418 // above the viewport
419 m_container
->m_controller
->emitViewportEntered();
422 void DolphinColumnView::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
424 QListView::selectionChanged(selected
, deselected
);
426 //QItemSelectionModel* selModel = m_container->selectionModel();
427 //selModel->select(selected, QItemSelectionModel::Select);
428 //selModel->select(deselected, QItemSelectionModel::Deselect);
431 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
433 QListView::currentChanged(current
, previous
);
434 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
437 void DolphinColumnView::setNameFilter(const QString
& nameFilter
)
439 m_proxyModel
->setFilterRegExp(nameFilter
);
442 void DolphinColumnView::setZoomLevel(int level
)
444 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
445 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
447 const bool showPreview
= m_container
->m_controller
->dolphinView()->showPreview();
449 settings
->setPreviewSize(size
);
451 settings
->setIconSize(size
);
454 updateDecorationSize(showPreview
);
457 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
459 m_container
->m_controller
->setItemView(this);
460 m_container
->m_controller
->emitItemEntered(index
);
463 void DolphinColumnView::requestActivation()
465 m_container
->m_controller
->setItemView(this);
466 m_container
->m_controller
->requestActivation();
468 m_container
->requestActivation(this);
469 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
470 selectionModel()->clear();
474 void DolphinColumnView::updateFont()
476 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
477 Q_ASSERT(settings
!= 0);
479 if (settings
->useSystemFont()) {
480 m_font
= KGlobalSettings::generalFont();
484 void DolphinColumnView::slotShowPreviewChanged()
486 const DolphinView
* view
= m_container
->m_controller
->dolphinView();
487 updateDecorationSize(view
->showPreview());
490 void DolphinColumnView::activate()
492 setFocus(Qt::OtherFocusReason
);
494 if (KGlobalSettings::singleClick()) {
495 connect(this, SIGNAL(clicked(const QModelIndex
&)),
496 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
498 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
499 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
502 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
503 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
509 void DolphinColumnView::deactivate()
512 if (KGlobalSettings::singleClick()) {
513 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
514 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
516 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
517 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
520 const QModelIndex current
= selectionModel()->currentIndex();
521 selectionModel()->clear();
522 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
526 void DolphinColumnView::updateDecorationSize(bool showPreview
)
528 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
529 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
530 const QSize
size(iconSize
, iconSize
);
533 m_decorationSize
= size
;
538 #include "dolphincolumnview.moc"