]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Two small selection improvements in DolphinView
[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 selectionManager->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle);
534 }
535
536 void DolphinView::clearSelection()
537 {
538 m_container->controller()->selectionManager()->clearSelection();
539 }
540
541 void DolphinView::renameSelectedItems()
542 {
543 KFileItemList items = selectedItems();
544 const int itemCount = items.count();
545 if (itemCount < 1) {
546 return;
547 }
548
549 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
550 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
551 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
552 m_viewAccessor.itemView()->edit(proxyIndex);
553 } else {*/
554 RenameDialog* dialog = new RenameDialog(this, items);
555 dialog->setAttribute(Qt::WA_DeleteOnClose);
556 dialog->show();
557 dialog->raise();
558 dialog->activateWindow();
559 //}
560
561 // assure that the current index remains visible when KDirLister
562 // will notify the view about changed items
563 m_assureVisibleCurrentIndex = true;
564 }
565
566 void DolphinView::trashSelectedItems()
567 {
568 const KUrl::List list = simplifiedSelectedUrls();
569 KonqOperations::del(this, KonqOperations::TRASH, list);
570 }
571
572 void DolphinView::deleteSelectedItems()
573 {
574 const KUrl::List list = simplifiedSelectedUrls();
575 const bool del = KonqOperations::askDeleteConfirmation(list,
576 KonqOperations::DEL,
577 KonqOperations::DEFAULT_CONFIRMATION,
578 this);
579
580 if (del) {
581 KIO::Job* job = KIO::del(list);
582 connect(job, SIGNAL(result(KJob*)),
583 this, SLOT(slotDeleteFileFinished(KJob*)));
584 }
585 }
586
587 void DolphinView::cutSelectedItems()
588 {
589 QMimeData* mimeData = selectionMimeData();
590 KonqMimeData::addIsCutSelection(mimeData, true);
591 QApplication::clipboard()->setMimeData(mimeData);
592 }
593
594 void DolphinView::copySelectedItems()
595 {
596 QMimeData* mimeData = selectionMimeData();
597 QApplication::clipboard()->setMimeData(mimeData);
598 }
599
600 void DolphinView::paste()
601 {
602 pasteToUrl(url());
603 }
604
605 void DolphinView::pasteIntoFolder()
606 {
607 const KFileItemList items = selectedItems();
608 if ((items.count() == 1) && items.first().isDir()) {
609 pasteToUrl(items.first().url());
610 }
611 }
612
613 void DolphinView::setPreviewsShown(bool show)
614 {
615 if (previewsShown() == show) {
616 return;
617 }
618
619 ViewProperties props(url());
620 props.setPreviewsShown(show);
621
622 m_container->setPreviewsShown(show);
623 emit previewsShownChanged(show);
624 }
625
626 void DolphinView::setHiddenFilesShown(bool show)
627 {
628 if (m_dirLister->showingDotFiles() == show) {
629 return;
630 }
631
632 m_selectedItems = selectedItems();
633
634 ViewProperties props(url());
635 props.setHiddenFilesShown(show);
636
637 m_dirLister->setShowingDotFiles(show);
638 m_dirLister->emitChanges();
639 emit hiddenFilesShownChanged(show);
640 }
641
642 void DolphinView::setCategorizedSorting(bool categorized)
643 {
644 if (categorized == categorizedSorting()) {
645 return;
646 }
647
648 ViewProperties props(url());
649 props.setCategorizedSorting(categorized);
650 props.save();
651
652 //m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
653
654 emit categorizedSortingChanged(categorized);
655 }
656
657 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
658 {
659 QWidget::mouseReleaseEvent(event);
660 setActive(true);
661 }
662
663 void DolphinView::contextMenuEvent(QContextMenuEvent* event)
664 {
665 Q_UNUSED(event);
666
667 const QPoint pos = m_container->mapFromGlobal(QCursor::pos());
668 const KItemListView* view = m_container->controller()->view();
669 if (view->itemAt(pos) < 0) {
670 // Only open the context-menu if the cursor is above the viewport
671 // (the context-menu for items is handled in slotItemClicked())
672 requestContextMenu(KFileItem(), url(), QList<QAction*>());
673 }
674 }
675
676 void DolphinView::activate()
677 {
678 setActive(true);
679 }
680
681 void DolphinView::slotItemClicked(int index, Qt::MouseButton button)
682 {
683 const KFileItem item = fileItemModel()->fileItem(index);
684
685 if (button & Qt::LeftButton) {
686 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
687 } else if (button & Qt::MidButton) {
688 if (item.isDir() || isTabsForFilesEnabled()) {
689 emit tabRequested(item.url());
690 }
691 } else if (button & Qt::RightButton) {
692 if (GeneralSettings::showToolTips()) {
693 m_toolTipManager->hideToolTip();
694 }
695 emit requestContextMenu(item, url(), QList<QAction*>());
696 }
697 }
698
699 void DolphinView::slotItemExpansionToggleClicked(int index)
700 {
701 // TODO: When doing a model->setExpanded(false) it should
702 // be checked here whether the current index is part of the
703 // closed sub-tree. If this is the case, the current index
704 // should be adjusted to the parent index.
705 KFileItemModel* model = fileItemModel();
706 const bool expanded = model->isExpanded(index);
707 model->setExpanded(index, !expanded);
708 }
709
710 void DolphinView::slotItemHovered(int index)
711 {
712 const KFileItem item = fileItemModel()->fileItem(index);
713
714 if (GeneralSettings::showToolTips()) {
715 QRectF itemRect = m_container->controller()->view()->itemBoundingRect(index);
716 const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
717 itemRect.moveTo(pos);
718
719 m_toolTipManager->showToolTip(item, itemRect);
720 }
721
722 emit requestItemInfo(item);
723 }
724
725 void DolphinView::slotItemUnhovered(int index)
726 {
727 Q_UNUSED(index);
728 if (GeneralSettings::showToolTips()) {
729 m_toolTipManager->hideToolTip();
730 }
731 emit requestItemInfo(KFileItem());
732 }
733
734 void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
735 {
736 const int currentCount = current.count();
737 const int previousCount = previous.count();
738 const bool selectionStateChanged = (currentCount == 0 && previousCount > 0) ||
739 (currentCount > 0 && previousCount == 0);
740
741 // If nothing has been selected before and something got selected (or if something
742 // was selected before and now nothing is selected) the selectionChangedSignal must
743 // be emitted asynchronously as fast as possible to update the edit-actions.
744 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
745 m_selectionChangedTimer->start();
746 }
747
748 void DolphinView::emitSelectionChangedSignal()
749 {
750 m_selectionChangedTimer->stop();
751 emit selectionChanged(selectedItems());
752 }
753
754 void DolphinView::openContextMenu(const QPoint& pos,
755 const QList<QAction*>& customActions)
756 {
757 KFileItem item;
758 const int index = m_container->controller()->view()->itemAt(pos);
759 if (index >= 0) {
760 item = fileItemModel()->fileItem(index);
761 }
762
763 emit requestContextMenu(item, url(), customActions);
764 }
765
766 void DolphinView::dropUrls(const KFileItem& destItem,
767 const KUrl& destPath,
768 QDropEvent* event)
769 {
770 Q_UNUSED(destItem);
771 Q_UNUSED(destPath);
772 markPastedUrlsAsSelected(event->mimeData());
773 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
774 }
775
776 void DolphinView::updateSorting(DolphinView::Sorting sorting)
777 {
778 ViewProperties props(url());
779 props.setSorting(sorting);
780
781 KItemModelBase* model = m_container->controller()->model();
782 model->setSortRole(sortRoleForSorting(sorting));
783
784 emit sortingChanged(sorting);
785 }
786
787 void DolphinView::updateSortOrder(Qt::SortOrder order)
788 {
789 ViewProperties props(url());
790 props.setSortOrder(order);
791
792 //m_viewAccessor.proxyModel()->setSortOrder(order);
793
794 emit sortOrderChanged(order);
795 }
796
797 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
798 {
799 ViewProperties props(url());
800 props.setSortFoldersFirst(foldersFirst);
801
802 //m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
803
804 emit sortFoldersFirstChanged(foldersFirst);
805 }
806
807 QPair<bool, QString> DolphinView::pasteInfo() const
808 {
809 return KonqOperations::pasteInfo(url());
810 }
811
812 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
813 {
814 m_tabsForFiles = tabsForFiles;
815 }
816
817 bool DolphinView::isTabsForFilesEnabled() const
818 {
819 return m_tabsForFiles;
820 }
821
822 bool DolphinView::itemsExpandable() const
823 {
824 return false; //m_viewAccessor.itemsExpandable();
825 }
826
827 void DolphinView::restoreState(QDataStream& stream)
828 {
829 // Restore the current item that had the keyboard focus
830 stream >> m_currentItemIndex;
831
832 // Restore the view position
833 stream >> m_restoredContentsPosition;
834
835 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
836 QSet<KUrl> urlsToExpand;
837 stream >> urlsToExpand;
838 /*const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
839 if (expander) {
840 m_expanderActive = true;
841 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
842 }
843 else {
844 m_expanderActive = false;
845 }*/
846 }
847
848 void DolphinView::saveState(QDataStream& stream)
849 {
850 // Save the current item that has the keyboard focus
851 stream << m_container->controller()->selectionManager()->currentItem();
852
853 // Save view position
854 const qreal x = m_container->horizontalScrollBar()->value();
855 const qreal y = m_container->verticalScrollBar()->value();
856 stream << QPoint(x, y);
857
858 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
859 //stream << m_viewAccessor.expandedUrls();
860 }
861
862 bool DolphinView::hasSelection() const
863 {
864 return m_container->controller()->selectionManager()->hasSelection();
865 }
866
867 KFileItem DolphinView::rootItem() const
868 {
869 return m_dirLister->rootItem();
870 }
871
872 void DolphinView::observeCreatedItem(const KUrl& url)
873 {
874 m_createdItemUrl = url;
875 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
876 // this, SLOT(selectAndScrollToCreatedItem()));
877 }
878
879 void DolphinView::selectAndScrollToCreatedItem()
880 {
881 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
882 if (dirIndex.isValid()) {
883 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
884 QAbstractItemView* view = m_viewAccessor.itemView();
885 if (view) {
886 view->setCurrentIndex(proxyIndex);
887 }
888 }
889
890 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
891 this, SLOT(selectAndScrollToCreatedItem()));*/
892 m_createdItemUrl = KUrl();
893 }
894
895 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
896 {
897 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
898 emit redirection(oldUrl, newUrl);
899 m_url = newUrl; // #186947
900 }
901 }
902
903 void DolphinView::updateViewState()
904 {
905 if (m_currentItemIndex >= 0) {
906 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
907 selectionManager->setCurrentItem(m_currentItemIndex);
908 m_currentItemIndex =-1;
909 }
910
911 if (!m_restoredContentsPosition.isNull()) {
912 const int x = m_restoredContentsPosition.x();
913 const int y = m_restoredContentsPosition.y();
914 m_restoredContentsPosition = QPoint();
915
916 m_container->horizontalScrollBar()->setValue(x);
917 m_container->verticalScrollBar()->setValue(y);
918 }
919
920 if (!m_selectedItems.isEmpty()) {
921 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
922 QSet<int> selectedItems = selectionManager->selectedItems();
923 const KFileItemModel* model = fileItemModel();
924
925 foreach (const KFileItem& selectedItem, m_selectedItems) {
926 const int index = model->index(selectedItem);
927 if (index >= 0) {
928 selectedItems.insert(index);
929 }
930 }
931
932 selectionManager->setSelectedItems(selectedItems);
933 m_selectedItems.clear();
934 }
935 }
936
937 void DolphinView::showHoverInformation(const KFileItem& item)
938 {
939 emit requestItemInfo(item);
940 }
941
942 void DolphinView::clearHoverInformation()
943 {
944 emit requestItemInfo(KFileItem());
945 }
946
947 void DolphinView::slotDeleteFileFinished(KJob* job)
948 {
949 if (job->error() == 0) {
950 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
951 } else if (job->error() != KIO::ERR_USER_CANCELED) {
952 emit errorMessage(job->errorString());
953 }
954 }
955
956 void DolphinView::slotDirListerStarted(const KUrl& url)
957 {
958 // Disable the writestate temporary until it can be determined in a fast way
959 // in DolphinView::slotDirListerCompleted()
960 if (m_isFolderWritable) {
961 m_isFolderWritable = false;
962 emit writeStateChanged(m_isFolderWritable);
963 }
964
965 emit startedPathLoading(url);
966 }
967
968 void DolphinView::slotDirListerCompleted()
969 {
970 // Update the view-state. This has to be done using a Qt::QueuedConnection
971 // because the view might not be in its final state yet (the view also
972 // listens to the completed()-signal from KDirLister and the order of
973 // of slots is undefined).
974 QTimer::singleShot(0, this, SLOT(updateViewState()));
975
976 emit finishedPathLoading(url());
977
978 updateWritableState();
979 }
980
981 void DolphinView::slotRefreshItems()
982 {
983 if (m_assureVisibleCurrentIndex) {
984 m_assureVisibleCurrentIndex = false;
985 //QAbstractItemView* view = m_viewAccessor.itemView();
986 //if (view) {
987 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
988 //}
989 }
990 }
991
992 KFileItemModel* DolphinView::fileItemModel() const
993 {
994 return static_cast<KFileItemModel*>(m_container->controller()->model());
995 }
996
997 void DolphinView::loadDirectory(const KUrl& url, bool reload)
998 {
999 if (!url.isValid()) {
1000 const QString location(url.pathOrUrl());
1001 if (location.isEmpty()) {
1002 emit errorMessage(i18nc("@info:status", "The location is empty."));
1003 } else {
1004 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1005 }
1006 return;
1007 }
1008
1009 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1010 }
1011
1012 void DolphinView::applyViewProperties()
1013 {
1014 m_container->beginTransaction();
1015
1016 const ViewProperties props(url());
1017
1018 const Mode mode = props.viewMode();
1019 if (m_mode != mode) {
1020 const Mode previousMode = m_mode;
1021 m_mode = mode;
1022
1023 // Changing the mode might result in changing
1024 // the zoom level. Remember the old zoom level so
1025 // that zoomLevelChanged() can get emitted.
1026 const int oldZoomLevel = m_container->zoomLevel();
1027
1028 switch (m_mode) {
1029 case IconsView: m_container->setItemLayout(KFileItemListView::IconsLayout); break;
1030 case CompactView: m_container->setItemLayout(KFileItemListView::CompactLayout); break;
1031 case DetailsView: m_container->setItemLayout(KFileItemListView::DetailsLayout); break;
1032 default: Q_ASSERT(false); break;
1033 }
1034
1035 emit modeChanged(m_mode, previousMode);
1036
1037 if (m_container->zoomLevel() != oldZoomLevel) {
1038 emit zoomLevelChanged(m_container->zoomLevel(), oldZoomLevel);
1039 }
1040 }
1041
1042 const bool hiddenFilesShown = props.hiddenFilesShown();
1043 if (hiddenFilesShown != m_dirLister->showingDotFiles()) {
1044 m_dirLister->setShowingDotFiles(hiddenFilesShown);
1045 m_dirLister->emitChanges();
1046 emit hiddenFilesShownChanged(hiddenFilesShown);
1047 }
1048
1049 /* m_storedCategorizedSorting = props.categorizedSorting();
1050 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1051 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1052 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1053 emit categorizedSortingChanged();
1054 }*/
1055
1056 const DolphinView::Sorting sorting = props.sorting();
1057 KItemModelBase* model = m_container->controller()->model();
1058 const QByteArray newSortRole = sortRoleForSorting(sorting);
1059 if (newSortRole != model->sortRole()) {
1060 model->setSortRole(newSortRole);
1061 emit sortingChanged(sorting);
1062 }
1063 /*
1064 const Qt::SortOrder sortOrder = props.sortOrder();
1065 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1066 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1067 emit sortOrderChanged(sortOrder);
1068 }
1069
1070 const bool sortFoldersFirst = props.sortFoldersFirst();
1071 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1072 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1073 emit sortFoldersFirstChanged(sortFoldersFirst);
1074 }
1075 */
1076 const QList<DolphinView::AdditionalInfo> infoList = props.additionalInfoList();
1077 if (infoList != m_additionalInfoList) {
1078 const QList<DolphinView::AdditionalInfo> previousList = m_additionalInfoList;
1079 m_additionalInfoList = infoList;
1080 applyAdditionalInfoListToView();
1081 emit additionalInfoListChanged(m_additionalInfoList, previousList);
1082 }
1083
1084 const bool previewsShown = props.previewsShown();
1085 if (previewsShown != m_container->previewsShown()) {
1086 const int oldZoomLevel = zoomLevel();
1087
1088 m_container->setPreviewsShown(previewsShown);
1089 emit previewsShownChanged(previewsShown);
1090
1091 // Changing the preview-state might result in a changed zoom-level
1092 if (oldZoomLevel != zoomLevel()) {
1093 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
1094 }
1095 }
1096
1097 m_container->endTransaction();
1098 }
1099
1100 void DolphinView::applyAdditionalInfoListToView()
1101 {
1102 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
1103
1104 QHash<QByteArray, int> visibleRoles;
1105 visibleRoles.insert("name", 0);
1106
1107 int index = 1;
1108 foreach (AdditionalInfo info, m_additionalInfoList) {
1109 visibleRoles.insert(infoAccessor.role(info), index);
1110 ++index;
1111 }
1112
1113 m_container->setVisibleRoles(visibleRoles);
1114 }
1115
1116 void DolphinView::pasteToUrl(const KUrl& url)
1117 {
1118 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1119 KonqOperations::doPaste(this, url);
1120 }
1121
1122 void DolphinView::updateZoomLevel(int oldZoomLevel)
1123 {
1124 Q_UNUSED(oldZoomLevel);
1125 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1126 if (oldZoomLevel != newZoomLevel) {
1127 m_viewModeController->setZoomLevel(newZoomLevel);
1128 emit zoomLevelChanged(newZoomLevel);
1129 }*/
1130 }
1131
1132 KUrl::List DolphinView::simplifiedSelectedUrls() const
1133 {
1134 Q_ASSERT(false); // TODO
1135 KUrl::List urls;
1136 /*
1137 const KFileItemList items = selectedItems();
1138 foreach (const KFileItem &item, items) {
1139 urls.append(item.url());
1140 }
1141
1142
1143 if (itemsExpandable()) {
1144 urls = KDirModel::simplifiedUrlList(urls);
1145 }*/
1146
1147 return urls;
1148 }
1149
1150 QMimeData* DolphinView::selectionMimeData() const
1151 {
1152 /*const QAbstractItemView* view = m_viewAccessor.itemView();
1153 Q_ASSERT((view) && (view->selectionModel()));
1154 const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
1155 return m_viewAccessor.dirModel()->mimeData(selection.indexes());*/
1156 return 0;
1157 }
1158
1159 void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
1160 {
1161 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1162 markUrlsAsSelected(urls);
1163 }
1164
1165 QItemSelection DolphinView::childrenMatchingPattern(const QModelIndex& parent, const QRegExp& pattern) const
1166 {
1167 Q_UNUSED(parent);
1168 Q_UNUSED(pattern);
1169 QItemSelection matchingIndexes;
1170 /*const DolphinSortFilterProxyModel* proxyModel = m_viewAccessor.proxyModel();
1171 const DolphinModel* dolphinModel = m_viewAccessor.dirModel();
1172
1173 const int rowCount = proxyModel->rowCount(parent);
1174
1175 for (int row = 0; row < rowCount; ++row) {
1176 QModelIndex index = proxyModel->index(row, 0, parent);
1177 QModelIndex sourceIndex = proxyModel->mapToSource(index);
1178
1179 if (sourceIndex.isValid() && pattern.exactMatch(dolphinModel->data(sourceIndex).toString())) {
1180 matchingIndexes += QItemSelectionRange(index);
1181 }
1182
1183 if (proxyModel->hasChildren(index)) {
1184 matchingIndexes += childrenMatchingPattern(index, pattern);
1185 }
1186 }*/
1187
1188 return matchingIndexes;
1189 }
1190
1191 void DolphinView::updateWritableState()
1192 {
1193 const bool wasFolderWritable = m_isFolderWritable;
1194 m_isFolderWritable = true;
1195
1196 const KFileItem item = m_dirLister->rootItem();
1197 if (!item.isNull()) {
1198 KFileItemListProperties capabilities(KFileItemList() << item);
1199 m_isFolderWritable = capabilities.supportsWriting();
1200 }
1201 if (m_isFolderWritable != wasFolderWritable) {
1202 emit writeStateChanged(m_isFolderWritable);
1203 }
1204 }
1205
1206 QByteArray DolphinView::sortRoleForSorting(Sorting sorting) const
1207 {
1208 switch (sorting) {
1209 case SortByName: return "name";
1210 case SortBySize: return "size";
1211 case SortByDate: return "date";
1212 case SortByPermissions: return "permissions";
1213 case SortByOwner: return "owner";
1214 case SortByGroup: return "group";
1215 case SortByType: return "type";
1216 case SortByDestination: return "destination";
1217 case SortByPath: return "path";
1218 default: break;
1219 }
1220
1221 return QByteArray();
1222 }
1223
1224 #include "dolphinview.moc"