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