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>
38 #include <kmimetyperesolver.h>
39 #include <konqmimedata.h>
41 #include <QAbstractProxyModel>
42 #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::ScrollBarAlwaysOn
);
70 setSelectionBehavior(SelectItems
);
71 setSelectionMode(QAbstractItemView::ExtendedSelection
);
72 setDragDropMode(QAbstractItemView::DragDrop
);
73 setDropIndicatorShown(false);
75 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
76 // check avoids a division by zero happening on versions before 4.3.1.
77 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
79 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
80 setVerticalScrollMode(QListView::ScrollPerPixel
);
81 setHorizontalScrollMode(QListView::ScrollPerPixel
);
84 // apply the column mode settings to the widget
85 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
86 Q_ASSERT(settings
!= 0);
88 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
89 m_font
.setItalic(settings
->italicFont());
90 m_font
.setBold(settings
->boldFont());
92 const int iconSize
= settings
->iconSize();
93 m_decorationSize
= 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(this);
109 m_dirLister
->setDelayedMimeTypes(true);
110 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
111 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
112 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
113 this, SLOT(generatePreviews(const KFileItemList
&)));
115 m_dolphinModel
= new DolphinModel(this);
116 m_dolphinModel
->setDirLister(m_dirLister
);
117 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
119 m_proxyModel
= new DolphinSortFilterProxyModel(this);
120 m_proxyModel
->setSourceModel(m_dolphinModel
);
122 setModel(m_proxyModel
);
123 new KMimeTypeResolver(this, m_dolphinModel
);
125 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
128 DolphinColumnWidget::~DolphinColumnWidget()
130 delete m_dolphinModel
;
132 m_dirLister
= 0; // deleted by m_dolphinModel
135 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
137 m_decorationSize
= size
;
141 void DolphinColumnWidget::setActive(bool active
)
143 if (m_active
== active
) {
156 void DolphinColumnWidget::reload()
159 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
162 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
164 if (show
!= m_dirLister
->showingDotFiles()) {
165 m_dirLister
->setShowingDotFiles(show
);
167 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
171 void DolphinColumnWidget::setShowPreview(bool show
)
173 if (show
!= m_showPreview
) {
175 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
179 void DolphinColumnWidget::updateBackground()
181 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
182 if (!m_active
|| !m_view
->m_active
) {
185 QPalette palette
= viewport()->palette();
186 palette
.setColor(viewport()->backgroundRole(), color
);
187 viewport()->setPalette(palette
);
192 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
194 m_proxyModel
->setFilterRegExp(nameFilter
);
198 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
200 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
201 viewOptions
.font
= m_font
;
202 viewOptions
.decorationSize
= m_decorationSize
;
203 viewOptions
.showDecorationSelected
= true;
207 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
209 DragAndDropHelper::startDrag(this, supportedActions
);
212 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
214 if (event
->mimeData()->hasUrls()) {
215 event
->acceptProposedAction();
221 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
223 QListView::dragLeaveEvent(event
);
225 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
227 setDirtyRegion(m_dropRect
);
230 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
232 QListView::dragMoveEvent(event
);
234 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
235 const QModelIndex index
= indexAt(event
->pos());
236 setDirtyRegion(m_dropRect
);
238 m_dropRect
.setSize(QSize()); // set as invalid
239 if (index
.isValid()) {
240 const KFileItem item
= itemForIndex(index
);
241 if (!item
.isNull() && item
.isDir()) {
242 m_dropRect
= visualRect(index
);
245 setDirtyRegion(m_dropRect
);
248 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
250 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
251 if (!urls
.isEmpty()) {
252 const QModelIndex index
= indexAt(event
->pos());
253 const KFileItem item
= itemForIndex(index
);
254 m_view
->m_controller
->indicateDroppedUrls(urls
,
257 event
->acceptProposedAction();
259 QListView::dropEvent(event
);
263 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
265 if (!m_childUrl
.isEmpty()) {
266 // indicate the shown URL of the next column by highlighting the shown folder item
267 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
268 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
269 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
270 const QRect itemRect
= visualRect(proxyIndex
);
271 QPainter
painter(viewport());
274 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
276 painter
.setPen(Qt::NoPen
);
277 painter
.setBrush(color
);
278 painter
.drawRect(itemRect
);
284 QListView::paintEvent(event
);
286 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
288 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
289 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect
, brush
);
293 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
295 m_view
->m_controller
->requestActivation();
297 m_view
->requestActivation(this);
298 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
301 QListView::mousePressEvent(event
);
304 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
306 QListView::keyPressEvent(event
);
308 const QItemSelectionModel
* selModel
= selectionModel();
309 const QModelIndex currentIndex
= selModel
->currentIndex();
310 const bool trigger
= currentIndex
.isValid()
311 && (event
->key() == Qt::Key_Return
)
312 && (selModel
->selectedIndexes().count() <= 1);
314 triggerItem(currentIndex
);
318 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
321 m_view
->requestActivation(this);
322 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
325 QListView::contextMenuEvent(event
);
327 const QModelIndex index
= indexAt(event
->pos());
328 if (index
.isValid() || m_active
) {
329 // Only open a context menu above an item or if the mouse is above
330 // the active column.
331 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
332 m_view
->m_controller
->triggerContextMenuRequest(pos
);
336 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
338 QListView::selectionChanged(selected
, deselected
);
340 QItemSelectionModel
* selModel
= m_view
->selectionModel();
341 selModel
->select(selected
, QItemSelectionModel::Select
);
342 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
345 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
347 const KFileItem item
= itemForIndex(index
);
348 m_view
->m_controller
->triggerItem(item
);
351 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
353 // TODO: same implementation as in DolphinView; create helper class
354 // for generatePreviews(), showPreview() and isCutItem()
356 if (m_view
->m_controller
->dolphinView()->showPreview()) {
357 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
358 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
359 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
363 void DolphinColumnWidget::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
365 // TODO: same implementation as in DolphinView; create helper class
366 // for generatePreviews(), showPreview() and isCutItem()
368 Q_ASSERT(!item
.isNull());
369 if (item
.url().directory() != m_dirLister
->url().path()) {
370 // the preview job is still working on items of an older URL, hence
371 // the item is not part of the directory model anymore
375 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
376 if (idx
.isValid() && (idx
.column() == 0)) {
377 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
378 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
379 KIconEffect iconEffect
;
380 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
381 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
383 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
388 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
390 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
391 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
392 m_view
->m_controller
->emitItemEntered(item
);
395 void DolphinColumnWidget::activate()
397 setFocus(Qt::OtherFocusReason
);
399 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
400 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
401 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
402 if (KGlobalSettings::singleClick()) {
403 connect(this, SIGNAL(clicked(const QModelIndex
&)),
404 this, SLOT(triggerItem(const QModelIndex
&)));
406 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
407 this, SLOT(triggerItem(const QModelIndex
&)));
410 if (!m_childUrl
.isEmpty()) {
411 // assure that the current index is set on the index that represents
413 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
414 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
415 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
421 void DolphinColumnWidget::deactivate()
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 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
430 this, SLOT(triggerItem(const QModelIndex
&)));
432 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
433 this, SLOT(triggerItem(const QModelIndex
&)));
436 selectionModel()->clear();
440 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
442 // TODO: same implementation as in DolphinView; create helper class
443 // for generatePreviews(), showPreview() and isCutItem()
445 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
446 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
448 const KUrl
& itemUrl
= item
.url();
449 KUrl::List::const_iterator it
= cutUrls
.begin();
450 const KUrl::List::const_iterator end
= cutUrls
.end();
452 if (*it
== itemUrl
) {
461 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
463 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
464 return m_dolphinModel
->itemForIndex(dirIndex
);
468 #include "dolphincolumnwidget.moc"