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 "dolphinfileitemdelegate.h"
27 #include "dolphinsortfilterproxymodel.h"
28 #include "settings/dolphinsettings.h"
29 #include "dolphinviewautoscroller.h"
30 #include "dolphin_columnmodesettings.h"
31 #include "dolphin_generalsettings.h"
32 #include "draganddrophelper.h"
33 #include "folderexpander.h"
34 #include "tooltips/tooltipmanager.h"
35 #include "viewextensionsfactory.h"
36 #include "viewmodecontroller.h"
37 #include "zoomlevelinfo.h"
39 #include <kcolorscheme.h>
40 #include <kdirlister.h>
41 #include <kfileitem.h>
42 #include <kio/previewjob.h>
44 #include <kiconeffect.h>
47 #include <konqmimedata.h>
49 #include <QApplication>
51 #include <QHeaderView>
57 DolphinColumnView::DolphinColumnView(QWidget
* parent
,
58 DolphinColumnViewContainer
* container
,
60 DolphinTreeView(parent
),
62 m_container(container
),
63 m_extensionsFactory(0),
74 setMouseTracking(true);
76 setUniformRowHeights(true);
77 setSelectionBehavior(SelectItems
);
78 setSelectionMode(QAbstractItemView::ExtendedSelection
);
79 setDragDropMode(QAbstractItemView::DragDrop
);
80 setDropIndicatorShown(false);
81 setRootIsDecorated(false);
82 setItemsExpandable(false);
83 setEditTriggers(QAbstractItemView::NoEditTriggers
);
84 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
86 m_resizeWidget
= new QLabel(this);
87 m_resizeWidget
->setPixmap(KIcon("transform-move").pixmap(KIconLoader::SizeSmall
));
88 m_resizeWidget
->setToolTip(i18nc("@info:tooltip", "Resize column"));
89 setCornerWidget(m_resizeWidget
);
90 m_resizeWidget
->installEventFilter(this);
92 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
93 Q_ASSERT(settings
!= 0);
95 if (settings
->useSystemFont()) {
96 m_font
= KGlobalSettings::generalFont();
98 m_font
= QFont(settings
->fontFamily(),
99 qRound(settings
->fontSize()),
100 settings
->fontWeight(),
101 settings
->italicFont());
102 m_font
.setPointSizeF(settings
->fontSize());
105 setMinimumWidth(settings
->fontSize() * 10);
106 setMaximumWidth(settings
->columnWidth());
108 connect(this, SIGNAL(viewportEntered()),
109 m_container
->m_dolphinViewController
, SLOT(emitViewportEntered()));
110 connect(this, SIGNAL(entered(const QModelIndex
&)),
111 this, SLOT(slotEntered(const QModelIndex
&)));
113 const DolphinView
* dolphinView
= m_container
->m_dolphinViewController
->view();
114 connect(dolphinView
, SIGNAL(showPreviewChanged()),
115 this, SLOT(slotShowPreviewChanged()));
117 m_dirLister
= new DolphinDirLister();
118 m_dirLister
->setAutoUpdate(true);
119 m_dirLister
->setMainWindow(window());
120 m_dirLister
->setDelayedMimeTypes(true);
121 const bool showHiddenFiles
= m_container
->m_dolphinViewController
->view()->showHiddenFiles();
122 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
124 m_dolphinModel
= new DolphinModel(this);
125 m_dolphinModel
->setDirLister(m_dirLister
);
126 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
128 m_proxyModel
= new DolphinSortFilterProxyModel(this);
129 m_proxyModel
->setSourceModel(m_dolphinModel
);
130 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
132 m_proxyModel
->setSorting(dolphinView
->sorting());
133 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
134 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
136 setModel(m_proxyModel
);
138 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
139 this, SLOT(updateFont()));
141 const ViewModeController
* viewModeController
= m_container
->m_viewModeController
;
142 connect(viewModeController
, SIGNAL(zoomLevelChanged(int)),
143 this, SLOT(setZoomLevel(int)));
144 const QString nameFilter
= viewModeController
->nameFilter();
145 if (!nameFilter
.isEmpty()) {
146 m_proxyModel
->setFilterFixedString(nameFilter
);
149 updateDecorationSize(dolphinView
->showPreview());
152 DolphinViewController
* dolphinViewController
= m_container
->m_dolphinViewController
;
153 m_extensionsFactory
= new ViewExtensionsFactory(this, dolphinViewController
, viewModeController
);
154 m_extensionsFactory
->fileItemDelegate()->setMinimizedNameColumn(true);
156 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
159 DolphinColumnView::~DolphinColumnView()
163 delete m_dolphinModel
;
165 m_dirLister
= 0; // deleted by m_dolphinModel
168 void DolphinColumnView::setActive(bool active
)
170 if (m_active
!= active
) {
181 void DolphinColumnView::updateBackground()
183 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
184 // cleaning up the cut-indication of DolphinColumnView with the code from
185 // DolphinView a common helper-class should be available which can be shared
186 // by all view implementations -> no hardcoded value anymore
187 const QPalette::ColorRole role
= viewport()->backgroundRole();
188 QColor color
= viewport()->palette().color(role
);
189 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
191 QPalette palette
= viewport()->palette();
192 palette
.setColor(role
, color
);
193 viewport()->setPalette(palette
);
198 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
201 const QModelIndex index
= indexAt(pos
);
202 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
203 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
204 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
209 void DolphinColumnView::setSelectionModel(QItemSelectionModel
* model
)
211 // If a change of the selection is done although the view is not active
212 // (e. g. by the selection markers), the column must be activated. This
213 // is done by listening to the current selectionChanged() signal.
214 if (selectionModel() != 0) {
215 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
216 this, SLOT(requestActivation()));
219 DolphinTreeView::setSelectionModel(model
);
221 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
222 this, SLOT(requestActivation()));
225 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
227 QStyleOptionViewItem viewOptions
= DolphinTreeView::viewOptions();
228 viewOptions
.font
= m_font
;
229 viewOptions
.fontMetrics
= QFontMetrics(m_font
);
230 viewOptions
.decorationSize
= m_decorationSize
;
231 viewOptions
.showDecorationSelected
= true;
235 bool DolphinColumnView::event(QEvent
* event
)
237 if (event
->type() == QEvent::Polish
) {
238 // Hide all columns except of the 'Name' column
239 for (int i
= DolphinModel::Name
+ 1; i
< DolphinModel::ExtraColumnCount
; ++i
) {
245 return DolphinTreeView::event(event
);
248 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
250 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_dolphinViewController
);
251 DolphinTreeView::startDrag(supportedActions
);
254 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
256 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
257 event
->acceptProposedAction();
260 DolphinTreeView::dragEnterEvent(event
);
263 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
265 DolphinTreeView::dragMoveEvent(event
);
267 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
268 // accept url drops, independently from the destination item
269 event
->acceptProposedAction();
273 void DolphinColumnView::dropEvent(QDropEvent
* event
)
275 const QModelIndex index
= indexAt(event
->pos());
276 m_container
->m_dolphinViewController
->setItemView(this);
277 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
278 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
279 m_container
->m_dolphinViewController
->indicateDroppedUrls(item
, url(), event
);
280 DolphinTreeView::dropEvent(event
);
283 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
285 if (!m_childUrl
.isEmpty()) {
286 // Indicate the shown URL of the next column by highlighting the shown folder item
287 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
288 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
289 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
290 QPainter
painter(viewport());
292 QStyleOptionViewItemV4 option
;
293 option
.initFrom(this);
294 option
.rect
= visualRect(proxyIndex
);
295 option
.state
= QStyle::State_Enabled
| QStyle::State_HasFocus
;
296 option
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
297 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &option
, &painter
, this);
301 DolphinTreeView::paintEvent(event
);
304 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
307 if (!indexAt(event
->pos()).isValid() && (QApplication::mouseButtons() & Qt::MidButton
)) {
308 m_container
->m_dolphinViewController
->replaceUrlByClipboard();
311 DolphinTreeView::mousePressEvent(event
);
314 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
316 DolphinTreeView::keyPressEvent(event
);
318 DolphinViewController
* controller
= m_container
->m_dolphinViewController
;
319 controller
->handleKeyPressEvent(event
);
320 switch (event
->key()) {
321 case Qt::Key_Right
: {
322 // Special key handling for the column: A Key_Right should
323 // open a new column for the currently selected folder.
324 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(currentIndex());
325 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
326 if (!item
.isNull() && item
.isDir()) {
327 controller
->emitItemTriggered(item
);
333 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
334 QItemSelectionModel::Current
|
335 QItemSelectionModel::Clear
);
343 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
346 DolphinTreeView::contextMenuEvent(event
);
347 m_container
->m_dolphinViewController
->triggerContextMenuRequest(event
->pos());
350 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
352 const int step
= m_decorationSize
.height();
353 verticalScrollBar()->setSingleStep(step
);
354 DolphinTreeView::wheelEvent(event
);
357 void DolphinColumnView::leaveEvent(QEvent
* event
)
359 DolphinTreeView::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_dolphinViewController
->emitViewportEntered();
366 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
368 DolphinTreeView::currentChanged(current
, previous
);
369 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
372 QRect
DolphinColumnView::visualRect(const QModelIndex
& index
) const
374 QRect rect
= DolphinTreeView::visualRect(index
);
376 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
377 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
378 if (!item
.isNull()) {
379 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.text(), viewOptions());
380 rect
.setWidth(width
);
386 bool DolphinColumnView::acceptsDrop(const QModelIndex
& index
) const
388 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
389 // Accept drops above directories
390 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
391 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
392 return !item
.isNull() && item
.isDir();
398 bool DolphinColumnView::eventFilter(QObject
* watched
, QEvent
* event
)
400 if (watched
== m_resizeWidget
) {
401 switch (event
->type()) {
402 case QEvent::MouseButtonPress
: {
403 // Initiate the resizing of the column
404 QMouseEvent
* mouseEvent
= static_cast<QMouseEvent
*>(event
);
405 m_resizeXOrigin
= mouseEvent
->globalX();
406 m_resizeWidget
->setMouseTracking(true);
411 case QEvent::MouseButtonDblClick
: {
412 // Reset the column width to the default value
413 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
414 setMaximumWidth(settings
->columnWidth());
415 m_container
->layoutColumns();
416 m_resizeWidget
->setMouseTracking(false);
417 m_resizeXOrigin
= -1;
422 case QEvent::MouseMove
: {
423 // Resize the column and trigger a relayout of the container
424 QMouseEvent
* mouseEvent
= static_cast<QMouseEvent
*>(event
);
425 int requestedWidth
= maximumWidth() - m_resizeXOrigin
+ mouseEvent
->globalX();;
426 if (requestedWidth
< minimumWidth()) {
427 requestedWidth
= minimumWidth();
429 setMaximumWidth(requestedWidth
);
431 m_container
->layoutColumns();
433 m_resizeXOrigin
= mouseEvent
->globalX();
439 case QEvent::MouseButtonRelease
: {
440 // The resizing has been finished
441 m_resizeWidget
->setMouseTracking(false);
442 m_resizeXOrigin
= -1;
451 return DolphinTreeView::eventFilter(watched
, event
);
453 void DolphinColumnView::setZoomLevel(int level
)
455 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
456 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
458 const bool showPreview
= m_container
->m_dolphinViewController
->view()->showPreview();
460 settings
->setPreviewSize(size
);
462 settings
->setIconSize(size
);
465 updateDecorationSize(showPreview
);
468 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
470 m_container
->m_dolphinViewController
->setItemView(this);
471 m_container
->m_dolphinViewController
->emitItemEntered(index
);
474 void DolphinColumnView::requestActivation()
476 m_container
->m_dolphinViewController
->requestActivation();
478 m_container
->requestActivation(this);
479 selectionModel()->clear();
483 void DolphinColumnView::updateFont()
485 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
486 Q_ASSERT(settings
!= 0);
488 if (settings
->useSystemFont()) {
489 m_font
= KGlobalSettings::generalFont();
493 void DolphinColumnView::slotShowPreviewChanged()
495 const DolphinView
* view
= m_container
->m_dolphinViewController
->view();
496 updateDecorationSize(view
->showPreview());
499 void DolphinColumnView::activate()
501 setFocus(Qt::OtherFocusReason
);
503 if (KGlobalSettings::singleClick()) {
504 connect(this, SIGNAL(clicked(const QModelIndex
&)),
505 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
507 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
508 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
511 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
512 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
518 void DolphinColumnView::deactivate()
521 if (KGlobalSettings::singleClick()) {
522 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
523 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
525 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
526 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
529 // It is important to disconnect the connection to requestActivation() temporary, otherwise the internal
530 // clearing of the selection would result in activating the column again.
531 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
532 this, SLOT(requestActivation()));
533 const QModelIndex current
= selectionModel()->currentIndex();
534 selectionModel()->clear();
535 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
536 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
537 this, SLOT(requestActivation()));
542 void DolphinColumnView::updateDecorationSize(bool showPreview
)
544 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
545 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
546 const QSize
size(iconSize
, iconSize
);
549 m_decorationSize
= size
;
554 #include "dolphincolumnview.moc"