]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Revert the 2.0 decision to always use KB for file-sizes
[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,
543 KGlobal::locale()->formatByteSize(totalFileSize));
544 } else if (fileCount > 0) {
545 summary = i18nc("@info:status files (size)", "%1 (%2)",
546 filesText,
547 KGlobal::locale()->formatByteSize(totalFileSize));
548 } else if (folderCount > 0) {
549 summary = foldersText;
550 }
551
552 return summary;
553 }
554
555 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
556 {
557 QList<QAction*> actions;
558
559 if (items.isEmpty()) {
560 const KFileItem item = fileItemModel()->rootItem();
561 actions = m_versionControlObserver->actions(KFileItemList() << item);
562 } else {
563 actions = m_versionControlObserver->actions(items);
564 }
565
566 return actions;
567 }
568
569 void DolphinView::setUrl(const KUrl& url)
570 {
571 if (url == m_url) {
572 return;
573 }
574
575 emit urlAboutToBeChanged(url);
576 m_url = url;
577
578 hideToolTip();
579
580 // It is important to clear the items from the model before
581 // applying the view properties, otherwise expensive operations
582 // might be done on the existing items although they get cleared
583 // anyhow afterwards by loadDirectory().
584 fileItemModel()->clear();
585 applyViewProperties();
586 loadDirectory(url);
587
588 emit urlChanged(url);
589 }
590
591 void DolphinView::selectAll()
592 {
593 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
594 selectionManager->setSelected(0, fileItemModel()->count());
595 }
596
597 void DolphinView::invertSelection()
598 {
599 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
600 selectionManager->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle);
601 }
602
603 void DolphinView::clearSelection()
604 {
605 m_container->controller()->selectionManager()->clearSelection();
606 }
607
608 void DolphinView::renameSelectedItems()
609 {
610 KFileItemList items = selectedItems();
611 const int itemCount = items.count();
612 if (itemCount < 1) {
613 return;
614 }
615
616 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
617 // renaming yet.
618 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
619 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
620 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
621 m_viewAccessor.itemView()->edit(proxyIndex);
622 } else {*/
623 RenameDialog* dialog = new RenameDialog(this, items);
624 dialog->setAttribute(Qt::WA_DeleteOnClose);
625 dialog->show();
626 dialog->raise();
627 dialog->activateWindow();
628 //}
629
630 // assure that the current index remains visible when KDirLister
631 // will notify the view about changed items
632 m_assureVisibleCurrentIndex = true;
633 }
634
635 void DolphinView::trashSelectedItems()
636 {
637 const KUrl::List list = simplifiedSelectedUrls();
638 KonqOperations::del(this, KonqOperations::TRASH, list);
639 }
640
641 void DolphinView::deleteSelectedItems()
642 {
643 const KUrl::List list = simplifiedSelectedUrls();
644 const bool del = KonqOperations::askDeleteConfirmation(list,
645 KonqOperations::DEL,
646 KonqOperations::DEFAULT_CONFIRMATION,
647 this);
648
649 if (del) {
650 KIO::Job* job = KIO::del(list);
651 connect(job, SIGNAL(result(KJob*)),
652 this, SLOT(slotDeleteFileFinished(KJob*)));
653 }
654 }
655
656 void DolphinView::cutSelectedItems()
657 {
658 QMimeData* mimeData = selectionMimeData();
659 KonqMimeData::addIsCutSelection(mimeData, true);
660 QApplication::clipboard()->setMimeData(mimeData);
661 }
662
663 void DolphinView::copySelectedItems()
664 {
665 QMimeData* mimeData = selectionMimeData();
666 QApplication::clipboard()->setMimeData(mimeData);
667 }
668
669 void DolphinView::paste()
670 {
671 pasteToUrl(url());
672 }
673
674 void DolphinView::pasteIntoFolder()
675 {
676 const KFileItemList items = selectedItems();
677 if ((items.count() == 1) && items.first().isDir()) {
678 pasteToUrl(items.first().url());
679 }
680 }
681
682 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
683 {
684 switch (event->type()) {
685 case QEvent::FocusIn:
686 if (watched == m_container) {
687 setActive(true);
688 }
689 break;
690
691 case QEvent::GraphicsSceneDragEnter:
692 if (watched == m_container->controller()->view()) {
693 m_dragging = true;
694 }
695 break;
696
697 case QEvent::GraphicsSceneDragLeave:
698 if (watched == m_container->controller()->view()) {
699 m_dragging = false;
700 }
701 break;
702
703 case QEvent::GraphicsSceneDrop:
704 if (watched == m_container->controller()->view()) {
705 m_dragging = false;
706 }
707 default:
708 break;
709 }
710
711 return QWidget::eventFilter(watched, event);
712 }
713
714 void DolphinView::wheelEvent(QWheelEvent* event)
715 {
716 if (event->modifiers().testFlag(Qt::ControlModifier)) {
717 const int numDegrees = event->delta() / 8;
718 const int numSteps = numDegrees / 15;
719
720 setZoomLevel(zoomLevel() + numSteps);
721 event->accept();
722 } else {
723 event->ignore();
724 }
725 }
726
727 void DolphinView::hideEvent(QHideEvent* event)
728 {
729 hideToolTip();
730 QWidget::hideEvent(event);
731 }
732
733 void DolphinView::activate()
734 {
735 setActive(true);
736 }
737
738 void DolphinView::slotItemActivated(int index)
739 {
740 const KFileItem item = fileItemModel()->fileItem(index);
741 if (!item.isNull()) {
742 emit itemActivated(item);
743 }
744 }
745
746 void DolphinView::slotItemsActivated(const QSet<int>& indexes)
747 {
748 Q_ASSERT(indexes.count() >= 2);
749
750 KFileItemList items;
751
752 KFileItemModel* model = fileItemModel();
753 QSetIterator<int> it(indexes);
754 while (it.hasNext()) {
755 const int index = it.next();
756 items.append(model->fileItem(index));
757 }
758
759 foreach (const KFileItem& item, items) {
760 if (item.isDir()) {
761 emit tabRequested(item.url());
762 } else {
763 emit itemActivated(item);
764 }
765 }
766 }
767
768 void DolphinView::slotItemMiddleClicked(int index)
769 {
770 const KFileItem item = fileItemModel()->fileItem(index);
771 if (item.isDir() || isTabsForFilesEnabled()) {
772 emit tabRequested(item.url());
773 }
774 }
775
776 void DolphinView::slotItemContextMenuRequested(int index, const QPointF& pos)
777 {
778 const KFileItem item = fileItemModel()->fileItem(index);
779 emit requestContextMenu(pos.toPoint(), item, url(), QList<QAction*>());
780 }
781
782 void DolphinView::slotViewContextMenuRequested(const QPointF& pos)
783 {
784 emit requestContextMenu(pos.toPoint(), KFileItem(), url(), QList<QAction*>());
785 }
786
787 void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
788 {
789 ViewProperties props(url());
790
791 QPointer<KMenu> menu = new KMenu(QApplication::activeWindow());
792
793 KItemListView* view = m_container->controller()->view();
794 const QSet<QByteArray> visibleRolesSet = view->visibleRoles().toSet();
795
796 bool nepomukRunning = false;
797 bool indexingEnabled = false;
798 #ifdef HAVE_NEPOMUK
799 nepomukRunning = (Nepomuk::ResourceManager::instance()->init() == 0);
800 if (nepomukRunning) {
801 KConfig config("nepomukserverrc");
802 indexingEnabled = config.group("Service-nepomukfileindexer").readEntry("autostart", false);
803 }
804 #endif
805
806 QString groupName;
807 QMenu* groupMenu = 0;
808
809 // Add all roles to the menu that can be shown or hidden by the user
810 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
811 foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
812 if (info.role == "name") {
813 // It should not be possible to hide the "name" role
814 continue;
815 }
816
817 const QString text = fileItemModel()->roleDescription(info.role);
818 QAction* action = 0;
819 if (info.group.isEmpty()) {
820 action = menu->addAction(text);
821 } else {
822 if (!groupMenu || info.group != groupName) {
823 groupName = info.group;
824 groupMenu = menu->addMenu(groupName);
825 }
826
827 action = groupMenu->addAction(text);
828 }
829
830 action->setCheckable(true);
831 action->setChecked(visibleRolesSet.contains(info.role));
832 action->setData(info.role);
833
834 const bool enable = (!info.requiresNepomuk && !info.requiresIndexer) ||
835 (info.requiresNepomuk && nepomukRunning) ||
836 (info.requiresIndexer && indexingEnabled);
837 action->setEnabled(enable);
838 }
839
840 menu->addSeparator();
841
842 QActionGroup* widthsGroup = new QActionGroup(menu);
843 const bool autoColumnWidths = props.headerColumnWidths().isEmpty();
844
845 QAction* autoAdjustWidthsAction = menu->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
846 autoAdjustWidthsAction->setCheckable(true);
847 autoAdjustWidthsAction->setChecked(autoColumnWidths);
848 autoAdjustWidthsAction->setActionGroup(widthsGroup);
849
850 QAction* customWidthsAction = menu->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
851 customWidthsAction->setCheckable(true);
852 customWidthsAction->setChecked(!autoColumnWidths);
853 customWidthsAction->setActionGroup(widthsGroup);
854
855 QAction* action = menu->exec(pos.toPoint());
856 if (menu && action) {
857 KItemListHeader* header = view->header();
858
859 if (action == autoAdjustWidthsAction) {
860 // Clear the column-widths from the viewproperties and turn on
861 // the automatic resizing of the columns
862 props.setHeaderColumnWidths(QList<int>());
863 header->setAutomaticColumnResizing(true);
864 } else if (action == customWidthsAction) {
865 // Apply the current column-widths as custom column-widths and turn
866 // off the automatic resizing of the columns
867 QList<int> columnWidths;
868 foreach (const QByteArray& role, view->visibleRoles()) {
869 columnWidths.append(header->columnWidth(role));
870 }
871 props.setHeaderColumnWidths(columnWidths);
872 header->setAutomaticColumnResizing(false);
873 } else {
874 // Show or hide the selected role
875 const QByteArray selectedRole = action->data().toByteArray();
876
877 QList<QByteArray> visibleRoles = view->visibleRoles();
878 if (action->isChecked()) {
879 visibleRoles.append(selectedRole);
880 } else {
881 visibleRoles.removeOne(selectedRole);
882 }
883
884 view->setVisibleRoles(visibleRoles);
885 props.setVisibleRoles(visibleRoles);
886
887 QList<int> columnWidths;
888 if (!header->automaticColumnResizing()) {
889 foreach (const QByteArray& role, view->visibleRoles()) {
890 columnWidths.append(header->columnWidth(role));
891 }
892 }
893 props.setHeaderColumnWidths(columnWidths);
894 }
895 }
896
897 delete menu;
898 }
899
900 void DolphinView::slotHeaderColumnWidthChanged(const QByteArray& role, qreal current, qreal previous)
901 {
902 Q_UNUSED(previous);
903
904 const QList<QByteArray> visibleRoles = m_container->visibleRoles();
905
906 ViewProperties props(url());
907 QList<int> columnWidths = props.headerColumnWidths();
908 if (columnWidths.count() != visibleRoles.count()) {
909 columnWidths.clear();
910 columnWidths.reserve(visibleRoles.count());
911 const KItemListHeader* header = m_container->controller()->view()->header();
912 foreach (const QByteArray& role, visibleRoles) {
913 const int width = header->columnWidth(role);
914 columnWidths.append(width);
915 }
916 }
917
918 const int roleIndex = visibleRoles.indexOf(role);
919 Q_ASSERT(roleIndex >= 0 && roleIndex < columnWidths.count());
920 columnWidths[roleIndex] = current;
921
922 props.setHeaderColumnWidths(columnWidths);
923 }
924
925 void DolphinView::slotItemHovered(int index)
926 {
927 const KFileItem item = fileItemModel()->fileItem(index);
928
929 if (GeneralSettings::showToolTips() && !m_dragging) {
930 QRectF itemRect = m_container->controller()->view()->itemContextRect(index);
931 const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
932 itemRect.moveTo(pos);
933
934 m_toolTipManager->showToolTip(item, itemRect);
935 }
936
937 emit requestItemInfo(item);
938 }
939
940 void DolphinView::slotItemUnhovered(int index)
941 {
942 Q_UNUSED(index);
943 hideToolTip();
944 emit requestItemInfo(KFileItem());
945 }
946
947 void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
948 {
949 KUrl destUrl;
950 KFileItem destItem = fileItemModel()->fileItem(index);
951 if (destItem.isNull() || (!destItem.isDir() && !destItem.isDesktopFile())) {
952 // Use the URL of the view as drop target if the item is no directory
953 // or desktop-file
954 destItem = fileItemModel()->rootItem();
955 destUrl = url();
956 } else {
957 // The item represents a directory or desktop-file
958 destUrl = destItem.url();
959 }
960
961 QDropEvent dropEvent(event->pos().toPoint(),
962 event->possibleActions(),
963 event->mimeData(),
964 event->buttons(),
965 event->modifiers());
966
967 const QString error = DragAndDropHelper::dropUrls(destItem, destUrl, &dropEvent);
968 if (!error.isEmpty()) {
969 emit errorMessage(error);
970 }
971
972 if (destUrl == url()) {
973 // Mark the dropped urls as selected.
974 markPastedUrlsAsSelected(event->mimeData());
975 }
976 }
977
978 void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
979 {
980 if (previous != 0) {
981 disconnect(previous, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
982 }
983
984 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
985 connect(current, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
986
987 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(current);
988 m_versionControlObserver->setModel(fileItemModel);
989 }
990
991 void DolphinView::slotMouseButtonPressed(int itemIndex, Qt::MouseButtons buttons)
992 {
993 hideToolTip();
994
995 if (itemIndex < 0) {
996 // Trigger the history navigation only when clicking on the viewport:
997 // Above an item the XButtons provide a simple way to select items in
998 // the singleClick mode.
999 if (buttons & Qt::XButton1) {
1000 emit goBackRequested();
1001 } else if (buttons & Qt::XButton2) {
1002 emit goForwardRequested();
1003 }
1004 }
1005 }
1006
1007 void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
1008 {
1009 const int currentCount = current.count();
1010 const int previousCount = previous.count();
1011 const bool selectionStateChanged = (currentCount == 0 && previousCount > 0) ||
1012 (currentCount > 0 && previousCount == 0);
1013
1014 // If nothing has been selected before and something got selected (or if something
1015 // was selected before and now nothing is selected) the selectionChangedSignal must
1016 // be emitted asynchronously as fast as possible to update the edit-actions.
1017 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
1018 m_selectionChangedTimer->start();
1019 }
1020
1021 void DolphinView::emitSelectionChangedSignal()
1022 {
1023 m_selectionChangedTimer->stop();
1024 emit selectionChanged(selectedItems());
1025 }
1026
1027 void DolphinView::updateSortRole(const QByteArray& role)
1028 {
1029 ViewProperties props(url());
1030 props.setSortRole(role);
1031
1032 KItemModelBase* model = m_container->controller()->model();
1033 model->setSortRole(role);
1034
1035 emit sortRoleChanged(role);
1036 }
1037
1038 void DolphinView::updateSortOrder(Qt::SortOrder order)
1039 {
1040 ViewProperties props(url());
1041 props.setSortOrder(order);
1042
1043 KItemModelBase* model = fileItemModel();
1044 model->setSortOrder(order);
1045
1046 emit sortOrderChanged(order);
1047 }
1048
1049 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
1050 {
1051 ViewProperties props(url());
1052 props.setSortFoldersFirst(foldersFirst);
1053
1054 KFileItemModel* model = fileItemModel();
1055 model->setSortFoldersFirst(foldersFirst);
1056
1057 emit sortFoldersFirstChanged(foldersFirst);
1058 }
1059
1060 QPair<bool, QString> DolphinView::pasteInfo() const
1061 {
1062 return KonqOperations::pasteInfo(url());
1063 }
1064
1065 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
1066 {
1067 m_tabsForFiles = tabsForFiles;
1068 }
1069
1070 bool DolphinView::isTabsForFilesEnabled() const
1071 {
1072 return m_tabsForFiles;
1073 }
1074
1075 bool DolphinView::itemsExpandable() const
1076 {
1077 return m_mode == DetailsView;
1078 }
1079
1080 void DolphinView::restoreState(QDataStream& stream)
1081 {
1082 // Restore the current item that had the keyboard focus
1083 stream >> m_currentItemUrl;
1084
1085 // Restore the view position
1086 stream >> m_restoredContentsPosition;
1087
1088 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1089 QSet<KUrl> urls;
1090 stream >> urls;
1091 fileItemModel()->restoreExpandedUrls(urls);
1092 }
1093
1094 void DolphinView::saveState(QDataStream& stream)
1095 {
1096 // Save the current item that has the keyboard focus
1097 const int currentIndex = m_container->controller()->selectionManager()->currentItem();
1098 if (currentIndex != -1) {
1099 KFileItem item = fileItemModel()->fileItem(currentIndex);
1100 Q_ASSERT(!item.isNull()); // If the current index is valid a item must exist
1101 KUrl currentItemUrl = item.url();
1102 stream << currentItemUrl;
1103 } else {
1104 stream << KUrl();
1105 }
1106
1107 // Save view position
1108 const qreal x = m_container->horizontalScrollBar()->value();
1109 const qreal y = m_container->verticalScrollBar()->value();
1110 stream << QPoint(x, y);
1111
1112 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1113 stream << fileItemModel()->expandedUrls();
1114 }
1115
1116 bool DolphinView::hasSelection() const
1117 {
1118 return m_container->controller()->selectionManager()->hasSelection();
1119 }
1120
1121 KFileItem DolphinView::rootItem() const
1122 {
1123 return m_dirLister->rootItem();
1124 }
1125
1126 void DolphinView::observeCreatedItem(const KUrl& url)
1127 {
1128 m_createdItemUrl = url;
1129 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1130 // this, SLOT(selectAndScrollToCreatedItem()));
1131 }
1132
1133 void DolphinView::selectAndScrollToCreatedItem()
1134 {
1135 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1136 if (dirIndex.isValid()) {
1137 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1138 QAbstractItemView* view = m_viewAccessor.itemView();
1139 if (view) {
1140 view->setCurrentIndex(proxyIndex);
1141 }
1142 }
1143
1144 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1145 this, SLOT(selectAndScrollToCreatedItem()));*/
1146 m_createdItemUrl = KUrl();
1147 }
1148
1149 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1150 {
1151 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
1152 emit redirection(oldUrl, newUrl);
1153 m_url = newUrl; // #186947
1154 }
1155 }
1156
1157 void DolphinView::updateViewState()
1158 {
1159 if (m_currentItemUrl != KUrl()) {
1160 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1161 const int currentIndex = fileItemModel()->index(m_currentItemUrl);
1162 if (currentIndex != -1) {
1163 selectionManager->setCurrentItem(currentIndex);
1164 } else {
1165 selectionManager->setCurrentItem(0);
1166 }
1167 m_currentItemUrl = KUrl();
1168 }
1169
1170 if (!m_restoredContentsPosition.isNull()) {
1171 const int x = m_restoredContentsPosition.x();
1172 const int y = m_restoredContentsPosition.y();
1173 m_restoredContentsPosition = QPoint();
1174
1175 m_container->horizontalScrollBar()->setValue(x);
1176 m_container->verticalScrollBar()->setValue(y);
1177 }
1178
1179 if (!m_selectedUrls.isEmpty()) {
1180 clearSelection();
1181
1182 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1183 QSet<int> selectedItems = selectionManager->selectedItems();
1184 const KFileItemModel* model = fileItemModel();
1185
1186 foreach (const KUrl& url, m_selectedUrls) {
1187 const int index = model->index(url);
1188 if (index >= 0) {
1189 selectedItems.insert(index);
1190 }
1191 }
1192
1193 selectionManager->setSelectedItems(selectedItems);
1194 m_selectedUrls.clear();
1195 }
1196 }
1197
1198 void DolphinView::hideToolTip()
1199 {
1200 if (GeneralSettings::showToolTips()) {
1201 m_toolTipManager->hideToolTip();
1202 }
1203 }
1204
1205 void DolphinView::showHoverInformation(const KFileItem& item)
1206 {
1207 emit requestItemInfo(item);
1208 }
1209
1210 void DolphinView::clearHoverInformation()
1211 {
1212 emit requestItemInfo(KFileItem());
1213 }
1214
1215 void DolphinView::slotDeleteFileFinished(KJob* job)
1216 {
1217 if (job->error() == 0) {
1218 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1219 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1220 emit errorMessage(job->errorString());
1221 }
1222 }
1223
1224 void DolphinView::slotDirListerStarted(const KUrl& url)
1225 {
1226 // Disable the writestate temporary until it can be determined in a fast way
1227 // in DolphinView::slotLoadingCompleted()
1228 if (m_isFolderWritable) {
1229 m_isFolderWritable = false;
1230 emit writeStateChanged(m_isFolderWritable);
1231 }
1232
1233 emit startedPathLoading(url);
1234 }
1235
1236 void DolphinView::slotLoadingCompleted()
1237 {
1238 // Update the view-state. This has to be done using a Qt::QueuedConnection
1239 // because the view might not be in its final state yet.
1240 QTimer::singleShot(0, this, SLOT(updateViewState()));
1241
1242 emit finishedPathLoading(url());
1243
1244 updateWritableState();
1245 }
1246
1247 void DolphinView::slotRefreshItems()
1248 {
1249 if (m_assureVisibleCurrentIndex) {
1250 m_assureVisibleCurrentIndex = false;
1251 //QAbstractItemView* view = m_viewAccessor.itemView();
1252 //if (view) {
1253 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1254 //}
1255 }
1256 }
1257
1258 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current, Qt::SortOrder previous)
1259 {
1260 Q_UNUSED(previous);
1261 Q_ASSERT(fileItemModel()->sortOrder() == current);
1262
1263 ViewProperties props(url());
1264 props.setSortOrder(current);
1265
1266 emit sortOrderChanged(current);
1267 }
1268
1269 void DolphinView::slotSortRoleChangedByHeader(const QByteArray& current, const QByteArray& previous)
1270 {
1271 Q_UNUSED(previous);
1272 Q_ASSERT(fileItemModel()->sortRole() == current);
1273
1274 ViewProperties props(url());
1275 props.setSortRole(current);
1276
1277 emit sortRoleChanged(current);
1278 }
1279
1280 void DolphinView::slotVisibleRolesChangedByHeader(const QList<QByteArray>& current,
1281 const QList<QByteArray>& previous)
1282 {
1283 Q_UNUSED(previous);
1284 Q_ASSERT(m_container->controller()->view()->visibleRoles() == current);
1285
1286 const QList<QByteArray> previousVisibleRoles = m_visibleRoles;
1287
1288 m_visibleRoles = current;
1289
1290 ViewProperties props(url());
1291 props.setVisibleRoles(m_visibleRoles);
1292
1293 emit visibleRolesChanged(m_visibleRoles, previousVisibleRoles);
1294 }
1295
1296 KFileItemModel* DolphinView::fileItemModel() const
1297 {
1298 return static_cast<KFileItemModel*>(m_container->controller()->model());
1299 }
1300
1301 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1302 {
1303 if (!url.isValid()) {
1304 const QString location(url.pathOrUrl());
1305 if (location.isEmpty()) {
1306 emit errorMessage(i18nc("@info:status", "The location is empty."));
1307 } else {
1308 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1309 }
1310 return;
1311 }
1312
1313 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1314 }
1315
1316 void DolphinView::applyViewProperties()
1317 {
1318 m_container->beginTransaction();
1319
1320 const ViewProperties props(url());
1321 KFileItemModel* model = fileItemModel();
1322
1323 const Mode mode = props.viewMode();
1324 if (m_mode != mode) {
1325 const Mode previousMode = m_mode;
1326 m_mode = mode;
1327
1328 // Changing the mode might result in changing
1329 // the zoom level. Remember the old zoom level so
1330 // that zoomLevelChanged() can get emitted.
1331 const int oldZoomLevel = m_container->zoomLevel();
1332
1333 switch (m_mode) {
1334 case IconsView: m_container->setItemLayout(KFileItemListView::IconsLayout); break;
1335 case CompactView: m_container->setItemLayout(KFileItemListView::CompactLayout); break;
1336 case DetailsView: m_container->setItemLayout(KFileItemListView::DetailsLayout); break;
1337 default: Q_ASSERT(false); break;
1338 }
1339
1340 emit modeChanged(m_mode, previousMode);
1341
1342 if (m_container->zoomLevel() != oldZoomLevel) {
1343 emit zoomLevelChanged(m_container->zoomLevel(), oldZoomLevel);
1344 }
1345 }
1346
1347 const bool hiddenFilesShown = props.hiddenFilesShown();
1348 if (hiddenFilesShown != model->showHiddenFiles()) {
1349 model->setShowHiddenFiles(hiddenFilesShown);
1350 emit hiddenFilesShownChanged(hiddenFilesShown);
1351 }
1352
1353 const bool groupedSorting = props.groupedSorting();
1354 if (groupedSorting != model->groupedSorting()) {
1355 model->setGroupedSorting(groupedSorting);
1356 emit groupedSortingChanged(groupedSorting);
1357 }
1358
1359 const QByteArray sortRole = props.sortRole();
1360 if (sortRole != model->sortRole()) {
1361 model->setSortRole(sortRole);
1362 emit sortRoleChanged(sortRole);
1363 }
1364
1365 const Qt::SortOrder sortOrder = props.sortOrder();
1366 if (sortOrder != model->sortOrder()) {
1367 model->setSortOrder(sortOrder);
1368 emit sortOrderChanged(sortOrder);
1369 }
1370
1371 const bool sortFoldersFirst = props.sortFoldersFirst();
1372 if (sortFoldersFirst != model->sortFoldersFirst()) {
1373 model->setSortFoldersFirst(sortFoldersFirst);
1374 emit sortFoldersFirstChanged(sortFoldersFirst);
1375 }
1376
1377 const QList<QByteArray> visibleRoles = props.visibleRoles();
1378 if (visibleRoles != m_visibleRoles) {
1379 const QList<QByteArray> previousVisibleRoles = m_visibleRoles;
1380 m_visibleRoles = visibleRoles;
1381 m_container->setVisibleRoles(visibleRoles);
1382 emit visibleRolesChanged(m_visibleRoles, previousVisibleRoles);
1383 }
1384
1385 const bool previewsShown = props.previewsShown();
1386 if (previewsShown != m_container->previewsShown()) {
1387 const int oldZoomLevel = zoomLevel();
1388
1389 m_container->setPreviewsShown(previewsShown);
1390 emit previewsShownChanged(previewsShown);
1391
1392 // Changing the preview-state might result in a changed zoom-level
1393 if (oldZoomLevel != zoomLevel()) {
1394 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
1395 }
1396 }
1397
1398 KItemListView* itemListView = m_container->controller()->view();
1399 if (itemListView->isHeaderVisible()) {
1400 KItemListHeader* header = itemListView->header();
1401 const QList<int> headerColumnWidths = props.headerColumnWidths();
1402 const int rolesCount = m_visibleRoles.count();
1403 if (headerColumnWidths.count() == rolesCount) {
1404 header->setAutomaticColumnResizing(false);
1405
1406 QHash<QByteArray, qreal> columnWidths;
1407 for (int i = 0; i < rolesCount; ++i) {
1408 columnWidths.insert(m_visibleRoles[i], headerColumnWidths[i]);
1409 }
1410 header->setColumnWidths(columnWidths);
1411 } else {
1412 header->setAutomaticColumnResizing(true);
1413 }
1414 }
1415
1416 m_container->endTransaction();
1417 }
1418
1419 void DolphinView::pasteToUrl(const KUrl& url)
1420 {
1421 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1422 KonqOperations::doPaste(this, url);
1423 }
1424
1425 KUrl::List DolphinView::simplifiedSelectedUrls() const
1426 {
1427 KUrl::List urls;
1428
1429 const KFileItemList items = selectedItems();
1430 foreach (const KFileItem &item, items) {
1431 urls.append(item.url());
1432 }
1433
1434 if (itemsExpandable()) {
1435 // TODO: Check if we still need KDirModel for this in KDE 5.0
1436 urls = KDirModel::simplifiedUrlList(urls);
1437 }
1438
1439 return urls;
1440 }
1441
1442 QMimeData* DolphinView::selectionMimeData() const
1443 {
1444 const KFileItemModel* model = fileItemModel();
1445 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1446 const QSet<int> selectedIndexes = selectionManager->selectedItems();
1447
1448 return model->createMimeData(selectedIndexes);
1449 }
1450
1451 void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
1452 {
1453 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1454 KUrl::List destUrls;
1455 foreach (const KUrl& source, sourceUrls) {
1456 KUrl destination(url().url() + "/" + source.fileName());
1457 destUrls << destination;
1458 }
1459 markUrlsAsSelected(destUrls);
1460 }
1461
1462 void DolphinView::updateWritableState()
1463 {
1464 const bool wasFolderWritable = m_isFolderWritable;
1465 m_isFolderWritable = true;
1466
1467 const KFileItem item = m_dirLister->rootItem();
1468 if (!item.isNull()) {
1469 KFileItemListProperties capabilities(KFileItemList() << item);
1470 m_isFolderWritable = capabilities.supportsWriting();
1471 }
1472 if (m_isFolderWritable != wasFolderWritable) {
1473 emit writeStateChanged(m_isFolderWritable);
1474 }
1475 }
1476
1477 #include "dolphinview.moc"