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