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(sortingChanged(DolphinView::Sorting
)),
103 this, SLOT(slotSortingChanged(DolphinView::Sorting
)));
104 connect(dolphinView
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
105 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
106 connect(dolphinView
, SIGNAL(sortFoldersFirstChanged(bool)),
107 this, SLOT(slotSortFoldersFirstChanged(bool)));
108 connect(dolphinView
, SIGNAL(showHiddenFilesChanged()),
109 this, SLOT(slotShowHiddenFilesChanged()));
110 connect(dolphinView
, SIGNAL(showPreviewChanged()),
111 this, SLOT(slotShowPreviewChanged()));
113 m_dirLister
= new DolphinDirLister();
114 m_dirLister
->setAutoUpdate(true);
115 m_dirLister
->setMainWindow(window());
116 m_dirLister
->setDelayedMimeTypes(true);
117 const bool showHiddenFiles
= m_container
->m_controller
->dolphinView()->showHiddenFiles();
118 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
120 m_dolphinModel
= new DolphinModel(this);
121 m_dolphinModel
->setDirLister(m_dirLister
);
122 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
124 m_proxyModel
= new DolphinSortFilterProxyModel(this);
125 m_proxyModel
->setSourceModel(m_dolphinModel
);
126 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
128 m_proxyModel
->setSorting(dolphinView
->sorting());
129 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
130 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
132 setModel(m_proxyModel
);
134 //m_dirLister->openUrl(url, KDirLister::NoFlags);
136 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
137 this, SLOT(updateFont()));
139 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
140 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
141 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
142 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
144 new VersionControlObserver(this);*/
146 DolphinController
* controller
= m_container
->m_controller
;
147 connect(controller
, SIGNAL(zoomLevelChanged(int)),
148 this, SLOT(setZoomLevel(int)));
150 const QString nameFilter
= controller
->nameFilter();
151 if (!nameFilter
.isEmpty()) {
152 m_proxyModel
->setFilterRegExp(nameFilter
);
154 connect(controller
, SIGNAL(nameFilterChanged(const QString
&)),
155 this, SLOT(setNameFilter(const QString
&)));
157 updateDecorationSize(dolphinView
->showPreview());
159 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
162 DolphinColumnView::~DolphinColumnView()
166 delete m_dolphinModel
;
168 m_dirLister
= 0; // deleted by m_dolphinModel
171 void DolphinColumnView::setActive(bool active
)
173 if (active
&& (m_container
->focusProxy() != this)) {
174 m_container
->setFocusProxy(this);
177 if (m_active
!= active
) {
188 /*void DolphinColumnView::setSorting(DolphinView::Sorting sorting)
190 m_proxyModel->setSorting(sorting);
193 void DolphinColumnView::setSortOrder(Qt::SortOrder order)
195 m_proxyModel->setSortOrder(order);
198 void DolphinColumnView::setSortFoldersFirst(bool foldersFirst)
200 m_proxyModel->setSortFoldersFirst(foldersFirst);
203 void DolphinColumnView::setShowHiddenFiles(bool show)
205 if (show != m_dirLister->showingDotFiles()) {
206 m_dirLister->setShowingDotFiles(show);
208 m_dirLister->openUrl(m_url, KDirLister::Reload);
212 void DolphinColumnView::setShowPreview(bool show)
214 m_previewGenerator->setPreviewShown(show);
217 m_dirLister->openUrl(m_url, KDirLister::Reload);
220 void DolphinColumnView::updateBackground()
222 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
223 // cleaning up the cut-indication of DolphinColumnView with the code from
224 // DolphinView a common helper-class should be available which can be shared
225 // by all view implementations -> no hardcoded value anymore
226 const QPalette::ColorRole role
= viewport()->backgroundRole();
227 QColor color
= viewport()->palette().color(role
);
228 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
230 QPalette palette
= viewport()->palette();
231 palette
.setColor(role
, color
);
232 viewport()->setPalette(palette
);
237 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
240 const QModelIndex index
= indexAt(pos
);
241 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
242 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
243 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
248 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
250 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
251 viewOptions
.font
= m_font
;
252 viewOptions
.decorationSize
= m_decorationSize
;
253 viewOptions
.showDecorationSelected
= true;
257 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
259 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_controller
);
262 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
264 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
265 event
->acceptProposedAction();
270 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
272 QListView::dragLeaveEvent(event
);
273 setDirtyRegion(m_dropRect
);
276 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
278 QListView::dragMoveEvent(event
);
280 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
281 const QModelIndex index
= indexAt(event
->pos());
282 setDirtyRegion(m_dropRect
);
284 m_dropRect
.setSize(QSize()); // set as invalid
285 if (index
.isValid()) {
286 m_container
->m_controller
->setItemView(this);
287 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
288 if (!item
.isNull() && item
.isDir()) {
289 m_dropRect
= visualRect(index
);
292 setDirtyRegion(m_dropRect
);
294 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
295 // accept url drops, independently from the destination item
296 event
->acceptProposedAction();
300 void DolphinColumnView::dropEvent(QDropEvent
* event
)
302 const QModelIndex index
= indexAt(event
->pos());
303 m_container
->m_controller
->setItemView(this);
304 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
305 m_container
->m_controller
->indicateDroppedUrls(item
, url(), event
);
306 QListView::dropEvent(event
);
309 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
311 if (!m_childUrl
.isEmpty()) {
312 // indicate the shown URL of the next column by highlighting the shown folder item
313 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
314 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
315 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
316 const QRect itemRect
= visualRect(proxyIndex
);
317 QPainter
painter(viewport());
318 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
320 painter
.setPen(Qt::NoPen
);
321 painter
.setBrush(color
);
322 painter
.drawRect(itemRect
);
326 QListView::paintEvent(event
);
329 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
332 if (!indexAt(event
->pos()).isValid()) {
333 if (QApplication::mouseButtons() & Qt::MidButton
) {
334 m_container
->m_controller
->replaceUrlByClipboard();
336 } else if (event
->button() == Qt::LeftButton
) {
337 // TODO: see comment in DolphinIconsView::mousePressEvent()
338 setState(QAbstractItemView::DraggingState
);
340 QListView::mousePressEvent(event
);
343 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
345 QListView::keyPressEvent(event
);
348 DolphinController
* controller
= m_container
->m_controller
;
349 controller
->handleKeyPressEvent(event
);
350 switch (event
->key()) {
351 case Qt::Key_Right
: {
352 // Special key handling for the column: A Key_Right should
353 // open a new column for the currently selected folder.
354 const QModelIndex index
= currentIndex();
355 const KFileItem item
= controller
->itemForIndex(index
);
356 if (!item
.isNull() && item
.isDir()) {
357 controller
->emitItemTriggered(item
);
363 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
364 QItemSelectionModel::Current
|
365 QItemSelectionModel::Clear
);
373 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
376 m_container
->requestActivation(this);
377 Q_ASSERT(m_container
->m_controller
->itemView() == this);
378 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
382 QListView::contextMenuEvent(event
);
384 const QModelIndex index
= indexAt(event
->pos());
385 if (!index
.isValid()) {
389 const QPoint pos
= m_container
->viewport()->mapFromGlobal(event
->globalPos());
390 Q_ASSERT(m_container
->m_controller
->itemView() == this);
391 m_container
->m_controller
->triggerContextMenuRequest(pos
);
394 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
396 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
397 if (event
->modifiers() & Qt::ControlModifier
) {
402 const int height
= m_decorationSize
.height();
403 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
404 verticalScrollBar()->setSingleStep(step
);
406 QListView::wheelEvent(event
);
409 void DolphinColumnView::leaveEvent(QEvent
* event
)
411 QListView::leaveEvent(event
);
412 // if the mouse is above an item and moved very fast outside the widget,
413 // no viewportEntered() signal might be emitted although the mouse has been moved
414 // above the viewport
415 m_container
->m_controller
->emitViewportEntered();
418 void DolphinColumnView::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
420 QListView::selectionChanged(selected
, deselected
);
422 //QItemSelectionModel* selModel = m_container->selectionModel();
423 //selModel->select(selected, QItemSelectionModel::Select);
424 //selModel->select(deselected, QItemSelectionModel::Deselect);
427 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
429 QListView::currentChanged(current
, previous
);
430 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
433 void DolphinColumnView::setNameFilter(const QString
& nameFilter
)
435 m_proxyModel
->setFilterRegExp(nameFilter
);
438 void DolphinColumnView::setZoomLevel(int level
)
440 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
441 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
443 const bool showPreview
= m_container
->m_controller
->dolphinView()->showPreview();
445 settings
->setPreviewSize(size
);
447 settings
->setIconSize(size
);
450 updateDecorationSize(showPreview
);
453 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
455 m_container
->m_controller
->setItemView(this);
456 m_container
->m_controller
->emitItemEntered(index
);
459 void DolphinColumnView::requestActivation()
461 m_container
->m_controller
->setItemView(this);
462 m_container
->m_controller
->requestActivation();
464 m_container
->requestActivation(this);
465 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
466 selectionModel()->clear();
470 void DolphinColumnView::updateFont()
472 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
473 Q_ASSERT(settings
!= 0);
475 if (settings
->useSystemFont()) {
476 m_font
= KGlobalSettings::generalFont();
480 void DolphinColumnView::slotShowPreviewChanged()
482 const DolphinView
* view
= m_container
->m_controller
->dolphinView();
483 updateDecorationSize(view
->showPreview());
486 void DolphinColumnView::activate()
488 setFocus(Qt::OtherFocusReason
);
490 if (KGlobalSettings::singleClick()) {
491 connect(this, SIGNAL(clicked(const QModelIndex
&)),
492 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
494 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
495 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
498 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
499 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
505 void DolphinColumnView::deactivate()
508 if (KGlobalSettings::singleClick()) {
509 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
510 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
512 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
513 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
516 const QModelIndex current
= selectionModel()->currentIndex();
517 selectionModel()->clear();
518 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
522 void DolphinColumnView::updateDecorationSize(bool showPreview
)
524 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
525 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
526 const QSize
size(iconSize
, iconSize
);
529 m_decorationSize
= size
;
534 #include "dolphincolumnview.moc"