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(showPreviewChanged()),
103 this, SLOT(slotShowPreviewChanged()));
105 m_dirLister
= new DolphinDirLister();
106 m_dirLister
->setAutoUpdate(true);
107 m_dirLister
->setMainWindow(window());
108 m_dirLister
->setDelayedMimeTypes(true);
109 const bool showHiddenFiles
= m_container
->m_controller
->dolphinView()->showHiddenFiles();
110 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
112 m_dolphinModel
= new DolphinModel(this);
113 m_dolphinModel
->setDirLister(m_dirLister
);
114 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
116 m_proxyModel
= new DolphinSortFilterProxyModel(this);
117 m_proxyModel
->setSourceModel(m_dolphinModel
);
118 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
120 m_proxyModel
->setSorting(dolphinView
->sorting());
121 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
122 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
124 setModel(m_proxyModel
);
126 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
127 this, SLOT(updateFont()));
129 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
130 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
131 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
132 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
134 new VersionControlObserver(this);*/
136 DolphinController
* controller
= m_container
->m_controller
;
137 connect(controller
, SIGNAL(zoomLevelChanged(int)),
138 this, SLOT(setZoomLevel(int)));
140 const QString nameFilter
= controller
->nameFilter();
141 if (!nameFilter
.isEmpty()) {
142 m_proxyModel
->setFilterRegExp(nameFilter
);
144 connect(controller
, SIGNAL(nameFilterChanged(const QString
&)),
145 this, SLOT(setNameFilter(const QString
&)));
147 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
148 updateDecorationSize(dolphinView
->showPreview());
151 DolphinColumnView::~DolphinColumnView()
155 delete m_dolphinModel
;
157 m_dirLister
= 0; // deleted by m_dolphinModel
160 void DolphinColumnView::setActive(bool active
)
162 if (active
&& (m_container
->focusProxy() != this)) {
163 m_container
->setFocusProxy(this);
166 if (m_active
!= active
) {
177 void DolphinColumnView::updateBackground()
179 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
180 // cleaning up the cut-indication of DolphinColumnView with the code from
181 // DolphinView a common helper-class should be available which can be shared
182 // by all view implementations -> no hardcoded value anymore
183 const QPalette::ColorRole role
= viewport()->backgroundRole();
184 QColor color
= viewport()->palette().color(role
);
185 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
187 QPalette palette
= viewport()->palette();
188 palette
.setColor(role
, color
);
189 viewport()->setPalette(palette
);
194 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
197 const QModelIndex index
= indexAt(pos
);
198 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
199 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
200 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
205 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
207 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
208 viewOptions
.font
= m_font
;
209 viewOptions
.decorationSize
= m_decorationSize
;
210 viewOptions
.showDecorationSelected
= true;
214 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
216 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_controller
);
219 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
221 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
222 event
->acceptProposedAction();
227 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
229 QListView::dragLeaveEvent(event
);
230 setDirtyRegion(m_dropRect
);
233 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
235 QListView::dragMoveEvent(event
);
237 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
238 const QModelIndex index
= indexAt(event
->pos());
239 setDirtyRegion(m_dropRect
);
241 m_dropRect
.setSize(QSize()); // set as invalid
242 if (index
.isValid()) {
243 m_container
->m_controller
->setItemView(this);
244 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
245 if (!item
.isNull() && item
.isDir()) {
246 m_dropRect
= visualRect(index
);
249 setDirtyRegion(m_dropRect
);
251 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
252 // accept url drops, independently from the destination item
253 event
->acceptProposedAction();
257 void DolphinColumnView::dropEvent(QDropEvent
* event
)
259 const QModelIndex index
= indexAt(event
->pos());
260 m_container
->m_controller
->setItemView(this);
261 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
262 m_container
->m_controller
->indicateDroppedUrls(item
, url(), event
);
263 QListView::dropEvent(event
);
266 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
268 if (!m_childUrl
.isEmpty()) {
269 // indicate the shown URL of the next column by highlighting the shown folder item
270 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
271 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
272 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
273 const QRect itemRect
= visualRect(proxyIndex
);
274 QPainter
painter(viewport());
275 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
277 painter
.setPen(Qt::NoPen
);
278 painter
.setBrush(color
);
279 painter
.drawRect(itemRect
);
283 QListView::paintEvent(event
);
286 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
289 if (!indexAt(event
->pos()).isValid()) {
290 if (QApplication::mouseButtons() & Qt::MidButton
) {
291 m_container
->m_controller
->replaceUrlByClipboard();
293 } else if (event
->button() == Qt::LeftButton
) {
294 // TODO: see comment in DolphinIconsView::mousePressEvent()
295 setState(QAbstractItemView::DraggingState
);
297 QListView::mousePressEvent(event
);
300 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
302 QListView::keyPressEvent(event
);
305 DolphinController
* controller
= m_container
->m_controller
;
306 controller
->handleKeyPressEvent(event
);
307 switch (event
->key()) {
308 case Qt::Key_Right
: {
309 // Special key handling for the column: A Key_Right should
310 // open a new column for the currently selected folder.
311 const QModelIndex index
= currentIndex();
312 const KFileItem item
= controller
->itemForIndex(index
);
313 if (!item
.isNull() && item
.isDir()) {
314 controller
->emitItemTriggered(item
);
320 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
321 QItemSelectionModel::Current
|
322 QItemSelectionModel::Clear
);
330 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
333 m_container
->requestActivation(this);
334 Q_ASSERT(m_container
->m_controller
->itemView() == this);
335 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
339 QListView::contextMenuEvent(event
);
341 const QModelIndex index
= indexAt(event
->pos());
342 if (!index
.isValid()) {
346 const QPoint pos
= m_container
->viewport()->mapFromGlobal(event
->globalPos());
347 Q_ASSERT(m_container
->m_controller
->itemView() == this);
348 m_container
->m_controller
->triggerContextMenuRequest(pos
);
351 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
353 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
354 if (event
->modifiers() & Qt::ControlModifier
) {
359 const int height
= m_decorationSize
.height();
360 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
361 verticalScrollBar()->setSingleStep(step
);
363 QListView::wheelEvent(event
);
366 void DolphinColumnView::leaveEvent(QEvent
* event
)
368 QListView::leaveEvent(event
);
369 // if the mouse is above an item and moved very fast outside the widget,
370 // no viewportEntered() signal might be emitted although the mouse has been moved
371 // above the viewport
372 m_container
->m_controller
->emitViewportEntered();
375 void DolphinColumnView::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
377 QListView::selectionChanged(selected
, deselected
);
379 //QItemSelectionModel* selModel = m_container->selectionModel();
380 //selModel->select(selected, QItemSelectionModel::Select);
381 //selModel->select(deselected, QItemSelectionModel::Deselect);
384 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
386 QListView::currentChanged(current
, previous
);
387 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
390 void DolphinColumnView::setNameFilter(const QString
& nameFilter
)
392 m_proxyModel
->setFilterRegExp(nameFilter
);
395 void DolphinColumnView::setZoomLevel(int level
)
397 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
398 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
400 const bool showPreview
= m_container
->m_controller
->dolphinView()->showPreview();
402 settings
->setPreviewSize(size
);
404 settings
->setIconSize(size
);
407 updateDecorationSize(showPreview
);
410 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
412 m_container
->m_controller
->setItemView(this);
413 m_container
->m_controller
->emitItemEntered(index
);
416 void DolphinColumnView::requestActivation()
418 m_container
->m_controller
->setItemView(this);
419 m_container
->m_controller
->requestActivation();
421 m_container
->requestActivation(this);
422 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
423 selectionModel()->clear();
427 void DolphinColumnView::updateFont()
429 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
430 Q_ASSERT(settings
!= 0);
432 if (settings
->useSystemFont()) {
433 m_font
= KGlobalSettings::generalFont();
437 void DolphinColumnView::slotShowPreviewChanged()
439 const DolphinView
* view
= m_container
->m_controller
->dolphinView();
440 updateDecorationSize(view
->showPreview());
443 void DolphinColumnView::activate()
445 setFocus(Qt::OtherFocusReason
);
447 if (KGlobalSettings::singleClick()) {
448 connect(this, SIGNAL(clicked(const QModelIndex
&)),
449 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
451 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
452 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
455 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
456 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
462 void DolphinColumnView::deactivate()
465 if (KGlobalSettings::singleClick()) {
466 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
467 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
469 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
470 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
473 const QModelIndex current
= selectionModel()->currentIndex();
474 selectionModel()->clear();
475 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
479 void DolphinColumnView::updateDecorationSize(bool showPreview
)
481 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
482 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
483 const QSize
size(iconSize
, iconSize
);
486 m_decorationSize
= size
;
491 #include "dolphincolumnview.moc"