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