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