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