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 updateDecorationSize(dolphinView
->showPreview());
178 DolphinColumnView::~DolphinColumnView()
182 delete m_dolphinModel
;
184 m_dirLister
= 0; // deleted by m_dolphinModel
187 void DolphinColumnView::setActive(bool active
)
189 if (active
&& (m_container
->focusProxy() != this)) {
190 m_container
->setFocusProxy(this);
193 if (m_active
!= active
) {
204 /*void DolphinColumnView::setSorting(DolphinView::Sorting sorting)
206 m_proxyModel->setSorting(sorting);
209 void DolphinColumnView::setSortOrder(Qt::SortOrder order)
211 m_proxyModel->setSortOrder(order);
214 void DolphinColumnView::setSortFoldersFirst(bool foldersFirst)
216 m_proxyModel->setSortFoldersFirst(foldersFirst);
219 void DolphinColumnView::setShowHiddenFiles(bool show)
221 if (show != m_dirLister->showingDotFiles()) {
222 m_dirLister->setShowingDotFiles(show);
224 m_dirLister->openUrl(m_url, KDirLister::Reload);
228 void DolphinColumnView::setShowPreview(bool show)
230 m_previewGenerator->setPreviewShown(show);
233 m_dirLister->openUrl(m_url, KDirLister::Reload);
236 void DolphinColumnView::updateBackground()
238 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
239 // cleaning up the cut-indication of DolphinColumnView with the code from
240 // DolphinView a common helper-class should be available which can be shared
241 // by all view implementations -> no hardcoded value anymore
242 const QPalette::ColorRole role
= viewport()->backgroundRole();
243 QColor color
= viewport()->palette().color(role
);
244 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
246 QPalette palette
= viewport()->palette();
247 palette
.setColor(role
, color
);
248 viewport()->setPalette(palette
);
253 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
256 const QModelIndex index
= indexAt(pos
);
257 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
258 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
259 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
264 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
266 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
267 viewOptions
.font
= m_font
;
268 viewOptions
.decorationSize
= m_decorationSize
;
269 viewOptions
.showDecorationSelected
= true;
273 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
275 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_controller
);
278 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
280 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
281 event
->acceptProposedAction();
286 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
288 QListView::dragLeaveEvent(event
);
289 setDirtyRegion(m_dropRect
);
292 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
294 QListView::dragMoveEvent(event
);
296 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
297 const QModelIndex index
= indexAt(event
->pos());
298 setDirtyRegion(m_dropRect
);
300 m_dropRect
.setSize(QSize()); // set as invalid
301 if (index
.isValid()) {
302 m_container
->m_controller
->setItemView(this);
303 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
304 if (!item
.isNull() && item
.isDir()) {
305 m_dropRect
= visualRect(index
);
308 setDirtyRegion(m_dropRect
);
310 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
311 // accept url drops, independently from the destination item
312 event
->acceptProposedAction();
316 void DolphinColumnView::dropEvent(QDropEvent
* event
)
318 const QModelIndex index
= indexAt(event
->pos());
319 m_container
->m_controller
->setItemView(this);
320 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
321 m_container
->m_controller
->indicateDroppedUrls(item
, url(), event
);
322 QListView::dropEvent(event
);
325 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
327 if (!m_childUrl
.isEmpty()) {
328 // indicate the shown URL of the next column by highlighting the shown folder item
329 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
330 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
331 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
332 const QRect itemRect
= visualRect(proxyIndex
);
333 QPainter
painter(viewport());
334 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
336 painter
.setPen(Qt::NoPen
);
337 painter
.setBrush(color
);
338 painter
.drawRect(itemRect
);
342 QListView::paintEvent(event
);
345 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
348 if (!indexAt(event
->pos()).isValid()) {
349 if (QApplication::mouseButtons() & Qt::MidButton
) {
350 m_container
->m_controller
->replaceUrlByClipboard();
352 } else if (event
->button() == Qt::LeftButton
) {
353 // TODO: see comment in DolphinIconsView::mousePressEvent()
354 setState(QAbstractItemView::DraggingState
);
356 QListView::mousePressEvent(event
);
359 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
361 QListView::keyPressEvent(event
);
364 DolphinController
* controller
= m_container
->m_controller
;
365 controller
->handleKeyPressEvent(event
);
366 switch (event
->key()) {
367 case Qt::Key_Right
: {
368 // Special key handling for the column: A Key_Right should
369 // open a new column for the currently selected folder.
370 const QModelIndex index
= currentIndex();
371 const KFileItem item
= controller
->itemForIndex(index
);
372 if (!item
.isNull() && item
.isDir()) {
373 controller
->emitItemTriggered(item
);
379 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
380 QItemSelectionModel::Current
|
381 QItemSelectionModel::Clear
);
388 if (m_toolTipManager
!= 0) {
389 m_toolTipManager
->hideTip();
393 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
396 m_container
->requestActivation(this);
397 Q_ASSERT(m_container
->m_controller
->itemView() == this);
398 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
402 QListView::contextMenuEvent(event
);
404 const QModelIndex index
= indexAt(event
->pos());
405 if (!index
.isValid()) {
409 if (m_toolTipManager
!= 0) {
410 m_toolTipManager
->hideTip();
413 const QPoint pos
= m_container
->viewport()->mapFromGlobal(event
->globalPos());
414 Q_ASSERT(m_container
->m_controller
->itemView() == this);
415 m_container
->m_controller
->triggerContextMenuRequest(pos
);
418 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
420 if (m_selectionManager
!= 0) {
421 m_selectionManager
->reset();
424 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
425 if (event
->modifiers() & Qt::ControlModifier
) {
430 const int height
= m_decorationSize
.height();
431 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
432 verticalScrollBar()->setSingleStep(step
);
434 QListView::wheelEvent(event
);
437 void DolphinColumnView::leaveEvent(QEvent
* event
)
439 QListView::leaveEvent(event
);
440 // if the mouse is above an item and moved very fast outside the widget,
441 // no viewportEntered() signal might be emitted although the mouse has been moved
442 // above the viewport
443 m_container
->m_controller
->emitViewportEntered();
446 void DolphinColumnView::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
448 QListView::selectionChanged(selected
, deselected
);
450 //QItemSelectionModel* selModel = m_container->selectionModel();
451 //selModel->select(selected, QItemSelectionModel::Select);
452 //selModel->select(deselected, QItemSelectionModel::Deselect);
455 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
457 QListView::currentChanged(current
, previous
);
458 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
462 void DolphinColumnView::setZoomLevel(int level
)
464 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
465 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
467 const bool showPreview
= m_container
->m_controller
->dolphinView()->showPreview();
469 settings
->setPreviewSize(size
);
471 settings
->setIconSize(size
);
474 updateDecorationSize(showPreview
);
477 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
479 m_container
->m_controller
->setItemView(this);
480 m_container
->m_controller
->emitItemEntered(index
);
483 void DolphinColumnView::requestActivation()
485 m_container
->m_controller
->setItemView(this);
486 m_container
->m_controller
->requestActivation();
488 m_container
->requestActivation(this);
489 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
490 selectionModel()->clear();
494 void DolphinColumnView::updateFont()
496 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
497 Q_ASSERT(settings
!= 0);
499 if (settings
->useSystemFont()) {
500 m_font
= KGlobalSettings::generalFont();
504 void DolphinColumnView::slotShowPreviewChanged()
506 const DolphinView
* view
= m_container
->m_controller
->dolphinView();
507 updateDecorationSize(view
->showPreview());
510 void DolphinColumnView::activate()
512 setFocus(Qt::OtherFocusReason
);
514 if (KGlobalSettings::singleClick()) {
515 connect(this, SIGNAL(clicked(const QModelIndex
&)),
516 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
518 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
519 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
522 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
523 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
529 void DolphinColumnView::deactivate()
532 if (KGlobalSettings::singleClick()) {
533 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
534 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
536 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
537 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
540 const QModelIndex current
= selectionModel()->currentIndex();
541 selectionModel()->clear();
542 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
546 void DolphinColumnView::updateDecorationSize(bool showPreview
)
548 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
549 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
550 const QSize
size(iconSize
, iconSize
);
553 m_decorationSize
= size
;
555 if (m_selectionManager
!= 0) {
556 m_selectionManager
->reset();
562 #include "dolphincolumnview.moc"