]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
959e4da6e2e049a88084b28068a321c7d9e8ebbb
[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 const KItemListSelectionManager::SelectionMode mode = enabled
301 ? KItemListSelectionManager::Select
302 : KItemListSelectionManager::Deselect;
303 const KFileItemModel* model = fileItemModel();
304 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
305
306 for (int index = 0; index < model->count(); index++) {
307 const KFileItem item = model->fileItem(index);
308 if (pattern.exactMatch(item.name())) {
309 // An alternative approach would be to store the matching items in a QSet<int> and
310 // select them in one go after the loop, but we'd need a new function
311 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
312 // for that.
313 selectionManager->setSelected(index, 1, mode);
314 }
315 }
316 }
317
318 void DolphinView::setZoomLevel(int level)
319 {
320 const int oldZoomLevel = zoomLevel();
321 m_container->setZoomLevel(level);
322 if (zoomLevel() != oldZoomLevel) {
323 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
324 }
325 }
326
327 int DolphinView::zoomLevel() const
328 {
329 return m_container->zoomLevel();
330 }
331
332 void DolphinView::setSorting(Sorting sorting)
333 {
334 if (sorting != this->sorting()) {
335 updateSorting(sorting);
336 }
337 }
338
339 DolphinView::Sorting DolphinView::sorting() const
340 {
341 return DolphinView::SortByName;
342 //return m_viewAccessor.proxyModel()->sorting();
343 }
344
345 void DolphinView::setSortOrder(Qt::SortOrder order)
346 {
347 if (sortOrder() != order) {
348 updateSortOrder(order);
349 }
350 }
351
352 Qt::SortOrder DolphinView::sortOrder() const
353 {
354 return Qt::AscendingOrder; // m_viewAccessor.proxyModel()->sortOrder();
355 }
356
357 void DolphinView::setSortFoldersFirst(bool foldersFirst)
358 {
359 if (sortFoldersFirst() != foldersFirst) {
360 updateSortFoldersFirst(foldersFirst);
361 }
362 }
363
364 bool DolphinView::sortFoldersFirst() const
365 {
366 return true; // m_viewAccessor.proxyModel()->sortFoldersFirst();
367 }
368
369 void DolphinView::setAdditionalInfoList(const QList<AdditionalInfo>& info)
370 {
371 const QList<AdditionalInfo> previousList = info;
372
373 ViewProperties props(url());
374 props.setAdditionalInfoList(info);
375
376 m_additionalInfoList = info;
377 applyAdditionalInfoListToView();
378
379 emit additionalInfoListChanged(m_additionalInfoList, previousList);
380 }
381
382 QList<DolphinView::AdditionalInfo> DolphinView::additionalInfoList() const
383 {
384 return m_additionalInfoList;
385 }
386
387 void DolphinView::reload()
388 {
389 QByteArray viewState;
390 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
391 saveState(saveStream);
392 m_selectedItems= selectedItems();
393
394 setUrl(url());
395 loadDirectory(url(), true);
396
397 QDataStream restoreStream(viewState);
398 restoreState(restoreStream);
399 }
400
401 void DolphinView::stopLoading()
402 {
403 m_dirLister->stop();
404 }
405
406 void DolphinView::refresh()
407 {
408 const bool oldActivationState = m_active;
409 const int oldZoomLevel = zoomLevel();
410 m_active = true;
411
412 applyViewProperties();
413 reload();
414
415 setActive(oldActivationState);
416 updateZoomLevel(oldZoomLevel);
417 }
418
419 void DolphinView::setNameFilter(const QString& nameFilter)
420 {
421 Q_UNUSED(nameFilter);
422 //m_viewModeController->setNameFilter(nameFilter);
423 }
424
425 QString DolphinView::nameFilter() const
426 {
427 return QString(); //m_viewModeController->nameFilter();
428 }
429
430 void DolphinView::calculateItemCount(int& fileCount,
431 int& folderCount,
432 KIO::filesize_t& totalFileSize) const
433 {
434 foreach (const KFileItem& item, m_dirLister->items()) {
435 if (item.isDir()) {
436 ++folderCount;
437 } else {
438 ++fileCount;
439 totalFileSize += item.size();
440 }
441 }
442 }
443
444 QString DolphinView::statusBarText() const
445 {
446 QString text;
447 int folderCount = 0;
448 int fileCount = 0;
449 KIO::filesize_t totalFileSize = 0;
450
451 if (hasSelection()) {
452 // give a summary of the status of the selected files
453 const KFileItemList list = selectedItems();
454 if (list.isEmpty()) {
455 // when an item is triggered, it is temporary selected but selectedItems()
456 // will return an empty list
457 return text;
458 }
459
460 KFileItemList::const_iterator it = list.begin();
461 const KFileItemList::const_iterator end = list.end();
462 while (it != end) {
463 const KFileItem& item = *it;
464 if (item.isDir()) {
465 ++folderCount;
466 } else {
467 ++fileCount;
468 totalFileSize += item.size();
469 }
470 ++it;
471 }
472
473 if (folderCount + fileCount == 1) {
474 // if only one item is selected, show the filename
475 const QString name = list.first().text();
476 text = (folderCount == 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name) :
477 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
478 name, KIO::convertSize(totalFileSize));
479 } else {
480 // at least 2 items are selected
481 const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
482 const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
483 if ((folderCount > 0) && (fileCount > 0)) {
484 text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
485 foldersText, filesText, KIO::convertSize(totalFileSize));
486 } else if (fileCount > 0) {
487 text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize));
488 } else {
489 Q_ASSERT(folderCount > 0);
490 text = foldersText;
491 }
492 }
493 } else {
494 calculateItemCount(fileCount, folderCount, totalFileSize);
495 text = KIO::itemsSummaryString(fileCount + folderCount,
496 fileCount, folderCount,
497 totalFileSize, true);
498 }
499
500 return text;
501 }
502
503 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
504 {
505 Q_UNUSED(items);
506 return QList<QAction*>(); //m_dolphinViewController->versionControlActions(items);
507 }
508
509 void DolphinView::setUrl(const KUrl& url)
510 {
511 if (url == m_url) {
512 return;
513 }
514
515 emit urlAboutToBeChanged(url);
516 m_url = url;
517
518 if (GeneralSettings::showToolTips()) {
519 m_toolTipManager->hideToolTip();
520 }
521
522 // It is important to clear the items from the model before
523 // applying the view properties, otherwise expensive operations
524 // might be done on the existing items although they get cleared
525 // anyhow afterwards by loadDirectory().
526 fileItemModel()->clear();
527 applyViewProperties();
528 loadDirectory(url);
529
530 emit urlChanged(url);
531 }
532
533 void DolphinView::selectAll()
534 {
535 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
536 selectionManager->setSelected(0, fileItemModel()->count());
537 }
538
539 void DolphinView::invertSelection()
540 {
541 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
542 selectionManager->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle);
543 }
544
545 void DolphinView::clearSelection()
546 {
547 m_container->controller()->selectionManager()->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 KFileItemModel* model = fileItemModel();
1162 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1163 const QSet<int> selectedIndexes = selectionManager->selectedItems();
1164
1165 return model->createMimeData(selectedIndexes);
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 void DolphinView::updateWritableState()
1175 {
1176 const bool wasFolderWritable = m_isFolderWritable;
1177 m_isFolderWritable = true;
1178
1179 const KFileItem item = m_dirLister->rootItem();
1180 if (!item.isNull()) {
1181 KFileItemListProperties capabilities(KFileItemList() << item);
1182 m_isFolderWritable = capabilities.supportsWriting();
1183 }
1184 if (m_isFolderWritable != wasFolderWritable) {
1185 emit writeStateChanged(m_isFolderWritable);
1186 }
1187 }
1188
1189 QByteArray DolphinView::sortRoleForSorting(Sorting sorting) const
1190 {
1191 switch (sorting) {
1192 case SortByName: return "name";
1193 case SortBySize: return "size";
1194 case SortByDate: return "date";
1195 case SortByPermissions: return "permissions";
1196 case SortByOwner: return "owner";
1197 case SortByGroup: return "group";
1198 case SortByType: return "type";
1199 case SortByDestination: return "destination";
1200 case SortByPath: return "path";
1201 default: break;
1202 }
1203
1204 return QByteArray();
1205 }
1206
1207 #include "dolphinview.moc"