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