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