]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
fixed issue that turning off the preview during the preview job still was active...
[dolphin.git] / src / dolphincolumnwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "dolphincolumnwidget.h"
21
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"
31
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 <kjob.h>
39 #include <kmimetyperesolver.h>
40 #include <konqmimedata.h>
41
42 #include <QAbstractProxyModel>
43 #include <QApplication>
44 #include <QClipboard>
45 #include <QPoint>
46 #include <QScrollBar>
47 #include <QTimer>
48 #include <QTimeLine>
49
50 DolphinColumnWidget::DolphinColumnWidget(QWidget* parent,
51 DolphinColumnView* columnView,
52 const KUrl& url) :
53 QListView(parent),
54 m_active(true),
55 m_showPreview(false),
56 m_view(columnView),
57 m_url(url),
58 m_childUrl(),
59 m_font(),
60 m_decorationSize(),
61 m_dirLister(0),
62 m_dolphinModel(0),
63 m_proxyModel(0),
64 m_previewJob(0),
65 m_dragging(false),
66 m_dropRect()
67 {
68 setMouseTracking(true);
69 viewport()->setAttribute(Qt::WA_Hover);
70 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
71 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
72 setSelectionBehavior(SelectItems);
73 setSelectionMode(QAbstractItemView::ExtendedSelection);
74 setDragDropMode(QAbstractItemView::DragDrop);
75 setDropIndicatorShown(false);
76
77 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
78 // check avoids a division by zero happening on versions before 4.3.1.
79 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
80 // ereslibre
81 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
82 setVerticalScrollMode(QListView::ScrollPerPixel);
83 setHorizontalScrollMode(QListView::ScrollPerPixel);
84 #endif
85
86 // apply the column mode settings to the widget
87 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
88 Q_ASSERT(settings != 0);
89
90 m_font = QFont(settings->fontFamily(), settings->fontSize());
91 m_font.setItalic(settings->italicFont());
92 m_font.setBold(settings->boldFont());
93
94 const int iconSize = settings->iconSize();
95 m_decorationSize = QSize(iconSize, iconSize);
96
97 KFileItemDelegate* delegate = new KFileItemDelegate(this);
98 setItemDelegate(delegate);
99
100 activate();
101
102 connect(this, SIGNAL(viewportEntered()),
103 m_view->m_controller, SLOT(emitViewportEntered()));
104 connect(this, SIGNAL(entered(const QModelIndex&)),
105 this, SLOT(slotEntered(const QModelIndex&)));
106
107 //m_dirLister = new DolphinDirLister(); TODO
108 m_dirLister = new KDirLister();
109 m_dirLister->setAutoUpdate(true);
110 m_dirLister->setMainWindow(this);
111 m_dirLister->setDelayedMimeTypes(true);
112 const bool showHiddenFiles = m_view->m_controller->dolphinView()->showHiddenFiles();
113 m_dirLister->setShowingDotFiles(showHiddenFiles);
114 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
115 this, SLOT(generatePreviews(const KFileItemList&)));
116
117 m_dolphinModel = new DolphinModel(this);
118 m_dolphinModel->setDirLister(m_dirLister);
119 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
120
121 m_proxyModel = new DolphinSortFilterProxyModel(this);
122 m_proxyModel->setSourceModel(m_dolphinModel);
123
124 setModel(m_proxyModel);
125 new KMimeTypeResolver(this, m_dolphinModel);
126
127 m_dirLister->openUrl(url, KDirLister::NoFlags);
128 }
129
130 DolphinColumnWidget::~DolphinColumnWidget()
131 {
132 delete m_proxyModel;
133 m_proxyModel = 0;
134 delete m_dolphinModel;
135 m_dolphinModel = 0;
136 m_dirLister = 0; // deleted by m_dolphinModel
137
138 if (m_previewJob != 0) {
139 m_previewJob->kill();
140 m_previewJob = 0;
141 }
142 }
143
144 void DolphinColumnWidget::setDecorationSize(const QSize& size)
145 {
146 m_decorationSize = size;
147 doItemsLayout();
148 }
149
150 void DolphinColumnWidget::setActive(bool active)
151 {
152 if (m_active == active) {
153 return;
154 }
155
156 m_active = active;
157
158 if (active) {
159 activate();
160 } else {
161 deactivate();
162 }
163 }
164
165 void DolphinColumnWidget::reload()
166 {
167 m_dirLister->stop();
168 m_dirLister->openUrl(m_url, KDirLister::Reload);
169 }
170
171 void DolphinColumnWidget::setShowHiddenFiles(bool show)
172 {
173 if (show != m_dirLister->showingDotFiles()) {
174 m_dirLister->setShowingDotFiles(show);
175 m_dirLister->stop();
176 m_dirLister->openUrl(m_url, KDirLister::Reload);
177 }
178 }
179
180 void DolphinColumnWidget::setShowPreview(bool show)
181 {
182 if (show != m_showPreview) {
183 m_dirLister->stop();
184 m_dirLister->openUrl(m_url, KDirLister::Reload);
185 }
186 }
187
188 void DolphinColumnWidget::updateBackground()
189 {
190 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
191 if (!m_active || !m_view->m_active) {
192 color.setAlpha(150);
193 }
194 QPalette palette = viewport()->palette();
195 palette.setColor(viewport()->backgroundRole(), color);
196 viewport()->setPalette(palette);
197
198 update();
199 }
200
201 void DolphinColumnWidget::setNameFilter(const QString& nameFilter)
202 {
203 m_proxyModel->setFilterRegExp(nameFilter);
204 }
205
206
207 QStyleOptionViewItem DolphinColumnWidget::viewOptions() const
208 {
209 QStyleOptionViewItem viewOptions = QListView::viewOptions();
210 viewOptions.font = m_font;
211 viewOptions.decorationSize = m_decorationSize;
212 viewOptions.showDecorationSelected = true;
213 return viewOptions;
214 }
215
216 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions)
217 {
218 DragAndDropHelper::startDrag(this, supportedActions);
219 }
220
221 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent* event)
222 {
223 if (event->mimeData()->hasUrls()) {
224 event->acceptProposedAction();
225 }
226
227 m_dragging = true;
228 }
229
230 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
231 {
232 QListView::dragLeaveEvent(event);
233
234 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
235 m_dragging = false;
236 setDirtyRegion(m_dropRect);
237 }
238
239 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event)
240 {
241 QListView::dragMoveEvent(event);
242
243 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
244 const QModelIndex index = indexAt(event->pos());
245 setDirtyRegion(m_dropRect);
246
247 m_dropRect.setSize(QSize()); // set as invalid
248 if (index.isValid()) {
249 const KFileItem item = itemForIndex(index);
250 if (!item.isNull() && item.isDir()) {
251 m_dropRect = visualRect(index);
252 }
253 }
254 setDirtyRegion(m_dropRect);
255 }
256
257 void DolphinColumnWidget::dropEvent(QDropEvent* event)
258 {
259 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
260 if (!urls.isEmpty()) {
261 const QModelIndex index = indexAt(event->pos());
262 const KFileItem item = itemForIndex(index);
263 m_view->m_controller->indicateDroppedUrls(urls,
264 url(),
265 item);
266 event->acceptProposedAction();
267 }
268 QListView::dropEvent(event);
269 m_dragging = false;
270 }
271
272 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
273 {
274 if (!m_childUrl.isEmpty()) {
275 // indicate the shown URL of the next column by highlighting the shown folder item
276 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
277 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
278 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
279 const QRect itemRect = visualRect(proxyIndex);
280 QPainter painter(viewport());
281 painter.save();
282
283 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
284 color.setAlpha(32);
285 painter.setPen(Qt::NoPen);
286 painter.setBrush(color);
287 painter.drawRect(itemRect);
288
289 painter.restore();
290 }
291 }
292
293 QListView::paintEvent(event);
294
295 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
296 if (m_dragging) {
297 const QBrush& brush = viewOptions().palette.brush(QPalette::Normal, QPalette::Highlight);
298 DragAndDropHelper::drawHoverIndication(viewport(), m_dropRect, brush);
299 }
300 }
301
302 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
303 {
304 m_view->m_controller->requestActivation();
305 if (!m_active) {
306 m_view->requestActivation(this);
307 m_view->m_controller->triggerUrlChangeRequest(m_url);
308 }
309
310 QListView::mousePressEvent(event);
311 }
312
313 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
314 {
315 QListView::keyPressEvent(event);
316
317 const QItemSelectionModel* selModel = selectionModel();
318 const QModelIndex currentIndex = selModel->currentIndex();
319 const bool trigger = currentIndex.isValid()
320 && (event->key() == Qt::Key_Return)
321 && (selModel->selectedIndexes().count() <= 1);
322 if (trigger) {
323 triggerItem(currentIndex);
324 }
325 }
326
327 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
328 {
329 if (!m_active) {
330 m_view->requestActivation(this);
331 m_view->m_controller->triggerUrlChangeRequest(m_url);
332 }
333
334 QListView::contextMenuEvent(event);
335
336 const QModelIndex index = indexAt(event->pos());
337 if (index.isValid() || m_active) {
338 // Only open a context menu above an item or if the mouse is above
339 // the active column.
340 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
341 m_view->m_controller->triggerContextMenuRequest(pos);
342 }
343 }
344
345 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
346 {
347 QListView::selectionChanged(selected, deselected);
348
349 QItemSelectionModel* selModel = m_view->selectionModel();
350 selModel->select(selected, QItemSelectionModel::Select);
351 selModel->select(deselected, QItemSelectionModel::Deselect);
352 }
353
354 void DolphinColumnWidget::triggerItem(const QModelIndex& index)
355 {
356 const KFileItem item = itemForIndex(index);
357 m_view->m_controller->triggerItem(item);
358 }
359
360 void DolphinColumnWidget::generatePreviews(const KFileItemList& items)
361 {
362 // TODO: same implementation as in DolphinView; create helper class
363 // for generatePreviews(), showPreview() and isCutItem()
364
365 if (m_view->m_controller->dolphinView()->showPreview()) {
366 if (m_previewJob != 0) {
367 m_previewJob->kill();
368 m_previewJob = 0;
369 }
370
371 m_previewJob = KIO::filePreview(items, 128);
372 connect(m_previewJob, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
373 this, SLOT(replaceIcon(const KFileItem&, const QPixmap&)));
374 connect(m_previewJob, SIGNAL(finished(KJob*)),
375 this, SLOT(slotPreviewJobFinished(KJob*)));
376 }
377 }
378
379 void DolphinColumnWidget::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
380 {
381 // TODO: same implementation as in DolphinView; create helper class
382 // for generatePreviews(), showPreview() and isCutItem()
383
384 Q_ASSERT(!item.isNull());
385 const bool showPreview = m_view->m_controller->dolphinView()->showPreview();
386 if (!showPreview || (item.url().directory() != m_dirLister->url().path())) {
387 // the preview job is still working on items of an older URL, hence
388 // the item is not part of the directory model anymore
389 return;
390 }
391
392 const QModelIndex idx = m_dolphinModel->indexForItem(item);
393 if (idx.isValid() && (idx.column() == 0)) {
394 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
395 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
396 KIconEffect iconEffect;
397 const QPixmap cutPixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
398 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
399 } else {
400 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
401 }
402 }
403 }
404
405 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
406 {
407 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
408 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
409 m_view->m_controller->emitItemEntered(item);
410 }
411
412 void DolphinColumnWidget::slotPreviewJobFinished(KJob* job)
413 {
414 Q_ASSERT(job == m_previewJob);
415 m_previewJob = 0;
416 }
417
418 void DolphinColumnWidget::activate()
419 {
420 setFocus(Qt::OtherFocusReason);
421
422 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
423 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
424 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
425 if (KGlobalSettings::singleClick()) {
426 connect(this, SIGNAL(clicked(const QModelIndex&)),
427 this, SLOT(triggerItem(const QModelIndex&)));
428 } else {
429 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
430 this, SLOT(triggerItem(const QModelIndex&)));
431 }
432
433 if (!m_childUrl.isEmpty()) {
434 // assure that the current index is set on the index that represents
435 // the child URL
436 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
437 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
438 selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::Current);
439 }
440
441 updateBackground();
442 }
443
444 void DolphinColumnWidget::deactivate()
445 {
446 clearFocus();
447
448 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
449 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
450 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
451 if (KGlobalSettings::singleClick()) {
452 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
453 this, SLOT(triggerItem(const QModelIndex&)));
454 } else {
455 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
456 this, SLOT(triggerItem(const QModelIndex&)));
457 }
458
459 selectionModel()->clear();
460 updateBackground();
461 }
462
463 bool DolphinColumnWidget::isCutItem(const KFileItem& item) const
464 {
465 // TODO: same implementation as in DolphinView; create helper class
466 // for generatePreviews(), showPreview() and isCutItem()
467
468 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
469 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
470
471 const KUrl& itemUrl = item.url();
472 KUrl::List::const_iterator it = cutUrls.begin();
473 const KUrl::List::const_iterator end = cutUrls.end();
474 while (it != end) {
475 if (*it == itemUrl) {
476 return true;
477 }
478 ++it;
479 }
480
481 return false;
482 }
483
484 KFileItem DolphinColumnWidget::itemForIndex(const QModelIndex& index) const
485 {
486 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
487 return m_dolphinModel->itemForIndex(dirIndex);
488 }
489
490
491 #include "dolphincolumnwidget.moc"