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