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