1 /***************************************************************************
2 * Copyright (C) 2007 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 "dolphincolumnwidget.h"
22 #include "dolphinmodel.h"
23 #include "dolphincolumnview.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 "selectionmanager.h"
34 #include "tooltips/tooltipmanager.h"
36 #include <kcolorscheme.h>
37 #include <kdirlister.h>
38 #include <kfileitem.h>
39 #include <kfilepreviewgenerator.h>
40 #include <kio/previewjob.h>
41 #include <kiconeffect.h>
43 #include <konqmimedata.h>
45 #include <QApplication>
51 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
52 DolphinColumnView
* columnView
,
57 m_selectionManager(0),
66 m_previewGenerator(0),
70 setMouseTracking(true);
71 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
72 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
73 setSelectionBehavior(SelectItems
);
74 setSelectionMode(QAbstractItemView::ExtendedSelection
);
75 setDragDropMode(QAbstractItemView::DragDrop
);
76 setDropIndicatorShown(false);
77 setSelectionRectVisible(true);
78 setEditTriggers(QAbstractItemView::NoEditTriggers
);
80 setVerticalScrollMode(QListView::ScrollPerPixel
);
81 setHorizontalScrollMode(QListView::ScrollPerPixel
);
83 m_autoScroller
= new DolphinViewAutoScroller(this);
85 // apply the column mode settings to the widget
86 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
87 Q_ASSERT(settings
!= 0);
89 if (settings
->useSystemFont()) {
90 m_font
= KGlobalSettings::generalFont();
92 m_font
= QFont(settings
->fontFamily(),
94 settings
->fontWeight(),
95 settings
->italicFont());
98 const int iconSize
= settings
->iconSize();
99 setDecorationSize(QSize(iconSize
, iconSize
));
101 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
102 delegate
->setShowToolTipWhenElided(false);
103 setItemDelegate(delegate
);
107 connect(this, SIGNAL(viewportEntered()),
108 m_view
->m_controller
, SLOT(emitViewportEntered()));
109 connect(this, SIGNAL(entered(const QModelIndex
&)),
110 this, SLOT(slotEntered(const QModelIndex
&)));
112 m_dirLister
= new DolphinDirLister();
113 m_dirLister
->setAutoUpdate(true);
114 m_dirLister
->setMainWindow(window());
115 m_dirLister
->setDelayedMimeTypes(true);
116 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
117 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
119 m_dolphinModel
= new DolphinModel(this);
120 m_dolphinModel
->setDirLister(m_dirLister
);
121 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
123 m_proxyModel
= new DolphinSortFilterProxyModel(this);
124 m_proxyModel
->setSourceModel(m_dolphinModel
);
125 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
126 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
127 m_proxyModel
->setSorting(dolphinView
->sorting());
128 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
129 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
131 setModel(m_proxyModel
);
133 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
134 m_selectionManager
= new SelectionManager(this);
135 connect(m_selectionManager
, SIGNAL(selectionChanged()),
136 this, SLOT(requestActivation()));
137 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
138 m_selectionManager
, SLOT(reset()));
141 m_previewGenerator
= new KFilePreviewGenerator(this);
142 m_previewGenerator
->setPreviewShown(m_view
->m_controller
->dolphinView()->showPreview());
144 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
145 m_toolTipManager
= new ToolTipManager(this, m_proxyModel
);
148 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
150 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
151 this, SLOT(updateFont()));
153 FolderExpander
* folderExpander
= new FolderExpander(this, m_proxyModel
);
154 folderExpander
->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
155 connect (folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
156 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
159 DolphinColumnWidget::~DolphinColumnWidget()
163 delete m_dolphinModel
;
165 m_dirLister
= 0; // deleted by m_dolphinModel
168 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
171 m_decorationSize
= size
;
173 if (m_previewGenerator
!= 0) {
174 m_previewGenerator
->updateIcons();
176 if (m_selectionManager
!= 0) {
177 m_selectionManager
->reset();
181 void DolphinColumnWidget::setActive(bool active
)
183 if (active
&& (m_view
->focusProxy() != this)) {
184 m_view
->setFocusProxy(this);
187 if (m_active
!= active
) {
198 void DolphinColumnWidget::reload()
201 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
204 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
206 m_proxyModel
->setSorting(sorting
);
209 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
211 m_proxyModel
->setSortOrder(order
);
214 void DolphinColumnWidget::setSortFoldersFirst(bool foldersFirst
)
216 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
219 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
221 if (show
!= m_dirLister
->showingDotFiles()) {
222 m_dirLister
->setShowingDotFiles(show
);
224 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
228 void DolphinColumnWidget::setShowPreview(bool show
)
230 m_previewGenerator
->setPreviewShown(show
);
233 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
236 void DolphinColumnWidget::updateBackground()
238 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
239 // cleaning up the cut-indication of DolphinColumnWidget with the code from
240 // DolphinView a common helper-class should be available which can be shared
241 // by all view implementations -> no hardcoded value anymore
242 const QPalette::ColorRole role
= viewport()->backgroundRole();
243 QColor color
= viewport()->palette().color(role
);
244 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
246 QPalette palette
= viewport()->palette();
247 palette
.setColor(role
, color
);
248 viewport()->setPalette(palette
);
253 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
255 m_proxyModel
->setFilterRegExp(nameFilter
);
258 void DolphinColumnWidget::editItem(const KFileItem
& item
)
260 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
261 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
262 if (proxyIndex
.isValid()) {
267 KFileItem
DolphinColumnWidget::itemAt(const QPoint
& pos
) const
270 const QModelIndex index
= indexAt(pos
);
271 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
272 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
273 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
278 KFileItemList
DolphinColumnWidget::selectedItems() const
280 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
281 KFileItemList itemList
;
283 const QModelIndexList indexList
= selection
.indexes();
284 foreach (const QModelIndex
&index
, indexList
) {
285 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
286 if (!item
.isNull()) {
287 itemList
.append(item
);
294 QMimeData
* DolphinColumnWidget::selectionMimeData() const
296 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
297 return m_dolphinModel
->mimeData(selection
.indexes());
300 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
302 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
303 viewOptions
.font
= m_font
;
304 viewOptions
.decorationSize
= m_decorationSize
;
305 viewOptions
.showDecorationSelected
= true;
309 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
311 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_view
->m_controller
);
314 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
316 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
317 event
->acceptProposedAction();
322 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
324 QListView::dragLeaveEvent(event
);
325 setDirtyRegion(m_dropRect
);
328 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
330 QListView::dragMoveEvent(event
);
332 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
333 const QModelIndex index
= indexAt(event
->pos());
334 setDirtyRegion(m_dropRect
);
336 m_dropRect
.setSize(QSize()); // set as invalid
337 if (index
.isValid()) {
338 m_view
->m_controller
->setItemView(this);
339 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
340 if (!item
.isNull() && item
.isDir()) {
341 m_dropRect
= visualRect(index
);
344 setDirtyRegion(m_dropRect
);
346 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
347 // accept url drops, independently from the destination item
348 event
->acceptProposedAction();
352 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
354 const QModelIndex index
= indexAt(event
->pos());
355 m_view
->m_controller
->setItemView(this);
356 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
357 m_view
->m_controller
->indicateDroppedUrls(item
, url(), event
);
358 QListView::dropEvent(event
);
361 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
363 if (!m_childUrl
.isEmpty()) {
364 // indicate the shown URL of the next column by highlighting the shown folder item
365 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
366 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
367 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
368 const QRect itemRect
= visualRect(proxyIndex
);
369 QPainter
painter(viewport());
370 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
372 painter
.setPen(Qt::NoPen
);
373 painter
.setBrush(color
);
374 painter
.drawRect(itemRect
);
378 QListView::paintEvent(event
);
381 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
384 if (!indexAt(event
->pos()).isValid()) {
385 if (QApplication::mouseButtons() & Qt::MidButton
) {
386 m_view
->m_controller
->replaceUrlByClipboard();
388 } else if (event
->button() == Qt::LeftButton
) {
389 // TODO: see comment in DolphinIconsView::mousePressEvent()
390 setState(QAbstractItemView::DraggingState
);
392 QListView::mousePressEvent(event
);
395 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
397 QListView::keyPressEvent(event
);
400 DolphinController
* controller
= m_view
->m_controller
;
401 controller
->handleKeyPressEvent(event
);
402 switch (event
->key()) {
403 case Qt::Key_Right
: {
404 // Special key handling for the column: A Key_Right should
405 // open a new column for the currently selected folder.
406 const QModelIndex index
= currentIndex();
407 const KFileItem item
= controller
->itemForIndex(index
);
408 if (!item
.isNull() && item
.isDir()) {
409 controller
->emitItemTriggered(item
);
415 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
416 QItemSelectionModel::Current
|
417 QItemSelectionModel::Clear
);
424 if (m_toolTipManager
!= 0) {
425 m_toolTipManager
->hideTip();
429 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
432 m_view
->requestActivation(this);
433 Q_ASSERT(m_view
->m_controller
->itemView() == this);
434 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
438 QListView::contextMenuEvent(event
);
440 const QModelIndex index
= indexAt(event
->pos());
441 if (!index
.isValid()) {
445 if (m_toolTipManager
!= 0) {
446 m_toolTipManager
->hideTip();
449 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
450 Q_ASSERT(m_view
->m_controller
->itemView() == this);
451 m_view
->m_controller
->triggerContextMenuRequest(pos
);
454 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
456 if (m_selectionManager
!= 0) {
457 m_selectionManager
->reset();
460 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
461 if (event
->modifiers() & Qt::ControlModifier
) {
466 const int height
= m_decorationSize
.height();
467 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
468 verticalScrollBar()->setSingleStep(step
);
470 QListView::wheelEvent(event
);
473 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
475 QListView::leaveEvent(event
);
476 // if the mouse is above an item and moved very fast outside the widget,
477 // no viewportEntered() signal might be emitted although the mouse has been moved
478 // above the viewport
479 m_view
->m_controller
->emitViewportEntered();
482 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
484 QListView::selectionChanged(selected
, deselected
);
486 QItemSelectionModel
* selModel
= m_view
->selectionModel();
487 selModel
->select(selected
, QItemSelectionModel::Select
);
488 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
491 void DolphinColumnWidget::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
493 QListView::currentChanged(current
, previous
);
494 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
497 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
499 m_view
->m_controller
->setItemView(this);
500 m_view
->m_controller
->emitItemEntered(index
);
503 void DolphinColumnWidget::requestActivation()
505 m_view
->m_controller
->setItemView(this);
506 m_view
->m_controller
->requestActivation();
508 m_view
->requestActivation(this);
509 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
510 selectionModel()->clear();
514 void DolphinColumnWidget::updateFont()
516 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
517 Q_ASSERT(settings
!= 0);
519 if (settings
->useSystemFont()) {
520 m_font
= KGlobalSettings::generalFont();
524 void DolphinColumnWidget::activate()
526 setFocus(Qt::OtherFocusReason
);
528 if (KGlobalSettings::singleClick()) {
529 connect(this, SIGNAL(clicked(const QModelIndex
&)),
530 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
532 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
533 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
536 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
537 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
543 void DolphinColumnWidget::deactivate()
546 if (KGlobalSettings::singleClick()) {
547 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
548 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
550 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
551 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
554 const QModelIndex current
= selectionModel()->currentIndex();
555 selectionModel()->clear();
556 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
560 #include "dolphincolumnwidget.moc"