]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
when going back in history apply the keyboard focus to the the previously shown directory
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QKeyEvent>
26 #include <QItemSelection>
27 #include <QBoxLayout>
28 #include <QTimer>
29 #include <QScrollBar>
30
31 #include <kactioncollection.h>
32 #include <kcolorscheme.h>
33 #include <kdirlister.h>
34 #include <kfileitemdelegate.h>
35 #include <kiconeffect.h>
36 #include <klocale.h>
37 #include <kio/deletejob.h>
38 #include <kio/netaccess.h>
39 #include <kio/previewjob.h>
40 #include <kjob.h>
41 #include <kmenu.h>
42 #include <kmessagebox.h>
43 #include <kmimetyperesolver.h>
44 #include <konq_operations.h>
45 #include <konqmimedata.h>
46 #include <ktoggleaction.h>
47 #include <kurl.h>
48
49 #include "dolphindropcontroller.h"
50 #include "dolphinmodel.h"
51 #include "dolphincolumnview.h"
52 #include "dolphincontroller.h"
53 #include "dolphinsortfilterproxymodel.h"
54 #include "dolphindetailsview.h"
55 #include "dolphiniconsview.h"
56 #include "dolphinsettings.h"
57 #include "dolphin_generalsettings.h"
58 #include "iconmanager.h"
59 #include "renamedialog.h"
60 #include "tooltipmanager.h"
61 #include "viewproperties.h"
62
63 DolphinView::DolphinView(QWidget* parent,
64 const KUrl& url,
65 KDirLister* dirLister,
66 DolphinModel* dolphinModel,
67 DolphinSortFilterProxyModel* proxyModel) :
68 QWidget(parent),
69 m_active(true),
70 m_showPreview(false),
71 m_loadingDirectory(false),
72 m_storedCategorizedSorting(false),
73 m_tabsForFiles(false),
74 m_isContextMenuOpen(false),
75 m_mode(DolphinView::IconsView),
76 m_topLayout(0),
77 m_controller(0),
78 m_iconsView(0),
79 m_detailsView(0),
80 m_columnView(0),
81 m_fileItemDelegate(0),
82 m_selectionModel(0),
83 m_dolphinModel(dolphinModel),
84 m_dirLister(dirLister),
85 m_proxyModel(proxyModel),
86 m_iconManager(0),
87 m_toolTipManager(0)
88 {
89 m_topLayout = new QVBoxLayout(this);
90 m_topLayout->setSpacing(0);
91 m_topLayout->setMargin(0);
92
93 m_controller = new DolphinController(this);
94 m_controller->setUrl(url);
95
96 // Receiver of the DolphinView signal 'urlChanged()' don't need
97 // to care whether the internal controller changed the URL already or whether
98 // the controller just requested an URL change and will be updated later.
99 // In both cases the URL has been changed:
100 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
101 this, SIGNAL(urlChanged(const KUrl&)));
102 connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
103 this, SIGNAL(urlChanged(const KUrl&)));
104
105 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
106 this, SLOT(openContextMenu(const QPoint&)));
107 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const KFileItem&)),
108 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const KFileItem&)));
109 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
110 this, SLOT(updateSorting(DolphinView::Sorting)));
111 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
112 this, SLOT(updateSortOrder(Qt::SortOrder)));
113 connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
114 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
115 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
116 this, SLOT(triggerItem(const KFileItem&)));
117 connect(m_controller, SIGNAL(tabRequested(const KUrl&)),
118 this, SIGNAL(tabRequested(const KUrl&)));
119 connect(m_controller, SIGNAL(activated()),
120 this, SLOT(activate()));
121 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
122 this, SLOT(showHoverInformation(const KFileItem&)));
123 connect(m_controller, SIGNAL(viewportEntered()),
124 this, SLOT(clearHoverInformation()));
125
126 applyViewProperties(url);
127 m_topLayout->addWidget(itemView());
128 }
129
130 DolphinView::~DolphinView()
131 {
132 }
133
134 const KUrl& DolphinView::url() const
135 {
136 return m_controller->url();
137 }
138
139 KUrl DolphinView::rootUrl() const
140 {
141 return isColumnViewActive() ? m_columnView->rootUrl() : url();
142 }
143
144 void DolphinView::setActive(bool active)
145 {
146 if (active == m_active) {
147 return;
148 }
149
150 m_active = active;
151 m_selectionModel->clearSelection();
152
153 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
154 if (active) {
155 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
156 // bypasses the problem when having a split view and changing the active view to
157 // update the some URL dependent states. A nicer approach should be no big deal...
158 emit urlChanged(url());
159 emit selectionChanged(selectedItems());
160 } else {
161 color.setAlpha(150);
162 }
163
164 QWidget* viewport = itemView()->viewport();
165 QPalette palette;
166 palette.setColor(viewport->backgroundRole(), color);
167 viewport->setPalette(palette);
168
169 update();
170
171 if (active) {
172 emit activated();
173 }
174
175 m_controller->indicateActivationChange(active);
176 }
177
178 bool DolphinView::isActive() const
179 {
180 return m_active;
181 }
182
183 void DolphinView::setMode(Mode mode)
184 {
185 if (mode == m_mode) {
186 return; // the wished mode is already set
187 }
188
189 m_mode = mode;
190
191 deleteView();
192
193 const KUrl viewPropsUrl = viewPropertiesUrl();
194 ViewProperties props(viewPropsUrl);
195 props.setViewMode(m_mode);
196 createView();
197
198 // the file item delegate has been recreated, apply the current
199 // additional information manually
200 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
201 m_fileItemDelegate->setShowInformation(infoList);
202 emit additionalInfoChanged();
203
204 // Not all view modes support categorized sorting. Adjust the sorting model
205 // if changing the view mode results in a change of the categorized sorting
206 // capabilities.
207 m_storedCategorizedSorting = props.categorizedSorting();
208 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
209 if (categorized != m_proxyModel->isCategorizedModel()) {
210 m_proxyModel->setCategorizedModel(categorized);
211 emit categorizedSortingChanged();
212 }
213
214 emit modeChanged();
215 }
216
217 DolphinView::Mode DolphinView::mode() const
218 {
219 return m_mode;
220 }
221
222 bool DolphinView::showPreview() const
223 {
224 return m_showPreview;
225 }
226
227 bool DolphinView::showHiddenFiles() const
228 {
229 return m_dirLister->showingDotFiles();
230 }
231
232 bool DolphinView::categorizedSorting() const
233 {
234 // If all view modes would support categorized sorting, returning
235 // m_proxyModel->isCategorizedModel() would be the way to go. As
236 // currently only the icons view supports caterized sorting, we remember
237 // the stored view properties state in m_storedCategorizedSorting and
238 // return this state. The application takes care to disable the corresponding
239 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
240 // that this setting is not applied to the current view mode.
241 return m_storedCategorizedSorting;
242 }
243
244 bool DolphinView::supportsCategorizedSorting() const
245 {
246 return m_iconsView != 0;
247 }
248
249 void DolphinView::selectAll()
250 {
251 QAbstractItemView* view = itemView();
252 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
253 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
254 // selection instead of selecting all items. This is bypassed for KDE 4.0
255 // by invoking clearSelection() first.
256 view->clearSelection();
257 view->selectAll();
258 }
259
260 void DolphinView::invertSelection()
261 {
262 if (isColumnViewActive()) {
263 // QAbstractItemView does not offer a virtual method invertSelection()
264 // as counterpart to QAbstractItemView::selectAll(). This makes it
265 // necessary to delegate the inverting of the selection to the
266 // column view, as only the selection of the active column should
267 // get inverted.
268 m_columnView->invertSelection();
269 } else {
270 QItemSelectionModel* selectionModel = itemView()->selectionModel();
271 const QAbstractItemModel* itemModel = selectionModel->model();
272
273 const QModelIndex topLeft = itemModel->index(0, 0);
274 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
275 itemModel->columnCount() - 1);
276
277 const QItemSelection selection(topLeft, bottomRight);
278 selectionModel->select(selection, QItemSelectionModel::Toggle);
279 }
280 }
281
282 bool DolphinView::hasSelection() const
283 {
284 return itemView()->selectionModel()->hasSelection();
285 }
286
287 void DolphinView::clearSelection()
288 {
289 itemView()->selectionModel()->clear();
290 }
291
292 KFileItemList DolphinView::selectedItems() const
293 {
294 const QAbstractItemView* view = itemView();
295
296 // Our view has a selection, we will map them back to the DolphinModel
297 // and then fill the KFileItemList.
298 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
299
300 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
301 KFileItemList itemList;
302
303 const QModelIndexList indexList = selection.indexes();
304 foreach (const QModelIndex &index, indexList) {
305 KFileItem item = m_dolphinModel->itemForIndex(index);
306 if (!item.isNull()) {
307 itemList.append(item);
308 }
309 }
310
311 return itemList;
312 }
313
314 KUrl::List DolphinView::selectedUrls() const
315 {
316 KUrl::List urls;
317 const KFileItemList list = selectedItems();
318 foreach (const KFileItem &item, list) {
319 urls.append(item.url());
320 }
321 return urls;
322 }
323
324 KFileItem DolphinView::fileItem(const QModelIndex& index) const
325 {
326 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
327 return m_dolphinModel->itemForIndex(dolphinModelIndex);
328 }
329
330 void DolphinView::setContentsPosition(int x, int y)
331 {
332 QAbstractItemView* view = itemView();
333
334 // the ColumnView takes care itself for the horizontal scrolling
335 if (!isColumnViewActive()) {
336 view->horizontalScrollBar()->setValue(x);
337 }
338 view->verticalScrollBar()->setValue(y);
339
340 m_loadingDirectory = false;
341 }
342
343 QPoint DolphinView::contentsPosition() const
344 {
345 const int x = itemView()->horizontalScrollBar()->value();
346 const int y = itemView()->verticalScrollBar()->value();
347 return QPoint(x, y);
348 }
349
350 void DolphinView::setCurrentItem(const KUrl& url)
351 {
352 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(url);
353 if (dirIndex.isValid()) {
354 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
355 QAbstractItemView* view = itemView();
356 const bool clearSelection = !hasSelection();
357 view->setCurrentIndex(proxyIndex);
358 if (clearSelection) {
359 view->clearSelection();
360 }
361 }
362 }
363
364 void DolphinView::zoomIn()
365 {
366 m_controller->triggerZoomIn();
367 m_iconManager->updatePreviews();
368 }
369
370 void DolphinView::zoomOut()
371 {
372 m_controller->triggerZoomOut();
373 m_iconManager->updatePreviews();
374 }
375
376 bool DolphinView::isZoomInPossible() const
377 {
378 return m_controller->isZoomInPossible();
379 }
380
381 bool DolphinView::isZoomOutPossible() const
382 {
383 return m_controller->isZoomOutPossible();
384 }
385
386 void DolphinView::setSorting(Sorting sorting)
387 {
388 if (sorting != this->sorting()) {
389 updateSorting(sorting);
390 }
391 }
392
393 DolphinView::Sorting DolphinView::sorting() const
394 {
395 return m_proxyModel->sorting();
396 }
397
398 void DolphinView::setSortOrder(Qt::SortOrder order)
399 {
400 if (sortOrder() != order) {
401 updateSortOrder(order);
402 }
403 }
404
405 Qt::SortOrder DolphinView::sortOrder() const
406 {
407 return m_proxyModel->sortOrder();
408 }
409
410 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
411 {
412 const KUrl viewPropsUrl = viewPropertiesUrl();
413 ViewProperties props(viewPropsUrl);
414 props.setAdditionalInfo(info);
415 m_fileItemDelegate->setShowInformation(info);
416
417 emit additionalInfoChanged();
418
419 if (itemView() != m_detailsView) {
420 // the details view requires no reloading of the directory, as it maps
421 // the file item delegate info to its columns internally
422 loadDirectory(viewPropsUrl);
423 }
424 }
425
426 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
427 {
428 return m_fileItemDelegate->showInformation();
429 }
430
431 void DolphinView::reload()
432 {
433 setUrl(url());
434 loadDirectory(url(), true);
435 }
436
437 void DolphinView::refresh()
438 {
439 const bool oldActivationState = m_active;
440 m_active = true;
441
442 createView();
443 applyViewProperties(m_controller->url());
444 reload();
445
446 setActive(oldActivationState);
447 }
448
449 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
450 {
451 if (m_controller->url() == url) {
452 return;
453 }
454
455 m_controller->setUrl(url); // emits urlChanged, which we forward
456
457 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
458 applyViewProperties(rootUrl);
459 loadDirectory(rootUrl);
460 if (itemView() == m_columnView) {
461 m_columnView->setRootUrl(rootUrl);
462 m_columnView->showColumn(url);
463 }
464 } else {
465 applyViewProperties(url);
466 loadDirectory(url);
467 }
468
469 emit startedPathLoading(url);
470 }
471
472 void DolphinView::setNameFilter(const QString& nameFilter)
473 {
474 m_proxyModel->setFilterRegExp(nameFilter);
475
476 if (isColumnViewActive()) {
477 // adjusting the directory lister is not enough in the case of the
478 // column view, as each column has its own directory lister internally...
479 m_columnView->setNameFilter(nameFilter);
480 }
481 }
482
483 void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
484 {
485 foreach (const KFileItem &item, m_dirLister->items()) {
486 if (item.isDir()) {
487 ++folderCount;
488 } else {
489 ++fileCount;
490 }
491 }
492 }
493
494 void DolphinView::setUrl(const KUrl& url)
495 {
496 updateView(url, KUrl());
497 }
498
499 void DolphinView::changeSelection(const KFileItemList& selection)
500 {
501 clearSelection();
502 if (selection.isEmpty()) {
503 return;
504 }
505 const KUrl& baseUrl = url();
506 KUrl url;
507 QItemSelection new_selection;
508 foreach(const KFileItem& item, selection) {
509 url = item.url().upUrl();
510 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
511 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
512 new_selection.select(index, index);
513 }
514 }
515 itemView()->selectionModel()->select(new_selection,
516 QItemSelectionModel::ClearAndSelect
517 | QItemSelectionModel::Current);
518 }
519
520 void DolphinView::renameSelectedItems()
521 {
522 const KFileItemList items = selectedItems();
523 if (items.count() > 1) {
524 // More than one item has been selected for renaming. Open
525 // a rename dialog and rename all items afterwards.
526 RenameDialog dialog(this, items);
527 if (dialog.exec() == QDialog::Rejected) {
528 return;
529 }
530
531 const QString newName = dialog.newName();
532 if (newName.isEmpty()) {
533 emit errorMessage(dialog.errorString());
534 } else {
535 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
536 // as one operation instead of n rename operations like it is done now...
537 Q_ASSERT(newName.contains('#'));
538
539 // iterate through all selected items and rename them...
540 int index = 1;
541 foreach (const KFileItem &item, items) {
542 const KUrl& oldUrl = item.url();
543 QString number;
544 number.setNum(index++);
545
546 QString name = newName;
547 name.replace('#', number);
548
549 if (oldUrl.fileName() != name) {
550 KUrl newUrl = oldUrl;
551 newUrl.setFileName(name);
552 KonqOperations::rename(this, oldUrl, newUrl);
553 emit doingOperation(KIO::FileUndoManager::Rename);
554 }
555 }
556 }
557 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
558 Q_ASSERT(items.count() == 1);
559
560 if (isColumnViewActive()) {
561 m_columnView->editItem(items.first());
562 } else {
563 const QModelIndex dirIndex = m_dolphinModel->indexForItem(items.first());
564 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
565 itemView()->edit(proxyIndex);
566 }
567 } else {
568 Q_ASSERT(items.count() == 1);
569
570 RenameDialog dialog(this, items);
571 if (dialog.exec() == QDialog::Rejected) {
572 return;
573 }
574
575 const QString& newName = dialog.newName();
576 if (newName.isEmpty()) {
577 emit errorMessage(dialog.errorString());
578 } else {
579 const KUrl& oldUrl = items.first().url();
580 KUrl newUrl = oldUrl;
581 newUrl.setFileName(newName);
582 KonqOperations::rename(this, oldUrl, newUrl);
583 emit doingOperation(KIO::FileUndoManager::Rename);
584 }
585 }
586 }
587
588 void DolphinView::trashSelectedItems()
589 {
590 emit doingOperation(KIO::FileUndoManager::Trash);
591 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
592 }
593
594 void DolphinView::deleteSelectedItems()
595 {
596 const KUrl::List list = selectedUrls();
597 const bool del = KonqOperations::askDeleteConfirmation(list,
598 KonqOperations::DEL,
599 KonqOperations::DEFAULT_CONFIRMATION,
600 this);
601
602 if (del) {
603 KIO::Job* job = KIO::del(list);
604 connect(job, SIGNAL(result(KJob*)),
605 this, SLOT(slotDeleteFileFinished(KJob*)));
606 }
607 }
608
609 void DolphinView::cutSelectedItems()
610 {
611 QMimeData* mimeData = new QMimeData();
612 const KUrl::List kdeUrls = selectedUrls();
613 const KUrl::List mostLocalUrls;
614 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
615 QApplication::clipboard()->setMimeData(mimeData);
616 }
617
618 void DolphinView::copySelectedItems()
619 {
620 QMimeData* mimeData = new QMimeData();
621 const KUrl::List kdeUrls = selectedUrls();
622 const KUrl::List mostLocalUrls;
623 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
624 QApplication::clipboard()->setMimeData(mimeData);
625 }
626
627 void DolphinView::paste()
628 {
629 pasteToUrl(url());
630 }
631
632 void DolphinView::pasteIntoFolder()
633 {
634 const KFileItemList items = selectedItems();
635 if ((items.count() == 1) && items.first().isDir()) {
636 pasteToUrl(items.first().url());
637 }
638 }
639
640 void DolphinView::setShowPreview(bool show)
641 {
642 if (m_showPreview == show) {
643 return;
644 }
645
646 const KUrl viewPropsUrl = viewPropertiesUrl();
647 ViewProperties props(viewPropsUrl);
648 props.setShowPreview(show);
649
650 m_showPreview = show;
651 m_iconManager->setShowPreview(show);
652 emit showPreviewChanged();
653
654 loadDirectory(viewPropsUrl);
655 }
656
657 void DolphinView::setShowHiddenFiles(bool show)
658 {
659 if (m_dirLister->showingDotFiles() == show) {
660 return;
661 }
662
663 const KUrl viewPropsUrl = viewPropertiesUrl();
664 ViewProperties props(viewPropsUrl);
665 props.setShowHiddenFiles(show);
666
667 m_dirLister->setShowingDotFiles(show);
668 emit showHiddenFilesChanged();
669
670 loadDirectory(viewPropsUrl);
671 }
672
673 void DolphinView::setCategorizedSorting(bool categorized)
674 {
675 if (categorized == categorizedSorting()) {
676 return;
677 }
678
679 // setCategorizedSorting(true) may only get invoked
680 // if the view supports categorized sorting
681 Q_ASSERT(!categorized || supportsCategorizedSorting());
682
683 ViewProperties props(viewPropertiesUrl());
684 props.setCategorizedSorting(categorized);
685 props.save();
686
687 m_storedCategorizedSorting = categorized;
688 m_proxyModel->setCategorizedModel(categorized);
689
690 emit categorizedSortingChanged();
691 }
692
693 void DolphinView::toggleSortOrder()
694 {
695 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
696 Qt::DescendingOrder :
697 Qt::AscendingOrder;
698 setSortOrder(order);
699 }
700
701 void DolphinView::toggleAdditionalInfo(QAction* action)
702 {
703 const KFileItemDelegate::Information info =
704 static_cast<KFileItemDelegate::Information>(action->data().toInt());
705
706 KFileItemDelegate::InformationList list = additionalInfo();
707
708 const bool show = action->isChecked();
709
710 const int index = list.indexOf(info);
711 const bool containsInfo = (index >= 0);
712 if (show && !containsInfo) {
713 list.append(info);
714 setAdditionalInfo(list);
715 } else if (!show && containsInfo) {
716 list.removeAt(index);
717 setAdditionalInfo(list);
718 Q_ASSERT(list.indexOf(info) < 0);
719 }
720 }
721
722
723 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
724 {
725 QWidget::mouseReleaseEvent(event);
726 setActive(true);
727 }
728
729 void DolphinView::wheelEvent(QWheelEvent* event)
730 {
731 if (event->modifiers() & Qt::ControlModifier) {
732 const int delta = event->delta();
733 if ((delta > 0) && isZoomInPossible()) {
734 zoomIn();
735 } else if ((delta < 0) && isZoomOutPossible()) {
736 zoomOut();
737 }
738 event->accept();
739 }
740 }
741
742 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
743 {
744 if ((watched == itemView()) && (event->type() == QEvent::FocusIn)) {
745 m_controller->requestActivation();
746 }
747
748 return QWidget::eventFilter(watched, event);
749 }
750
751 void DolphinView::activate()
752 {
753 setActive(true);
754 }
755
756 void DolphinView::triggerItem(const KFileItem& item)
757 {
758 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
759 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
760 // items are selected by the user, hence don't trigger the
761 // item specified by 'index'
762 return;
763 }
764
765 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
766 if (item.isNull() || m_isContextMenuOpen) {
767 return;
768 }
769
770 if (m_toolTipManager != 0) {
771 m_toolTipManager->hideTip();
772 }
773 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
774 }
775
776 void DolphinView::emitSelectionChangedSignal()
777 {
778 emit selectionChanged(DolphinView::selectedItems());
779 }
780
781 void DolphinView::openContextMenu(const QPoint& pos)
782 {
783 KFileItem item;
784
785 const QModelIndex index = itemView()->indexAt(pos);
786 if (index.isValid() && (index.column() == DolphinModel::Name)) {
787 item = fileItem(index);
788 }
789
790 if (m_toolTipManager != 0) {
791 m_toolTipManager->hideTip();
792 }
793
794 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
795 emit requestContextMenu(item, url());
796 m_isContextMenuOpen = false;
797 }
798
799 void DolphinView::dropUrls(const KUrl::List& urls,
800 const KUrl& destPath,
801 const KFileItem& destItem)
802 {
803 Q_ASSERT(!urls.isEmpty());
804 const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
805 destItem.url() : destPath;
806 const KUrl sourceDir = KUrl(urls.first().directory());
807 if (sourceDir != destination) {
808 dropUrls(urls, destination);
809 }
810 }
811
812 void DolphinView::dropUrls(const KUrl::List& urls,
813 const KUrl& destination)
814 {
815 DolphinDropController dropController(this);
816 // forward doingOperation signal up to the mainwindow
817 connect(&dropController, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)),
818 this, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)));
819 dropController.dropUrls(urls, destination);
820 }
821
822 void DolphinView::updateSorting(DolphinView::Sorting sorting)
823 {
824 ViewProperties props(viewPropertiesUrl());
825 props.setSorting(sorting);
826
827 m_proxyModel->setSorting(sorting);
828
829 emit sortingChanged(sorting);
830 }
831
832 void DolphinView::updateSortOrder(Qt::SortOrder order)
833 {
834 ViewProperties props(viewPropertiesUrl());
835 props.setSortOrder(order);
836
837 m_proxyModel->setSortOrder(order);
838
839 emit sortOrderChanged(order);
840 }
841
842 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
843 {
844 ViewProperties props(viewPropertiesUrl());
845 props.setAdditionalInfo(info);
846 props.save();
847
848 m_fileItemDelegate->setShowInformation(info);
849
850 emit additionalInfoChanged();
851 }
852
853 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
854 {
855 const bool enable = (m_mode == DolphinView::DetailsView) ||
856 (m_mode == DolphinView::IconsView);
857
858 QAction* showSizeInfo = collection->action("show_size_info");
859 QAction* showDateInfo = collection->action("show_date_info");
860 QAction* showPermissionsInfo = collection->action("show_permissions_info");
861 QAction* showOwnerInfo = collection->action("show_owner_info");
862 QAction* showGroupInfo = collection->action("show_group_info");
863 QAction* showMimeInfo = collection->action("show_mime_info");
864
865 showSizeInfo->setChecked(false);
866 showDateInfo->setChecked(false);
867 showPermissionsInfo->setChecked(false);
868 showOwnerInfo->setChecked(false);
869 showGroupInfo->setChecked(false);
870 showMimeInfo->setChecked(false);
871
872 showSizeInfo->setEnabled(enable);
873 showDateInfo->setEnabled(enable);
874 showPermissionsInfo->setEnabled(enable);
875 showOwnerInfo->setEnabled(enable);
876 showGroupInfo->setEnabled(enable);
877 showMimeInfo->setEnabled(enable);
878
879 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
880 switch (info) {
881 case KFileItemDelegate::Size:
882 showSizeInfo->setChecked(true);
883 break;
884 case KFileItemDelegate::ModificationTime:
885 showDateInfo->setChecked(true);
886 break;
887 case KFileItemDelegate::Permissions:
888 showPermissionsInfo->setChecked(true);
889 break;
890 case KFileItemDelegate::Owner:
891 showOwnerInfo->setChecked(true);
892 break;
893 case KFileItemDelegate::OwnerAndGroup:
894 showGroupInfo->setChecked(true);
895 break;
896 case KFileItemDelegate::FriendlyMimeType:
897 showMimeInfo->setChecked(true);
898 break;
899 default:
900 break;
901 }
902 }
903 }
904
905 QPair<bool, QString> DolphinView::pasteInfo() const
906 {
907 QPair<bool, QString> ret;
908 QClipboard* clipboard = QApplication::clipboard();
909 const QMimeData* mimeData = clipboard->mimeData();
910
911 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
912 if (!urls.isEmpty()) {
913 ret.first = true;
914 if (urls.count() == 1) {
915 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, urls.first(), true);
916 ret.second = item.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
917 i18nc("@action:inmenu", "Paste One File");
918
919 } else {
920 ret.second = i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls.count());
921 }
922 } else {
923 ret.first = false;
924 ret.second = i18nc("@action:inmenu", "Paste");
925 }
926
927 return ret;
928 }
929
930 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
931 {
932 m_tabsForFiles = tabsForFiles;
933 }
934
935 bool DolphinView::isTabsForFilesEnabled() const
936 {
937 return m_tabsForFiles;
938 }
939
940 void DolphinView::emitContentsMoved()
941 {
942 // only emit the contents moved signal if:
943 // - no directory loading is ongoing (this would reset the contents position
944 // always to (0, 0))
945 // - if the Column View is active: the column view does an automatic
946 // positioning during the loading operation, which must be remembered
947 if (!m_loadingDirectory || isColumnViewActive()) {
948 const QPoint pos(contentsPosition());
949 emit contentsMoved(pos.x(), pos.y());
950 }
951 }
952
953 void DolphinView::showHoverInformation(const KFileItem& item)
954 {
955 emit requestItemInfo(item);
956 }
957
958 void DolphinView::clearHoverInformation()
959 {
960 emit requestItemInfo(KFileItem());
961 }
962
963 void DolphinView::slotDeleteFileFinished(KJob* job)
964 {
965 if (job->error() == 0) {
966 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
967 } else {
968 emit errorMessage(job->errorString());
969 }
970 }
971
972 void DolphinView::loadDirectory(const KUrl& url, bool reload)
973 {
974 if (!url.isValid()) {
975 const QString location(url.pathOrUrl());
976 if (location.isEmpty()) {
977 emit errorMessage(i18nc("@info:status", "The location is empty."));
978 } else {
979 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
980 }
981 return;
982 }
983
984 m_loadingDirectory = true;
985
986 m_dirLister->stop();
987 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
988
989 if (isColumnViewActive()) {
990 // adjusting the directory lister is not enough in the case of the
991 // column view, as each column has its own directory lister internally...
992 if (reload) {
993 m_columnView->reload();
994 } else {
995 m_columnView->showColumn(url);
996 }
997 }
998 }
999
1000 KUrl DolphinView::viewPropertiesUrl() const
1001 {
1002 if (isColumnViewActive()) {
1003 return m_columnView->rootUrl();
1004 }
1005
1006 return url();
1007 }
1008
1009 void DolphinView::applyViewProperties(const KUrl& url)
1010 {
1011 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
1012 // The column view is active, hence don't apply the view properties
1013 // of sub directories (represented by columns) to the view. The
1014 // view always represents the properties of the first column.
1015 return;
1016 }
1017
1018 const ViewProperties props(url);
1019
1020 const Mode mode = props.viewMode();
1021 if (m_mode != mode) {
1022 m_mode = mode;
1023 createView();
1024 emit modeChanged();
1025 }
1026 if (itemView() == 0) {
1027 createView();
1028 }
1029 Q_ASSERT(itemView() != 0);
1030 Q_ASSERT(m_fileItemDelegate != 0);
1031
1032 const bool showHiddenFiles = props.showHiddenFiles();
1033 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
1034 m_dirLister->setShowingDotFiles(showHiddenFiles);
1035 emit showHiddenFilesChanged();
1036 }
1037
1038 m_storedCategorizedSorting = props.categorizedSorting();
1039 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1040 if (categorized != m_proxyModel->isCategorizedModel()) {
1041 m_proxyModel->setCategorizedModel(categorized);
1042 emit categorizedSortingChanged();
1043 }
1044
1045 const DolphinView::Sorting sorting = props.sorting();
1046 if (sorting != m_proxyModel->sorting()) {
1047 m_proxyModel->setSorting(sorting);
1048 emit sortingChanged(sorting);
1049 }
1050
1051 const Qt::SortOrder sortOrder = props.sortOrder();
1052 if (sortOrder != m_proxyModel->sortOrder()) {
1053 m_proxyModel->setSortOrder(sortOrder);
1054 emit sortOrderChanged(sortOrder);
1055 }
1056
1057 KFileItemDelegate::InformationList info = props.additionalInfo();
1058 if (info != m_fileItemDelegate->showInformation()) {
1059 m_fileItemDelegate->setShowInformation(info);
1060 emit additionalInfoChanged();
1061 }
1062
1063 const bool showPreview = props.showPreview();
1064 if (showPreview != m_showPreview) {
1065 m_showPreview = showPreview;
1066 m_iconManager->setShowPreview(showPreview);
1067 emit showPreviewChanged();
1068 }
1069 }
1070
1071 void DolphinView::createView()
1072 {
1073 deleteView();
1074 Q_ASSERT(m_iconsView == 0);
1075 Q_ASSERT(m_detailsView == 0);
1076 Q_ASSERT(m_columnView == 0);
1077
1078 QAbstractItemView* view = 0;
1079 switch (m_mode) {
1080 case IconsView: {
1081 m_iconsView = new DolphinIconsView(this, m_controller);
1082 view = m_iconsView;
1083 break;
1084 }
1085
1086 case DetailsView:
1087 m_detailsView = new DolphinDetailsView(this, m_controller);
1088 view = m_detailsView;
1089 break;
1090
1091 case ColumnView:
1092 m_columnView = new DolphinColumnView(this, m_controller);
1093 view = m_columnView;
1094 break;
1095 }
1096
1097 Q_ASSERT(view != 0);
1098 view->installEventFilter(this);
1099
1100 m_controller->setItemView(view);
1101
1102 m_fileItemDelegate = new KFileItemDelegate(view);
1103 view->setItemDelegate(m_fileItemDelegate);
1104
1105 view->setModel(m_proxyModel);
1106 if (m_selectionModel != 0) {
1107 view->setSelectionModel(m_selectionModel);
1108 } else {
1109 m_selectionModel = view->selectionModel();
1110 }
1111
1112 // reparent the selection model, as it should not be deleted
1113 // when deleting the model
1114 m_selectionModel->setParent(this);
1115
1116 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1117
1118 new KMimeTypeResolver(view, m_dolphinModel);
1119 m_iconManager = new IconManager(view, m_proxyModel);
1120 m_iconManager->setShowPreview(m_showPreview);
1121
1122 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1123 m_toolTipManager = new ToolTipManager(view, m_proxyModel);
1124 }
1125
1126 m_topLayout->insertWidget(1, view);
1127
1128 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1129 this, SLOT(emitSelectionChangedSignal()));
1130 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1131 this, SLOT(emitContentsMoved()));
1132 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1133 this, SLOT(emitContentsMoved()));
1134 }
1135
1136 void DolphinView::deleteView()
1137 {
1138 QAbstractItemView* view = itemView();
1139 if (view != 0) {
1140 m_topLayout->removeWidget(view);
1141 view->close();
1142 view->deleteLater();
1143 view = 0;
1144 m_iconsView = 0;
1145 m_detailsView = 0;
1146 m_columnView = 0;
1147 m_fileItemDelegate = 0;
1148 m_iconManager = 0;
1149 m_toolTipManager = 0;
1150 }
1151 }
1152
1153 QAbstractItemView* DolphinView::itemView() const
1154 {
1155 if (m_detailsView != 0) {
1156 return m_detailsView;
1157 } else if (m_columnView != 0) {
1158 return m_columnView;
1159 }
1160
1161 return m_iconsView;
1162 }
1163
1164 bool DolphinView::isCutItem(const KFileItem& item) const
1165 {
1166 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1167 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1168
1169 const KUrl& itemUrl = item.url();
1170 KUrl::List::const_iterator it = cutUrls.begin();
1171 const KUrl::List::const_iterator end = cutUrls.end();
1172 while (it != end) {
1173 if (*it == itemUrl) {
1174 return true;
1175 }
1176 ++it;
1177 }
1178
1179 return false;
1180 }
1181
1182 void DolphinView::pasteToUrl(const KUrl& url)
1183 {
1184 QClipboard* clipboard = QApplication::clipboard();
1185 const QMimeData* mimeData = clipboard->mimeData();
1186
1187 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1188 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1189 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, url);
1190 emit doingOperation(KIO::FileUndoManager::Move);
1191 clipboard->clear();
1192 } else {
1193 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, url);
1194 emit doingOperation(KIO::FileUndoManager::Copy);
1195 }
1196 }
1197
1198 #include "dolphinview.moc"