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