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());
95 connect(this, SIGNAL(viewportEntered()),
96 m_container
->m_controller
, SLOT(emitViewportEntered()));
97 connect(this, SIGNAL(entered(const QModelIndex
&)),
98 this, SLOT(slotEntered(const QModelIndex
&)));
100 const DolphinView
* dolphinView
= m_container
->m_controller
->dolphinView();
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_controller
->dolphinView()->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 DolphinController
* controller
= m_container
->m_controller
;
129 connect(controller
, SIGNAL(zoomLevelChanged(int)),
130 this, SLOT(setZoomLevel(int)));
132 const QString nameFilter
= controller
->nameFilter();
133 if (!nameFilter
.isEmpty()) {
134 m_proxyModel
->setFilterFixedString(nameFilter
);
137 updateDecorationSize(dolphinView
->showPreview());
138 m_extensionsFactory
= new ViewExtensionsFactory(this, controller
);
141 DolphinColumnView::~DolphinColumnView()
145 delete m_dolphinModel
;
147 m_dirLister
= 0; // deleted by m_dolphinModel
150 void DolphinColumnView::setActive(bool active
)
152 if (active
&& (m_container
->focusProxy() != this)) {
153 m_container
->setFocusProxy(this);
156 if (m_active
!= active
) {
167 void DolphinColumnView::updateBackground()
169 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
170 // cleaning up the cut-indication of DolphinColumnView with the code from
171 // DolphinView a common helper-class should be available which can be shared
172 // by all view implementations -> no hardcoded value anymore
173 const QPalette::ColorRole role
= viewport()->backgroundRole();
174 QColor color
= viewport()->palette().color(role
);
175 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
177 QPalette palette
= viewport()->palette();
178 palette
.setColor(role
, color
);
179 viewport()->setPalette(palette
);
184 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
187 const QModelIndex index
= indexAt(pos
);
188 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
189 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
190 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
195 void DolphinColumnView::setSelectionModel(QItemSelectionModel
* model
)
197 // If a change of the selection is done although the view is not active
198 // (e. g. by the selection markers), the column must be activated. This
199 // is done by listening to the current selectionChanged() signal.
200 if (selectionModel() != 0) {
201 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
202 this, SLOT(requestActivation()));
205 QListView::setSelectionModel(model
);
207 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
208 this, SLOT(requestActivation()));
211 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
213 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
214 viewOptions
.font
= m_font
;
215 viewOptions
.fontMetrics
= QFontMetrics(m_font
);
216 viewOptions
.decorationSize
= m_decorationSize
;
217 viewOptions
.showDecorationSelected
= true;
221 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
223 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_controller
);
226 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
228 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
229 event
->acceptProposedAction();
234 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent
* event
)
236 QListView::dragLeaveEvent(event
);
237 setDirtyRegion(m_dropRect
);
240 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
242 QListView::dragMoveEvent(event
);
244 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
245 const QModelIndex index
= indexAt(event
->pos());
246 setDirtyRegion(m_dropRect
);
248 m_dropRect
.setSize(QSize()); // set as invalid
249 if (index
.isValid()) {
250 m_container
->m_controller
->setItemView(this);
251 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
252 if (!item
.isNull() && item
.isDir()) {
253 m_dropRect
= visualRect(index
);
256 setDirtyRegion(m_dropRect
);
258 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
259 // accept url drops, independently from the destination item
260 event
->acceptProposedAction();
264 void DolphinColumnView::dropEvent(QDropEvent
* event
)
266 const QModelIndex index
= indexAt(event
->pos());
267 m_container
->m_controller
->setItemView(this);
268 const KFileItem item
= m_container
->m_controller
->itemForIndex(index
);
269 m_container
->m_controller
->indicateDroppedUrls(item
, url(), event
);
270 QListView::dropEvent(event
);
273 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
275 if (!m_childUrl
.isEmpty()) {
276 // indicate the shown URL of the next column by highlighting the shown folder item
277 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
278 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
279 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
280 const QRect itemRect
= visualRect(proxyIndex
);
281 QPainter
painter(viewport());
282 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
284 painter
.setPen(Qt::NoPen
);
285 painter
.setBrush(color
);
286 painter
.drawRect(itemRect
);
290 QListView::paintEvent(event
);
293 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
296 if (!indexAt(event
->pos()).isValid()) {
297 if (QApplication::mouseButtons() & Qt::MidButton
) {
298 m_container
->m_controller
->replaceUrlByClipboard();
300 } else if (event
->button() == Qt::LeftButton
) {
301 // TODO: see comment in DolphinIconsView::mousePressEvent()
302 setState(QAbstractItemView::DraggingState
);
304 QListView::mousePressEvent(event
);
307 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
309 QListView::keyPressEvent(event
);
312 DolphinController
* controller
= m_container
->m_controller
;
313 controller
->handleKeyPressEvent(event
);
314 switch (event
->key()) {
315 case Qt::Key_Right
: {
316 // Special key handling for the column: A Key_Right should
317 // open a new column for the currently selected folder.
318 const QModelIndex index
= currentIndex();
319 const KFileItem item
= controller
->itemForIndex(index
);
320 if (!item
.isNull() && item
.isDir()) {
321 controller
->emitItemTriggered(item
);
327 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
328 QItemSelectionModel::Current
|
329 QItemSelectionModel::Clear
);
337 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
340 m_container
->requestActivation(this);
341 Q_ASSERT(m_container
->m_controller
->itemView() == this);
342 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
346 QListView::contextMenuEvent(event
);
347 m_container
->m_controller
->triggerContextMenuRequest(event
->pos());
350 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
352 const int step
= m_decorationSize
.height();
353 verticalScrollBar()->setSingleStep(step
);
354 QListView::wheelEvent(event
);
357 void DolphinColumnView::leaveEvent(QEvent
* event
)
359 QListView::leaveEvent(event
);
360 // if the mouse is above an item and moved very fast outside the widget,
361 // no viewportEntered() signal might be emitted although the mouse has been moved
362 // above the viewport
363 m_container
->m_controller
->emitViewportEntered();
366 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
368 QListView::currentChanged(current
, previous
);
369 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
372 void DolphinColumnView::setZoomLevel(int level
)
374 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
375 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
377 const bool showPreview
= m_container
->m_controller
->dolphinView()->showPreview();
379 settings
->setPreviewSize(size
);
381 settings
->setIconSize(size
);
384 updateDecorationSize(showPreview
);
387 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
389 m_container
->m_controller
->setItemView(this);
390 m_container
->m_controller
->emitItemEntered(index
);
393 void DolphinColumnView::requestActivation()
395 m_container
->m_controller
->setItemView(this);
396 m_container
->m_controller
->requestActivation();
398 m_container
->requestActivation(this);
399 m_container
->m_controller
->triggerUrlChangeRequest(m_url
);
400 selectionModel()->clear();
404 void DolphinColumnView::updateFont()
406 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
407 Q_ASSERT(settings
!= 0);
409 if (settings
->useSystemFont()) {
410 m_font
= KGlobalSettings::generalFont();
414 void DolphinColumnView::slotShowPreviewChanged()
416 const DolphinView
* view
= m_container
->m_controller
->dolphinView();
417 updateDecorationSize(view
->showPreview());
420 void DolphinColumnView::activate()
422 setFocus(Qt::OtherFocusReason
);
424 if (KGlobalSettings::singleClick()) {
425 connect(this, SIGNAL(clicked(const QModelIndex
&)),
426 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
428 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
429 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
432 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
433 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
439 void DolphinColumnView::deactivate()
442 if (KGlobalSettings::singleClick()) {
443 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
444 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
446 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
447 m_container
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
450 const QModelIndex current
= selectionModel()->currentIndex();
451 selectionModel()->clear();
452 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
456 void DolphinColumnView::updateDecorationSize(bool showPreview
)
458 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
459 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
460 const QSize
size(iconSize
, iconSize
);
463 m_decorationSize
= size
;
468 #include "dolphincolumnview.moc"