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 "viewextensionsfactory.h"
35 #include "zoomlevelinfo.h"
37 #include <kcolorscheme.h>
38 #include <kdirlister.h>
39 #include <kfileitem.h>
40 #include <kio/previewjob.h>
41 #include <kiconeffect.h>
43 #include <konqmimedata.h>
45 #include <QApplication>
51 DolphinColumnView::DolphinColumnView(QWidget
* parent
,
52 DolphinColumnViewContainer
* container
,
56 m_container(container
),
57 m_extensionsFactory(0),
67 setMouseTracking(true);
68 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
69 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
70 setSelectionBehavior(SelectItems
);
71 setSelectionMode(QAbstractItemView::ExtendedSelection
);
72 setDragDropMode(QAbstractItemView::DragDrop
);
73 setDropIndicatorShown(false);
74 setSelectionRectVisible(true);
75 setEditTriggers(QAbstractItemView::NoEditTriggers
);
77 setVerticalScrollMode(QListView::ScrollPerPixel
);
78 setHorizontalScrollMode(QListView::ScrollPerPixel
);
80 // apply the column mode settings to the widget
81 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
82 Q_ASSERT(settings
!= 0);
84 if (settings
->useSystemFont()) {
85 m_font
= KGlobalSettings::generalFont();
87 m_font
= QFont(settings
->fontFamily(),
89 settings
->fontWeight(),
90 settings
->italicFont());
93 connect(this, SIGNAL(viewportEntered()),
94 m_container
->m_controller
, SLOT(emitViewportEntered()));
95 connect(this, SIGNAL(entered(const QModelIndex
&)),
96 this, SLOT(slotEntered(const QModelIndex
&)));
98 const DolphinView
* dolphinView
= m_container
->m_controller
->dolphinView();
99 connect(dolphinView
, SIGNAL(showPreviewChanged()),
100 this, SLOT(slotShowPreviewChanged()));
102 m_dirLister
= new DolphinDirLister();
103 m_dirLister
->setAutoUpdate(true);
104 m_dirLister
->setMainWindow(window());
105 m_dirLister
->setDelayedMimeTypes(true);
106 const bool showHiddenFiles
= m_container
->m_controller
->dolphinView()->showHiddenFiles();
107 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
109 m_dolphinModel
= new DolphinModel(this);
110 m_dolphinModel
->setDirLister(m_dirLister
);
111 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
113 m_proxyModel
= new DolphinSortFilterProxyModel(this);
114 m_proxyModel
->setSourceModel(m_dolphinModel
);
115 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
117 m_proxyModel
->setSorting(dolphinView
->sorting());
118 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
119 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
121 setModel(m_proxyModel
);
123 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
124 this, SLOT(updateFont()));
126 DolphinController
* controller
= m_container
->m_controller
;
127 connect(controller
, SIGNAL(zoomLevelChanged(int)),
128 this, SLOT(setZoomLevel(int)));
130 const QString nameFilter
= controller
->nameFilter();
131 if (!nameFilter
.isEmpty()) {
132 m_proxyModel
->setFilterFixedString(nameFilter
);
135 updateDecorationSize(dolphinView
->showPreview());
136 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
139 DolphinColumnView::~DolphinColumnView()
143 delete m_dolphinModel
;
145 m_dirLister
= 0; // deleted by m_dolphinModel
148 void DolphinColumnView::setActive(bool active
)
150 if (active
&& (m_container
->focusProxy() != this)) {
151 m_container
->setFocusProxy(this);
154 if (m_active
!= active
) {
165 void DolphinColumnView::updateBackground()
167 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
168 // cleaning up the cut-indication of DolphinColumnView with the code from
169 // DolphinView a common helper-class should be available which can be shared
170 // by all view implementations -> no hardcoded value anymore
171 const QPalette::ColorRole role
= viewport()->backgroundRole();
172 QColor color
= viewport()->palette().color(role
);
173 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
175 QPalette palette
= viewport()->palette();
176 palette
.setColor(role
, color
);
177 viewport()->setPalette(palette
);
182 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
185 const QModelIndex index
= indexAt(pos
);
186 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
187 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
188 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
193 void DolphinColumnView::setSelectionModel(QItemSelectionModel
* model
)
195 // If a change of the selection is done although the view is not active
196 // (e. g. by the selection markers), the column must be activated. This
197 // is done by listening to the current selectionChanged() signal.
198 if (selectionModel() != 0) {
199 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
200 this, SLOT(requestActivation()));
203 QListView::setSelectionModel(model
);
205 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
206 this, SLOT(requestActivation()));
209 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
211 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
212 viewOptions
.font
= m_font
;
213 viewOptions
.fontMetrics
= QFontMetrics(m_font
);
214 viewOptions
.decorationSize
= m_decorationSize
;
215 viewOptions
.showDecorationSelected
= true;
219 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
221 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_controller
);
224 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
226 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
227 event
->acceptProposedAction();
232 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
234 QListView::dragLeaveEvent(event
);
235 setDirtyRegion(m_dropRect
);
238 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
240 QListView::dragMoveEvent(event
);
242 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
243 const QModelIndex index
= indexAt(event
->pos());
244 setDirtyRegion(m_dropRect
);
246 m_dropRect
.setSize(QSize()); // set as invalid
247 if (index
.isValid()) {
248 m_container
->m_controller
->setItemView(this);
249 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
250 if (!item
.isNull() && item
.isDir()) {
251 m_dropRect
= visualRect(index
);
254 setDirtyRegion(m_dropRect
);
256 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
257 // accept url drops, independently from the destination item
258 event
->acceptProposedAction();
262 void DolphinColumnView::dropEvent(QDropEvent
* event
)
264 const QModelIndex index
= indexAt(event
->pos());
265 m_container
->m_controller
->setItemView(this);
266 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
267 m_container
->m_controller
->indicateDroppedUrls(item
, url(), event
);
268 QListView::dropEvent(event
);
271 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
273 if (!m_childUrl
.isEmpty()) {
274 // indicate the shown URL of the next column by highlighting the shown folder item
275 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
276 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
277 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
278 const QRect itemRect
= visualRect(proxyIndex
);
279 QPainter
painter(viewport());
280 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
282 painter
.setPen(Qt::NoPen
);
283 painter
.setBrush(color
);
284 painter
.drawRect(itemRect
);
288 QListView::paintEvent(event
);
291 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
294 if (!indexAt(event
->pos()).isValid()) {
295 if (QApplication::mouseButtons() & Qt::MidButton
) {
296 m_container
->m_controller
->replaceUrlByClipboard();
298 } else if (event
->button() == Qt::LeftButton
) {
299 // TODO: see comment in DolphinIconsView::mousePressEvent()
300 setState(QAbstractItemView::DraggingState
);
302 QListView::mousePressEvent(event
);
305 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
307 QListView::keyPressEvent(event
);
310 DolphinController
* controller
= m_container
->m_controller
;
311 controller
->handleKeyPressEvent(event
);
312 switch (event
->key()) {
313 case Qt::Key_Right
: {
314 // Special key handling for the column: A Key_Right should
315 // open a new column for the currently selected folder.
316 const QModelIndex index
= currentIndex();
317 const KFileItem item
= controller
->itemForIndex(index
);
318 if (!item
.isNull() && item
.isDir()) {
319 controller
->emitItemTriggered(item
);
325 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
326 QItemSelectionModel::Current
|
327 QItemSelectionModel::Clear
);
335 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
338 QListView::contextMenuEvent(event
);
339 m_container
->m_controller
->triggerContextMenuRequest(event
->pos());
342 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
344 const int step
= m_decorationSize
.height();
345 verticalScrollBar()->setSingleStep(step
);
346 QListView::wheelEvent(event
);
349 void DolphinColumnView::leaveEvent(QEvent
* event
)
351 QListView::leaveEvent(event
);
352 // if the mouse is above an item and moved very fast outside the widget,
353 // no viewportEntered() signal might be emitted although the mouse has been moved
354 // above the viewport
355 m_container
->m_controller
->emitViewportEntered();
358 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
360 QListView::currentChanged(current
, previous
);
361 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
364 void DolphinColumnView::setZoomLevel(int level
)
366 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
367 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
369 const bool showPreview
= m_container
->m_controller
->dolphinView()->showPreview();
371 settings
->setPreviewSize(size
);
373 settings
->setIconSize(size
);
376 updateDecorationSize(showPreview
);
379 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
381 m_container
->m_controller
->setItemView(this);
382 m_container
->m_controller
->emitItemEntered(index
);
385 void DolphinColumnView::requestActivation()
388 m_container
->requestActivation(this);
389 selectionModel()->clear();
393 void DolphinColumnView::updateFont()
395 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
396 Q_ASSERT(settings
!= 0);
398 if (settings
->useSystemFont()) {
399 m_font
= KGlobalSettings::generalFont();
403 void DolphinColumnView::slotShowPreviewChanged()
405 const DolphinView
* view
= m_container
->m_controller
->dolphinView();
406 updateDecorationSize(view
->showPreview());
409 void DolphinColumnView::activate()
411 setFocus(Qt::OtherFocusReason
);
413 if (KGlobalSettings::singleClick()) {
414 connect(this, SIGNAL(clicked(const QModelIndex
&)),
415 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
417 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
418 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
421 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
422 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
428 void DolphinColumnView::deactivate()
431 if (KGlobalSettings::singleClick()) {
432 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
433 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
435 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
436 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
439 const QModelIndex current
= selectionModel()->currentIndex();
440 selectionModel()->clear();
441 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
445 void DolphinColumnView::updateDecorationSize(bool showPreview
)
447 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
448 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
449 const QSize
size(iconSize
, iconSize
);
452 m_decorationSize
= size
;
457 #include "dolphincolumnview.moc"