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