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 "viewextensionsfactory.h"
37 #include "zoomlevelinfo.h"
39 #include <kcolorscheme.h>
40 #include <kdirlister.h>
41 #include <kfileitem.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),
61 m_extensionsFactory(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_dirLister->openUrl(url, KDirLister::NoFlags);
153 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
154 this, SLOT(updateFont()));
156 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
157 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
158 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
159 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
161 new VersionControlObserver(this);*/
163 DolphinController
* controller
= m_container
->m_controller
;
164 connect(controller
, SIGNAL(zoomLevelChanged(int)),
165 this, SLOT(setZoomLevel(int)));
167 const QString nameFilter
= controller
->nameFilter();
168 if (!nameFilter
.isEmpty()) {
169 m_proxyModel
->setFilterRegExp(nameFilter
);
171 connect(controller
, SIGNAL(nameFilterChanged(const QString
&)),
172 this, SLOT(setNameFilter(const QString
&)));
174 updateDecorationSize(dolphinView
->showPreview());
176 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
179 DolphinColumnView::~DolphinColumnView()
183 delete m_dolphinModel
;
185 m_dirLister
= 0; // deleted by m_dolphinModel
188 void DolphinColumnView::setActive(bool active
)
190 if (active
&& (m_container
->focusProxy() != this)) {
191 m_container
->setFocusProxy(this);
194 if (m_active
!= active
) {
205 /*void DolphinColumnView::setSorting(DolphinView::Sorting sorting)
207 m_proxyModel->setSorting(sorting);
210 void DolphinColumnView::setSortOrder(Qt::SortOrder order)
212 m_proxyModel->setSortOrder(order);
215 void DolphinColumnView::setSortFoldersFirst(bool foldersFirst)
217 m_proxyModel->setSortFoldersFirst(foldersFirst);
220 void DolphinColumnView::setShowHiddenFiles(bool show)
222 if (show != m_dirLister->showingDotFiles()) {
223 m_dirLister->setShowingDotFiles(show);
225 m_dirLister->openUrl(m_url, KDirLister::Reload);
229 void DolphinColumnView::setShowPreview(bool show)
231 m_previewGenerator->setPreviewShown(show);
234 m_dirLister->openUrl(m_url, KDirLister::Reload);
237 void DolphinColumnView::updateBackground()
239 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
240 // cleaning up the cut-indication of DolphinColumnView with the code from
241 // DolphinView a common helper-class should be available which can be shared
242 // by all view implementations -> no hardcoded value anymore
243 const QPalette::ColorRole role
= viewport()->backgroundRole();
244 QColor color
= viewport()->palette().color(role
);
245 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
247 QPalette palette
= viewport()->palette();
248 palette
.setColor(role
, color
);
249 viewport()->setPalette(palette
);
254 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
257 const QModelIndex index
= indexAt(pos
);
258 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
259 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
260 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
265 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
267 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
268 viewOptions
.font
= m_font
;
269 viewOptions
.decorationSize
= m_decorationSize
;
270 viewOptions
.showDecorationSelected
= true;
274 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
276 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_controller
);
279 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
281 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
282 event
->acceptProposedAction();
287 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
289 QListView::dragLeaveEvent(event
);
290 setDirtyRegion(m_dropRect
);
293 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
295 QListView::dragMoveEvent(event
);
297 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
298 const QModelIndex index
= indexAt(event
->pos());
299 setDirtyRegion(m_dropRect
);
301 m_dropRect
.setSize(QSize()); // set as invalid
302 if (index
.isValid()) {
303 m_container
->m_controller
->setItemView(this);
304 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
305 if (!item
.isNull() && item
.isDir()) {
306 m_dropRect
= visualRect(index
);
309 setDirtyRegion(m_dropRect
);
311 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
312 // accept url drops, independently from the destination item
313 event
->acceptProposedAction();
317 void DolphinColumnView::dropEvent(QDropEvent
* event
)
319 const QModelIndex index
= indexAt(event
->pos());
320 m_container
->m_controller
->setItemView(this);
321 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
322 m_container
->m_controller
->indicateDroppedUrls(item
, url(), event
);
323 QListView::dropEvent(event
);
326 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
328 if (!m_childUrl
.isEmpty()) {
329 // indicate the shown URL of the next column by highlighting the shown folder item
330 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
331 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
332 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
333 const QRect itemRect
= visualRect(proxyIndex
);
334 QPainter
painter(viewport());
335 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
337 painter
.setPen(Qt::NoPen
);
338 painter
.setBrush(color
);
339 painter
.drawRect(itemRect
);
343 QListView::paintEvent(event
);
346 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
349 if (!indexAt(event
->pos()).isValid()) {
350 if (QApplication::mouseButtons() & Qt::MidButton
) {
351 m_container
->m_controller
->replaceUrlByClipboard();
353 } else if (event
->button() == Qt::LeftButton
) {
354 // TODO: see comment in DolphinIconsView::mousePressEvent()
355 setState(QAbstractItemView::DraggingState
);
357 QListView::mousePressEvent(event
);
360 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
362 QListView::keyPressEvent(event
);
365 DolphinController
* controller
= m_container
->m_controller
;
366 controller
->handleKeyPressEvent(event
);
367 switch (event
->key()) {
368 case Qt::Key_Right
: {
369 // Special key handling for the column: A Key_Right should
370 // open a new column for the currently selected folder.
371 const QModelIndex index
= currentIndex();
372 const KFileItem item
= controller
->itemForIndex(index
);
373 if (!item
.isNull() && item
.isDir()) {
374 controller
->emitItemTriggered(item
);
380 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
381 QItemSelectionModel::Current
|
382 QItemSelectionModel::Clear
);
390 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
393 m_container
->requestActivation(this);
394 Q_ASSERT(m_container
->m_controller
->itemView() == this);
395 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
399 QListView::contextMenuEvent(event
);
401 const QModelIndex index
= indexAt(event
->pos());
402 if (!index
.isValid()) {
406 const QPoint pos
= m_container
->viewport()->mapFromGlobal(event
->globalPos());
407 Q_ASSERT(m_container
->m_controller
->itemView() == this);
408 m_container
->m_controller
->triggerContextMenuRequest(pos
);
411 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
413 if (m_selectionManager
!= 0) {
414 m_selectionManager
->reset();
417 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
418 if (event
->modifiers() & Qt::ControlModifier
) {
423 const int height
= m_decorationSize
.height();
424 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
425 verticalScrollBar()->setSingleStep(step
);
427 QListView::wheelEvent(event
);
430 void DolphinColumnView::leaveEvent(QEvent
* event
)
432 QListView::leaveEvent(event
);
433 // if the mouse is above an item and moved very fast outside the widget,
434 // no viewportEntered() signal might be emitted although the mouse has been moved
435 // above the viewport
436 m_container
->m_controller
->emitViewportEntered();
439 void DolphinColumnView::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
441 QListView::selectionChanged(selected
, deselected
);
443 //QItemSelectionModel* selModel = m_container->selectionModel();
444 //selModel->select(selected, QItemSelectionModel::Select);
445 //selModel->select(deselected, QItemSelectionModel::Deselect);
448 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
450 QListView::currentChanged(current
, previous
);
451 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
454 void DolphinColumnView::setNameFilter(const QString
& nameFilter
)
456 m_proxyModel
->setFilterRegExp(nameFilter
);
459 void DolphinColumnView::setZoomLevel(int level
)
461 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
462 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
464 const bool showPreview
= m_container
->m_controller
->dolphinView()->showPreview();
466 settings
->setPreviewSize(size
);
468 settings
->setIconSize(size
);
471 updateDecorationSize(showPreview
);
474 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
476 m_container
->m_controller
->setItemView(this);
477 m_container
->m_controller
->emitItemEntered(index
);
480 void DolphinColumnView::requestActivation()
482 m_container
->m_controller
->setItemView(this);
483 m_container
->m_controller
->requestActivation();
485 m_container
->requestActivation(this);
486 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
487 selectionModel()->clear();
491 void DolphinColumnView::updateFont()
493 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
494 Q_ASSERT(settings
!= 0);
496 if (settings
->useSystemFont()) {
497 m_font
= KGlobalSettings::generalFont();
501 void DolphinColumnView::slotShowPreviewChanged()
503 const DolphinView
* view
= m_container
->m_controller
->dolphinView();
504 updateDecorationSize(view
->showPreview());
507 void DolphinColumnView::activate()
509 setFocus(Qt::OtherFocusReason
);
511 if (KGlobalSettings::singleClick()) {
512 connect(this, SIGNAL(clicked(const QModelIndex
&)),
513 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
515 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
516 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
519 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
520 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
526 void DolphinColumnView::deactivate()
529 if (KGlobalSettings::singleClick()) {
530 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
531 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
533 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
534 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
537 const QModelIndex current
= selectionModel()->currentIndex();
538 selectionModel()->clear();
539 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
543 void DolphinColumnView::updateDecorationSize(bool showPreview
)
545 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
546 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
547 const QSize
size(iconSize
, iconSize
);
550 m_decorationSize
= size
;
552 if (m_selectionManager
!= 0) {
553 m_selectionManager
->reset();
559 #include "dolphincolumnview.moc"