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