]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
8171751e4b8ed8955df66da14cf8f0efe5a66125
[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(dirLoadingStarted()), this, SLOT(slotDirLoadingStarted()));
140 connect(model, SIGNAL(dirLoadingCompleted()), this, SLOT(slotDirLoadingCompleted()));
141 connect(model, SIGNAL(dirLoadingProgress(int)), this, SIGNAL(dirLoadingProgress(int)));
142 connect(model, SIGNAL(dirSortingProgress(int)), this, SIGNAL(dirSortingProgress(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(redirection(KUrl,KUrl)), this, SLOT(slotRedirection(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->header(), SIGNAL(columnWidthChanged(QByteArray,qreal,qreal)),
160 this, SLOT(slotHeaderColumnWidthChanged(QByteArray,qreal,qreal)));
161
162 KItemListSelectionManager* selectionManager = controller->selectionManager();
163 connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)),
164 this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
165
166 m_toolTipManager = new ToolTipManager(this);
167
168 m_versionControlObserver = new VersionControlObserver(this);
169 m_versionControlObserver->setModel(model);
170 connect(m_versionControlObserver, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
171 connect(m_versionControlObserver, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString)));
172 connect(m_versionControlObserver, SIGNAL(operationCompletedMessage(QString)), this, SIGNAL(operationCompletedMessage(QString)));
173
174 applyViewProperties();
175 m_topLayout->addWidget(m_container);
176
177 loadDirectory(url);
178 }
179
180 DolphinView::~DolphinView()
181 {
182 }
183
184 KUrl DolphinView::url() const
185 {
186 return m_url;
187 }
188
189 void DolphinView::setActive(bool active)
190 {
191 if (active == m_active) {
192 return;
193 }
194
195 m_active = active;
196
197 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
198 if (!active) {
199 color.setAlpha(150);
200 }
201
202 QWidget* viewport = m_container->viewport();
203 if (viewport) {
204 QPalette palette;
205 palette.setColor(viewport->backgroundRole(), color);
206 viewport->setPalette(palette);
207 }
208
209 update();
210
211 if (active) {
212 m_container->setFocus();
213 emit activated();
214 emit writeStateChanged(m_isFolderWritable);
215 }
216 }
217
218 bool DolphinView::isActive() const
219 {
220 return m_active;
221 }
222
223 void DolphinView::setMode(Mode mode)
224 {
225 if (mode != m_mode) {
226 ViewProperties props(url());
227 props.setViewMode(mode);
228 props.save();
229
230 applyViewProperties();
231 }
232 }
233
234 DolphinView::Mode DolphinView::mode() const
235 {
236 return m_mode;
237 }
238
239 void DolphinView::setPreviewsShown(bool show)
240 {
241 if (previewsShown() == show) {
242 return;
243 }
244
245 ViewProperties props(url());
246 props.setPreviewsShown(show);
247
248 m_container->setPreviewsShown(show);
249 emit previewsShownChanged(show);
250 }
251
252 bool DolphinView::previewsShown() const
253 {
254 return m_container->previewsShown();
255 }
256
257 void DolphinView::setHiddenFilesShown(bool show)
258 {
259 KFileItemModel* model = fileItemModel();
260 if (model->showHiddenFiles() == show) {
261 return;
262 }
263
264 const KFileItemList itemList = selectedItems();
265 m_selectedUrls.clear();
266 m_selectedUrls = itemList.urlList();
267
268 ViewProperties props(url());
269 props.setHiddenFilesShown(show);
270
271 model->setShowHiddenFiles(show);
272 emit hiddenFilesShownChanged(show);
273 }
274
275 bool DolphinView::hiddenFilesShown() const
276 {
277 return fileItemModel()->showHiddenFiles();
278 }
279
280 void DolphinView::setGroupedSorting(bool grouped)
281 {
282 if (grouped == groupedSorting()) {
283 return;
284 }
285
286 ViewProperties props(url());
287 props.setGroupedSorting(grouped);
288 props.save();
289
290 m_container->controller()->model()->setGroupedSorting(grouped);
291
292 emit groupedSortingChanged(grouped);
293 }
294
295 bool DolphinView::groupedSorting() const
296 {
297 return fileItemModel()->groupedSorting();
298 }
299
300 KFileItemList DolphinView::items() const
301 {
302 KFileItemList list;
303 const KFileItemModel* model = fileItemModel();
304 const int itemCount = model->count();
305 list.reserve(itemCount);
306
307 for (int i = 0; i < itemCount; ++i) {
308 list.append(model->fileItem(i));
309 }
310
311 return list;
312 }
313
314 int DolphinView::itemsCount() const
315 {
316 return fileItemModel()->count();
317 }
318
319 KFileItemList DolphinView::selectedItems() const
320 {
321 const KFileItemModel* model = fileItemModel();
322 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
323 const QSet<int> selectedIndexes = selectionManager->selectedItems();
324
325 KFileItemList selectedItems;
326 QSetIterator<int> it(selectedIndexes);
327 while (it.hasNext()) {
328 const int index = it.next();
329 selectedItems.append(model->fileItem(index));
330 }
331 return selectedItems;
332 }
333
334 int DolphinView::selectedItemsCount() const
335 {
336 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
337 return selectionManager->selectedItems().count();
338 }
339
340 void DolphinView::markUrlsAsSelected(const QList<KUrl>& urls)
341 {
342 m_selectedUrls = urls;
343 }
344
345 void DolphinView::markUrlAsCurrent(const KUrl& url)
346 {
347 m_currentItemUrl = url;
348 }
349
350 void DolphinView::selectItems(const QRegExp& pattern, bool enabled)
351 {
352 const KItemListSelectionManager::SelectionMode mode = enabled
353 ? KItemListSelectionManager::Select
354 : KItemListSelectionManager::Deselect;
355 const KFileItemModel* model = fileItemModel();
356 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
357
358 for (int index = 0; index < model->count(); index++) {
359 const KFileItem item = model->fileItem(index);
360 if (pattern.exactMatch(item.text())) {
361 // An alternative approach would be to store the matching items in a QSet<int> and
362 // select them in one go after the loop, but we'd need a new function
363 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
364 // for that.
365 selectionManager->setSelected(index, 1, mode);
366 }
367 }
368 }
369
370 void DolphinView::setZoomLevel(int level)
371 {
372 const int oldZoomLevel = zoomLevel();
373 m_container->setZoomLevel(level);
374 if (zoomLevel() != oldZoomLevel) {
375 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
376 }
377 }
378
379 int DolphinView::zoomLevel() const
380 {
381 return m_container->zoomLevel();
382 }
383
384 void DolphinView::setSortRole(const QByteArray& role)
385 {
386 if (role != sortRole()) {
387 updateSortRole(role);
388 }
389 }
390
391 QByteArray DolphinView::sortRole() const
392 {
393 const KItemModelBase* model = m_container->controller()->model();
394 return model->sortRole();
395 }
396
397 void DolphinView::setSortOrder(Qt::SortOrder order)
398 {
399 if (sortOrder() != order) {
400 updateSortOrder(order);
401 }
402 }
403
404 Qt::SortOrder DolphinView::sortOrder() const
405 {
406 KItemModelBase* model = fileItemModel();
407 return model->sortOrder();
408 }
409
410 void DolphinView::setSortFoldersFirst(bool foldersFirst)
411 {
412 if (sortFoldersFirst() != foldersFirst) {
413 updateSortFoldersFirst(foldersFirst);
414 }
415 }
416
417 bool DolphinView::sortFoldersFirst() const
418 {
419 KFileItemModel* model = fileItemModel();
420 return model->sortFoldersFirst();
421 }
422
423 void DolphinView::setVisibleRoles(const QList<QByteArray>& roles)
424 {
425 const QList<QByteArray> previousRoles = roles;
426
427 ViewProperties props(url());
428 props.setVisibleRoles(roles);
429
430 m_visibleRoles = roles;
431 m_container->setVisibleRoles(roles);
432
433 emit visibleRolesChanged(m_visibleRoles, previousRoles);
434 }
435
436 QList<QByteArray> DolphinView::visibleRoles() const
437 {
438 return m_visibleRoles;
439 }
440
441 void DolphinView::reload()
442 {
443 QByteArray viewState;
444 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
445 saveState(saveStream);
446
447 const KFileItemList itemList = selectedItems();
448 m_selectedUrls.clear();
449 m_selectedUrls = itemList.urlList();
450
451 setUrl(url());
452 loadDirectory(url(), true);
453
454 QDataStream restoreStream(viewState);
455 restoreState(restoreStream);
456 }
457
458 void DolphinView::stopLoading()
459 {
460 fileItemModel()->cancelDirLoading();
461 }
462
463 void DolphinView::readSettings()
464 {
465 const int oldZoomLevel = m_container->zoomLevel();
466
467 GeneralSettings::self()->readConfig();
468 m_container->readSettings();
469 applyViewProperties();
470
471 const int newZoomLevel = m_container->zoomLevel();
472 if (newZoomLevel != oldZoomLevel) {
473 emit zoomLevelChanged(newZoomLevel, oldZoomLevel);
474 }
475 }
476
477 void DolphinView::writeSettings()
478 {
479 GeneralSettings::self()->writeConfig();
480 m_container->writeSettings();
481 }
482
483 void DolphinView::setNameFilter(const QString& nameFilter)
484 {
485 fileItemModel()->setNameFilter(nameFilter);
486 }
487
488 QString DolphinView::nameFilter() const
489 {
490 return fileItemModel()->nameFilter();
491 }
492
493 QString DolphinView::statusBarText() const
494 {
495 QString summary;
496 QString foldersText;
497 QString filesText;
498
499 int folderCount = 0;
500 int fileCount = 0;
501 KIO::filesize_t totalFileSize = 0;
502
503 if (m_container->controller()->selectionManager()->hasSelection()) {
504 // Give a summary of the status of the selected files
505 const KFileItemList list = selectedItems();
506 foreach (const KFileItem& item, list) {
507 if (item.isDir()) {
508 ++folderCount;
509 } else {
510 ++fileCount;
511 totalFileSize += item.size();
512 }
513 }
514
515 if (folderCount + fileCount == 1) {
516 // If only one item is selected, show the filename
517 filesText = i18nc("@info:status", "<filename>%1</filename> selected", list.first().text());
518 } else {
519 // At least 2 items are selected
520 foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
521 filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
522 }
523 } else {
524 calculateItemCount(fileCount, folderCount, totalFileSize);
525 foldersText = i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount);
526 filesText = i18ncp("@info:status", "1 File", "%1 Files", fileCount);
527 }
528
529 if (fileCount > 0 && folderCount > 0) {
530 summary = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
531 foldersText, filesText,
532 KGlobal::locale()->formatByteSize(totalFileSize));
533 } else if (fileCount > 0) {
534 summary = i18nc("@info:status files (size)", "%1 (%2)",
535 filesText,
536 KGlobal::locale()->formatByteSize(totalFileSize));
537 } else if (folderCount > 0) {
538 summary = foldersText;
539 }
540
541 return summary;
542 }
543
544 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
545 {
546 QList<QAction*> actions;
547
548 if (items.isEmpty()) {
549 const KFileItem item = fileItemModel()->rootItem();
550 actions = m_versionControlObserver->actions(KFileItemList() << item);
551 } else {
552 actions = m_versionControlObserver->actions(items);
553 }
554
555 return actions;
556 }
557
558 void DolphinView::setUrl(const KUrl& url)
559 {
560 if (url == m_url) {
561 return;
562 }
563
564 emit urlAboutToBeChanged(url);
565 m_url = url;
566
567 hideToolTip();
568
569 // It is important to clear the items from the model before
570 // applying the view properties, otherwise expensive operations
571 // might be done on the existing items although they get cleared
572 // anyhow afterwards by loadDirectory().
573 fileItemModel()->clear();
574 applyViewProperties();
575 loadDirectory(url);
576
577 emit urlChanged(url);
578 }
579
580 void DolphinView::selectAll()
581 {
582 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
583 selectionManager->setSelected(0, fileItemModel()->count());
584 }
585
586 void DolphinView::invertSelection()
587 {
588 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
589 selectionManager->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle);
590 }
591
592 void DolphinView::clearSelection()
593 {
594 m_container->controller()->selectionManager()->clearSelection();
595 }
596
597 void DolphinView::renameSelectedItems()
598 {
599 KFileItemList items = selectedItems();
600 const int itemCount = items.count();
601 if (itemCount < 1) {
602 return;
603 }
604
605 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
606 // renaming yet.
607 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
608 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
609 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
610 m_viewAccessor.itemView()->edit(proxyIndex);
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(dirLoadingCompleted()), this, SLOT(slotDirLoadingCompleted()));
972 }
973
974 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
975 connect(current, SIGNAL(loadingCompleted()), this, SLOT(slotDirLoadingCompleted()));
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->setSortFoldersFirst(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()->restoreExpandedUrls(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()->expandedUrls();
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::slotRedirection(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::slotDirLoadingStarted()
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 dirLoadingStarted();
1236 }
1237
1238 void DolphinView::slotDirLoadingCompleted()
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 dirLoadingCompleted();
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 KFileItemModel* DolphinView::fileItemModel() const
1293 {
1294 return static_cast<KFileItemModel*>(m_container->controller()->model());
1295 }
1296
1297 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1298 {
1299 if (!url.isValid()) {
1300 const QString location(url.pathOrUrl());
1301 if (location.isEmpty()) {
1302 emit errorMessage(i18nc("@info:status", "The location is empty."));
1303 } else {
1304 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1305 }
1306 return;
1307 }
1308
1309 KFileItemModel* model = fileItemModel();
1310 if (reload) {
1311 model->refreshDir(url);
1312 } else {
1313 model->loadDir(url);
1314 }
1315 }
1316
1317 void DolphinView::applyViewProperties()
1318 {
1319 m_container->beginTransaction();
1320
1321 const ViewProperties props(url());
1322 KFileItemModel* model = fileItemModel();
1323
1324 const Mode mode = props.viewMode();
1325 if (m_mode != mode) {
1326 const Mode previousMode = m_mode;
1327 m_mode = mode;
1328
1329 // Changing the mode might result in changing
1330 // the zoom level. Remember the old zoom level so
1331 // that zoomLevelChanged() can get emitted.
1332 const int oldZoomLevel = m_container->zoomLevel();
1333
1334 switch (m_mode) {
1335 case IconsView: m_container->setItemLayout(KFileItemListView::IconsLayout); break;
1336 case CompactView: m_container->setItemLayout(KFileItemListView::CompactLayout); break;
1337 case DetailsView: m_container->setItemLayout(KFileItemListView::DetailsLayout); break;
1338 default: Q_ASSERT(false); break;
1339 }
1340
1341 emit modeChanged(m_mode, previousMode);
1342
1343 if (m_container->zoomLevel() != oldZoomLevel) {
1344 emit zoomLevelChanged(m_container->zoomLevel(), oldZoomLevel);
1345 }
1346 }
1347
1348 const bool hiddenFilesShown = props.hiddenFilesShown();
1349 if (hiddenFilesShown != model->showHiddenFiles()) {
1350 model->setShowHiddenFiles(hiddenFilesShown);
1351 emit hiddenFilesShownChanged(hiddenFilesShown);
1352 }
1353
1354 const bool groupedSorting = props.groupedSorting();
1355 if (groupedSorting != model->groupedSorting()) {
1356 model->setGroupedSorting(groupedSorting);
1357 emit groupedSortingChanged(groupedSorting);
1358 }
1359
1360 const QByteArray sortRole = props.sortRole();
1361 if (sortRole != model->sortRole()) {
1362 model->setSortRole(sortRole);
1363 emit sortRoleChanged(sortRole);
1364 }
1365
1366 const Qt::SortOrder sortOrder = props.sortOrder();
1367 if (sortOrder != model->sortOrder()) {
1368 model->setSortOrder(sortOrder);
1369 emit sortOrderChanged(sortOrder);
1370 }
1371
1372 const bool sortFoldersFirst = props.sortFoldersFirst();
1373 if (sortFoldersFirst != model->sortFoldersFirst()) {
1374 model->setSortFoldersFirst(sortFoldersFirst);
1375 emit sortFoldersFirstChanged(sortFoldersFirst);
1376 }
1377
1378 const QList<QByteArray> visibleRoles = props.visibleRoles();
1379 if (visibleRoles != m_visibleRoles) {
1380 const QList<QByteArray> previousVisibleRoles = m_visibleRoles;
1381 m_visibleRoles = visibleRoles;
1382 m_container->setVisibleRoles(visibleRoles);
1383 emit visibleRolesChanged(m_visibleRoles, previousVisibleRoles);
1384 }
1385
1386 const bool previewsShown = props.previewsShown();
1387 if (previewsShown != m_container->previewsShown()) {
1388 const int oldZoomLevel = zoomLevel();
1389
1390 m_container->setPreviewsShown(previewsShown);
1391 emit previewsShownChanged(previewsShown);
1392
1393 // Changing the preview-state might result in a changed zoom-level
1394 if (oldZoomLevel != zoomLevel()) {
1395 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
1396 }
1397 }
1398
1399 KItemListView* itemListView = m_container->controller()->view();
1400 if (itemListView->isHeaderVisible()) {
1401 KItemListHeader* header = itemListView->header();
1402 const QList<int> headerColumnWidths = props.headerColumnWidths();
1403 const int rolesCount = m_visibleRoles.count();
1404 if (headerColumnWidths.count() == rolesCount) {
1405 header->setAutomaticColumnResizing(false);
1406
1407 QHash<QByteArray, qreal> columnWidths;
1408 for (int i = 0; i < rolesCount; ++i) {
1409 columnWidths.insert(m_visibleRoles[i], headerColumnWidths[i]);
1410 }
1411 header->setColumnWidths(columnWidths);
1412 } else {
1413 header->setAutomaticColumnResizing(true);
1414 }
1415 }
1416
1417 m_container->endTransaction();
1418 }
1419
1420 void DolphinView::pasteToUrl(const KUrl& url)
1421 {
1422 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1423 KonqOperations::doPaste(this, url);
1424 }
1425
1426 KUrl::List DolphinView::simplifiedSelectedUrls() const
1427 {
1428 KUrl::List urls;
1429
1430 const KFileItemList items = selectedItems();
1431 foreach (const KFileItem &item, items) {
1432 urls.append(item.url());
1433 }
1434
1435 if (itemsExpandable()) {
1436 // TODO: Check if we still need KDirModel for this in KDE 5.0
1437 urls = KDirModel::simplifiedUrlList(urls);
1438 }
1439
1440 return urls;
1441 }
1442
1443 QMimeData* DolphinView::selectionMimeData() const
1444 {
1445 const KFileItemModel* model = fileItemModel();
1446 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1447 const QSet<int> selectedIndexes = selectionManager->selectedItems();
1448
1449 return model->createMimeData(selectedIndexes);
1450 }
1451
1452 void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
1453 {
1454 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1455 KUrl::List destUrls;
1456 foreach (const KUrl& source, sourceUrls) {
1457 KUrl destination(url().url() + "/" + source.fileName());
1458 destUrls << destination;
1459 }
1460 markUrlsAsSelected(destUrls);
1461 }
1462
1463 void DolphinView::updateWritableState()
1464 {
1465 const bool wasFolderWritable = m_isFolderWritable;
1466 m_isFolderWritable = true;
1467
1468 const KFileItem item = fileItemModel()->rootItem();
1469 if (!item.isNull()) {
1470 KFileItemListProperties capabilities(KFileItemList() << item);
1471 m_isFolderWritable = capabilities.supportsWriting();
1472 }
1473 if (m_isFolderWritable != wasFolderWritable) {
1474 emit writeStateChanged(m_isFolderWritable);
1475 }
1476 }
1477
1478 #include "dolphinview.moc"