]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
When dropping an Amarok file into Dolphin, mimeData->hasUrls() returns true although...
[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 "dolphinsortfilterproxymodel.h"
27 #include "dolphinsettings.h"
28 #include "dolphinviewautoscroller.h"
29 #include "dolphin_columnmodesettings.h"
30 #include "dolphin_generalsettings.h"
31 #include "draganddrophelper.h"
32 #include "folderexpander.h"
33 #include "selectionmanager.h"
34 #include "tooltipmanager.h"
35
36 #include <kcolorscheme.h>
37 #include <kdirlister.h>
38 #include <kfileitem.h>
39 #include <kfilepreviewgenerator.h>
40 #include <kio/previewjob.h>
41 #include <kiconeffect.h>
42 #include <kjob.h>
43 #include <konqmimedata.h>
44
45 #include <QApplication>
46 #include <QClipboard>
47 #include <QPainter>
48 #include <QPoint>
49 #include <QScrollBar>
50
51 DolphinColumnWidget::DolphinColumnWidget(QWidget* parent,
52 DolphinColumnView* columnView,
53 const KUrl& url) :
54 QListView(parent),
55 m_active(true),
56 m_view(columnView),
57 m_selectionManager(0),
58 m_autoScroller(0),
59 m_url(url),
60 m_childUrl(),
61 m_font(),
62 m_decorationSize(),
63 m_dirLister(0),
64 m_dolphinModel(0),
65 m_proxyModel(0),
66 m_previewGenerator(0),
67 m_dropRect()
68 {
69 setMouseTracking(true);
70 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
71 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
72 setSelectionBehavior(SelectItems);
73 setSelectionMode(QAbstractItemView::ExtendedSelection);
74 setDragDropMode(QAbstractItemView::DragDrop);
75 setDropIndicatorShown(false);
76 setSelectionRectVisible(true);
77 setEditTriggers(QAbstractItemView::NoEditTriggers);
78
79 setVerticalScrollMode(QListView::ScrollPerPixel);
80 setHorizontalScrollMode(QListView::ScrollPerPixel);
81
82 m_autoScroller = new DolphinViewAutoScroller(this);
83
84 // apply the column mode settings to the widget
85 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
86 Q_ASSERT(settings != 0);
87
88 if (settings->useSystemFont()) {
89 m_font = KGlobalSettings::generalFont();
90 } else {
91 m_font = QFont(settings->fontFamily(),
92 settings->fontSize(),
93 settings->fontWeight(),
94 settings->italicFont());
95 }
96
97 const int iconSize = settings->iconSize();
98 setDecorationSize(QSize(iconSize, iconSize));
99
100 KFileItemDelegate* delegate = new KFileItemDelegate(this);
101 delegate->setShowToolTipWhenElided(false);
102 setItemDelegate(delegate);
103
104 activate();
105
106 connect(this, SIGNAL(viewportEntered()),
107 m_view->m_controller, SLOT(emitViewportEntered()));
108 connect(this, SIGNAL(entered(const QModelIndex&)),
109 this, SLOT(slotEntered(const QModelIndex&)));
110
111 m_dirLister = new DolphinDirLister();
112 m_dirLister->setAutoUpdate(true);
113 m_dirLister->setMainWindow(window());
114 m_dirLister->setDelayedMimeTypes(true);
115 const bool showHiddenFiles = m_view->m_controller->dolphinView()->showHiddenFiles();
116 m_dirLister->setShowingDotFiles(showHiddenFiles);
117
118 m_dolphinModel = new DolphinModel(this);
119 m_dolphinModel->setDirLister(m_dirLister);
120 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
121
122 m_proxyModel = new DolphinSortFilterProxyModel(this);
123 m_proxyModel->setSourceModel(m_dolphinModel);
124 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
125 const DolphinView* dolphinView = m_view->m_controller->dolphinView();
126 m_proxyModel->setSorting(dolphinView->sorting());
127 m_proxyModel->setSortOrder(dolphinView->sortOrder());
128
129 setModel(m_proxyModel);
130
131 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
132 m_selectionManager = new SelectionManager(this);
133 connect(m_selectionManager, SIGNAL(selectionChanged()),
134 this, SLOT(requestActivation()));
135 connect(m_view->m_controller, SIGNAL(urlChanged(const KUrl&)),
136 m_selectionManager, SLOT(reset()));
137 }
138
139 m_previewGenerator = new KFilePreviewGenerator(this);
140 m_previewGenerator->setPreviewShown(m_view->m_controller->dolphinView()->showPreview());
141
142 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
143 new ToolTipManager(this, m_proxyModel);
144 }
145
146 m_dirLister->openUrl(url, KDirLister::NoFlags);
147
148 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
149 this, SLOT(updateFont()));
150
151 FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
152 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
153 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
154 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
155 }
156
157 DolphinColumnWidget::~DolphinColumnWidget()
158 {
159 delete m_proxyModel;
160 m_proxyModel = 0;
161 delete m_dolphinModel;
162 m_dolphinModel = 0;
163 m_dirLister = 0; // deleted by m_dolphinModel
164 }
165
166 void DolphinColumnWidget::setDecorationSize(const QSize& size)
167 {
168 setIconSize(size);
169 m_decorationSize = size;
170 doItemsLayout();
171 if (m_previewGenerator != 0) {
172 m_previewGenerator->updatePreviews();
173 }
174 if (m_selectionManager != 0) {
175 m_selectionManager->reset();
176 }
177 }
178
179 void DolphinColumnWidget::setActive(bool active)
180 {
181 if (m_active == active) {
182 return;
183 }
184
185 m_active = active;
186
187 if (active) {
188 activate();
189 } else {
190 deactivate();
191 }
192 }
193
194 void DolphinColumnWidget::reload()
195 {
196 m_dirLister->stop();
197 m_dirLister->openUrl(m_url, KDirLister::Reload);
198 }
199
200 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting)
201 {
202 m_proxyModel->setSorting(sorting);
203 }
204
205 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order)
206 {
207 m_proxyModel->setSortOrder(order);
208 }
209
210 void DolphinColumnWidget::setShowHiddenFiles(bool show)
211 {
212 if (show != m_dirLister->showingDotFiles()) {
213 m_dirLister->setShowingDotFiles(show);
214 m_dirLister->stop();
215 m_dirLister->openUrl(m_url, KDirLister::Reload);
216 }
217 }
218
219 void DolphinColumnWidget::setShowPreview(bool show)
220 {
221 m_previewGenerator->setPreviewShown(show);
222
223 m_dirLister->stop();
224 m_dirLister->openUrl(m_url, KDirLister::Reload);
225 }
226
227 void DolphinColumnWidget::updateBackground()
228 {
229 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
230 // cleaning up the cut-indication of DolphinColumnWidget with the code from
231 // DolphinView a common helper-class should be available which can be shared
232 // by all view implementations -> no hardcoded value anymore
233 const QPalette::ColorRole role = viewport()->backgroundRole();
234 QColor color = viewport()->palette().color(role);
235 color.setAlpha((m_active && m_view->m_active) ? 255 : 150);
236
237 QPalette palette = viewport()->palette();
238 palette.setColor(role, color);
239 viewport()->setPalette(palette);
240
241 update();
242 }
243
244 void DolphinColumnWidget::setNameFilter(const QString& nameFilter)
245 {
246 m_proxyModel->setFilterRegExp(nameFilter);
247 }
248
249 void DolphinColumnWidget::editItem(const KFileItem& item)
250 {
251 const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
252 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
253 if (proxyIndex.isValid()) {
254 edit(proxyIndex);
255 }
256 }
257
258 KFileItem DolphinColumnWidget::itemAt(const QPoint& pos) const
259 {
260 KFileItem item;
261 const QModelIndex index = indexAt(pos);
262 if (index.isValid() && (index.column() == DolphinModel::Name)) {
263 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
264 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
265 }
266 return item;
267 }
268
269 KFileItemList DolphinColumnWidget::selectedItems() const
270 {
271 const QItemSelection selection = m_proxyModel->mapSelectionToSource(selectionModel()->selection());
272 KFileItemList itemList;
273
274 const QModelIndexList indexList = selection.indexes();
275 foreach (const QModelIndex &index, indexList) {
276 KFileItem item = m_dolphinModel->itemForIndex(index);
277 if (!item.isNull()) {
278 itemList.append(item);
279 }
280 }
281
282 return itemList;
283 }
284
285 QMimeData* DolphinColumnWidget::selectionMimeData() const
286 {
287 const QItemSelection selection = m_proxyModel->mapSelectionToSource(selectionModel()->selection());
288 return m_dolphinModel->mimeData(selection.indexes());
289 }
290
291 QStyleOptionViewItem DolphinColumnWidget::viewOptions() const
292 {
293 QStyleOptionViewItem viewOptions = QListView::viewOptions();
294 viewOptions.font = m_font;
295 viewOptions.decorationSize = m_decorationSize;
296 viewOptions.showDecorationSelected = true;
297 return viewOptions;
298 }
299
300 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions)
301 {
302 DragAndDropHelper::instance().startDrag(this, supportedActions, m_view->m_controller);
303 }
304
305 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent* event)
306 {
307 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
308 event->acceptProposedAction();
309 requestActivation();
310 }
311 }
312
313 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
314 {
315 QListView::dragLeaveEvent(event);
316 setDirtyRegion(m_dropRect);
317 }
318
319 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event)
320 {
321 QListView::dragMoveEvent(event);
322
323 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
324 const QModelIndex index = indexAt(event->pos());
325 setDirtyRegion(m_dropRect);
326
327 m_dropRect.setSize(QSize()); // set as invalid
328 if (index.isValid()) {
329 m_view->m_controller->setItemView(this);
330 const KFileItem item = m_view->m_controller->itemForIndex(index);
331 if (!item.isNull() && item.isDir()) {
332 m_dropRect = visualRect(index);
333 }
334 }
335 setDirtyRegion(m_dropRect);
336
337 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
338 // accept url drops, independently from the destination item
339 event->acceptProposedAction();
340 }
341 }
342
343 void DolphinColumnWidget::dropEvent(QDropEvent* event)
344 {
345 const QModelIndex index = indexAt(event->pos());
346 m_view->m_controller->setItemView(this);
347 const KFileItem item = m_view->m_controller->itemForIndex(index);
348 m_view->m_controller->indicateDroppedUrls(item, url(), event);
349 QListView::dropEvent(event);
350 }
351
352 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
353 {
354 if (!m_childUrl.isEmpty()) {
355 // indicate the shown URL of the next column by highlighting the shown folder item
356 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
357 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
358 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
359 const QRect itemRect = visualRect(proxyIndex);
360 QPainter painter(viewport());
361 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
362 color.setAlpha(32);
363 painter.setPen(Qt::NoPen);
364 painter.setBrush(color);
365 painter.drawRect(itemRect);
366 }
367 }
368
369 QListView::paintEvent(event);
370 }
371
372 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
373 {
374 requestActivation();
375 if (!indexAt(event->pos()).isValid()) {
376 if (QApplication::mouseButtons() & Qt::MidButton) {
377 m_view->m_controller->replaceUrlByClipboard();
378 }
379 } else if (event->button() == Qt::LeftButton) {
380 // TODO: see comment in DolphinIconsView::mousePressEvent()
381 setState(QAbstractItemView::DraggingState);
382 }
383 QListView::mousePressEvent(event);
384 }
385
386 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
387 {
388 QListView::keyPressEvent(event);
389 requestActivation();
390 m_view->m_controller->handleKeyPressEvent(event);
391 }
392
393 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
394 {
395 if (!m_active) {
396 m_view->requestActivation(this);
397 Q_ASSERT(m_view->m_controller->itemView() == this);
398 m_view->m_controller->triggerUrlChangeRequest(m_url);
399 }
400 Q_ASSERT(m_active);
401
402 QListView::contextMenuEvent(event);
403
404 const QModelIndex index = indexAt(event->pos());
405 if (!index.isValid()) {
406 clearSelection();
407 }
408
409 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
410 Q_ASSERT(m_view->m_controller->itemView() == this);
411 m_view->m_controller->triggerContextMenuRequest(pos);
412 }
413
414 void DolphinColumnWidget::wheelEvent(QWheelEvent* event)
415 {
416 if (m_selectionManager != 0) {
417 m_selectionManager->reset();
418 }
419
420 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
421 if (event->modifiers() & Qt::ControlModifier) {
422 event->ignore();
423 return;
424 }
425
426 const int height = m_decorationSize.height();
427 const int step = (height >= KIconLoader::SizeHuge) ? height / 10 : (KIconLoader::SizeHuge - height) / 2;
428 verticalScrollBar()->setSingleStep(step);
429
430 QListView::wheelEvent(event);
431 }
432
433 void DolphinColumnWidget::leaveEvent(QEvent* event)
434 {
435 QListView::leaveEvent(event);
436 // if the mouse is above an item and moved very fast outside the widget,
437 // no viewportEntered() signal might be emitted although the mouse has been moved
438 // above the viewport
439 m_view->m_controller->emitViewportEntered();
440 }
441
442 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
443 {
444 QListView::selectionChanged(selected, deselected);
445
446 QItemSelectionModel* selModel = m_view->selectionModel();
447 selModel->select(selected, QItemSelectionModel::Select);
448 selModel->select(deselected, QItemSelectionModel::Deselect);
449 }
450
451 void DolphinColumnWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous)
452 {
453 QListView::currentChanged(current, previous);
454 if (current.isValid() && !m_autoScroller->isActive()) {
455 scrollTo(current);
456 }
457 }
458
459 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
460 {
461 m_view->m_controller->setItemView(this);
462 m_view->m_controller->emitItemEntered(index);
463 }
464
465 void DolphinColumnWidget::requestActivation()
466 {
467 m_view->m_controller->setItemView(this);
468 m_view->m_controller->requestActivation();
469 if (!m_active) {
470 m_view->requestActivation(this);
471 m_view->m_controller->triggerUrlChangeRequest(m_url);
472 selectionModel()->clear();
473 }
474 }
475
476 void DolphinColumnWidget::updateFont()
477 {
478 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
479 Q_ASSERT(settings != 0);
480
481 if (settings->useSystemFont()) {
482 m_font = KGlobalSettings::generalFont();
483 }
484 }
485
486 void DolphinColumnWidget::activate()
487 {
488 setFocus(Qt::OtherFocusReason);
489
490 connect(this, SIGNAL(clicked(const QModelIndex&)),
491 m_view->m_controller, SLOT(requestTab(const QModelIndex&)));
492 if (KGlobalSettings::singleClick()) {
493 connect(this, SIGNAL(clicked(const QModelIndex&)),
494 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
495 } else {
496 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
497 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
498 }
499
500 if (selectionModel() && selectionModel()->currentIndex().isValid())
501 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
502
503 updateBackground();
504 }
505
506 void DolphinColumnWidget::deactivate()
507 {
508 clearFocus();
509
510 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
511 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
512 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
513 if (KGlobalSettings::singleClick()) {
514 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
515 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
516 } else {
517 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
518 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
519 }
520
521 const QModelIndex current = selectionModel()->currentIndex();
522 selectionModel()->clear();
523 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
524 updateBackground();
525 }
526
527 #include "dolphincolumnwidget.moc"