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"
37 #include <kcolorscheme.h>
38 #include <kdirlister.h>
39 #include <kfileitem.h>
40 #include <kfilepreviewgenerator.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_selectionManager(0),
67 m_previewGenerator(0),
71 setMouseTracking(true);
72 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
73 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
74 setSelectionBehavior(SelectItems
);
75 setSelectionMode(QAbstractItemView::ExtendedSelection
);
76 setDragDropMode(QAbstractItemView::DragDrop
);
77 setDropIndicatorShown(false);
78 setSelectionRectVisible(true);
79 setEditTriggers(QAbstractItemView::NoEditTriggers
);
81 setVerticalScrollMode(QListView::ScrollPerPixel
);
82 setHorizontalScrollMode(QListView::ScrollPerPixel
);
84 m_autoScroller
= new DolphinViewAutoScroller(this);
86 // apply the column mode settings to the widget
87 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
88 Q_ASSERT(settings
!= 0);
90 if (settings
->useSystemFont()) {
91 m_font
= KGlobalSettings::generalFont();
93 m_font
= QFont(settings
->fontFamily(),
95 settings
->fontWeight(),
96 settings
->italicFont());
99 // KFileItemDelegate* delegate = new KFileItemDelegate(this);
100 // delegate->setShowToolTipWhenElided(false);
101 // setItemDelegate(delegate);
105 connect(this, SIGNAL(viewportEntered()),
106 m_container
->m_controller
, SLOT(emitViewportEntered()));
107 connect(this, SIGNAL(entered(const QModelIndex
&)),
108 this, SLOT(slotEntered(const QModelIndex
&)));
110 const DolphinView
* dolphinView
= m_container
->m_controller
->dolphinView();
111 connect(dolphinView
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
112 this, SLOT(slotSortingChanged(DolphinView::Sorting
)));
113 connect(dolphinView
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
114 this, SLOT(slotSortOrderChanged(Qt::SortOrder
)));
115 connect(dolphinView
, SIGNAL(sortFoldersFirstChanged(bool)),
116 this, SLOT(slotSortFoldersFirstChanged(bool)));
117 connect(dolphinView
, SIGNAL(showHiddenFilesChanged()),
118 this, SLOT(slotShowHiddenFilesChanged()));
119 connect(dolphinView
, SIGNAL(showPreviewChanged()),
120 this, SLOT(slotShowPreviewChanged()));
122 m_dirLister
= new DolphinDirLister();
123 m_dirLister
->setAutoUpdate(true);
124 m_dirLister
->setMainWindow(window());
125 m_dirLister
->setDelayedMimeTypes(true);
126 const bool showHiddenFiles
= m_container
->m_controller
->dolphinView()->showHiddenFiles();
127 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
129 m_dolphinModel
= new DolphinModel(this);
130 m_dolphinModel
->setDirLister(m_dirLister
);
131 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
133 m_proxyModel
= new DolphinSortFilterProxyModel(this);
134 m_proxyModel
->setSourceModel(m_dolphinModel
);
135 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
137 m_proxyModel
->setSorting(dolphinView
->sorting());
138 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
139 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
141 setModel(m_proxyModel
);
143 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
144 m_selectionManager
= new SelectionManager(this);
145 connect(m_selectionManager
, SIGNAL(selectionChanged()),
146 this, SLOT(requestActivation()));
147 connect(m_container
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
148 m_selectionManager
, SLOT(reset()));
151 //m_previewGenerator = new KFilePreviewGenerator(this);
152 //m_previewGenerator->setPreviewShown(m_container->m_controller->dolphinView()->showPreview());
154 //if (DolphinSettings::instance().generalSettings()->showToolTips()) {
155 // m_toolTipManager = new ToolTipManager(this, m_proxyModel);
158 //m_dirLister->openUrl(url, KDirLister::NoFlags);
160 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
161 this, SLOT(updateFont()));
163 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
164 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
165 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
166 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
168 new VersionControlObserver(this);*/
170 updateDecorationSize(m_container
->m_controller
->dolphinView()->showPreview());
173 DolphinColumnView::~DolphinColumnView()
177 delete m_dolphinModel
;
179 m_dirLister
= 0; // deleted by m_dolphinModel
182 void DolphinColumnView::setActive(bool active
)
184 if (active
&& (m_container
->focusProxy() != this)) {
185 m_container
->setFocusProxy(this);
188 if (m_active
!= active
) {
199 /*void DolphinColumnView::setSorting(DolphinView::Sorting sorting)
201 m_proxyModel->setSorting(sorting);
204 void DolphinColumnView::setSortOrder(Qt::SortOrder order)
206 m_proxyModel->setSortOrder(order);
209 void DolphinColumnView::setSortFoldersFirst(bool foldersFirst)
211 m_proxyModel->setSortFoldersFirst(foldersFirst);
214 void DolphinColumnView::setShowHiddenFiles(bool show)
216 if (show != m_dirLister->showingDotFiles()) {
217 m_dirLister->setShowingDotFiles(show);
219 m_dirLister->openUrl(m_url, KDirLister::Reload);
223 void DolphinColumnView::setShowPreview(bool show)
225 m_previewGenerator->setPreviewShown(show);
228 m_dirLister->openUrl(m_url, KDirLister::Reload);
231 void DolphinColumnView::updateBackground()
233 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
234 // cleaning up the cut-indication of DolphinColumnView with the code from
235 // DolphinView a common helper-class should be available which can be shared
236 // by all view implementations -> no hardcoded value anymore
237 const QPalette::ColorRole role
= viewport()->backgroundRole();
238 QColor color
= viewport()->palette().color(role
);
239 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
241 QPalette palette
= viewport()->palette();
242 palette
.setColor(role
, color
);
243 viewport()->setPalette(palette
);
248 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
251 const QModelIndex index
= indexAt(pos
);
252 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
253 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
254 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
259 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
261 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
262 viewOptions
.font
= m_font
;
263 viewOptions
.decorationSize
= m_decorationSize
;
264 viewOptions
.showDecorationSelected
= true;
268 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
270 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_controller
);
273 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
275 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
276 event
->acceptProposedAction();
281 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
283 QListView::dragLeaveEvent(event
);
284 setDirtyRegion(m_dropRect
);
287 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
289 QListView::dragMoveEvent(event
);
291 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
292 const QModelIndex index
= indexAt(event
->pos());
293 setDirtyRegion(m_dropRect
);
295 m_dropRect
.setSize(QSize()); // set as invalid
296 if (index
.isValid()) {
297 m_container
->m_controller
->setItemView(this);
298 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
299 if (!item
.isNull() && item
.isDir()) {
300 m_dropRect
= visualRect(index
);
303 setDirtyRegion(m_dropRect
);
305 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
306 // accept url drops, independently from the destination item
307 event
->acceptProposedAction();
311 void DolphinColumnView::dropEvent(QDropEvent
* event
)
313 const QModelIndex index
= indexAt(event
->pos());
314 m_container
->m_controller
->setItemView(this);
315 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
316 m_container
->m_controller
->indicateDroppedUrls(item
, url(), event
);
317 QListView::dropEvent(event
);
320 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
322 if (!m_childUrl
.isEmpty()) {
323 // indicate the shown URL of the next column by highlighting the shown folder item
324 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
325 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
326 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
327 const QRect itemRect
= visualRect(proxyIndex
);
328 QPainter
painter(viewport());
329 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
331 painter
.setPen(Qt::NoPen
);
332 painter
.setBrush(color
);
333 painter
.drawRect(itemRect
);
337 QListView::paintEvent(event
);
340 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
343 if (!indexAt(event
->pos()).isValid()) {
344 if (QApplication::mouseButtons() & Qt::MidButton
) {
345 m_container
->m_controller
->replaceUrlByClipboard();
347 } else if (event
->button() == Qt::LeftButton
) {
348 // TODO: see comment in DolphinIconsView::mousePressEvent()
349 setState(QAbstractItemView::DraggingState
);
351 QListView::mousePressEvent(event
);
354 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
356 QListView::keyPressEvent(event
);
359 DolphinController
* controller
= m_container
->m_controller
;
360 controller
->handleKeyPressEvent(event
);
361 switch (event
->key()) {
362 case Qt::Key_Right
: {
363 // Special key handling for the column: A Key_Right should
364 // open a new column for the currently selected folder.
365 const QModelIndex index
= currentIndex();
366 const KFileItem item
= controller
->itemForIndex(index
);
367 if (!item
.isNull() && item
.isDir()) {
368 controller
->emitItemTriggered(item
);
374 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
375 QItemSelectionModel::Current
|
376 QItemSelectionModel::Clear
);
383 if (m_toolTipManager
!= 0) {
384 m_toolTipManager
->hideTip();
388 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
391 m_container
->requestActivation(this);
392 Q_ASSERT(m_container
->m_controller
->itemView() == this);
393 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
397 QListView::contextMenuEvent(event
);
399 const QModelIndex index
= indexAt(event
->pos());
400 if (!index
.isValid()) {
404 if (m_toolTipManager
!= 0) {
405 m_toolTipManager
->hideTip();
408 const QPoint pos
= m_container
->viewport()->mapFromGlobal(event
->globalPos());
409 Q_ASSERT(m_container
->m_controller
->itemView() == this);
410 m_container
->m_controller
->triggerContextMenuRequest(pos
);
413 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
415 if (m_selectionManager
!= 0) {
416 m_selectionManager
->reset();
419 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
420 if (event
->modifiers() & Qt::ControlModifier
) {
425 const int height
= m_decorationSize
.height();
426 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
427 verticalScrollBar()->setSingleStep(step
);
429 QListView::wheelEvent(event
);
432 void DolphinColumnView::leaveEvent(QEvent
* event
)
434 QListView::leaveEvent(event
);
435 // if the mouse is above an item and moved very fast outside the widget,
436 // no viewportEntered() signal might be emitted although the mouse has been moved
437 // above the viewport
438 m_container
->m_controller
->emitViewportEntered();
441 void DolphinColumnView::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
443 QListView::selectionChanged(selected
, deselected
);
445 //QItemSelectionModel* selModel = m_container->selectionModel();
446 //selModel->select(selected, QItemSelectionModel::Select);
447 //selModel->select(deselected, QItemSelectionModel::Deselect);
450 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
452 QListView::currentChanged(current
, previous
);
453 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
456 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
458 m_container
->m_controller
->setItemView(this);
459 m_container
->m_controller
->emitItemEntered(index
);
462 void DolphinColumnView::requestActivation()
464 m_container
->m_controller
->setItemView(this);
465 m_container
->m_controller
->requestActivation();
467 m_container
->requestActivation(this);
468 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
469 selectionModel()->clear();
473 void DolphinColumnView::updateFont()
475 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
476 Q_ASSERT(settings
!= 0);
478 if (settings
->useSystemFont()) {
479 m_font
= KGlobalSettings::generalFont();
483 void DolphinColumnView::slotShowPreviewChanged()
485 const DolphinView
* view
= m_container
->m_controller
->dolphinView();
486 updateDecorationSize(view
->showPreview());
489 void DolphinColumnView::activate()
491 setFocus(Qt::OtherFocusReason
);
493 if (KGlobalSettings::singleClick()) {
494 connect(this, SIGNAL(clicked(const QModelIndex
&)),
495 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
497 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
498 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
501 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
502 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
508 void DolphinColumnView::deactivate()
511 if (KGlobalSettings::singleClick()) {
512 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
513 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
515 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
516 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
519 const QModelIndex current
= selectionModel()->currentIndex();
520 selectionModel()->clear();
521 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
525 void DolphinColumnView::updateDecorationSize(bool showPreview
)
527 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
528 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
529 const QSize
size(iconSize
, iconSize
);
532 m_decorationSize
= size
;
534 if (m_selectionManager
!= 0) {
535 m_selectionManager
->reset();
541 #include "dolphincolumnview.moc"