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