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