]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Removed warning, minor coding style update
[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 Q_UNUSED(index);
707
708 const KFileItemList items = selectedItems();
709 if (items.isEmpty()) {
710 return;
711 }
712
713 if (items.count() == 1) {
714 emit itemActivated(items.at(0)); // caught by DolphinViewContainer or DolphinPart
715 } else {
716 foreach (const KFileItem& item, items) {
717 if (item.isDir()) {
718 emit tabRequested(item.url());
719 } else {
720 emit itemActivated(item);
721 }
722 }
723 }
724 }
725
726 void DolphinView::slotItemMiddleClicked(int index)
727 {
728 const KFileItem item = fileItemModel()->fileItem(index);
729 if (item.isDir() || isTabsForFilesEnabled()) {
730 emit tabRequested(item.url());
731 }
732 }
733
734 void DolphinView::slotContextMenuRequested(int index, const QPointF& pos)
735 {
736 Q_UNUSED(pos);
737 if (GeneralSettings::showToolTips()) {
738 m_toolTipManager->hideToolTip();
739 }
740 const KFileItem item = fileItemModel()->fileItem(index);
741 emit requestContextMenu(item, url(), QList<QAction*>());
742 }
743
744 void DolphinView::slotItemExpansionToggleClicked(int index)
745 {
746 // TODO: When doing a model->setExpanded(false) it should
747 // be checked here whether the current index is part of the
748 // closed sub-tree. If this is the case, the current index
749 // should be adjusted to the parent index.
750 KFileItemModel* model = fileItemModel();
751 const bool expanded = model->isExpanded(index);
752 model->setExpanded(index, !expanded);
753 }
754
755 void DolphinView::slotItemHovered(int index)
756 {
757 const KFileItem item = fileItemModel()->fileItem(index);
758
759 if (GeneralSettings::showToolTips()) {
760 QRectF itemRect = m_container->controller()->view()->itemBoundingRect(index);
761 const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
762 itemRect.moveTo(pos);
763
764 m_toolTipManager->showToolTip(item, itemRect);
765 }
766
767 emit requestItemInfo(item);
768 }
769
770 void DolphinView::slotItemUnhovered(int index)
771 {
772 Q_UNUSED(index);
773 if (GeneralSettings::showToolTips()) {
774 m_toolTipManager->hideToolTip();
775 }
776 emit requestItemInfo(KFileItem());
777 }
778
779 void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
780 {
781 const int currentCount = current.count();
782 const int previousCount = previous.count();
783 const bool selectionStateChanged = (currentCount == 0 && previousCount > 0) ||
784 (currentCount > 0 && previousCount == 0);
785
786 // If nothing has been selected before and something got selected (or if something
787 // was selected before and now nothing is selected) the selectionChangedSignal must
788 // be emitted asynchronously as fast as possible to update the edit-actions.
789 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
790 m_selectionChangedTimer->start();
791 }
792
793 void DolphinView::emitSelectionChangedSignal()
794 {
795 m_selectionChangedTimer->stop();
796 emit selectionChanged(selectedItems());
797 }
798
799 void DolphinView::openContextMenu(const QPoint& pos,
800 const QList<QAction*>& customActions)
801 {
802 KFileItem item;
803 const int index = m_container->controller()->view()->itemAt(pos);
804 if (index >= 0) {
805 item = fileItemModel()->fileItem(index);
806 }
807
808 emit requestContextMenu(item, url(), customActions);
809 }
810
811 void DolphinView::dropUrls(const KFileItem& destItem,
812 const KUrl& destPath,
813 QDropEvent* event)
814 {
815 Q_UNUSED(destItem);
816 Q_UNUSED(destPath);
817 markPastedUrlsAsSelected(event->mimeData());
818 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
819 }
820
821 void DolphinView::updateSorting(DolphinView::Sorting sorting)
822 {
823 ViewProperties props(url());
824 props.setSorting(sorting);
825
826 KItemModelBase* model = m_container->controller()->model();
827 model->setSortRole(sortRoleForSorting(sorting));
828
829 emit sortingChanged(sorting);
830 }
831
832 void DolphinView::updateSortOrder(Qt::SortOrder order)
833 {
834 ViewProperties props(url());
835 props.setSortOrder(order);
836
837 //m_viewAccessor.proxyModel()->setSortOrder(order);
838
839 emit sortOrderChanged(order);
840 }
841
842 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
843 {
844 ViewProperties props(url());
845 props.setSortFoldersFirst(foldersFirst);
846
847 //m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
848
849 emit sortFoldersFirstChanged(foldersFirst);
850 }
851
852 QPair<bool, QString> DolphinView::pasteInfo() const
853 {
854 return KonqOperations::pasteInfo(url());
855 }
856
857 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
858 {
859 m_tabsForFiles = tabsForFiles;
860 }
861
862 bool DolphinView::isTabsForFilesEnabled() const
863 {
864 return m_tabsForFiles;
865 }
866
867 bool DolphinView::itemsExpandable() const
868 {
869 return m_mode == DetailsView;
870 }
871
872 void DolphinView::restoreState(QDataStream& stream)
873 {
874 // Restore the current item that had the keyboard focus
875 stream >> m_currentItemIndex;
876
877 // Restore the view position
878 stream >> m_restoredContentsPosition;
879
880 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
881 QSet<KUrl> urlsToExpand;
882 stream >> urlsToExpand;
883 /*const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
884 if (expander) {
885 m_expanderActive = true;
886 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
887 }
888 else {
889 m_expanderActive = false;
890 }*/
891 }
892
893 void DolphinView::saveState(QDataStream& stream)
894 {
895 // Save the current item that has the keyboard focus
896 stream << m_container->controller()->selectionManager()->currentItem();
897
898 // Save view position
899 const qreal x = m_container->horizontalScrollBar()->value();
900 const qreal y = m_container->verticalScrollBar()->value();
901 stream << QPoint(x, y);
902
903 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
904 //stream << m_viewAccessor.expandedUrls();
905 }
906
907 bool DolphinView::hasSelection() const
908 {
909 return m_container->controller()->selectionManager()->hasSelection();
910 }
911
912 KFileItem DolphinView::rootItem() const
913 {
914 return m_dirLister->rootItem();
915 }
916
917 void DolphinView::observeCreatedItem(const KUrl& url)
918 {
919 m_createdItemUrl = url;
920 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
921 // this, SLOT(selectAndScrollToCreatedItem()));
922 }
923
924 void DolphinView::selectAndScrollToCreatedItem()
925 {
926 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
927 if (dirIndex.isValid()) {
928 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
929 QAbstractItemView* view = m_viewAccessor.itemView();
930 if (view) {
931 view->setCurrentIndex(proxyIndex);
932 }
933 }
934
935 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
936 this, SLOT(selectAndScrollToCreatedItem()));*/
937 m_createdItemUrl = KUrl();
938 }
939
940 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
941 {
942 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
943 emit redirection(oldUrl, newUrl);
944 m_url = newUrl; // #186947
945 }
946 }
947
948 void DolphinView::updateViewState()
949 {
950 if (m_currentItemIndex >= 0) {
951 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
952 selectionManager->setCurrentItem(m_currentItemIndex);
953 m_currentItemIndex =-1;
954 }
955
956 if (!m_restoredContentsPosition.isNull()) {
957 const int x = m_restoredContentsPosition.x();
958 const int y = m_restoredContentsPosition.y();
959 m_restoredContentsPosition = QPoint();
960
961 m_container->horizontalScrollBar()->setValue(x);
962 m_container->verticalScrollBar()->setValue(y);
963 }
964
965 if (!m_selectedItems.isEmpty()) {
966 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
967 QSet<int> selectedItems = selectionManager->selectedItems();
968 const KFileItemModel* model = fileItemModel();
969
970 foreach (const KFileItem& selectedItem, m_selectedItems) {
971 const int index = model->index(selectedItem);
972 if (index >= 0) {
973 selectedItems.insert(index);
974 }
975 }
976
977 selectionManager->setSelectedItems(selectedItems);
978 m_selectedItems.clear();
979 }
980 }
981
982 void DolphinView::showHoverInformation(const KFileItem& item)
983 {
984 emit requestItemInfo(item);
985 }
986
987 void DolphinView::clearHoverInformation()
988 {
989 emit requestItemInfo(KFileItem());
990 }
991
992 void DolphinView::slotDeleteFileFinished(KJob* job)
993 {
994 if (job->error() == 0) {
995 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
996 } else if (job->error() != KIO::ERR_USER_CANCELED) {
997 emit errorMessage(job->errorString());
998 }
999 }
1000
1001 void DolphinView::slotDirListerStarted(const KUrl& url)
1002 {
1003 // Disable the writestate temporary until it can be determined in a fast way
1004 // in DolphinView::slotDirListerCompleted()
1005 if (m_isFolderWritable) {
1006 m_isFolderWritable = false;
1007 emit writeStateChanged(m_isFolderWritable);
1008 }
1009
1010 emit startedPathLoading(url);
1011 }
1012
1013 void DolphinView::slotDirListerCompleted()
1014 {
1015 // Update the view-state. This has to be done using a Qt::QueuedConnection
1016 // because the view might not be in its final state yet (the view also
1017 // listens to the completed()-signal from KDirLister and the order of
1018 // of slots is undefined).
1019 QTimer::singleShot(0, this, SLOT(updateViewState()));
1020
1021 emit finishedPathLoading(url());
1022
1023 updateWritableState();
1024 }
1025
1026 void DolphinView::slotRefreshItems()
1027 {
1028 if (m_assureVisibleCurrentIndex) {
1029 m_assureVisibleCurrentIndex = false;
1030 //QAbstractItemView* view = m_viewAccessor.itemView();
1031 //if (view) {
1032 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1033 //}
1034 }
1035 }
1036
1037 KFileItemModel* DolphinView::fileItemModel() const
1038 {
1039 return static_cast<KFileItemModel*>(m_container->controller()->model());
1040 }
1041
1042 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1043 {
1044 if (!url.isValid()) {
1045 const QString location(url.pathOrUrl());
1046 if (location.isEmpty()) {
1047 emit errorMessage(i18nc("@info:status", "The location is empty."));
1048 } else {
1049 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1050 }
1051 return;
1052 }
1053
1054 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1055 }
1056
1057 void DolphinView::applyViewProperties()
1058 {
1059 m_container->beginTransaction();
1060
1061 const ViewProperties props(url());
1062
1063 const Mode mode = props.viewMode();
1064 if (m_mode != mode) {
1065 const Mode previousMode = m_mode;
1066 m_mode = mode;
1067
1068 // Changing the mode might result in changing
1069 // the zoom level. Remember the old zoom level so
1070 // that zoomLevelChanged() can get emitted.
1071 const int oldZoomLevel = m_container->zoomLevel();
1072
1073 switch (m_mode) {
1074 case IconsView: m_container->setItemLayout(KFileItemListView::IconsLayout); break;
1075 case CompactView: m_container->setItemLayout(KFileItemListView::CompactLayout); break;
1076 case DetailsView: m_container->setItemLayout(KFileItemListView::DetailsLayout); break;
1077 default: Q_ASSERT(false); break;
1078 }
1079
1080 emit modeChanged(m_mode, previousMode);
1081
1082 if (m_container->zoomLevel() != oldZoomLevel) {
1083 emit zoomLevelChanged(m_container->zoomLevel(), oldZoomLevel);
1084 }
1085 }
1086
1087 const bool hiddenFilesShown = props.hiddenFilesShown();
1088 if (hiddenFilesShown != m_dirLister->showingDotFiles()) {
1089 m_dirLister->setShowingDotFiles(hiddenFilesShown);
1090 m_dirLister->emitChanges();
1091 emit hiddenFilesShownChanged(hiddenFilesShown);
1092 }
1093
1094 /* m_storedCategorizedSorting = props.categorizedSorting();
1095 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1096 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1097 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1098 emit categorizedSortingChanged();
1099 }*/
1100
1101 const DolphinView::Sorting sorting = props.sorting();
1102 KItemModelBase* model = m_container->controller()->model();
1103 const QByteArray newSortRole = sortRoleForSorting(sorting);
1104 if (newSortRole != model->sortRole()) {
1105 model->setSortRole(newSortRole);
1106 emit sortingChanged(sorting);
1107 }
1108 /*
1109 const Qt::SortOrder sortOrder = props.sortOrder();
1110 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1111 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1112 emit sortOrderChanged(sortOrder);
1113 }
1114
1115 const bool sortFoldersFirst = props.sortFoldersFirst();
1116 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1117 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1118 emit sortFoldersFirstChanged(sortFoldersFirst);
1119 }
1120 */
1121 const QList<DolphinView::AdditionalInfo> infoList = props.additionalInfoList();
1122 if (infoList != m_additionalInfoList) {
1123 const QList<DolphinView::AdditionalInfo> previousList = m_additionalInfoList;
1124 m_additionalInfoList = infoList;
1125 applyAdditionalInfoListToView();
1126 emit additionalInfoListChanged(m_additionalInfoList, previousList);
1127 }
1128
1129 const bool previewsShown = props.previewsShown();
1130 if (previewsShown != m_container->previewsShown()) {
1131 const int oldZoomLevel = zoomLevel();
1132
1133 m_container->setPreviewsShown(previewsShown);
1134 emit previewsShownChanged(previewsShown);
1135
1136 // Changing the preview-state might result in a changed zoom-level
1137 if (oldZoomLevel != zoomLevel()) {
1138 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
1139 }
1140 }
1141
1142 m_container->endTransaction();
1143 }
1144
1145 void DolphinView::applyAdditionalInfoListToView()
1146 {
1147 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
1148
1149 QHash<QByteArray, int> visibleRoles;
1150 visibleRoles.insert("name", 0);
1151
1152 int index = 1;
1153 foreach (AdditionalInfo info, m_additionalInfoList) {
1154 visibleRoles.insert(infoAccessor.role(info), index);
1155 ++index;
1156 }
1157
1158 m_container->setVisibleRoles(visibleRoles);
1159 }
1160
1161 void DolphinView::pasteToUrl(const KUrl& url)
1162 {
1163 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1164 KonqOperations::doPaste(this, url);
1165 }
1166
1167 void DolphinView::updateZoomLevel(int oldZoomLevel)
1168 {
1169 Q_UNUSED(oldZoomLevel);
1170 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1171 if (oldZoomLevel != newZoomLevel) {
1172 m_viewModeController->setZoomLevel(newZoomLevel);
1173 emit zoomLevelChanged(newZoomLevel);
1174 }*/
1175 }
1176
1177 KUrl::List DolphinView::simplifiedSelectedUrls() const
1178 {
1179 KUrl::List urls;
1180
1181 const KFileItemList items = selectedItems();
1182 foreach (const KFileItem &item, items) {
1183 urls.append(item.url());
1184 }
1185
1186 if (itemsExpandable()) {
1187 // TODO: Check if we still need KDirModel for this in KDE 5.0
1188 urls = KDirModel::simplifiedUrlList(urls);
1189 }
1190
1191 return urls;
1192 }
1193
1194 QMimeData* DolphinView::selectionMimeData() const
1195 {
1196 const KFileItemModel* model = fileItemModel();
1197 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1198 const QSet<int> selectedIndexes = selectionManager->selectedItems();
1199
1200 return model->createMimeData(selectedIndexes);
1201 }
1202
1203 void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
1204 {
1205 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1206 markUrlsAsSelected(urls);
1207 }
1208
1209 void DolphinView::updateWritableState()
1210 {
1211 const bool wasFolderWritable = m_isFolderWritable;
1212 m_isFolderWritable = true;
1213
1214 const KFileItem item = m_dirLister->rootItem();
1215 if (!item.isNull()) {
1216 KFileItemListProperties capabilities(KFileItemList() << item);
1217 m_isFolderWritable = capabilities.supportsWriting();
1218 }
1219 if (m_isFolderWritable != wasFolderWritable) {
1220 emit writeStateChanged(m_isFolderWritable);
1221 }
1222 }
1223
1224 QByteArray DolphinView::sortRoleForSorting(Sorting sorting) const
1225 {
1226 switch (sorting) {
1227 case SortByName: return "name";
1228 case SortBySize: return "size";
1229 case SortByDate: return "date";
1230 case SortByPermissions: return "permissions";
1231 case SortByOwner: return "owner";
1232 case SortByGroup: return "group";
1233 case SortByType: return "type";
1234 case SortByDestination: return "destination";
1235 case SortByPath: return "path";
1236 default: break;
1237 }
1238
1239 return QByteArray();
1240 }
1241
1242 #include "dolphinview.moc"