X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/ae1980442d70aa04b1699fbba1c3503fdab58e6d..fdf854bd81d9e42df2d8672d49a0b7fcdb7443a5:/src/views/dolphinview.cpp diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index 3beb246a1..0c5ebb1df 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -119,6 +119,7 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent) KItemListController *controller = new KItemListController(m_model, m_view, this); const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1; controller->setAutoActivationDelay(delay); + connect(controller, &KItemListController::doubleClickViewBackground, this, &DolphinView::doubleClickViewBackground); // The EnlargeSmallPreviews setting can only be changed after the model // has been set in the view by KItemListController. @@ -126,6 +127,9 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent) m_container = new KItemListContainer(controller, this); m_container->installEventFilter(this); +#ifndef QT_NO_ACCESSIBILITY + m_view->setAccessibleParentsObject(m_container); +#endif setFocusProxy(m_container); connect(m_container->horizontalScrollBar(), &QScrollBar::valueChanged, this, [=] { hideToolTip(); @@ -143,11 +147,13 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent) // This is made using a heavily-modified QLabel rather than a KTitleWidget // because KTitleWidget can't be told to turn off mouse-selectable text m_placeholderLabel = new QLabel(this); + // Don't consume mouse events + m_placeholderLabel->setAttribute(Qt::WA_TransparentForMouseEvents); + QFont placeholderLabelFont; // To match the size of a level 2 Heading/KTitleWidget placeholderLabelFont.setPointSize(qRound(placeholderLabelFont.pointSize() * 1.3)); m_placeholderLabel->setFont(placeholderLabelFont); - m_placeholderLabel->setTextInteractionFlags(Qt::NoTextInteraction); m_placeholderLabel->setWordWrap(true); m_placeholderLabel->setAlignment(Qt::AlignCenter); // Match opacity of QML placeholder label component @@ -160,10 +166,6 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent) auto *centeringLayout = new QVBoxLayout(m_container); centeringLayout->addWidget(m_placeholderLabel); centeringLayout->setAlignment(m_placeholderLabel, Qt::AlignCenter); - m_placeholderLabel->setContextMenuPolicy(Qt::CustomContextMenu); - connect(m_placeholderLabel, &QWidget::customContextMenuRequested, this, [this](const QPoint &pos) { - slotViewContextMenuRequested(m_placeholderLabel->mapToGlobal(pos)); - }); controller->setSelectionBehavior(KItemListController::MultiSelection); connect(controller, &KItemListController::itemActivated, this, &DolphinView::slotItemActivated); @@ -228,7 +230,9 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent) m_versionControlObserver->setView(this); m_versionControlObserver->setModel(m_model); connect(m_versionControlObserver, &VersionControlObserver::infoMessage, this, &DolphinView::infoMessage); - connect(m_versionControlObserver, &VersionControlObserver::errorMessage, this, &DolphinView::errorMessage); + connect(m_versionControlObserver, &VersionControlObserver::errorMessage, this, [this](const QString &message) { + Q_EMIT errorMessage(message, KIO::ERR_UNKNOWN); + }); connect(m_versionControlObserver, &VersionControlObserver::operationCompletedMessage, this, &DolphinView::operationCompletedMessage); m_twoClicksRenamingTimer = new QTimer(this); @@ -264,7 +268,6 @@ void DolphinView::setActive(bool active) if (active) { m_container->setFocus(); Q_EMIT activated(); - Q_EMIT writeStateChanged(m_isFolderWritable); } } @@ -276,6 +279,10 @@ bool DolphinView::isActive() const void DolphinView::setViewMode(Mode mode) { if (mode != m_mode) { + // Reset scrollbars before changing the view mode. + m_container->horizontalScrollBar()->setValue(0); + m_container->verticalScrollBar()->setValue(0); + ViewProperties props(viewPropertiesUrl()); props.setViewMode(mode); @@ -302,8 +309,8 @@ void DolphinView::setSelectionModeEnabled(const bool enabled) m_view->setStyle(m_proxyStyle.get()); m_view->setEnabledSelectionToggles(DolphinItemListView::SelectionTogglesEnabled::False); } else { - setStyle(QApplication::style()); - m_view->setStyle(QApplication::style()); + setStyle(nullptr); + m_view->setStyle(nullptr); m_view->setEnabledSelectionToggles(DolphinItemListView::SelectionTogglesEnabled::FollowSetting); } m_container->controller()->setSelectionModeEnabled(enabled); @@ -368,9 +375,8 @@ void DolphinView::setGroupedSorting(bool grouped) ViewProperties props(viewPropertiesUrl()); props.setGroupedSorting(grouped); - props.save(); - m_container->controller()->model()->setGroupedSorting(grouped); + m_model->setGroupedSorting(grouped); Q_EMIT groupedSortingChanged(grouped); } @@ -464,7 +470,13 @@ int DolphinView::zoomLevel() const void DolphinView::setSortRole(const QByteArray &role) { if (role != sortRole()) { - updateSortRole(role); + ViewProperties props(viewPropertiesUrl()); + props.setSortRole(role); + + KItemModelBase *model = m_container->controller()->model(); + model->setSortRole(role); + + Q_EMIT sortRoleChanged(role); } } @@ -477,7 +489,12 @@ QByteArray DolphinView::sortRole() const void DolphinView::setSortOrder(Qt::SortOrder order) { if (sortOrder() != order) { - updateSortOrder(order); + ViewProperties props(viewPropertiesUrl()); + props.setSortOrder(order); + + m_model->setSortOrder(order); + + Q_EMIT sortOrderChanged(order); } } @@ -634,11 +651,11 @@ void DolphinView::emitStatusBarText(const int folderCount, const int fileCount, if (selection == HasSelection) { // At least 2 items are selected because the case of 1 selected item is handled in // DolphinView::requestStatusBarText(). - foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount); - filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount); + foldersText = i18ncp("@info:status", "1 folder selected", "%1 folders selected", folderCount); + filesText = i18ncp("@info:status", "1 file selected", "%1 files selected", fileCount); } else { - foldersText = i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount); - filesText = i18ncp("@info:status", "1 File", "%1 Files", fileCount); + foldersText = i18ncp("@info:status", "1 folder", "%1 folders", folderCount); + filesText = i18ncp("@info:status", "1 file", "%1 files", fileCount); } if (fileCount > 0 && folderCount > 0) { @@ -648,7 +665,7 @@ void DolphinView::emitStatusBarText(const int folderCount, const int fileCount, } else if (folderCount > 0) { summary = foldersText; } else { - summary = i18nc("@info:status", "0 Folders, 0 Files"); + summary = i18nc("@info:status", "0 folders, 0 files"); } Q_EMIT statusBarTextChanged(summary); } @@ -723,22 +740,39 @@ void DolphinView::renameSelectedItems() if (items.count() == 1 && GeneralSettings::renameInline()) { const int index = m_model->index(items.first()); - QMetaObject::Connection *const connection = new QMetaObject::Connection; - *connection = connect(m_view, &KItemListView::scrollingStopped, this, [=]() { - QObject::disconnect(*connection); - delete connection; - - m_view->editRole(index, "text"); + connect( + m_view, + &KItemListView::scrollingStopped, + this, + [this, index]() { + m_view->editRole(index, "text"); - hideToolTip(); + hideToolTip(); - connect(m_view, &DolphinItemListView::roleEditingFinished, this, &DolphinView::slotRoleEditingFinished); - }); + connect(m_view, &DolphinItemListView::roleEditingFinished, this, &DolphinView::slotRoleEditingFinished); + }, + Qt::SingleShotConnection); m_view->scrollToItem(index); } else { KIO::RenameFileDialog *dialog = new KIO::RenameFileDialog(items, this); - connect(dialog, &KIO::RenameFileDialog::renamingFinished, this, &DolphinView::slotRenameDialogRenamingFinished); + connect(dialog, &KIO::RenameFileDialog::renamingFinished, this, [this, items](const QList &urls) { + // The model may have already been updated, so it's possible that we don't find the old items. + for (int i = 0; i < items.count(); ++i) { + const int index = m_model->index(items[i]); + if (index >= 0) { + QHash data; + data.insert("text", urls[i].fileName()); + m_model->setData(index, data); + } + } + + forceUrlsSelection(urls.first(), urls); + updateSelectionState(); + }); + connect(dialog, &KIO::RenameFileDialog::error, this, [this](KJob *job) { + KMessageBox::error(this, job->errorString()); + }); dialog->open(); } @@ -846,9 +880,12 @@ void DolphinView::duplicateSelectedItems() const QMimeDatabase db; + m_clearSelectionBeforeSelectingNewItems = true; + m_markFirstNewlySelectedItemAsCurrent = true; + m_selectJobCreatedItems = true; + // Duplicate all selected items and append "copy" to the end of the file name // but before the filename extension, if present - QList newSelection; for (const auto &item : itemList) { const QUrl originalURL = item.url(); const QString originalDirectoryPath = originalURL.adjusted(QUrl::RemoveFilename).path(); @@ -873,15 +910,14 @@ void DolphinView::duplicateSelectedItems() } KIO::CopyJob *job = KIO::copyAs(originalURL, duplicateURL); + job->setAutoRename(true); KJobWidgets::setWindow(job, this); - if (job) { - newSelection << duplicateURL; - KIO::FileUndoManager::self()->recordCopyJob(job); - } + connect(job, &KIO::CopyJob::result, this, &DolphinView::slotJobResult); + connect(job, &KIO::CopyJob::copyingDone, this, &DolphinView::slotItemCreatedFromJob); + connect(job, &KIO::CopyJob::copyingLinkDone, this, &DolphinView::slotItemLinkCreatedFromJob); + KIO::FileUndoManager::self()->recordCopyJob(job); } - - forceUrlsSelection(newSelection.first(), newSelection); } void DolphinView::stopLoading() @@ -1055,24 +1091,14 @@ void DolphinView::slotItemsActivated(const KItemSet &indexes) 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()); -#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0) - const int answer = KMessageBox::warningTwoActions( + const int answer = KMessageBox::warningContinueCancel( this, question, {}, -#else - const int answer = - KMessageBox::warningYesNo(this, - question, - {}, -#endif KGuiItem(i18ncp("@action:button", "Open %1 Item", "Open %1 Items", indexes.count()), QStringLiteral("document-open")), - KStandardGuiItem::cancel()); -#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0) - if (answer != KMessageBox::PrimaryAction) { -#else - if (answer != KMessageBox::Yes) { -#endif + KStandardGuiItem::cancel(), + QStringLiteral("ConfirmOpenManyFolders")); + if (answer != KMessageBox::PrimaryAction && answer != KMessageBox::Continue) { return; } } @@ -1108,9 +1134,8 @@ void DolphinView::slotItemsActivated(const KItemSet &indexes) void DolphinView::slotItemMiddleClicked(int index) { const KFileItem &item = m_model->fileItem(index); - const QUrl &url = openItemAsFolderUrl(item); + const QUrl &url = openItemAsFolderUrl(item, GeneralSettings::browseThroughArchives()); const auto modifiers = QGuiApplication::keyboardModifiers(); - const QString &archiveProtocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype()); if (!url.isEmpty()) { // keep in sync with KUrlNavigator::slotNavigatorButtonClicked if (modifiers & Qt::ShiftModifier) { @@ -1118,7 +1143,7 @@ void DolphinView::slotItemMiddleClicked(int index) } else { Q_EMIT tabRequested(url); } - } else if (!archiveProtocol.isEmpty() && isTabsForFilesEnabled()) { + } else if (isTabsForFilesEnabled()) { // keep in sync with KUrlNavigator::slotNavigatorButtonClicked if (modifiers & Qt::ShiftModifier) { Q_EMIT activeTabRequested(item.url()); @@ -1151,7 +1176,7 @@ void DolphinView::slotHeaderContextMenuRequested(const QPointF &pos) { ViewProperties props(viewPropertiesUrl()); - QPointer menu = new QMenu(QApplication::activeWindow()); + QPointer menu = new QMenu(this); KItemListView *view = m_container->controller()->view(); const QList visibleRolesSet = view->visibleRoles(); @@ -1415,6 +1440,11 @@ void DolphinView::slotItemCreatedFromJob(KIO::Job *, const QUrl &, const QUrl &t slotItemCreated(to); } +void DolphinView::slotItemLinkCreatedFromJob(KIO::Job *, const QUrl &, const QString &, const QUrl &to) +{ + slotItemCreated(to); +} + void DolphinView::slotItemCreated(const QUrl &url) { if (m_markFirstNewlySelectedItemAsCurrent) { @@ -1426,10 +1456,20 @@ void DolphinView::slotItemCreated(const QUrl &url) } } +void DolphinView::onDirectoryLoadingCompletedAfterJob() +{ + // the model should now contain all the items created by the job + m_selectJobCreatedItems = true; // to make sure we overwrite selection + // update the view: scroll into View and selection + updateViewState(); + m_selectJobCreatedItems = false; + m_selectedUrls.clear(); +} + void DolphinView::slotJobResult(KJob *job) { if (job->error() && job->error() != KIO::ERR_USER_CANCELED) { - Q_EMIT errorMessage(job->errorString()); + Q_EMIT errorMessage(job->errorString(), job->error()); } if (!m_selectJobCreatedItems) { m_selectedUrls.clear(); @@ -1441,21 +1481,7 @@ void DolphinView::slotJobResult(KJob *job) updateSelectionState(); if (!m_selectedUrls.isEmpty()) { // not all urls were found, the model may not be up to date - // TODO KF6 replace with Qt::singleShotConnection - QMetaObject::Connection *const connection = new QMetaObject::Connection; - *connection = connect( - m_model, - &KFileItemModel::directoryLoadingCompleted, - this, - [this, connection]() { - // the model should now contain all the items created by the job - updateSelectionState(); - m_selectJobCreatedItems = false; - m_selectedUrls.clear(); - QObject::disconnect(*connection); - delete connection; - }, - Qt::UniqueConnection); + connect(m_model, &KFileItemModel::directoryLoadingCompleted, this, &DolphinView::onDirectoryLoadingCompletedAfterJob, Qt::SingleShotConnection); } else { m_selectJobCreatedItems = false; m_selectedUrls.clear(); @@ -1512,27 +1538,6 @@ void DolphinView::slotStatJobResult(KJob *job) emitStatusBarText(folderCount, fileCount, totalFileSize, NoSelection); } -void DolphinView::updateSortRole(const QByteArray &role) -{ - ViewProperties props(viewPropertiesUrl()); - props.setSortRole(role); - - KItemModelBase *model = m_container->controller()->model(); - model->setSortRole(role); - - Q_EMIT sortRoleChanged(role); -} - -void DolphinView::updateSortOrder(Qt::SortOrder order) -{ - ViewProperties props(viewPropertiesUrl()); - props.setSortOrder(order); - - m_model->setSortOrder(order); - - Q_EMIT sortOrderChanged(order); -} - void DolphinView::updateSortFoldersFirst(bool foldersFirst) { ViewProperties props(viewPropertiesUrl()); @@ -1726,6 +1731,7 @@ void DolphinView::updateSelectionState() if (!m_selectedUrls.isEmpty()) { KItemListSelectionManager *selectionManager = m_container->controller()->selectionManager(); + const bool shouldScrollToCurrentItem = m_clearSelectionBeforeSelectingNewItems; // if there is a selection already, leave it that way // unless some drop/paste job are in the process of creating items if (!selectionManager->hasSelection() || m_selectJobCreatedItems) { @@ -1750,6 +1756,10 @@ void DolphinView::updateSelectionState() if (!selectedItems.isEmpty()) { selectionManager->beginAnchoredSelection(selectionManager->currentItem()); selectionManager->setSelectedItems(selectedItems); + selectionManager->endAnchoredSelection(); + if (shouldScrollToCurrentItem) { + m_view->scrollToItem(selectedItems.first()); + } } } } @@ -1768,7 +1778,7 @@ void DolphinView::updateViewState() // scroll to current item and reset the state if (m_scrollToCurrentItem) { - m_view->scrollToItem(currentIndex); + m_view->scrollToItem(currentIndex, KItemListView::ViewItemPosition::Middle); m_scrollToCurrentItem = false; } m_currentItemUrl = QUrl(); @@ -1832,7 +1842,7 @@ void DolphinView::slotTrashFileFinished(KJob *job) selectNextItem(); // Fixes BUG: 419914 via selecting next item Q_EMIT operationCompletedMessage(i18nc("@info:status", "Trash operation completed.")); } else if (job->error() != KIO::ERR_USER_CANCELED) { - Q_EMIT errorMessage(job->errorString()); + Q_EMIT errorMessage(job->errorString(), job->error()); } } @@ -1842,7 +1852,7 @@ void DolphinView::slotDeleteFileFinished(KJob *job) selectNextItem(); // Fixes BUG: 419914 via selecting next item Q_EMIT operationCompletedMessage(i18nc("@info:status", "Delete operation completed.")); } else if (job->error() != KIO::ERR_USER_CANCELED) { - Q_EMIT errorMessage(job->errorString()); + Q_EMIT errorMessage(job->errorString(), job->error()); } } @@ -1998,13 +2008,8 @@ void DolphinView::slotRoleEditingFinished(int index, const QByteArray &role, con if (!hiddenFilesShown() && newName.startsWith(QLatin1Char('.')) && !oldItem.name().startsWith(QLatin1Char('.'))) { KGuiItem yesGuiItem(i18nc("@action:button", "Rename and Hide"), QStringLiteral("view-hidden")); -#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0) const auto code = KMessageBox::questionTwoActions(this, -#else - const auto code = - KMessageBox::questionYesNo(this, -#endif oldItem.isFile() ? i18n("Adding a dot to the beginning of this file's name will hide it from view.\n" "Do you still want to rename it?") : i18n("Adding a dot to the beginning of this folder's name will hide it from view.\n" @@ -2014,11 +2019,7 @@ void DolphinView::slotRoleEditingFinished(int index, const QByteArray &role, con KStandardGuiItem::cancel(), QStringLiteral("ConfirmHide")); -#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0) if (code == KMessageBox::SecondaryAction) { -#else - if (code == KMessageBox::No) { -#endif return; } } @@ -2043,6 +2044,7 @@ void DolphinView::slotRoleEditingFinished(int index, const QByteArray &role, con if (!newNameExistsAlready) { forceUrlsSelection(newUrl, {newUrl}); + updateSelectionState(); // Only connect the result signal if there is no item with the new name // in the model yet, see bug 328262. @@ -2063,9 +2065,9 @@ void DolphinView::loadDirectory(const QUrl &url, bool reload) if (!url.isValid()) { const QString location(url.toDisplayString(QUrl::PreferLocalFile)); if (location.isEmpty()) { - Q_EMIT errorMessage(i18nc("@info:status", "The location is empty.")); + Q_EMIT errorMessage(i18nc("@info:status", "The location is empty."), KIO::ERR_UNKNOWN); } else { - Q_EMIT errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location)); + Q_EMIT errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location), KIO::ERR_UNKNOWN); } return; } @@ -2209,8 +2211,16 @@ void DolphinView::pasteToUrl(const QUrl &url) m_clearSelectionBeforeSelectingNewItems = true; m_markFirstNewlySelectedItemAsCurrent = true; m_selectJobCreatedItems = true; - // TODO KF6 use KIO::PasteJob::copyJobStarted to hook to earlier events copying/moving connect(job, &KIO::PasteJob::itemCreated, this, &DolphinView::slotItemCreated); + connect(job, &KIO::PasteJob::copyJobStarted, this, [this](const KIO::CopyJob *copyJob) { + connect(copyJob, &KIO::CopyJob::copying, this, &DolphinView::slotItemCreatedFromJob); + connect(copyJob, &KIO::CopyJob::moving, this, &DolphinView::slotItemCreatedFromJob); + connect(copyJob, &KIO::CopyJob::linking, this, [this](KIO::Job *job, const QString &src, const QUrl &dest) { + Q_UNUSED(job) + Q_UNUSED(src) + slotItemCreated(dest); + }); + }); connect(job, &KIO::PasteJob::result, this, &DolphinView::slotJobResult); } @@ -2261,6 +2271,11 @@ void DolphinView::updateWritableState() } } +bool DolphinView::isFolderWritable() const +{ + return m_isFolderWritable; +} + QUrl DolphinView::viewPropertiesUrl() const { if (m_viewPropertiesContext.isEmpty()) { @@ -2273,11 +2288,6 @@ QUrl DolphinView::viewPropertiesUrl() const return url; } -void DolphinView::slotRenameDialogRenamingFinished(const QList &urls) -{ - forceUrlsSelection(urls.first(), urls); -} - void DolphinView::forceUrlsSelection(const QUrl ¤t, const QList &selected) { clearSelection(); @@ -2301,7 +2311,7 @@ void DolphinView::copyPathToClipboard() if (clipboard == nullptr) { return; } - clipboard->setText(path); + clipboard->setText(QDir::toNativeSeparators(path)); } void DolphinView::slotIncreaseZoom()