]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Mark the last visitied directory as active when going back in history.
[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_mode(DolphinView::IconsView),
94 m_topLayout(0),
95 m_controller(0),
96 m_iconsView(0),
97 m_detailsView(0),
98 m_columnView(0),
99 m_fileItemDelegate(0),
100 m_selectionModel(0),
101 m_dolphinModel(dolphinModel),
102 m_dirLister(dirLister),
103 m_proxyModel(proxyModel),
104 m_previewGenerator(0),
105 m_toolTipManager(0),
106 m_rootUrl(),
107 m_activeItemUrl(),
108 m_createdItemUrl(),
109 m_selectedItems(),
110 m_newFileNames(),
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 applyViewProperties(url);
162 m_topLayout->addWidget(itemView());
163 }
164
165 DolphinView::~DolphinView()
166 {
167 delete m_expandedDragSource;
168 m_expandedDragSource = 0;
169 }
170
171 const KUrl& DolphinView::url() const
172 {
173 return m_controller->url();
174 }
175
176 KUrl DolphinView::rootUrl() const
177 {
178 return isColumnViewActive() ? m_columnView->rootUrl() : url();
179 }
180
181 void DolphinView::setActive(bool active)
182 {
183 if (active == m_active) {
184 return;
185 }
186
187 m_active = active;
188
189 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
190 if (active) {
191 emit selectionChanged(selectedItems());
192 } else {
193 color.setAlpha(150);
194 }
195
196 QWidget* viewport = itemView()->viewport();
197 QPalette palette;
198 palette.setColor(viewport->backgroundRole(), color);
199 viewport->setPalette(palette);
200
201 update();
202
203 if (active) {
204 itemView()->setFocus();
205 emit activated();
206 }
207
208 m_controller->indicateActivationChange(active);
209 }
210
211 bool DolphinView::isActive() const
212 {
213 return m_active;
214 }
215
216 void DolphinView::setMode(Mode mode)
217 {
218 if (mode == m_mode) {
219 return; // the wished mode is already set
220 }
221
222 const int oldZoomLevel = m_controller->zoomLevel();
223 m_mode = mode;
224
225 deleteView();
226
227 const KUrl viewPropsUrl = viewPropertiesUrl();
228 ViewProperties props(viewPropsUrl);
229 props.setViewMode(m_mode);
230 createView();
231
232 // the file item delegate has been recreated, apply the current
233 // additional information manually
234 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
235 m_fileItemDelegate->setShowInformation(infoList);
236 emit additionalInfoChanged();
237
238 // Not all view modes support categorized sorting. Adjust the sorting model
239 // if changing the view mode results in a change of the categorized sorting
240 // capabilities.
241 m_storedCategorizedSorting = props.categorizedSorting();
242 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
243 if (categorized != m_proxyModel->isCategorizedModel()) {
244 m_proxyModel->setCategorizedModel(categorized);
245 emit categorizedSortingChanged();
246 }
247
248 emit modeChanged();
249
250 updateZoomLevel(oldZoomLevel);
251 if (m_showPreview) {
252 loadDirectory(viewPropsUrl);
253 }
254 }
255
256 DolphinView::Mode DolphinView::mode() const
257 {
258 return m_mode;
259 }
260
261 bool DolphinView::showPreview() const
262 {
263 return m_showPreview;
264 }
265
266 bool DolphinView::showHiddenFiles() const
267 {
268 return m_dirLister->showingDotFiles();
269 }
270
271 bool DolphinView::categorizedSorting() const
272 {
273 // If all view modes would support categorized sorting, returning
274 // m_proxyModel->isCategorizedModel() would be the way to go. As
275 // currently only the icons view supports caterized sorting, we remember
276 // the stored view properties state in m_storedCategorizedSorting and
277 // return this state. The application takes care to disable the corresponding
278 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
279 // that this setting is not applied to the current view mode.
280 return m_storedCategorizedSorting;
281 }
282
283 bool DolphinView::supportsCategorizedSorting() const
284 {
285 return m_iconsView != 0;
286 }
287
288 void DolphinView::selectAll()
289 {
290 QAbstractItemView* view = itemView();
291 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
292 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
293 // selection instead of selecting all items. This is bypassed for KDE 4.0
294 // by invoking clearSelection() first.
295 view->clearSelection();
296 view->selectAll();
297 }
298
299 void DolphinView::invertSelection()
300 {
301 if (isColumnViewActive()) {
302 // QAbstractItemView does not offer a virtual method invertSelection()
303 // as counterpart to QAbstractItemView::selectAll(). This makes it
304 // necessary to delegate the inverting of the selection to the
305 // column view, as only the selection of the active column should
306 // get inverted.
307 m_columnView->invertSelection();
308 } else {
309 QItemSelectionModel* selectionModel = itemView()->selectionModel();
310 const QAbstractItemModel* itemModel = selectionModel->model();
311
312 const QModelIndex topLeft = itemModel->index(0, 0);
313 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
314 itemModel->columnCount() - 1);
315
316 const QItemSelection selection(topLeft, bottomRight);
317 selectionModel->select(selection, QItemSelectionModel::Toggle);
318 }
319 }
320
321 bool DolphinView::hasSelection() const
322 {
323 return itemView()->selectionModel()->hasSelection();
324 }
325
326 void DolphinView::clearSelection()
327 {
328 QItemSelectionModel* selModel = itemView()->selectionModel();
329 const QModelIndex currentIndex = selModel->currentIndex();
330 selModel->setCurrentIndex(currentIndex, QItemSelectionModel::Current |
331 QItemSelectionModel::Clear);
332 }
333
334 KFileItemList DolphinView::selectedItems() const
335 {
336 if (isColumnViewActive()) {
337 return m_columnView->selectedItems();
338 }
339
340 const QAbstractItemView* view = itemView();
341
342 // Our view has a selection, we will map them back to the DolphinModel
343 // and then fill the KFileItemList.
344 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
345
346 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
347 KFileItemList itemList;
348
349 const QModelIndexList indexList = selection.indexes();
350 foreach (const QModelIndex &index, indexList) {
351 KFileItem item = m_dolphinModel->itemForIndex(index);
352 if (!item.isNull()) {
353 itemList.append(item);
354 }
355 }
356
357 return itemList;
358 }
359
360 KUrl::List DolphinView::selectedUrls() const
361 {
362 KUrl::List urls;
363 const KFileItemList list = selectedItems();
364 foreach (const KFileItem &item, list) {
365 urls.append(item.url());
366 }
367 return urls;
368 }
369
370 int DolphinView::selectedItemsCount() const
371 {
372 if (isColumnViewActive()) {
373 // TODO: get rid of this special case by adjusting the dir lister
374 // to the current column
375 return m_columnView->selectedItems().count();
376 }
377
378 return itemView()->selectionModel()->selection().count();
379 }
380
381 void DolphinView::setContentsPosition(int x, int y)
382 {
383 QAbstractItemView* view = itemView();
384
385 // the ColumnView takes care itself for the horizontal scrolling
386 if (!isColumnViewActive()) {
387 view->horizontalScrollBar()->setValue(x);
388 }
389 view->verticalScrollBar()->setValue(y);
390
391 m_loadingDirectory = false;
392 }
393
394 QPoint DolphinView::contentsPosition() const
395 {
396 const int x = itemView()->horizontalScrollBar()->value();
397 const int y = itemView()->verticalScrollBar()->value();
398 return QPoint(x, y);
399 }
400
401 void DolphinView::setZoomLevel(int level)
402 {
403 if (level < ZoomLevelInfo::minimumLevel()) {
404 level = ZoomLevelInfo::minimumLevel();
405 } else if (level > ZoomLevelInfo::maximumLevel()) {
406 level = ZoomLevelInfo::maximumLevel();
407 }
408
409 if (level != zoomLevel()) {
410 m_controller->setZoomLevel(level);
411 m_previewGenerator->updateIcons();
412 emit zoomLevelChanged(level);
413 }
414 }
415
416 int DolphinView::zoomLevel() const
417 {
418 return m_controller->zoomLevel();
419 }
420
421 void DolphinView::setSorting(Sorting sorting)
422 {
423 if (sorting != this->sorting()) {
424 updateSorting(sorting);
425 }
426 }
427
428 DolphinView::Sorting DolphinView::sorting() const
429 {
430 return m_proxyModel->sorting();
431 }
432
433 void DolphinView::setSortOrder(Qt::SortOrder order)
434 {
435 if (sortOrder() != order) {
436 updateSortOrder(order);
437 }
438 }
439
440 Qt::SortOrder DolphinView::sortOrder() const
441 {
442 return m_proxyModel->sortOrder();
443 }
444
445 void DolphinView::setSortFoldersFirst(bool foldersFirst)
446 {
447 if (sortFoldersFirst() != foldersFirst) {
448 updateSortFoldersFirst(foldersFirst);
449 }
450 }
451
452 bool DolphinView::sortFoldersFirst() const
453 {
454 return m_proxyModel->sortFoldersFirst();
455 }
456
457 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
458 {
459 const KUrl viewPropsUrl = viewPropertiesUrl();
460 ViewProperties props(viewPropsUrl);
461 props.setAdditionalInfo(info);
462 m_fileItemDelegate->setShowInformation(info);
463
464 emit additionalInfoChanged();
465
466 if (itemView() != m_detailsView) {
467 // the details view requires no reloading of the directory, as it maps
468 // the file item delegate info to its columns internally
469 loadDirectory(viewPropsUrl);
470 }
471 }
472
473 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
474 {
475 return m_fileItemDelegate->showInformation();
476 }
477
478 void DolphinView::reload()
479 {
480 setUrl(url());
481 loadDirectory(url(), true);
482 }
483
484 void DolphinView::refresh()
485 {
486 m_ignoreViewProperties = false;
487
488 const bool oldActivationState = m_active;
489 const int oldZoomLevel = m_controller->zoomLevel();
490 m_active = true;
491
492 createView();
493 applyViewProperties(m_controller->url());
494 reload();
495
496 setActive(oldActivationState);
497 updateZoomLevel(oldZoomLevel);
498 }
499
500 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
501 {
502 if (m_controller->url() == url) {
503 return;
504 }
505
506 m_previewGenerator->cancelPreviews();
507 m_controller->setUrl(url); // emits urlChanged, which we forward
508
509 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
510 applyViewProperties(rootUrl);
511 loadDirectory(rootUrl);
512 if (itemView() == m_columnView) {
513 m_columnView->setRootUrl(rootUrl);
514 m_columnView->showColumn(url);
515 }
516 } else {
517 applyViewProperties(url);
518 loadDirectory(url);
519 }
520
521 emit startedPathLoading(url);
522 }
523
524 void DolphinView::setNameFilter(const QString& nameFilter)
525 {
526 m_proxyModel->setFilterRegExp(nameFilter);
527
528 if (isColumnViewActive()) {
529 // adjusting the directory lister is not enough in the case of the
530 // column view, as each column has its own directory lister internally...
531 m_columnView->setNameFilter(nameFilter);
532 }
533 }
534
535 void DolphinView::calculateItemCount(int& fileCount,
536 int& folderCount,
537 KIO::filesize_t& totalFileSize) const
538 {
539 foreach (const KFileItem& item, m_dirLister->items()) {
540 if (item.isDir()) {
541 ++folderCount;
542 } else {
543 ++fileCount;
544 totalFileSize += item.size();
545 }
546 }
547 }
548
549 QString DolphinView::statusBarText() const
550 {
551 QString text;
552 int folderCount = 0;
553 int fileCount = 0;
554 KIO::filesize_t totalFileSize = 0;
555
556 if (hasSelection()) {
557 // give a summary of the status of the selected files
558 const KFileItemList list = selectedItems();
559 if (list.isEmpty()) {
560 // when an item is triggered, it is temporary selected but selectedItems()
561 // will return an empty list
562 return text;
563 }
564
565 KFileItemList::const_iterator it = list.begin();
566 const KFileItemList::const_iterator end = list.end();
567 while (it != end) {
568 const KFileItem& item = *it;
569 if (item.isDir()) {
570 ++folderCount;
571 } else {
572 ++fileCount;
573 totalFileSize += item.size();
574 }
575 ++it;
576 }
577
578 if (folderCount + fileCount == 1) {
579 // if only one item is selected, show the filename
580 const QString name = list.first().name();
581 text = (folderCount == 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name) :
582 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
583 name, KIO::convertSize(totalFileSize));
584 } else {
585 // at least 2 items are selected
586 const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
587 const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
588 if ((folderCount > 0) && (fileCount > 0)) {
589 text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
590 foldersText, filesText, KIO::convertSize(totalFileSize));
591 } else if (fileCount > 0) {
592 text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize));
593 } else {
594 Q_ASSERT(folderCount > 0);
595 text = foldersText;
596 }
597 }
598 } else {
599 calculateItemCount(fileCount, folderCount, totalFileSize);
600 text = KIO::itemsSummaryString(fileCount + folderCount,
601 fileCount, folderCount,
602 totalFileSize, true);
603 }
604
605 return text;
606 }
607
608 void DolphinView::setUrl(const KUrl& url)
609 {
610 m_newFileNames.clear();
611 updateView(url, KUrl());
612 }
613
614 void DolphinView::changeSelection(const KFileItemList& selection)
615 {
616 clearSelection();
617 if (selection.isEmpty()) {
618 return;
619 }
620 const KUrl& baseUrl = url();
621 KUrl url;
622 QItemSelection newSelection;
623 foreach(const KFileItem& item, selection) {
624 url = item.url().upUrl();
625 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
626 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
627 newSelection.select(index, index);
628 }
629 }
630 itemView()->selectionModel()->select(newSelection,
631 QItemSelectionModel::ClearAndSelect
632 | QItemSelectionModel::Current);
633 }
634
635 void DolphinView::renameSelectedItems()
636 {
637 KFileItemList items = selectedItems();
638 const int itemCount = items.count();
639 if (itemCount < 1) {
640 return;
641 }
642
643 if (itemCount > 1) {
644 // More than one item has been selected for renaming. Open
645 // a rename dialog and rename all items afterwards.
646 QPointer<RenameDialog> dialog = new RenameDialog(this, items);
647 if (dialog->exec() == QDialog::Rejected) {
648 delete dialog;
649 return;
650 }
651
652 const QString newName = dialog->newName();
653 if (newName.isEmpty()) {
654 emit errorMessage(dialog->errorString());
655 delete dialog;
656 return;
657 }
658 delete dialog;
659
660 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
661 // as one operation instead of n rename operations like it is done now...
662 Q_ASSERT(newName.contains('#'));
663
664 // currently the items are sorted by the selection order, resort
665 // them by the file name
666 qSort(items.begin(), items.end(), lessThan);
667
668 // iterate through all selected items and rename them...
669 int index = 1;
670 foreach (const KFileItem& item, items) {
671 const KUrl& oldUrl = item.url();
672 QString number;
673 number.setNum(index++);
674
675 QString name = newName;
676 name.replace('#', number);
677
678 if (oldUrl.fileName() != name) {
679 KUrl newUrl = oldUrl;
680 newUrl.setFileName(name);
681 KonqOperations::rename(this, oldUrl, newUrl);
682 }
683 }
684 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
685 Q_ASSERT(itemCount == 1);
686
687 if (isColumnViewActive()) {
688 m_columnView->editItem(items.first());
689 } else {
690 const QModelIndex dirIndex = m_dolphinModel->indexForItem(items.first());
691 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
692 itemView()->edit(proxyIndex);
693 }
694 } else {
695 Q_ASSERT(itemCount == 1);
696
697 QPointer<RenameDialog> dialog = new RenameDialog(this, items);
698 if (dialog->exec() == QDialog::Rejected) {
699 delete dialog;
700 return;
701 }
702
703 const QString newName = dialog->newName();
704 if (newName.isEmpty()) {
705 emit errorMessage(dialog->errorString());
706 delete dialog;
707 return;
708 }
709 delete dialog;
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 addNewFileNames(event->mimeData());
989 DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
990 }
991
992 void DolphinView::updateSorting(DolphinView::Sorting sorting)
993 {
994 ViewProperties props(viewPropertiesUrl());
995 props.setSorting(sorting);
996
997 m_proxyModel->setSorting(sorting);
998
999 emit sortingChanged(sorting);
1000 }
1001
1002 void DolphinView::updateSortOrder(Qt::SortOrder order)
1003 {
1004 ViewProperties props(viewPropertiesUrl());
1005 props.setSortOrder(order);
1006
1007 m_proxyModel->setSortOrder(order);
1008
1009 emit sortOrderChanged(order);
1010 }
1011
1012 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
1013 {
1014 ViewProperties props(viewPropertiesUrl());
1015 props.setSortFoldersFirst(foldersFirst);
1016
1017 m_proxyModel->setSortFoldersFirst(foldersFirst);
1018
1019 emit sortFoldersFirstChanged(foldersFirst);
1020 }
1021
1022 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
1023 {
1024 ViewProperties props(viewPropertiesUrl());
1025 props.setAdditionalInfo(info);
1026 props.save();
1027
1028 m_fileItemDelegate->setShowInformation(info);
1029
1030 emit additionalInfoChanged();
1031 }
1032
1033 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
1034 {
1035 const bool enable = (m_mode == DolphinView::DetailsView) ||
1036 (m_mode == DolphinView::IconsView);
1037
1038 QAction* showSizeInfo = collection->action("show_size_info");
1039 QAction* showDateInfo = collection->action("show_date_info");
1040 QAction* showPermissionsInfo = collection->action("show_permissions_info");
1041 QAction* showOwnerInfo = collection->action("show_owner_info");
1042 QAction* showGroupInfo = collection->action("show_group_info");
1043 QAction* showMimeInfo = collection->action("show_mime_info");
1044
1045 showSizeInfo->setChecked(false);
1046 showDateInfo->setChecked(false);
1047 showPermissionsInfo->setChecked(false);
1048 showOwnerInfo->setChecked(false);
1049 showGroupInfo->setChecked(false);
1050 showMimeInfo->setChecked(false);
1051
1052 showSizeInfo->setEnabled(enable);
1053 showDateInfo->setEnabled(enable);
1054 showPermissionsInfo->setEnabled(enable);
1055 showOwnerInfo->setEnabled(enable);
1056 showGroupInfo->setEnabled(enable);
1057 showMimeInfo->setEnabled(enable);
1058
1059 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
1060 switch (info) {
1061 case KFileItemDelegate::Size:
1062 showSizeInfo->setChecked(true);
1063 break;
1064 case KFileItemDelegate::ModificationTime:
1065 showDateInfo->setChecked(true);
1066 break;
1067 case KFileItemDelegate::Permissions:
1068 showPermissionsInfo->setChecked(true);
1069 break;
1070 case KFileItemDelegate::Owner:
1071 showOwnerInfo->setChecked(true);
1072 break;
1073 case KFileItemDelegate::OwnerAndGroup:
1074 showGroupInfo->setChecked(true);
1075 break;
1076 case KFileItemDelegate::FriendlyMimeType:
1077 showMimeInfo->setChecked(true);
1078 break;
1079 default:
1080 break;
1081 }
1082 }
1083 }
1084
1085 QPair<bool, QString> DolphinView::pasteInfo() const
1086 {
1087 return KonqOperations::pasteInfo(url());
1088 }
1089
1090 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
1091 {
1092 m_tabsForFiles = tabsForFiles;
1093 }
1094
1095 bool DolphinView::isTabsForFilesEnabled() const
1096 {
1097 return m_tabsForFiles;
1098 }
1099
1100 void DolphinView::activateItem(const KUrl& url)
1101 {
1102 m_activeItemUrl = url;
1103 }
1104
1105 bool DolphinView::itemsExpandable() const
1106 {
1107 return (m_detailsView != 0) && m_detailsView->itemsExpandable();
1108 }
1109
1110 void DolphinView::deleteWhenNotDragSource(QAbstractItemView *view)
1111 {
1112 if (view == 0)
1113 return;
1114
1115 if (DragAndDropHelper::instance().isDragSource(view)) {
1116 // We must store for later deletion.
1117 if (m_expandedDragSource != 0) {
1118 // The old stored view is obviously not the drag source anymore.
1119 m_expandedDragSource->deleteLater();
1120 m_expandedDragSource = 0;
1121 }
1122 view->hide();
1123 m_expandedDragSource = view;
1124 }
1125 else {
1126 view->deleteLater();
1127 }
1128 }
1129
1130 void DolphinView::observeCreatedItem(const KUrl& url)
1131 {
1132 m_createdItemUrl = url;
1133 connect(m_dolphinModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1134 this, SLOT(selectAndScrollToCreatedItem()));
1135 }
1136
1137 void DolphinView::selectAndScrollToCreatedItem()
1138 {
1139 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_createdItemUrl);
1140 if (dirIndex.isValid()) {
1141 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
1142 itemView()->setCurrentIndex(proxyIndex);
1143 }
1144
1145 disconnect(m_dolphinModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1146 this, SLOT(selectAndScrollToCreatedItem()));
1147 m_createdItemUrl = KUrl();
1148 }
1149
1150 void DolphinView::restoreSelection()
1151 {
1152 disconnect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreSelection()));
1153 changeSelection(m_selectedItems);
1154 }
1155
1156 void DolphinView::emitContentsMoved()
1157 {
1158 // only emit the contents moved signal if:
1159 // - no directory loading is ongoing (this would reset the contents position
1160 // always to (0, 0))
1161 // - if the Column View is active: the column view does an automatic
1162 // positioning during the loading operation, which must be remembered
1163 if (!m_loadingDirectory || isColumnViewActive()) {
1164 const QPoint pos(contentsPosition());
1165 emit contentsMoved(pos.x(), pos.y());
1166 }
1167 }
1168
1169 void DolphinView::showHoverInformation(const KFileItem& item)
1170 {
1171 emit requestItemInfo(item);
1172 }
1173
1174 void DolphinView::clearHoverInformation()
1175 {
1176 emit requestItemInfo(KFileItem());
1177 }
1178
1179 void DolphinView::slotDeleteFileFinished(KJob* job)
1180 {
1181 if (job->error() == 0) {
1182 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1183 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1184 emit errorMessage(job->errorString());
1185 }
1186 }
1187
1188 void DolphinView::slotRequestUrlChange(const KUrl& url)
1189 {
1190 emit requestUrlChange(url);
1191 m_controller->setUrl(url);
1192 }
1193
1194 void DolphinView::slotDirListerCompleted()
1195 {
1196 if (!m_activeItemUrl.isEmpty()) {
1197 // assure that the current item remains visible
1198 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_activeItemUrl);
1199 if (dirIndex.isValid()) {
1200 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
1201 QAbstractItemView* view = itemView();
1202 const bool clearSelection = !hasSelection();
1203 view->setCurrentIndex(proxyIndex);
1204 if (clearSelection) {
1205 view->clearSelection();
1206 }
1207 m_activeItemUrl.clear();
1208 }
1209 }
1210
1211 if (!m_newFileNames.isEmpty()) {
1212 // select all newly added items created by a paste operation or
1213 // a drag & drop operation
1214 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1215 const int rowCount = m_proxyModel->rowCount();
1216 for (int row = 0; row < rowCount; ++row) {
1217 const QModelIndex proxyIndex = m_proxyModel->index(row, 0);
1218 const QModelIndex dirIndex = m_proxyModel->mapToSource(proxyIndex);
1219 const KUrl url = m_dolphinModel->itemForIndex(dirIndex).url();
1220 if (m_newFileNames.contains(url.fileName())) {
1221 selectionModel->select(proxyIndex, QItemSelectionModel::Select);
1222 }
1223 }
1224
1225 m_newFileNames.clear();
1226 }
1227 }
1228
1229 void DolphinView::slotRefreshItems()
1230 {
1231 if (m_assureVisibleCurrentIndex) {
1232 m_assureVisibleCurrentIndex = false;
1233 itemView()->scrollTo(itemView()->currentIndex());
1234 }
1235 }
1236
1237 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1238 {
1239 if (!url.isValid()) {
1240 const QString location(url.pathOrUrl());
1241 if (location.isEmpty()) {
1242 emit errorMessage(i18nc("@info:status", "The location is empty."));
1243 } else {
1244 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1245 }
1246 return;
1247 }
1248
1249 m_loadingDirectory = true;
1250
1251 if (reload) {
1252 m_selectedItems = selectedItems();
1253 connect(m_dirLister, SIGNAL(completed()), this, SLOT(restoreSelection()));
1254 }
1255
1256 m_dirLister->stop();
1257 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1258
1259 if (isColumnViewActive()) {
1260 // adjusting the directory lister is not enough in the case of the
1261 // column view, as each column has its own directory lister internally...
1262 if (reload) {
1263 m_columnView->reload();
1264 } else {
1265 m_columnView->showColumn(url);
1266 }
1267 }
1268 }
1269
1270 KUrl DolphinView::viewPropertiesUrl() const
1271 {
1272 if (isColumnViewActive()) {
1273 return m_columnView->rootUrl();
1274 }
1275
1276 return url();
1277 }
1278
1279 void DolphinView::applyViewProperties(const KUrl& url)
1280 {
1281 if (m_ignoreViewProperties) {
1282 return;
1283 }
1284
1285 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
1286 // The column view is active, hence don't apply the view properties
1287 // of sub directories (represented by columns) to the view. The
1288 // view always represents the properties of the first column.
1289 return;
1290 }
1291
1292 const ViewProperties props(url);
1293
1294 const Mode mode = props.viewMode();
1295 if (m_mode != mode) {
1296 const int oldZoomLevel = m_controller->zoomLevel();
1297
1298 m_mode = mode;
1299 createView();
1300 emit modeChanged();
1301
1302 updateZoomLevel(oldZoomLevel);
1303 }
1304 if (itemView() == 0) {
1305 createView();
1306 }
1307 Q_ASSERT(itemView() != 0);
1308 Q_ASSERT(m_fileItemDelegate != 0);
1309
1310 const bool showHiddenFiles = props.showHiddenFiles();
1311 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
1312 m_dirLister->setShowingDotFiles(showHiddenFiles);
1313 emit showHiddenFilesChanged();
1314 }
1315
1316 m_storedCategorizedSorting = props.categorizedSorting();
1317 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1318 if (categorized != m_proxyModel->isCategorizedModel()) {
1319 m_proxyModel->setCategorizedModel(categorized);
1320 emit categorizedSortingChanged();
1321 }
1322
1323 const DolphinView::Sorting sorting = props.sorting();
1324 if (sorting != m_proxyModel->sorting()) {
1325 m_proxyModel->setSorting(sorting);
1326 emit sortingChanged(sorting);
1327 }
1328
1329 const Qt::SortOrder sortOrder = props.sortOrder();
1330 if (sortOrder != m_proxyModel->sortOrder()) {
1331 m_proxyModel->setSortOrder(sortOrder);
1332 emit sortOrderChanged(sortOrder);
1333 }
1334
1335 const bool sortFoldersFirst = props.sortFoldersFirst();
1336 if (sortFoldersFirst != m_proxyModel->sortFoldersFirst()) {
1337 m_proxyModel->setSortFoldersFirst(sortFoldersFirst);
1338 emit sortFoldersFirstChanged(sortFoldersFirst);
1339 }
1340
1341 KFileItemDelegate::InformationList info = props.additionalInfo();
1342 if (info != m_fileItemDelegate->showInformation()) {
1343 m_fileItemDelegate->setShowInformation(info);
1344 emit additionalInfoChanged();
1345 }
1346
1347 const bool showPreview = props.showPreview();
1348 if (showPreview != m_showPreview) {
1349 m_showPreview = showPreview;
1350 m_previewGenerator->setPreviewShown(showPreview);
1351
1352 const int oldZoomLevel = m_controller->zoomLevel();
1353 emit showPreviewChanged();
1354
1355 // Enabling or disabling the preview might change the icon size of the view.
1356 // As the view does not emit a signal when the icon size has been changed,
1357 // the used zoom level of the controller must be adjusted manually:
1358 updateZoomLevel(oldZoomLevel);
1359 }
1360
1361 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1362 // During the lifetime of a DolphinView instance the global view properties
1363 // should not be changed. This allows e. g. to split a view and use different
1364 // view properties for each view.
1365 m_ignoreViewProperties = true;
1366 }
1367 }
1368
1369 void DolphinView::createView()
1370 {
1371 deleteView();
1372 Q_ASSERT(m_iconsView == 0);
1373 Q_ASSERT(m_detailsView == 0);
1374 Q_ASSERT(m_columnView == 0);
1375
1376 QAbstractItemView* view = 0;
1377 switch (m_mode) {
1378 case IconsView: {
1379 m_iconsView = new DolphinIconsView(this, m_controller);
1380 view = m_iconsView;
1381 break;
1382 }
1383
1384 case DetailsView:
1385 m_detailsView = new DolphinDetailsView(this, m_controller);
1386 view = m_detailsView;
1387 break;
1388
1389 case ColumnView:
1390 m_columnView = new DolphinColumnView(this, m_controller);
1391 view = m_columnView;
1392 break;
1393 }
1394
1395 Q_ASSERT(view != 0);
1396 view->installEventFilter(this);
1397 view->viewport()->installEventFilter(this);
1398 setFocusProxy(view);
1399
1400 if (m_mode != ColumnView) {
1401 // Give the view the ability to auto-expand its directories on hovering
1402 // (the column view takes care about this itself). If the details view
1403 // uses expandable folders, the auto-expanding should be used always.
1404 DolphinSettings& settings = DolphinSettings::instance();
1405 const bool enabled = settings.generalSettings()->autoExpandFolders() ||
1406 ((m_detailsView != 0) && settings.detailsModeSettings()->expandableFolders());
1407
1408 FolderExpander* folderExpander = new FolderExpander(view, m_proxyModel);
1409 folderExpander->setEnabled(enabled);
1410 connect(folderExpander, SIGNAL(enterDir(const QModelIndex&)),
1411 m_controller, SLOT(triggerItem(const QModelIndex&)));
1412 }
1413 else {
1414 // Listen out for requests to delete the current column.
1415 connect(m_columnView, SIGNAL(requestColumnDeletion(QAbstractItemView*)),
1416 this, SLOT(deleteWhenNotDragSource(QAbstractItemView*)));
1417 }
1418
1419 m_controller->setItemView(view);
1420
1421 m_fileItemDelegate = new DolphinFileItemDelegate(view);
1422 m_fileItemDelegate->setShowToolTipWhenElided(false);
1423 m_fileItemDelegate->setMinimizedNameColumn(m_mode == DetailsView);
1424 view->setItemDelegate(m_fileItemDelegate);
1425
1426 view->setModel(m_proxyModel);
1427 if (m_selectionModel != 0) {
1428 view->setSelectionModel(m_selectionModel);
1429 } else {
1430 m_selectionModel = view->selectionModel();
1431 }
1432
1433 // reparent the selection model, as it should not be deleted
1434 // when deleting the model
1435 m_selectionModel->setParent(this);
1436
1437 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1438
1439 m_previewGenerator = new KFilePreviewGenerator(view);
1440 m_previewGenerator->setPreviewShown(m_showPreview);
1441
1442 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1443 m_toolTipManager = new ToolTipManager(view, m_proxyModel);
1444 connect(m_controller, SIGNAL(hideToolTip()),
1445 m_toolTipManager, SLOT(hideTip()));
1446 }
1447
1448 m_topLayout->insertWidget(1, view);
1449
1450 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1451 this, SLOT(emitSelectionChangedSignal()));
1452 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1453 this, SLOT(emitContentsMoved()));
1454 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1455 this, SLOT(emitContentsMoved()));
1456 }
1457
1458 void DolphinView::deleteView()
1459 {
1460 QAbstractItemView* view = itemView();
1461 if (view != 0) {
1462 // It's important to set the keyboard focus to the parent
1463 // before deleting the view: Otherwise when having a split
1464 // view the other view will get the focus and will request
1465 // an activation (see DolphinView::eventFilter()).
1466 setFocusProxy(0);
1467 setFocus();
1468
1469 m_topLayout->removeWidget(view);
1470 view->close();
1471
1472 // m_previewGenerator's parent is not always destroyed, and we
1473 // don't want two active at once - manually delete.
1474 delete m_previewGenerator;
1475 m_previewGenerator = 0;
1476
1477 disconnect(view);
1478 m_controller->disconnect(view);
1479 view->disconnect();
1480
1481 deleteWhenNotDragSource(view);
1482 view = 0;
1483
1484 m_iconsView = 0;
1485 m_detailsView = 0;
1486 m_columnView = 0;
1487 m_fileItemDelegate = 0;
1488 m_toolTipManager = 0;
1489 }
1490 }
1491
1492 QAbstractItemView* DolphinView::itemView() const
1493 {
1494 if (m_detailsView != 0) {
1495 return m_detailsView;
1496 } else if (m_columnView != 0) {
1497 return m_columnView;
1498 }
1499
1500 return m_iconsView;
1501 }
1502
1503 void DolphinView::pasteToUrl(const KUrl& url)
1504 {
1505 addNewFileNames(QApplication::clipboard()->mimeData());
1506 KonqOperations::doPaste(this, url);
1507 }
1508
1509 void DolphinView::updateZoomLevel(int oldZoomLevel)
1510 {
1511 const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1512 if (oldZoomLevel != newZoomLevel) {
1513 m_controller->setZoomLevel(newZoomLevel);
1514 emit zoomLevelChanged(newZoomLevel);
1515 }
1516 }
1517
1518 KUrl::List DolphinView::simplifiedSelectedUrls() const
1519 {
1520 KUrl::List list = selectedUrls();
1521 if (itemsExpandable() ) {
1522 list = KDirModel::simplifiedUrlList(list);
1523 }
1524 return list;
1525 }
1526
1527 QMimeData* DolphinView::selectionMimeData() const
1528 {
1529 if (isColumnViewActive()) {
1530 return m_columnView->selectionMimeData();
1531 }
1532
1533 const QAbstractItemView* view = itemView();
1534 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
1535 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
1536 return m_dolphinModel->mimeData(selection.indexes());
1537 }
1538
1539 void DolphinView::addNewFileNames(const QMimeData* mimeData)
1540 {
1541 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1542 foreach (const KUrl& url, urls) {
1543 m_newFileNames.insert(url.fileName());
1544 }
1545 }
1546
1547 #include "dolphinview.moc"