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