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