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 <kmimetyperesolver.h>
41 #include <konqmimedata.h>
43 #include "iconmanager.h"
45 #include <QApplication>
50 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
51 DolphinColumnView
* columnView
,
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 SelectionManager
* selManager
= new SelectionManager(this);
129 connect(selManager
, SIGNAL(selectionChanged()),
130 this, SLOT(requestActivation()));
131 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
132 selManager
, SLOT(reset()));
135 new KMimeTypeResolver(this, m_dolphinModel
);
136 m_iconManager
= new IconManager(this, m_proxyModel
);
137 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
139 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
140 new ToolTipManager(this, m_proxyModel
);
143 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
145 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
146 this, SLOT(updateFont()));
149 DolphinColumnWidget::~DolphinColumnWidget()
153 delete m_dolphinModel
;
155 m_dirLister
= 0; // deleted by m_dolphinModel
158 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
161 m_decorationSize
= size
;
163 if (m_iconManager
!= 0) {
164 m_iconManager
->updatePreviews();
168 void DolphinColumnWidget::setActive(bool active
)
170 if (m_active
== active
) {
183 void DolphinColumnWidget::reload()
186 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
189 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
191 m_proxyModel
->setSorting(sorting
);
194 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
196 m_proxyModel
->setSortOrder(order
);
199 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
201 if (show
!= m_dirLister
->showingDotFiles()) {
202 m_dirLister
->setShowingDotFiles(show
);
204 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
208 void DolphinColumnWidget::setShowPreview(bool show
)
210 m_iconManager
->setShowPreview(show
);
213 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
216 void DolphinColumnWidget::updateBackground()
218 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
219 // cleaning up the cut-indication of DolphinColumnWidget with the code from
220 // DolphinView a common helper-class should be available which can be shared
221 // by all view implementations -> no hardcoded value anymore
222 const QPalette::ColorRole role
= viewport()->backgroundRole();
223 QColor color
= viewport()->palette().color(role
);
224 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
226 QPalette palette
= viewport()->palette();
227 palette
.setColor(role
, color
);
228 viewport()->setPalette(palette
);
233 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
235 m_proxyModel
->setFilterRegExp(nameFilter
);
238 void DolphinColumnWidget::editItem(const KFileItem
& item
)
240 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
241 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
242 if (proxyIndex
.isValid()) {
247 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
249 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
250 viewOptions
.font
= m_font
;
251 viewOptions
.decorationSize
= m_decorationSize
;
252 viewOptions
.showDecorationSelected
= true;
256 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
258 DragAndDropHelper::startDrag(this, supportedActions
);
261 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
263 if (event
->mimeData()->hasUrls()) {
264 event
->acceptProposedAction();
268 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
270 QListView::dragLeaveEvent(event
);
271 setDirtyRegion(m_dropRect
);
274 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
276 QListView::dragMoveEvent(event
);
278 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
279 const QModelIndex index
= indexAt(event
->pos());
280 setDirtyRegion(m_dropRect
);
282 m_dropRect
.setSize(QSize()); // set as invalid
283 if (index
.isValid()) {
284 m_view
->m_controller
->setItemView(this);
285 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
286 if (!item
.isNull() && item
.isDir()) {
287 m_dropRect
= visualRect(index
);
290 setDirtyRegion(m_dropRect
);
292 if (event
->mimeData()->hasUrls()) {
293 // accept url drops, independently from the destination item
294 event
->acceptProposedAction();
298 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
300 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
301 if (!urls
.isEmpty()) {
302 const QModelIndex index
= indexAt(event
->pos());
303 m_view
->m_controller
->setItemView(this);
304 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
305 m_view
->m_controller
->indicateDroppedUrls(urls
,
308 event
->acceptProposedAction();
310 QListView::dropEvent(event
);
313 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
315 if (!m_childUrl
.isEmpty()) {
316 // indicate the shown URL of the next column by highlighting the shown folder item
317 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
318 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
319 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
320 const QRect itemRect
= visualRect(proxyIndex
);
321 QPainter
painter(viewport());
322 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
324 painter
.setPen(Qt::NoPen
);
325 painter
.setBrush(color
);
326 painter
.drawRect(itemRect
);
330 QListView::paintEvent(event
);
333 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
336 QListView::mousePressEvent(event
);
339 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
341 QListView::keyPressEvent(event
);
343 m_view
->m_controller
->handleKeyPressEvent(event
);
346 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
349 m_view
->requestActivation(this);
350 Q_ASSERT(m_view
->m_controller
->itemView() == this);
351 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
354 QListView::contextMenuEvent(event
);
356 const QModelIndex index
= indexAt(event
->pos());
357 if (index
.isValid() || m_active
) {
358 // Only open a context menu above an item or if the mouse is above
359 // the active column.
360 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
361 Q_ASSERT(m_view
->m_controller
->itemView() == this);
362 m_view
->m_controller
->triggerContextMenuRequest(pos
);
366 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
368 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
369 if (event
->modifiers() & Qt::ControlModifier
) {
373 QListView::wheelEvent(event
);
376 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
378 QListView::leaveEvent(event
);
379 // if the mouse is above an item and moved very fast outside the widget,
380 // no viewportEntered() signal might be emitted although the mouse has been moved
381 // above the viewport
382 m_view
->m_controller
->emitViewportEntered();
385 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
387 QListView::selectionChanged(selected
, deselected
);
389 QItemSelectionModel
* selModel
= m_view
->selectionModel();
390 selModel
->select(selected
, QItemSelectionModel::Select
);
391 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
394 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
396 m_view
->m_controller
->setItemView(this);
397 m_view
->m_controller
->emitItemEntered(index
);
400 void DolphinColumnWidget::requestActivation()
402 m_view
->m_controller
->setItemView(this);
403 m_view
->m_controller
->requestActivation();
405 m_view
->requestActivation(this);
406 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
407 selectionModel()->clear();
411 void DolphinColumnWidget::updateFont()
413 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
414 Q_ASSERT(settings
!= 0);
416 if (settings
->useSystemFont()) {
417 m_font
= KGlobalSettings::generalFont();
421 void DolphinColumnWidget::activate()
423 setFocus(Qt::OtherFocusReason
);
425 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
426 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
427 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
428 if (KGlobalSettings::singleClick()) {
429 connect(this, SIGNAL(clicked(const QModelIndex
&)),
430 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
432 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
433 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
436 if (selectionModel() && selectionModel()->currentIndex().isValid())
437 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
442 void DolphinColumnWidget::deactivate()
446 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
447 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
448 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
449 if (KGlobalSettings::singleClick()) {
450 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
451 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
453 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
454 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
457 const QModelIndex current
= selectionModel()->currentIndex();
458 selectionModel()->clear();
459 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
463 #include "dolphincolumnwidget.moc"