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);
72 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
73 // check avoids a division by zero happening on versions before 4.3.1.
74 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
76 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
77 setVerticalScrollMode(QListView::ScrollPerPixel
);
78 setHorizontalScrollMode(QListView::ScrollPerPixel
);
81 // apply the column mode settings to the widget
82 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
83 Q_ASSERT(settings
!= 0);
85 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
86 m_font
.setItalic(settings
->italicFont());
87 m_font
.setBold(settings
->boldFont());
89 const int iconSize
= settings
->iconSize();
90 m_decorationSize
= QSize(iconSize
, iconSize
);
92 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
93 setItemDelegate(delegate
);
97 connect(this, SIGNAL(viewportEntered()),
98 m_view
->m_controller
, SLOT(emitViewportEntered()));
99 connect(this, SIGNAL(entered(const QModelIndex
&)),
100 this, SLOT(slotEntered(const QModelIndex
&)));
102 //m_dirLister = new DolphinDirLister(); TODO
103 m_dirLister
= new KDirLister();
104 m_dirLister
->setAutoUpdate(true);
105 m_dirLister
->setMainWindow(this);
106 m_dirLister
->setDelayedMimeTypes(true);
107 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
108 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
109 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
110 this, SLOT(generatePreviews(const KFileItemList
&)));
112 m_dolphinModel
= new DolphinModel(this);
113 m_dolphinModel
->setDirLister(m_dirLister
);
114 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
116 m_proxyModel
= new DolphinSortFilterProxyModel(this);
117 m_proxyModel
->setSourceModel(m_dolphinModel
);
119 setModel(m_proxyModel
);
120 new KMimeTypeResolver(this, m_dolphinModel
);
122 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
125 DolphinColumnWidget::~DolphinColumnWidget()
129 delete m_dolphinModel
;
131 m_dirLister
= 0; // deleted by m_dolphinModel
133 if (m_previewJob
!= 0) {
134 m_previewJob
->kill();
139 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
141 m_decorationSize
= size
;
145 void DolphinColumnWidget::setActive(bool active
)
147 if (m_active
== active
) {
160 void DolphinColumnWidget::reload()
163 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
166 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
168 if (show
!= m_dirLister
->showingDotFiles()) {
169 m_dirLister
->setShowingDotFiles(show
);
171 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
175 void DolphinColumnWidget::setShowPreview(bool show
)
177 if (show
!= m_showPreview
) {
179 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
183 void DolphinColumnWidget::updateBackground()
185 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
186 if (!m_active
|| !m_view
->m_active
) {
189 QPalette palette
= viewport()->palette();
190 palette
.setColor(viewport()->backgroundRole(), color
);
191 viewport()->setPalette(palette
);
196 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
198 m_proxyModel
->setFilterRegExp(nameFilter
);
202 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
204 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
205 viewOptions
.font
= m_font
;
206 viewOptions
.decorationSize
= m_decorationSize
;
207 viewOptions
.showDecorationSelected
= true;
211 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
213 DragAndDropHelper::startDrag(this, supportedActions
);
216 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
218 if (event
->mimeData()->hasUrls()) {
219 event
->acceptProposedAction();
225 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
227 QListView::dragLeaveEvent(event
);
229 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
231 setDirtyRegion(m_dropRect
);
234 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
236 QListView::dragMoveEvent(event
);
238 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
239 const QModelIndex index
= indexAt(event
->pos());
240 setDirtyRegion(m_dropRect
);
242 m_dropRect
.setSize(QSize()); // set as invalid
243 if (index
.isValid()) {
244 const KFileItem item
= itemForIndex(index
);
245 if (!item
.isNull() && item
.isDir()) {
246 m_dropRect
= visualRect(index
);
249 setDirtyRegion(m_dropRect
);
252 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
254 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
255 if (!urls
.isEmpty()) {
256 const QModelIndex index
= indexAt(event
->pos());
257 const KFileItem item
= itemForIndex(index
);
258 m_view
->m_controller
->indicateDroppedUrls(urls
,
261 event
->acceptProposedAction();
263 QListView::dropEvent(event
);
267 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
269 if (!m_childUrl
.isEmpty()) {
270 // indicate the shown URL of the next column by highlighting the shown folder item
271 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
272 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
273 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
274 const QRect itemRect
= visualRect(proxyIndex
);
275 QPainter
painter(viewport());
278 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
280 painter
.setPen(Qt::NoPen
);
281 painter
.setBrush(color
);
282 painter
.drawRect(itemRect
);
288 QListView::paintEvent(event
);
290 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
292 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
293 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect
, brush
);
297 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
299 m_view
->m_controller
->requestActivation();
301 m_view
->requestActivation(this);
302 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
305 QListView::mousePressEvent(event
);
308 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
310 QListView::keyPressEvent(event
);
312 const QItemSelectionModel
* selModel
= selectionModel();
313 const QModelIndex currentIndex
= selModel
->currentIndex();
314 const bool trigger
= currentIndex
.isValid()
315 && (event
->key() == Qt::Key_Return
)
316 && (selModel
->selectedIndexes().count() <= 1);
318 triggerItem(currentIndex
);
322 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
325 m_view
->requestActivation(this);
326 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
329 QListView::contextMenuEvent(event
);
331 const QModelIndex index
= indexAt(event
->pos());
332 if (index
.isValid() || m_active
) {
333 // Only open a context menu above an item or if the mouse is above
334 // the active column.
335 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
336 m_view
->m_controller
->triggerContextMenuRequest(pos
);
340 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
342 QListView::selectionChanged(selected
, deselected
);
344 QItemSelectionModel
* selModel
= m_view
->selectionModel();
345 selModel
->select(selected
, QItemSelectionModel::Select
);
346 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
349 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
351 const KFileItem item
= itemForIndex(index
);
352 m_view
->m_controller
->triggerItem(item
);
355 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
357 // TODO: same implementation as in DolphinView; create helper class
358 // for generatePreviews(), showPreview() and isCutItem()
360 if (m_view
->m_controller
->dolphinView()->showPreview()) {
361 if (m_previewJob
!= 0) {
362 m_previewJob
->kill();
366 m_previewJob
= KIO::filePreview(items
, 128);
367 connect(m_previewJob
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
368 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
369 connect(m_previewJob
, SIGNAL(finished(KJob
*)),
370 this, SLOT(slotPreviewJobFinished(KJob
*)));
374 void DolphinColumnWidget::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
376 // TODO: same implementation as in DolphinView; create helper class
377 // for generatePreviews(), showPreview() and isCutItem()
379 Q_ASSERT(!item
.isNull());
380 const bool showPreview
= m_view
->m_controller
->dolphinView()->showPreview();
381 if (!showPreview
|| (item
.url().directory() != m_dirLister
->url().path())) {
382 // the preview job is still working on items of an older URL, hence
383 // the item is not part of the directory model anymore
387 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
388 if (idx
.isValid() && (idx
.column() == 0)) {
389 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
390 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
391 KIconEffect iconEffect
;
392 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
393 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
395 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
400 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
402 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
403 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
404 m_view
->m_controller
->emitItemEntered(item
);
407 void DolphinColumnWidget::slotPreviewJobFinished(KJob
* job
)
409 Q_ASSERT(job
== m_previewJob
);
413 void DolphinColumnWidget::activate()
415 setFocus(Qt::OtherFocusReason
);
417 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
418 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
419 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
420 if (KGlobalSettings::singleClick()) {
421 connect(this, SIGNAL(clicked(const QModelIndex
&)),
422 this, SLOT(triggerItem(const QModelIndex
&)));
424 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
425 this, SLOT(triggerItem(const QModelIndex
&)));
428 if (!m_childUrl
.isEmpty()) {
429 // assure that the current index is set on the index that represents
431 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
432 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
433 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
439 void DolphinColumnWidget::deactivate()
443 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
444 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
445 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
446 if (KGlobalSettings::singleClick()) {
447 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
448 this, SLOT(triggerItem(const QModelIndex
&)));
450 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
451 this, SLOT(triggerItem(const QModelIndex
&)));
454 selectionModel()->clear();
458 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
460 // TODO: same implementation as in DolphinView; create helper class
461 // for generatePreviews(), showPreview() and isCutItem()
463 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
464 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
466 const KUrl
& itemUrl
= item
.url();
467 KUrl::List::const_iterator it
= cutUrls
.begin();
468 const KUrl::List::const_iterator end
= cutUrls
.end();
470 if (*it
== itemUrl
) {
479 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
481 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
482 return m_dolphinModel
->itemForIndex(dirIndex
);
486 #include "dolphincolumnwidget.moc"