]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
assure that clipboard items don't get selected when changing the URL
[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 <kfilepreviewgenerator.h>
35 #include <kiconeffect.h>
36 #include <kfileitem.h>
37 #include <klocale.h>
38 #include <kio/deletejob.h>
39 #include <kio/netaccess.h>
40 #include <kio/previewjob.h>
41 #include <kjob.h>
42 #include <kmenu.h>
43 #include <kmessagebox.h>
44 #include <kmimetyperesolver.h>
45 #include <konq_fileitemcapabilities.h>
46 #include <konq_operations.h>
47 #include <konqmimedata.h>
48 #include <kstringhandler.h>
49 #include <ktoggleaction.h>
50 #include <kurl.h>
51
52 #include "dolphinmodel.h"
53 #include "dolphincolumnview.h"
54 #include "dolphincontroller.h"
55 #include "dolphindetailsview.h"
56 #include "dolphinfileitemdelegate.h"
57 #include "dolphinnewmenuobserver.h"
58 #include "dolphinsortfilterproxymodel.h"
59 #include "dolphin_detailsmodesettings.h"
60 #include "dolphiniconsview.h"
61 #include "dolphin_generalsettings.h"
62 #include "draganddrophelper.h"
63 #include "folderexpander.h"
64 #include "renamedialog.h"
65 #include "tooltips/tooltipmanager.h"
66 #include "settings/dolphinsettings.h"
67 #include "viewproperties.h"
68 #include "zoomlevelinfo.h"
69
70 /**
71 * Helper function for sorting items with qSort() in
72 * DolphinView::renameSelectedItems().
73 */
74 bool lessThan(const KFileItem& item1, const KFileItem& item2)
75 {
76 return KStringHandler::naturalCompare(item1.name(), item2.name()) < 0;
77 }
78
79 DolphinView::DolphinView(QWidget* parent,
80 const KUrl& url,
81 KDirLister* dirLister,
82 DolphinModel* dolphinModel,
83 DolphinSortFilterProxyModel* proxyModel) :
84 QWidget(parent),
85 m_active(true),
86 m_showPreview(false),
87 m_loadingDirectory(false),
88 m_storedCategorizedSorting(false),
89 m_tabsForFiles(false),
90 m_isContextMenuOpen(false),
91 m_ignoreViewProperties(false),
92 m_assureVisibleCurrentIndex(false),
93 m_selectClipboardItems(false),
94 m_mode(DolphinView::IconsView),
95 m_topLayout(0),
96 m_controller(0),
97 m_iconsView(0),
98 m_detailsView(0),
99 m_columnView(0),
100 m_fileItemDelegate(0),
101 m_selectionModel(0),
102 m_dolphinModel(dolphinModel),
103 m_dirLister(dirLister),
104 m_proxyModel(proxyModel),
105 m_previewGenerator(0),
106 m_toolTipManager(0),
107 m_rootUrl(),
108 m_currentItemUrl(),
109 m_createdItemUrl(),
110 m_selectedItems(),
111 m_expandedDragSource(0)
112 {
113 m_topLayout = new QVBoxLayout(this);
114 m_topLayout->setSpacing(0);
115 m_topLayout->setMargin(0);
116
117 m_controller = new DolphinController(this);
118 m_controller->setUrl(url);
119
120 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
121 this, SIGNAL(urlChanged(const KUrl&)));
122 connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
123 this, SLOT(slotRequestUrlChange(const KUrl&)));
124
125 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&, const QList<QAction*>&)),
126 this, SLOT(openContextMenu(const QPoint&, const QList<QAction*>&)));
127 connect(m_controller, SIGNAL(urlsDropped(const KFileItem&, const KUrl&, QDropEvent*)),
128 this, SLOT(dropUrls(const KFileItem&, const KUrl&, QDropEvent*)));
129 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
130 this, SLOT(updateSorting(DolphinView::Sorting)));
131 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
132 this, SLOT(updateSortOrder(Qt::SortOrder)));
133 connect(m_controller, SIGNAL(sortFoldersFirstChanged(bool)),
134 this, SLOT(updateSortFoldersFirst(bool)));
135 connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
136 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
137 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
138 this, SLOT(triggerItem(const KFileItem&)));
139 connect(m_controller, SIGNAL(tabRequested(const KUrl&)),
140 this, SIGNAL(tabRequested(const KUrl&)));
141 connect(m_controller, SIGNAL(activated()),
142 this, SLOT(activate()));
143 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
144 this, SLOT(showHoverInformation(const KFileItem&)));
145 connect(m_controller, SIGNAL(viewportEntered()),
146 this, SLOT(clearHoverInformation()));
147
148 connect(m_dirLister, SIGNAL(redirection(KUrl, KUrl)),
149 this, SIGNAL(redirection(KUrl, KUrl)));
150 connect(m_dirLister, SIGNAL(completed()),
151 this, SLOT(slotDirListerCompleted()));
152 connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
153 this, SLOT(slotRefreshItems()));
154
155 // When a new item has been created by the "Create New..." menu, the item should
156 // get selected and it must be assured that the item will get visible. As the
157 // creation is done asynchronously, several signals must be checked:
158 connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl&)),
159 this, SLOT(observeCreatedItem(const KUrl&)));
160
161 // when a copy/move-operation has been finished, the pasted items should get selected
162 connect(KIO::FileUndoManager::self(), SIGNAL(jobRecordingFinished(CommandType)),
163 this, SLOT(slotJobRecordingFinished(CommandType)));
164
165 applyViewProperties(url);
166 m_topLayout->addWidget(itemView());
167 }
168
169 DolphinView::~DolphinView()
170 {
171 delete m_expandedDragSource;
172 m_expandedDragSource = 0;
173 }
174
175 const KUrl& DolphinView::url() const
176 {
177 return m_controller->url();
178 }
179
180 KUrl DolphinView::rootUrl() const
181 {
182 return isColumnViewActive() ? m_columnView->rootUrl() : url();
183 }
184
185 void DolphinView::setActive(bool active)
186 {
187 if (active == m_active) {
188 return;
189 }
190
191 m_active = active;
192
193 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
194 if (active) {
195 emit selectionChanged(selectedItems());
196 } else {
197 color.setAlpha(150);
198 }
199
200 QWidget* viewport = itemView()->viewport();
201 QPalette palette;
202 palette.setColor(viewport->backgroundRole(), color);
203 viewport->setPalette(palette);
204
205 update();
206
207 if (active) {
208 itemView()->setFocus();
209 emit activated();
210 }
211
212 m_controller->indicateActivationChange(active);
213 }
214
215 bool DolphinView::isActive() const
216 {
217 return m_active;
218 }
219
220 void DolphinView::setMode(Mode mode)
221 {
222 if (mode == m_mode) {
223 return; // the wished mode is already set
224 }
225
226 const int oldZoomLevel = m_controller->zoomLevel();
227 m_mode = mode;
228
229 deleteView();
230
231 const KUrl viewPropsUrl = viewPropertiesUrl();
232 ViewProperties props(viewPropsUrl);
233 props.setViewMode(m_mode);
234 createView();
235
236 // the file item delegate has been recreated, apply the current
237 // additional information manually
238 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
239 m_fileItemDelegate->setShowInformation(infoList);
240 emit additionalInfoChanged();
241
242 // Not all view modes support categorized sorting. Adjust the sorting model
243 // if changing the view mode results in a change of the categorized sorting
244 // capabilities.
245 m_storedCategorizedSorting = props.categorizedSorting();
246 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
247 if (categorized != m_proxyModel->isCategorizedModel()) {
248 m_proxyModel->setCategorizedModel(categorized);
249 emit categorizedSortingChanged();
250 }
251
252 emit modeChanged();
253
254 updateZoomLevel(oldZoomLevel);
255 if (m_showPreview) {
256 loadDirectory(viewPropsUrl);
257 }
258 }
259
260 DolphinView::Mode DolphinView::mode() const
261 {
262 return m_mode;
263 }
264
265 bool DolphinView::showPreview() const
266 {
267 return m_showPreview;
268 }
269
270 bool DolphinView::showHiddenFiles() const
271 {
272 return m_dirLister->showingDotFiles();
273 }
274
275 bool DolphinView::categorizedSorting() const
276 {
277 // If all view modes would support categorized sorting, returning
278 // m_proxyModel->isCategorizedModel() would be the way to go. As
279 // currently only the icons view supports caterized sorting, we remember
280 // the stored view properties state in m_storedCategorizedSorting and
281 // return this state. The application takes care to disable the corresponding
282 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
283 // that this setting is not applied to the current view mode.
284 return m_storedCategorizedSorting;
285 }
286
287 bool DolphinView::supportsCategorizedSorting() const
288 {
289 return m_iconsView != 0;
290 }
291
292 void DolphinView::selectAll()
293 {
294 QAbstractItemView* view = itemView();
295 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
296 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
297 // selection instead of selecting all items. This is bypassed for KDE 4.0
298 // by invoking clearSelection() first.
299 view->clearSelection();
300 view->selectAll();
301 }
302
303 void DolphinView::invertSelection()
304 {
305 if (isColumnViewActive()) {
306 // QAbstractItemView does not offer a virtual method invertSelection()
307 // as counterpart to QAbstractItemView::selectAll(). This makes it
308 // necessary to delegate the inverting of the selection to the
309 // column view, as only the selection of the active column should
310 // get inverted.
311 m_columnView->invertSelection();
312 } else {
313 QItemSelectionModel* selectionModel = itemView()->selectionModel();
314 const QAbstractItemModel* itemModel = selectionModel->model();
315
316 const QModelIndex topLeft = itemModel->index(0, 0);
317 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
318 itemModel->columnCount() - 1);
319
320 const QItemSelection selection(topLeft, bottomRight);
321 selectionModel->select(selection, QItemSelectionModel::Toggle);
322 }
323 }
324
325 bool DolphinView::hasSelection() const
326 {
327 return itemView()->selectionModel()->hasSelection();
328 }
329
330 void DolphinView::clearSelection()
331 {
332 QItemSelectionModel* selModel = itemView()->selectionModel();
333 const QModelIndex currentIndex = selModel->currentIndex();
334 selModel->setCurrentIndex(currentIndex, QItemSelectionModel::Current |
335 QItemSelectionModel::Clear);
336 }
337
338 KFileItemList DolphinView::selectedItems() const
339 {
340 if (isColumnViewActive()) {
341 return m_columnView->selectedItems();
342 }
343
344 const QAbstractItemView* view = itemView();
345
346 // Our view has a selection, we will map them back to the DolphinModel
347 // and then fill the KFileItemList.
348 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
349
350 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
351 KFileItemList itemList;
352
353 const QModelIndexList indexList = selection.indexes();
354 foreach (const QModelIndex &index, indexList) {
355 KFileItem item = m_dolphinModel->itemForIndex(index);
356 if (!item.isNull()) {
357 itemList.append(item);
358 }
359 }
360
361 return itemList;
362 }
363
364 KUrl::List DolphinView::selectedUrls() const
365 {
366 KUrl::List urls;
367 const KFileItemList list = selectedItems();
368 foreach (const KFileItem &item, list) {
369 urls.append(item.url());
370 }
371 return urls;
372 }
373
374 int DolphinView::selectedItemsCount() const
375 {
376 if (isColumnViewActive()) {
377 // TODO: get rid of this special case by adjusting the dir lister
378 // to the current column
379 return m_columnView->selectedItems().count();
380 }
381
382 return itemView()->selectionModel()->selection().count();
383 }
384
385 void DolphinView::setContentsPosition(int x, int y)
386 {
387 QAbstractItemView* view = itemView();
388
389 // the ColumnView takes care itself for the horizontal scrolling
390 if (!isColumnViewActive()) {
391 view->horizontalScrollBar()->setValue(x);
392 }
393 view->verticalScrollBar()->setValue(y);
394
395 m_loadingDirectory = false;
396 }
397
398 QPoint DolphinView::contentsPosition() const
399 {
400 const int x = itemView()->horizontalScrollBar()->value();
401 const int y = itemView()->verticalScrollBar()->value();
402 return QPoint(x, y);
403 }
404
405 void DolphinView::setZoomLevel(int level)
406 {
407 if (level < ZoomLevelInfo::minimumLevel()) {
408 level = ZoomLevelInfo::minimumLevel();
409 } else if (level > ZoomLevelInfo::maximumLevel()) {
410 level = ZoomLevelInfo::maximumLevel();
411 }
412
413 if (level != zoomLevel()) {
414 m_controller->setZoomLevel(level);
415 m_previewGenerator->updateIcons();
416 emit zoomLevelChanged(level);
417 }
418 }
419
420 int DolphinView::zoomLevel() const
421 {
422 return m_controller->zoomLevel();
423 }
424
425 void DolphinView::setSorting(Sorting sorting)
426 {
427 if (sorting != this->sorting()) {
428 updateSorting(sorting);
429 }
430 }
431
432 DolphinView::Sorting DolphinView::sorting() const
433 {
434 return m_proxyModel->sorting();
435 }
436
437 void DolphinView::setSortOrder(Qt::SortOrder order)
438 {
439 if (sortOrder() != order) {
440 updateSortOrder(order);
441 }
442 }
443
444 Qt::SortOrder DolphinView::sortOrder() const
445 {
446 return m_proxyModel->sortOrder();
447 }
448
449 void DolphinView::setSortFoldersFirst(bool foldersFirst)
450 {
451 if (sortFoldersFirst() != foldersFirst) {
452 updateSortFoldersFirst(foldersFirst);
453 }
454 }
455
456 bool DolphinView::sortFoldersFirst() const
457 {
458 return m_proxyModel->sortFoldersFirst();
459 }
460
461 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
462 {
463 const KUrl viewPropsUrl = viewPropertiesUrl();
464 ViewProperties props(viewPropsUrl);
465 props.setAdditionalInfo(info);
466 m_fileItemDelegate->setShowInformation(info);
467
468 emit additionalInfoChanged();
469
470 if (itemView() != m_detailsView) {
471 // the details view requires no reloading of the directory, as it maps
472 // the file item delegate info to its columns internally
473 loadDirectory(viewPropsUrl);
474 }
475 }
476
477 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
478 {
479 return m_fileItemDelegate->showInformation();
480 }
481
482 void DolphinView::reload()
483 {
484 setUrl(url());
485 loadDirectory(url(), true);
486 }
487
488 void DolphinView::refresh()
489 {
490 m_ignoreViewProperties = false;
491
492 const bool oldActivationState = m_active;
493 const int oldZoomLevel = m_controller->zoomLevel();
494 m_active = true;
495
496 createView();
497 applyViewProperties(m_controller->url());
498 reload();
499
500 setActive(oldActivationState);
501 updateZoomLevel(oldZoomLevel);
502 }
503
504 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
505 {
506 if (m_controller->url() == url) {
507 return;
508 }
509
510 m_previewGenerator->cancelPreviews();
511 m_controller->setUrl(url); // emits urlChanged, which we forward
512
513 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
514 applyViewProperties(rootUrl);
515 loadDirectory(rootUrl);
516 if (itemView() == m_columnView) {
517 m_columnView->setRootUrl(rootUrl);
518 m_columnView->showColumn(url);
519 }
520 } else {
521 applyViewProperties(url);
522 loadDirectory(url);
523 }
524
525 emit startedPathLoading(url);
526 }
527
528 void DolphinView::setNameFilter(const QString& nameFilter)
529 {
530 m_proxyModel->setFilterRegExp(nameFilter);
531
532 if (isColumnViewActive()) {
533 // adjusting the directory lister is not enough in the case of the
534 // column view, as each column has its own directory lister internally...
535 m_columnView->setNameFilter(nameFilter);
536 }
537 }
538
539 void DolphinView::calculateItemCount(int& fileCount,
540 int& folderCount,
541 KIO::filesize_t& totalFileSize) const
542 {
543 foreach (const KFileItem& item, m_dirLister->items()) {
544 if (item.isDir()) {
545 ++folderCount;
546 } else {
547 ++fileCount;
548 totalFileSize += item.size();
549 }
550 }
551 }
552
553 QString DolphinView::statusBarText() const
554 {
555 QString text;
556 int folderCount = 0;
557 int fileCount = 0;
558 KIO::filesize_t totalFileSize = 0;
559
560 if (hasSelection()) {
561 // give a summary of the status of the selected files
562 const KFileItemList list = selectedItems();
563 if (list.isEmpty()) {
564 // when an item is triggered, it is temporary selected but selectedItems()
565 // will return an empty list
566 return text;
567 }
568
569 KFileItemList::const_iterator it = list.begin();
570 const KFileItemList::const_iterator end = list.end();
571 while (it != end) {
572 const KFileItem& item = *it;
573 if (item.isDir()) {
574 ++folderCount;
575 } else {
576 ++fileCount;
577 totalFileSize += item.size();
578 }
579 ++it;
580 }
581
582 if (folderCount + fileCount == 1) {
583 // if only one item is selected, show the filename
584 const QString name = list.first().name();
585 text = (folderCount == 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name) :
586 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
587 name, KIO::convertSize(totalFileSize));
588 } else {
589 // at least 2 items are selected
590 const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
591 const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
592 if ((folderCount > 0) && (fileCount > 0)) {
593 text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
594 foldersText, filesText, KIO::convertSize(totalFileSize));
595 } else if (fileCount > 0) {
596 text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize));
597 } else {
598 Q_ASSERT(folderCount > 0);
599 text = foldersText;
600 }
601 }
602 } else {
603 calculateItemCount(fileCount, folderCount, totalFileSize);
604 text = KIO::itemsSummaryString(fileCount + folderCount,
605 fileCount, folderCount,
606 totalFileSize, true);
607 }
608
609 return text;
610 }
611
612 void DolphinView::setUrl(const KUrl& url)
613 {
614 // remember current item candidate (see slotDirListerCompleted())
615 m_selectClipboardItems = false;
616 m_currentItemUrl = url;
617 updateView(url, KUrl());
618 }
619
620 void DolphinView::changeSelection(const KFileItemList& selection)
621 {
622 clearSelection();
623 if (selection.isEmpty()) {
624 return;
625 }
626 const KUrl& baseUrl = url();
627 KUrl url;
628 QItemSelection newSelection;
629 foreach(const KFileItem& item, selection) {
630 url = item.url().upUrl();
631 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
632 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
633 newSelection.select(index, index);
634 }
635 }
636 itemView()->selectionModel()->select(newSelection,
637 QItemSelectionModel::ClearAndSelect
638 | QItemSelectionModel::Current);
639 }
640
641 void DolphinView::renameSelectedItems()
642 {
643 KFileItemList items = selectedItems();
644 const int itemCount = items.count();
645 if (itemCount < 1) {
646 return;
647 }
648
649 if (itemCount > 1) {
650 // More than one item has been selected for renaming. Open
651 // a rename dialog and rename all items afterwards.
652 RenameDialog dialog(this, items);
653 if (dialog.exec() == QDialog::Rejected) {
654 return;
655 }
656
657 const QString newName = dialog.newName();
658 if (newName.isEmpty()) {
659 emit errorMessage(dialog.errorString());
660 return;
661 }
662
663 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
664 // as one operation instead of n rename operations like it is done now...
665 Q_ASSERT(newName.contains('#'));
666
667 // currently the items are sorted by the selection order, resort
668 // them by the file name
669 qSort(items.begin(), items.end(), lessThan);
670
671 // iterate through all selected items and rename them...
672 int index = 1;
673 foreach (const KFileItem& item, items) {
674 const KUrl& oldUrl = item.url();
675 QString number;
676 number.setNum(index++);
677
678 QString name = newName;
679 name.replace('#', number);
680
681 if (oldUrl.fileName() != name) {
682 KUrl newUrl = oldUrl;
683 newUrl.setFileName(name);
684 KonqOperations::rename(this, oldUrl, newUrl);
685 }
686 }
687 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
688 Q_ASSERT(itemCount == 1);
689
690 if (isColumnViewActive()) {
691 m_columnView->editItem(items.first());
692 } else {
693 const QModelIndex dirIndex = m_dolphinModel->indexForItem(items.first());
694 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
695 itemView()->edit(proxyIndex);
696 }
697 } else {
698 Q_ASSERT(itemCount == 1);
699
700 RenameDialog dialog(this, items);
701 if (dialog.exec() == QDialog::Rejected) {
702 return;
703 }
704
705 const QString& newName = dialog.newName();
706 if (newName.isEmpty()) {
707 emit errorMessage(dialog.errorString());
708 return;
709 }
710
711 const KUrl& oldUrl = items.first().url();
712 KUrl newUrl = oldUrl;
713 newUrl.setFileName(newName);
714 KonqOperations::rename(this, oldUrl, newUrl);
715 }
716
717 // assure that the current index remains visible when KDirLister
718 // will notify the view about changed items
719 m_assureVisibleCurrentIndex = true;
720 }
721
722 void DolphinView::trashSelectedItems()
723 {
724 const KUrl::List list = simplifiedSelectedUrls();
725 KonqOperations::del(this, KonqOperations::TRASH, list);
726 }
727
728 void DolphinView::deleteSelectedItems()
729 {
730 const KUrl::List list = simplifiedSelectedUrls();
731 const bool del = KonqOperations::askDeleteConfirmation(list,
732 KonqOperations::DEL,
733 KonqOperations::DEFAULT_CONFIRMATION,
734 this);
735
736 if (del) {
737 KIO::Job* job = KIO::del(list);
738 connect(job, SIGNAL(result(KJob*)),
739 this, SLOT(slotDeleteFileFinished(KJob*)));
740 }
741 }
742
743 void DolphinView::cutSelectedItems()
744 {
745 QMimeData* mimeData = selectionMimeData();
746 KonqMimeData::addIsCutSelection(mimeData, true);
747 QApplication::clipboard()->setMimeData(mimeData);
748 }
749
750 void DolphinView::copySelectedItems()
751 {
752 QMimeData* mimeData = selectionMimeData();
753 QApplication::clipboard()->setMimeData(mimeData);
754 }
755
756 void DolphinView::paste()
757 {
758 pasteToUrl(url());
759 }
760
761 void DolphinView::pasteIntoFolder()
762 {
763 const KFileItemList items = selectedItems();
764 if ((items.count() == 1) && items.first().isDir()) {
765 pasteToUrl(items.first().url());
766 }
767 }
768
769 void DolphinView::setShowPreview(bool show)
770 {
771 if (m_showPreview == show) {
772 return;
773 }
774
775 const KUrl viewPropsUrl = viewPropertiesUrl();
776 ViewProperties props(viewPropsUrl);
777 props.setShowPreview(show);
778
779 m_showPreview = show;
780 m_previewGenerator->setPreviewShown(show);
781
782 const int oldZoomLevel = m_controller->zoomLevel();
783 emit showPreviewChanged();
784
785 // Enabling or disabling the preview might change the icon size of the view.
786 // As the view does not emit a signal when the icon size has been changed,
787 // the used zoom level of the controller must be adjusted manually:
788 updateZoomLevel(oldZoomLevel);
789
790 loadDirectory(viewPropsUrl);
791 }
792
793 void DolphinView::setShowHiddenFiles(bool show)
794 {
795 if (m_dirLister->showingDotFiles() == show) {
796 return;
797 }
798
799 const KUrl viewPropsUrl = viewPropertiesUrl();
800 ViewProperties props(viewPropsUrl);
801 props.setShowHiddenFiles(show);
802
803 m_dirLister->setShowingDotFiles(show);
804 emit showHiddenFilesChanged();
805
806 loadDirectory(viewPropsUrl);
807 }
808
809 void DolphinView::setCategorizedSorting(bool categorized)
810 {
811 if (categorized == categorizedSorting()) {
812 return;
813 }
814
815 // setCategorizedSorting(true) may only get invoked
816 // if the view supports categorized sorting
817 Q_ASSERT(!categorized || supportsCategorizedSorting());
818
819 ViewProperties props(viewPropertiesUrl());
820 props.setCategorizedSorting(categorized);
821 props.save();
822
823 m_storedCategorizedSorting = categorized;
824 m_proxyModel->setCategorizedModel(categorized);
825
826 emit categorizedSortingChanged();
827 }
828
829 void DolphinView::toggleSortOrder()
830 {
831 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
832 Qt::DescendingOrder :
833 Qt::AscendingOrder;
834 setSortOrder(order);
835 }
836
837 void DolphinView::toggleSortFoldersFirst()
838 {
839 setSortFoldersFirst(!sortFoldersFirst());
840 }
841
842 void DolphinView::toggleAdditionalInfo(QAction* action)
843 {
844 const KFileItemDelegate::Information info =
845 static_cast<KFileItemDelegate::Information>(action->data().toInt());
846
847 KFileItemDelegate::InformationList list = additionalInfo();
848
849 const bool show = action->isChecked();
850
851 const int index = list.indexOf(info);
852 const bool containsInfo = (index >= 0);
853 if (show && !containsInfo) {
854 list.append(info);
855 setAdditionalInfo(list);
856 } else if (!show && containsInfo) {
857 list.removeAt(index);
858 setAdditionalInfo(list);
859 Q_ASSERT(list.indexOf(info) < 0);
860 }
861 }
862
863 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
864 {
865 QWidget::mouseReleaseEvent(event);
866 setActive(true);
867 }
868
869 void DolphinView::wheelEvent(QWheelEvent* event)
870 {
871 if (event->modifiers() & Qt::ControlModifier) {
872 const int delta = event->delta();
873 const int level = zoomLevel();
874 if (delta > 0) {
875 setZoomLevel(level + 1);
876 } else if (delta < 0) {
877 setZoomLevel(level - 1);
878 }
879 event->accept();
880 }
881 }
882
883 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
884 {
885 switch (event->type()) {
886 case QEvent::FocusIn:
887 if (watched == itemView()) {
888 m_controller->requestActivation();
889 }
890 break;
891
892 case QEvent::MouseButtonPress:
893 if ((watched == itemView()->viewport()) && (m_expandedDragSource != 0)) {
894 // Listening to a mousebutton press event to delete expanded views is a
895 // workaround, as it seems impossible for the FolderExpander to know when
896 // a dragging outside a view has been finished. However it works quite well:
897 // A mousebutton press event indicates that a drag operation must be
898 // finished already.
899 m_expandedDragSource->deleteLater();
900 m_expandedDragSource = 0;
901 }
902 break;
903
904 case QEvent::DragEnter:
905 if (watched == itemView()->viewport()) {
906 setActive(true);
907 }
908 break;
909
910 case QEvent::KeyPress:
911 if (watched == itemView()) {
912 if (m_toolTipManager != 0) {
913 m_toolTipManager->hideTip();
914 }
915
916 // clear the selection when Escape has been pressed
917 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
918 if (keyEvent->key() == Qt::Key_Escape) {
919 clearSelection();
920 }
921 }
922 break;
923
924 default:
925 break;
926 }
927
928 return QWidget::eventFilter(watched, event);
929 }
930
931 void DolphinView::activate()
932 {
933 setActive(true);
934 }
935
936 void DolphinView::triggerItem(const KFileItem& item)
937 {
938 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
939 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
940 // items are selected by the user, hence don't trigger the
941 // item specified by 'index'
942 return;
943 }
944
945 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
946 if (item.isNull() || m_isContextMenuOpen) {
947 return;
948 }
949
950 if (m_toolTipManager != 0) {
951 m_toolTipManager->hideTip();
952 }
953 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
954 }
955
956 void DolphinView::emitSelectionChangedSignal()
957 {
958 emit selectionChanged(DolphinView::selectedItems());
959 }
960
961 void DolphinView::openContextMenu(const QPoint& pos,
962 const QList<QAction*>& customActions)
963 {
964 KFileItem item;
965 if (isColumnViewActive()) {
966 item = m_columnView->itemAt(pos);
967 } else {
968 const QModelIndex index = itemView()->indexAt(pos);
969 if (index.isValid() && (index.column() == DolphinModel::Name)) {
970 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
971 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
972 }
973 }
974
975 if (m_toolTipManager != 0) {
976 m_toolTipManager->hideTip();
977 }
978
979 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
980 emit requestContextMenu(item, url(), customActions);
981 m_isContextMenuOpen = false;
982 }
983
984 void DolphinView::dropUrls(const KFileItem& destItem,
985 const KUrl& destPath,
986 QDropEvent* event)
987 {
988 DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
989 }
990
991 void DolphinView::updateSorting(DolphinView::Sorting sorting)
992 {
993 ViewProperties props(viewPropertiesUrl());
994 props.setSorting(sorting);
995
996 m_proxyModel->setSorting(sorting);
997
998 emit sortingChanged(sorting);
999 }
1000
1001 void DolphinView::updateSortOrder(Qt::SortOrder order)
1002 {
1003 ViewProperties props(viewPropertiesUrl());
1004 props.setSortOrder(order);
1005
1006 m_proxyModel->setSortOrder(order);
1007
1008 emit sortOrderChanged(order);
1009 }
1010
1011 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
1012 {
1013 ViewProperties props(viewPropertiesUrl());
1014 props.setSortFoldersFirst(foldersFirst);
1015
1016 m_proxyModel->setSortFoldersFirst(foldersFirst);
1017
1018 emit sortFoldersFirstChanged(foldersFirst);
1019 }
1020
1021 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
1022 {
1023 ViewProperties props(viewPropertiesUrl());
1024 props.setAdditionalInfo(info);
1025 props.save();
1026
1027 m_fileItemDelegate->setShowInformation(info);
1028
1029 emit additionalInfoChanged();
1030 }
1031
1032 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
1033 {
1034 const bool enable = (m_mode == DolphinView::DetailsView) ||
1035 (m_mode == DolphinView::IconsView);
1036
1037 QAction* showSizeInfo = collection->action("show_size_info");
1038 QAction* showDateInfo = collection->action("show_date_info");
1039 QAction* showPermissionsInfo = collection->action("show_permissions_info");
1040 QAction* showOwnerInfo = collection->action("show_owner_info");
1041 QAction* showGroupInfo = collection->action("show_group_info");
1042 QAction* showMimeInfo = collection->action("show_mime_info");
1043
1044 showSizeInfo->setChecked(false);
1045 showDateInfo->setChecked(false);
1046 showPermissionsInfo->setChecked(false);
1047 showOwnerInfo->setChecked(false);
1048 showGroupInfo->setChecked(false);
1049 showMimeInfo->setChecked(false);
1050
1051 showSizeInfo->setEnabled(enable);
1052 showDateInfo->setEnabled(enable);
1053 showPermissionsInfo->setEnabled(enable);
1054 showOwnerInfo->setEnabled(enable);
1055 showGroupInfo->setEnabled(enable);
1056 showMimeInfo->setEnabled(enable);
1057
1058 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
1059 switch (info) {
1060 case KFileItemDelegate::Size:
1061 showSizeInfo->setChecked(true);
1062 break;
1063 case KFileItemDelegate::ModificationTime:
1064 showDateInfo->setChecked(true);
1065 break;
1066 case KFileItemDelegate::Permissions:
1067 showPermissionsInfo->setChecked(true);
1068 break;
1069 case KFileItemDelegate::Owner:
1070 showOwnerInfo->setChecked(true);
1071 break;
1072 case KFileItemDelegate::OwnerAndGroup:
1073 showGroupInfo->setChecked(true);
1074 break;
1075 case KFileItemDelegate::FriendlyMimeType:
1076 showMimeInfo->setChecked(true);
1077 break;
1078 default:
1079 break;
1080 }
1081 }
1082 }
1083
1084 QPair<bool, QString> DolphinView::pasteInfo() const
1085 {
1086 return KonqOperations::pasteInfo(url());
1087 }
1088
1089 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
1090 {
1091 m_tabsForFiles = tabsForFiles;
1092 }
1093
1094 bool DolphinView::isTabsForFilesEnabled() const
1095 {
1096 return m_tabsForFiles;
1097 }
1098
1099 bool DolphinView::itemsExpandable() const
1100 {
1101 return (m_detailsView != 0) && m_detailsView->itemsExpandable();
1102 }
1103
1104 void DolphinView::deleteWhenNotDragSource(QAbstractItemView *view)
1105 {
1106 if (view == 0)
1107 return;
1108
1109 if (DragAndDropHelper::instance().isDragSource(view)) {
1110 // We must store for later deletion.
1111 if (m_expandedDragSource != 0) {
1112 // The old stored view is obviously not the drag source anymore.
1113 m_expandedDragSource->deleteLater();
1114 m_expandedDragSource = 0;
1115 }
1116 view->hide();
1117 m_expandedDragSource = view;
1118 }
1119 else {
1120 view->deleteLater();
1121 }
1122 }
1123
1124 void DolphinView::observeCreatedItem(const KUrl& url)
1125 {
1126 m_createdItemUrl = url;
1127 connect(m_dolphinModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1128 this, SLOT(selectAndScrollToCreatedItem()));
1129 }
1130
1131 void DolphinView::selectAndScrollToCreatedItem()
1132 {
1133 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_createdItemUrl);
1134 if (dirIndex.isValid()) {
1135 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
1136 itemView()->setCurrentIndex(proxyIndex);
1137 }
1138
1139 disconnect(m_dolphinModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1140 this, SLOT(selectAndScrollToCreatedItem()));
1141 m_createdItemUrl = KUrl();
1142 }
1143
1144 void DolphinView::restoreSelection()
1145 {
1146 disconnect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreSelection()));
1147 changeSelection(m_selectedItems);
1148 }
1149
1150 void DolphinView::slotJobRecordingFinished(CommandType command)
1151 {
1152 // Assure that the pasted items get selected. This must be done
1153 // asynchronously in slotDirListerCompleted().
1154 m_selectClipboardItems = ((command == KIO::FileUndoManager::Copy) ||
1155 (command == KIO::FileUndoManager::Move)) &&
1156 !hasSelection();
1157 }
1158
1159 void DolphinView::emitContentsMoved()
1160 {
1161 // only emit the contents moved signal if:
1162 // - no directory loading is ongoing (this would reset the contents position
1163 // always to (0, 0))
1164 // - if the Column View is active: the column view does an automatic
1165 // positioning during the loading operation, which must be remembered
1166 if (!m_loadingDirectory || isColumnViewActive()) {
1167 const QPoint pos(contentsPosition());
1168 emit contentsMoved(pos.x(), pos.y());
1169 }
1170 }
1171
1172 void DolphinView::showHoverInformation(const KFileItem& item)
1173 {
1174 emit requestItemInfo(item);
1175 }
1176
1177 void DolphinView::clearHoverInformation()
1178 {
1179 emit requestItemInfo(KFileItem());
1180 }
1181
1182 void DolphinView::slotDeleteFileFinished(KJob* job)
1183 {
1184 if (job->error() == 0) {
1185 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1186 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1187 emit errorMessage(job->errorString());
1188 }
1189 }
1190
1191 void DolphinView::slotRequestUrlChange(const KUrl& url)
1192 {
1193 emit requestUrlChange(url);
1194 m_controller->setUrl(url);
1195 }
1196
1197 void DolphinView::slotDirListerCompleted()
1198 {
1199 if (!m_currentItemUrl.isEmpty()) {
1200 // assure that the current item remains visible
1201 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl);
1202 if (dirIndex.isValid()) {
1203 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
1204 QAbstractItemView* view = itemView();
1205 const bool clearSelection = !hasSelection();
1206 view->setCurrentIndex(proxyIndex);
1207 if (clearSelection) {
1208 view->clearSelection();
1209 }
1210 }
1211 m_currentItemUrl.clear();
1212 }
1213
1214 if (m_selectClipboardItems) {
1215 m_selectClipboardItems = false;
1216
1217 // select all items that have been pasted from the clipboard to
1218 // the current directory
1219 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1220 const KUrl::List copiedUrls = KUrl::List::fromMimeData(mimeData);
1221
1222 QSet<QString> fileNames;
1223 foreach (const KUrl& url, copiedUrls) {
1224 fileNames.insert(url.fileName());
1225 }
1226
1227 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1228 const int rowCount = m_proxyModel->rowCount();
1229 for (int row = 0; row < rowCount; ++row) {
1230 const QModelIndex proxyIndex = m_proxyModel->index(row, 0);
1231 const QModelIndex dirIndex = m_proxyModel->mapToSource(proxyIndex);
1232 const KUrl url = m_dolphinModel->itemForIndex(dirIndex).url();
1233 if (fileNames.contains(url.fileName())) {
1234 selectionModel->select(proxyIndex, QItemSelectionModel::Select);
1235 }
1236 }
1237 }
1238 }
1239
1240 void DolphinView::slotRefreshItems()
1241 {
1242 if (m_assureVisibleCurrentIndex) {
1243 m_assureVisibleCurrentIndex = false;
1244 itemView()->scrollTo(itemView()->currentIndex());
1245 }
1246 }
1247
1248 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1249 {
1250 if (!url.isValid()) {
1251 const QString location(url.pathOrUrl());
1252 if (location.isEmpty()) {
1253 emit errorMessage(i18nc("@info:status", "The location is empty."));
1254 } else {
1255 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1256 }
1257 return;
1258 }
1259
1260 m_loadingDirectory = true;
1261
1262 if (reload) {
1263 m_selectedItems = selectedItems();
1264 connect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreSelection()));
1265 }
1266
1267 m_dirLister->stop();
1268 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1269
1270 if (isColumnViewActive()) {
1271 // adjusting the directory lister is not enough in the case of the
1272 // column view, as each column has its own directory lister internally...
1273 if (reload) {
1274 m_columnView->reload();
1275 } else {
1276 m_columnView->showColumn(url);
1277 }
1278 }
1279 }
1280
1281 KUrl DolphinView::viewPropertiesUrl() const
1282 {
1283 if (isColumnViewActive()) {
1284 return m_columnView->rootUrl();
1285 }
1286
1287 return url();
1288 }
1289
1290 void DolphinView::applyViewProperties(const KUrl& url)
1291 {
1292 if (m_ignoreViewProperties) {
1293 return;
1294 }
1295
1296 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
1297 // The column view is active, hence don't apply the view properties
1298 // of sub directories (represented by columns) to the view. The
1299 // view always represents the properties of the first column.
1300 return;
1301 }
1302
1303 const ViewProperties props(url);
1304
1305 const Mode mode = props.viewMode();
1306 if (m_mode != mode) {
1307 const int oldZoomLevel = m_controller->zoomLevel();
1308
1309 m_mode = mode;
1310 createView();
1311 emit modeChanged();
1312
1313 updateZoomLevel(oldZoomLevel);
1314 }
1315 if (itemView() == 0) {
1316 createView();
1317 }
1318 Q_ASSERT(itemView() != 0);
1319 Q_ASSERT(m_fileItemDelegate != 0);
1320
1321 const bool showHiddenFiles = props.showHiddenFiles();
1322 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
1323 m_dirLister->setShowingDotFiles(showHiddenFiles);
1324 emit showHiddenFilesChanged();
1325 }
1326
1327 m_storedCategorizedSorting = props.categorizedSorting();
1328 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1329 if (categorized != m_proxyModel->isCategorizedModel()) {
1330 m_proxyModel->setCategorizedModel(categorized);
1331 emit categorizedSortingChanged();
1332 }
1333
1334 const DolphinView::Sorting sorting = props.sorting();
1335 if (sorting != m_proxyModel->sorting()) {
1336 m_proxyModel->setSorting(sorting);
1337 emit sortingChanged(sorting);
1338 }
1339
1340 const Qt::SortOrder sortOrder = props.sortOrder();
1341 if (sortOrder != m_proxyModel->sortOrder()) {
1342 m_proxyModel->setSortOrder(sortOrder);
1343 emit sortOrderChanged(sortOrder);
1344 }
1345
1346 const bool sortFoldersFirst = props.sortFoldersFirst();
1347 if (sortFoldersFirst != m_proxyModel->sortFoldersFirst()) {
1348 m_proxyModel->setSortFoldersFirst(sortFoldersFirst);
1349 emit sortFoldersFirstChanged(sortFoldersFirst);
1350 }
1351
1352 KFileItemDelegate::InformationList info = props.additionalInfo();
1353 if (info != m_fileItemDelegate->showInformation()) {
1354 m_fileItemDelegate->setShowInformation(info);
1355 emit additionalInfoChanged();
1356 }
1357
1358 const bool showPreview = props.showPreview();
1359 if (showPreview != m_showPreview) {
1360 m_showPreview = showPreview;
1361 m_previewGenerator->setPreviewShown(showPreview);
1362
1363 const int oldZoomLevel = m_controller->zoomLevel();
1364 emit showPreviewChanged();
1365
1366 // Enabling or disabling the preview might change the icon size of the view.
1367 // As the view does not emit a signal when the icon size has been changed,
1368 // the used zoom level of the controller must be adjusted manually:
1369 updateZoomLevel(oldZoomLevel);
1370 }
1371
1372 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1373 // During the lifetime of a DolphinView instance the global view properties
1374 // should not be changed. This allows e. g. to split a view and use different
1375 // view properties for each view.
1376 m_ignoreViewProperties = true;
1377 }
1378 }
1379
1380 void DolphinView::createView()
1381 {
1382 deleteView();
1383 Q_ASSERT(m_iconsView == 0);
1384 Q_ASSERT(m_detailsView == 0);
1385 Q_ASSERT(m_columnView == 0);
1386
1387 QAbstractItemView* view = 0;
1388 switch (m_mode) {
1389 case IconsView: {
1390 m_iconsView = new DolphinIconsView(this, m_controller);
1391 view = m_iconsView;
1392 break;
1393 }
1394
1395 case DetailsView:
1396 m_detailsView = new DolphinDetailsView(this, m_controller);
1397 view = m_detailsView;
1398 break;
1399
1400 case ColumnView:
1401 m_columnView = new DolphinColumnView(this, m_controller);
1402 view = m_columnView;
1403 break;
1404 }
1405
1406 Q_ASSERT(view != 0);
1407 view->installEventFilter(this);
1408 view->viewport()->installEventFilter(this);
1409 setFocusProxy(view);
1410
1411 if (m_mode != ColumnView) {
1412 // Give the view the ability to auto-expand its directories on hovering
1413 // (the column view takes care about this itself). If the details view
1414 // uses expandable folders, the auto-expanding should be used always.
1415 DolphinSettings& settings = DolphinSettings::instance();
1416 const bool enabled = settings.generalSettings()->autoExpandFolders() ||
1417 ((m_detailsView != 0) && settings.detailsModeSettings()->expandableFolders());
1418
1419 FolderExpander* folderExpander = new FolderExpander(view, m_proxyModel);
1420 folderExpander->setEnabled(enabled);
1421 connect(folderExpander, SIGNAL(enterDir(const QModelIndex&)),
1422 m_controller, SLOT(triggerItem(const QModelIndex&)));
1423 }
1424 else {
1425 // Listen out for requests to delete the current column.
1426 connect(m_columnView, SIGNAL(requestColumnDeletion(QAbstractItemView*)),
1427 this, SLOT(deleteWhenNotDragSource(QAbstractItemView*)));
1428 }
1429
1430 m_controller->setItemView(view);
1431
1432 m_fileItemDelegate = new DolphinFileItemDelegate(view);
1433 m_fileItemDelegate->setShowToolTipWhenElided(false);
1434 m_fileItemDelegate->setMinimizedNameColumn(m_mode == DetailsView);
1435 view->setItemDelegate(m_fileItemDelegate);
1436
1437 view->setModel(m_proxyModel);
1438 if (m_selectionModel != 0) {
1439 view->setSelectionModel(m_selectionModel);
1440 } else {
1441 m_selectionModel = view->selectionModel();
1442 }
1443
1444 // reparent the selection model, as it should not be deleted
1445 // when deleting the model
1446 m_selectionModel->setParent(this);
1447
1448 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1449
1450 m_previewGenerator = new KFilePreviewGenerator(view);
1451 m_previewGenerator->setPreviewShown(m_showPreview);
1452
1453 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1454 m_toolTipManager = new ToolTipManager(view, m_proxyModel);
1455 connect(m_controller, SIGNAL(hideToolTip()),
1456 m_toolTipManager, SLOT(hideTip()));
1457 }
1458
1459 m_topLayout->insertWidget(1, view);
1460
1461 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1462 this, SLOT(emitSelectionChangedSignal()));
1463 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1464 this, SLOT(emitContentsMoved()));
1465 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1466 this, SLOT(emitContentsMoved()));
1467 }
1468
1469 void DolphinView::deleteView()
1470 {
1471 QAbstractItemView* view = itemView();
1472 if (view != 0) {
1473 // It's important to set the keyboard focus to the parent
1474 // before deleting the view: Otherwise when having a split
1475 // view the other view will get the focus and will request
1476 // an activation (see DolphinView::eventFilter()).
1477 setFocusProxy(0);
1478 setFocus();
1479
1480 m_topLayout->removeWidget(view);
1481 view->close();
1482
1483 // m_previewGenerator's parent is not always destroyed, and we
1484 // don't want two active at once - manually delete.
1485 delete m_previewGenerator;
1486 m_previewGenerator = 0;
1487
1488 disconnect(view);
1489 m_controller->disconnect(view);
1490 view->disconnect();
1491
1492 deleteWhenNotDragSource(view);
1493 view = 0;
1494
1495 m_iconsView = 0;
1496 m_detailsView = 0;
1497 m_columnView = 0;
1498 m_fileItemDelegate = 0;
1499 m_toolTipManager = 0;
1500 }
1501 }
1502
1503 QAbstractItemView* DolphinView::itemView() const
1504 {
1505 if (m_detailsView != 0) {
1506 return m_detailsView;
1507 } else if (m_columnView != 0) {
1508 return m_columnView;
1509 }
1510
1511 return m_iconsView;
1512 }
1513
1514 void DolphinView::pasteToUrl(const KUrl& url)
1515 {
1516 KonqOperations::doPaste(this, url);
1517 }
1518
1519 void DolphinView::updateZoomLevel(int oldZoomLevel)
1520 {
1521 const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1522 if (oldZoomLevel != newZoomLevel) {
1523 m_controller->setZoomLevel(newZoomLevel);
1524 emit zoomLevelChanged(newZoomLevel);
1525 }
1526 }
1527
1528 KUrl::List DolphinView::simplifiedSelectedUrls() const
1529 {
1530 KUrl::List list = selectedUrls();
1531 if (itemsExpandable() ) {
1532 list = KDirModel::simplifiedUrlList(list);
1533 }
1534 return list;
1535 }
1536
1537 QMimeData* DolphinView::selectionMimeData() const
1538 {
1539 if (isColumnViewActive()) {
1540 return m_columnView->selectionMimeData();
1541 }
1542
1543 const QAbstractItemView* view = itemView();
1544 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
1545 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
1546 return m_dolphinModel->mimeData(selection.indexes());
1547 }
1548
1549
1550 #include "dolphinview.moc"