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());
130 setModel(m_proxyModel
);
132 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
133 m_selectionManager
= new SelectionManager(this);
134 connect(m_selectionManager
, SIGNAL(selectionChanged()),
135 this, SLOT(requestActivation()));
136 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
137 m_selectionManager
, SLOT(reset()));
140 m_previewGenerator
= new KFilePreviewGenerator(this);
141 m_previewGenerator
->setPreviewShown(m_view
->m_controller
->dolphinView()->showPreview());
143 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
144 m_toolTipManager
= new ToolTipManager(this, m_proxyModel
);
147 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
149 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
150 this, SLOT(updateFont()));
152 FolderExpander
* folderExpander
= new FolderExpander(this, m_proxyModel
);
153 folderExpander
->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
154 connect (folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
155 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
158 DolphinColumnWidget::~DolphinColumnWidget()
162 delete m_dolphinModel
;
164 m_dirLister
= 0; // deleted by m_dolphinModel
167 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
170 m_decorationSize
= size
;
172 if (m_previewGenerator
!= 0) {
173 m_previewGenerator
->updateIcons();
175 if (m_selectionManager
!= 0) {
176 m_selectionManager
->reset();
180 void DolphinColumnWidget::setActive(bool active
)
182 if (active
&& (m_view
->focusProxy() != this)) {
183 m_view
->setFocusProxy(this);
186 if (m_active
!= active
) {
197 void DolphinColumnWidget::reload()
200 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
203 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
205 m_proxyModel
->setSorting(sorting
);
208 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
210 m_proxyModel
->setSortOrder(order
);
213 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
215 if (show
!= m_dirLister
->showingDotFiles()) {
216 m_dirLister
->setShowingDotFiles(show
);
218 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
222 void DolphinColumnWidget::setShowPreview(bool show
)
224 m_previewGenerator
->setPreviewShown(show
);
227 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
230 void DolphinColumnWidget::updateBackground()
232 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
233 // cleaning up the cut-indication of DolphinColumnWidget with the code from
234 // DolphinView a common helper-class should be available which can be shared
235 // by all view implementations -> no hardcoded value anymore
236 const QPalette::ColorRole role
= viewport()->backgroundRole();
237 QColor color
= viewport()->palette().color(role
);
238 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
240 QPalette palette
= viewport()->palette();
241 palette
.setColor(role
, color
);
242 viewport()->setPalette(palette
);
247 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
249 m_proxyModel
->setFilterRegExp(nameFilter
);
252 void DolphinColumnWidget::editItem(const KFileItem
& item
)
254 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
255 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
256 if (proxyIndex
.isValid()) {
261 KFileItem
DolphinColumnWidget::itemAt(const QPoint
& pos
) const
264 const QModelIndex index
= indexAt(pos
);
265 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
266 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
267 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
272 KFileItemList
DolphinColumnWidget::selectedItems() const
274 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
275 KFileItemList itemList
;
277 const QModelIndexList indexList
= selection
.indexes();
278 foreach (const QModelIndex
&index
, indexList
) {
279 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
280 if (!item
.isNull()) {
281 itemList
.append(item
);
288 QMimeData
* DolphinColumnWidget::selectionMimeData() const
290 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
291 return m_dolphinModel
->mimeData(selection
.indexes());
294 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
296 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
297 viewOptions
.font
= m_font
;
298 viewOptions
.decorationSize
= m_decorationSize
;
299 viewOptions
.showDecorationSelected
= true;
303 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
305 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_view
->m_controller
);
308 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
310 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
311 event
->acceptProposedAction();
316 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
318 QListView::dragLeaveEvent(event
);
319 setDirtyRegion(m_dropRect
);
322 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
324 QListView::dragMoveEvent(event
);
326 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
327 const QModelIndex index
= indexAt(event
->pos());
328 setDirtyRegion(m_dropRect
);
330 m_dropRect
.setSize(QSize()); // set as invalid
331 if (index
.isValid()) {
332 m_view
->m_controller
->setItemView(this);
333 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
334 if (!item
.isNull() && item
.isDir()) {
335 m_dropRect
= visualRect(index
);
338 setDirtyRegion(m_dropRect
);
340 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
341 // accept url drops, independently from the destination item
342 event
->acceptProposedAction();
346 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
348 const QModelIndex index
= indexAt(event
->pos());
349 m_view
->m_controller
->setItemView(this);
350 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
351 m_view
->m_controller
->indicateDroppedUrls(item
, url(), event
);
352 QListView::dropEvent(event
);
355 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
357 if (!m_childUrl
.isEmpty()) {
358 // indicate the shown URL of the next column by highlighting the shown folder item
359 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
360 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
361 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
362 const QRect itemRect
= visualRect(proxyIndex
);
363 QPainter
painter(viewport());
364 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
366 painter
.setPen(Qt::NoPen
);
367 painter
.setBrush(color
);
368 painter
.drawRect(itemRect
);
372 QListView::paintEvent(event
);
375 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
378 if (!indexAt(event
->pos()).isValid()) {
379 if (QApplication::mouseButtons() & Qt::MidButton
) {
380 m_view
->m_controller
->replaceUrlByClipboard();
382 } else if (event
->button() == Qt::LeftButton
) {
383 // TODO: see comment in DolphinIconsView::mousePressEvent()
384 setState(QAbstractItemView::DraggingState
);
386 QListView::mousePressEvent(event
);
389 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
391 QListView::keyPressEvent(event
);
394 DolphinController
* controller
= m_view
->m_controller
;
395 controller
->handleKeyPressEvent(event
);
396 switch (event
->key()) {
397 case Qt::Key_Right
: {
398 // Special key handling for the column: A Key_Right should
399 // open a new column for the currently selected folder.
400 const QModelIndex index
= currentIndex();
401 const KFileItem item
= controller
->itemForIndex(index
);
402 if (!item
.isNull() && item
.isDir()) {
403 controller
->emitItemTriggered(item
);
409 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
410 QItemSelectionModel::Current
|
411 QItemSelectionModel::Clear
);
418 if (m_toolTipManager
!= 0) {
419 m_toolTipManager
->hideTip();
423 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
426 m_view
->requestActivation(this);
427 Q_ASSERT(m_view
->m_controller
->itemView() == this);
428 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
432 QListView::contextMenuEvent(event
);
434 const QModelIndex index
= indexAt(event
->pos());
435 if (!index
.isValid()) {
439 if (m_toolTipManager
!= 0) {
440 m_toolTipManager
->hideTip();
443 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
444 Q_ASSERT(m_view
->m_controller
->itemView() == this);
445 m_view
->m_controller
->triggerContextMenuRequest(pos
);
448 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
450 if (m_selectionManager
!= 0) {
451 m_selectionManager
->reset();
454 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
455 if (event
->modifiers() & Qt::ControlModifier
) {
460 const int height
= m_decorationSize
.height();
461 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
462 verticalScrollBar()->setSingleStep(step
);
464 QListView::wheelEvent(event
);
467 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
469 QListView::leaveEvent(event
);
470 // if the mouse is above an item and moved very fast outside the widget,
471 // no viewportEntered() signal might be emitted although the mouse has been moved
472 // above the viewport
473 m_view
->m_controller
->emitViewportEntered();
476 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
478 QListView::selectionChanged(selected
, deselected
);
480 QItemSelectionModel
* selModel
= m_view
->selectionModel();
481 selModel
->select(selected
, QItemSelectionModel::Select
);
482 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
485 void DolphinColumnWidget::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
487 QListView::currentChanged(current
, previous
);
488 if (current
.isValid() && !m_autoScroller
->isActive()) {
493 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
495 m_view
->m_controller
->setItemView(this);
496 m_view
->m_controller
->emitItemEntered(index
);
499 void DolphinColumnWidget::slotClicked(const QModelIndex
& index
)
501 DolphinController
* controller
= m_view
->m_controller
;
502 if (KGlobalSettings::singleClick()) {
503 controller
->triggerItem(index
);
505 // even when using double click, a directory should be opened
506 // after the first click
507 const KFileItem item
= controller
->itemForIndex(index
);
508 if (!item
.isNull() && item
.isDir()) {
509 controller
->triggerItem(index
);
514 void DolphinColumnWidget::slotDoubleClicked(const QModelIndex
& index
)
516 if (!KGlobalSettings::singleClick()) {
517 m_view
->m_controller
->triggerItem(index
);
521 void DolphinColumnWidget::requestActivation()
523 m_view
->m_controller
->setItemView(this);
524 m_view
->m_controller
->requestActivation();
526 m_view
->requestActivation(this);
527 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
528 selectionModel()->clear();
532 void DolphinColumnWidget::updateFont()
534 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
535 Q_ASSERT(settings
!= 0);
537 if (settings
->useSystemFont()) {
538 m_font
= KGlobalSettings::generalFont();
542 void DolphinColumnWidget::activate()
544 setFocus(Qt::OtherFocusReason
);
546 connect(this, SIGNAL(clicked(const QModelIndex
&)),
547 m_view
->m_controller
, SLOT(requestTab(const QModelIndex
&)));
548 connect(this, SIGNAL(clicked(const QModelIndex
&)),
549 this, SLOT(slotClicked(const QModelIndex
&)));
550 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
551 this, SLOT(slotDoubleClicked(const QModelIndex
&)));
553 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
554 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
560 void DolphinColumnWidget::deactivate()
563 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
564 m_view
->m_controller
, SLOT(requestTab(const QModelIndex
&)));
565 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
566 this, SLOT(slotClicked(const QModelIndex
&)));
567 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
568 this, SLOT(slotDoubleClicked(const QModelIndex
&)));
570 const QModelIndex current
= selectionModel()->currentIndex();
571 selectionModel()->clear();
572 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
576 #include "dolphincolumnwidget.moc"