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 "iconmanager.h"
33 #include "selectionmanager.h"
34 #include "tooltipmanager.h"
36 #include <kcolorscheme.h>
37 #include <kdirlister.h>
38 #include <kfileitem.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),
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 setItemDelegate(delegate
);
100 connect(this, SIGNAL(viewportEntered()),
101 m_view
->m_controller
, SLOT(emitViewportEntered()));
102 connect(this, SIGNAL(entered(const QModelIndex
&)),
103 this, SLOT(slotEntered(const QModelIndex
&)));
105 //m_dirLister = new DolphinDirLister(); TODO
106 m_dirLister
= new KDirLister();
107 m_dirLister
->setAutoUpdate(true);
108 m_dirLister
->setMainWindow(window());
109 m_dirLister
->setDelayedMimeTypes(true);
110 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
111 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
113 m_dolphinModel
= new DolphinModel(this);
114 m_dolphinModel
->setDirLister(m_dirLister
);
115 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
117 m_proxyModel
= new DolphinSortFilterProxyModel(this);
118 m_proxyModel
->setSourceModel(m_dolphinModel
);
119 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
120 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
121 m_proxyModel
->setSorting(dolphinView
->sorting());
122 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
124 setModel(m_proxyModel
);
126 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
127 m_selectionManager
= new SelectionManager(this);
128 connect(m_selectionManager
, SIGNAL(selectionChanged()),
129 this, SLOT(requestActivation()));
130 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
131 m_selectionManager
, SLOT(reset()));
134 m_iconManager
= new IconManager(this, m_proxyModel
);
135 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
137 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
138 new ToolTipManager(this, m_proxyModel
);
141 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
143 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
144 this, SLOT(updateFont()));
146 FolderExpander
* folderExpander
= new FolderExpander(this, m_proxyModel
);
147 folderExpander
->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
148 connect (folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
149 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
152 DolphinColumnWidget::~DolphinColumnWidget()
156 delete m_dolphinModel
;
158 m_dirLister
= 0; // deleted by m_dolphinModel
161 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
164 m_decorationSize
= size
;
166 if (m_iconManager
!= 0) {
167 m_iconManager
->updatePreviews();
169 if (m_selectionManager
!= 0) {
170 m_selectionManager
->reset();
174 void DolphinColumnWidget::setActive(bool active
)
176 if (m_active
== active
) {
189 void DolphinColumnWidget::reload()
192 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
195 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
197 m_proxyModel
->setSorting(sorting
);
200 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
202 m_proxyModel
->setSortOrder(order
);
205 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
207 if (show
!= m_dirLister
->showingDotFiles()) {
208 m_dirLister
->setShowingDotFiles(show
);
210 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
214 void DolphinColumnWidget::setShowPreview(bool show
)
216 m_iconManager
->setShowPreview(show
);
219 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
222 void DolphinColumnWidget::updateBackground()
224 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
225 // cleaning up the cut-indication of DolphinColumnWidget with the code from
226 // DolphinView a common helper-class should be available which can be shared
227 // by all view implementations -> no hardcoded value anymore
228 const QPalette::ColorRole role
= viewport()->backgroundRole();
229 QColor color
= viewport()->palette().color(role
);
230 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
232 QPalette palette
= viewport()->palette();
233 palette
.setColor(role
, color
);
234 viewport()->setPalette(palette
);
239 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
241 m_proxyModel
->setFilterRegExp(nameFilter
);
244 void DolphinColumnWidget::editItem(const KFileItem
& item
)
246 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
247 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
248 if (proxyIndex
.isValid()) {
253 KFileItem
DolphinColumnWidget::itemAt(const QPoint
& pos
) const
256 const QModelIndex index
= indexAt(pos
);
257 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
258 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
259 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
264 KFileItemList
DolphinColumnWidget::selectedItems() const
266 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(selectionModel()->selection());
267 KFileItemList itemList
;
269 const QModelIndexList indexList
= selection
.indexes();
270 foreach (const QModelIndex
&index
, indexList
) {
271 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
272 if (!item
.isNull()) {
273 itemList
.append(item
);
280 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
282 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
283 viewOptions
.font
= m_font
;
284 viewOptions
.decorationSize
= m_decorationSize
;
285 viewOptions
.showDecorationSelected
= true;
289 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
291 DragAndDropHelper::startDrag(this, supportedActions
);
294 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
296 if (event
->mimeData()->hasUrls()) {
297 event
->acceptProposedAction();
301 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
303 QListView::dragLeaveEvent(event
);
304 setDirtyRegion(m_dropRect
);
307 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
309 QListView::dragMoveEvent(event
);
311 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
312 const QModelIndex index
= indexAt(event
->pos());
313 setDirtyRegion(m_dropRect
);
315 m_dropRect
.setSize(QSize()); // set as invalid
316 if (index
.isValid()) {
317 m_view
->m_controller
->setItemView(this);
318 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
319 if (!item
.isNull() && item
.isDir()) {
320 m_dropRect
= visualRect(index
);
323 setDirtyRegion(m_dropRect
);
325 if (event
->mimeData()->hasUrls()) {
326 // accept url drops, independently from the destination item
327 event
->acceptProposedAction();
331 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
333 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
334 if (!urls
.isEmpty()) {
335 const QModelIndex index
= indexAt(event
->pos());
336 m_view
->m_controller
->setItemView(this);
337 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
338 m_view
->m_controller
->indicateDroppedUrls(urls
,
341 event
->acceptProposedAction();
343 QListView::dropEvent(event
);
346 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
348 if (!m_childUrl
.isEmpty()) {
349 // indicate the shown URL of the next column by highlighting the shown folder item
350 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
351 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
352 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
353 const QRect itemRect
= visualRect(proxyIndex
);
354 QPainter
painter(viewport());
355 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
357 painter
.setPen(Qt::NoPen
);
358 painter
.setBrush(color
);
359 painter
.drawRect(itemRect
);
363 QListView::paintEvent(event
);
366 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
369 if (!indexAt(event
->pos()).isValid()) {
370 if (QApplication::mouseButtons() & Qt::MidButton
) {
371 m_view
->m_controller
->replaceUrlByClipboard();
373 } else if (event
->button() == Qt::LeftButton
) {
374 // TODO: see comment in DolphinIconsView::mousePressEvent()
375 setState(QAbstractItemView::DraggingState
);
377 QListView::mousePressEvent(event
);
380 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
382 QListView::keyPressEvent(event
);
384 m_view
->m_controller
->handleKeyPressEvent(event
);
387 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
390 m_view
->requestActivation(this);
391 Q_ASSERT(m_view
->m_controller
->itemView() == this);
392 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
396 QListView::contextMenuEvent(event
);
398 const QModelIndex index
= indexAt(event
->pos());
399 if (!index
.isValid()) {
403 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
404 Q_ASSERT(m_view
->m_controller
->itemView() == this);
405 m_view
->m_controller
->triggerContextMenuRequest(pos
);
408 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
410 if (m_selectionManager
!= 0) {
411 m_selectionManager
->reset();
414 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
415 if (event
->modifiers() & Qt::ControlModifier
) {
420 QListView::wheelEvent(event
);
423 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
425 QListView::leaveEvent(event
);
426 // if the mouse is above an item and moved very fast outside the widget,
427 // no viewportEntered() signal might be emitted although the mouse has been moved
428 // above the viewport
429 m_view
->m_controller
->emitViewportEntered();
432 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
434 QListView::selectionChanged(selected
, deselected
);
436 QItemSelectionModel
* selModel
= m_view
->selectionModel();
437 selModel
->select(selected
, QItemSelectionModel::Select
);
438 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
441 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
443 m_view
->m_controller
->setItemView(this);
444 m_view
->m_controller
->emitItemEntered(index
);
447 void DolphinColumnWidget::requestActivation()
449 m_view
->m_controller
->setItemView(this);
450 m_view
->m_controller
->requestActivation();
452 m_view
->requestActivation(this);
453 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
454 selectionModel()->clear();
458 void DolphinColumnWidget::updateFont()
460 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
461 Q_ASSERT(settings
!= 0);
463 if (settings
->useSystemFont()) {
464 m_font
= KGlobalSettings::generalFont();
468 void DolphinColumnWidget::activate()
470 setFocus(Qt::OtherFocusReason
);
472 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
473 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
474 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
475 if (KGlobalSettings::singleClick()) {
476 connect(this, SIGNAL(clicked(const QModelIndex
&)),
477 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
479 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
480 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
483 if (selectionModel() && selectionModel()->currentIndex().isValid())
484 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
489 void DolphinColumnWidget::deactivate()
493 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
494 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
495 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
496 if (KGlobalSettings::singleClick()) {
497 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
498 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
500 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
501 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
504 const QModelIndex current
= selectionModel()->currentIndex();
505 selectionModel()->clear();
506 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
510 #include "dolphincolumnwidget.moc"