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