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