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