X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/854b0acd1a259fab40e42c8470bb144c955dcc5a..64afe7b22622f79b34aafd54501b08120ab2fc5c:/src/views/dolphinview.cpp diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index 455905f6a..63b53f2e2 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -20,6 +20,8 @@ #include "dolphinview.h" +#include + #include #include #include @@ -31,9 +33,10 @@ #include #include +#include +#include #include #include -#include #include #include #include @@ -41,10 +44,13 @@ #include #include #include +#include +#include #include #include #include #include +#include #include #include #include @@ -56,12 +62,10 @@ #include #include -#include "additionalinfoaccessor.h" -#include "dolphindirlister.h" #include "dolphinnewfilemenuobserver.h" #include "dolphin_detailsmodesettings.h" #include "dolphin_generalsettings.h" -#include "dolphinitemlistcontainer.h" +#include "dolphinitemlistview.h" #include "draganddrophelper.h" #include "renamedialog.h" #include "versioncontrol/versioncontrolobserver.h" @@ -70,9 +74,12 @@ #include "views/tooltips/tooltipmanager.h" #include "zoomlevelinfo.h" +#ifdef HAVE_BALOO + #include +#endif + namespace { const int MaxModeEnum = DolphinView::CompactView; - const int MaxSortingEnum = DolphinView::SortByPath; }; DolphinView::DolphinView(const KUrl& url, QWidget* parent) : @@ -81,18 +88,23 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) : m_tabsForFiles(false), m_assureVisibleCurrentIndex(false), m_isFolderWritable(true), + m_dragging(false), m_url(url), + m_viewPropertiesContext(), m_mode(DolphinView::IconsView), - m_additionalInfoList(), + m_visibleRoles(), m_topLayout(0), - m_dirLister(0), + m_model(0), + m_view(0), m_container(0), m_toolTipManager(0), m_selectionChangedTimer(0), m_currentItemUrl(), + m_scrollToCurrentItem(false), m_restoredContentsPosition(), - m_createdItemUrl(), m_selectedUrls(), + m_clearSelectionBeforeSelectingNewItems(false), + m_markFirstNewlySelectedItemAsCurrent(false), m_versionControlObserver(0) { m_topLayout = new QVBoxLayout(this); @@ -111,57 +123,74 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) : connect(m_selectionChangedTimer, SIGNAL(timeout()), this, SLOT(emitSelectionChangedSignal())); - m_dirLister = new DolphinDirLister(this); - m_dirLister->setAutoUpdate(true); - m_dirLister->setDelayedMimeTypes(true); - - connect(m_dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(slotRedirection(KUrl,KUrl))); - connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotDirListerStarted(KUrl))); - connect(m_dirLister, SIGNAL(refreshItems(QList >)), - this, SLOT(slotRefreshItems())); - - connect(m_dirLister, SIGNAL(clear()), this, SIGNAL(itemCountChanged())); - connect(m_dirLister, SIGNAL(newItems(KFileItemList)), this, SIGNAL(itemCountChanged())); - connect(m_dirLister, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString))); - connect(m_dirLister, SIGNAL(errorMessage(QString)), this, SIGNAL(infoMessage(QString))); - connect(m_dirLister, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int))); - connect(m_dirLister, SIGNAL(urlIsFileError(KUrl)), this, SIGNAL(urlIsFileError(KUrl))); - connect(m_dirLister, SIGNAL(itemsDeleted(KFileItemList)), this, SIGNAL(itemCountChanged())); - - m_container = new DolphinItemListContainer(m_dirLister, this); - m_container->setVisibleRoles(QList() << "name"); + m_model = new KFileItemModel(this); + m_view = new DolphinItemListView(); + m_view->setEnabledSelectionToggles(GeneralSettings::showSelectionToggle()); + m_view->setVisibleRoles(QList() << "text"); + applyModeToView(); + + KItemListController* controller = new KItemListController(m_model, m_view, this); + const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1; + controller->setAutoActivationDelay(delay); + + // The EnlargeSmallPreviews setting can only be changed after the model + // has been set in the view by KItemListController. + m_view->setEnlargeSmallPreviews(GeneralSettings::enlargeSmallPreviews()); + + m_container = new KItemListContainer(controller, this); m_container->installEventFilter(this); setFocusProxy(m_container); + connect(m_container->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip())); + connect(m_container->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip())); - KItemListController* controller = m_container->controller(); controller->setSelectionBehavior(KItemListController::MultiSelection); - if (GeneralSettings::autoExpandFolders()) { - controller->setAutoActivationDelay(750); - } connect(controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int))); - connect(controller, SIGNAL(itemsActivated(QSet)), this, SLOT(slotItemsActivated(QSet))); + connect(controller, SIGNAL(itemsActivated(KItemSet)), this, SLOT(slotItemsActivated(KItemSet))); connect(controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int))); connect(controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF))); connect(controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF))); connect(controller, SIGNAL(headerContextMenuRequested(QPointF)), this, SLOT(slotHeaderContextMenuRequested(QPointF))); + connect(controller, SIGNAL(mouseButtonPressed(int,Qt::MouseButtons)), this, SLOT(slotMouseButtonPressed(int,Qt::MouseButtons))); connect(controller, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int))); connect(controller, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int))); connect(controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*))); + connect(controller, SIGNAL(escapePressed()), this, SLOT(stopLoading())); connect(controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)), this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*))); - KFileItemModel* model = fileItemModel(); - if (model) { - connect(model, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted())); - } + connect(m_model, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted())); + connect(m_model, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted())); + connect(m_model, SIGNAL(directoryLoadingCanceled()), this, SIGNAL(directoryLoadingCanceled())); + connect(m_model, SIGNAL(directoryLoadingProgress(int)), this, SIGNAL(directoryLoadingProgress(int))); + connect(m_model, SIGNAL(directorySortingProgress(int)), this, SIGNAL(directorySortingProgress(int))); + connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet)), + this, SLOT(slotItemsChanged())); + connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)), this, SIGNAL(itemCountChanged())); + connect(m_model, SIGNAL(itemsInserted(KItemRangeList)), this, SIGNAL(itemCountChanged())); + connect(m_model, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString))); + connect(m_model, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString))); + connect(m_model, SIGNAL(directoryRedirection(KUrl,KUrl)), this, SLOT(slotDirectoryRedirection(KUrl,KUrl))); + connect(m_model, SIGNAL(urlIsFileError(KUrl)), this, SIGNAL(urlIsFileError(KUrl))); + + m_view->installEventFilter(this); + connect(m_view, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)), + this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder,Qt::SortOrder))); + connect(m_view, SIGNAL(sortRoleChanged(QByteArray,QByteArray)), + this, SLOT(slotSortRoleChangedByHeader(QByteArray,QByteArray))); + connect(m_view, SIGNAL(visibleRolesChanged(QList,QList)), + this, SLOT(slotVisibleRolesChangedByHeader(QList,QList))); + connect(m_view, SIGNAL(roleEditingCanceled(int,QByteArray,QVariant)), + this, SLOT(slotRoleEditingCanceled())); + connect(m_view->header(), SIGNAL(columnWidthChanged(QByteArray,qreal,qreal)), + this, SLOT(slotHeaderColumnWidthChanged(QByteArray,qreal,qreal))); KItemListSelectionManager* selectionManager = controller->selectionManager(); - connect(selectionManager, SIGNAL(selectionChanged(QSet,QSet)), - this, SLOT(slotSelectionChanged(QSet,QSet))); + connect(selectionManager, SIGNAL(selectionChanged(KItemSet,KItemSet)), + this, SLOT(slotSelectionChanged(KItemSet,KItemSet))); m_toolTipManager = new ToolTipManager(this); m_versionControlObserver = new VersionControlObserver(this); - m_versionControlObserver->setModel(model); + m_versionControlObserver->setModel(m_model); connect(m_versionControlObserver, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString))); connect(m_versionControlObserver, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString))); connect(m_versionControlObserver, SIGNAL(operationCompletedMessage(QString)), this, SIGNAL(operationCompletedMessage(QString))); @@ -218,11 +247,14 @@ bool DolphinView::isActive() const void DolphinView::setMode(Mode mode) { if (mode != m_mode) { - ViewProperties props(url()); + ViewProperties props(viewPropertiesUrl()); props.setViewMode(mode); - props.save(); - applyViewProperties(); + // We pass the new ViewProperties to applyViewProperties, rather than + // storing them on disk and letting applyViewProperties() read them + // from there, to prevent that changing the view mode fails if the + // .directory file is not writable (see bug 318534). + applyViewProperties(props); } } @@ -237,21 +269,27 @@ void DolphinView::setPreviewsShown(bool show) return; } - ViewProperties props(url()); + ViewProperties props(viewPropertiesUrl()); props.setPreviewsShown(show); - m_container->setPreviewsShown(show); + const int oldZoomLevel = m_view->zoomLevel(); + m_view->setPreviewsShown(show); emit previewsShownChanged(show); + + const int newZoomLevel = m_view->zoomLevel(); + if (newZoomLevel != oldZoomLevel) { + emit zoomLevelChanged(newZoomLevel, oldZoomLevel); + } } bool DolphinView::previewsShown() const { - return m_container->previewsShown(); + return m_view->previewsShown(); } void DolphinView::setHiddenFilesShown(bool show) { - if (m_dirLister->showingDotFiles() == show) { + if (m_model->showHiddenFiles() == show) { return; } @@ -259,17 +297,16 @@ void DolphinView::setHiddenFilesShown(bool show) m_selectedUrls.clear(); m_selectedUrls = itemList.urlList(); - ViewProperties props(url()); + ViewProperties props(viewPropertiesUrl()); props.setHiddenFilesShown(show); - m_dirLister->setShowingDotFiles(show); - m_dirLister->emitChanges(); + m_model->setShowHiddenFiles(show); emit hiddenFilesShownChanged(show); } bool DolphinView::hiddenFilesShown() const { - return m_dirLister->showingDotFiles(); + return m_model->showHiddenFiles(); } void DolphinView::setGroupedSorting(bool grouped) @@ -278,7 +315,7 @@ void DolphinView::setGroupedSorting(bool grouped) return; } - ViewProperties props(url()); + ViewProperties props(viewPropertiesUrl()); props.setGroupedSorting(grouped); props.save(); @@ -289,28 +326,34 @@ void DolphinView::setGroupedSorting(bool grouped) bool DolphinView::groupedSorting() const { - return fileItemModel()->groupedSorting(); + return m_model->groupedSorting(); } KFileItemList DolphinView::items() const { - return m_dirLister->items(); + KFileItemList list; + const int itemCount = m_model->count(); + list.reserve(itemCount); + + for (int i = 0; i < itemCount; ++i) { + list.append(m_model->fileItem(i)); + } + + return list; +} + +int DolphinView::itemsCount() const +{ + return m_model->count(); } KFileItemList DolphinView::selectedItems() const { - const KFileItemModel* model = fileItemModel(); const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager(); - const QSet selectedIndexes = selectionManager->selectedItems(); - - QList sortedIndexes = selectedIndexes.toList(); - qSort(sortedIndexes); KFileItemList selectedItems; - QListIterator it(sortedIndexes); - while (it.hasNext()) { - const int index = it.next(); - selectedItems.append(model->fileItem(index)); + foreach (int index, selectionManager->selectedItems()) { + selectedItems.append(m_model->fileItem(index)); } return selectedItems; } @@ -329,22 +372,22 @@ void DolphinView::markUrlsAsSelected(const QList& urls) void DolphinView::markUrlAsCurrent(const KUrl& url) { m_currentItemUrl = url; + m_scrollToCurrentItem = true; } -void DolphinView::setItemSelectionEnabled(const QRegExp& pattern, bool enabled) +void DolphinView::selectItems(const QRegExp& pattern, bool enabled) { const KItemListSelectionManager::SelectionMode mode = enabled ? KItemListSelectionManager::Select : KItemListSelectionManager::Deselect; - const KFileItemModel* model = fileItemModel(); KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager(); - for (int index = 0; index < model->count(); index++) { - const KFileItem item = model->fileItem(index); + for (int index = 0; index < m_model->count(); index++) { + const KFileItem item = m_model->fileItem(index); if (pattern.exactMatch(item.text())) { - // An alternative approach would be to store the matching items in a QSet and + // An alternative approach would be to store the matching items in a KItemSet and // select them in one go after the loop, but we'd need a new function - // KItemListSelectionManager::setSelected(QSet, SelectionMode mode) + // KItemListSelectionManager::setSelected(KItemSet, SelectionMode mode) // for that. selectionManager->setSelected(index, 1, mode); } @@ -354,28 +397,29 @@ void DolphinView::setItemSelectionEnabled(const QRegExp& pattern, bool enabled) void DolphinView::setZoomLevel(int level) { const int oldZoomLevel = zoomLevel(); - m_container->setZoomLevel(level); + m_view->setZoomLevel(level); if (zoomLevel() != oldZoomLevel) { + hideToolTip(); emit zoomLevelChanged(zoomLevel(), oldZoomLevel); } } int DolphinView::zoomLevel() const { - return m_container->zoomLevel(); + return m_view->zoomLevel(); } -void DolphinView::setSorting(Sorting sorting) +void DolphinView::setSortRole(const QByteArray& role) { - if (sorting != this->sorting()) { - updateSorting(sorting); + if (role != sortRole()) { + updateSortRole(role); } } -DolphinView::Sorting DolphinView::sorting() const +QByteArray DolphinView::sortRole() const { - KItemModelBase* model = m_container->controller()->model(); - return sortingForSortRole(model->sortRole()); + const KItemModelBase* model = m_container->controller()->model(); + return model->sortRole(); } void DolphinView::setSortOrder(Qt::SortOrder order) @@ -387,8 +431,7 @@ void DolphinView::setSortOrder(Qt::SortOrder order) Qt::SortOrder DolphinView::sortOrder() const { - KItemModelBase* model = fileItemModel(); - return model->sortOrder(); + return m_model->sortOrder(); } void DolphinView::setSortFoldersFirst(bool foldersFirst) @@ -400,26 +443,25 @@ void DolphinView::setSortFoldersFirst(bool foldersFirst) bool DolphinView::sortFoldersFirst() const { - KFileItemModel* model = fileItemModel(); - return model->sortFoldersFirst(); + return m_model->sortDirectoriesFirst(); } -void DolphinView::setAdditionalInfoList(const QList& info) +void DolphinView::setVisibleRoles(const QList& roles) { - const QList previousList = info; + const QList previousRoles = roles; - ViewProperties props(url()); - props.setAdditionalInfoList(info); + ViewProperties props(viewPropertiesUrl()); + props.setVisibleRoles(roles); - m_additionalInfoList = info; - applyAdditionalInfoListToView(); + m_visibleRoles = roles; + m_view->setVisibleRoles(roles); - emit additionalInfoListChanged(m_additionalInfoList, previousList); + emit visibleRolesChanged(m_visibleRoles, previousRoles); } -QList DolphinView::additionalInfoList() const +QList DolphinView::visibleRoles() const { - return m_additionalInfoList; + return m_visibleRoles; } void DolphinView::reload() @@ -439,46 +481,47 @@ void DolphinView::reload() restoreState(restoreStream); } -void DolphinView::stopLoading() -{ - m_dirLister->stop(); -} - -void DolphinView::refresh() +void DolphinView::readSettings() { - const bool oldActivationState = m_active; - const int oldZoomLevel = zoomLevel(); - m_active = true; + const int oldZoomLevel = m_view->zoomLevel(); + GeneralSettings::self()->readConfig(); + m_view->readSettings(); applyViewProperties(); - reload(); - setActive(oldActivationState); - updateZoomLevel(oldZoomLevel); + const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1; + m_container->controller()->setAutoActivationDelay(delay); + + const int newZoomLevel = m_view->zoomLevel(); + if (newZoomLevel != oldZoomLevel) { + emit zoomLevelChanged(newZoomLevel, oldZoomLevel); + } +} + +void DolphinView::writeSettings() +{ + GeneralSettings::self()->writeConfig(); + m_view->writeSettings(); } void DolphinView::setNameFilter(const QString& nameFilter) { - fileItemModel()->setNameFilter(nameFilter); + m_model->setNameFilter(nameFilter); } QString DolphinView::nameFilter() const { - return fileItemModel()->nameFilter(); + return m_model->nameFilter(); } -void DolphinView::calculateItemCount(int& fileCount, - int& folderCount, - KIO::filesize_t& totalFileSize) const +void DolphinView::setMimeTypeFilters(const QStringList& filters) { - foreach (const KFileItem& item, m_dirLister->items()) { - if (item.isDir()) { - ++folderCount; - } else { - ++fileCount; - totalFileSize += item.size(); - } - } + return m_model->setMimeTypeFilters(filters); +} + +QStringList DolphinView::mimeTypeFilters() const +{ + return m_model->mimeTypeFilters(); } QString DolphinView::statusBarText() const @@ -491,7 +534,7 @@ QString DolphinView::statusBarText() const int fileCount = 0; KIO::filesize_t totalFileSize = 0; - if (hasSelection()) { + if (m_container->controller()->selectionManager()->hasSelection()) { // Give a summary of the status of the selected files const KFileItemList list = selectedItems(); foreach (const KFileItem& item, list) { @@ -504,8 +547,8 @@ QString DolphinView::statusBarText() const } if (folderCount + fileCount == 1) { - // If only one item is selected, show the filename - filesText = i18nc("@info:status", "%1 selected", list.first().text()); + // If only one item is selected, show info about it + return list.first().getStatusBarInfo(); } else { // At least 2 items are selected foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount); @@ -519,11 +562,16 @@ QString DolphinView::statusBarText() const if (fileCount > 0 && folderCount > 0) { summary = i18nc("@info:status folders, files (size)", "%1, %2 (%3)", - foldersText, filesText, fileSizeText(totalFileSize)); + foldersText, filesText, + KGlobal::locale()->formatByteSize(totalFileSize)); } else if (fileCount > 0) { - summary = i18nc("@info:status files (size)", "%1 (%2)", filesText, fileSizeText(totalFileSize)); + summary = i18nc("@info:status files (size)", "%1 (%2)", + filesText, + KGlobal::locale()->formatByteSize(totalFileSize)); } else if (folderCount > 0) { summary = foldersText; + } else { + summary = i18nc("@info:status", "0 Folders, 0 Files"); } return summary; @@ -534,8 +582,10 @@ QList DolphinView::versionControlActions(const KFileItemList& items) c QList actions; if (items.isEmpty()) { - const KFileItem item = fileItemModel()->rootItem(); - actions = m_versionControlObserver->actions(KFileItemList() << item); + const KFileItem item = m_model->rootItem(); + if (!item.isNull()) { + actions = m_versionControlObserver->actions(KFileItemList() << item); + } } else { actions = m_versionControlObserver->actions(items); } @@ -549,18 +599,21 @@ void DolphinView::setUrl(const KUrl& url) return; } + clearSelection(); + emit urlAboutToBeChanged(url); m_url = url; - if (GeneralSettings::showToolTips()) { - m_toolTipManager->hideToolTip(); - } + hideToolTip(); + + disconnect(m_view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)), + this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant))); // It is important to clear the items from the model before // applying the view properties, otherwise expensive operations // might be done on the existing items although they get cleared // anyhow afterwards by loadDirectory(). - fileItemModel()->clear(); + m_model->clear(); applyViewProperties(); loadDirectory(url); @@ -570,44 +623,47 @@ void DolphinView::setUrl(const KUrl& url) void DolphinView::selectAll() { KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager(); - selectionManager->setSelected(0, fileItemModel()->count()); + selectionManager->setSelected(0, m_model->count()); } void DolphinView::invertSelection() { KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager(); - selectionManager->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle); + selectionManager->setSelected(0, m_model->count(), KItemListSelectionManager::Toggle); } void DolphinView::clearSelection() { + m_selectedUrls.clear(); m_container->controller()->selectionManager()->clearSelection(); } void DolphinView::renameSelectedItems() { - KFileItemList items = selectedItems(); - const int itemCount = items.count(); - if (itemCount < 1) { + const KFileItemList items = selectedItems(); + if (items.isEmpty()) { return; } - // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline - // renaming yet. - /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) { - const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first()); - const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex); - m_viewAccessor.itemView()->edit(proxyIndex); - } else {*/ + if (items.count() == 1 && GeneralSettings::renameInline()) { + const int index = m_model->index(items.first()); + m_view->editRole(index, "text"); + + hideToolTip(); + + connect(m_view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)), + this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant))); + } else { RenameDialog* dialog = new RenameDialog(this, items); dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->show(); dialog->raise(); dialog->activateWindow(); - //} + } - // assure that the current index remains visible when KDirLister - // will notify the view about changed items + // Assure that the current index remains visible when KFileItemModel + // will notify the view about changed items (which might result in + // a changed sorting). m_assureVisibleCurrentIndex = true; } @@ -627,6 +683,9 @@ void DolphinView::deleteSelectedItems() if (del) { KIO::Job* job = KIO::del(list); + if (job->ui()) { + job->ui()->setWindow(this); + } connect(job, SIGNAL(result(KJob*)), this, SLOT(slotDeleteFileFinished(KJob*))); } @@ -658,6 +717,11 @@ void DolphinView::pasteIntoFolder() } } +void DolphinView::stopLoading() +{ + m_model->cancelDirectoryLoading(); +} + bool DolphinView::eventFilter(QObject* watched, QEvent* event) { switch (event->type()) { @@ -667,6 +731,22 @@ bool DolphinView::eventFilter(QObject* watched, QEvent* event) } break; + case QEvent::GraphicsSceneDragEnter: + if (watched == m_view) { + m_dragging = true; + } + break; + + case QEvent::GraphicsSceneDragLeave: + if (watched == m_view) { + m_dragging = false; + } + break; + + case QEvent::GraphicsSceneDrop: + if (watched == m_view) { + m_dragging = false; + } default: break; } @@ -687,6 +767,26 @@ void DolphinView::wheelEvent(QWheelEvent* event) } } +void DolphinView::hideEvent(QHideEvent* event) +{ + hideToolTip(); + QWidget::hideEvent(event); +} + +bool DolphinView::event(QEvent* event) +{ + /* See Bug 297355 + * Dolphin leaves file preview tooltips open even when is not visible. + * + * Hide tool-tip when Dolphin loses focus. + */ + if (event->type() == QEvent::WindowDeactivate) { + hideToolTip(); + } + + return QWidget::event(event); +} + void DolphinView::activate() { setActive(true); @@ -694,115 +794,213 @@ void DolphinView::activate() void DolphinView::slotItemActivated(int index) { - const KFileItem item = fileItemModel()->fileItem(index); + const KFileItem item = m_model->fileItem(index); if (!item.isNull()) { emit itemActivated(item); } } -void DolphinView::slotItemsActivated(const QSet& indexes) +void DolphinView::slotItemsActivated(const KItemSet& indexes) { Q_ASSERT(indexes.count() >= 2); + if (indexes.count() > 5) { + QString question = i18np("Are you sure you want to open 1 item?", "Are you sure you want to open %1 items?", indexes.count()); + const int answer = KMessageBox::warningYesNo(this, question); + if (answer != KMessageBox::Yes) { + return; + } + } + KFileItemList items; + items.reserve(indexes.count()); - KFileItemModel* model = fileItemModel(); - QSetIterator it(indexes); - while (it.hasNext()) { - const int index = it.next(); - items.append(model->fileItem(index)); - } + foreach (int index, indexes) { + KFileItem item = m_model->fileItem(index); + const KUrl& url = openItemAsFolderUrl(item); - foreach (const KFileItem& item, items) { - if (item.isDir()) { - emit tabRequested(item.url()); + if (!url.isEmpty()) { // Open folders in new tabs + emit tabRequested(url); } else { - emit itemActivated(item); + items.append(item); } } + + if (items.count() == 1) { + emit itemActivated(items.first()); + } else if (items.count() > 1) { + emit itemsActivated(items); + } } void DolphinView::slotItemMiddleClicked(int index) { - const KFileItem item = fileItemModel()->fileItem(index); - if (item.isDir() || isTabsForFilesEnabled()) { + const KFileItem& item = m_model->fileItem(index); + const KUrl& url = openItemAsFolderUrl(item); + if (!url.isEmpty()) { + emit tabRequested(url); + } else if (isTabsForFilesEnabled()) { emit tabRequested(item.url()); } } void DolphinView::slotItemContextMenuRequested(int index, const QPointF& pos) { - if (GeneralSettings::showToolTips()) { - m_toolTipManager->hideToolTip(); + // Force emit of a selection changed signal before we request the + // context menu, to update the edit-actions first. (See Bug 294013) + if (m_selectionChangedTimer->isActive()) { + emitSelectionChangedSignal(); } - const KFileItem item = fileItemModel()->fileItem(index); + + const KFileItem item = m_model->fileItem(index); emit requestContextMenu(pos.toPoint(), item, url(), QList()); } void DolphinView::slotViewContextMenuRequested(const QPointF& pos) { - if (GeneralSettings::showToolTips()) { - m_toolTipManager->hideToolTip(); - } emit requestContextMenu(pos.toPoint(), KFileItem(), url(), QList()); } void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos) { - QWeakPointer menu = new KMenu(QApplication::activeWindow()); + ViewProperties props(viewPropertiesUrl()); + + QPointer menu = new KMenu(QApplication::activeWindow()); KItemListView* view = m_container->controller()->view(); const QSet visibleRolesSet = view->visibleRoles().toSet(); + bool indexingEnabled = false; +#ifdef HAVE_BALOO + Baloo::IndexerConfig config; + indexingEnabled = config.fileIndexingEnabled(); +#endif + + QString groupName; + QMenu* groupMenu = 0; + // Add all roles to the menu that can be shown or hidden by the user - const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance(); - const QList keys = infoAccessor.keys(); - foreach (const DolphinView::AdditionalInfo info, keys) { - const QByteArray& role = infoAccessor.role(info); - if (role != "name") { - const QString text = fileItemModel()->roleDescription(role); - - QAction* action = menu.data()->addAction(text); - action->setCheckable(true); - action->setChecked(visibleRolesSet.contains(role)); - action->setData(info); + const QList rolesInfo = KFileItemModel::rolesInformation(); + foreach (const KFileItemModel::RoleInfo& info, rolesInfo) { + if (info.role == "text") { + // It should not be possible to hide the "text" role + continue; } - } - - QAction* action = menu.data()->exec(pos.toPoint()); - if (action) { - // Show or hide the selected role - const DolphinView::AdditionalInfo info = - static_cast(action->data().toInt()); - ViewProperties props(url()); - QList infoList = props.additionalInfoList(); + const QString text = m_model->roleDescription(info.role); + QAction* action = 0; + if (info.group.isEmpty()) { + action = menu->addAction(text); + } else { + if (!groupMenu || info.group != groupName) { + groupName = info.group; + groupMenu = menu->addMenu(groupName); + } - const QByteArray selectedRole = infoAccessor.role(info); - QList visibleRoles = view->visibleRoles(); + action = groupMenu->addAction(text); + } - if (action->isChecked()) { - const int index = keys.indexOf(info) + 1; - visibleRoles.insert(index, selectedRole); - infoList.insert(index, info); + action->setCheckable(true); + action->setChecked(visibleRolesSet.contains(info.role)); + action->setData(info.role); + + const bool enable = (!info.requiresBaloo && !info.requiresIndexer) || + (info.requiresBaloo) || + (info.requiresIndexer && indexingEnabled); + action->setEnabled(enable); + } + + menu->addSeparator(); + + QActionGroup* widthsGroup = new QActionGroup(menu); + const bool autoColumnWidths = props.headerColumnWidths().isEmpty(); + + QAction* autoAdjustWidthsAction = menu->addAction(i18nc("@action:inmenu", "Automatic Column Widths")); + autoAdjustWidthsAction->setCheckable(true); + autoAdjustWidthsAction->setChecked(autoColumnWidths); + autoAdjustWidthsAction->setActionGroup(widthsGroup); + + QAction* customWidthsAction = menu->addAction(i18nc("@action:inmenu", "Custom Column Widths")); + customWidthsAction->setCheckable(true); + customWidthsAction->setChecked(!autoColumnWidths); + customWidthsAction->setActionGroup(widthsGroup); + + QAction* action = menu->exec(pos.toPoint()); + if (menu && action) { + KItemListHeader* header = view->header(); + + if (action == autoAdjustWidthsAction) { + // Clear the column-widths from the viewproperties and turn on + // the automatic resizing of the columns + props.setHeaderColumnWidths(QList()); + header->setAutomaticColumnResizing(true); + } else if (action == customWidthsAction) { + // Apply the current column-widths as custom column-widths and turn + // off the automatic resizing of the columns + QList columnWidths; + foreach (const QByteArray& role, view->visibleRoles()) { + columnWidths.append(header->columnWidth(role)); + } + props.setHeaderColumnWidths(columnWidths); + header->setAutomaticColumnResizing(false); } else { - visibleRoles.removeOne(selectedRole); - infoList.removeOne(info); + // Show or hide the selected role + const QByteArray selectedRole = action->data().toByteArray(); + + QList visibleRoles = view->visibleRoles(); + if (action->isChecked()) { + visibleRoles.append(selectedRole); + } else { + visibleRoles.removeOne(selectedRole); + } + + view->setVisibleRoles(visibleRoles); + props.setVisibleRoles(visibleRoles); + + QList columnWidths; + if (!header->automaticColumnResizing()) { + foreach (const QByteArray& role, view->visibleRoles()) { + columnWidths.append(header->columnWidth(role)); + } + } + props.setHeaderColumnWidths(columnWidths); } + } + + delete menu; +} + +void DolphinView::slotHeaderColumnWidthChanged(const QByteArray& role, qreal current, qreal previous) +{ + Q_UNUSED(previous); + + const QList visibleRoles = m_view->visibleRoles(); - view->setVisibleRoles(visibleRoles); - props.setAdditionalInfoList(infoList); + ViewProperties props(viewPropertiesUrl()); + QList columnWidths = props.headerColumnWidths(); + if (columnWidths.count() != visibleRoles.count()) { + columnWidths.clear(); + columnWidths.reserve(visibleRoles.count()); + const KItemListHeader* header = m_view->header(); + foreach (const QByteArray& role, visibleRoles) { + const int width = header->columnWidth(role); + columnWidths.append(width); + } } - delete menu.data(); + const int roleIndex = visibleRoles.indexOf(role); + Q_ASSERT(roleIndex >= 0 && roleIndex < columnWidths.count()); + columnWidths[roleIndex] = current; + + props.setHeaderColumnWidths(columnWidths); } void DolphinView::slotItemHovered(int index) { - const KFileItem item = fileItemModel()->fileItem(index); + const KFileItem item = m_model->fileItem(index); - if (GeneralSettings::showToolTips()) { - QRectF itemRect = m_container->controller()->view()->itemRect(index); + if (GeneralSettings::showToolTips() && !m_dragging) { + QRectF itemRect = m_container->controller()->view()->itemContextRect(index); const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint()); itemRect.moveTo(pos); @@ -815,15 +1013,23 @@ void DolphinView::slotItemHovered(int index) void DolphinView::slotItemUnhovered(int index) { Q_UNUSED(index); - if (GeneralSettings::showToolTips()) { - m_toolTipManager->hideToolTip(); - } + hideToolTip(); emit requestItemInfo(KFileItem()); } void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event) { - const KFileItem destItem = fileItemModel()->fileItem(index); + KUrl destUrl; + KFileItem destItem = m_model->fileItem(index); + if (destItem.isNull() || (!destItem.isDir() && !destItem.isDesktopFile())) { + // Use the URL of the view as drop target if the item is no directory + // or desktop-file + destItem = m_model->rootItem(); + destUrl = url(); + } else { + // The item represents a directory or desktop-file + destUrl = destItem.url(); + } QDropEvent dropEvent(event->pos().toPoint(), event->possibleActions(), @@ -831,23 +1037,65 @@ void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* even event->buttons(), event->modifiers()); - DragAndDropHelper::dropUrls(destItem, url(), &dropEvent, this); + QString error; + KonqOperations* op = DragAndDropHelper::dropUrls(destItem, destUrl, &dropEvent, error); + if (!error.isEmpty()) { + emit infoMessage(error); + } + + if (op && destUrl == url()) { + // Mark the dropped urls as selected. + m_clearSelectionBeforeSelectingNewItems = true; + connect(op, SIGNAL(aboutToCreate(KUrl::List)), this, SLOT(slotAboutToCreate(KUrl::List))); + } + + setActive(true); } void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* previous) { if (previous != 0) { - disconnect(previous, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted())); + disconnect(previous, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted())); + m_versionControlObserver->setModel(0); } - Q_ASSERT(qobject_cast(current)); - connect(current, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted())); + if (current) { + Q_ASSERT(qobject_cast(current)); + connect(current, SIGNAL(loadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted())); - KFileItemModel* fileItemModel = static_cast(current); - m_versionControlObserver->setModel(fileItemModel); + KFileItemModel* fileItemModel = static_cast(current); + m_versionControlObserver->setModel(fileItemModel); + } } -void DolphinView::slotSelectionChanged(const QSet& current, const QSet& previous) +void DolphinView::slotMouseButtonPressed(int itemIndex, Qt::MouseButtons buttons) +{ + hideToolTip(); + + if (itemIndex < 0) { + // Trigger the history navigation only when clicking on the viewport: + // Above an item the XButtons provide a simple way to select items in + // the singleClick mode. + if (buttons & Qt::XButton1) { + emit goBackRequested(); + } else if (buttons & Qt::XButton2) { + emit goForwardRequested(); + } + } +} + +void DolphinView::slotAboutToCreate(const KUrl::List& urls) +{ + if (!urls.isEmpty()) { + if (m_markFirstNewlySelectedItemAsCurrent) { + markUrlAsCurrent(urls.first()); + m_markFirstNewlySelectedItemAsCurrent = false; + } + m_selectedUrls << KDirModel::simplifiedUrlList(urls); + } +} + +void DolphinView::slotSelectionChanged(const KItemSet& current, const KItemSet& previous) { const int currentCount = current.count(); const int previousCount = previous.count(); @@ -867,45 +1115,33 @@ void DolphinView::emitSelectionChangedSignal() emit selectionChanged(selectedItems()); } -void DolphinView::dropUrls(const KFileItem& destItem, - const KUrl& destPath, - QDropEvent* event) -{ - Q_UNUSED(destItem); - Q_UNUSED(destPath); - markPastedUrlsAsSelected(event->mimeData()); - //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this); -} - -void DolphinView::updateSorting(DolphinView::Sorting sorting) +void DolphinView::updateSortRole(const QByteArray& role) { - ViewProperties props(url()); - props.setSorting(sorting); + ViewProperties props(viewPropertiesUrl()); + props.setSortRole(role); KItemModelBase* model = m_container->controller()->model(); - model->setSortRole(sortRoleForSorting(sorting)); + model->setSortRole(role); - emit sortingChanged(sorting); + emit sortRoleChanged(role); } void DolphinView::updateSortOrder(Qt::SortOrder order) { - ViewProperties props(url()); + ViewProperties props(viewPropertiesUrl()); props.setSortOrder(order); - KItemModelBase* model = fileItemModel(); - model->setSortOrder(order); + m_model->setSortOrder(order); emit sortOrderChanged(order); } void DolphinView::updateSortFoldersFirst(bool foldersFirst) { - ViewProperties props(url()); + ViewProperties props(viewPropertiesUrl()); props.setSortFoldersFirst(foldersFirst); - KFileItemModel* model = fileItemModel(); - model->setSortFoldersFirst(foldersFirst); + m_model->setSortDirectoriesFirst(foldersFirst); emit sortFoldersFirstChanged(foldersFirst); } @@ -941,7 +1177,7 @@ void DolphinView::restoreState(QDataStream& stream) // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes) QSet urls; stream >> urls; - fileItemModel()->restoreExpandedUrls(urls); + m_model->restoreExpandedDirectories(urls); } void DolphinView::saveState(QDataStream& stream) @@ -949,7 +1185,7 @@ void DolphinView::saveState(QDataStream& stream) // Save the current item that has the keyboard focus const int currentIndex = m_container->controller()->selectionManager()->currentItem(); if (currentIndex != -1) { - KFileItem item = fileItemModel()->fileItem(currentIndex); + KFileItem item = m_model->fileItem(currentIndex); Q_ASSERT(!item.isNull()); // If the current index is valid a item must exist KUrl currentItemUrl = item.url(); stream << currentItemUrl; @@ -963,43 +1199,77 @@ void DolphinView::saveState(QDataStream& stream) stream << QPoint(x, y); // Save expanded folders (only relevant for the details view - the set will be empty in other view modes) - stream << fileItemModel()->expandedUrls(); + stream << m_model->expandedDirectories(); } -bool DolphinView::hasSelection() const +KFileItem DolphinView::rootItem() const { - return m_container->controller()->selectionManager()->hasSelection(); + return m_model->rootItem(); } -KFileItem DolphinView::rootItem() const +void DolphinView::setViewPropertiesContext(const QString& context) { - return m_dirLister->rootItem(); + m_viewPropertiesContext = context; } -void DolphinView::observeCreatedItem(const KUrl& url) +QString DolphinView::viewPropertiesContext() const { - m_createdItemUrl = url; - //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)), - // this, SLOT(selectAndScrollToCreatedItem())); + return m_viewPropertiesContext; } -void DolphinView::selectAndScrollToCreatedItem() +KUrl DolphinView::openItemAsFolderUrl(const KFileItem& item, const bool browseThroughArchives) { - /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl); - if (dirIndex.isValid()) { - const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex); - QAbstractItemView* view = m_viewAccessor.itemView(); - if (view) { - view->setCurrentIndex(proxyIndex); + if (item.isNull()) { + return KUrl(); + } + + KUrl url = item.targetUrl(); + + if (item.isDir()) { + return url; + } + + if (item.isMimeTypeKnown()) { + const QString& mimetype = item.mimetype(); + + if (browseThroughArchives && item.isFile() && url.isLocalFile()) { + // Generic mechanism for redirecting to tar:// when clicking on a tar file, + // zip:// when clicking on a zip file, etc. + // The .protocol file specifies the mimetype that the kioslave handles. + // Note that we don't use mimetype inheritance since we don't want to + // open OpenDocument files as zip folders... + const QString& protocol = KProtocolManager::protocolForArchiveMimetype(mimetype); + if (!protocol.isEmpty()) { + url.setProtocol(protocol); + return url; + } + } + + if (mimetype == QLatin1String("application/x-desktop")) { + // Redirect to the URL in Type=Link desktop files, unless it is a http(s) URL. + KDesktopFile desktopFile(url.toLocalFile()); + if (desktopFile.hasLinkType()) { + const QString linkUrl = desktopFile.readUrl(); + if (!linkUrl.startsWith(QLatin1String("http"))) { + return linkUrl; + } + } } } - disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)), - this, SLOT(selectAndScrollToCreatedItem()));*/ - m_createdItemUrl = KUrl(); + return KUrl(); +} + +void DolphinView::observeCreatedItem(const KUrl& url) +{ + if (m_active) { + clearSelection(); + markUrlAsCurrent(url); + markUrlsAsSelected(QList() << url); + } } -void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl) +void DolphinView::slotDirectoryRedirection(const KUrl& oldUrl, const KUrl& newUrl) { if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) { emit redirection(oldUrl, newUrl); @@ -1011,13 +1281,20 @@ void DolphinView::updateViewState() { if (m_currentItemUrl != KUrl()) { KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager(); - const int currentIndex = fileItemModel()->index(m_currentItemUrl); + const int currentIndex = m_model->index(m_currentItemUrl); if (currentIndex != -1) { selectionManager->setCurrentItem(currentIndex); + + // scroll to current item and reset the state + if (m_scrollToCurrentItem) { + m_view->scrollToItem(currentIndex); + m_scrollToCurrentItem = false; + } + + m_currentItemUrl = KUrl(); } else { selectionManager->setCurrentItem(0); } - m_currentItemUrl = KUrl(); } if (!m_restoredContentsPosition.isNull()) { @@ -1031,18 +1308,49 @@ void DolphinView::updateViewState() if (!m_selectedUrls.isEmpty()) { KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager(); - QSet selectedItems = selectionManager->selectedItems(); - const KFileItemModel* model = fileItemModel(); - foreach (const KUrl& url, m_selectedUrls) { - const int index = model->index(url); + if (m_clearSelectionBeforeSelectingNewItems) { + selectionManager->clearSelection(); + m_clearSelectionBeforeSelectingNewItems = false; + } + + KItemSet selectedItems = selectionManager->selectedItems(); + + QList::iterator it = m_selectedUrls.begin(); + while (it != m_selectedUrls.end()) { + const int index = m_model->index(*it); if (index >= 0) { selectedItems.insert(index); + it = m_selectedUrls.erase(it); + } else { + ++it; } } selectionManager->setSelectedItems(selectedItems); - m_selectedUrls.clear(); + } +} + +void DolphinView::hideToolTip() +{ + if (GeneralSettings::showToolTips()) { + m_toolTipManager->hideToolTip(); + } +} + +void DolphinView::calculateItemCount(int& fileCount, + int& folderCount, + KIO::filesize_t& totalFileSize) const +{ + const int itemCount = m_model->count(); + for (int i = 0; i < itemCount; ++i) { + const KFileItem item = m_model->fileItem(i); + if (item.isDir()) { + ++folderCount; + } else { + ++fileCount; + totalFileSize += item.size(); + } } } @@ -1065,7 +1373,17 @@ void DolphinView::slotDeleteFileFinished(KJob* job) } } -void DolphinView::slotDirListerStarted(const KUrl& url) +void DolphinView::slotRenamingFailed(const KUrl& oldUrl, const KUrl& newUrl) +{ + const int index = m_model->index(newUrl); + if (index >= 0) { + QHash data; + data.insert("text", oldUrl.fileName()); + m_model->setData(index, data); + } +} + +void DolphinView::slotDirectoryLoadingStarted() { // Disable the writestate temporary until it can be determined in a fast way // in DolphinView::slotLoadingCompleted() @@ -1074,36 +1392,105 @@ void DolphinView::slotDirListerStarted(const KUrl& url) emit writeStateChanged(m_isFolderWritable); } - emit startedPathLoading(url); + emit directoryLoadingStarted(); } -void DolphinView::slotLoadingCompleted() +void DolphinView::slotDirectoryLoadingCompleted() { - // Update the view-state. This has to be done using a Qt::QueuedConnection - // because the view might not be in its final state yet (the view also - // listens to the completed()-signal from KDirLister and the order of - // of slots is undefined). + // Update the view-state. This has to be done asynchronously + // because the view might not be in its final state yet. QTimer::singleShot(0, this, SLOT(updateViewState())); - emit finishedPathLoading(url()); + emit directoryLoadingCompleted(); updateWritableState(); } -void DolphinView::slotRefreshItems() +void DolphinView::slotItemsChanged() { - if (m_assureVisibleCurrentIndex) { - m_assureVisibleCurrentIndex = false; - //QAbstractItemView* view = m_viewAccessor.itemView(); - //if (view) { - // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex()); - //} - } + m_assureVisibleCurrentIndex = false; } -KFileItemModel* DolphinView::fileItemModel() const +void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current, Qt::SortOrder previous) { - return static_cast(m_container->controller()->model()); + Q_UNUSED(previous); + Q_ASSERT(m_model->sortOrder() == current); + + ViewProperties props(viewPropertiesUrl()); + props.setSortOrder(current); + + emit sortOrderChanged(current); +} + +void DolphinView::slotSortRoleChangedByHeader(const QByteArray& current, const QByteArray& previous) +{ + Q_UNUSED(previous); + Q_ASSERT(m_model->sortRole() == current); + + ViewProperties props(viewPropertiesUrl()); + props.setSortRole(current); + + emit sortRoleChanged(current); +} + +void DolphinView::slotVisibleRolesChangedByHeader(const QList& current, + const QList& previous) +{ + Q_UNUSED(previous); + Q_ASSERT(m_container->controller()->view()->visibleRoles() == current); + + const QList previousVisibleRoles = m_visibleRoles; + + m_visibleRoles = current; + + ViewProperties props(viewPropertiesUrl()); + props.setVisibleRoles(m_visibleRoles); + + emit visibleRolesChanged(m_visibleRoles, previousVisibleRoles); +} + +void DolphinView::slotRoleEditingCanceled() +{ + disconnect(m_view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)), + this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant))); +} + +void DolphinView::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value) +{ + disconnect(m_view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)), + this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant))); + + if (index < 0 || index >= m_model->count()) { + return; + } + + if (role == "text") { + const KFileItem oldItem = m_model->fileItem(index); + const QString newName = value.toString(); + if (!newName.isEmpty() && newName != oldItem.text() && newName != QLatin1String(".") && newName != QLatin1String("..")) { + const KUrl oldUrl = oldItem.url(); + + const KUrl newUrl(url().path(KUrl::AddTrailingSlash) + newName); + const bool newNameExistsAlready = (m_model->index(newUrl) >= 0); + if (!newNameExistsAlready) { + // Only change the data in the model if no item with the new name + // is in the model yet. If there is an item with the new name + // already, calling KonqOperations::rename() will open a dialog + // asking for a new name, and KFileItemModel will update the + // data when the dir lister signals that the file name has changed. + QHash data; + data.insert(role, value); + m_model->setData(index, data); + } + + KonqOperations* op = KonqOperations::renameV2(this, oldUrl, newName); + if (op && !newNameExistsAlready) { + // Only connect the renamingFailed signal if there is no item with the new name + // in the model yet, see bug 328262. + connect(op, SIGNAL(renamingFailed(KUrl,KUrl)), SLOT(slotRenamingFailed(KUrl,KUrl))); + } + } + } } void DolphinView::loadDirectory(const KUrl& url, bool reload) @@ -1118,14 +1505,22 @@ void DolphinView::loadDirectory(const KUrl& url, bool reload) return; } - m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags); + if (reload) { + m_model->refreshDirectory(url); + } else { + m_model->loadDirectory(url); + } } void DolphinView::applyViewProperties() { - m_container->beginTransaction(); + const ViewProperties props(viewPropertiesUrl()); + applyViewProperties(props); +} - const ViewProperties props(url()); +void DolphinView::applyViewProperties(const ViewProperties& props) +{ + m_view->beginTransaction(); const Mode mode = props.viewMode(); if (m_mode != mode) { @@ -1135,69 +1530,59 @@ void DolphinView::applyViewProperties() // Changing the mode might result in changing // the zoom level. Remember the old zoom level so // that zoomLevelChanged() can get emitted. - const int oldZoomLevel = m_container->zoomLevel(); - - switch (m_mode) { - case IconsView: m_container->setItemLayout(KFileItemListView::IconsLayout); break; - case CompactView: m_container->setItemLayout(KFileItemListView::CompactLayout); break; - case DetailsView: m_container->setItemLayout(KFileItemListView::DetailsLayout); break; - default: Q_ASSERT(false); break; - } + const int oldZoomLevel = m_view->zoomLevel(); + applyModeToView(); emit modeChanged(m_mode, previousMode); - if (m_container->zoomLevel() != oldZoomLevel) { - emit zoomLevelChanged(m_container->zoomLevel(), oldZoomLevel); + if (m_view->zoomLevel() != oldZoomLevel) { + emit zoomLevelChanged(m_view->zoomLevel(), oldZoomLevel); } } const bool hiddenFilesShown = props.hiddenFilesShown(); - if (hiddenFilesShown != m_dirLister->showingDotFiles()) { - m_dirLister->setShowingDotFiles(hiddenFilesShown); - m_dirLister->emitChanges(); + if (hiddenFilesShown != m_model->showHiddenFiles()) { + m_model->setShowHiddenFiles(hiddenFilesShown); emit hiddenFilesShownChanged(hiddenFilesShown); } - KFileItemModel* model = fileItemModel(); - const bool groupedSorting = props.groupedSorting(); - if (groupedSorting != model->groupedSorting()) { - model->setGroupedSorting(groupedSorting); + if (groupedSorting != m_model->groupedSorting()) { + m_model->setGroupedSorting(groupedSorting); emit groupedSortingChanged(groupedSorting); } - const DolphinView::Sorting sorting = props.sorting(); - const QByteArray newSortRole = sortRoleForSorting(sorting); - if (newSortRole != model->sortRole()) { - model->setSortRole(newSortRole); - emit sortingChanged(sorting); + const QByteArray sortRole = props.sortRole(); + if (sortRole != m_model->sortRole()) { + m_model->setSortRole(sortRole); + emit sortRoleChanged(sortRole); } const Qt::SortOrder sortOrder = props.sortOrder(); - if (sortOrder != model->sortOrder()) { - model->setSortOrder(sortOrder); + if (sortOrder != m_model->sortOrder()) { + m_model->setSortOrder(sortOrder); emit sortOrderChanged(sortOrder); } const bool sortFoldersFirst = props.sortFoldersFirst(); - if (sortFoldersFirst != model->sortFoldersFirst()) { - model->setSortFoldersFirst(sortFoldersFirst); + if (sortFoldersFirst != m_model->sortDirectoriesFirst()) { + m_model->setSortDirectoriesFirst(sortFoldersFirst); emit sortFoldersFirstChanged(sortFoldersFirst); } - const QList infoList = props.additionalInfoList(); - if (infoList != m_additionalInfoList) { - const QList previousList = m_additionalInfoList; - m_additionalInfoList = infoList; - applyAdditionalInfoListToView(); - emit additionalInfoListChanged(m_additionalInfoList, previousList); + const QList visibleRoles = props.visibleRoles(); + if (visibleRoles != m_visibleRoles) { + const QList previousVisibleRoles = m_visibleRoles; + m_visibleRoles = visibleRoles; + m_view->setVisibleRoles(visibleRoles); + emit visibleRolesChanged(m_visibleRoles, previousVisibleRoles); } const bool previewsShown = props.previewsShown(); - if (previewsShown != m_container->previewsShown()) { + if (previewsShown != m_view->previewsShown()) { const int oldZoomLevel = zoomLevel(); - m_container->setPreviewsShown(previewsShown); + m_view->setPreviewsShown(previewsShown); emit previewsShownChanged(previewsShown); // Changing the preview-state might result in a changed zoom-level @@ -1206,38 +1591,45 @@ void DolphinView::applyViewProperties() } } - m_container->endTransaction(); -} - -void DolphinView::applyAdditionalInfoListToView() -{ - const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance(); + KItemListView* itemListView = m_container->controller()->view(); + if (itemListView->isHeaderVisible()) { + KItemListHeader* header = itemListView->header(); + const QList headerColumnWidths = props.headerColumnWidths(); + const int rolesCount = m_visibleRoles.count(); + if (headerColumnWidths.count() == rolesCount) { + header->setAutomaticColumnResizing(false); - QList visibleRoles; - visibleRoles.reserve(m_additionalInfoList.count() + 1); - visibleRoles.append("name"); - - foreach (AdditionalInfo info, m_additionalInfoList) { - visibleRoles.append(infoAccessor.role(info)); + QHash columnWidths; + for (int i = 0; i < rolesCount; ++i) { + columnWidths.insert(m_visibleRoles[i], headerColumnWidths[i]); + } + header->setColumnWidths(columnWidths); + } else { + header->setAutomaticColumnResizing(true); + } } - m_container->setVisibleRoles(visibleRoles); + m_view->endTransaction(); } -void DolphinView::pasteToUrl(const KUrl& url) +void DolphinView::applyModeToView() { - markPastedUrlsAsSelected(QApplication::clipboard()->mimeData()); - KonqOperations::doPaste(this, url); + switch (m_mode) { + case IconsView: m_view->setItemLayout(KFileItemListView::IconsLayout); break; + case CompactView: m_view->setItemLayout(KFileItemListView::CompactLayout); break; + case DetailsView: m_view->setItemLayout(KFileItemListView::DetailsLayout); break; + default: Q_ASSERT(false); break; + } } -void DolphinView::updateZoomLevel(int oldZoomLevel) +void DolphinView::pasteToUrl(const KUrl& url) { - Q_UNUSED(oldZoomLevel); - /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize()); - if (oldZoomLevel != newZoomLevel) { - m_viewModeController->setZoomLevel(newZoomLevel); - emit zoomLevelChanged(newZoomLevel); - }*/ + KonqOperations* op = KonqOperations::doPasteV2(this, url); + if (op) { + m_clearSelectionBeforeSelectingNewItems = true; + m_markFirstNewlySelectedItemAsCurrent = true; + connect(op, SIGNAL(aboutToCreate(KUrl::List)), this, SLOT(slotAboutToCreate(KUrl::List))); + } } KUrl::List DolphinView::simplifiedSelectedUrls() const @@ -1245,7 +1637,7 @@ KUrl::List DolphinView::simplifiedSelectedUrls() const KUrl::List urls; const KFileItemList items = selectedItems(); - foreach (const KFileItem &item, items) { + foreach (const KFileItem& item, items) { urls.append(item.url()); } @@ -1259,25 +1651,18 @@ KUrl::List DolphinView::simplifiedSelectedUrls() const QMimeData* DolphinView::selectionMimeData() const { - const KFileItemModel* model = fileItemModel(); const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager(); - const QSet selectedIndexes = selectionManager->selectedItems(); - - return model->createMimeData(selectedIndexes); -} + const KItemSet selectedIndexes = selectionManager->selectedItems(); -void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData) -{ - const KUrl::List urls = KUrl::List::fromMimeData(mimeData); - markUrlsAsSelected(urls); + return m_model->createMimeData(selectedIndexes); } void DolphinView::updateWritableState() { const bool wasFolderWritable = m_isFolderWritable; - m_isFolderWritable = true; + m_isFolderWritable = false; - const KFileItem item = m_dirLister->rootItem(); + const KFileItem item = m_model->rootItem(); if (!item.isNull()) { KFileItemListProperties capabilities(KFileItemList() << item); m_isFolderWritable = capabilities.supportsWriting(); @@ -1287,62 +1672,16 @@ void DolphinView::updateWritableState() } } -QByteArray DolphinView::sortRoleForSorting(Sorting sorting) const +KUrl DolphinView::viewPropertiesUrl() const { - switch (sorting) { - case SortByName: return "name"; - case SortBySize: return "size"; - case SortByDate: return "date"; - case SortByPermissions: return "permissions"; - case SortByOwner: return "owner"; - case SortByGroup: return "group"; - case SortByType: return "type"; - case SortByDestination: return "destination"; - case SortByPath: return "path"; - default: break; + if (m_viewPropertiesContext.isEmpty()) { + return m_url; } - return QByteArray(); -} - -DolphinView::Sorting DolphinView::sortingForSortRole(const QByteArray& sortRole) const -{ - static QHash sortHash; - if (sortHash.isEmpty()) { - sortHash.insert("name", SortByName); - sortHash.insert("size", SortBySize); - sortHash.insert("date", SortByDate); - sortHash.insert("permissions", SortByPermissions); - sortHash.insert("owner", SortByOwner); - sortHash.insert("group", SortByGroup); - sortHash.insert("type", SortByType); - sortHash.insert("destination", SortByDestination); - sortHash.insert("path", SortByPath); - } - return sortHash.value(sortRole); -} - -QString DolphinView::fileSizeText(KIO::filesize_t fileSize) -{ - const KLocale* locale = KGlobal::locale(); - const unsigned int multiplier = (locale->binaryUnitDialect() == KLocale::MetricBinaryDialect) - ? 1000 : 1024; - - QString text; - if (fileSize < multiplier) { - // Show the size in bytes - text = locale->formatByteSize(fileSize, 0, KLocale::DefaultBinaryDialect, KLocale::UnitByte); - } else if (fileSize < multiplier * multiplier) { - // Show the size in kilobytes and always round up. This is done - // for consistency with the values shown e.g. in the "Size" column - // of the details-view. - fileSize += (multiplier / 2) - 1; - text = locale->formatByteSize(fileSize, 0, KLocale::DefaultBinaryDialect, KLocale::UnitKiloByte); - } else { - // Show the size in the best fitting unit having one decimal - text = locale->formatByteSize(fileSize, 1); - } - return text; + KUrl url; + url.setProtocol(m_url.protocol()); + url.setPath(m_viewPropertiesContext); + return url; } #include "dolphinview.moc"