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