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 "selectionmanager.h"
32 #include "tooltipmanager.h"
34 #include <kcolorscheme.h>
35 #include <kdirlister.h>
36 #include <kfileitem.h>
37 #include <kio/previewjob.h>
38 #include <kiconeffect.h>
40 #include <konqmimedata.h>
42 #include "iconmanager.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
);
125 const bool useSelManager
= KGlobalSettings::singleClick() &&
126 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_iconManager
= new IconManager(this, m_proxyModel
);
136 m_iconManager
->setShowPreview(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()));
148 DolphinColumnWidget::~DolphinColumnWidget()
152 delete m_dolphinModel
;
154 m_dirLister
= 0; // deleted by m_dolphinModel
157 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
160 m_decorationSize
= size
;
162 if (m_iconManager
!= 0) {
163 m_iconManager
->updatePreviews();
165 if (m_selectionManager
!= 0) {
166 m_selectionManager
->reset();
170 void DolphinColumnWidget::setActive(bool active
)
172 if (m_active
== active
) {
185 void DolphinColumnWidget::reload()
188 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
191 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
193 m_proxyModel
->setSorting(sorting
);
196 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
198 m_proxyModel
->setSortOrder(order
);
201 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
203 if (show
!= m_dirLister
->showingDotFiles()) {
204 m_dirLister
->setShowingDotFiles(show
);
206 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
210 void DolphinColumnWidget::setShowPreview(bool show
)
212 m_iconManager
->setShowPreview(show
);
215 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
218 void DolphinColumnWidget::updateBackground()
220 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
221 // cleaning up the cut-indication of DolphinColumnWidget with the code from
222 // DolphinView a common helper-class should be available which can be shared
223 // by all view implementations -> no hardcoded value anymore
224 const QPalette::ColorRole role
= viewport()->backgroundRole();
225 QColor color
= viewport()->palette().color(role
);
226 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
228 QPalette palette
= viewport()->palette();
229 palette
.setColor(role
, color
);
230 viewport()->setPalette(palette
);
235 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
237 m_proxyModel
->setFilterRegExp(nameFilter
);
240 void DolphinColumnWidget::editItem(const KFileItem
& item
)
242 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
243 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
244 if (proxyIndex
.isValid()) {
249 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
251 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
252 viewOptions
.font
= m_font
;
253 viewOptions
.decorationSize
= m_decorationSize
;
254 viewOptions
.showDecorationSelected
= true;
258 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
260 DragAndDropHelper::startDrag(this, supportedActions
);
263 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
265 if (event
->mimeData()->hasUrls()) {
266 event
->acceptProposedAction();
270 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
272 QListView::dragLeaveEvent(event
);
273 setDirtyRegion(m_dropRect
);
276 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
278 QListView::dragMoveEvent(event
);
280 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
281 const QModelIndex index
= indexAt(event
->pos());
282 setDirtyRegion(m_dropRect
);
284 m_dropRect
.setSize(QSize()); // set as invalid
285 if (index
.isValid()) {
286 m_view
->m_controller
->setItemView(this);
287 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
288 if (!item
.isNull() && item
.isDir()) {
289 m_dropRect
= visualRect(index
);
292 setDirtyRegion(m_dropRect
);
294 if (event
->mimeData()->hasUrls()) {
295 // accept url drops, independently from the destination item
296 event
->acceptProposedAction();
300 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
302 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
303 if (!urls
.isEmpty()) {
304 const QModelIndex index
= indexAt(event
->pos());
305 m_view
->m_controller
->setItemView(this);
306 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
307 m_view
->m_controller
->indicateDroppedUrls(urls
,
310 event
->acceptProposedAction();
312 QListView::dropEvent(event
);
315 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
317 if (!m_childUrl
.isEmpty()) {
318 // indicate the shown URL of the next column by highlighting the shown folder item
319 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
320 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
321 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
322 const QRect itemRect
= visualRect(proxyIndex
);
323 QPainter
painter(viewport());
324 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
326 painter
.setPen(Qt::NoPen
);
327 painter
.setBrush(color
);
328 painter
.drawRect(itemRect
);
332 QListView::paintEvent(event
);
335 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
338 if (!indexAt(event
->pos()).isValid()) {
339 if (QApplication::mouseButtons() & Qt::MidButton
) {
340 m_view
->m_controller
->replaceUrlByClipboard();
342 } else if (event
->button() == Qt::LeftButton
) {
343 // TODO: see comment in DolphinIconsView::mousePressEvent()
344 setState(QAbstractItemView::DraggingState
);
346 QListView::mousePressEvent(event
);
349 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
351 QListView::keyPressEvent(event
);
353 m_view
->m_controller
->handleKeyPressEvent(event
);
356 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
359 m_view
->requestActivation(this);
360 Q_ASSERT(m_view
->m_controller
->itemView() == this);
361 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
364 QListView::contextMenuEvent(event
);
366 const QModelIndex index
= indexAt(event
->pos());
367 if (index
.isValid() || m_active
) {
368 // Only open a context menu above an item or if the mouse is above
369 // the active column.
370 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
371 Q_ASSERT(m_view
->m_controller
->itemView() == this);
372 m_view
->m_controller
->triggerContextMenuRequest(pos
);
376 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
378 if (m_selectionManager
!= 0) {
379 m_selectionManager
->reset();
382 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
383 if (event
->modifiers() & Qt::ControlModifier
) {
388 QListView::wheelEvent(event
);
391 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
393 QListView::leaveEvent(event
);
394 // if the mouse is above an item and moved very fast outside the widget,
395 // no viewportEntered() signal might be emitted although the mouse has been moved
396 // above the viewport
397 m_view
->m_controller
->emitViewportEntered();
400 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
402 QListView::selectionChanged(selected
, deselected
);
404 QItemSelectionModel
* selModel
= m_view
->selectionModel();
405 selModel
->select(selected
, QItemSelectionModel::Select
);
406 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
409 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
411 m_view
->m_controller
->setItemView(this);
412 m_view
->m_controller
->emitItemEntered(index
);
415 void DolphinColumnWidget::requestActivation()
417 m_view
->m_controller
->setItemView(this);
418 m_view
->m_controller
->requestActivation();
420 m_view
->requestActivation(this);
421 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
422 selectionModel()->clear();
426 void DolphinColumnWidget::updateFont()
428 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
429 Q_ASSERT(settings
!= 0);
431 if (settings
->useSystemFont()) {
432 m_font
= KGlobalSettings::generalFont();
436 void DolphinColumnWidget::activate()
438 setFocus(Qt::OtherFocusReason
);
440 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
441 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
442 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
443 if (KGlobalSettings::singleClick()) {
444 connect(this, SIGNAL(clicked(const QModelIndex
&)),
445 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
447 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
448 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
451 if (selectionModel() && selectionModel()->currentIndex().isValid())
452 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
457 void DolphinColumnWidget::deactivate()
461 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
462 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
463 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
464 if (KGlobalSettings::singleClick()) {
465 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
466 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
468 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
469 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
472 const QModelIndex current
= selectionModel()->currentIndex();
473 selectionModel()->clear();
474 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
478 #include "dolphincolumnwidget.moc"