]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
First step to reactivate version control plugin functionality
[dolphin.git] / src / views / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QAbstractItemView>
24 #include <QApplication>
25 #include <QBoxLayout>
26 #include <QClipboard>
27 #include <QDropEvent>
28 #include <QGraphicsSceneDragDropEvent>
29 #include <QKeyEvent>
30 #include <QItemSelection>
31 #include <QTimer>
32 #include <QScrollBar>
33
34 #include <KActionCollection>
35 #include <KColorScheme>
36 #include <KDirLister>
37 #include <KDirModel>
38 #include <KIconEffect>
39 #include <KFileItem>
40 #include <KFileItemListProperties>
41 #include <KLocale>
42 #include <kitemviews/kfileitemmodel.h>
43 #include <kitemviews/kfileitemlistview.h>
44 #include <kitemviews/kitemlistselectionmanager.h>
45 #include <kitemviews/kitemlistview.h>
46 #include <kitemviews/kitemlistcontroller.h>
47 #include <KIO/DeleteJob>
48 #include <KIO/NetAccess>
49 #include <KIO/PreviewJob>
50 #include <KJob>
51 #include <KMenu>
52 #include <KMessageBox>
53 #include <konq_fileitemcapabilities.h>
54 #include <konq_operations.h>
55 #include <konqmimedata.h>
56 #include <KToggleAction>
57 #include <KUrl>
58
59 #include "additionalinfoaccessor.h"
60 #include "dolphindirlister.h"
61 #include "dolphinnewfilemenuobserver.h"
62 #include "dolphin_detailsmodesettings.h"
63 #include "dolphin_generalsettings.h"
64 #include "dolphinitemlistcontainer.h"
65 #include "draganddrophelper.h"
66 #include "renamedialog.h"
67 #include "settings/dolphinsettings.h"
68 #include "versioncontrol/versioncontrolobserver.h"
69 #include "viewmodecontroller.h"
70 #include "viewproperties.h"
71 #include "views/tooltips/tooltipmanager.h"
72 #include "zoomlevelinfo.h"
73
74 namespace {
75 const int MaxModeEnum = DolphinView::CompactView;
76 const int MaxSortingEnum = DolphinView::SortByPath;
77 };
78
79 DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
80 QWidget(parent),
81 m_active(true),
82 m_tabsForFiles(false),
83 m_assureVisibleCurrentIndex(false),
84 m_isFolderWritable(true),
85 m_url(url),
86 m_mode(DolphinView::IconsView),
87 m_additionalInfoList(),
88 m_topLayout(0),
89 m_dirLister(0),
90 m_container(0),
91 m_toolTipManager(0),
92 m_selectionChangedTimer(0),
93 m_currentItemIndex(-1),
94 m_restoredContentsPosition(),
95 m_createdItemUrl(),
96 m_selectedItems(),
97 m_versionControlObserver(0)
98 {
99 m_topLayout = new QVBoxLayout(this);
100 m_topLayout->setSpacing(0);
101 m_topLayout->setMargin(0);
102
103 //m_dolphinViewController = new DolphinViewController(this);
104
105 //m_viewModeController = new ViewModeController(this);
106 //m_viewModeController->setUrl(url);
107
108 /*connect(m_viewModeController, SIGNAL(urlChanged(KUrl)),
109 this, SIGNAL(urlChanged(KUrl)));
110
111 connect(m_dolphinViewController, SIGNAL(requestContextMenu(QPoint,QList<QAction*>)),
112 this, SLOT(openContextMenu(QPoint,QList<QAction*>)));
113 connect(m_dolphinViewController, SIGNAL(urlsDropped(KFileItem,KUrl,QDropEvent*)),
114 this, SLOT(dropUrls(KFileItem,KUrl,QDropEvent*)));
115 connect(m_dolphinViewController, SIGNAL(sortingChanged(DolphinView::Sorting)),
116 this, SLOT(updateSorting(DolphinView::Sorting)));
117 connect(m_dolphinViewController, SIGNAL(sortOrderChanged(Qt::SortOrder)),
118 this, SLOT(updateSortOrder(Qt::SortOrder)));
119 connect(m_dolphinViewController, SIGNAL(sortFoldersFirstChanged(bool)),
120 this, SLOT(updateSortFoldersFirst(bool)));
121 connect(m_dolphinViewController, SIGNAL(additionalInfoChanged(QList<DolphinView::AdditionalInfo>)),
122 this, SLOT(updateAdditionalInfo(QList<DolphinView::AdditionalInfo>)));*/
123 //connect(m_dolphinViewController, SIGNAL(itemActivated(KFileItem)),
124 // this, SLOT(triggerItem(KFileItem)));
125 //connect(m_dolphinViewController, SIGNAL(tabRequested(KUrl)),
126 // this, SIGNAL(tabRequested(KUrl)));
127 /*connect(m_dolphinViewController, SIGNAL(activated()),
128 this, SLOT(activate()));
129 connect(m_dolphinViewController, SIGNAL(itemEntered(KFileItem)),
130 this, SLOT(showHoverInformation(KFileItem)));
131 connect(m_dolphinViewController, SIGNAL(viewportEntered()),
132 this, SLOT(clearHoverInformation()));
133 connect(m_dolphinViewController, SIGNAL(urlChangeRequested(KUrl)),
134 this, SLOT(slotUrlChangeRequested(KUrl)));*/
135
136 // When a new item has been created by the "Create New..." menu, the item should
137 // get selected and it must be assured that the item will get visible. As the
138 // creation is done asynchronously, several signals must be checked:
139 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl)),
140 this, SLOT(observeCreatedItem(KUrl)));
141
142 m_selectionChangedTimer = new QTimer(this);
143 m_selectionChangedTimer->setSingleShot(true);
144 m_selectionChangedTimer->setInterval(300);
145 connect(m_selectionChangedTimer, SIGNAL(timeout()),
146 this, SLOT(emitSelectionChangedSignal()));
147
148 m_dirLister = new DolphinDirLister(this);
149 m_dirLister->setAutoUpdate(true);
150 m_dirLister->setDelayedMimeTypes(true);
151
152 connect(m_dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(slotRedirection(KUrl,KUrl)));
153 connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotDirListerStarted(KUrl)));
154 connect(m_dirLister, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
155 connect(m_dirLister, SIGNAL(refreshItems(QList<QPair<KFileItem,KFileItem> >)),
156 this, SLOT(slotRefreshItems()));
157
158 connect(m_dirLister, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
159 connect(m_dirLister, SIGNAL(newItems(KFileItemList)), this, SIGNAL(itemCountChanged()));
160 connect(m_dirLister, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
161 connect(m_dirLister, SIGNAL(errorMessage(QString)), this, SIGNAL(infoMessage(QString)));
162 connect(m_dirLister, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
163 connect(m_dirLister, SIGNAL(urlIsFileError(KUrl)), this, SIGNAL(urlIsFileError(KUrl)));
164 connect(m_dirLister, SIGNAL(itemsDeleted(KFileItemList)), this, SIGNAL(itemCountChanged()));
165
166 m_container = new DolphinItemListContainer(m_dirLister, this);
167 QHash<QByteArray, int> visibleRoles;
168 visibleRoles.insert("name", 0);
169 m_container->setVisibleRoles(visibleRoles);
170
171 KItemListController* controller = m_container->controller();
172 controller->setSelectionBehavior(KItemListController::MultiSelection);
173 connect(controller, SIGNAL(itemActivated(int)),
174 this, SLOT(slotItemActivated(int)));
175 connect(controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
176 connect(controller, SIGNAL(contextMenuRequested(int,QPointF)), this, SLOT(slotContextMenuRequested(int,QPointF)));
177 connect(controller, SIGNAL(itemExpansionToggleClicked(int)), this, SLOT(slotItemExpansionToggleClicked(int)));
178 connect(controller, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
179 connect(controller, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
180 connect(controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
181 connect(controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)), this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
182
183 KItemListSelectionManager* selectionManager = controller->selectionManager();
184 connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)),
185 this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
186
187 m_toolTipManager = new ToolTipManager(this);
188
189 m_versionControlObserver = new VersionControlObserver(this);
190 m_versionControlObserver->setModel(fileItemModel());
191 connect(m_versionControlObserver, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
192 connect(m_versionControlObserver, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString)));
193 connect(m_versionControlObserver, SIGNAL(operationCompletedMessage(QString)), this, SIGNAL(operationCompletedMessage(QString)));
194
195 applyViewProperties();
196 m_topLayout->addWidget(m_container);
197
198 loadDirectory(url);
199 }
200
201 DolphinView::~DolphinView()
202 {
203 }
204
205 KUrl DolphinView::url() const
206 {
207 return m_url;
208 }
209
210 void DolphinView::setActive(bool active)
211 {
212 if (active == m_active) {
213 return;
214 }
215
216 m_active = active;
217
218 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
219 if (!active) {
220 color.setAlpha(150);
221 }
222
223 /*QAbstractItemView* view = m_viewAccessor.itemView();
224 QWidget* viewport = view ? view->viewport() : 0;
225 if (viewport) {
226 QPalette palette;
227 palette.setColor(viewport->backgroundRole(), color);
228 viewport->setPalette(palette);
229 }*/
230
231 update();
232
233 if (active) {
234 //if (view) {
235 // view->setFocus();
236 //}
237 emit activated();
238 emit writeStateChanged(m_isFolderWritable);
239 }
240
241 //m_viewModeController->indicateActivationChange(active);
242 }
243
244 bool DolphinView::isActive() const
245 {
246 return m_active;
247 }
248
249 void DolphinView::setMode(Mode mode)
250 {
251 if (mode != m_mode) {
252 ViewProperties props(url());
253 props.setViewMode(mode);
254 props.save();
255
256 applyViewProperties();
257 }
258 }
259
260 DolphinView::Mode DolphinView::mode() const
261 {
262 return m_mode;
263 }
264
265 bool DolphinView::previewsShown() const
266 {
267 return m_container->previewsShown();
268 }
269
270 bool DolphinView::hiddenFilesShown() const
271 {
272 return m_dirLister->showingDotFiles();
273 }
274
275 bool DolphinView::categorizedSorting() const
276 {
277 return false; //m_storedCategorizedSorting;
278 }
279
280 KFileItemList DolphinView::items() const
281 {
282 return m_dirLister->items();
283 }
284
285 KFileItemList DolphinView::selectedItems() const
286 {
287 const KFileItemModel* model = fileItemModel();
288 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
289 const QSet<int> selectedIndexes = selectionManager->selectedItems();
290
291 KFileItemList selectedItems;
292 QSetIterator<int> it(selectedIndexes);
293 while (it.hasNext()) {
294 const int index = it.next();
295 selectedItems.append(model->fileItem(index));
296 }
297 return selectedItems;
298 }
299
300 int DolphinView::selectedItemsCount() const
301 {
302 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
303 return selectionManager->selectedItems().count();
304 }
305
306 void DolphinView::markUrlsAsSelected(const QList<KUrl>& urls)
307 {
308 foreach (const KUrl& url, urls) {
309 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
310 m_selectedItems.append(item);
311 }
312 }
313
314 void DolphinView::setItemSelectionEnabled(const QRegExp& pattern, bool enabled)
315 {
316 const KItemListSelectionManager::SelectionMode mode = enabled
317 ? KItemListSelectionManager::Select
318 : KItemListSelectionManager::Deselect;
319 const KFileItemModel* model = fileItemModel();
320 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
321
322 for (int index = 0; index < model->count(); index++) {
323 const KFileItem item = model->fileItem(index);
324 if (pattern.exactMatch(item.name())) {
325 // An alternative approach would be to store the matching items in a QSet<int> and
326 // select them in one go after the loop, but we'd need a new function
327 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
328 // for that.
329 selectionManager->setSelected(index, 1, mode);
330 }
331 }
332 }
333
334 void DolphinView::setZoomLevel(int level)
335 {
336 const int oldZoomLevel = zoomLevel();
337 m_container->setZoomLevel(level);
338 if (zoomLevel() != oldZoomLevel) {
339 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
340 }
341 }
342
343 int DolphinView::zoomLevel() const
344 {
345 return m_container->zoomLevel();
346 }
347
348 void DolphinView::setSorting(Sorting sorting)
349 {
350 if (sorting != this->sorting()) {
351 updateSorting(sorting);
352 }
353 }
354
355 DolphinView::Sorting DolphinView::sorting() const
356 {
357 return DolphinView::SortByName;
358 //return m_viewAccessor.proxyModel()->sorting();
359 }
360
361 void DolphinView::setSortOrder(Qt::SortOrder order)
362 {
363 if (sortOrder() != order) {
364 updateSortOrder(order);
365 }
366 }
367
368 Qt::SortOrder DolphinView::sortOrder() const
369 {
370 return Qt::AscendingOrder; // m_viewAccessor.proxyModel()->sortOrder();
371 }
372
373 void DolphinView::setSortFoldersFirst(bool foldersFirst)
374 {
375 if (sortFoldersFirst() != foldersFirst) {
376 updateSortFoldersFirst(foldersFirst);
377 }
378 }
379
380 bool DolphinView::sortFoldersFirst() const
381 {
382 return true; // m_viewAccessor.proxyModel()->sortFoldersFirst();
383 }
384
385 void DolphinView::setAdditionalInfoList(const QList<AdditionalInfo>& info)
386 {
387 const QList<AdditionalInfo> previousList = info;
388
389 ViewProperties props(url());
390 props.setAdditionalInfoList(info);
391
392 m_additionalInfoList = info;
393 applyAdditionalInfoListToView();
394
395 emit additionalInfoListChanged(m_additionalInfoList, previousList);
396 }
397
398 QList<DolphinView::AdditionalInfo> DolphinView::additionalInfoList() const
399 {
400 return m_additionalInfoList;
401 }
402
403 void DolphinView::reload()
404 {
405 QByteArray viewState;
406 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
407 saveState(saveStream);
408 m_selectedItems= selectedItems();
409
410 setUrl(url());
411 loadDirectory(url(), true);
412
413 QDataStream restoreStream(viewState);
414 restoreState(restoreStream);
415 }
416
417 void DolphinView::stopLoading()
418 {
419 m_dirLister->stop();
420 }
421
422 void DolphinView::refresh()
423 {
424 const bool oldActivationState = m_active;
425 const int oldZoomLevel = zoomLevel();
426 m_active = true;
427
428 applyViewProperties();
429 reload();
430
431 setActive(oldActivationState);
432 updateZoomLevel(oldZoomLevel);
433 }
434
435 void DolphinView::setNameFilter(const QString& nameFilter)
436 {
437 Q_UNUSED(nameFilter);
438 //m_viewModeController->setNameFilter(nameFilter);
439 }
440
441 QString DolphinView::nameFilter() const
442 {
443 return QString(); //m_viewModeController->nameFilter();
444 }
445
446 void DolphinView::calculateItemCount(int& fileCount,
447 int& folderCount,
448 KIO::filesize_t& totalFileSize) const
449 {
450 foreach (const KFileItem& item, m_dirLister->items()) {
451 if (item.isDir()) {
452 ++folderCount;
453 } else {
454 ++fileCount;
455 totalFileSize += item.size();
456 }
457 }
458 }
459
460 QString DolphinView::statusBarText() const
461 {
462 QString text;
463 int folderCount = 0;
464 int fileCount = 0;
465 KIO::filesize_t totalFileSize = 0;
466
467 if (hasSelection()) {
468 // give a summary of the status of the selected files
469 const KFileItemList list = selectedItems();
470 if (list.isEmpty()) {
471 // when an item is triggered, it is temporary selected but selectedItems()
472 // will return an empty list
473 return text;
474 }
475
476 KFileItemList::const_iterator it = list.begin();
477 const KFileItemList::const_iterator end = list.end();
478 while (it != end) {
479 const KFileItem& item = *it;
480 if (item.isDir()) {
481 ++folderCount;
482 } else {
483 ++fileCount;
484 totalFileSize += item.size();
485 }
486 ++it;
487 }
488
489 if (folderCount + fileCount == 1) {
490 // if only one item is selected, show the filename
491 const QString name = list.first().text();
492 text = (folderCount == 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name) :
493 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
494 name, KIO::convertSize(totalFileSize));
495 } else {
496 // at least 2 items are selected
497 const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
498 const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
499 if ((folderCount > 0) && (fileCount > 0)) {
500 text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
501 foldersText, filesText, KIO::convertSize(totalFileSize));
502 } else if (fileCount > 0) {
503 text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize));
504 } else {
505 Q_ASSERT(folderCount > 0);
506 text = foldersText;
507 }
508 }
509 } else {
510 calculateItemCount(fileCount, folderCount, totalFileSize);
511 text = KIO::itemsSummaryString(fileCount + folderCount,
512 fileCount, folderCount,
513 totalFileSize, true);
514 }
515
516 return text;
517 }
518
519 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
520 {
521 Q_UNUSED(items);
522 return QList<QAction*>(); //m_dolphinViewController->versionControlActions(items);
523 }
524
525 void DolphinView::setUrl(const KUrl& url)
526 {
527 if (url == m_url) {
528 return;
529 }
530
531 emit urlAboutToBeChanged(url);
532 m_url = url;
533
534 if (GeneralSettings::showToolTips()) {
535 m_toolTipManager->hideToolTip();
536 }
537
538 // It is important to clear the items from the model before
539 // applying the view properties, otherwise expensive operations
540 // might be done on the existing items although they get cleared
541 // anyhow afterwards by loadDirectory().
542 fileItemModel()->clear();
543 applyViewProperties();
544 loadDirectory(url);
545
546 emit urlChanged(url);
547 }
548
549 void DolphinView::selectAll()
550 {
551 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
552 selectionManager->setSelected(0, fileItemModel()->count());
553 }
554
555 void DolphinView::invertSelection()
556 {
557 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
558 selectionManager->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle);
559 }
560
561 void DolphinView::clearSelection()
562 {
563 m_container->controller()->selectionManager()->clearSelection();
564 }
565
566 void DolphinView::renameSelectedItems()
567 {
568 KFileItemList items = selectedItems();
569 const int itemCount = items.count();
570 if (itemCount < 1) {
571 return;
572 }
573
574 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
575 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
576 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
577 m_viewAccessor.itemView()->edit(proxyIndex);
578 } else {*/
579 RenameDialog* dialog = new RenameDialog(this, items);
580 dialog->setAttribute(Qt::WA_DeleteOnClose);
581 dialog->show();
582 dialog->raise();
583 dialog->activateWindow();
584 //}
585
586 // assure that the current index remains visible when KDirLister
587 // will notify the view about changed items
588 m_assureVisibleCurrentIndex = true;
589 }
590
591 void DolphinView::trashSelectedItems()
592 {
593 const KUrl::List list = simplifiedSelectedUrls();
594 KonqOperations::del(this, KonqOperations::TRASH, list);
595 }
596
597 void DolphinView::deleteSelectedItems()
598 {
599 const KUrl::List list = simplifiedSelectedUrls();
600 const bool del = KonqOperations::askDeleteConfirmation(list,
601 KonqOperations::DEL,
602 KonqOperations::DEFAULT_CONFIRMATION,
603 this);
604
605 if (del) {
606 KIO::Job* job = KIO::del(list);
607 connect(job, SIGNAL(result(KJob*)),
608 this, SLOT(slotDeleteFileFinished(KJob*)));
609 }
610 }
611
612 void DolphinView::cutSelectedItems()
613 {
614 QMimeData* mimeData = selectionMimeData();
615 KonqMimeData::addIsCutSelection(mimeData, true);
616 QApplication::clipboard()->setMimeData(mimeData);
617 }
618
619 void DolphinView::copySelectedItems()
620 {
621 QMimeData* mimeData = selectionMimeData();
622 QApplication::clipboard()->setMimeData(mimeData);
623 }
624
625 void DolphinView::paste()
626 {
627 pasteToUrl(url());
628 }
629
630 void DolphinView::pasteIntoFolder()
631 {
632 const KFileItemList items = selectedItems();
633 if ((items.count() == 1) && items.first().isDir()) {
634 pasteToUrl(items.first().url());
635 }
636 }
637
638 void DolphinView::setPreviewsShown(bool show)
639 {
640 if (previewsShown() == show) {
641 return;
642 }
643
644 ViewProperties props(url());
645 props.setPreviewsShown(show);
646
647 m_container->setPreviewsShown(show);
648 emit previewsShownChanged(show);
649 }
650
651 void DolphinView::setHiddenFilesShown(bool show)
652 {
653 if (m_dirLister->showingDotFiles() == show) {
654 return;
655 }
656
657 m_selectedItems = selectedItems();
658
659 ViewProperties props(url());
660 props.setHiddenFilesShown(show);
661
662 m_dirLister->setShowingDotFiles(show);
663 m_dirLister->emitChanges();
664 emit hiddenFilesShownChanged(show);
665 }
666
667 void DolphinView::setCategorizedSorting(bool categorized)
668 {
669 if (categorized == categorizedSorting()) {
670 return;
671 }
672
673 ViewProperties props(url());
674 props.setCategorizedSorting(categorized);
675 props.save();
676
677 //m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
678
679 emit categorizedSortingChanged(categorized);
680 }
681
682 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
683 {
684 QWidget::mouseReleaseEvent(event);
685 setActive(true);
686 }
687
688 void DolphinView::contextMenuEvent(QContextMenuEvent* event)
689 {
690 Q_UNUSED(event);
691
692 const QPoint pos = m_container->mapFromGlobal(QCursor::pos());
693 const KItemListView* view = m_container->controller()->view();
694 if (view->itemAt(pos) < 0) {
695 // Only open the context-menu if the cursor is above the viewport
696 // (the context-menu for items is handled in slotContextMenuRequested())
697 requestContextMenu(KFileItem(), url(), QList<QAction*>());
698 }
699 }
700
701 void DolphinView::wheelEvent(QWheelEvent* event)
702 {
703 if (event->modifiers().testFlag(Qt::ControlModifier)) {
704 const int numDegrees = event->delta() / 8;
705 const int numSteps = numDegrees / 15;
706
707 setZoomLevel(zoomLevel() + numSteps);
708 }
709 event->accept();
710 }
711
712 void DolphinView::activate()
713 {
714 setActive(true);
715 }
716
717 void DolphinView::slotItemActivated(int index)
718 {
719 Q_UNUSED(index);
720
721 const KFileItemList items = selectedItems();
722 if (items.isEmpty()) {
723 return;
724 }
725
726 if (items.count() == 1) {
727 emit itemActivated(items.at(0)); // caught by DolphinViewContainer or DolphinPart
728 } else {
729 foreach (const KFileItem& item, items) {
730 if (item.isDir()) {
731 emit tabRequested(item.url());
732 } else {
733 emit itemActivated(item);
734 }
735 }
736 }
737 }
738
739 void DolphinView::slotItemMiddleClicked(int index)
740 {
741 const KFileItem item = fileItemModel()->fileItem(index);
742 if (item.isDir() || isTabsForFilesEnabled()) {
743 emit tabRequested(item.url());
744 }
745 }
746
747 void DolphinView::slotContextMenuRequested(int index, const QPointF& pos)
748 {
749 Q_UNUSED(pos);
750 if (GeneralSettings::showToolTips()) {
751 m_toolTipManager->hideToolTip();
752 }
753 const KFileItem item = fileItemModel()->fileItem(index);
754 emit requestContextMenu(item, url(), QList<QAction*>());
755 }
756
757 void DolphinView::slotItemExpansionToggleClicked(int index)
758 {
759 // TODO: When doing a model->setExpanded(false) it should
760 // be checked here whether the current index is part of the
761 // closed sub-tree. If this is the case, the current index
762 // should be adjusted to the parent index.
763 KFileItemModel* model = fileItemModel();
764 const bool expanded = model->isExpanded(index);
765 model->setExpanded(index, !expanded);
766 }
767
768 void DolphinView::slotItemHovered(int index)
769 {
770 const KFileItem item = fileItemModel()->fileItem(index);
771
772 if (GeneralSettings::showToolTips()) {
773 QRectF itemRect = m_container->controller()->view()->itemBoundingRect(index);
774 const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
775 itemRect.moveTo(pos);
776
777 m_toolTipManager->showToolTip(item, itemRect);
778 }
779
780 emit requestItemInfo(item);
781 }
782
783 void DolphinView::slotItemUnhovered(int index)
784 {
785 Q_UNUSED(index);
786 if (GeneralSettings::showToolTips()) {
787 m_toolTipManager->hideToolTip();
788 }
789 emit requestItemInfo(KFileItem());
790 }
791
792 void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
793 {
794 const KFileItem destItem = fileItemModel()->fileItem(index);
795
796 QDropEvent dropEvent(event->pos().toPoint(),
797 event->possibleActions(),
798 event->mimeData(),
799 event->buttons(),
800 event->modifiers());
801
802 DragAndDropHelper::dropUrls(destItem, url(), &dropEvent, this);
803 }
804
805 void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
806 {
807 Q_UNUSED(previous);
808 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
809
810 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(current);
811 m_versionControlObserver->setModel(fileItemModel);
812 }
813
814 void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
815 {
816 const int currentCount = current.count();
817 const int previousCount = previous.count();
818 const bool selectionStateChanged = (currentCount == 0 && previousCount > 0) ||
819 (currentCount > 0 && previousCount == 0);
820
821 // If nothing has been selected before and something got selected (or if something
822 // was selected before and now nothing is selected) the selectionChangedSignal must
823 // be emitted asynchronously as fast as possible to update the edit-actions.
824 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
825 m_selectionChangedTimer->start();
826 }
827
828 void DolphinView::emitSelectionChangedSignal()
829 {
830 m_selectionChangedTimer->stop();
831 emit selectionChanged(selectedItems());
832 }
833
834 void DolphinView::openContextMenu(const QPoint& pos,
835 const QList<QAction*>& customActions)
836 {
837 KFileItem item;
838 const int index = m_container->controller()->view()->itemAt(pos);
839 if (index >= 0) {
840 item = fileItemModel()->fileItem(index);
841 }
842
843 emit requestContextMenu(item, url(), customActions);
844 }
845
846 void DolphinView::dropUrls(const KFileItem& destItem,
847 const KUrl& destPath,
848 QDropEvent* event)
849 {
850 Q_UNUSED(destItem);
851 Q_UNUSED(destPath);
852 markPastedUrlsAsSelected(event->mimeData());
853 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
854 }
855
856 void DolphinView::updateSorting(DolphinView::Sorting sorting)
857 {
858 ViewProperties props(url());
859 props.setSorting(sorting);
860
861 KItemModelBase* model = m_container->controller()->model();
862 model->setSortRole(sortRoleForSorting(sorting));
863
864 emit sortingChanged(sorting);
865 }
866
867 void DolphinView::updateSortOrder(Qt::SortOrder order)
868 {
869 ViewProperties props(url());
870 props.setSortOrder(order);
871
872 //m_viewAccessor.proxyModel()->setSortOrder(order);
873
874 emit sortOrderChanged(order);
875 }
876
877 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
878 {
879 ViewProperties props(url());
880 props.setSortFoldersFirst(foldersFirst);
881
882 //m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
883
884 emit sortFoldersFirstChanged(foldersFirst);
885 }
886
887 QPair<bool, QString> DolphinView::pasteInfo() const
888 {
889 return KonqOperations::pasteInfo(url());
890 }
891
892 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
893 {
894 m_tabsForFiles = tabsForFiles;
895 }
896
897 bool DolphinView::isTabsForFilesEnabled() const
898 {
899 return m_tabsForFiles;
900 }
901
902 bool DolphinView::itemsExpandable() const
903 {
904 return m_mode == DetailsView;
905 }
906
907 void DolphinView::restoreState(QDataStream& stream)
908 {
909 // Restore the current item that had the keyboard focus
910 stream >> m_currentItemIndex;
911
912 // Restore the view position
913 stream >> m_restoredContentsPosition;
914
915 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
916 QSet<KUrl> urlsToExpand;
917 stream >> urlsToExpand;
918 /*const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
919 if (expander) {
920 m_expanderActive = true;
921 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
922 }
923 else {
924 m_expanderActive = false;
925 }*/
926 }
927
928 void DolphinView::saveState(QDataStream& stream)
929 {
930 // Save the current item that has the keyboard focus
931 stream << m_container->controller()->selectionManager()->currentItem();
932
933 // Save view position
934 const qreal x = m_container->horizontalScrollBar()->value();
935 const qreal y = m_container->verticalScrollBar()->value();
936 stream << QPoint(x, y);
937
938 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
939 //stream << m_viewAccessor.expandedUrls();
940 }
941
942 bool DolphinView::hasSelection() const
943 {
944 return m_container->controller()->selectionManager()->hasSelection();
945 }
946
947 KFileItem DolphinView::rootItem() const
948 {
949 return m_dirLister->rootItem();
950 }
951
952 void DolphinView::observeCreatedItem(const KUrl& url)
953 {
954 m_createdItemUrl = url;
955 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
956 // this, SLOT(selectAndScrollToCreatedItem()));
957 }
958
959 void DolphinView::selectAndScrollToCreatedItem()
960 {
961 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
962 if (dirIndex.isValid()) {
963 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
964 QAbstractItemView* view = m_viewAccessor.itemView();
965 if (view) {
966 view->setCurrentIndex(proxyIndex);
967 }
968 }
969
970 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
971 this, SLOT(selectAndScrollToCreatedItem()));*/
972 m_createdItemUrl = KUrl();
973 }
974
975 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
976 {
977 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
978 emit redirection(oldUrl, newUrl);
979 m_url = newUrl; // #186947
980 }
981 }
982
983 void DolphinView::updateViewState()
984 {
985 if (m_currentItemIndex >= 0) {
986 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
987 selectionManager->setCurrentItem(m_currentItemIndex);
988 m_currentItemIndex =-1;
989 }
990
991 if (!m_restoredContentsPosition.isNull()) {
992 const int x = m_restoredContentsPosition.x();
993 const int y = m_restoredContentsPosition.y();
994 m_restoredContentsPosition = QPoint();
995
996 m_container->horizontalScrollBar()->setValue(x);
997 m_container->verticalScrollBar()->setValue(y);
998 }
999
1000 if (!m_selectedItems.isEmpty()) {
1001 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1002 QSet<int> selectedItems = selectionManager->selectedItems();
1003 const KFileItemModel* model = fileItemModel();
1004
1005 foreach (const KFileItem& selectedItem, m_selectedItems) {
1006 const int index = model->index(selectedItem);
1007 if (index >= 0) {
1008 selectedItems.insert(index);
1009 }
1010 }
1011
1012 selectionManager->setSelectedItems(selectedItems);
1013 m_selectedItems.clear();
1014 }
1015 }
1016
1017 void DolphinView::showHoverInformation(const KFileItem& item)
1018 {
1019 emit requestItemInfo(item);
1020 }
1021
1022 void DolphinView::clearHoverInformation()
1023 {
1024 emit requestItemInfo(KFileItem());
1025 }
1026
1027 void DolphinView::slotDeleteFileFinished(KJob* job)
1028 {
1029 if (job->error() == 0) {
1030 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1031 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1032 emit errorMessage(job->errorString());
1033 }
1034 }
1035
1036 void DolphinView::slotDirListerStarted(const KUrl& url)
1037 {
1038 // Disable the writestate temporary until it can be determined in a fast way
1039 // in DolphinView::slotDirListerCompleted()
1040 if (m_isFolderWritable) {
1041 m_isFolderWritable = false;
1042 emit writeStateChanged(m_isFolderWritable);
1043 }
1044
1045 emit startedPathLoading(url);
1046 }
1047
1048 void DolphinView::slotDirListerCompleted()
1049 {
1050 // Update the view-state. This has to be done using a Qt::QueuedConnection
1051 // because the view might not be in its final state yet (the view also
1052 // listens to the completed()-signal from KDirLister and the order of
1053 // of slots is undefined).
1054 QTimer::singleShot(0, this, SLOT(updateViewState()));
1055
1056 emit finishedPathLoading(url());
1057
1058 updateWritableState();
1059 }
1060
1061 void DolphinView::slotRefreshItems()
1062 {
1063 if (m_assureVisibleCurrentIndex) {
1064 m_assureVisibleCurrentIndex = false;
1065 //QAbstractItemView* view = m_viewAccessor.itemView();
1066 //if (view) {
1067 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1068 //}
1069 }
1070 }
1071
1072 KFileItemModel* DolphinView::fileItemModel() const
1073 {
1074 return static_cast<KFileItemModel*>(m_container->controller()->model());
1075 }
1076
1077 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1078 {
1079 if (!url.isValid()) {
1080 const QString location(url.pathOrUrl());
1081 if (location.isEmpty()) {
1082 emit errorMessage(i18nc("@info:status", "The location is empty."));
1083 } else {
1084 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1085 }
1086 return;
1087 }
1088
1089 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1090 }
1091
1092 void DolphinView::applyViewProperties()
1093 {
1094 m_container->beginTransaction();
1095
1096 const ViewProperties props(url());
1097
1098 const Mode mode = props.viewMode();
1099 if (m_mode != mode) {
1100 const Mode previousMode = m_mode;
1101 m_mode = mode;
1102
1103 // Changing the mode might result in changing
1104 // the zoom level. Remember the old zoom level so
1105 // that zoomLevelChanged() can get emitted.
1106 const int oldZoomLevel = m_container->zoomLevel();
1107
1108 switch (m_mode) {
1109 case IconsView: m_container->setItemLayout(KFileItemListView::IconsLayout); break;
1110 case CompactView: m_container->setItemLayout(KFileItemListView::CompactLayout); break;
1111 case DetailsView: m_container->setItemLayout(KFileItemListView::DetailsLayout); break;
1112 default: Q_ASSERT(false); break;
1113 }
1114
1115 emit modeChanged(m_mode, previousMode);
1116
1117 if (m_container->zoomLevel() != oldZoomLevel) {
1118 emit zoomLevelChanged(m_container->zoomLevel(), oldZoomLevel);
1119 }
1120 }
1121
1122 const bool hiddenFilesShown = props.hiddenFilesShown();
1123 if (hiddenFilesShown != m_dirLister->showingDotFiles()) {
1124 m_dirLister->setShowingDotFiles(hiddenFilesShown);
1125 m_dirLister->emitChanges();
1126 emit hiddenFilesShownChanged(hiddenFilesShown);
1127 }
1128
1129 /* m_storedCategorizedSorting = props.categorizedSorting();
1130 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1131 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1132 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1133 emit categorizedSortingChanged();
1134 }*/
1135
1136 const DolphinView::Sorting sorting = props.sorting();
1137 KItemModelBase* model = m_container->controller()->model();
1138 const QByteArray newSortRole = sortRoleForSorting(sorting);
1139 if (newSortRole != model->sortRole()) {
1140 model->setSortRole(newSortRole);
1141 emit sortingChanged(sorting);
1142 }
1143 /*
1144 const Qt::SortOrder sortOrder = props.sortOrder();
1145 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1146 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1147 emit sortOrderChanged(sortOrder);
1148 }
1149
1150 const bool sortFoldersFirst = props.sortFoldersFirst();
1151 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1152 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1153 emit sortFoldersFirstChanged(sortFoldersFirst);
1154 }
1155 */
1156 const QList<DolphinView::AdditionalInfo> infoList = props.additionalInfoList();
1157 if (infoList != m_additionalInfoList) {
1158 const QList<DolphinView::AdditionalInfo> previousList = m_additionalInfoList;
1159 m_additionalInfoList = infoList;
1160 applyAdditionalInfoListToView();
1161 emit additionalInfoListChanged(m_additionalInfoList, previousList);
1162 }
1163
1164 const bool previewsShown = props.previewsShown();
1165 if (previewsShown != m_container->previewsShown()) {
1166 const int oldZoomLevel = zoomLevel();
1167
1168 m_container->setPreviewsShown(previewsShown);
1169 emit previewsShownChanged(previewsShown);
1170
1171 // Changing the preview-state might result in a changed zoom-level
1172 if (oldZoomLevel != zoomLevel()) {
1173 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
1174 }
1175 }
1176
1177 m_container->endTransaction();
1178 }
1179
1180 void DolphinView::applyAdditionalInfoListToView()
1181 {
1182 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
1183
1184 QHash<QByteArray, int> visibleRoles;
1185 visibleRoles.insert("name", 0);
1186
1187 int index = 1;
1188 foreach (AdditionalInfo info, m_additionalInfoList) {
1189 visibleRoles.insert(infoAccessor.role(info), index);
1190 ++index;
1191 }
1192
1193 m_container->setVisibleRoles(visibleRoles);
1194 }
1195
1196 void DolphinView::pasteToUrl(const KUrl& url)
1197 {
1198 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1199 KonqOperations::doPaste(this, url);
1200 }
1201
1202 void DolphinView::updateZoomLevel(int oldZoomLevel)
1203 {
1204 Q_UNUSED(oldZoomLevel);
1205 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1206 if (oldZoomLevel != newZoomLevel) {
1207 m_viewModeController->setZoomLevel(newZoomLevel);
1208 emit zoomLevelChanged(newZoomLevel);
1209 }*/
1210 }
1211
1212 KUrl::List DolphinView::simplifiedSelectedUrls() const
1213 {
1214 KUrl::List urls;
1215
1216 const KFileItemList items = selectedItems();
1217 foreach (const KFileItem &item, items) {
1218 urls.append(item.url());
1219 }
1220
1221 if (itemsExpandable()) {
1222 // TODO: Check if we still need KDirModel for this in KDE 5.0
1223 urls = KDirModel::simplifiedUrlList(urls);
1224 }
1225
1226 return urls;
1227 }
1228
1229 QMimeData* DolphinView::selectionMimeData() const
1230 {
1231 const KFileItemModel* model = fileItemModel();
1232 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1233 const QSet<int> selectedIndexes = selectionManager->selectedItems();
1234
1235 return model->createMimeData(selectedIndexes);
1236 }
1237
1238 void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
1239 {
1240 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1241 markUrlsAsSelected(urls);
1242 }
1243
1244 void DolphinView::updateWritableState()
1245 {
1246 const bool wasFolderWritable = m_isFolderWritable;
1247 m_isFolderWritable = true;
1248
1249 const KFileItem item = m_dirLister->rootItem();
1250 if (!item.isNull()) {
1251 KFileItemListProperties capabilities(KFileItemList() << item);
1252 m_isFolderWritable = capabilities.supportsWriting();
1253 }
1254 if (m_isFolderWritable != wasFolderWritable) {
1255 emit writeStateChanged(m_isFolderWritable);
1256 }
1257 }
1258
1259 QByteArray DolphinView::sortRoleForSorting(Sorting sorting) const
1260 {
1261 switch (sorting) {
1262 case SortByName: return "name";
1263 case SortBySize: return "size";
1264 case SortByDate: return "date";
1265 case SortByPermissions: return "permissions";
1266 case SortByOwner: return "owner";
1267 case SortByGroup: return "group";
1268 case SortByType: return "type";
1269 case SortByDestination: return "destination";
1270 case SortByPath: return "path";
1271 default: break;
1272 }
1273
1274 return QByteArray();
1275 }
1276
1277 #include "dolphinview.moc"