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),
69 setMouseTracking(true);
70 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
71 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
72 setSelectionBehavior(SelectItems
);
73 setSelectionMode(QAbstractItemView::ExtendedSelection
);
74 setDragDropMode(QAbstractItemView::DragDrop
);
75 setDropIndicatorShown(false);
76 setSelectionRectVisible(true);
77 setEditTriggers(QAbstractItemView::NoEditTriggers
);
79 setVerticalScrollMode(QListView::ScrollPerPixel
);
80 setHorizontalScrollMode(QListView::ScrollPerPixel
);
82 m_autoScroller
= new DolphinViewAutoScroller(this);
84 // apply the column mode settings to the widget
85 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
86 Q_ASSERT(settings
!= 0);
88 if (settings
->useSystemFont()) {
89 m_font
= KGlobalSettings::generalFont();
91 m_font
= QFont(settings
->fontFamily(),
93 settings
->fontWeight(),
94 settings
->italicFont());
97 const int iconSize
= settings
->iconSize();
98 setDecorationSize(QSize(iconSize
, iconSize
));
100 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
101 delegate
->setShowToolTipWhenElided(false);
102 setItemDelegate(delegate
);
106 connect(this, SIGNAL(viewportEntered()),
107 m_view
->m_controller
, SLOT(emitViewportEntered()));
108 connect(this, SIGNAL(entered(const QModelIndex
&)),
109 this, SLOT(slotEntered(const QModelIndex
&)));
111 m_dirLister
= new DolphinDirLister();
112 m_dirLister
->setAutoUpdate(true);
113 m_dirLister
->setMainWindow(window());
114 m_dirLister
->setDelayedMimeTypes(true);
115 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
116 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
118 m_dolphinModel
= new DolphinModel(this);
119 m_dolphinModel
->setDirLister(m_dirLister
);
120 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
122 m_proxyModel
= new DolphinSortFilterProxyModel(this);
123 m_proxyModel
->setSourceModel(m_dolphinModel
);
124 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
125 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
126 m_proxyModel
->setSorting(dolphinView
->sorting());
127 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
129 setModel(m_proxyModel
);
131 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
132 m_selectionManager
= new SelectionManager(this);
133 connect(m_selectionManager
, SIGNAL(selectionChanged()),
134 this, SLOT(requestActivation()));
135 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
136 m_selectionManager
, SLOT(reset()));
139 m_previewGenerator
= new KFilePreviewGenerator(this);
140 m_previewGenerator
->setPreviewShown(m_view
->m_controller
->dolphinView()->showPreview());
142 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
143 new ToolTipManager(this, m_proxyModel
);
146 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
148 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
149 this, SLOT(updateFont()));
151 FolderExpander
* folderExpander
= new FolderExpander(this, m_proxyModel
);
152 folderExpander
->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
153 connect (folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
154 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
157 DolphinColumnWidget::~DolphinColumnWidget()
161 delete m_dolphinModel
;
163 m_dirLister
= 0; // deleted by m_dolphinModel
166 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
169 m_decorationSize
= size
;
171 if (m_previewGenerator
!= 0) {
172 m_previewGenerator
->updatePreviews();
174 if (m_selectionManager
!= 0) {
175 m_selectionManager
->reset();
179 void DolphinColumnWidget::setActive(bool active
)
181 if (active
&& (m_view
->focusProxy() != this)) {
182 m_view
->setFocusProxy(this);
185 if (m_active
!= active
) {
196 void DolphinColumnWidget::reload()
199 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
202 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
204 m_proxyModel
->setSorting(sorting
);
207 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
209 m_proxyModel
->setSortOrder(order
);
212 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
214 if (show
!= m_dirLister
->showingDotFiles()) {
215 m_dirLister
->setShowingDotFiles(show
);
217 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
221 void DolphinColumnWidget::setShowPreview(bool show
)
223 m_previewGenerator
->setPreviewShown(show
);
226 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
229 void DolphinColumnWidget::updateBackground()
231 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
232 // cleaning up the cut-indication of DolphinColumnWidget with the code from
233 // DolphinView a common helper-class should be available which can be shared
234 // by all view implementations -> no hardcoded value anymore
235 const QPalette::ColorRole role
= viewport()->backgroundRole();
236 QColor color
= viewport()->palette().color(role
);
237 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
239 QPalette palette
= viewport()->palette();
240 palette
.setColor(role
, color
);
241 viewport()->setPalette(palette
);
246 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
248 m_proxyModel
->setFilterRegExp(nameFilter
);
251 void DolphinColumnWidget::editItem(const KFileItem
& item
)
253 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
254 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
255 if (proxyIndex
.isValid()) {
260 KFileItem
DolphinColumnWidget::itemAt(const QPoint
& pos
) const
263 const QModelIndex index
= indexAt(pos
);
264 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
265 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
266 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
271 KFileItemList
DolphinColumnWidget::selectedItems() const
273 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
274 KFileItemList itemList
;
276 const QModelIndexList indexList
= selection
.indexes();
277 foreach (const QModelIndex
&index
, indexList
) {
278 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
279 if (!item
.isNull()) {
280 itemList
.append(item
);
287 QMimeData
* DolphinColumnWidget::selectionMimeData() const
289 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
290 return m_dolphinModel
->mimeData(selection
.indexes());
293 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
295 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
296 viewOptions
.font
= m_font
;
297 viewOptions
.decorationSize
= m_decorationSize
;
298 viewOptions
.showDecorationSelected
= true;
302 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
304 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_view
->m_controller
);
307 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
309 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
310 event
->acceptProposedAction();
315 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
317 QListView::dragLeaveEvent(event
);
318 setDirtyRegion(m_dropRect
);
321 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
323 QListView::dragMoveEvent(event
);
325 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
326 const QModelIndex index
= indexAt(event
->pos());
327 setDirtyRegion(m_dropRect
);
329 m_dropRect
.setSize(QSize()); // set as invalid
330 if (index
.isValid()) {
331 m_view
->m_controller
->setItemView(this);
332 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
333 if (!item
.isNull() && item
.isDir()) {
334 m_dropRect
= visualRect(index
);
337 setDirtyRegion(m_dropRect
);
339 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
340 // accept url drops, independently from the destination item
341 event
->acceptProposedAction();
345 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
347 const QModelIndex index
= indexAt(event
->pos());
348 m_view
->m_controller
->setItemView(this);
349 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
350 m_view
->m_controller
->indicateDroppedUrls(item
, url(), event
);
351 QListView::dropEvent(event
);
354 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
356 if (!m_childUrl
.isEmpty()) {
357 // indicate the shown URL of the next column by highlighting the shown folder item
358 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
359 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
360 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
361 const QRect itemRect
= visualRect(proxyIndex
);
362 QPainter
painter(viewport());
363 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
365 painter
.setPen(Qt::NoPen
);
366 painter
.setBrush(color
);
367 painter
.drawRect(itemRect
);
371 QListView::paintEvent(event
);
374 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
377 if (!indexAt(event
->pos()).isValid()) {
378 if (QApplication::mouseButtons() & Qt::MidButton
) {
379 m_view
->m_controller
->replaceUrlByClipboard();
381 } else if (event
->button() == Qt::LeftButton
) {
382 // TODO: see comment in DolphinIconsView::mousePressEvent()
383 setState(QAbstractItemView::DraggingState
);
385 QListView::mousePressEvent(event
);
388 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
390 QListView::keyPressEvent(event
);
392 m_view
->m_controller
->handleKeyPressEvent(event
);
395 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
398 m_view
->requestActivation(this);
399 Q_ASSERT(m_view
->m_controller
->itemView() == this);
400 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
404 QListView::contextMenuEvent(event
);
406 const QModelIndex index
= indexAt(event
->pos());
407 if (!index
.isValid()) {
411 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
412 Q_ASSERT(m_view
->m_controller
->itemView() == this);
413 m_view
->m_controller
->triggerContextMenuRequest(pos
);
416 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
418 if (m_selectionManager
!= 0) {
419 m_selectionManager
->reset();
422 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
423 if (event
->modifiers() & Qt::ControlModifier
) {
428 const int height
= m_decorationSize
.height();
429 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
430 verticalScrollBar()->setSingleStep(step
);
432 QListView::wheelEvent(event
);
435 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
437 QListView::leaveEvent(event
);
438 // if the mouse is above an item and moved very fast outside the widget,
439 // no viewportEntered() signal might be emitted although the mouse has been moved
440 // above the viewport
441 m_view
->m_controller
->emitViewportEntered();
444 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
446 QListView::selectionChanged(selected
, deselected
);
448 QItemSelectionModel
* selModel
= m_view
->selectionModel();
449 selModel
->select(selected
, QItemSelectionModel::Select
);
450 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
453 void DolphinColumnWidget::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
455 QListView::currentChanged(current
, previous
);
456 if (current
.isValid() && !m_autoScroller
->isActive()) {
461 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
463 m_view
->m_controller
->setItemView(this);
464 m_view
->m_controller
->emitItemEntered(index
);
467 void DolphinColumnWidget::requestActivation()
469 m_view
->m_controller
->setItemView(this);
470 m_view
->m_controller
->requestActivation();
472 m_view
->requestActivation(this);
473 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
474 selectionModel()->clear();
478 void DolphinColumnWidget::updateFont()
480 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
481 Q_ASSERT(settings
!= 0);
483 if (settings
->useSystemFont()) {
484 m_font
= KGlobalSettings::generalFont();
488 void DolphinColumnWidget::activate()
490 setFocus(Qt::OtherFocusReason
);
492 connect(this, SIGNAL(clicked(const QModelIndex
&)),
493 m_view
->m_controller
, SLOT(requestTab(const QModelIndex
&)));
494 if (KGlobalSettings::singleClick()) {
495 connect(this, SIGNAL(clicked(const QModelIndex
&)),
496 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
498 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
499 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
502 if (selectionModel() && selectionModel()->currentIndex().isValid())
503 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
508 void DolphinColumnWidget::deactivate()
512 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
513 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
514 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
515 if (KGlobalSettings::singleClick()) {
516 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
517 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
519 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
520 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
523 const QModelIndex current
= selectionModel()->currentIndex();
524 selectionModel()->clear();
525 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
529 #include "dolphincolumnwidget.moc"