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 "dolphinmodel.h"
27 #include "dolphinsortfilterproxymodel.h"
28 #include "dolphinsettings.h"
29 #include "dolphin_columnmodesettings.h"
30 #include "draganddrophelper.h"
32 #include <kcolorutils.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 <QAbstractProxyModel>
43 #include <QApplication>
50 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
51 DolphinColumnView
* columnView
,
68 setMouseTracking(true);
69 viewport()->setAttribute(Qt::WA_Hover
);
70 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
71 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
72 setSelectionBehavior(SelectItems
);
73 setSelectionMode(QAbstractItemView::ExtendedSelection
);
74 setDragDropMode(QAbstractItemView::DragDrop
);
75 setDropIndicatorShown(false);
77 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
78 // check avoids a division by zero happening on versions before 4.3.1.
79 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
81 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
82 setVerticalScrollMode(QListView::ScrollPerPixel
);
83 setHorizontalScrollMode(QListView::ScrollPerPixel
);
86 // apply the column mode settings to the widget
87 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
88 Q_ASSERT(settings
!= 0);
90 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
91 m_font
.setItalic(settings
->italicFont());
92 m_font
.setBold(settings
->boldFont());
94 const int iconSize
= settings
->iconSize();
95 m_decorationSize
= QSize(iconSize
, iconSize
);
97 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
98 setItemDelegate(delegate
);
102 connect(this, SIGNAL(viewportEntered()),
103 m_view
->m_controller
, SLOT(emitViewportEntered()));
104 connect(this, SIGNAL(entered(const QModelIndex
&)),
105 this, SLOT(slotEntered(const QModelIndex
&)));
107 //m_dirLister = new DolphinDirLister(); TODO
108 m_dirLister
= new KDirLister();
109 m_dirLister
->setAutoUpdate(true);
110 m_dirLister
->setMainWindow(this);
111 m_dirLister
->setDelayedMimeTypes(true);
112 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
113 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
114 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
115 this, SLOT(generatePreviews(const KFileItemList
&)));
117 m_dolphinModel
= new DolphinModel(this);
118 m_dolphinModel
->setDirLister(m_dirLister
);
119 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
121 m_proxyModel
= new DolphinSortFilterProxyModel(this);
122 m_proxyModel
->setSourceModel(m_dolphinModel
);
124 setModel(m_proxyModel
);
125 new KMimeTypeResolver(this, m_dolphinModel
);
127 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
130 DolphinColumnWidget::~DolphinColumnWidget()
134 delete m_dolphinModel
;
136 m_dirLister
= 0; // deleted by m_dolphinModel
138 if (m_previewJob
!= 0) {
139 m_previewJob
->kill();
144 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
146 m_decorationSize
= size
;
150 void DolphinColumnWidget::setActive(bool active
)
152 if (m_active
== active
) {
165 void DolphinColumnWidget::reload()
168 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
171 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
173 if (show
!= m_dirLister
->showingDotFiles()) {
174 m_dirLister
->setShowingDotFiles(show
);
176 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
180 void DolphinColumnWidget::setShowPreview(bool show
)
182 if (show
!= m_showPreview
) {
184 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
188 void DolphinColumnWidget::updateBackground()
190 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
191 if (!m_active
|| !m_view
->m_active
) {
194 QPalette palette
= viewport()->palette();
195 palette
.setColor(viewport()->backgroundRole(), color
);
196 viewport()->setPalette(palette
);
201 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
203 m_proxyModel
->setFilterRegExp(nameFilter
);
207 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
209 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
210 viewOptions
.font
= m_font
;
211 viewOptions
.decorationSize
= m_decorationSize
;
212 viewOptions
.showDecorationSelected
= true;
216 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
218 DragAndDropHelper::startDrag(this, supportedActions
);
221 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
223 if (event
->mimeData()->hasUrls()) {
224 event
->acceptProposedAction();
230 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
232 QListView::dragLeaveEvent(event
);
234 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
236 setDirtyRegion(m_dropRect
);
239 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
241 QListView::dragMoveEvent(event
);
243 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
244 const QModelIndex index
= indexAt(event
->pos());
245 setDirtyRegion(m_dropRect
);
247 m_dropRect
.setSize(QSize()); // set as invalid
248 if (index
.isValid()) {
249 const KFileItem item
= itemForIndex(index
);
250 if (!item
.isNull() && item
.isDir()) {
251 m_dropRect
= visualRect(index
);
254 setDirtyRegion(m_dropRect
);
257 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
259 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
260 if (!urls
.isEmpty()) {
261 const QModelIndex index
= indexAt(event
->pos());
262 const KFileItem item
= itemForIndex(index
);
263 m_view
->m_controller
->indicateDroppedUrls(urls
,
266 event
->acceptProposedAction();
268 QListView::dropEvent(event
);
272 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
274 if (!m_childUrl
.isEmpty()) {
275 // indicate the shown URL of the next column by highlighting the shown folder item
276 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
277 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
278 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
279 const QRect itemRect
= visualRect(proxyIndex
);
280 QPainter
painter(viewport());
283 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
285 painter
.setPen(Qt::NoPen
);
286 painter
.setBrush(color
);
287 painter
.drawRect(itemRect
);
293 QListView::paintEvent(event
);
295 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
297 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
298 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect
, brush
);
302 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
304 m_view
->m_controller
->requestActivation();
306 m_view
->requestActivation(this);
307 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
310 QListView::mousePressEvent(event
);
313 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
315 QListView::keyPressEvent(event
);
317 const QItemSelectionModel
* selModel
= selectionModel();
318 const QModelIndex currentIndex
= selModel
->currentIndex();
319 const bool trigger
= currentIndex
.isValid()
320 && (event
->key() == Qt::Key_Return
)
321 && (selModel
->selectedIndexes().count() <= 1);
323 triggerItem(currentIndex
);
327 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
330 m_view
->requestActivation(this);
331 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
334 QListView::contextMenuEvent(event
);
336 const QModelIndex index
= indexAt(event
->pos());
337 if (index
.isValid() || m_active
) {
338 // Only open a context menu above an item or if the mouse is above
339 // the active column.
340 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
341 m_view
->m_controller
->triggerContextMenuRequest(pos
);
345 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
347 QListView::selectionChanged(selected
, deselected
);
349 QItemSelectionModel
* selModel
= m_view
->selectionModel();
350 selModel
->select(selected
, QItemSelectionModel::Select
);
351 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
354 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
356 const KFileItem item
= itemForIndex(index
);
357 m_view
->m_controller
->triggerItem(item
);
360 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
362 // TODO: same implementation as in DolphinView; create helper class
363 // for generatePreviews(), showPreview() and isCutItem()
365 if (m_view
->m_controller
->dolphinView()->showPreview()) {
366 if (m_previewJob
!= 0) {
367 m_previewJob
->kill();
371 m_previewJob
= KIO::filePreview(items
, 128);
372 connect(m_previewJob
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
373 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
374 connect(m_previewJob
, SIGNAL(finished(KJob
*)),
375 this, SLOT(slotPreviewJobFinished(KJob
*)));
379 void DolphinColumnWidget::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
381 // TODO: same implementation as in DolphinView; create helper class
382 // for generatePreviews(), showPreview() and isCutItem()
384 Q_ASSERT(!item
.isNull());
385 const bool showPreview
= m_view
->m_controller
->dolphinView()->showPreview();
386 if (!showPreview
|| (item
.url().directory() != m_dirLister
->url().path())) {
387 // the preview job is still working on items of an older URL, hence
388 // the item is not part of the directory model anymore
392 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
393 if (idx
.isValid() && (idx
.column() == 0)) {
394 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
395 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
396 KIconEffect iconEffect
;
397 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
398 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
400 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
405 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
407 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
408 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
409 m_view
->m_controller
->emitItemEntered(item
);
412 void DolphinColumnWidget::slotPreviewJobFinished(KJob
* job
)
414 Q_ASSERT(job
== m_previewJob
);
418 void DolphinColumnWidget::activate()
420 setFocus(Qt::OtherFocusReason
);
422 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
423 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
424 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
425 if (KGlobalSettings::singleClick()) {
426 connect(this, SIGNAL(clicked(const QModelIndex
&)),
427 this, SLOT(triggerItem(const QModelIndex
&)));
429 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
430 this, SLOT(triggerItem(const QModelIndex
&)));
433 if (!m_childUrl
.isEmpty()) {
434 // assure that the current index is set on the index that represents
436 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
437 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
438 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
444 void DolphinColumnWidget::deactivate()
448 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
449 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
450 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
451 if (KGlobalSettings::singleClick()) {
452 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
453 this, SLOT(triggerItem(const QModelIndex
&)));
455 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
456 this, SLOT(triggerItem(const QModelIndex
&)));
459 selectionModel()->clear();
463 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
465 // TODO: same implementation as in DolphinView; create helper class
466 // for generatePreviews(), showPreview() and isCutItem()
468 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
469 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
471 const KUrl
& itemUrl
= item
.url();
472 KUrl::List::const_iterator it
= cutUrls
.begin();
473 const KUrl::List::const_iterator end
= cutUrls
.end();
475 if (*it
== itemUrl
) {
484 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
486 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
487 return m_dolphinModel
->itemForIndex(dirIndex
);
491 #include "dolphincolumnwidget.moc"