diff options
| author | Méven Car <[email protected]> | 2025-11-16 13:53:58 +0100 |
|---|---|---|
| committer | Méven Car <[email protected]> | 2026-01-20 10:45:34 +0100 |
| commit | 93a6978655fc1ce9e0fd8ea08029859f71c32db9 (patch) | |
| tree | a65c505d9b951317dcc4a5d5bfc16cf7f5992995 /src | |
| parent | 38fb26518a14f68ce66a14a1197866fed865c430 (diff) | |
dolphinview: when creating a subfolder expand to it if in details mode
So the new folder is in view.
Diffstat (limited to 'src')
| -rw-r--r-- | src/dolphincontextmenu.cpp | 5 | ||||
| -rw-r--r-- | src/dolphinmainwindow.cpp | 3 | ||||
| -rw-r--r-- | src/kitemviews/kfileitemmodel.cpp | 36 | ||||
| -rw-r--r-- | src/tests/kfileitemmodeltest.cpp | 3 | ||||
| -rw-r--r-- | src/views/dolphinview.cpp | 7 | ||||
| -rw-r--r-- | src/views/dolphinview.h | 5 |
6 files changed, 53 insertions, 6 deletions
diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 53f155af4..577990e19 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -225,7 +225,10 @@ void DolphinContextMenu::addDirectoryItemContextMenu() newFileMenu->setEnabled(selectedItemsProps.supportsWriting()); connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater); connect(newFileMenu, &DolphinNewFileMenu::fileCreationRejected, newFileMenu, &DolphinNewFileMenu::deleteLater); - connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater); + connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, [newFileMenu, mainWindow = m_mainWindow](const QUrl &newDirectory) { + mainWindow->activeViewContainer()->view()->expandToUrl(newDirectory); + newFileMenu->deleteLater(); + }); connect(newFileMenu, &DolphinNewFileMenu::directoryCreationRejected, newFileMenu, &DolphinNewFileMenu::deleteLater); QMenu *menu = newFileMenu->menu(); diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 4d3fedbce..37f328e96 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -1821,6 +1821,9 @@ void DolphinMainWindow::setupActions() menu->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); m_newFileMenu->setPopupMode(QToolButton::InstantPopup); connect(menu, &QMenu::aboutToShow, this, &DolphinMainWindow::updateNewMenu); + connect(m_newFileMenu, &KNewFileMenu::directoryCreated, this, [this](const QUrl &createdDirectory) { + activeViewContainer()->view()->expandToUrl(createdDirectory); + }); QAction *newWindow = KStandardAction::openNew(this, &DolphinMainWindow::openNewMainWindow, actionCollection()); newWindow->setText(i18nc("@action:inmenu File", "New &Window")); diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index eb1d4e72a..383f45e9f 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -750,15 +750,41 @@ void KFileItemModel::expandParentDirectories(const QUrl &url) m_urlsToExpand.insert(urlToExpand); } + auto expandUrlAfterInserted = [this](const QUrl &url) { + // need to wait for the item to be added to the model + QMetaObject::Connection *connection = new QMetaObject::Connection; + *connection = connect(this, &KFileItemModel::itemsInserted, this, [this, url, connection](const KItemRangeList &ranges) { + int idx = -1; + for (const KItemRange &it : ranges) { + for (int i = 0; i < it.count; ++i) { + if (fileItem(it.index + i).url() == url) { + idx = it.index + i; + break; + } + } + if (idx != -1) { + break; + } + } + + if (idx != -1) { + setExpanded(idx, true); + disconnect(*connection); + delete connection; + } + }); + }; + // KDirLister::open() must called at least once to trigger an initial // loading. The pending URLs that must be restored are handled // in slotCompleted(). - QSetIterator<QUrl> it2(m_urlsToExpand); - while (it2.hasNext()) { - const int idx = index(it2.next()); - if (idx >= 0 && !isExpanded(idx)) { + for (const auto &url : std::as_const(m_urlsToExpand)) { + const int idx = index(url); + if (idx >= 0) { setExpanded(idx, true); - break; + } else { + // expand to url asynchronously + expandUrlAfterInserted(url); } } } diff --git a/src/tests/kfileitemmodeltest.cpp b/src/tests/kfileitemmodeltest.cpp index 4c6db590d..b8e0e9893 100644 --- a/src/tests/kfileitemmodeltest.cpp +++ b/src/tests/kfileitemmodeltest.cpp @@ -696,6 +696,9 @@ void KFileItemModelTest::testExpandParentItems() // Expand the parents of "a2/b2/c2". m_model->expandParentDirectories(QUrl::fromLocalFile(m_testDir->path() + "a2/b2/c2")); + // a first load is occuring for a2/b2/ + QVERIFY(loadingCompletedSpy.wait()); + // a second load for a2/b2/c2/ expansion QVERIFY(loadingCompletedSpy.wait()); // The model should now contain "a 1/", "a2/", "a2/b2/", and "a2/b2/c2/". diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index bc6759d4a..e471c198d 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -2725,4 +2725,11 @@ void DolphinView::setPreferredSortOrder(const QByteArray &role, Qt::SortOrder or m_rolesSortOrder[role] = order; } +void DolphinView::expandToUrl(const QUrl &directory) +{ + if (viewMode() == Mode::DetailsView && DetailsModeSettings::expandableFolders()) { + m_model->expandParentDirectories(directory); + } +} + #include "moc_dolphinview.cpp" diff --git a/src/views/dolphinview.h b/src/views/dolphinview.h index 55b6b5829..4918a7539 100644 --- a/src/views/dolphinview.h +++ b/src/views/dolphinview.h @@ -393,6 +393,11 @@ public: */ void setStatusBarOffset(int offset); + /** + * Expands to directory if in detail mode + */ + void expandToUrl(const QUrl &directory); + public Q_SLOTS: void reload(); |
