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 "dolphin_columnmodesettings.h"
29 #include "dolphin_generalsettings.h"
30 #include "draganddrophelper.h"
31 #include "folderexpander.h"
32 #include "selectionmanager.h"
33 #include "tooltipmanager.h"
35 #include <kcolorscheme.h>
36 #include <kdirlister.h>
37 #include <kfileitem.h>
38 #include <kfilepreviewgenerator.h>
39 #include <kio/previewjob.h>
40 #include <kiconeffect.h>
42 #include <konqmimedata.h>
44 #include <QApplication>
49 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
50 DolphinColumnView
* columnView
,
55 m_selectionManager(0),
63 m_previewGenerator(0),
66 setMouseTracking(true);
67 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
68 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
69 setSelectionBehavior(SelectItems
);
70 setSelectionMode(QAbstractItemView::ExtendedSelection
);
71 setDragDropMode(QAbstractItemView::DragDrop
);
72 setDropIndicatorShown(false);
73 setSelectionRectVisible(true);
74 setEditTriggers(QAbstractItemView::NoEditTriggers
);
76 setVerticalScrollMode(QListView::ScrollPerPixel
);
77 setHorizontalScrollMode(QListView::ScrollPerPixel
);
79 // apply the column mode settings to the widget
80 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
81 Q_ASSERT(settings
!= 0);
83 if (settings
->useSystemFont()) {
84 m_font
= KGlobalSettings::generalFont();
86 m_font
= QFont(settings
->fontFamily(),
88 settings
->fontWeight(),
89 settings
->italicFont());
92 const int iconSize
= settings
->iconSize();
93 setDecorationSize(QSize(iconSize
, iconSize
));
95 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
96 delegate
->setShowToolTipWhenElided(false);
97 setItemDelegate(delegate
);
101 connect(this, SIGNAL(viewportEntered()),
102 m_view
->m_controller
, SLOT(emitViewportEntered()));
103 connect(this, SIGNAL(entered(const QModelIndex
&)),
104 this, SLOT(slotEntered(const QModelIndex
&)));
106 //m_dirLister = new DolphinDirLister(); TODO
107 m_dirLister
= new KDirLister();
108 m_dirLister
->setAutoUpdate(true);
109 m_dirLister
->setMainWindow(window());
110 m_dirLister
->setDelayedMimeTypes(true);
111 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
112 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
114 m_dolphinModel
= new DolphinModel(this);
115 m_dolphinModel
->setDirLister(m_dirLister
);
116 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
118 m_proxyModel
= new DolphinSortFilterProxyModel(this);
119 m_proxyModel
->setSourceModel(m_dolphinModel
);
120 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
121 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
122 m_proxyModel
->setSorting(dolphinView
->sorting());
123 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
125 setModel(m_proxyModel
);
127 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
128 m_selectionManager
= new SelectionManager(this);
129 connect(m_selectionManager
, SIGNAL(selectionChanged()),
130 this, SLOT(requestActivation()));
131 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
132 m_selectionManager
, SLOT(reset()));
135 m_previewGenerator
= new KFilePreviewGenerator(this);
136 m_previewGenerator
->setPreviewShown(m_view
->m_controller
->dolphinView()->showPreview());
138 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
139 new ToolTipManager(this, m_proxyModel
);
142 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
144 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
145 this, SLOT(updateFont()));
147 FolderExpander
* folderExpander
= new FolderExpander(this, m_proxyModel
);
148 folderExpander
->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
149 connect (folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
150 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
153 DolphinColumnWidget::~DolphinColumnWidget()
157 delete m_dolphinModel
;
159 m_dirLister
= 0; // deleted by m_dolphinModel
162 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
165 m_decorationSize
= size
;
167 if (m_previewGenerator
!= 0) {
168 m_previewGenerator
->updatePreviews();
170 if (m_selectionManager
!= 0) {
171 m_selectionManager
->reset();
175 void DolphinColumnWidget::setActive(bool active
)
177 if (m_active
== active
) {
190 void DolphinColumnWidget::reload()
193 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
196 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
198 m_proxyModel
->setSorting(sorting
);
201 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
203 m_proxyModel
->setSortOrder(order
);
206 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
208 if (show
!= m_dirLister
->showingDotFiles()) {
209 m_dirLister
->setShowingDotFiles(show
);
211 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
215 void DolphinColumnWidget::setShowPreview(bool show
)
217 m_previewGenerator
->setPreviewShown(show
);
220 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
223 void DolphinColumnWidget::updateBackground()
225 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
226 // cleaning up the cut-indication of DolphinColumnWidget with the code from
227 // DolphinView a common helper-class should be available which can be shared
228 // by all view implementations -> no hardcoded value anymore
229 const QPalette::ColorRole role
= viewport()->backgroundRole();
230 QColor color
= viewport()->palette().color(role
);
231 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
233 QPalette palette
= viewport()->palette();
234 palette
.setColor(role
, color
);
235 viewport()->setPalette(palette
);
240 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
242 m_proxyModel
->setFilterRegExp(nameFilter
);
245 void DolphinColumnWidget::editItem(const KFileItem
& item
)
247 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
248 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
249 if (proxyIndex
.isValid()) {
254 KFileItem
DolphinColumnWidget::itemAt(const QPoint
& pos
) const
257 const QModelIndex index
= indexAt(pos
);
258 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
259 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
260 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
265 KFileItemList
DolphinColumnWidget::selectedItems() const
267 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
268 KFileItemList itemList
;
270 const QModelIndexList indexList
= selection
.indexes();
271 foreach (const QModelIndex
&index
, indexList
) {
272 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
273 if (!item
.isNull()) {
274 itemList
.append(item
);
281 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
283 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
284 viewOptions
.font
= m_font
;
285 viewOptions
.decorationSize
= m_decorationSize
;
286 viewOptions
.showDecorationSelected
= true;
290 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
292 DragAndDropHelper::startDrag(this, supportedActions
);
295 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
297 if (event
->mimeData()->hasUrls()) {
298 event
->acceptProposedAction();
302 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
304 QListView::dragLeaveEvent(event
);
305 setDirtyRegion(m_dropRect
);
308 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
310 QListView::dragMoveEvent(event
);
312 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
313 const QModelIndex index
= indexAt(event
->pos());
314 setDirtyRegion(m_dropRect
);
316 m_dropRect
.setSize(QSize()); // set as invalid
317 if (index
.isValid()) {
318 m_view
->m_controller
->setItemView(this);
319 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
320 if (!item
.isNull() && item
.isDir()) {
321 m_dropRect
= visualRect(index
);
324 setDirtyRegion(m_dropRect
);
326 if (event
->mimeData()->hasUrls()) {
327 // accept url drops, independently from the destination item
328 event
->acceptProposedAction();
332 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
334 const QModelIndex index
= indexAt(event
->pos());
335 m_view
->m_controller
->setItemView(this);
336 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
337 m_view
->m_controller
->indicateDroppedUrls(item
, url(), event
);
338 QListView::dropEvent(event
);
341 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
343 if (!m_childUrl
.isEmpty()) {
344 // indicate the shown URL of the next column by highlighting the shown folder item
345 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
346 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
347 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
348 const QRect itemRect
= visualRect(proxyIndex
);
349 QPainter
painter(viewport());
350 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
352 painter
.setPen(Qt::NoPen
);
353 painter
.setBrush(color
);
354 painter
.drawRect(itemRect
);
358 QListView::paintEvent(event
);
361 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
364 if (!indexAt(event
->pos()).isValid()) {
365 if (QApplication::mouseButtons() & Qt::MidButton
) {
366 m_view
->m_controller
->replaceUrlByClipboard();
368 } else if (event
->button() == Qt::LeftButton
) {
369 // TODO: see comment in DolphinIconsView::mousePressEvent()
370 setState(QAbstractItemView::DraggingState
);
372 QListView::mousePressEvent(event
);
375 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
377 QListView::keyPressEvent(event
);
379 m_view
->m_controller
->handleKeyPressEvent(event
);
382 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
385 m_view
->requestActivation(this);
386 Q_ASSERT(m_view
->m_controller
->itemView() == this);
387 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
391 QListView::contextMenuEvent(event
);
393 const QModelIndex index
= indexAt(event
->pos());
394 if (!index
.isValid()) {
398 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
399 Q_ASSERT(m_view
->m_controller
->itemView() == this);
400 m_view
->m_controller
->triggerContextMenuRequest(pos
);
403 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
405 if (m_selectionManager
!= 0) {
406 m_selectionManager
->reset();
409 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
410 if (event
->modifiers() & Qt::ControlModifier
) {
415 QListView::wheelEvent(event
);
418 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
420 QListView::leaveEvent(event
);
421 // if the mouse is above an item and moved very fast outside the widget,
422 // no viewportEntered() signal might be emitted although the mouse has been moved
423 // above the viewport
424 m_view
->m_controller
->emitViewportEntered();
427 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
429 QListView::selectionChanged(selected
, deselected
);
431 QItemSelectionModel
* selModel
= m_view
->selectionModel();
432 selModel
->select(selected
, QItemSelectionModel::Select
);
433 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
436 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
438 m_view
->m_controller
->setItemView(this);
439 m_view
->m_controller
->emitItemEntered(index
);
442 void DolphinColumnWidget::requestActivation()
444 m_view
->m_controller
->setItemView(this);
445 m_view
->m_controller
->requestActivation();
447 m_view
->requestActivation(this);
448 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
449 selectionModel()->clear();
453 void DolphinColumnWidget::updateFont()
455 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
456 Q_ASSERT(settings
!= 0);
458 if (settings
->useSystemFont()) {
459 m_font
= KGlobalSettings::generalFont();
463 void DolphinColumnWidget::activate()
465 setFocus(Qt::OtherFocusReason
);
467 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
468 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
469 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
470 if (KGlobalSettings::singleClick()) {
471 connect(this, SIGNAL(clicked(const QModelIndex
&)),
472 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
474 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
475 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
478 if (selectionModel() && selectionModel()->currentIndex().isValid())
479 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
484 void DolphinColumnWidget::deactivate()
488 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
489 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
490 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
491 if (KGlobalSettings::singleClick()) {
492 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
493 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
495 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
496 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
499 const QModelIndex current
= selectionModel()->currentIndex();
500 selectionModel()->clear();
501 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
505 #include "dolphincolumnwidget.moc"