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()
132 delete m_dolphinModel
;
134 m_dirLister
= 0; // deleted by m_dolphinModel
137 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
139 m_decorationSize
= size
;
143 void DolphinColumnWidget::setActive(bool active
)
145 if (m_active
== active
) {
158 void DolphinColumnWidget::reload()
161 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
164 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
166 if (show
!= m_dirLister
->showingDotFiles()) {
167 m_dirLister
->setShowingDotFiles(show
);
169 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
173 void DolphinColumnWidget::setShowPreview(bool show
)
175 if (show
!= m_showPreview
) {
177 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
181 void DolphinColumnWidget::updateBackground()
183 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
184 if (!m_active
|| !m_view
->m_active
) {
187 QPalette palette
= viewport()->palette();
188 palette
.setColor(viewport()->backgroundRole(), color
);
189 viewport()->setPalette(palette
);
194 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
196 m_proxyModel
->setFilterRegExp(nameFilter
);
200 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
202 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
203 viewOptions
.font
= m_font
;
204 viewOptions
.decorationSize
= m_decorationSize
;
205 viewOptions
.showDecorationSelected
= true;
209 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
211 DragAndDropHelper::startDrag(this, supportedActions
);
214 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
216 if (event
->mimeData()->hasUrls()) {
217 event
->acceptProposedAction();
223 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
225 QListView::dragLeaveEvent(event
);
227 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
229 setDirtyRegion(m_dropRect
);
232 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
234 QListView::dragMoveEvent(event
);
236 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
237 const QModelIndex index
= indexAt(event
->pos());
238 setDirtyRegion(m_dropRect
);
240 m_dropRect
.setSize(QSize()); // set as invalid
241 if (index
.isValid()) {
242 const KFileItem item
= itemForIndex(index
);
243 if (!item
.isNull() && item
.isDir()) {
244 m_dropRect
= visualRect(index
);
247 setDirtyRegion(m_dropRect
);
250 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
252 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
253 if (!urls
.isEmpty()) {
254 const QModelIndex index
= indexAt(event
->pos());
255 const KFileItem item
= itemForIndex(index
);
256 m_view
->m_controller
->indicateDroppedUrls(urls
,
259 event
->acceptProposedAction();
261 QListView::dropEvent(event
);
265 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
267 if (!m_childUrl
.isEmpty()) {
268 // indicate the shown URL of the next column by highlighting the shown folder item
269 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
270 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
271 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
272 const QRect itemRect
= visualRect(proxyIndex
);
273 QPainter
painter(viewport());
276 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
278 painter
.setPen(Qt::NoPen
);
279 painter
.setBrush(color
);
280 painter
.drawRect(itemRect
);
286 QListView::paintEvent(event
);
288 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
290 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
291 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect
, brush
);
295 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
297 m_view
->m_controller
->requestActivation();
299 m_view
->requestActivation(this);
300 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
303 QListView::mousePressEvent(event
);
306 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
308 QListView::keyPressEvent(event
);
310 const QItemSelectionModel
* selModel
= selectionModel();
311 const QModelIndex currentIndex
= selModel
->currentIndex();
312 const bool trigger
= currentIndex
.isValid()
313 && (event
->key() == Qt::Key_Return
)
314 && (selModel
->selectedIndexes().count() <= 1);
316 triggerItem(currentIndex
);
320 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
323 m_view
->requestActivation(this);
324 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
327 QListView::contextMenuEvent(event
);
329 const QModelIndex index
= indexAt(event
->pos());
330 if (index
.isValid() || m_active
) {
331 // Only open a context menu above an item or if the mouse is above
332 // the active column.
333 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
334 m_view
->m_controller
->triggerContextMenuRequest(pos
);
338 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
340 QListView::selectionChanged(selected
, deselected
);
342 QItemSelectionModel
* selModel
= m_view
->selectionModel();
343 selModel
->select(selected
, QItemSelectionModel::Select
);
344 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
347 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
349 const KFileItem item
= itemForIndex(index
);
350 m_view
->m_controller
->triggerItem(item
);
353 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
355 // TODO: same implementation as in DolphinView; create helper class
356 // for generatePreviews(), showPreview() and isCutItem()
358 if (m_view
->m_controller
->dolphinView()->showPreview()) {
359 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
360 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
361 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
365 void DolphinColumnWidget::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
367 // TODO: same implementation as in DolphinView; create helper class
368 // for generatePreviews(), showPreview() and isCutItem()
370 Q_ASSERT(!item
.isNull());
371 if (item
.url().directory() != m_dirLister
->url().path()) {
372 // the preview job is still working on items of an older URL, hence
373 // the item is not part of the directory model anymore
377 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
378 if (idx
.isValid() && (idx
.column() == 0)) {
379 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
380 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
381 KIconEffect iconEffect
;
382 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
383 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
385 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
390 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
392 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
393 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
394 m_view
->m_controller
->emitItemEntered(item
);
397 void DolphinColumnWidget::activate()
399 setFocus(Qt::OtherFocusReason
);
401 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
402 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
403 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
404 if (KGlobalSettings::singleClick()) {
405 connect(this, SIGNAL(clicked(const QModelIndex
&)),
406 this, SLOT(triggerItem(const QModelIndex
&)));
408 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
409 this, SLOT(triggerItem(const QModelIndex
&)));
412 if (!m_childUrl
.isEmpty()) {
413 // assure that the current index is set on the index that represents
415 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
416 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
417 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
423 void DolphinColumnWidget::deactivate()
427 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
428 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
429 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
430 if (KGlobalSettings::singleClick()) {
431 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
432 this, SLOT(triggerItem(const QModelIndex
&)));
434 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
435 this, SLOT(triggerItem(const QModelIndex
&)));
438 selectionModel()->clear();
442 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
444 // TODO: same implementation as in DolphinView; create helper class
445 // for generatePreviews(), showPreview() and isCutItem()
447 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
448 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
450 const KUrl
& itemUrl
= item
.url();
451 KUrl::List::const_iterator it
= cutUrls
.begin();
452 const KUrl::List::const_iterator end
= cutUrls
.end();
454 if (*it
== itemUrl
) {
463 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
465 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
466 return m_dolphinModel
->itemForIndex(dirIndex
);
470 #include "dolphincolumnwidget.moc"