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 "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 "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 (m_active
== active
) {
194 void DolphinColumnWidget::reload()
197 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
200 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
202 m_proxyModel
->setSorting(sorting
);
205 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
207 m_proxyModel
->setSortOrder(order
);
210 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
212 if (show
!= m_dirLister
->showingDotFiles()) {
213 m_dirLister
->setShowingDotFiles(show
);
215 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
219 void DolphinColumnWidget::setShowPreview(bool show
)
221 m_previewGenerator
->setPreviewShown(show
);
224 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
227 void DolphinColumnWidget::updateBackground()
229 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
230 // cleaning up the cut-indication of DolphinColumnWidget with the code from
231 // DolphinView a common helper-class should be available which can be shared
232 // by all view implementations -> no hardcoded value anymore
233 const QPalette::ColorRole role
= viewport()->backgroundRole();
234 QColor color
= viewport()->palette().color(role
);
235 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
237 QPalette palette
= viewport()->palette();
238 palette
.setColor(role
, color
);
239 viewport()->setPalette(palette
);
244 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
246 m_proxyModel
->setFilterRegExp(nameFilter
);
249 void DolphinColumnWidget::editItem(const KFileItem
& item
)
251 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
252 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
253 if (proxyIndex
.isValid()) {
258 KFileItem
DolphinColumnWidget::itemAt(const QPoint
& pos
) const
261 const QModelIndex index
= indexAt(pos
);
262 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
263 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
264 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
269 KFileItemList
DolphinColumnWidget::selectedItems() const
271 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
272 KFileItemList itemList
;
274 const QModelIndexList indexList
= selection
.indexes();
275 foreach (const QModelIndex
&index
, indexList
) {
276 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
277 if (!item
.isNull()) {
278 itemList
.append(item
);
285 QMimeData
* DolphinColumnWidget::selectionMimeData() const
287 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
288 return m_dolphinModel
->mimeData(selection
.indexes());
291 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
293 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
294 viewOptions
.font
= m_font
;
295 viewOptions
.decorationSize
= m_decorationSize
;
296 viewOptions
.showDecorationSelected
= true;
300 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
302 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_view
->m_controller
);
305 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
307 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
308 event
->acceptProposedAction();
313 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
315 QListView::dragLeaveEvent(event
);
316 setDirtyRegion(m_dropRect
);
319 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
321 QListView::dragMoveEvent(event
);
323 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
324 const QModelIndex index
= indexAt(event
->pos());
325 setDirtyRegion(m_dropRect
);
327 m_dropRect
.setSize(QSize()); // set as invalid
328 if (index
.isValid()) {
329 m_view
->m_controller
->setItemView(this);
330 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
331 if (!item
.isNull() && item
.isDir()) {
332 m_dropRect
= visualRect(index
);
335 setDirtyRegion(m_dropRect
);
337 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
338 // accept url drops, independently from the destination item
339 event
->acceptProposedAction();
343 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
345 const QModelIndex index
= indexAt(event
->pos());
346 m_view
->m_controller
->setItemView(this);
347 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
348 m_view
->m_controller
->indicateDroppedUrls(item
, url(), event
);
349 QListView::dropEvent(event
);
352 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
354 if (!m_childUrl
.isEmpty()) {
355 // indicate the shown URL of the next column by highlighting the shown folder item
356 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
357 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
358 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
359 const QRect itemRect
= visualRect(proxyIndex
);
360 QPainter
painter(viewport());
361 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
363 painter
.setPen(Qt::NoPen
);
364 painter
.setBrush(color
);
365 painter
.drawRect(itemRect
);
369 QListView::paintEvent(event
);
372 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
375 if (!indexAt(event
->pos()).isValid()) {
376 if (QApplication::mouseButtons() & Qt::MidButton
) {
377 m_view
->m_controller
->replaceUrlByClipboard();
379 } else if (event
->button() == Qt::LeftButton
) {
380 // TODO: see comment in DolphinIconsView::mousePressEvent()
381 setState(QAbstractItemView::DraggingState
);
383 QListView::mousePressEvent(event
);
386 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
388 QListView::keyPressEvent(event
);
390 m_view
->m_controller
->handleKeyPressEvent(event
);
393 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
396 m_view
->requestActivation(this);
397 Q_ASSERT(m_view
->m_controller
->itemView() == this);
398 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
402 QListView::contextMenuEvent(event
);
404 const QModelIndex index
= indexAt(event
->pos());
405 if (!index
.isValid()) {
409 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
410 Q_ASSERT(m_view
->m_controller
->itemView() == this);
411 m_view
->m_controller
->triggerContextMenuRequest(pos
);
414 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
416 if (m_selectionManager
!= 0) {
417 m_selectionManager
->reset();
420 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
421 if (event
->modifiers() & Qt::ControlModifier
) {
426 const int height
= m_decorationSize
.height();
427 const int step
= (height
>= KIconLoader::SizeHuge
) ? height
/ 10 : (KIconLoader::SizeHuge
- height
) / 2;
428 verticalScrollBar()->setSingleStep(step
);
430 QListView::wheelEvent(event
);
433 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
435 QListView::leaveEvent(event
);
436 // if the mouse is above an item and moved very fast outside the widget,
437 // no viewportEntered() signal might be emitted although the mouse has been moved
438 // above the viewport
439 m_view
->m_controller
->emitViewportEntered();
442 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
444 QListView::selectionChanged(selected
, deselected
);
446 QItemSelectionModel
* selModel
= m_view
->selectionModel();
447 selModel
->select(selected
, QItemSelectionModel::Select
);
448 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
451 void DolphinColumnWidget::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
453 QListView::currentChanged(current
, previous
);
454 if (current
.isValid() && !m_autoScroller
->isActive()) {
459 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
461 m_view
->m_controller
->setItemView(this);
462 m_view
->m_controller
->emitItemEntered(index
);
465 void DolphinColumnWidget::requestActivation()
467 m_view
->m_controller
->setItemView(this);
468 m_view
->m_controller
->requestActivation();
470 m_view
->requestActivation(this);
471 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
472 selectionModel()->clear();
476 void DolphinColumnWidget::updateFont()
478 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
479 Q_ASSERT(settings
!= 0);
481 if (settings
->useSystemFont()) {
482 m_font
= KGlobalSettings::generalFont();
486 void DolphinColumnWidget::activate()
488 setFocus(Qt::OtherFocusReason
);
490 connect(this, SIGNAL(clicked(const QModelIndex
&)),
491 m_view
->m_controller
, SLOT(requestTab(const QModelIndex
&)));
492 if (KGlobalSettings::singleClick()) {
493 connect(this, SIGNAL(clicked(const QModelIndex
&)),
494 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
496 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
497 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
500 if (selectionModel() && selectionModel()->currentIndex().isValid())
501 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
506 void DolphinColumnWidget::deactivate()
510 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
511 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
512 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
513 if (KGlobalSettings::singleClick()) {
514 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
515 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
517 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
518 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
521 const QModelIndex current
= selectionModel()->currentIndex();
522 selectionModel()->clear();
523 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
527 #include "dolphincolumnwidget.moc"