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