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 "dolphinviewcontroller.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 "viewextensionsfactory.h"
35 #include "viewmodecontroller.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(),
89 qRound(settings
->fontSize()),
90 settings
->fontWeight(),
91 settings
->italicFont());
92 m_font
.setPointSizeF(settings
->fontSize());
95 connect(this, SIGNAL(viewportEntered()),
96 m_container
->m_dolphinViewController
, SLOT(emitViewportEntered()));
97 connect(this, SIGNAL(entered(const QModelIndex
&)),
98 this, SLOT(slotEntered(const QModelIndex
&)));
100 const DolphinView
* dolphinView
= m_container
->m_dolphinViewController
->view();
101 connect(dolphinView
, SIGNAL(showPreviewChanged()),
102 this, SLOT(slotShowPreviewChanged()));
104 m_dirLister
= new DolphinDirLister();
105 m_dirLister
->setAutoUpdate(true);
106 m_dirLister
->setMainWindow(window());
107 m_dirLister
->setDelayedMimeTypes(true);
108 const bool showHiddenFiles
= m_container
->m_dolphinViewController
->view()->showHiddenFiles();
109 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
111 m_dolphinModel
= new DolphinModel(this);
112 m_dolphinModel
->setDirLister(m_dirLister
);
113 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
115 m_proxyModel
= new DolphinSortFilterProxyModel(this);
116 m_proxyModel
->setSourceModel(m_dolphinModel
);
117 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
119 m_proxyModel
->setSorting(dolphinView
->sorting());
120 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
121 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
123 setModel(m_proxyModel
);
125 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
126 this, SLOT(updateFont()));
128 DolphinViewController
* dolphinViewController
= m_container
->m_dolphinViewController
;
129 connect(dolphinViewController
, SIGNAL(zoomLevelChanged(int)),
130 this, SLOT(setZoomLevel(int)));
132 const ViewModeController
* viewModeController
= m_container
->m_viewModeController
;
133 const QString nameFilter
= viewModeController
->nameFilter();
134 if (!nameFilter
.isEmpty()) {
135 m_proxyModel
->setFilterFixedString(nameFilter
);
138 updateDecorationSize(dolphinView
->showPreview());
140 m_extensionsFactory
= new ViewExtensionsFactory(this, dolphinViewController
, viewModeController
);
142 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
145 DolphinColumnView::~DolphinColumnView()
149 delete m_dolphinModel
;
151 m_dirLister
= 0; // deleted by m_dolphinModel
154 void DolphinColumnView::setActive(bool active
)
156 if (active
&& (m_container
->focusProxy() != this)) {
157 m_container
->setFocusProxy(this);
160 if (m_active
!= active
) {
171 void DolphinColumnView::updateBackground()
173 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
174 // cleaning up the cut-indication of DolphinColumnView with the code from
175 // DolphinView a common helper-class should be available which can be shared
176 // by all view implementations -> no hardcoded value anymore
177 const QPalette::ColorRole role
= viewport()->backgroundRole();
178 QColor color
= viewport()->palette().color(role
);
179 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
181 QPalette palette
= viewport()->palette();
182 palette
.setColor(role
, color
);
183 viewport()->setPalette(palette
);
188 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
191 const QModelIndex index
= indexAt(pos
);
192 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
193 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
194 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
199 void DolphinColumnView::setSelectionModel(QItemSelectionModel
* model
)
201 // If a change of the selection is done although the view is not active
202 // (e. g. by the selection markers), the column must be activated. This
203 // is done by listening to the current selectionChanged() signal.
204 if (selectionModel() != 0) {
205 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
206 this, SLOT(requestActivation()));
209 QListView::setSelectionModel(model
);
211 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
212 this, SLOT(requestActivation()));
215 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
217 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
218 viewOptions
.font
= m_font
;
219 viewOptions
.fontMetrics
= QFontMetrics(m_font
);
220 viewOptions
.decorationSize
= m_decorationSize
;
221 viewOptions
.showDecorationSelected
= true;
225 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
227 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_dolphinViewController
);
230 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
232 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
233 event
->acceptProposedAction();
238 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
240 QListView::dragLeaveEvent(event
);
241 setDirtyRegion(m_dropRect
);
244 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
246 QListView::dragMoveEvent(event
);
248 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
249 const QModelIndex index
= indexAt(event
->pos());
250 setDirtyRegion(m_dropRect
);
252 m_dropRect
.setSize(QSize()); // set as invalid
253 if (index
.isValid()) {
254 m_container
->m_dolphinViewController
->setItemView(this);
255 const KFileItem item
= m_container
->m_dolphinViewController
->itemForIndex(index
);
256 if (!item
.isNull() && item
.isDir()) {
257 m_dropRect
= visualRect(index
);
260 setDirtyRegion(m_dropRect
);
262 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
263 // accept url drops, independently from the destination item
264 event
->acceptProposedAction();
268 void DolphinColumnView::dropEvent(QDropEvent
* event
)
270 const QModelIndex index
= indexAt(event
->pos());
271 m_container
->m_dolphinViewController
->setItemView(this);
272 const KFileItem item
= m_container
->m_dolphinViewController
->itemForIndex(index
);
273 m_container
->m_dolphinViewController
->indicateDroppedUrls(item
, url(), event
);
274 QListView::dropEvent(event
);
277 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
279 if (!m_childUrl
.isEmpty()) {
280 // indicate the shown URL of the next column by highlighting the shown folder item
281 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
282 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
283 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
284 const QRect itemRect
= visualRect(proxyIndex
);
285 QPainter
painter(viewport());
286 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
288 painter
.setPen(Qt::NoPen
);
289 painter
.setBrush(color
);
290 painter
.drawRect(itemRect
);
294 QListView::paintEvent(event
);
297 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
300 if (!indexAt(event
->pos()).isValid()) {
301 if (QApplication::mouseButtons() & Qt::MidButton
) {
302 m_container
->m_dolphinViewController
->replaceUrlByClipboard();
304 } else if (event
->button() == Qt::LeftButton
) {
305 // TODO: see comment in DolphinIconsView::mousePressEvent()
306 setState(QAbstractItemView::DraggingState
);
308 QListView::mousePressEvent(event
);
311 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
313 QListView::keyPressEvent(event
);
316 DolphinViewController
* controller
= m_container
->m_dolphinViewController
;
317 controller
->handleKeyPressEvent(event
);
318 switch (event
->key()) {
319 case Qt::Key_Right
: {
320 // Special key handling for the column: A Key_Right should
321 // open a new column for the currently selected folder.
322 const QModelIndex index
= currentIndex();
323 const KFileItem item
= controller
->itemForIndex(index
);
324 if (!item
.isNull() && item
.isDir()) {
325 controller
->emitItemTriggered(item
);
331 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
332 QItemSelectionModel::Current
|
333 QItemSelectionModel::Clear
);
341 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
344 QListView::contextMenuEvent(event
);
345 m_container
->m_dolphinViewController
->triggerContextMenuRequest(event
->pos());
348 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
350 const int step
= m_decorationSize
.height();
351 verticalScrollBar()->setSingleStep(step
);
352 QListView::wheelEvent(event
);
355 void DolphinColumnView::leaveEvent(QEvent
* event
)
357 QListView::leaveEvent(event
);
358 // if the mouse is above an item and moved very fast outside the widget,
359 // no viewportEntered() signal might be emitted although the mouse has been moved
360 // above the viewport
361 m_container
->m_dolphinViewController
->emitViewportEntered();
364 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
366 QListView::currentChanged(current
, previous
);
367 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
370 void DolphinColumnView::setZoomLevel(int level
)
372 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
373 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
375 const bool showPreview
= m_container
->m_dolphinViewController
->view()->showPreview();
377 settings
->setPreviewSize(size
);
379 settings
->setIconSize(size
);
382 updateDecorationSize(showPreview
);
385 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
387 m_container
->m_dolphinViewController
->setItemView(this);
388 m_container
->m_dolphinViewController
->emitItemEntered(index
);
391 void DolphinColumnView::requestActivation()
393 m_container
->m_dolphinViewController
->requestActivation();
395 m_container
->requestActivation(this);
396 selectionModel()->clear();
400 void DolphinColumnView::updateFont()
402 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
403 Q_ASSERT(settings
!= 0);
405 if (settings
->useSystemFont()) {
406 m_font
= KGlobalSettings::generalFont();
410 void DolphinColumnView::slotShowPreviewChanged()
412 const DolphinView
* view
= m_container
->m_dolphinViewController
->view();
413 updateDecorationSize(view
->showPreview());
416 void DolphinColumnView::activate()
418 setFocus(Qt::OtherFocusReason
);
420 if (KGlobalSettings::singleClick()) {
421 connect(this, SIGNAL(clicked(const QModelIndex
&)),
422 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
424 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
425 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
428 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
429 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
435 void DolphinColumnView::deactivate()
438 if (KGlobalSettings::singleClick()) {
439 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
440 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
442 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
443 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
446 const QModelIndex current
= selectionModel()->currentIndex();
447 selectionModel()->clear();
448 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
452 void DolphinColumnView::updateDecorationSize(bool showPreview
)
454 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
455 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
456 const QSize
size(iconSize
, iconSize
);
459 m_decorationSize
= size
;
464 #include "dolphincolumnview.moc"