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 "draganddrophelper.h"
31 #include <kcolorscheme.h>
32 #include <kdirlister.h>
33 #include <kfileitem.h>
34 #include <kio/previewjob.h>
35 #include <kiconeffect.h>
37 #include <kmimetyperesolver.h>
38 #include <konqmimedata.h>
40 #include <QApplication>
45 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
46 DolphinColumnView
* columnView
,
63 setMouseTracking(true);
64 viewport()->setAttribute(Qt::WA_Hover
);
65 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
66 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
67 setSelectionBehavior(SelectItems
);
68 setSelectionMode(QAbstractItemView::ExtendedSelection
);
69 setDragDropMode(QAbstractItemView::DragDrop
);
70 setDropIndicatorShown(false);
71 setSelectionRectVisible(true);
73 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
74 // check avoids a division by zero happening on versions before 4.3.1.
75 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
77 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
78 setVerticalScrollMode(QListView::ScrollPerPixel
);
79 setHorizontalScrollMode(QListView::ScrollPerPixel
);
82 // apply the column mode settings to the widget
83 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
84 Q_ASSERT(settings
!= 0);
86 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
87 m_font
.setItalic(settings
->italicFont());
88 m_font
.setBold(settings
->boldFont());
90 const int iconSize
= settings
->iconSize();
91 m_decorationSize
= QSize(iconSize
, iconSize
);
93 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
94 setItemDelegate(delegate
);
98 connect(this, SIGNAL(viewportEntered()),
99 m_view
->m_controller
, SLOT(emitViewportEntered()));
100 connect(this, SIGNAL(entered(const QModelIndex
&)),
101 this, SLOT(slotEntered(const QModelIndex
&)));
103 //m_dirLister = new DolphinDirLister(); TODO
104 m_dirLister
= new KDirLister();
105 m_dirLister
->setAutoUpdate(true);
106 m_dirLister
->setMainWindow(this);
107 m_dirLister
->setDelayedMimeTypes(true);
108 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
109 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
110 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
111 this, SLOT(generatePreviews(const KFileItemList
&)));
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
);
121 setModel(m_proxyModel
);
122 new KMimeTypeResolver(this, m_dolphinModel
);
124 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
127 DolphinColumnWidget::~DolphinColumnWidget()
131 delete m_dolphinModel
;
133 m_dirLister
= 0; // deleted by m_dolphinModel
135 if (m_previewJob
!= 0) {
136 m_previewJob
->kill();
141 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
143 m_decorationSize
= size
;
147 void DolphinColumnWidget::setActive(bool active
)
149 if (m_active
== active
) {
162 void DolphinColumnWidget::reload()
165 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
168 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
170 if (show
!= m_dirLister
->showingDotFiles()) {
171 m_dirLister
->setShowingDotFiles(show
);
173 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
177 void DolphinColumnWidget::setShowPreview(bool show
)
179 if (show
!= m_showPreview
) {
181 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
185 void DolphinColumnWidget::updateBackground()
187 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
188 if (!m_active
|| !m_view
->m_active
) {
191 QPalette palette
= viewport()->palette();
192 palette
.setColor(viewport()->backgroundRole(), color
);
193 viewport()->setPalette(palette
);
198 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
200 m_proxyModel
->setFilterRegExp(nameFilter
);
204 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
206 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
207 viewOptions
.font
= m_font
;
208 viewOptions
.decorationSize
= m_decorationSize
;
209 viewOptions
.showDecorationSelected
= true;
213 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
215 DragAndDropHelper::startDrag(this, supportedActions
);
218 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
220 if (event
->mimeData()->hasUrls()) {
221 event
->acceptProposedAction();
227 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
229 QListView::dragLeaveEvent(event
);
231 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
233 setDirtyRegion(m_dropRect
);
236 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
238 QListView::dragMoveEvent(event
);
240 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
241 const QModelIndex index
= indexAt(event
->pos());
242 setDirtyRegion(m_dropRect
);
244 m_dropRect
.setSize(QSize()); // set as invalid
245 if (index
.isValid()) {
246 const KFileItem item
= itemForIndex(index
);
247 if (!item
.isNull() && item
.isDir()) {
248 m_dropRect
= visualRect(index
);
251 setDirtyRegion(m_dropRect
);
253 if (event
->mimeData()->hasUrls()) {
254 // accept url drops, independently from the destination item
255 event
->acceptProposedAction();
259 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
261 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
262 if (!urls
.isEmpty()) {
263 const QModelIndex index
= indexAt(event
->pos());
264 const KFileItem item
= itemForIndex(index
);
265 m_view
->m_controller
->indicateDroppedUrls(urls
,
268 event
->acceptProposedAction();
270 QListView::dropEvent(event
);
274 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
276 if (!m_childUrl
.isEmpty()) {
277 // indicate the shown URL of the next column by highlighting the shown folder item
278 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
279 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
280 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
281 const QRect itemRect
= visualRect(proxyIndex
);
282 QPainter
painter(viewport());
285 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
287 painter
.setPen(Qt::NoPen
);
288 painter
.setBrush(color
);
289 painter
.drawRect(itemRect
);
295 QListView::paintEvent(event
);
297 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
299 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
300 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
304 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
306 m_view
->m_controller
->requestActivation();
308 m_view
->requestActivation(this);
309 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
312 QListView::mousePressEvent(event
);
315 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
317 QListView::keyPressEvent(event
);
319 const QItemSelectionModel
* selModel
= selectionModel();
320 const QModelIndex currentIndex
= selModel
->currentIndex();
321 const bool trigger
= currentIndex
.isValid()
322 && (event
->key() == Qt::Key_Return
)
323 && (selModel
->selectedIndexes().count() <= 1);
325 triggerItem(currentIndex
);
329 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
332 m_view
->requestActivation(this);
333 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
336 QListView::contextMenuEvent(event
);
338 const QModelIndex index
= indexAt(event
->pos());
339 if (index
.isValid() || m_active
) {
340 // Only open a context menu above an item or if the mouse is above
341 // the active column.
342 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
343 m_view
->m_controller
->triggerContextMenuRequest(pos
);
347 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
349 QListView::selectionChanged(selected
, deselected
);
351 QItemSelectionModel
* selModel
= m_view
->selectionModel();
352 selModel
->select(selected
, QItemSelectionModel::Select
);
353 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
356 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
358 const KFileItem item
= itemForIndex(index
);
359 m_view
->m_controller
->triggerItem(item
);
362 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
364 // TODO: same implementation as in DolphinView; create helper class
365 // for generatePreviews(), showPreview() and isCutItem()
367 if (m_view
->m_controller
->dolphinView()->showPreview()) {
368 if (m_previewJob
!= 0) {
369 m_previewJob
->kill();
373 m_previewJob
= KIO::filePreview(items
, 128);
374 connect(m_previewJob
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
375 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
376 connect(m_previewJob
, SIGNAL(finished(KJob
*)),
377 this, SLOT(slotPreviewJobFinished(KJob
*)));
381 void DolphinColumnWidget::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
383 // TODO: same implementation as in DolphinView; create helper class
384 // for generatePreviews(), showPreview() and isCutItem()
386 Q_ASSERT(!item
.isNull());
387 const bool showPreview
= m_view
->m_controller
->dolphinView()->showPreview();
388 if (!showPreview
|| (item
.url().directory() != m_dirLister
->url().path())) {
389 // the preview job is still working on items of an older URL, hence
390 // the item is not part of the directory model anymore
394 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
395 if (idx
.isValid() && (idx
.column() == 0)) {
396 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
397 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
398 KIconEffect iconEffect
;
399 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
400 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
402 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
407 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
409 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
410 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
411 m_view
->m_controller
->emitItemEntered(item
);
414 void DolphinColumnWidget::slotPreviewJobFinished(KJob
* job
)
416 Q_ASSERT(job
== m_previewJob
);
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 this, SLOT(triggerItem(const QModelIndex
&)));
431 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
432 this, SLOT(triggerItem(const QModelIndex
&)));
435 if (!m_childUrl
.isEmpty()) {
436 // assure that the current index is set on the index that represents
438 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
439 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
440 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
446 void DolphinColumnWidget::deactivate()
450 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
451 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
452 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
453 if (KGlobalSettings::singleClick()) {
454 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
455 this, SLOT(triggerItem(const QModelIndex
&)));
457 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
458 this, SLOT(triggerItem(const QModelIndex
&)));
461 selectionModel()->clear();
465 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
467 // TODO: same implementation as in DolphinView; create helper class
468 // for generatePreviews(), showPreview() and isCutItem()
470 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
471 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
473 const KUrl
& itemUrl
= item
.url();
474 KUrl::List::const_iterator it
= cutUrls
.begin();
475 const KUrl::List::const_iterator end
= cutUrls
.end();
477 if (*it
== itemUrl
) {
486 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
488 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
489 return m_dolphinModel
->itemForIndex(dirIndex
);
493 #include "dolphincolumnwidget.moc"