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