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