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