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
,
65 setMouseTracking(true);
66 viewport()->setAttribute(Qt::WA_Hover
);
67 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
68 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
69 setSelectionBehavior(SelectItems
);
70 setSelectionMode(QAbstractItemView::ExtendedSelection
);
71 setDragDropMode(QAbstractItemView::DragDrop
);
72 setDropIndicatorShown(false);
74 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
75 // check avoids a division by zero happening on versions before 4.3.1.
76 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
78 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
79 setVerticalScrollMode(QListView::ScrollPerPixel
);
80 setHorizontalScrollMode(QListView::ScrollPerPixel
);
83 // apply the column mode settings to the widget
84 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
85 Q_ASSERT(settings
!= 0);
87 m_viewOptions
= QListView::viewOptions();
89 QFont
font(settings
->fontFamily(), settings
->fontSize());
90 font
.setItalic(settings
->italicFont());
91 font
.setBold(settings
->boldFont());
92 m_viewOptions
.font
= font
;
94 const int iconSize
= settings
->iconSize();
95 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
97 m_viewOptions
.showDecorationSelected
= true;
99 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
100 setItemDelegate(delegate
);
104 connect(this, SIGNAL(viewportEntered()),
105 m_view
->m_controller
, SLOT(emitViewportEntered()));
106 connect(this, SIGNAL(entered(const QModelIndex
&)),
107 this, SLOT(slotEntered(const QModelIndex
&)));
109 //m_dirLister = new DolphinDirLister(); TODO
110 m_dirLister
= new KDirLister();
111 m_dirLister
->setAutoUpdate(true);
112 m_dirLister
->setMainWindow(this);
113 m_dirLister
->setDelayedMimeTypes(true);
114 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
115 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
116 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
117 this, SLOT(generatePreviews(const KFileItemList
&)));
119 m_dolphinModel
= new DolphinModel(this);
120 m_dolphinModel
->setDirLister(m_dirLister
);
121 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
123 m_proxyModel
= new DolphinSortFilterProxyModel(this);
124 m_proxyModel
->setSourceModel(m_dolphinModel
);
126 setModel(m_proxyModel
);
127 new KMimeTypeResolver(this, m_dolphinModel
);
129 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
132 DolphinColumnWidget::~DolphinColumnWidget()
138 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
140 m_viewOptions
.decorationSize
= size
;
144 void DolphinColumnWidget::setActive(bool active
)
146 if (m_active
== active
) {
159 void DolphinColumnWidget::reload()
162 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
165 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
167 if (show
!= m_dirLister
->showingDotFiles()) {
168 m_dirLister
->setShowingDotFiles(show
);
170 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
174 void DolphinColumnWidget::setShowPreview(bool show
)
176 if (show
!= m_showPreview
) {
178 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
182 void DolphinColumnWidget::updateBackground()
184 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
185 if (!m_active
|| !m_view
->m_active
) {
188 QPalette palette
= viewport()->palette();
189 palette
.setColor(viewport()->backgroundRole(), color
);
190 viewport()->setPalette(palette
);
195 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
197 m_proxyModel
->setFilterRegExp(nameFilter
);
200 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
202 DragAndDropHelper::startDrag(this, supportedActions
);
205 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
207 if (event
->mimeData()->hasUrls()) {
208 event
->acceptProposedAction();
214 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
216 QListView::dragLeaveEvent(event
);
218 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
220 setDirtyRegion(m_dropRect
);
223 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
225 QListView::dragMoveEvent(event
);
227 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
228 const QModelIndex index
= indexAt(event
->pos());
229 setDirtyRegion(m_dropRect
);
231 m_dropRect
.setSize(QSize()); // set as invalid
232 if (index
.isValid()) {
233 const KFileItem item
= itemForIndex(index
);
234 if (!item
.isNull() && item
.isDir()) {
235 m_dropRect
= visualRect(index
);
238 setDirtyRegion(m_dropRect
);
241 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
243 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
244 if (!urls
.isEmpty()) {
245 const QModelIndex index
= indexAt(event
->pos());
246 const KFileItem item
= itemForIndex(index
);
247 m_view
->m_controller
->indicateDroppedUrls(urls
,
250 event
->acceptProposedAction();
252 QListView::dropEvent(event
);
256 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
258 if (!m_childUrl
.isEmpty()) {
259 // indicate the shown URL of the next column by highlighting the shown folder item
260 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
261 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
262 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
263 const QRect itemRect
= visualRect(proxyIndex
);
264 QPainter
painter(viewport());
267 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
269 painter
.setPen(Qt::NoPen
);
270 painter
.setBrush(color
);
271 painter
.drawRect(itemRect
);
277 QListView::paintEvent(event
);
279 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
281 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
282 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect
, brush
);
286 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
288 m_view
->m_controller
->requestActivation();
290 m_view
->requestActivation(this);
291 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
294 QListView::mousePressEvent(event
);
297 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
299 QListView::keyPressEvent(event
);
301 const QItemSelectionModel
* selModel
= selectionModel();
302 const QModelIndex currentIndex
= selModel
->currentIndex();
303 const bool trigger
= currentIndex
.isValid()
304 && (event
->key() == Qt::Key_Return
)
305 && (selModel
->selectedIndexes().count() <= 1);
307 triggerItem(currentIndex
);
311 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
314 m_view
->requestActivation(this);
315 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
318 QListView::contextMenuEvent(event
);
320 const QModelIndex index
= indexAt(event
->pos());
321 if (index
.isValid() || m_active
) {
322 // Only open a context menu above an item or if the mouse is above
323 // the active column.
324 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
325 m_view
->m_controller
->triggerContextMenuRequest(pos
);
329 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
331 QListView::selectionChanged(selected
, deselected
);
333 QItemSelectionModel
* selModel
= m_view
->selectionModel();
334 selModel
->select(selected
, QItemSelectionModel::Select
);
335 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
338 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
340 const KFileItem item
= itemForIndex(index
);
341 m_view
->m_controller
->triggerItem(item
);
344 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
346 // TODO: same implementation as in DolphinView; create helper class
347 // for generatePreviews(), showPreview() and isCutItem()
349 if (m_view
->m_controller
->dolphinView()->showPreview()) {
350 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
351 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
352 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
356 void DolphinColumnWidget::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
358 // TODO: same implementation as in DolphinView; create helper class
359 // for generatePreviews(), showPreview() and isCutItem()
361 Q_ASSERT(!item
.isNull());
362 if (item
.url().directory() != m_dirLister
->url().path()) {
363 // the preview job is still working on items of an older URL, hence
364 // the item is not part of the directory model anymore
368 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
369 if (idx
.isValid() && (idx
.column() == 0)) {
370 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
371 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
372 KIconEffect iconEffect
;
373 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
374 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
376 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
381 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
383 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
384 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
385 m_view
->m_controller
->emitItemEntered(item
);
388 void DolphinColumnWidget::activate()
390 setFocus(Qt::OtherFocusReason
);
392 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
393 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
394 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
395 if (KGlobalSettings::singleClick()) {
396 connect(this, SIGNAL(clicked(const QModelIndex
&)),
397 this, SLOT(triggerItem(const QModelIndex
&)));
399 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
400 this, SLOT(triggerItem(const QModelIndex
&)));
403 if (!m_childUrl
.isEmpty()) {
404 // assure that the current index is set on the index that represents
406 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
407 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
408 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
414 void DolphinColumnWidget::deactivate()
418 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
419 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
420 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
421 if (KGlobalSettings::singleClick()) {
422 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
423 this, SLOT(triggerItem(const QModelIndex
&)));
425 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
426 this, SLOT(triggerItem(const QModelIndex
&)));
429 selectionModel()->clear();
433 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
435 // TODO: same implementation as in DolphinView; create helper class
436 // for generatePreviews(), showPreview() and isCutItem()
438 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
439 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
441 const KUrl
& itemUrl
= item
.url();
442 KUrl::List::const_iterator it
= cutUrls
.begin();
443 const KUrl::List::const_iterator end
= cutUrls
.end();
445 if (*it
== itemUrl
) {
454 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
456 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
457 return m_dolphinModel
->itemForIndex(dirIndex
);
461 #include "dolphincolumnwidget.moc"