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