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 "selectionmanager.h"
34 #include "tooltips/tooltipmanager.h"
35 #include "versioncontrolobserver.h"
36 #include "zoomlevelinfo.h"
38 #include <kcolorscheme.h>
39 #include <kdirlister.h>
40 #include <kfileitem.h>
41 #include <kfilepreviewgenerator.h>
42 #include <kio/previewjob.h>
43 #include <kiconeffect.h>
45 #include <konqmimedata.h>
47 #include <QApplication>
53 DolphinColumnView::DolphinColumnView(QWidget
* parent
,
54 DolphinColumnViewContainer
* container
,
58 m_container(container
),
59 m_selectionManager(0),
68 m_previewGenerator(0),
72 setMouseTracking(true);
73 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
74 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
75 setSelectionBehavior(SelectItems
);
76 setSelectionMode(QAbstractItemView::ExtendedSelection
);
77 setDragDropMode(QAbstractItemView::DragDrop
);
78 setDropIndicatorShown(false);
79 setSelectionRectVisible(true);
80 setEditTriggers(QAbstractItemView::NoEditTriggers
);
82 setVerticalScrollMode(QListView::ScrollPerPixel
);
83 setHorizontalScrollMode(QListView::ScrollPerPixel
);
85 m_autoScroller
= new DolphinViewAutoScroller(this);
87 // apply the column mode settings to the widget
88 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
89 Q_ASSERT(settings
!= 0);
91 if (settings
->useSystemFont()) {
92 m_font
= KGlobalSettings::generalFont();
94 m_font
= QFont(settings
->fontFamily(),
96 settings
->fontWeight(),
97 settings
->italicFont());
100 // KFileItemDelegate* delegate = new KFileItemDelegate(this);
101 // delegate->setShowToolTipWhenElided(false);
102 // setItemDelegate(delegate);
106 connect(this, SIGNAL(viewportEntered()),
107 m_container
->m_controller
, SLOT(emitViewportEntered()));
108 connect(this, SIGNAL(entered(const QModelIndex
&)),
109 this, SLOT(slotEntered(const QModelIndex
&)));
111 const DolphinView
* dolphinView
= m_container
->m_controller
->dolphinView();
112 connect(dolphinView
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
113 this, SLOT(slotSortingChanged(DolphinView::Sorting
)));
114 connect(dolphinView
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
115 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
116 connect(dolphinView
, SIGNAL(sortFoldersFirstChanged(bool)),
117 this, SLOT(slotSortFoldersFirstChanged(bool)));
118 connect(dolphinView
, SIGNAL(showHiddenFilesChanged()),
119 this, SLOT(slotShowHiddenFilesChanged()));
120 connect(dolphinView
, SIGNAL(showPreviewChanged()),
121 this, SLOT(slotShowPreviewChanged()));
123 m_dirLister
= new DolphinDirLister();
124 m_dirLister
->setAutoUpdate(true);
125 m_dirLister
->setMainWindow(window());
126 m_dirLister
->setDelayedMimeTypes(true);
127 const bool showHiddenFiles
= m_container
->m_controller
->dolphinView()->showHiddenFiles();
128 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
130 m_dolphinModel
= new DolphinModel(this);
131 m_dolphinModel
->setDirLister(m_dirLister
);
132 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
134 m_proxyModel
= new DolphinSortFilterProxyModel(this);
135 m_proxyModel
->setSourceModel(m_dolphinModel
);
136 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
138 m_proxyModel
->setSorting(dolphinView
->sorting());
139 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
140 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
142 setModel(m_proxyModel
);
144 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
145 m_selectionManager
= new SelectionManager(this);
146 connect(m_selectionManager
, SIGNAL(selectionChanged()),
147 this, SLOT(requestActivation()));
148 connect(m_container
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
149 m_selectionManager
, SLOT(reset()));
152 //m_previewGenerator = new KFilePreviewGenerator(this);
153 //m_previewGenerator->setPreviewShown(m_container->m_controller->dolphinView()->showPreview());
155 //if (DolphinSettings::instance().generalSettings()->showToolTips()) {
156 // m_toolTipManager = new ToolTipManager(this, m_proxyModel);
159 //m_dirLister->openUrl(url, KDirLister::NoFlags);
161 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
162 this, SLOT(updateFont()));
164 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
165 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
166 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
167 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
169 new VersionControlObserver(this);*/
171 DolphinController
* controller
= m_container
->m_controller
;
172 connect(controller
, SIGNAL(zoomLevelChanged(int)),
173 this, SLOT(setZoomLevel(int)));
175 const QString nameFilter
= controller
->nameFilter();
176 if (!nameFilter
.isEmpty()) {
177 m_proxyModel
->setFilterRegExp(nameFilter
);
179 connect(controller
, SIGNAL(nameFilterChanged(const QString
&)),
180 this, SLOT(setNameFilter(const QString
&)));
182 updateDecorationSize(dolphinView
->showPreview());
185 DolphinColumnView::~DolphinColumnView()
189 delete m_dolphinModel
;
191 m_dirLister
= 0; // deleted by m_dolphinModel
194 void DolphinColumnView::setActive(bool active
)
196 if (active
&& (m_container
->focusProxy() != this)) {
197 m_container
->setFocusProxy(this);
200 if (m_active
!= active
) {
211 /*void DolphinColumnView::setSorting(DolphinView::Sorting sorting)
213 m_proxyModel->setSorting(sorting);
216 void DolphinColumnView::setSortOrder(Qt::SortOrder order)
218 m_proxyModel->setSortOrder(order);
221 void DolphinColumnView::setSortFoldersFirst(bool foldersFirst)
223 m_proxyModel->setSortFoldersFirst(foldersFirst);
226 void DolphinColumnView::setShowHiddenFiles(bool show)
228 if (show != m_dirLister->showingDotFiles()) {
229 m_dirLister->setShowingDotFiles(show);
231 m_dirLister->openUrl(m_url, KDirLister::Reload);
235 void DolphinColumnView::setShowPreview(bool show)
237 m_previewGenerator->setPreviewShown(show);
240 m_dirLister->openUrl(m_url, KDirLister::Reload);
243 void DolphinColumnView::updateBackground()
245 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
246 // cleaning up the cut-indication of DolphinColumnView with the code from
247 // DolphinView a common helper-class should be available which can be shared
248 // by all view implementations -> no hardcoded value anymore
249 const QPalette::ColorRole role
= viewport()->backgroundRole();
250 QColor color
= viewport()->palette().color(role
);
251 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
253 QPalette palette
= viewport()->palette();
254 palette
.setColor(role
, color
);
255 viewport()->setPalette(palette
);
260 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
263 const QModelIndex index
= indexAt(pos
);
264 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
265 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
266 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
271 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
273 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
274 viewOptions
.font
= m_font
;
275 viewOptions
.decorationSize
= m_decorationSize
;
276 viewOptions
.showDecorationSelected
= true;
280 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
282 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_controller
);
285 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
287 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
288 event
->acceptProposedAction();
293 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
295 QListView::dragLeaveEvent(event
);
296 setDirtyRegion(m_dropRect
);
299 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
301 QListView::dragMoveEvent(event
);
303 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
304 const QModelIndex index
= indexAt(event
->pos());
305 setDirtyRegion(m_dropRect
);
307 m_dropRect
.setSize(QSize()); // set as invalid
308 if (index
.isValid()) {
309 m_container
->m_controller
->setItemView(this);
310 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
311 if (!item
.isNull() && item
.isDir()) {
312 m_dropRect
= visualRect(index
);
315 setDirtyRegion(m_dropRect
);
317 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
318 // accept url drops, independently from the destination item
319 event
->acceptProposedAction();
323 void DolphinColumnView::dropEvent(QDropEvent
* event
)
325 const QModelIndex index
= indexAt(event
->pos());
326 m_container
->m_controller
->setItemView(this);
327 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
328 m_container
->m_controller
->indicateDroppedUrls(item
, url(), event
);
329 QListView::dropEvent(event
);
332 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
334 if (!m_childUrl
.isEmpty()) {
335 // indicate the shown URL of the next column by highlighting the shown folder item
336 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
337 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
338 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
339 const QRect itemRect
= visualRect(proxyIndex
);
340 QPainter
painter(viewport());
341 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
343 painter
.setPen(Qt::NoPen
);
344 painter
.setBrush(color
);
345 painter
.drawRect(itemRect
);
349 QListView::paintEvent(event
);
352 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
355 if (!indexAt(event
->pos()).isValid()) {
356 if (QApplication::mouseButtons() & Qt::MidButton
) {
357 m_container
->m_controller
->replaceUrlByClipboard();
359 } else if (event
->button() == Qt::LeftButton
) {
360 // TODO: see comment in DolphinIconsView::mousePressEvent()
361 setState(QAbstractItemView::DraggingState
);
363 QListView::mousePressEvent(event
);
366 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
368 QListView::keyPressEvent(event
);
371 DolphinController
* controller
= m_container
->m_controller
;
372 controller
->handleKeyPressEvent(event
);
373 switch (event
->key()) {
374 case Qt::Key_Right
: {
375 // Special key handling for the column: A Key_Right should
376 // open a new column for the currently selected folder.
377 const QModelIndex index
= currentIndex();
378 const KFileItem item
= controller
->itemForIndex(index
);
379 if (!item
.isNull() && item
.isDir()) {
380 controller
->emitItemTriggered(item
);
386 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
387 QItemSelectionModel::Current
|
388 QItemSelectionModel::Clear
);
395 if (m_toolTipManager
!= 0) {
396 m_toolTipManager
->hideTip();
400 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
403 m_container
->requestActivation(this);
404 Q_ASSERT(m_container
->m_controller
->itemView() == this);
405 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
409 QListView::contextMenuEvent(event
);
411 const QModelIndex index
= indexAt(event
->pos());
412 if (!index
.isValid()) {
416 if (m_toolTipManager
!= 0) {
417 m_toolTipManager
->hideTip();
420 const QPoint pos
= m_container
->viewport()->mapFromGlobal(event
->globalPos());
421 Q_ASSERT(m_container
->m_controller
->itemView() == this);
422 m_container
->m_controller
->triggerContextMenuRequest(pos
);
425 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
427 if (m_selectionManager
!= 0) {
428 m_selectionManager
->reset();
431 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
432 if (event
->modifiers() & Qt::ControlModifier
) {
437 const int height
= m_decorationSize
.height();
438 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
439 verticalScrollBar()->setSingleStep(step
);
441 QListView::wheelEvent(event
);
444 void DolphinColumnView::leaveEvent(QEvent
* event
)
446 QListView::leaveEvent(event
);
447 // if the mouse is above an item and moved very fast outside the widget,
448 // no viewportEntered() signal might be emitted although the mouse has been moved
449 // above the viewport
450 m_container
->m_controller
->emitViewportEntered();
453 void DolphinColumnView::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
455 QListView::selectionChanged(selected
, deselected
);
457 //QItemSelectionModel* selModel = m_container->selectionModel();
458 //selModel->select(selected, QItemSelectionModel::Select);
459 //selModel->select(deselected, QItemSelectionModel::Deselect);
462 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
464 QListView::currentChanged(current
, previous
);
465 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
468 void DolphinColumnView::setNameFilter(const QString
& nameFilter
)
470 m_proxyModel
->setFilterRegExp(nameFilter
);
473 void DolphinColumnView::setZoomLevel(int level
)
475 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
476 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
478 const bool showPreview
= m_container
->m_controller
->dolphinView()->showPreview();
480 settings
->setPreviewSize(size
);
482 settings
->setIconSize(size
);
485 updateDecorationSize(showPreview
);
488 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
490 m_container
->m_controller
->setItemView(this);
491 m_container
->m_controller
->emitItemEntered(index
);
494 void DolphinColumnView::requestActivation()
496 m_container
->m_controller
->setItemView(this);
497 m_container
->m_controller
->requestActivation();
499 m_container
->requestActivation(this);
500 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
501 selectionModel()->clear();
505 void DolphinColumnView::updateFont()
507 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
508 Q_ASSERT(settings
!= 0);
510 if (settings
->useSystemFont()) {
511 m_font
= KGlobalSettings::generalFont();
515 void DolphinColumnView::slotShowPreviewChanged()
517 const DolphinView
* view
= m_container
->m_controller
->dolphinView();
518 updateDecorationSize(view
->showPreview());
521 void DolphinColumnView::activate()
523 setFocus(Qt::OtherFocusReason
);
525 if (KGlobalSettings::singleClick()) {
526 connect(this, SIGNAL(clicked(const QModelIndex
&)),
527 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
529 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
530 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
533 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
534 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
540 void DolphinColumnView::deactivate()
543 if (KGlobalSettings::singleClick()) {
544 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
545 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
547 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
548 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
551 const QModelIndex current
= selectionModel()->currentIndex();
552 selectionModel()->clear();
553 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
557 void DolphinColumnView::updateDecorationSize(bool showPreview
)
559 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
560 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
561 const QSize
size(iconSize
, iconSize
);
564 m_decorationSize
= size
;
566 if (m_selectionManager
!= 0) {
567 m_selectionManager
->reset();
573 #include "dolphincolumnview.moc"