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