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>
50 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
51 DolphinColumnView
* columnView
,
56 m_selectionManager(0),
64 m_previewGenerator(0),
67 setMouseTracking(true);
68 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
69 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
70 setSelectionBehavior(SelectItems
);
71 setSelectionMode(QAbstractItemView::ExtendedSelection
);
72 setDragDropMode(QAbstractItemView::DragDrop
);
73 setDropIndicatorShown(false);
74 setSelectionRectVisible(true);
75 setEditTriggers(QAbstractItemView::NoEditTriggers
);
77 setVerticalScrollMode(QListView::ScrollPerPixel
);
78 setHorizontalScrollMode(QListView::ScrollPerPixel
);
80 new DolphinViewAutoScroller(this);
82 // apply the column mode settings to the widget
83 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
84 Q_ASSERT(settings
!= 0);
86 if (settings
->useSystemFont()) {
87 m_font
= KGlobalSettings::generalFont();
89 m_font
= QFont(settings
->fontFamily(),
91 settings
->fontWeight(),
92 settings
->italicFont());
95 const int iconSize
= settings
->iconSize();
96 setDecorationSize(QSize(iconSize
, iconSize
));
98 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
99 delegate
->setShowToolTipWhenElided(false);
100 setItemDelegate(delegate
);
104 connect(this, SIGNAL(viewportEntered()),
105 m_view
->m_controller
, SLOT(emitViewportEntered()));
106 connect(this, SIGNAL(entered(const QModelIndex
&)),
107 this, SLOT(slotEntered(const QModelIndex
&)));
109 m_dirLister
= new DolphinDirLister();
110 m_dirLister
->setAutoUpdate(true);
111 m_dirLister
->setMainWindow(window());
112 m_dirLister
->setDelayedMimeTypes(true);
113 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
114 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
116 m_dolphinModel
= new DolphinModel(this);
117 m_dolphinModel
->setDirLister(m_dirLister
);
118 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
120 m_proxyModel
= new DolphinSortFilterProxyModel(this);
121 m_proxyModel
->setSourceModel(m_dolphinModel
);
122 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
123 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
124 m_proxyModel
->setSorting(dolphinView
->sorting());
125 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
127 setModel(m_proxyModel
);
129 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
130 m_selectionManager
= new SelectionManager(this);
131 connect(m_selectionManager
, SIGNAL(selectionChanged()),
132 this, SLOT(requestActivation()));
133 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
134 m_selectionManager
, SLOT(reset()));
137 m_previewGenerator
= new KFilePreviewGenerator(this);
138 m_previewGenerator
->setPreviewShown(m_view
->m_controller
->dolphinView()->showPreview());
140 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
141 new ToolTipManager(this, m_proxyModel
);
144 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
146 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
147 this, SLOT(updateFont()));
149 FolderExpander
* folderExpander
= new FolderExpander(this, m_proxyModel
);
150 folderExpander
->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
151 connect (folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
152 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
155 DolphinColumnWidget::~DolphinColumnWidget()
159 delete m_dolphinModel
;
161 m_dirLister
= 0; // deleted by m_dolphinModel
164 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
167 m_decorationSize
= size
;
169 if (m_previewGenerator
!= 0) {
170 m_previewGenerator
->updatePreviews();
172 if (m_selectionManager
!= 0) {
173 m_selectionManager
->reset();
177 void DolphinColumnWidget::setActive(bool active
)
179 if (m_active
== active
) {
192 void DolphinColumnWidget::reload()
195 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
198 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
200 m_proxyModel
->setSorting(sorting
);
203 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
205 m_proxyModel
->setSortOrder(order
);
208 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
210 if (show
!= m_dirLister
->showingDotFiles()) {
211 m_dirLister
->setShowingDotFiles(show
);
213 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
217 void DolphinColumnWidget::setShowPreview(bool show
)
219 m_previewGenerator
->setPreviewShown(show
);
222 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
225 void DolphinColumnWidget::updateBackground()
227 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
228 // cleaning up the cut-indication of DolphinColumnWidget with the code from
229 // DolphinView a common helper-class should be available which can be shared
230 // by all view implementations -> no hardcoded value anymore
231 const QPalette::ColorRole role
= viewport()->backgroundRole();
232 QColor color
= viewport()->palette().color(role
);
233 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
235 QPalette palette
= viewport()->palette();
236 palette
.setColor(role
, color
);
237 viewport()->setPalette(palette
);
242 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
244 m_proxyModel
->setFilterRegExp(nameFilter
);
247 void DolphinColumnWidget::editItem(const KFileItem
& item
)
249 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
250 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
251 if (proxyIndex
.isValid()) {
256 KFileItem
DolphinColumnWidget::itemAt(const QPoint
& pos
) const
259 const QModelIndex index
= indexAt(pos
);
260 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
261 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
262 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
267 KFileItemList
DolphinColumnWidget::selectedItems() const
269 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
270 KFileItemList itemList
;
272 const QModelIndexList indexList
= selection
.indexes();
273 foreach (const QModelIndex
&index
, indexList
) {
274 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
275 if (!item
.isNull()) {
276 itemList
.append(item
);
283 QMimeData
* DolphinColumnWidget::selectionMimeData() const
285 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
286 return m_dolphinModel
->mimeData(selection
.indexes());
289 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
291 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
292 viewOptions
.font
= m_font
;
293 viewOptions
.decorationSize
= m_decorationSize
;
294 viewOptions
.showDecorationSelected
= true;
298 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
300 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_view
->m_controller
);
303 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
305 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
306 event
->acceptProposedAction();
311 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
313 QListView::dragLeaveEvent(event
);
314 setDirtyRegion(m_dropRect
);
317 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
319 QListView::dragMoveEvent(event
);
321 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
322 const QModelIndex index
= indexAt(event
->pos());
323 setDirtyRegion(m_dropRect
);
325 m_dropRect
.setSize(QSize()); // set as invalid
326 if (index
.isValid()) {
327 m_view
->m_controller
->setItemView(this);
328 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
329 if (!item
.isNull() && item
.isDir()) {
330 m_dropRect
= visualRect(index
);
333 setDirtyRegion(m_dropRect
);
335 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
336 // accept url drops, independently from the destination item
337 event
->acceptProposedAction();
341 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
343 const QModelIndex index
= indexAt(event
->pos());
344 m_view
->m_controller
->setItemView(this);
345 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
346 m_view
->m_controller
->indicateDroppedUrls(item
, url(), event
);
347 QListView::dropEvent(event
);
350 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
352 if (!m_childUrl
.isEmpty()) {
353 // indicate the shown URL of the next column by highlighting the shown folder item
354 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
355 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
356 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
357 const QRect itemRect
= visualRect(proxyIndex
);
358 QPainter
painter(viewport());
359 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
361 painter
.setPen(Qt::NoPen
);
362 painter
.setBrush(color
);
363 painter
.drawRect(itemRect
);
367 QListView::paintEvent(event
);
370 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
373 if (!indexAt(event
->pos()).isValid()) {
374 if (QApplication::mouseButtons() & Qt::MidButton
) {
375 m_view
->m_controller
->replaceUrlByClipboard();
377 } else if (event
->button() == Qt::LeftButton
) {
378 // TODO: see comment in DolphinIconsView::mousePressEvent()
379 setState(QAbstractItemView::DraggingState
);
381 QListView::mousePressEvent(event
);
384 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
386 QListView::keyPressEvent(event
);
388 m_view
->m_controller
->handleKeyPressEvent(event
);
391 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
394 m_view
->requestActivation(this);
395 Q_ASSERT(m_view
->m_controller
->itemView() == this);
396 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
400 QListView::contextMenuEvent(event
);
402 const QModelIndex index
= indexAt(event
->pos());
403 if (!index
.isValid()) {
407 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
408 Q_ASSERT(m_view
->m_controller
->itemView() == this);
409 m_view
->m_controller
->triggerContextMenuRequest(pos
);
412 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
414 if (m_selectionManager
!= 0) {
415 m_selectionManager
->reset();
418 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
419 if (event
->modifiers() & Qt::ControlModifier
) {
424 QListView::wheelEvent(event
);
427 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
429 QListView::leaveEvent(event
);
430 // if the mouse is above an item and moved very fast outside the widget,
431 // no viewportEntered() signal might be emitted although the mouse has been moved
432 // above the viewport
433 m_view
->m_controller
->emitViewportEntered();
436 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
438 QListView::selectionChanged(selected
, deselected
);
440 QItemSelectionModel
* selModel
= m_view
->selectionModel();
441 selModel
->select(selected
, QItemSelectionModel::Select
);
442 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
445 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
447 m_view
->m_controller
->setItemView(this);
448 m_view
->m_controller
->emitItemEntered(index
);
451 void DolphinColumnWidget::requestActivation()
453 m_view
->m_controller
->setItemView(this);
454 m_view
->m_controller
->requestActivation();
456 m_view
->requestActivation(this);
457 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
458 selectionModel()->clear();
462 void DolphinColumnWidget::updateFont()
464 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
465 Q_ASSERT(settings
!= 0);
467 if (settings
->useSystemFont()) {
468 m_font
= KGlobalSettings::generalFont();
472 void DolphinColumnWidget::activate()
474 setFocus(Qt::OtherFocusReason
);
476 connect(this, SIGNAL(clicked(const QModelIndex
&)),
477 m_view
->m_controller
, SLOT(requestTab(const QModelIndex
&)));
478 if (KGlobalSettings::singleClick()) {
479 connect(this, SIGNAL(clicked(const QModelIndex
&)),
480 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
482 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
483 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
486 if (selectionModel() && selectionModel()->currentIndex().isValid())
487 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
492 void DolphinColumnWidget::deactivate()
496 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
497 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
498 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
499 if (KGlobalSettings::singleClick()) {
500 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
501 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
503 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
504 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
507 const QModelIndex current
= selectionModel()->currentIndex();
508 selectionModel()->clear();
509 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
513 #include "dolphincolumnwidget.moc"