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"
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kfileitem.h>
36 #include <kio/previewjob.h>
37 #include <kiconeffect.h>
39 #include <kmimetyperesolver.h>
40 #include <konqmimedata.h>
42 #include "iconmanager.h"
44 #include <QApplication>
49 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
50 DolphinColumnView
* columnView
,
66 setMouseTracking(true);
67 viewport()->setAttribute(Qt::WA_Hover
);
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);
76 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
77 // check avoids a division by zero happening on versions before 4.3.1.
78 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
80 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
81 setVerticalScrollMode(QListView::ScrollPerPixel
);
82 setHorizontalScrollMode(QListView::ScrollPerPixel
);
85 // apply the column mode settings to the widget
86 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
87 Q_ASSERT(settings
!= 0);
89 if (settings
->useSystemFont()) {
90 m_font
= KGlobalSettings::generalFont();
92 m_font
= QFont(settings
->fontFamily(),
94 settings
->fontWeight(),
95 settings
->italicFont());
98 const int iconSize
= settings
->iconSize();
99 setDecorationSize(QSize(iconSize
, iconSize
));
101 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
102 setItemDelegate(delegate
);
106 connect(this, SIGNAL(viewportEntered()),
107 m_view
->m_controller
, SLOT(emitViewportEntered()));
108 connect(this, SIGNAL(entered(const QModelIndex
&)),
109 this, SLOT(slotEntered(const QModelIndex
&)));
111 //m_dirLister = new DolphinDirLister(); TODO
112 m_dirLister
= new KDirLister();
113 m_dirLister
->setAutoUpdate(true);
114 m_dirLister
->setMainWindow(window());
115 m_dirLister
->setDelayedMimeTypes(true);
116 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
117 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
119 m_dolphinModel
= new DolphinModel(this);
120 m_dolphinModel
->setDirLister(m_dirLister
);
121 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
123 m_proxyModel
= new DolphinSortFilterProxyModel(this);
124 m_proxyModel
->setSourceModel(m_dolphinModel
);
125 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
126 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
127 m_proxyModel
->setSorting(dolphinView
->sorting());
128 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
130 setModel(m_proxyModel
);
131 const bool useSelManager
= KGlobalSettings::singleClick() &&
132 DolphinSettings::instance().generalSettings()->showSelectionToggle();
134 SelectionManager
* selManager
= new SelectionManager(this);
135 connect(selManager
, SIGNAL(selectionChanged()),
136 this, SLOT(requestActivation()));
137 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
138 selManager
, SLOT(reset()));
141 new KMimeTypeResolver(this, m_dolphinModel
);
142 m_iconManager
= new IconManager(this, m_proxyModel
);
143 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
145 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
147 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
148 this, SLOT(updateFont()));
151 DolphinColumnWidget::~DolphinColumnWidget()
155 delete m_dolphinModel
;
157 m_dirLister
= 0; // deleted by m_dolphinModel
160 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
163 m_decorationSize
= size
;
167 void DolphinColumnWidget::setActive(bool active
)
169 if (m_active
== active
) {
182 void DolphinColumnWidget::reload()
185 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
188 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
190 m_proxyModel
->setSorting(sorting
);
193 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
195 m_proxyModel
->setSortOrder(order
);
198 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
200 if (show
!= m_dirLister
->showingDotFiles()) {
201 m_dirLister
->setShowingDotFiles(show
);
203 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
207 void DolphinColumnWidget::setShowPreview(bool show
)
209 m_iconManager
->setShowPreview(show
);
212 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
215 void DolphinColumnWidget::updateBackground()
217 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
218 // cleaning up the cut-indication of DolphinColumnWidget with the code from
219 // DolphinView a common helper-class should be available which can be shared
220 // by all view implementations -> no hardcoded value anymore
221 const QPalette::ColorRole role
= viewport()->backgroundRole();
222 QColor color
= viewport()->palette().color(role
);
223 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
225 QPalette palette
= viewport()->palette();
226 palette
.setColor(role
, color
);
227 viewport()->setPalette(palette
);
232 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
234 m_proxyModel
->setFilterRegExp(nameFilter
);
238 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
240 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
241 viewOptions
.font
= m_font
;
242 viewOptions
.decorationSize
= m_decorationSize
;
243 viewOptions
.showDecorationSelected
= true;
247 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
249 DragAndDropHelper::startDrag(this, supportedActions
);
252 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
254 if (event
->mimeData()->hasUrls()) {
255 event
->acceptProposedAction();
261 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
263 QListView::dragLeaveEvent(event
);
265 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
267 setDirtyRegion(m_dropRect
);
270 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
272 QListView::dragMoveEvent(event
);
274 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
275 const QModelIndex index
= indexAt(event
->pos());
276 setDirtyRegion(m_dropRect
);
278 m_dropRect
.setSize(QSize()); // set as invalid
279 if (index
.isValid()) {
280 m_view
->m_controller
->setItemView(this);
281 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
282 if (!item
.isNull() && item
.isDir()) {
283 m_dropRect
= visualRect(index
);
286 setDirtyRegion(m_dropRect
);
288 if (event
->mimeData()->hasUrls()) {
289 // accept url drops, independently from the destination item
290 event
->acceptProposedAction();
294 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
296 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
297 if (!urls
.isEmpty()) {
298 const QModelIndex index
= indexAt(event
->pos());
299 m_view
->m_controller
->setItemView(this);
300 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
301 m_view
->m_controller
->indicateDroppedUrls(urls
,
304 event
->acceptProposedAction();
306 QListView::dropEvent(event
);
310 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
312 if (!m_childUrl
.isEmpty()) {
313 // indicate the shown URL of the next column by highlighting the shown folder item
314 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
315 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
316 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
317 const QRect itemRect
= visualRect(proxyIndex
);
318 QPainter
painter(viewport());
321 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
323 painter
.setPen(Qt::NoPen
);
324 painter
.setBrush(color
);
325 painter
.drawRect(itemRect
);
331 QListView::paintEvent(event
);
333 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
335 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
336 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
340 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
343 QListView::mousePressEvent(event
);
346 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
348 QListView::keyPressEvent(event
);
350 m_view
->m_controller
->handleKeyPressEvent(event
);
353 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
356 m_view
->requestActivation(this);
357 Q_ASSERT(m_view
->m_controller
->itemView() == this);
358 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
361 QListView::contextMenuEvent(event
);
363 const QModelIndex index
= indexAt(event
->pos());
364 if (index
.isValid() || m_active
) {
365 // Only open a context menu above an item or if the mouse is above
366 // the active column.
367 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
368 Q_ASSERT(m_view
->m_controller
->itemView() == this);
369 m_view
->m_controller
->triggerContextMenuRequest(pos
);
373 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
375 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
376 // (installing an event filter does not work, as the wheel event is handled first)
377 if (event
->modifiers() & Qt::ControlModifier
) {
381 QListView::wheelEvent(event
);
384 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
386 QListView::selectionChanged(selected
, deselected
);
388 QItemSelectionModel
* selModel
= m_view
->selectionModel();
389 selModel
->select(selected
, QItemSelectionModel::Select
);
390 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
393 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
395 m_view
->m_controller
->setItemView(this);
396 m_view
->m_controller
->emitItemEntered(index
);
399 void DolphinColumnWidget::requestActivation()
401 m_view
->m_controller
->requestActivation();
403 m_view
->requestActivation(this);
404 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
405 selectionModel()->clear();
407 Q_ASSERT(m_view
->m_controller
->itemView() == this);
410 void DolphinColumnWidget::updateFont()
412 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
413 Q_ASSERT(settings
!= 0);
415 if (settings
->useSystemFont()) {
416 m_font
= KGlobalSettings::generalFont();
420 void DolphinColumnWidget::activate()
422 setFocus(Qt::OtherFocusReason
);
424 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
425 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
426 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
427 if (KGlobalSettings::singleClick()) {
428 connect(this, SIGNAL(clicked(const QModelIndex
&)),
429 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
431 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
432 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
435 if (selectionModel() && selectionModel()->currentIndex().isValid())
436 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
441 void DolphinColumnWidget::deactivate()
445 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
446 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
447 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
448 if (KGlobalSettings::singleClick()) {
449 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
450 this, SLOT(triggerItem(const QModelIndex
&)));
452 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
453 this, SLOT(triggerItem(const QModelIndex
&)));
456 const QModelIndex current
= selectionModel()->currentIndex();
457 selectionModel()->clear();
458 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
462 #include "dolphincolumnwidget.moc"