]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Simplified the clickable resource metadata display. This also fixes a bug where somet...
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 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 <QAbstractItemView>
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QKeyEvent>
27 #include <QItemSelection>
28 #include <QBoxLayout>
29 #include <QTimer>
30 #include <QScrollBar>
31
32 #include <kactioncollection.h>
33 #include <kcolorscheme.h>
34 #include <kdirlister.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 "dolphincolumnviewcontainer.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 "renamedialog.h"
64 #include "settings/dolphinsettings.h"
65 #include "viewproperties.h"
66 #include "zoomlevelinfo.h"
67 #include "dolphindetailsviewexpander.h"
68
69 /**
70 * Helper function for sorting items with qSort() in
71 * DolphinView::renameSelectedItems().
72 */
73 bool lessThan(const KFileItem& item1, const KFileItem& item2)
74 {
75 return KStringHandler::naturalCompare(item1.name(), item2.name()) < 0;
76 }
77
78 DolphinView::DolphinView(QWidget* parent,
79 const KUrl& url,
80 DolphinSortFilterProxyModel* proxyModel) :
81 QWidget(parent),
82 m_active(true),
83 m_showPreview(false),
84 m_loadingDirectory(false),
85 m_storedCategorizedSorting(false),
86 m_tabsForFiles(false),
87 m_isContextMenuOpen(false),
88 m_ignoreViewProperties(false),
89 m_assureVisibleCurrentIndex(false),
90 m_mode(DolphinView::IconsView),
91 m_topLayout(0),
92 m_controller(0),
93 m_viewAccessor(proxyModel),
94 m_selectionModel(0),
95 m_selectionChangedTimer(0),
96 m_rootUrl(),
97 m_activeItemUrl(),
98 m_restoredContentsPosition(),
99 m_createdItemUrl(),
100 m_selectedItems(),
101 m_newFileNames()
102 {
103 m_topLayout = new QVBoxLayout(this);
104 m_topLayout->setSpacing(0);
105 m_topLayout->setMargin(0);
106
107 m_controller = new DolphinController(this);
108 m_controller->setUrl(url);
109
110 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
111 this, SIGNAL(urlChanged(const KUrl&)));
112 connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
113 this, SLOT(slotRequestUrlChange(const KUrl&)));
114
115 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&, const QList<QAction*>&)),
116 this, SLOT(openContextMenu(const QPoint&, const QList<QAction*>&)));
117 connect(m_controller, SIGNAL(urlsDropped(const KFileItem&, const KUrl&, QDropEvent*)),
118 this, SLOT(dropUrls(const KFileItem&, const KUrl&, QDropEvent*)));
119 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
120 this, SLOT(updateSorting(DolphinView::Sorting)));
121 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
122 this, SLOT(updateSortOrder(Qt::SortOrder)));
123 connect(m_controller, SIGNAL(sortFoldersFirstChanged(bool)),
124 this, SLOT(updateSortFoldersFirst(bool)));
125 connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
126 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
127 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
128 this, SLOT(triggerItem(const KFileItem&)));
129 connect(m_controller, SIGNAL(tabRequested(const KUrl&)),
130 this, SIGNAL(tabRequested(const KUrl&)));
131 connect(m_controller, SIGNAL(activated()),
132 this, SLOT(activate()));
133 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
134 this, SLOT(showHoverInformation(const KFileItem&)));
135 connect(m_controller, SIGNAL(viewportEntered()),
136 this, SLOT(clearHoverInformation()));
137
138 KDirLister* dirLister = m_viewAccessor.dirLister();
139 connect(dirLister, SIGNAL(redirection(KUrl,KUrl)),
140 this, SLOT(slotRedirection(KUrl,KUrl)));
141 connect(dirLister, SIGNAL(completed()),
142 this, SLOT(slotDirListerCompleted()));
143 connect(dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
144 this, SLOT(slotRefreshItems()));
145
146 // When a new item has been created by the "Create New..." menu, the item should
147 // get selected and it must be assured that the item will get visible. As the
148 // creation is done asynchronously, several signals must be checked:
149 connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl&)),
150 this, SLOT(observeCreatedItem(const KUrl&)));
151
152 m_selectionChangedTimer = new QTimer(this);
153 m_selectionChangedTimer->setSingleShot(true);
154 m_selectionChangedTimer->setInterval(300);
155 connect(m_selectionChangedTimer, SIGNAL(timeout()),
156 this, SLOT(emitSelectionChangedSignal()));
157
158 applyViewProperties();
159 m_topLayout->addWidget(m_viewAccessor.itemView());
160 }
161
162 DolphinView::~DolphinView()
163 {
164 }
165
166 const KUrl& DolphinView::url() const
167 {
168 return m_controller->url();
169 }
170
171 KUrl DolphinView::rootUrl() const
172 {
173 const KUrl viewUrl = url();
174 const KUrl root = m_viewAccessor.rootUrl();
175 if (root.isEmpty() || !root.isParentOf(viewUrl)) {
176 return viewUrl;
177 }
178 return root;
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 emitSelectionChangedSignal();
192 } else {
193 color.setAlpha(150);
194 }
195
196 QWidget* viewport = m_viewAccessor.itemView()->viewport();
197 QPalette palette;
198 palette.setColor(viewport->backgroundRole(), color);
199 viewport->setPalette(palette);
200
201 update();
202
203 if (active) {
204 m_viewAccessor.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 = rootUrl();
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_viewAccessor.itemDelegate()->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_viewAccessor.proxyModel()->isCategorizedModel()) {
244 m_viewAccessor.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_viewAccessor.dirLister()->showingDotFiles();
269 }
270
271 bool DolphinView::categorizedSorting() const
272 {
273 // If all view modes would support categorized sorting, returning
274 // m_viewAccessor.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_viewAccessor.supportsCategorizedSorting();
286 }
287
288 void DolphinView::selectAll()
289 {
290 QAbstractItemView* view = m_viewAccessor.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 QItemSelectionModel* selectionModel = m_viewAccessor.itemView()->selectionModel();
302 const QAbstractItemModel* itemModel = selectionModel->model();
303
304 const QModelIndex topLeft = itemModel->index(0, 0);
305 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
306 itemModel->columnCount() - 1);
307
308 const QItemSelection selection(topLeft, bottomRight);
309 selectionModel->select(selection, QItemSelectionModel::Toggle);
310 }
311
312 bool DolphinView::hasSelection() const
313 {
314 const QAbstractItemView* view = m_viewAccessor.itemView();
315 return view && view->selectionModel()->hasSelection();
316 }
317
318 void DolphinView::clearSelection()
319 {
320 QItemSelectionModel* selModel = m_viewAccessor.itemView()->selectionModel();
321 const QModelIndex currentIndex = selModel->currentIndex();
322 selModel->setCurrentIndex(currentIndex, QItemSelectionModel::Current |
323 QItemSelectionModel::Clear);
324 m_selectedItems.clear();
325 }
326
327 KFileItemList DolphinView::selectedItems() const
328 {
329 const QAbstractItemView* view = m_viewAccessor.itemView();
330
331 // Our view has a selection, we will map them back to the DolphinModel
332 // and then fill the KFileItemList.
333 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
334
335 const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
336 KFileItemList itemList;
337
338 const QModelIndexList indexList = selection.indexes();
339 foreach (const QModelIndex &index, indexList) {
340 KFileItem item = m_viewAccessor.dirModel()->itemForIndex(index);
341 if (!item.isNull()) {
342 itemList.append(item);
343 }
344 }
345
346 return itemList;
347 }
348
349 KUrl::List DolphinView::selectedUrls() const
350 {
351 KUrl::List urls;
352 const KFileItemList list = selectedItems();
353 foreach (const KFileItem &item, list) {
354 urls.append(item.url());
355 }
356 return urls;
357 }
358
359 int DolphinView::selectedItemsCount() const
360 {
361 return m_viewAccessor.itemView()->selectionModel()->selectedIndexes().count();
362 }
363
364 void DolphinView::setContentsPosition(int x, int y)
365 {
366 QAbstractItemView* view = m_viewAccessor.itemView();
367 Q_ASSERT(view != 0);
368 view->horizontalScrollBar()->setValue(x);
369 view->verticalScrollBar()->setValue(y);
370
371 m_loadingDirectory = false;
372 }
373
374 void DolphinView::setRestoredContentsPosition(const QPoint& pos)
375 {
376 // TODO: This function is called by DolphinViewContainer.
377 // If it makes use of DolphinView::restoreState(...) to restore the
378 // view state in KDE 4.5, this function can be removed.
379 m_restoredContentsPosition = pos;
380 }
381
382 QPoint DolphinView::contentsPosition() const
383 {
384 // TODO: If DolphinViewContainer uses DolphinView::saveState(...) to save the
385 // view state in KDE 4.5, this code can be moved to DolphinView::saveState.
386 QAbstractItemView* view = m_viewAccessor.itemView();
387 Q_ASSERT(view != 0);
388 const int x = view->horizontalScrollBar()->value();
389 const int y = view->verticalScrollBar()->value();
390 return QPoint(x, y);
391 }
392
393 void DolphinView::setZoomLevel(int level)
394 {
395 if (level < ZoomLevelInfo::minimumLevel()) {
396 level = ZoomLevelInfo::minimumLevel();
397 } else if (level > ZoomLevelInfo::maximumLevel()) {
398 level = ZoomLevelInfo::maximumLevel();
399 }
400
401 if (level != zoomLevel()) {
402 m_controller->setZoomLevel(level);
403 emit zoomLevelChanged(level);
404 }
405 }
406
407 int DolphinView::zoomLevel() const
408 {
409 return m_controller->zoomLevel();
410 }
411
412 void DolphinView::setSorting(Sorting sorting)
413 {
414 if (sorting != this->sorting()) {
415 updateSorting(sorting);
416 }
417 }
418
419 DolphinView::Sorting DolphinView::sorting() const
420 {
421 return m_viewAccessor.proxyModel()->sorting();
422 }
423
424 void DolphinView::setSortOrder(Qt::SortOrder order)
425 {
426 if (sortOrder() != order) {
427 updateSortOrder(order);
428 }
429 }
430
431 Qt::SortOrder DolphinView::sortOrder() const
432 {
433 return m_viewAccessor.proxyModel()->sortOrder();
434 }
435
436 void DolphinView::setSortFoldersFirst(bool foldersFirst)
437 {
438 if (sortFoldersFirst() != foldersFirst) {
439 updateSortFoldersFirst(foldersFirst);
440 }
441 }
442
443 bool DolphinView::sortFoldersFirst() const
444 {
445 return m_viewAccessor.proxyModel()->sortFoldersFirst();
446 }
447
448 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
449 {
450 const KUrl viewPropsUrl = rootUrl();
451 ViewProperties props(viewPropsUrl);
452 props.setAdditionalInfo(info);
453 m_viewAccessor.itemDelegate()->setShowInformation(info);
454
455 emit additionalInfoChanged();
456
457 if (m_viewAccessor.reloadOnAdditionalInfoChange()) {
458 loadDirectory(viewPropsUrl);
459 }
460 }
461
462 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
463 {
464 return m_viewAccessor.itemDelegate()->showInformation();
465 }
466
467 void DolphinView::reload()
468 {
469 setUrl(url());
470 loadDirectory(url(), true);
471 }
472
473 void DolphinView::refresh()
474 {
475 m_ignoreViewProperties = false;
476
477 const bool oldActivationState = m_active;
478 const int oldZoomLevel = m_controller->zoomLevel();
479 m_active = true;
480
481 createView();
482 applyViewProperties();
483 reload();
484
485 setActive(oldActivationState);
486 updateZoomLevel(oldZoomLevel);
487 }
488
489 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
490 {
491 Q_UNUSED(rootUrl); // TODO: remove after columnview-cleanup has been finished
492
493 if (m_controller->url() == url) {
494 return;
495 }
496
497 m_controller->setUrl(url); // emits urlChanged, which we forward
498 m_viewAccessor.prepareUrlChange(url);
499 applyViewProperties();
500 loadDirectory(url);
501
502 // When changing the URL there is no need to keep the version
503 // data of the previous URL.
504 m_viewAccessor.dirModel()->clearVersionData();
505
506 emit startedPathLoading(url);
507 }
508
509 void DolphinView::setNameFilter(const QString& nameFilter)
510 {
511 m_controller->setNameFilter(nameFilter);
512 }
513
514 void DolphinView::calculateItemCount(int& fileCount,
515 int& folderCount,
516 KIO::filesize_t& totalFileSize) const
517 {
518 foreach (const KFileItem& item, m_viewAccessor.dirLister()->items()) {
519 if (item.isDir()) {
520 ++folderCount;
521 } else {
522 ++fileCount;
523 totalFileSize += item.size();
524 }
525 }
526 }
527
528 QString DolphinView::statusBarText() const
529 {
530 QString text;
531 int folderCount = 0;
532 int fileCount = 0;
533 KIO::filesize_t totalFileSize = 0;
534
535 if (hasSelection()) {
536 // give a summary of the status of the selected files
537 const KFileItemList list = selectedItems();
538 if (list.isEmpty()) {
539 // when an item is triggered, it is temporary selected but selectedItems()
540 // will return an empty list
541 return text;
542 }
543
544 KFileItemList::const_iterator it = list.begin();
545 const KFileItemList::const_iterator end = list.end();
546 while (it != end) {
547 const KFileItem& item = *it;
548 if (item.isDir()) {
549 ++folderCount;
550 } else {
551 ++fileCount;
552 totalFileSize += item.size();
553 }
554 ++it;
555 }
556
557 if (folderCount + fileCount == 1) {
558 // if only one item is selected, show the filename
559 const QString name = list.first().name();
560 text = (folderCount == 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name) :
561 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
562 name, KIO::convertSize(totalFileSize));
563 } else {
564 // at least 2 items are selected
565 const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
566 const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
567 if ((folderCount > 0) && (fileCount > 0)) {
568 text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
569 foldersText, filesText, KIO::convertSize(totalFileSize));
570 } else if (fileCount > 0) {
571 text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize));
572 } else {
573 Q_ASSERT(folderCount > 0);
574 text = foldersText;
575 }
576 }
577 } else {
578 calculateItemCount(fileCount, folderCount, totalFileSize);
579 text = KIO::itemsSummaryString(fileCount + folderCount,
580 fileCount, folderCount,
581 totalFileSize, true);
582 }
583
584 return text;
585 }
586
587 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
588 {
589 return m_controller->versionControlActions(items);
590 }
591
592 void DolphinView::setUrl(const KUrl& url)
593 {
594 m_newFileNames.clear();
595 updateView(url, KUrl());
596 }
597
598 void DolphinView::changeSelection(const KFileItemList& selection)
599 {
600 clearSelection();
601 if (selection.isEmpty()) {
602 return;
603 }
604 const KUrl& baseUrl = url();
605 KUrl url;
606 QItemSelection newSelection;
607 foreach(const KFileItem& item, selection) {
608 url = item.url().upUrl();
609 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
610 QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item));
611 newSelection.select(index, index);
612 }
613 }
614 m_viewAccessor.itemView()->selectionModel()->select(newSelection,
615 QItemSelectionModel::ClearAndSelect
616 | QItemSelectionModel::Current);
617 }
618
619 void DolphinView::renameSelectedItems()
620 {
621 KFileItemList items = selectedItems();
622 const int itemCount = items.count();
623 if (itemCount < 1) {
624 return;
625 }
626
627 if (itemCount > 1) {
628 // More than one item has been selected for renaming. Open
629 // a rename dialog and rename all items afterwards.
630 QPointer<RenameDialog> dialog = new RenameDialog(this, items);
631 if (dialog->exec() == QDialog::Rejected) {
632 delete dialog;
633 return;
634 }
635
636 const QString newName = dialog->newName();
637 if (newName.isEmpty()) {
638 emit errorMessage(dialog->errorString());
639 delete dialog;
640 return;
641 }
642 delete dialog;
643
644 // the selection would be invalid after renaming the items, so just clear
645 // it before
646 clearSelection();
647
648 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
649 // as one operation instead of n rename operations like it is done now...
650 Q_ASSERT(newName.contains('#'));
651
652 // currently the items are sorted by the selection order, resort
653 // them by the file name
654 qSort(items.begin(), items.end(), lessThan);
655
656 // iterate through all selected items and rename them...
657 int index = 1;
658 foreach (const KFileItem& item, items) {
659 const KUrl& oldUrl = item.url();
660 QString number;
661 number.setNum(index++);
662
663 QString name = newName;
664 name.replace('#', number);
665
666 if (oldUrl.fileName() != name) {
667 KUrl newUrl = oldUrl;
668 newUrl.setFileName(name);
669 KonqOperations::rename(this, oldUrl, newUrl);
670 }
671 }
672 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
673 Q_ASSERT(itemCount == 1);
674 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
675 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
676 m_viewAccessor.itemView()->edit(proxyIndex);
677 } else {
678 Q_ASSERT(itemCount == 1);
679
680 QPointer<RenameDialog> dialog = new RenameDialog(this, items);
681 if (dialog->exec() == QDialog::Rejected) {
682 delete dialog;
683 return;
684 }
685
686 const QString newName = dialog->newName();
687 if (newName.isEmpty()) {
688 emit errorMessage(dialog->errorString());
689 delete dialog;
690 return;
691 }
692 delete dialog;
693
694 const KUrl& oldUrl = items.first().url();
695 KUrl newUrl = oldUrl;
696 newUrl.setFileName(newName);
697 KonqOperations::rename(this, oldUrl, newUrl);
698 }
699
700 // assure that the current index remains visible when KDirLister
701 // will notify the view about changed items
702 m_assureVisibleCurrentIndex = true;
703 }
704
705 void DolphinView::trashSelectedItems()
706 {
707 const KUrl::List list = simplifiedSelectedUrls();
708 KonqOperations::del(this, KonqOperations::TRASH, list);
709 }
710
711 void DolphinView::deleteSelectedItems()
712 {
713 const KUrl::List list = simplifiedSelectedUrls();
714 const bool del = KonqOperations::askDeleteConfirmation(list,
715 KonqOperations::DEL,
716 KonqOperations::DEFAULT_CONFIRMATION,
717 this);
718
719 if (del) {
720 KIO::Job* job = KIO::del(list);
721 connect(job, SIGNAL(result(KJob*)),
722 this, SLOT(slotDeleteFileFinished(KJob*)));
723 }
724 }
725
726 void DolphinView::cutSelectedItems()
727 {
728 QMimeData* mimeData = selectionMimeData();
729 KonqMimeData::addIsCutSelection(mimeData, true);
730 QApplication::clipboard()->setMimeData(mimeData);
731 }
732
733 void DolphinView::copySelectedItems()
734 {
735 QMimeData* mimeData = selectionMimeData();
736 QApplication::clipboard()->setMimeData(mimeData);
737 }
738
739 void DolphinView::paste()
740 {
741 pasteToUrl(url());
742 }
743
744 void DolphinView::pasteIntoFolder()
745 {
746 const KFileItemList items = selectedItems();
747 if ((items.count() == 1) && items.first().isDir()) {
748 pasteToUrl(items.first().url());
749 }
750 }
751
752 void DolphinView::setShowPreview(bool show)
753 {
754 if (m_showPreview == show) {
755 return;
756 }
757
758 const KUrl viewPropsUrl = rootUrl();
759 ViewProperties props(viewPropsUrl);
760 props.setShowPreview(show);
761
762 m_showPreview = show;
763 const int oldZoomLevel = m_controller->zoomLevel();
764 emit showPreviewChanged();
765
766 // Enabling or disabling the preview might change the icon size of the view.
767 // As the view does not emit a signal when the icon size has been changed,
768 // the used zoom level of the controller must be adjusted manually:
769 updateZoomLevel(oldZoomLevel);
770 }
771
772 void DolphinView::setShowHiddenFiles(bool show)
773 {
774 if (m_viewAccessor.dirLister()->showingDotFiles() == show) {
775 return;
776 }
777
778 const KUrl viewPropsUrl = rootUrl();
779 ViewProperties props(viewPropsUrl);
780 props.setShowHiddenFiles(show);
781
782 m_viewAccessor.dirLister()->setShowingDotFiles(show);
783 emit showHiddenFilesChanged();
784 }
785
786 void DolphinView::setCategorizedSorting(bool categorized)
787 {
788 if (categorized == categorizedSorting()) {
789 return;
790 }
791
792 // setCategorizedSorting(true) may only get invoked
793 // if the view supports categorized sorting
794 Q_ASSERT(!categorized || supportsCategorizedSorting());
795
796 ViewProperties props(rootUrl());
797 props.setCategorizedSorting(categorized);
798 props.save();
799
800 m_storedCategorizedSorting = categorized;
801 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
802
803 emit categorizedSortingChanged();
804 }
805
806 void DolphinView::toggleSortOrder()
807 {
808 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
809 Qt::DescendingOrder :
810 Qt::AscendingOrder;
811 setSortOrder(order);
812 }
813
814 void DolphinView::toggleSortFoldersFirst()
815 {
816 setSortFoldersFirst(!sortFoldersFirst());
817 }
818
819 void DolphinView::toggleAdditionalInfo(QAction* action)
820 {
821 const KFileItemDelegate::Information info =
822 static_cast<KFileItemDelegate::Information>(action->data().toInt());
823
824 KFileItemDelegate::InformationList list = additionalInfo();
825
826 const bool show = action->isChecked();
827
828 const int index = list.indexOf(info);
829 const bool containsInfo = (index >= 0);
830 if (show && !containsInfo) {
831 list.append(info);
832 setAdditionalInfo(list);
833 } else if (!show && containsInfo) {
834 list.removeAt(index);
835 setAdditionalInfo(list);
836 Q_ASSERT(list.indexOf(info) < 0);
837 }
838 }
839
840 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
841 {
842 QWidget::mouseReleaseEvent(event);
843 setActive(true);
844 }
845
846 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
847 {
848 switch (event->type()) {
849 case QEvent::FocusIn:
850 if (watched == m_viewAccessor.itemView()) {
851 m_controller->requestActivation();
852 }
853 break;
854
855 case QEvent::DragEnter:
856 if (watched == m_viewAccessor.itemView()->viewport()) {
857 setActive(true);
858 }
859 break;
860
861 case QEvent::KeyPress:
862 if (watched == m_viewAccessor.itemView()) {
863 // clear the selection when Escape has been pressed
864 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
865 if (keyEvent->key() == Qt::Key_Escape) {
866 clearSelection();
867 }
868 }
869 break;
870
871 case QEvent::Wheel:
872 if (watched == m_viewAccessor.itemView()->viewport()) {
873 // Ctrl+wheel events should cause icon zooming, but not if the left mouse button is pressed
874 // (the user is probably trying to scroll during a selection in that case)
875 QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
876 if (wheelEvent->modifiers() & Qt::ControlModifier && !(wheelEvent->buttons() & Qt::LeftButton)) {
877 const int delta = wheelEvent->delta();
878 const int level = zoomLevel();
879 if (delta > 0) {
880 setZoomLevel(level + 1);
881 } else if (delta < 0) {
882 setZoomLevel(level - 1);
883 }
884 return true;
885 }
886 }
887 break;
888
889 default:
890 break;
891 }
892
893 return QWidget::eventFilter(watched, event);
894 }
895
896 void DolphinView::activate()
897 {
898 setActive(true);
899 }
900
901 void DolphinView::triggerItem(const KFileItem& item)
902 {
903 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
904 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
905 // items are selected by the user, hence don't trigger the
906 // item specified by 'index'
907 return;
908 }
909
910 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
911 if (item.isNull() || m_isContextMenuOpen) {
912 return;
913 }
914
915 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
916 }
917
918 void DolphinView::emitDelayedSelectionChangedSignal()
919 {
920 // Invoke emitSelectionChangedSignal() with a delay of 300 ms. This assures
921 // that fast selection changes don't result in expensive operations to
922 // collect all file items for the signal (see DolphinView::selectedItems()).
923 m_selectionChangedTimer->start();
924 }
925
926 void DolphinView::emitSelectionChangedSignal()
927 {
928 emit selectionChanged(DolphinView::selectedItems());
929 }
930
931 void DolphinView::openContextMenu(const QPoint& pos,
932 const QList<QAction*>& customActions)
933 {
934 KFileItem item;
935 const QModelIndex index = m_viewAccessor.itemView()->indexAt(pos);
936 if (index.isValid() && (index.column() == DolphinModel::Name)) {
937 const QModelIndex dolphinModelIndex = m_viewAccessor.proxyModel()->mapToSource(index);
938 item = m_viewAccessor.dirModel()->itemForIndex(dolphinModelIndex);
939 }
940
941 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
942 emit requestContextMenu(item, url(), customActions);
943 m_isContextMenuOpen = false;
944 }
945
946 void DolphinView::dropUrls(const KFileItem& destItem,
947 const KUrl& destPath,
948 QDropEvent* event)
949 {
950 addNewFileNames(event->mimeData());
951 DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
952 }
953
954 void DolphinView::updateSorting(DolphinView::Sorting sorting)
955 {
956 ViewProperties props(rootUrl());
957 props.setSorting(sorting);
958
959 m_viewAccessor.proxyModel()->setSorting(sorting);
960
961 emit sortingChanged(sorting);
962 }
963
964 void DolphinView::updateSortOrder(Qt::SortOrder order)
965 {
966 ViewProperties props(rootUrl());
967 props.setSortOrder(order);
968
969 m_viewAccessor.proxyModel()->setSortOrder(order);
970
971 emit sortOrderChanged(order);
972 }
973
974 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
975 {
976 ViewProperties props(rootUrl());
977 props.setSortFoldersFirst(foldersFirst);
978
979 m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
980
981 emit sortFoldersFirstChanged(foldersFirst);
982 }
983
984 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
985 {
986 ViewProperties props(rootUrl());
987 props.setAdditionalInfo(info);
988 props.save();
989
990 m_viewAccessor.itemDelegate()->setShowInformation(info);
991
992 emit additionalInfoChanged();
993 }
994
995 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
996 {
997 const bool enable = (m_mode == DolphinView::DetailsView) ||
998 (m_mode == DolphinView::IconsView);
999
1000 QAction* showSizeInfo = collection->action("show_size_info");
1001 QAction* showDateInfo = collection->action("show_date_info");
1002 QAction* showPermissionsInfo = collection->action("show_permissions_info");
1003 QAction* showOwnerInfo = collection->action("show_owner_info");
1004 QAction* showGroupInfo = collection->action("show_group_info");
1005 QAction* showMimeInfo = collection->action("show_mime_info");
1006
1007 showSizeInfo->setChecked(false);
1008 showDateInfo->setChecked(false);
1009 showPermissionsInfo->setChecked(false);
1010 showOwnerInfo->setChecked(false);
1011 showGroupInfo->setChecked(false);
1012 showMimeInfo->setChecked(false);
1013
1014 showSizeInfo->setEnabled(enable);
1015 showDateInfo->setEnabled(enable);
1016 showPermissionsInfo->setEnabled(enable);
1017 showOwnerInfo->setEnabled(enable);
1018 showGroupInfo->setEnabled(enable);
1019 showMimeInfo->setEnabled(enable);
1020
1021 foreach (KFileItemDelegate::Information info, m_viewAccessor.itemDelegate()->showInformation()) {
1022 switch (info) {
1023 case KFileItemDelegate::Size:
1024 showSizeInfo->setChecked(true);
1025 break;
1026 case KFileItemDelegate::ModificationTime:
1027 showDateInfo->setChecked(true);
1028 break;
1029 case KFileItemDelegate::Permissions:
1030 showPermissionsInfo->setChecked(true);
1031 break;
1032 case KFileItemDelegate::Owner:
1033 showOwnerInfo->setChecked(true);
1034 break;
1035 case KFileItemDelegate::OwnerAndGroup:
1036 showGroupInfo->setChecked(true);
1037 break;
1038 case KFileItemDelegate::FriendlyMimeType:
1039 showMimeInfo->setChecked(true);
1040 break;
1041 default:
1042 break;
1043 }
1044 }
1045 }
1046
1047 QPair<bool, QString> DolphinView::pasteInfo() const
1048 {
1049 return KonqOperations::pasteInfo(url());
1050 }
1051
1052 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
1053 {
1054 m_tabsForFiles = tabsForFiles;
1055 }
1056
1057 bool DolphinView::isTabsForFilesEnabled() const
1058 {
1059 return m_tabsForFiles;
1060 }
1061
1062 void DolphinView::activateItem(const KUrl& url)
1063 {
1064 // TODO: If DolphinViewContainer uses DolphinView::restoreState(...) to restore the
1065 // view state in KDE 4.5, this function can be removed.
1066 m_activeItemUrl = url;
1067 }
1068
1069 bool DolphinView::itemsExpandable() const
1070 {
1071 return m_viewAccessor.itemsExpandable();
1072 }
1073
1074 void DolphinView::restoreState(QDataStream &stream)
1075 {
1076 // current item
1077 stream >> m_activeItemUrl;
1078
1079 // view position
1080 stream >> m_restoredContentsPosition;
1081
1082 // expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1083 QSet<KUrl> urlsToExpand;
1084 stream >> urlsToExpand;
1085 const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
1086
1087 if (expander) {
1088 m_expanderActive = true;
1089 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
1090 }
1091 else {
1092 m_expanderActive = false;
1093 }
1094 }
1095
1096 void DolphinView::saveState(QDataStream &stream)
1097 {
1098 // current item
1099 KFileItem currentItem;
1100 const QAbstractItemView* view = m_viewAccessor.itemView();
1101
1102 if(view) {
1103 const QModelIndex proxyIndex = view->currentIndex();
1104 const QModelIndex dirModelIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
1105 currentItem = m_viewAccessor.dirModel()->itemForIndex(dirModelIndex);
1106 }
1107
1108 KUrl currentUrl;
1109 if (!currentItem.isNull())
1110 currentUrl = currentItem.url();
1111
1112 stream << currentUrl;
1113
1114 // view position
1115 stream << contentsPosition();
1116
1117 // expanded folders (only relevant for the details view - the set will be empty in other view modes)
1118 stream << m_viewAccessor.expandedUrls();
1119 }
1120
1121 void DolphinView::observeCreatedItem(const KUrl& url)
1122 {
1123 m_createdItemUrl = url;
1124 connect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1125 this, SLOT(selectAndScrollToCreatedItem()));
1126 }
1127
1128 void DolphinView::selectAndScrollToCreatedItem()
1129 {
1130 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1131 if (dirIndex.isValid()) {
1132 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1133 m_viewAccessor.itemView()->setCurrentIndex(proxyIndex);
1134 }
1135
1136 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
1137 this, SLOT(selectAndScrollToCreatedItem()));
1138 m_createdItemUrl = KUrl();
1139 }
1140
1141 void DolphinView::restoreSelection()
1142 {
1143 disconnect(m_viewAccessor.dirLister(), SIGNAL(completed()), this, SLOT(restoreSelection()));
1144 changeSelection(m_selectedItems);
1145 }
1146
1147 void DolphinView::emitContentsMoved()
1148 {
1149 // TODO: If DolphinViewContainer uses DolphinView::saveState(...) to save the
1150 // view state in KDE 4.5, the contentsMoved signal might not be needed anymore,
1151 // depending on how the implementation is done.
1152 // In that case, the code in contentsPosition() can be moved to saveState().
1153
1154 // only emit the contents moved signal if no directory loading is ongoing
1155 // (this would reset the contents position always to (0, 0))
1156 if (!m_loadingDirectory) {
1157 const QPoint pos(contentsPosition());
1158 emit contentsMoved(pos.x(), pos.y());
1159 }
1160 }
1161
1162 void DolphinView::showHoverInformation(const KFileItem& item)
1163 {
1164 emit requestItemInfo(item);
1165 }
1166
1167 void DolphinView::clearHoverInformation()
1168 {
1169 emit requestItemInfo(KFileItem());
1170 }
1171
1172 void DolphinView::slotDeleteFileFinished(KJob* job)
1173 {
1174 if (job->error() == 0) {
1175 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1176 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1177 emit errorMessage(job->errorString());
1178 }
1179 }
1180
1181 void DolphinView::slotRequestUrlChange(const KUrl& url)
1182 {
1183 emit requestUrlChange(url);
1184 m_controller->setUrl(url);
1185 }
1186
1187 void DolphinView::slotDirListerCompleted()
1188 {
1189 if (!m_expanderActive) {
1190 slotLoadingCompleted();
1191 }
1192
1193 if (!m_newFileNames.isEmpty()) {
1194 // select all newly added items created by a paste operation or
1195 // a drag & drop operation
1196 const int rowCount = m_viewAccessor.proxyModel()->rowCount();
1197 QItemSelection selection;
1198 for (int row = 0; row < rowCount; ++row) {
1199 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->index(row, 0);
1200 const QModelIndex dirIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
1201 const KUrl url = m_viewAccessor.dirModel()->itemForIndex(dirIndex).url();
1202 if (m_newFileNames.contains(url.fileName())) {
1203 selection.merge(QItemSelection(proxyIndex, proxyIndex), QItemSelectionModel::Select);
1204 }
1205 }
1206 m_viewAccessor.itemView()->selectionModel()->select(selection, QItemSelectionModel::Select);
1207
1208 m_newFileNames.clear();
1209 }
1210 }
1211
1212 void DolphinView::slotLoadingCompleted()
1213 {
1214 m_expanderActive = false;
1215 m_loadingDirectory = false;
1216
1217 if (!m_activeItemUrl.isEmpty()) {
1218 // assure that the current item remains visible
1219 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_activeItemUrl);
1220 if (dirIndex.isValid()) {
1221 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1222 QAbstractItemView* view = m_viewAccessor.itemView();
1223 const bool clearSelection = !hasSelection();
1224 view->setCurrentIndex(proxyIndex);
1225 if (clearSelection) {
1226 view->clearSelection();
1227 }
1228 m_activeItemUrl.clear();
1229 }
1230 }
1231
1232 // Restore the contents position. This has to be done using a Qt::QueuedConnection
1233 // because the view might not be in its final state yet.
1234 QMetaObject::invokeMethod(this, "restoreContentsPosition", Qt::QueuedConnection);
1235 }
1236
1237 void DolphinView::slotRefreshItems()
1238 {
1239 if (m_assureVisibleCurrentIndex) {
1240 m_assureVisibleCurrentIndex = false;
1241 m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1242 }
1243 }
1244
1245 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1246 {
1247 if (!url.isValid()) {
1248 const QString location(url.pathOrUrl());
1249 if (location.isEmpty()) {
1250 emit errorMessage(i18nc("@info:status", "The location is empty."));
1251 } else {
1252 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1253 }
1254 return;
1255 }
1256
1257 m_loadingDirectory = true;
1258 m_expanderActive = false;
1259
1260 if (reload) {
1261 m_selectedItems = selectedItems();
1262 connect(m_viewAccessor.dirLister(), SIGNAL(completed()), this, SLOT(restoreSelection()));
1263 }
1264
1265 m_viewAccessor.dirLister()->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1266 }
1267
1268 void DolphinView::applyViewProperties()
1269 {
1270 if (m_ignoreViewProperties) {
1271 return;
1272 }
1273
1274 const ViewProperties props(rootUrl());
1275
1276 const Mode mode = props.viewMode();
1277 if (m_mode != mode) {
1278 const int oldZoomLevel = m_controller->zoomLevel();
1279
1280 m_mode = mode;
1281 createView();
1282 emit modeChanged();
1283
1284 updateZoomLevel(oldZoomLevel);
1285 }
1286 if (m_viewAccessor.itemView() == 0) {
1287 createView();
1288 }
1289 Q_ASSERT(m_viewAccessor.itemView() != 0);
1290 Q_ASSERT(m_viewAccessor.itemDelegate() != 0);
1291
1292 const bool showHiddenFiles = props.showHiddenFiles();
1293 if (showHiddenFiles != m_viewAccessor.dirLister()->showingDotFiles()) {
1294 m_viewAccessor.dirLister()->setShowingDotFiles(showHiddenFiles);
1295 emit showHiddenFilesChanged();
1296 }
1297
1298 m_storedCategorizedSorting = props.categorizedSorting();
1299 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1300 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1301 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1302 emit categorizedSortingChanged();
1303 }
1304
1305 const DolphinView::Sorting sorting = props.sorting();
1306 if (sorting != m_viewAccessor.proxyModel()->sorting()) {
1307 m_viewAccessor.proxyModel()->setSorting(sorting);
1308 emit sortingChanged(sorting);
1309 }
1310
1311 const Qt::SortOrder sortOrder = props.sortOrder();
1312 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1313 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1314 emit sortOrderChanged(sortOrder);
1315 }
1316
1317 const bool sortFoldersFirst = props.sortFoldersFirst();
1318 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1319 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1320 emit sortFoldersFirstChanged(sortFoldersFirst);
1321 }
1322
1323 KFileItemDelegate::InformationList info = props.additionalInfo();
1324 if (info != m_viewAccessor.itemDelegate()->showInformation()) {
1325 m_viewAccessor.itemDelegate()->setShowInformation(info);
1326 emit additionalInfoChanged();
1327 }
1328
1329 const bool showPreview = props.showPreview();
1330 if (showPreview != m_showPreview) {
1331 m_showPreview = showPreview;
1332 const int oldZoomLevel = m_controller->zoomLevel();
1333 emit showPreviewChanged();
1334
1335 // Enabling or disabling the preview might change the icon size of the view.
1336 // As the view does not emit a signal when the icon size has been changed,
1337 // the used zoom level of the controller must be adjusted manually:
1338 updateZoomLevel(oldZoomLevel);
1339 }
1340
1341 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1342 // During the lifetime of a DolphinView instance the global view properties
1343 // should not be changed. This allows e. g. to split a view and use different
1344 // view properties for each view.
1345 m_ignoreViewProperties = true;
1346 }
1347 }
1348
1349 void DolphinView::createView()
1350 {
1351 deleteView();
1352
1353 Q_ASSERT(m_viewAccessor.itemView() == 0);
1354 m_viewAccessor.createView(this, m_controller, m_mode);
1355
1356 QAbstractItemView* view = m_viewAccessor.itemView();
1357 Q_ASSERT(view != 0);
1358 view->installEventFilter(this);
1359 view->viewport()->installEventFilter(this);
1360
1361 m_controller->setItemView(view);
1362 connect(m_controller, SIGNAL(selectionChanged()),
1363 this, SLOT(emitDelayedSelectionChangedSignal()));
1364
1365 // When changing the view mode, the selection is lost due to reinstantiating
1366 // a new item view with a custom selection model. Pass the ownership of the
1367 // selection model to DolphinView, so that it can be shared by all item views.
1368 if (m_selectionModel != 0) {
1369 view->setSelectionModel(m_selectionModel);
1370 } else {
1371 m_selectionModel = view->selectionModel();
1372 }
1373 m_selectionModel->setParent(this);
1374
1375 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1376 this, SLOT(emitContentsMoved()));
1377 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1378 this, SLOT(emitContentsMoved()));
1379
1380 setFocusProxy(m_viewAccessor.layoutTarget());
1381 m_topLayout->insertWidget(1, m_viewAccessor.layoutTarget());
1382 }
1383
1384 void DolphinView::deleteView()
1385 {
1386 QAbstractItemView* view = m_viewAccessor.itemView();
1387 if (view != 0) {
1388 // It's important to set the keyboard focus to the parent
1389 // before deleting the view: Otherwise when having a split
1390 // view the other view will get the focus and will request
1391 // an activation (see DolphinView::eventFilter()).
1392 setFocusProxy(0);
1393 setFocus();
1394
1395 m_topLayout->removeWidget(view);
1396 view->close();
1397
1398 // disconnect all signal/slots
1399 disconnect(view);
1400 m_controller->disconnect(view);
1401 view->disconnect();
1402 disconnect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1403 this, SLOT(emitContentsMoved()));
1404 disconnect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1405 this, SLOT(emitContentsMoved()));
1406
1407 m_viewAccessor.deleteView();
1408 }
1409 }
1410
1411 void DolphinView::pasteToUrl(const KUrl& url)
1412 {
1413 addNewFileNames(QApplication::clipboard()->mimeData());
1414 KonqOperations::doPaste(this, url);
1415 }
1416
1417 void DolphinView::updateZoomLevel(int oldZoomLevel)
1418 {
1419 const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1420 if (oldZoomLevel != newZoomLevel) {
1421 m_controller->setZoomLevel(newZoomLevel);
1422 emit zoomLevelChanged(newZoomLevel);
1423 }
1424 }
1425
1426 KUrl::List DolphinView::simplifiedSelectedUrls() const
1427 {
1428 KUrl::List list = selectedUrls();
1429 if (itemsExpandable() ) {
1430 list = KDirModel::simplifiedUrlList(list);
1431 }
1432 return list;
1433 }
1434
1435 QMimeData* DolphinView::selectionMimeData() const
1436 {
1437 const QAbstractItemView* view = m_viewAccessor.itemView();
1438 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
1439 const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
1440 return m_viewAccessor.dirModel()->mimeData(selection.indexes());
1441 }
1442
1443 void DolphinView::addNewFileNames(const QMimeData* mimeData)
1444 {
1445 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1446 foreach (const KUrl& url, urls) {
1447 m_newFileNames.insert(url.fileName());
1448 }
1449 }
1450
1451 DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel* proxyModel) :
1452 m_iconsView(0),
1453 m_detailsView(0),
1454 m_columnsContainer(0),
1455 m_proxyModel(proxyModel),
1456 m_dragSource(0)
1457 {
1458 }
1459
1460 DolphinView::ViewAccessor::~ViewAccessor()
1461 {
1462 delete m_dragSource;
1463 m_dragSource = 0;
1464 }
1465
1466 void DolphinView::ViewAccessor::createView(QWidget* parent,
1467 DolphinController* controller,
1468 Mode mode)
1469 {
1470 Q_ASSERT(itemView() == 0);
1471
1472 switch (mode) {
1473 case IconsView:
1474 m_iconsView = new DolphinIconsView(parent, controller, m_proxyModel);
1475 break;
1476
1477 case DetailsView:
1478 m_detailsView = new DolphinDetailsView(parent, controller, m_proxyModel);
1479 break;
1480
1481 case ColumnView:
1482 m_columnsContainer = new DolphinColumnViewContainer(parent, controller);
1483 break;
1484
1485 default:
1486 Q_ASSERT(false);
1487 }
1488 }
1489
1490 void DolphinView::ViewAccessor::deleteView()
1491 {
1492 QAbstractItemView* view = itemView();
1493 if (view != 0) {
1494 if (DragAndDropHelper::instance().isDragSource(view)) {
1495 // The view is a drag source (the feature "Open folders
1496 // during drag operations" is used). Deleting the view
1497 // during an ongoing drag operation is not allowed, so
1498 // this will postponed.
1499 if (m_dragSource != 0) {
1500 // the old stored view is obviously not the drag source anymore
1501 m_dragSource->deleteLater();
1502 m_dragSource = 0;
1503 }
1504 view->hide();
1505 m_dragSource = view;
1506 } else {
1507 view->deleteLater();
1508 }
1509 }
1510
1511 m_iconsView = 0;
1512 m_detailsView = 0;
1513
1514 if (m_columnsContainer != 0) {
1515 m_columnsContainer->deleteLater();
1516 }
1517 m_columnsContainer = 0;
1518 }
1519
1520
1521 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl& url)
1522 {
1523 if (m_columnsContainer != 0) {
1524 m_columnsContainer->showColumn(url);
1525 }
1526
1527 if(!m_detailsViewExpander.isNull()) {
1528 // Stop expanding items in the current folder
1529 m_detailsViewExpander->stop();
1530 }
1531 }
1532
1533 QAbstractItemView* DolphinView::ViewAccessor::itemView() const
1534 {
1535 if (m_iconsView != 0) {
1536 return m_iconsView;
1537 }
1538
1539 if (m_detailsView != 0) {
1540 return m_detailsView;
1541 }
1542
1543 if (m_columnsContainer != 0) {
1544 return m_columnsContainer->activeColumn();
1545 }
1546
1547 return 0;
1548 }
1549
1550 KFileItemDelegate* DolphinView::ViewAccessor::itemDelegate() const
1551 {
1552 return static_cast<KFileItemDelegate*>(itemView()->itemDelegate());
1553 }
1554
1555 QWidget* DolphinView::ViewAccessor::layoutTarget() const
1556 {
1557 if (m_columnsContainer != 0) {
1558 return m_columnsContainer;
1559 }
1560 return itemView();
1561 }
1562
1563 KUrl DolphinView::ViewAccessor::rootUrl() const
1564 {
1565 return (m_columnsContainer != 0) ? m_columnsContainer->rootUrl() : KUrl();
1566 }
1567
1568 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
1569 {
1570 return m_iconsView != 0;
1571 }
1572
1573 bool DolphinView::ViewAccessor::itemsExpandable() const
1574 {
1575 return (m_detailsView != 0) && m_detailsView->itemsExpandable();
1576 }
1577
1578
1579 QSet<KUrl> DolphinView::ViewAccessor::expandedUrls() const
1580 {
1581 if(m_detailsView != 0) {
1582 return m_detailsView->expandedUrls();
1583 }
1584 else {
1585 return QSet<KUrl>();
1586 }
1587 }
1588
1589 const DolphinDetailsViewExpander* DolphinView::ViewAccessor::setExpandedUrls(const QSet<KUrl>& urlsToExpand)
1590 {
1591 if((m_detailsView != 0) && m_detailsView->itemsExpandable() && !urlsToExpand.isEmpty()) {
1592 m_detailsViewExpander = new DolphinDetailsViewExpander(m_detailsView, urlsToExpand);
1593 return m_detailsViewExpander;
1594 }
1595 else {
1596 return 0;
1597 }
1598 }
1599
1600 bool DolphinView::ViewAccessor::reloadOnAdditionalInfoChange() const
1601 {
1602 // the details view requires no reloading of the directory, as it maps
1603 // the file item delegate info to its columns internally
1604 return m_detailsView != 0;
1605 }
1606
1607 DolphinModel* DolphinView::ViewAccessor::dirModel() const
1608 {
1609 return static_cast<DolphinModel*>(proxyModel()->sourceModel());
1610 }
1611
1612 DolphinSortFilterProxyModel* DolphinView::ViewAccessor::proxyModel() const
1613 {
1614 if (m_columnsContainer != 0) {
1615 return static_cast<DolphinSortFilterProxyModel*>(m_columnsContainer->activeColumn()->model());
1616 }
1617 return m_proxyModel;
1618 }
1619
1620 KDirLister* DolphinView::ViewAccessor::dirLister() const
1621 {
1622 return dirModel()->dirLister();
1623 }
1624
1625 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1626 {
1627 emit redirection(oldUrl, newUrl);
1628 m_controller->redirectToUrl(newUrl); // #186947
1629 }
1630
1631 void DolphinView::restoreContentsPosition()
1632 {
1633 if (!m_restoredContentsPosition.isNull()) {
1634 setContentsPosition(m_restoredContentsPosition.x(), m_restoredContentsPosition.y());
1635 m_restoredContentsPosition = QPoint();
1636 }
1637 }
1638
1639 #include "dolphinview.moc"