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
);
251 bool destIsDir
= false;
252 if (index
.isValid()) {
253 const KFileItem item
= itemForIndex(index
);
254 if (!item
.isNull() && item
.isDir()) {
255 m_dropRect
= visualRect(index
);
258 } else { // dropping on viewport
261 if (destIsDir
&& event
->mimeData()->hasUrls()) {
262 event
->acceptProposedAction();
266 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
268 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
269 if (!urls
.isEmpty()) {
270 const QModelIndex index
= indexAt(event
->pos());
271 const KFileItem item
= itemForIndex(index
);
272 m_view
->m_controller
->indicateDroppedUrls(urls
,
275 event
->acceptProposedAction();
277 QListView::dropEvent(event
);
281 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
283 if (!m_childUrl
.isEmpty()) {
284 // indicate the shown URL of the next column by highlighting the shown folder item
285 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
286 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
287 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
288 const QRect itemRect
= visualRect(proxyIndex
);
289 QPainter
painter(viewport());
292 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
294 painter
.setPen(Qt::NoPen
);
295 painter
.setBrush(color
);
296 painter
.drawRect(itemRect
);
302 QListView::paintEvent(event
);
304 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
306 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
307 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
311 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
313 m_view
->m_controller
->requestActivation();
315 m_view
->requestActivation(this);
316 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
319 QListView::mousePressEvent(event
);
322 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
324 QListView::keyPressEvent(event
);
326 const QItemSelectionModel
* selModel
= selectionModel();
327 const QModelIndex currentIndex
= selModel
->currentIndex();
328 const bool trigger
= currentIndex
.isValid()
329 && (event
->key() == Qt::Key_Return
)
330 && (selModel
->selectedIndexes().count() <= 1);
332 triggerItem(currentIndex
);
336 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
339 m_view
->requestActivation(this);
340 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
343 QListView::contextMenuEvent(event
);
345 const QModelIndex index
= indexAt(event
->pos());
346 if (index
.isValid() || m_active
) {
347 // Only open a context menu above an item or if the mouse is above
348 // the active column.
349 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
350 m_view
->m_controller
->triggerContextMenuRequest(pos
);
354 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
356 QListView::selectionChanged(selected
, deselected
);
358 QItemSelectionModel
* selModel
= m_view
->selectionModel();
359 selModel
->select(selected
, QItemSelectionModel::Select
);
360 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
363 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
365 const KFileItem item
= itemForIndex(index
);
366 m_view
->m_controller
->triggerItem(item
);
369 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
371 // TODO: same implementation as in DolphinView; create helper class
372 // for generatePreviews(), showPreview() and isCutItem()
374 if (m_view
->m_controller
->dolphinView()->showPreview()) {
375 if (m_previewJob
!= 0) {
376 m_previewJob
->kill();
380 m_previewJob
= KIO::filePreview(items
, 128);
381 connect(m_previewJob
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
382 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
383 connect(m_previewJob
, SIGNAL(finished(KJob
*)),
384 this, SLOT(slotPreviewJobFinished(KJob
*)));
388 void DolphinColumnWidget::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
390 // TODO: same implementation as in DolphinView; create helper class
391 // for generatePreviews(), showPreview() and isCutItem()
393 Q_ASSERT(!item
.isNull());
394 const bool showPreview
= m_view
->m_controller
->dolphinView()->showPreview();
395 if (!showPreview
|| (item
.url().directory() != m_dirLister
->url().path())) {
396 // the preview job is still working on items of an older URL, hence
397 // the item is not part of the directory model anymore
401 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
402 if (idx
.isValid() && (idx
.column() == 0)) {
403 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
404 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
405 KIconEffect iconEffect
;
406 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
407 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
409 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
414 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
416 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
417 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
418 m_view
->m_controller
->emitItemEntered(item
);
421 void DolphinColumnWidget::slotPreviewJobFinished(KJob
* job
)
423 Q_ASSERT(job
== m_previewJob
);
427 void DolphinColumnWidget::activate()
429 setFocus(Qt::OtherFocusReason
);
431 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
432 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
433 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
434 if (KGlobalSettings::singleClick()) {
435 connect(this, SIGNAL(clicked(const QModelIndex
&)),
436 this, SLOT(triggerItem(const QModelIndex
&)));
438 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
439 this, SLOT(triggerItem(const QModelIndex
&)));
442 if (!m_childUrl
.isEmpty()) {
443 // assure that the current index is set on the index that represents
445 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
446 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
447 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
453 void DolphinColumnWidget::deactivate()
457 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
458 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
459 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
460 if (KGlobalSettings::singleClick()) {
461 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
462 this, SLOT(triggerItem(const QModelIndex
&)));
464 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
465 this, SLOT(triggerItem(const QModelIndex
&)));
468 selectionModel()->clear();
472 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
474 // TODO: same implementation as in DolphinView; create helper class
475 // for generatePreviews(), showPreview() and isCutItem()
477 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
478 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
480 const KUrl
& itemUrl
= item
.url();
481 KUrl::List::const_iterator it
= cutUrls
.begin();
482 const KUrl::List::const_iterator end
= cutUrls
.end();
484 if (*it
== itemUrl
) {
493 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
495 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
496 return m_dolphinModel
->itemForIndex(dirIndex
);
500 #include "dolphincolumnwidget.moc"