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
);
123 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
125 m_dolphinModel
= new DolphinModel(this);
126 m_dolphinModel
->setDirLister(m_dirLister
);
127 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
129 m_proxyModel
= new DolphinSortFilterProxyModel(this);
130 m_proxyModel
->setSourceModel(m_dolphinModel
);
131 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
133 m_proxyModel
->setSorting(dolphinView
->sorting());
134 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
135 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
137 setModel(m_proxyModel
);
139 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
140 this, SLOT(updateFont()));
142 const ViewModeController
* viewModeController
= m_container
->m_viewModeController
;
143 connect(viewModeController
, SIGNAL(zoomLevelChanged(int)),
144 this, SLOT(setZoomLevel(int)));
145 const QString nameFilter
= viewModeController
->nameFilter();
146 if (!nameFilter
.isEmpty()) {
147 m_proxyModel
->setFilterFixedString(nameFilter
);
150 updateDecorationSize(dolphinView
->showPreview());
153 DolphinViewController
* dolphinViewController
= m_container
->m_dolphinViewController
;
154 m_extensionsFactory
= new ViewExtensionsFactory(this, dolphinViewController
, viewModeController
);
155 m_extensionsFactory
->fileItemDelegate()->setMinimizedNameColumn(true);
157 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
160 DolphinColumnView::~DolphinColumnView()
164 delete m_dolphinModel
;
166 m_dirLister
= 0; // deleted by m_dolphinModel
169 void DolphinColumnView::setActive(bool active
)
171 if (m_active
!= active
) {
182 void DolphinColumnView::updateBackground()
184 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
185 // cleaning up the cut-indication of DolphinColumnView with the code from
186 // DolphinView a common helper-class should be available which can be shared
187 // by all view implementations -> no hardcoded value anymore
188 const QPalette::ColorRole role
= viewport()->backgroundRole();
189 QColor color
= viewport()->palette().color(role
);
190 color
.setAlpha((m_active
&& m_container
->m_active
) ? 255 : 150);
192 QPalette palette
= viewport()->palette();
193 palette
.setColor(role
, color
);
194 viewport()->setPalette(palette
);
199 KFileItem
DolphinColumnView::itemAt(const QPoint
& pos
) const
202 const QModelIndex index
= indexAt(pos
);
203 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
204 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
205 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
210 void DolphinColumnView::setSelectionModel(QItemSelectionModel
* model
)
212 // If a change of the selection is done although the view is not active
213 // (e. g. by the selection markers), the column must be activated. This
214 // is done by listening to the current selectionChanged() signal.
215 if (selectionModel() != 0) {
216 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
217 this, SLOT(requestActivation()));
220 DolphinTreeView::setSelectionModel(model
);
222 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
223 this, SLOT(requestActivation()));
226 QStyleOptionViewItem
DolphinColumnView::viewOptions() const
228 QStyleOptionViewItem viewOptions
= DolphinTreeView::viewOptions();
229 viewOptions
.font
= m_font
;
230 viewOptions
.fontMetrics
= QFontMetrics(m_font
);
231 viewOptions
.decorationSize
= m_decorationSize
;
232 viewOptions
.showDecorationSelected
= true;
236 bool DolphinColumnView::event(QEvent
* event
)
238 if (event
->type() == QEvent::Polish
) {
239 // Hide all columns except of the 'Name' column
240 for (int i
= DolphinModel::Name
+ 1; i
< DolphinModel::ExtraColumnCount
; ++i
) {
246 return DolphinTreeView::event(event
);
249 void DolphinColumnView::startDrag(Qt::DropActions supportedActions
)
251 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_container
->m_dolphinViewController
);
252 DolphinTreeView::startDrag(supportedActions
);
255 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
257 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
258 event
->acceptProposedAction();
261 DolphinTreeView::dragEnterEvent(event
);
264 void DolphinColumnView::dragMoveEvent(QDragMoveEvent
* event
)
266 DolphinTreeView::dragMoveEvent(event
);
268 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
269 // accept url drops, independently from the destination item
270 event
->acceptProposedAction();
274 void DolphinColumnView::dropEvent(QDropEvent
* event
)
276 const QModelIndex index
= indexAt(event
->pos());
277 m_container
->m_dolphinViewController
->setItemView(this);
278 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
279 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
280 m_container
->m_dolphinViewController
->indicateDroppedUrls(item
, url(), event
);
281 DolphinTreeView::dropEvent(event
);
284 void DolphinColumnView::paintEvent(QPaintEvent
* event
)
286 if (!m_childUrl
.isEmpty()) {
287 // Indicate the shown URL of the next column by highlighting the shown folder item
288 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
289 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
290 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
291 QPainter
painter(viewport());
293 QStyleOptionViewItemV4 option
;
294 option
.initFrom(this);
295 option
.rect
= visualRect(proxyIndex
);
296 option
.state
= QStyle::State_Enabled
| QStyle::State_HasFocus
;
297 option
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
298 style()->drawPrimitive(QStyle::PE_FrameFocusRect
, &option
, &painter
, this);
302 DolphinTreeView::paintEvent(event
);
305 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
308 if (!indexAt(event
->pos()).isValid() && (QApplication::mouseButtons() & Qt::MidButton
)) {
309 m_container
->m_dolphinViewController
->replaceUrlByClipboard();
312 DolphinTreeView::mousePressEvent(event
);
315 void DolphinColumnView::keyPressEvent(QKeyEvent
* event
)
317 DolphinTreeView::keyPressEvent(event
);
319 DolphinViewController
* controller
= m_container
->m_dolphinViewController
;
320 controller
->handleKeyPressEvent(event
);
321 switch (event
->key()) {
322 case Qt::Key_Right
: {
323 // Special key handling for the column: A Key_Right should
324 // open a new column for the currently selected folder.
325 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(currentIndex());
326 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
327 if (!item
.isNull() && item
.isDir()) {
328 controller
->emitItemTriggered(item
);
334 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
335 QItemSelectionModel::Current
|
336 QItemSelectionModel::Clear
);
344 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
347 DolphinTreeView::contextMenuEvent(event
);
348 m_container
->m_dolphinViewController
->triggerContextMenuRequest(event
->pos());
351 void DolphinColumnView::wheelEvent(QWheelEvent
* event
)
353 const int step
= m_decorationSize
.height();
354 verticalScrollBar()->setSingleStep(step
);
355 DolphinTreeView::wheelEvent(event
);
358 void DolphinColumnView::leaveEvent(QEvent
* event
)
360 DolphinTreeView::leaveEvent(event
);
361 // if the mouse is above an item and moved very fast outside the widget,
362 // no viewportEntered() signal might be emitted although the mouse has been moved
363 // above the viewport
364 m_container
->m_dolphinViewController
->emitViewportEntered();
367 void DolphinColumnView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
369 DolphinTreeView::currentChanged(current
, previous
);
370 m_extensionsFactory
->handleCurrentIndexChange(current
, previous
);
373 QRect
DolphinColumnView::visualRect(const QModelIndex
& index
) const
375 QRect rect
= DolphinTreeView::visualRect(index
);
377 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
378 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
379 if (!item
.isNull()) {
380 const int width
= DolphinFileItemDelegate::nameColumnWidth(item
.text(), viewOptions());
381 rect
.setWidth(width
);
387 bool DolphinColumnView::acceptsDrop(const QModelIndex
& index
) const
389 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
390 // Accept drops above directories
391 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
392 const KFileItem item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
393 return !item
.isNull() && item
.isDir();
399 bool DolphinColumnView::eventFilter(QObject
* watched
, QEvent
* event
)
401 if (watched
== m_resizeWidget
) {
402 switch (event
->type()) {
403 case QEvent::MouseButtonPress
: {
404 // Initiate the resizing of the column
405 QMouseEvent
* mouseEvent
= static_cast<QMouseEvent
*>(event
);
406 m_resizeXOrigin
= mouseEvent
->globalX();
407 m_resizeWidget
->setMouseTracking(true);
412 case QEvent::MouseButtonDblClick
: {
413 // Reset the column width to the default value
414 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
415 setMaximumWidth(settings
->columnWidth());
416 m_container
->layoutColumns();
417 m_resizeWidget
->setMouseTracking(false);
418 m_resizeXOrigin
= -1;
423 case QEvent::MouseMove
: {
424 // Resize the column and trigger a relayout of the container
425 QMouseEvent
* mouseEvent
= static_cast<QMouseEvent
*>(event
);
426 int requestedWidth
= maximumWidth() - m_resizeXOrigin
+ mouseEvent
->globalX();;
427 if (requestedWidth
< minimumWidth()) {
428 requestedWidth
= minimumWidth();
430 setMaximumWidth(requestedWidth
);
432 m_container
->layoutColumns();
434 m_resizeXOrigin
= mouseEvent
->globalX();
440 case QEvent::MouseButtonRelease
: {
441 // The resizing has been finished
442 m_resizeWidget
->setMouseTracking(false);
443 m_resizeXOrigin
= -1;
452 return DolphinTreeView::eventFilter(watched
, event
);
454 void DolphinColumnView::setZoomLevel(int level
)
456 const int size
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
457 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
459 const bool showPreview
= m_container
->m_dolphinViewController
->view()->showPreview();
461 settings
->setPreviewSize(size
);
463 settings
->setIconSize(size
);
466 updateDecorationSize(showPreview
);
469 void DolphinColumnView::slotEntered(const QModelIndex
& index
)
471 m_container
->m_dolphinViewController
->setItemView(this);
472 m_container
->m_dolphinViewController
->emitItemEntered(index
);
475 void DolphinColumnView::requestActivation()
477 m_container
->m_dolphinViewController
->requestActivation();
479 m_container
->requestActivation(this);
480 selectionModel()->clear();
484 void DolphinColumnView::updateFont()
486 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
487 Q_ASSERT(settings
!= 0);
489 if (settings
->useSystemFont()) {
490 m_font
= KGlobalSettings::generalFont();
494 void DolphinColumnView::slotShowPreviewChanged()
496 const DolphinView
* view
= m_container
->m_dolphinViewController
->view();
497 updateDecorationSize(view
->showPreview());
500 void DolphinColumnView::slotDirListerCompleted()
502 if (!m_childUrl
.isEmpty()) {
506 // Try to optimize the width of the column, so that no name gets clipped
507 const int requiredWidth
= sizeHintForColumn(DolphinModel::Name
);
509 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
510 if (requiredWidth
> settings
->columnWidth()) {
511 int frameAroundContents
= 0;
512 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
)) {
513 // TODO: Using 2 PM_DefaultFrameWidths are not sufficient. Check Qt-code
514 // for other pixelmetrics that should be added...
515 frameAroundContents
= style()->pixelMetric(QStyle::PM_DefaultFrameWidth
) * 4;
518 const int scrollBarWidth
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, 0, verticalScrollBar());
520 setMaximumWidth(requiredWidth
+ frameAroundContents
+ scrollBarWidth
);
521 m_container
->layoutColumns();
523 m_container
->assureVisibleActiveColumn();
528 void DolphinColumnView::activate()
530 setFocus(Qt::OtherFocusReason
);
532 if (KGlobalSettings::singleClick()) {
533 connect(this, SIGNAL(clicked(const QModelIndex
&)),
534 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
536 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
537 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
540 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
541 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
547 void DolphinColumnView::deactivate()
550 if (KGlobalSettings::singleClick()) {
551 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
552 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
554 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
555 m_container
->m_dolphinViewController
, SLOT(triggerItem(const QModelIndex
&)));
558 // It is important to disconnect the connection to requestActivation() temporary, otherwise the internal
559 // clearing of the selection would result in activating the column again.
560 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
561 this, SLOT(requestActivation()));
562 const QModelIndex current
= selectionModel()->currentIndex();
563 selectionModel()->clear();
564 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
565 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
566 this, SLOT(requestActivation()));
571 void DolphinColumnView::updateDecorationSize(bool showPreview
)
573 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
574 const int iconSize
= showPreview
? settings
->previewSize() : settings
->iconSize();
575 const QSize
size(iconSize
, iconSize
);
578 m_decorationSize
= size
;
583 #include "dolphincolumnview.moc"