┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/views
diff options
context:
space:
mode:
authorMéven Car <[email protected]>2025-11-02 15:49:47 +0100
committerMéven Car <[email protected]>2025-11-21 13:25:55 +0000
commit60e109632fd63b335ca1cc037c5d5c3e291349f5 (patch)
tree70f824fee05fbad2fc3de6d086900055ead749d4 /src/views
parente114f654ed1216aaa64593354ca1306a53fefef0 (diff)
context menu: use selected item as containing folder for New file menu
Use current view url as fallback. Don't update the selection after a directory is created, except if there was no selection. CCBUG: 508196 BUG: 512020
Diffstat (limited to 'src/views')
-rw-r--r--src/views/dolphinnewfilemenuobserver.cpp4
-rw-r--r--src/views/dolphinnewfilemenuobserver.h1
-rw-r--r--src/views/dolphinview.cpp81
-rw-r--r--src/views/dolphinview.h9
4 files changed, 92 insertions, 3 deletions
diff --git a/src/views/dolphinnewfilemenuobserver.cpp b/src/views/dolphinnewfilemenuobserver.cpp
index fa549cf84..d0cd40840 100644
--- a/src/views/dolphinnewfilemenuobserver.cpp
+++ b/src/views/dolphinnewfilemenuobserver.cpp
@@ -23,14 +23,14 @@ DolphinNewFileMenuObserver &DolphinNewFileMenuObserver::instance()
void DolphinNewFileMenuObserver::attach(const DolphinNewFileMenu *menu)
{
connect(menu, &DolphinNewFileMenu::fileCreated, this, &DolphinNewFileMenuObserver::itemCreated);
- connect(menu, &DolphinNewFileMenu::directoryCreated, this, &DolphinNewFileMenuObserver::itemCreated);
+ connect(menu, &DolphinNewFileMenu::directoryCreated, this, &DolphinNewFileMenuObserver::directoryCreated);
connect(menu, &DolphinNewFileMenu::errorMessage, this, &DolphinNewFileMenuObserver::errorMessage);
}
void DolphinNewFileMenuObserver::detach(const DolphinNewFileMenu *menu)
{
disconnect(menu, &DolphinNewFileMenu::fileCreated, this, &DolphinNewFileMenuObserver::itemCreated);
- disconnect(menu, &DolphinNewFileMenu::directoryCreated, this, &DolphinNewFileMenuObserver::itemCreated);
+ disconnect(menu, &DolphinNewFileMenu::directoryCreated, this, &DolphinNewFileMenuObserver::directoryCreated);
disconnect(menu, &DolphinNewFileMenu::errorMessage, this, &DolphinNewFileMenuObserver::errorMessage);
}
diff --git a/src/views/dolphinnewfilemenuobserver.h b/src/views/dolphinnewfilemenuobserver.h
index 23bd6a836..308837fb9 100644
--- a/src/views/dolphinnewfilemenuobserver.h
+++ b/src/views/dolphinnewfilemenuobserver.h
@@ -31,6 +31,7 @@ public:
Q_SIGNALS:
void itemCreated(const QUrl &url);
+ void directoryCreated(const QUrl &url);
void errorMessage(const QString &error);
private:
diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp
index 4c909caca..70b32968a 100644
--- a/src/views/dolphinview.cpp
+++ b/src/views/dolphinview.cpp
@@ -113,6 +113,7 @@ DolphinView::DolphinView(const QUrl &url, QWidget *parent)
// get selected and it must be assured that the item will get visible. As the
// creation is done asynchronously, several signals must be checked:
connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::itemCreated, this, &DolphinView::observeCreatedItem);
+ connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::directoryCreated, this, &DolphinView::observeCreatedDirectory);
m_selectionChangedTimer = new QTimer(this);
m_selectionChangedTimer->setSingleShot(true);
@@ -431,6 +432,15 @@ KFileItemList DolphinView::selectedItems() const
return selectedItems;
}
+std::optional<KFileItem> DolphinView::firstSelectedItem() const
+{
+ const KItemListSelectionManager *selectionManager = m_container->controller()->selectionManager();
+ if (selectionManager->selectedItems().count() == 1) {
+ return {m_model->fileItem(selectionManager->selectedItems().first())};
+ }
+ return std::nullopt;
+}
+
int DolphinView::selectedItemsCount() const
{
const KItemListSelectionManager *selectionManager = m_container->controller()->selectionManager();
@@ -1780,11 +1790,80 @@ void DolphinView::resetZoomLevel()
setZoomLevel(ZoomLevelInfo::zoomLevelForIconSize(QSize(userDefaultIconSize, userDefaultIconSize)));
}
+void DolphinView::selectFileOnceAvailable(const QUrl &url, std::function<bool()> condition)
+{
+ // need to wait for the item to be added to the model
+ QMetaObject::Connection *connection = new QMetaObject::Connection;
+ *connection = connect(m_model, &KFileItemModel::itemsInserted, this, [this, url, connection, condition](const KItemRangeList &ranges) {
+ bool found = false;
+ for (const KItemRange &it : ranges) {
+ for (int i = 0; i < it.count; ++i) {
+ if (m_model->fileItem(it.index + i).url() == url) {
+ found = true;
+ break;
+ }
+ }
+ if (found) {
+ break;
+ }
+ }
+ // check whether the selection should be changed
+ if (condition()) {
+ forceUrlsSelection(url, {url});
+ }
+ if (found) {
+ disconnect(*connection);
+ delete connection;
+ }
+ });
+}
+
+void DolphinView::observeCreatedDirectory(const QUrl &url)
+{
+ if (!m_active) {
+ return;
+ }
+
+ // if there was no selection but a new directory was created
+ if (m_container->controller()->selectionManager()->hasSelection()) {
+ return;
+ }
+
+ // select the new directory
+ if (!m_model->fileItem(url).isNull()) {
+ forceUrlsSelection(url, {url});
+ return;
+ }
+
+ // since this is async make sure the selection state hasn't change in the meantime
+ std::function<bool()> condition([this]() {
+ return !m_container->controller()->selectionManager()->hasSelection();
+ });
+
+ // need to wait for the item to be added to the model
+ selectFileOnceAvailable(url, condition);
+}
+
void DolphinView::observeCreatedItem(const QUrl &url)
{
- if (m_active) {
+ if (!m_active) {
+ return;
+ }
+
+ // select the new file
+ if (!m_model->fileItem(url).isNull()) {
forceUrlsSelection(url, {url});
+ return;
}
+
+ // since this is async make sure the selection state hasn't change in the meantime
+ auto selection = m_container->controller()->selectionManager()->selectedItems();
+ std::function<bool()> condition([this, selection]() {
+ return selection == m_container->controller()->selectionManager()->selectedItems();
+ });
+
+ // need to wait for the item to be added to the model
+ selectFileOnceAvailable(url, condition);
}
void DolphinView::slotDirectoryRedirection(const QUrl &oldUrl, const QUrl &newUrl)
diff --git a/src/views/dolphinview.h b/src/views/dolphinview.h
index 4a400ce2a..55b6b5829 100644
--- a/src/views/dolphinview.h
+++ b/src/views/dolphinview.h
@@ -167,6 +167,11 @@ public:
int selectedItemsCount() const;
/**
+ * Returns the selected kfileitem if one is selected and only one
+ */
+ std::optional<KFileItem> firstSelectedItem() const;
+
+ /**
* Marks the items indicated by \p urls to get selected after the
* directory DolphinView::url() has been loaded. Note that nothing
* gets selected if no loading of a directory has been triggered
@@ -854,6 +859,8 @@ private Q_SLOTS:
*/
void observeCreatedItem(const QUrl &url);
+ void observeCreatedDirectory(const QUrl &url);
+
/**
* Selects the next item after prev selection deleted/trashed
*/
@@ -953,6 +960,8 @@ private:
bool tryShowNameToolTip(QHelpEvent *event);
+ void selectFileOnceAvailable(const QUrl &url, std::function<bool()> condition);
+
private:
void updatePalette();
void showLoadingPlaceholder();