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