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 "revisioncontrolobserver.h"
34 #include "selectionmanager.h"
35 #include "tooltips/tooltipmanager.h"
37 #include <kcolorscheme.h>
38 #include <kdirlister.h>
39 #include <kfileitem.h>
40 #include <kfilepreviewgenerator.h>
41 #include <kio/previewjob.h>
42 #include <kiconeffect.h>
44 #include <konqmimedata.h>
46 #include <QApplication>
52 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
53 DolphinColumnView
* columnView
,
58 m_selectionManager(0),
67 m_previewGenerator(0),
71 setMouseTracking(true);
72 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
73 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
74 setSelectionBehavior(SelectItems
);
75 setSelectionMode(QAbstractItemView::ExtendedSelection
);
76 setDragDropMode(QAbstractItemView::DragDrop
);
77 setDropIndicatorShown(false);
78 setSelectionRectVisible(true);
79 setEditTriggers(QAbstractItemView::NoEditTriggers
);
81 setVerticalScrollMode(QListView::ScrollPerPixel
);
82 setHorizontalScrollMode(QListView::ScrollPerPixel
);
84 m_autoScroller
= new DolphinViewAutoScroller(this);
86 // apply the column mode settings to the widget
87 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
88 Q_ASSERT(settings
!= 0);
90 if (settings
->useSystemFont()) {
91 m_font
= KGlobalSettings::generalFont();
93 m_font
= QFont(settings
->fontFamily(),
95 settings
->fontWeight(),
96 settings
->italicFont());
99 const int iconSize
= settings
->iconSize();
100 setDecorationSize(QSize(iconSize
, iconSize
));
102 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
103 delegate
->setShowToolTipWhenElided(false);
104 setItemDelegate(delegate
);
108 connect(this, SIGNAL(viewportEntered()),
109 m_view
->m_controller
, SLOT(emitViewportEntered()));
110 connect(this, SIGNAL(entered(const QModelIndex
&)),
111 this, SLOT(slotEntered(const QModelIndex
&)));
113 m_dirLister
= new DolphinDirLister();
114 m_dirLister
->setAutoUpdate(true);
115 m_dirLister
->setMainWindow(window());
116 m_dirLister
->setDelayedMimeTypes(true);
117 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
118 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
120 m_dolphinModel
= new DolphinModel(this);
121 m_dolphinModel
->setDirLister(m_dirLister
);
122 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
124 m_proxyModel
= new DolphinSortFilterProxyModel(this);
125 m_proxyModel
->setSourceModel(m_dolphinModel
);
126 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
127 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
128 m_proxyModel
->setSorting(dolphinView
->sorting());
129 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
130 m_proxyModel
->setSortFoldersFirst(dolphinView
->sortFoldersFirst());
132 setModel(m_proxyModel
);
134 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
135 m_selectionManager
= new SelectionManager(this);
136 connect(m_selectionManager
, SIGNAL(selectionChanged()),
137 this, SLOT(requestActivation()));
138 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
139 m_selectionManager
, SLOT(reset()));
142 m_previewGenerator
= new KFilePreviewGenerator(this);
143 m_previewGenerator
->setPreviewShown(m_view
->m_controller
->dolphinView()->showPreview());
145 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
146 m_toolTipManager
= new ToolTipManager(this, m_proxyModel
);
149 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
151 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
152 this, SLOT(updateFont()));
154 FolderExpander
* folderExpander
= new FolderExpander(this, m_proxyModel
);
155 folderExpander
->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
156 connect (folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
157 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
159 new RevisionControlObserver(this);
162 DolphinColumnWidget::~DolphinColumnWidget()
166 delete m_dolphinModel
;
168 m_dirLister
= 0; // deleted by m_dolphinModel
171 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
174 m_decorationSize
= size
;
176 if (m_previewGenerator
!= 0) {
177 m_previewGenerator
->updateIcons();
179 if (m_selectionManager
!= 0) {
180 m_selectionManager
->reset();
184 void DolphinColumnWidget::setActive(bool active
)
186 if (active
&& (m_view
->focusProxy() != this)) {
187 m_view
->setFocusProxy(this);
190 if (m_active
!= active
) {
201 void DolphinColumnWidget::reload()
204 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
207 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
209 m_proxyModel
->setSorting(sorting
);
212 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
214 m_proxyModel
->setSortOrder(order
);
217 void DolphinColumnWidget::setSortFoldersFirst(bool foldersFirst
)
219 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
222 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
224 if (show
!= m_dirLister
->showingDotFiles()) {
225 m_dirLister
->setShowingDotFiles(show
);
227 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
231 void DolphinColumnWidget::setShowPreview(bool show
)
233 m_previewGenerator
->setPreviewShown(show
);
236 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
239 void DolphinColumnWidget::updateBackground()
241 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
242 // cleaning up the cut-indication of DolphinColumnWidget with the code from
243 // DolphinView a common helper-class should be available which can be shared
244 // by all view implementations -> no hardcoded value anymore
245 const QPalette::ColorRole role
= viewport()->backgroundRole();
246 QColor color
= viewport()->palette().color(role
);
247 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
249 QPalette palette
= viewport()->palette();
250 palette
.setColor(role
, color
);
251 viewport()->setPalette(palette
);
256 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
258 m_proxyModel
->setFilterRegExp(nameFilter
);
261 void DolphinColumnWidget::editItem(const KFileItem
& item
)
263 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
264 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
265 if (proxyIndex
.isValid()) {
270 KFileItem
DolphinColumnWidget::itemAt(const QPoint
& pos
) const
273 const QModelIndex index
= indexAt(pos
);
274 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
275 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
276 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
281 KFileItemList
DolphinColumnWidget::selectedItems() const
283 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
284 KFileItemList itemList
;
286 const QModelIndexList indexList
= selection
.indexes();
287 foreach (const QModelIndex
&index
, indexList
) {
288 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
289 if (!item
.isNull()) {
290 itemList
.append(item
);
297 QMimeData
* DolphinColumnWidget::selectionMimeData() const
299 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
300 return m_dolphinModel
->mimeData(selection
.indexes());
303 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
305 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
306 viewOptions
.font
= m_font
;
307 viewOptions
.decorationSize
= m_decorationSize
;
308 viewOptions
.showDecorationSelected
= true;
312 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
314 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_view
->m_controller
);
317 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
319 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
320 event
->acceptProposedAction();
325 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
327 QListView::dragLeaveEvent(event
);
328 setDirtyRegion(m_dropRect
);
331 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
333 QListView::dragMoveEvent(event
);
335 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
336 const QModelIndex index
= indexAt(event
->pos());
337 setDirtyRegion(m_dropRect
);
339 m_dropRect
.setSize(QSize()); // set as invalid
340 if (index
.isValid()) {
341 m_view
->m_controller
->setItemView(this);
342 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
343 if (!item
.isNull() && item
.isDir()) {
344 m_dropRect
= visualRect(index
);
347 setDirtyRegion(m_dropRect
);
349 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
350 // accept url drops, independently from the destination item
351 event
->acceptProposedAction();
355 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
357 const QModelIndex index
= indexAt(event
->pos());
358 m_view
->m_controller
->setItemView(this);
359 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
360 m_view
->m_controller
->indicateDroppedUrls(item
, url(), event
);
361 QListView::dropEvent(event
);
364 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
366 if (!m_childUrl
.isEmpty()) {
367 // indicate the shown URL of the next column by highlighting the shown folder item
368 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
369 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
370 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
371 const QRect itemRect
= visualRect(proxyIndex
);
372 QPainter
painter(viewport());
373 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
375 painter
.setPen(Qt::NoPen
);
376 painter
.setBrush(color
);
377 painter
.drawRect(itemRect
);
381 QListView::paintEvent(event
);
384 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
387 if (!indexAt(event
->pos()).isValid()) {
388 if (QApplication::mouseButtons() & Qt::MidButton
) {
389 m_view
->m_controller
->replaceUrlByClipboard();
391 } else if (event
->button() == Qt::LeftButton
) {
392 // TODO: see comment in DolphinIconsView::mousePressEvent()
393 setState(QAbstractItemView::DraggingState
);
395 QListView::mousePressEvent(event
);
398 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
400 QListView::keyPressEvent(event
);
403 DolphinController
* controller
= m_view
->m_controller
;
404 controller
->handleKeyPressEvent(event
);
405 switch (event
->key()) {
406 case Qt::Key_Right
: {
407 // Special key handling for the column: A Key_Right should
408 // open a new column for the currently selected folder.
409 const QModelIndex index
= currentIndex();
410 const KFileItem item
= controller
->itemForIndex(index
);
411 if (!item
.isNull() && item
.isDir()) {
412 controller
->emitItemTriggered(item
);
418 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
419 QItemSelectionModel::Current
|
420 QItemSelectionModel::Clear
);
427 if (m_toolTipManager
!= 0) {
428 m_toolTipManager
->hideTip();
432 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
435 m_view
->requestActivation(this);
436 Q_ASSERT(m_view
->m_controller
->itemView() == this);
437 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
441 QListView::contextMenuEvent(event
);
443 const QModelIndex index
= indexAt(event
->pos());
444 if (!index
.isValid()) {
448 if (m_toolTipManager
!= 0) {
449 m_toolTipManager
->hideTip();
452 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
453 Q_ASSERT(m_view
->m_controller
->itemView() == this);
454 m_view
->m_controller
->triggerContextMenuRequest(pos
);
457 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
459 if (m_selectionManager
!= 0) {
460 m_selectionManager
->reset();
463 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
464 if (event
->modifiers() & Qt::ControlModifier
) {
469 const int height
= m_decorationSize
.height();
470 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
471 verticalScrollBar()->setSingleStep(step
);
473 QListView::wheelEvent(event
);
476 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
478 QListView::leaveEvent(event
);
479 // if the mouse is above an item and moved very fast outside the widget,
480 // no viewportEntered() signal might be emitted although the mouse has been moved
481 // above the viewport
482 m_view
->m_controller
->emitViewportEntered();
485 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
487 QListView::selectionChanged(selected
, deselected
);
489 QItemSelectionModel
* selModel
= m_view
->selectionModel();
490 selModel
->select(selected
, QItemSelectionModel::Select
);
491 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
494 void DolphinColumnWidget::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
496 QListView::currentChanged(current
, previous
);
497 m_autoScroller
->handleCurrentIndexChange(current
, previous
);
500 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
502 m_view
->m_controller
->setItemView(this);
503 m_view
->m_controller
->emitItemEntered(index
);
506 void DolphinColumnWidget::requestActivation()
508 m_view
->m_controller
->setItemView(this);
509 m_view
->m_controller
->requestActivation();
511 m_view
->requestActivation(this);
512 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
513 selectionModel()->clear();
517 void DolphinColumnWidget::updateFont()
519 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
520 Q_ASSERT(settings
!= 0);
522 if (settings
->useSystemFont()) {
523 m_font
= KGlobalSettings::generalFont();
527 void DolphinColumnWidget::activate()
529 setFocus(Qt::OtherFocusReason
);
531 if (KGlobalSettings::singleClick()) {
532 connect(this, SIGNAL(clicked(const QModelIndex
&)),
533 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
535 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
536 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
539 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
540 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
546 void DolphinColumnWidget::deactivate()
549 if (KGlobalSettings::singleClick()) {
550 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
551 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
553 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
554 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
557 const QModelIndex current
= selectionModel()->currentIndex();
558 selectionModel()->clear();
559 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
563 #include "dolphincolumnwidget.moc"