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