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