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