]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Select copied/moved items automatically if no item is already selected. This gives...
[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_currentItemUrl = url;
616 updateView(url, KUrl());
617 }
618
619 void DolphinView::changeSelection(const KFileItemList& selection)
620 {
621 clearSelection();
622 if (selection.isEmpty()) {
623 return;
624 }
625 const KUrl& baseUrl = url();
626 KUrl url;
627 QItemSelection newSelection;
628 foreach(const KFileItem& item, selection) {
629 url = item.url().upUrl();
630 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
631 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
632 newSelection.select(index, index);
633 }
634 }
635 itemView()->selectionModel()->select(newSelection,
636 QItemSelectionModel::ClearAndSelect
637 | QItemSelectionModel::Current);
638 }
639
640 void DolphinView::renameSelectedItems()
641 {
642 KFileItemList items = selectedItems();
643 const int itemCount = items.count();
644 if (itemCount < 1) {
645 return;
646 }
647
648 if (itemCount > 1) {
649 // More than one item has been selected for renaming. Open
650 // a rename dialog and rename all items afterwards.
651 RenameDialog dialog(this, items);
652 if (dialog.exec() == QDialog::Rejected) {
653 return;
654 }
655
656 const QString newName = dialog.newName();
657 if (newName.isEmpty()) {
658 emit errorMessage(dialog.errorString());
659 return;
660 }
661
662 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
663 // as one operation instead of n rename operations like it is done now...
664 Q_ASSERT(newName.contains('#'));
665
666 // currently the items are sorted by the selection order, resort
667 // them by the file name
668 qSort(items.begin(), items.end(), lessThan);
669
670 // iterate through all selected items and rename them...
671 int index = 1;
672 foreach (const KFileItem& item, items) {
673 const KUrl& oldUrl = item.url();
674 QString number;
675 number.setNum(index++);
676
677 QString name = newName;
678 name.replace('#', number);
679
680 if (oldUrl.fileName() != name) {
681 KUrl newUrl = oldUrl;
682 newUrl.setFileName(name);
683 KonqOperations::rename(this, oldUrl, newUrl);
684 }
685 }
686 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
687 Q_ASSERT(itemCount == 1);
688
689 if (isColumnViewActive()) {
690 m_columnView->editItem(items.first());
691 } else {
692 const QModelIndex dirIndex = m_dolphinModel->indexForItem(items.first());
693 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
694 itemView()->edit(proxyIndex);
695 }
696 } else {
697 Q_ASSERT(itemCount == 1);
698
699 RenameDialog dialog(this, items);
700 if (dialog.exec() == QDialog::Rejected) {
701 return;
702 }
703
704 const QString& newName = dialog.newName();
705 if (newName.isEmpty()) {
706 emit errorMessage(dialog.errorString());
707 return;
708 }
709
710 const KUrl& oldUrl = items.first().url();
711 KUrl newUrl = oldUrl;
712 newUrl.setFileName(newName);
713 KonqOperations::rename(this, oldUrl, newUrl);
714 }
715
716 // assure that the current index remains visible when KDirLister
717 // will notify the view about changed items
718 m_assureVisibleCurrentIndex = true;
719 }
720
721 void DolphinView::trashSelectedItems()
722 {
723 const KUrl::List list = simplifiedSelectedUrls();
724 KonqOperations::del(this, KonqOperations::TRASH, list);
725 }
726
727 void DolphinView::deleteSelectedItems()
728 {
729 const KUrl::List list = simplifiedSelectedUrls();
730 const bool del = KonqOperations::askDeleteConfirmation(list,
731 KonqOperations::DEL,
732 KonqOperations::DEFAULT_CONFIRMATION,
733 this);
734
735 if (del) {
736 KIO::Job* job = KIO::del(list);
737 connect(job, SIGNAL(result(KJob*)),
738 this, SLOT(slotDeleteFileFinished(KJob*)));
739 }
740 }
741
742 void DolphinView::cutSelectedItems()
743 {
744 QMimeData* mimeData = selectionMimeData();
745 KonqMimeData::addIsCutSelection(mimeData, true);
746 QApplication::clipboard()->setMimeData(mimeData);
747 }
748
749 void DolphinView::copySelectedItems()
750 {
751 QMimeData* mimeData = selectionMimeData();
752 QApplication::clipboard()->setMimeData(mimeData);
753 }
754
755 void DolphinView::paste()
756 {
757 pasteToUrl(url());
758 }
759
760 void DolphinView::pasteIntoFolder()
761 {
762 const KFileItemList items = selectedItems();
763 if ((items.count() == 1) && items.first().isDir()) {
764 pasteToUrl(items.first().url());
765 }
766 }
767
768 void DolphinView::setShowPreview(bool show)
769 {
770 if (m_showPreview == show) {
771 return;
772 }
773
774 const KUrl viewPropsUrl = viewPropertiesUrl();
775 ViewProperties props(viewPropsUrl);
776 props.setShowPreview(show);
777
778 m_showPreview = show;
779 m_previewGenerator->setPreviewShown(show);
780
781 const int oldZoomLevel = m_controller->zoomLevel();
782 emit showPreviewChanged();
783
784 // Enabling or disabling the preview might change the icon size of the view.
785 // As the view does not emit a signal when the icon size has been changed,
786 // the used zoom level of the controller must be adjusted manually:
787 updateZoomLevel(oldZoomLevel);
788
789 loadDirectory(viewPropsUrl);
790 }
791
792 void DolphinView::setShowHiddenFiles(bool show)
793 {
794 if (m_dirLister->showingDotFiles() == show) {
795 return;
796 }
797
798 const KUrl viewPropsUrl = viewPropertiesUrl();
799 ViewProperties props(viewPropsUrl);
800 props.setShowHiddenFiles(show);
801
802 m_dirLister->setShowingDotFiles(show);
803 emit showHiddenFilesChanged();
804
805 loadDirectory(viewPropsUrl);
806 }
807
808 void DolphinView::setCategorizedSorting(bool categorized)
809 {
810 if (categorized == categorizedSorting()) {
811 return;
812 }
813
814 // setCategorizedSorting(true) may only get invoked
815 // if the view supports categorized sorting
816 Q_ASSERT(!categorized || supportsCategorizedSorting());
817
818 ViewProperties props(viewPropertiesUrl());
819 props.setCategorizedSorting(categorized);
820 props.save();
821
822 m_storedCategorizedSorting = categorized;
823 m_proxyModel->setCategorizedModel(categorized);
824
825 emit categorizedSortingChanged();
826 }
827
828 void DolphinView::toggleSortOrder()
829 {
830 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
831 Qt::DescendingOrder :
832 Qt::AscendingOrder;
833 setSortOrder(order);
834 }
835
836 void DolphinView::toggleSortFoldersFirst()
837 {
838 setSortFoldersFirst(!sortFoldersFirst());
839 }
840
841 void DolphinView::toggleAdditionalInfo(QAction* action)
842 {
843 const KFileItemDelegate::Information info =
844 static_cast<KFileItemDelegate::Information>(action->data().toInt());
845
846 KFileItemDelegate::InformationList list = additionalInfo();
847
848 const bool show = action->isChecked();
849
850 const int index = list.indexOf(info);
851 const bool containsInfo = (index >= 0);
852 if (show && !containsInfo) {
853 list.append(info);
854 setAdditionalInfo(list);
855 } else if (!show && containsInfo) {
856 list.removeAt(index);
857 setAdditionalInfo(list);
858 Q_ASSERT(list.indexOf(info) < 0);
859 }
860 }
861
862 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
863 {
864 QWidget::mouseReleaseEvent(event);
865 setActive(true);
866 }
867
868 void DolphinView::wheelEvent(QWheelEvent* event)
869 {
870 if (event->modifiers() & Qt::ControlModifier) {
871 const int delta = event->delta();
872 const int level = zoomLevel();
873 if (delta > 0) {
874 setZoomLevel(level + 1);
875 } else if (delta < 0) {
876 setZoomLevel(level - 1);
877 }
878 event->accept();
879 }
880 }
881
882 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
883 {
884 switch (event->type()) {
885 case QEvent::FocusIn:
886 if (watched == itemView()) {
887 m_controller->requestActivation();
888 }
889 break;
890
891 case QEvent::MouseButtonPress:
892 if ((watched == itemView()->viewport()) && (m_expandedDragSource != 0)) {
893 // Listening to a mousebutton press event to delete expanded views is a
894 // workaround, as it seems impossible for the FolderExpander to know when
895 // a dragging outside a view has been finished. However it works quite well:
896 // A mousebutton press event indicates that a drag operation must be
897 // finished already.
898 m_expandedDragSource->deleteLater();
899 m_expandedDragSource = 0;
900 }
901 break;
902
903 case QEvent::DragEnter:
904 if (watched == itemView()->viewport()) {
905 setActive(true);
906 }
907 break;
908
909 case QEvent::KeyPress:
910 if (watched == itemView()) {
911 if (m_toolTipManager != 0) {
912 m_toolTipManager->hideTip();
913 }
914
915 // clear the selection when Escape has been pressed
916 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
917 if (keyEvent->key() == Qt::Key_Escape) {
918 clearSelection();
919 }
920 }
921 break;
922
923 default:
924 break;
925 }
926
927 return QWidget::eventFilter(watched, event);
928 }
929
930 void DolphinView::activate()
931 {
932 setActive(true);
933 }
934
935 void DolphinView::triggerItem(const KFileItem& item)
936 {
937 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
938 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
939 // items are selected by the user, hence don't trigger the
940 // item specified by 'index'
941 return;
942 }
943
944 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
945 if (item.isNull() || m_isContextMenuOpen) {
946 return;
947 }
948
949 if (m_toolTipManager != 0) {
950 m_toolTipManager->hideTip();
951 }
952 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
953 }
954
955 void DolphinView::emitSelectionChangedSignal()
956 {
957 emit selectionChanged(DolphinView::selectedItems());
958 }
959
960 void DolphinView::openContextMenu(const QPoint& pos,
961 const QList<QAction*>& customActions)
962 {
963 KFileItem item;
964 if (isColumnViewActive()) {
965 item = m_columnView->itemAt(pos);
966 } else {
967 const QModelIndex index = itemView()->indexAt(pos);
968 if (index.isValid() && (index.column() == DolphinModel::Name)) {
969 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
970 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
971 }
972 }
973
974 if (m_toolTipManager != 0) {
975 m_toolTipManager->hideTip();
976 }
977
978 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
979 emit requestContextMenu(item, url(), customActions);
980 m_isContextMenuOpen = false;
981 }
982
983 void DolphinView::dropUrls(const KFileItem& destItem,
984 const KUrl& destPath,
985 QDropEvent* event)
986 {
987 DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
988 }
989
990 void DolphinView::updateSorting(DolphinView::Sorting sorting)
991 {
992 ViewProperties props(viewPropertiesUrl());
993 props.setSorting(sorting);
994
995 m_proxyModel->setSorting(sorting);
996
997 emit sortingChanged(sorting);
998 }
999
1000 void DolphinView::updateSortOrder(Qt::SortOrder order)
1001 {
1002 ViewProperties props(viewPropertiesUrl());
1003 props.setSortOrder(order);
1004
1005 m_proxyModel->setSortOrder(order);
1006
1007 emit sortOrderChanged(order);
1008 }
1009
1010 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
1011 {
1012 ViewProperties props(viewPropertiesUrl());
1013 props.setSortFoldersFirst(foldersFirst);
1014
1015 m_proxyModel->setSortFoldersFirst(foldersFirst);
1016
1017 emit sortFoldersFirstChanged(foldersFirst);
1018 }
1019
1020 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
1021 {
1022 ViewProperties props(viewPropertiesUrl());
1023 props.setAdditionalInfo(info);
1024 props.save();
1025
1026 m_fileItemDelegate->setShowInformation(info);
1027
1028 emit additionalInfoChanged();
1029 }
1030
1031 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
1032 {
1033 const bool enable = (m_mode == DolphinView::DetailsView) ||
1034 (m_mode == DolphinView::IconsView);
1035
1036 QAction* showSizeInfo = collection->action("show_size_info");
1037 QAction* showDateInfo = collection->action("show_date_info");
1038 QAction* showPermissionsInfo = collection->action("show_permissions_info");
1039 QAction* showOwnerInfo = collection->action("show_owner_info");
1040 QAction* showGroupInfo = collection->action("show_group_info");
1041 QAction* showMimeInfo = collection->action("show_mime_info");
1042
1043 showSizeInfo->setChecked(false);
1044 showDateInfo->setChecked(false);
1045 showPermissionsInfo->setChecked(false);
1046 showOwnerInfo->setChecked(false);
1047 showGroupInfo->setChecked(false);
1048 showMimeInfo->setChecked(false);
1049
1050 showSizeInfo->setEnabled(enable);
1051 showDateInfo->setEnabled(enable);
1052 showPermissionsInfo->setEnabled(enable);
1053 showOwnerInfo->setEnabled(enable);
1054 showGroupInfo->setEnabled(enable);
1055 showMimeInfo->setEnabled(enable);
1056
1057 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
1058 switch (info) {
1059 case KFileItemDelegate::Size:
1060 showSizeInfo->setChecked(true);
1061 break;
1062 case KFileItemDelegate::ModificationTime:
1063 showDateInfo->setChecked(true);
1064 break;
1065 case KFileItemDelegate::Permissions:
1066 showPermissionsInfo->setChecked(true);
1067 break;
1068 case KFileItemDelegate::Owner:
1069 showOwnerInfo->setChecked(true);
1070 break;
1071 case KFileItemDelegate::OwnerAndGroup:
1072 showGroupInfo->setChecked(true);
1073 break;
1074 case KFileItemDelegate::FriendlyMimeType:
1075 showMimeInfo->setChecked(true);
1076 break;
1077 default:
1078 break;
1079 }
1080 }
1081 }
1082
1083 QPair<bool, QString> DolphinView::pasteInfo() const
1084 {
1085 return KonqOperations::pasteInfo(url());
1086 }
1087
1088 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
1089 {
1090 m_tabsForFiles = tabsForFiles;
1091 }
1092
1093 bool DolphinView::isTabsForFilesEnabled() const
1094 {
1095 return m_tabsForFiles;
1096 }
1097
1098 bool DolphinView::itemsExpandable() const
1099 {
1100 return (m_detailsView != 0) && m_detailsView->itemsExpandable();
1101 }
1102
1103 void DolphinView::deleteWhenNotDragSource(QAbstractItemView *view)
1104 {
1105 if (view == 0)
1106 return;
1107
1108 if (DragAndDropHelper::instance().isDragSource(view)) {
1109 // We must store for later deletion.
1110 if (m_expandedDragSource != 0) {
1111 // The old stored view is obviously not the drag source anymore.
1112 m_expandedDragSource->deleteLater();
1113 m_expandedDragSource = 0;
1114 }
1115 view->hide();
1116 m_expandedDragSource = view;
1117 }
1118 else {
1119 view->deleteLater();
1120 }
1121 }
1122
1123 void DolphinView::observeCreatedItem(const KUrl& url)
1124 {
1125 m_createdItemUrl = url;
1126 connect(m_dolphinModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1127 this, SLOT(selectAndScrollToCreatedItem()));
1128 }
1129
1130 void DolphinView::selectAndScrollToCreatedItem()
1131 {
1132 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_createdItemUrl);
1133 if (dirIndex.isValid()) {
1134 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
1135 itemView()->setCurrentIndex(proxyIndex);
1136 }
1137
1138 disconnect(m_dolphinModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1139 this, SLOT(selectAndScrollToCreatedItem()));
1140 m_createdItemUrl = KUrl();
1141 }
1142
1143 void DolphinView::restoreSelection()
1144 {
1145 disconnect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreSelection()));
1146 changeSelection(m_selectedItems);
1147 }
1148
1149 void DolphinView::slotJobRecordingFinished(CommandType command)
1150 {
1151 // Assure that the pasted items get selected. This must be done
1152 // asynchronously in slotDirListerCompleted().
1153 m_selectClipboardItems = ((command == KIO::FileUndoManager::Copy) ||
1154 (command == KIO::FileUndoManager::Move)) &&
1155 !hasSelection();
1156 }
1157
1158 void DolphinView::emitContentsMoved()
1159 {
1160 // only emit the contents moved signal if:
1161 // - no directory loading is ongoing (this would reset the contents position
1162 // always to (0, 0))
1163 // - if the Column View is active: the column view does an automatic
1164 // positioning during the loading operation, which must be remembered
1165 if (!m_loadingDirectory || isColumnViewActive()) {
1166 const QPoint pos(contentsPosition());
1167 emit contentsMoved(pos.x(), pos.y());
1168 }
1169 }
1170
1171 void DolphinView::showHoverInformation(const KFileItem& item)
1172 {
1173 emit requestItemInfo(item);
1174 }
1175
1176 void DolphinView::clearHoverInformation()
1177 {
1178 emit requestItemInfo(KFileItem());
1179 }
1180
1181 void DolphinView::slotDeleteFileFinished(KJob* job)
1182 {
1183 if (job->error() == 0) {
1184 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1185 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1186 emit errorMessage(job->errorString());
1187 }
1188 }
1189
1190 void DolphinView::slotRequestUrlChange(const KUrl& url)
1191 {
1192 emit requestUrlChange(url);
1193 m_controller->setUrl(url);
1194 }
1195
1196 void DolphinView::slotDirListerCompleted()
1197 {
1198 if (!m_currentItemUrl.isEmpty()) {
1199 // assure that the current item remains visible
1200 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl);
1201 if (dirIndex.isValid()) {
1202 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
1203 QAbstractItemView* view = itemView();
1204 const bool clearSelection = !hasSelection();
1205 view->setCurrentIndex(proxyIndex);
1206 if (clearSelection) {
1207 view->clearSelection();
1208 }
1209 }
1210 m_currentItemUrl.clear();
1211 }
1212
1213 if (m_selectClipboardItems) {
1214 m_selectClipboardItems = false;
1215
1216 // select all items that have been pasted from the clipboard to
1217 // the current directory
1218 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1219 const KUrl::List copiedUrls = KUrl::List::fromMimeData(mimeData);
1220
1221 QSet<QString> fileNames;
1222 foreach (const KUrl& url, copiedUrls) {
1223 fileNames.insert(url.fileName());
1224 }
1225
1226 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1227 const int rowCount = m_proxyModel->rowCount();
1228 for (int row = 0; row < rowCount; ++row) {
1229 const QModelIndex proxyIndex = m_proxyModel->index(row, 0);
1230 const QModelIndex dirIndex = m_proxyModel->mapToSource(proxyIndex);
1231 const KUrl url = m_dolphinModel->itemForIndex(dirIndex).url();
1232 if (fileNames.contains(url.fileName())) {
1233 selectionModel->select(proxyIndex, QItemSelectionModel::Select);
1234 }
1235 }
1236 }
1237 }
1238
1239 void DolphinView::slotRefreshItems()
1240 {
1241 if (m_assureVisibleCurrentIndex) {
1242 m_assureVisibleCurrentIndex = false;
1243 itemView()->scrollTo(itemView()->currentIndex());
1244 }
1245 }
1246
1247 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1248 {
1249 if (!url.isValid()) {
1250 const QString location(url.pathOrUrl());
1251 if (location.isEmpty()) {
1252 emit errorMessage(i18nc("@info:status", "The location is empty."));
1253 } else {
1254 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1255 }
1256 return;
1257 }
1258
1259 m_loadingDirectory = true;
1260
1261 if (reload) {
1262 m_selectedItems = selectedItems();
1263 connect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreSelection()));
1264 }
1265
1266 m_dirLister->stop();
1267 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1268
1269 if (isColumnViewActive()) {
1270 // adjusting the directory lister is not enough in the case of the
1271 // column view, as each column has its own directory lister internally...
1272 if (reload) {
1273 m_columnView->reload();
1274 } else {
1275 m_columnView->showColumn(url);
1276 }
1277 }
1278 }
1279
1280 KUrl DolphinView::viewPropertiesUrl() const
1281 {
1282 if (isColumnViewActive()) {
1283 return m_columnView->rootUrl();
1284 }
1285
1286 return url();
1287 }
1288
1289 void DolphinView::applyViewProperties(const KUrl& url)
1290 {
1291 if (m_ignoreViewProperties) {
1292 return;
1293 }
1294
1295 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
1296 // The column view is active, hence don't apply the view properties
1297 // of sub directories (represented by columns) to the view. The
1298 // view always represents the properties of the first column.
1299 return;
1300 }
1301
1302 const ViewProperties props(url);
1303
1304 const Mode mode = props.viewMode();
1305 if (m_mode != mode) {
1306 const int oldZoomLevel = m_controller->zoomLevel();
1307
1308 m_mode = mode;
1309 createView();
1310 emit modeChanged();
1311
1312 updateZoomLevel(oldZoomLevel);
1313 }
1314 if (itemView() == 0) {
1315 createView();
1316 }
1317 Q_ASSERT(itemView() != 0);
1318 Q_ASSERT(m_fileItemDelegate != 0);
1319
1320 const bool showHiddenFiles = props.showHiddenFiles();
1321 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
1322 m_dirLister->setShowingDotFiles(showHiddenFiles);
1323 emit showHiddenFilesChanged();
1324 }
1325
1326 m_storedCategorizedSorting = props.categorizedSorting();
1327 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1328 if (categorized != m_proxyModel->isCategorizedModel()) {
1329 m_proxyModel->setCategorizedModel(categorized);
1330 emit categorizedSortingChanged();
1331 }
1332
1333 const DolphinView::Sorting sorting = props.sorting();
1334 if (sorting != m_proxyModel->sorting()) {
1335 m_proxyModel->setSorting(sorting);
1336 emit sortingChanged(sorting);
1337 }
1338
1339 const Qt::SortOrder sortOrder = props.sortOrder();
1340 if (sortOrder != m_proxyModel->sortOrder()) {
1341 m_proxyModel->setSortOrder(sortOrder);
1342 emit sortOrderChanged(sortOrder);
1343 }
1344
1345 const bool sortFoldersFirst = props.sortFoldersFirst();
1346 if (sortFoldersFirst != m_proxyModel->sortFoldersFirst()) {
1347 m_proxyModel->setSortFoldersFirst(sortFoldersFirst);
1348 emit sortFoldersFirstChanged(sortFoldersFirst);
1349 }
1350
1351 KFileItemDelegate::InformationList info = props.additionalInfo();
1352 if (info != m_fileItemDelegate->showInformation()) {
1353 m_fileItemDelegate->setShowInformation(info);
1354 emit additionalInfoChanged();
1355 }
1356
1357 const bool showPreview = props.showPreview();
1358 if (showPreview != m_showPreview) {
1359 m_showPreview = showPreview;
1360 m_previewGenerator->setPreviewShown(showPreview);
1361
1362 const int oldZoomLevel = m_controller->zoomLevel();
1363 emit showPreviewChanged();
1364
1365 // Enabling or disabling the preview might change the icon size of the view.
1366 // As the view does not emit a signal when the icon size has been changed,
1367 // the used zoom level of the controller must be adjusted manually:
1368 updateZoomLevel(oldZoomLevel);
1369 }
1370
1371 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1372 // During the lifetime of a DolphinView instance the global view properties
1373 // should not be changed. This allows e. g. to split a view and use different
1374 // view properties for each view.
1375 m_ignoreViewProperties = true;
1376 }
1377 }
1378
1379 void DolphinView::createView()
1380 {
1381 deleteView();
1382 Q_ASSERT(m_iconsView == 0);
1383 Q_ASSERT(m_detailsView == 0);
1384 Q_ASSERT(m_columnView == 0);
1385
1386 QAbstractItemView* view = 0;
1387 switch (m_mode) {
1388 case IconsView: {
1389 m_iconsView = new DolphinIconsView(this, m_controller);
1390 view = m_iconsView;
1391 break;
1392 }
1393
1394 case DetailsView:
1395 m_detailsView = new DolphinDetailsView(this, m_controller);
1396 view = m_detailsView;
1397 break;
1398
1399 case ColumnView:
1400 m_columnView = new DolphinColumnView(this, m_controller);
1401 view = m_columnView;
1402 break;
1403 }
1404
1405 Q_ASSERT(view != 0);
1406 view->installEventFilter(this);
1407 view->viewport()->installEventFilter(this);
1408 setFocusProxy(view);
1409
1410 if (m_mode != ColumnView) {
1411 // Give the view the ability to auto-expand its directories on hovering
1412 // (the column view takes care about this itself). If the details view
1413 // uses expandable folders, the auto-expanding should be used always.
1414 DolphinSettings& settings = DolphinSettings::instance();
1415 const bool enabled = settings.generalSettings()->autoExpandFolders() ||
1416 ((m_detailsView != 0) && settings.detailsModeSettings()->expandableFolders());
1417
1418 FolderExpander* folderExpander = new FolderExpander(view, m_proxyModel);
1419 folderExpander->setEnabled(enabled);
1420 connect(folderExpander, SIGNAL(enterDir(const QModelIndex&)),
1421 m_controller, SLOT(triggerItem(const QModelIndex&)));
1422 }
1423 else {
1424 // Listen out for requests to delete the current column.
1425 connect(m_columnView, SIGNAL(requestColumnDeletion(QAbstractItemView*)),
1426 this, SLOT(deleteWhenNotDragSource(QAbstractItemView*)));
1427 }
1428
1429 m_controller->setItemView(view);
1430
1431 m_fileItemDelegate = new DolphinFileItemDelegate(view);
1432 m_fileItemDelegate->setShowToolTipWhenElided(false);
1433 m_fileItemDelegate->setMinimizedNameColumn(m_mode == DetailsView);
1434 view->setItemDelegate(m_fileItemDelegate);
1435
1436 view->setModel(m_proxyModel);
1437 if (m_selectionModel != 0) {
1438 view->setSelectionModel(m_selectionModel);
1439 } else {
1440 m_selectionModel = view->selectionModel();
1441 }
1442
1443 // reparent the selection model, as it should not be deleted
1444 // when deleting the model
1445 m_selectionModel->setParent(this);
1446
1447 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1448
1449 m_previewGenerator = new KFilePreviewGenerator(view);
1450 m_previewGenerator->setPreviewShown(m_showPreview);
1451
1452 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1453 m_toolTipManager = new ToolTipManager(view, m_proxyModel);
1454 connect(m_controller, SIGNAL(hideToolTip()),
1455 m_toolTipManager, SLOT(hideTip()));
1456 }
1457
1458 m_topLayout->insertWidget(1, view);
1459
1460 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1461 this, SLOT(emitSelectionChangedSignal()));
1462 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1463 this, SLOT(emitContentsMoved()));
1464 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1465 this, SLOT(emitContentsMoved()));
1466 }
1467
1468 void DolphinView::deleteView()
1469 {
1470 QAbstractItemView* view = itemView();
1471 if (view != 0) {
1472 // It's important to set the keyboard focus to the parent
1473 // before deleting the view: Otherwise when having a split
1474 // view the other view will get the focus and will request
1475 // an activation (see DolphinView::eventFilter()).
1476 setFocusProxy(0);
1477 setFocus();
1478
1479 m_topLayout->removeWidget(view);
1480 view->close();
1481
1482 // m_previewGenerator's parent is not always destroyed, and we
1483 // don't want two active at once - manually delete.
1484 delete m_previewGenerator;
1485 m_previewGenerator = 0;
1486
1487 disconnect(view);
1488 m_controller->disconnect(view);
1489 view->disconnect();
1490
1491 deleteWhenNotDragSource(view);
1492 view = 0;
1493
1494 m_iconsView = 0;
1495 m_detailsView = 0;
1496 m_columnView = 0;
1497 m_fileItemDelegate = 0;
1498 m_toolTipManager = 0;
1499 }
1500 }
1501
1502 QAbstractItemView* DolphinView::itemView() const
1503 {
1504 if (m_detailsView != 0) {
1505 return m_detailsView;
1506 } else if (m_columnView != 0) {
1507 return m_columnView;
1508 }
1509
1510 return m_iconsView;
1511 }
1512
1513 void DolphinView::pasteToUrl(const KUrl& url)
1514 {
1515 KonqOperations::doPaste(this, url);
1516 }
1517
1518 void DolphinView::updateZoomLevel(int oldZoomLevel)
1519 {
1520 const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1521 if (oldZoomLevel != newZoomLevel) {
1522 m_controller->setZoomLevel(newZoomLevel);
1523 emit zoomLevelChanged(newZoomLevel);
1524 }
1525 }
1526
1527 KUrl::List DolphinView::simplifiedSelectedUrls() const
1528 {
1529 KUrl::List list = selectedUrls();
1530 if (itemsExpandable() ) {
1531 list = KDirModel::simplifiedUrlList(list);
1532 }
1533 return list;
1534 }
1535
1536 QMimeData* DolphinView::selectionMimeData() const
1537 {
1538 if (isColumnViewActive()) {
1539 return m_columnView->selectionMimeData();
1540 }
1541
1542 const QAbstractItemView* view = itemView();
1543 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
1544 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
1545 return m_dolphinModel->mimeData(selection.indexes());
1546 }
1547
1548
1549 #include "dolphinview.moc"