From f7ff4e1c77d1d608c52dcb1c29d2de198a368dd6 Mon Sep 17 00:00:00 2001 From: kateleet Date: Sun, 26 Dec 2021 22:38:12 +0800 Subject: [PATCH] feat: new folder new interaction, no popup dialog --- model/foldermodel.cpp | 49 +++++++++++++++++++++++++++++++++++++++--- model/foldermodel.h | 9 ++++++++ qml/Desktop/Main.qml | 4 ++++ qml/FolderListView.qml | 1 - qml/FolderPage.qml | 5 +++++ 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/model/foldermodel.cpp b/model/foldermodel.cpp index 21ed5ad..cc34e97 100644 --- a/model/foldermodel.cpp +++ b/model/foldermodel.cpp @@ -105,6 +105,7 @@ FolderModel::FolderModel(QObject *parent) , m_viewAdapter(nullptr) , m_mimeAppManager(MimeAppManager::self()) , m_sizeJob(nullptr) + , m_currentIndex(-1) { QSettings settings("cutefishos", qApp->applicationName()); m_showHiddenFiles = settings.value("showHiddenFiles", false).toBool(); @@ -144,6 +145,8 @@ FolderModel::FolderModel(QObject *parent) // Position dropped items at the desired target position. connect(this, &QAbstractItemModel::rowsInserted, this, [this](const QModelIndex &parent, int first, int last) { + QModelIndex changeIdx; + for (int i = first; i <= last; ++i) { const auto idx = index(i, 0, parent); const auto url = itemForIndex(idx).url(); @@ -153,7 +156,19 @@ FolderModel::FolderModel(QObject *parent) m_dropTargetPositions.erase(it); Q_EMIT move(pos.x(), pos.y(), {url}); } + + if (url == m_newDocumentUrl) { + changeIdx = idx; + m_newDocumentUrl.clear(); + } } + + QTimer::singleShot(0, this, [=] { + if (changeIdx.isValid()) { + setSelected(changeIdx.row()); + emit requestRename(); + } + }); }); /* @@ -334,6 +349,11 @@ KFileItem FolderModel::itemForIndex(const QModelIndex &index) const return m_dirModel->itemForIndex(mapToSource(index)); } +QModelIndex FolderModel::indexForUrl(const QUrl &url) const +{ + return m_dirModel->indexForUrl(url); +} + KFileItem FolderModel::fileItem(int index) const { if (index >= 0 && index < count()) { @@ -357,6 +377,11 @@ QList FolderModel::selectedUrls() const return urls; } +int FolderModel::currentIndex() const +{ + return m_currentIndex; +} + QString FolderModel::url() const { return m_url; @@ -797,6 +822,9 @@ void FolderModel::setSelected(int row) return; m_selectionModel->select(index(row, 0), QItemSelectionModel::Select); + m_currentIndex = row; + + emit currentIndexChanged(); } void FolderModel::selectAll() @@ -866,9 +894,24 @@ void FolderModel::unpinSelection() void FolderModel::newFolder() { - CreateFolderDialog *dlg = new CreateFolderDialog; - dlg->setPath(rootItem().url().toString()); - dlg->show(); + QString rootPath = rootItem().url().toString(); + QString baseName = tr("New Folder"); + QString newName = baseName; + + int i = 0; + while (true) { + if (QFile::exists(rootItem().url().toLocalFile() + "/" + newName)) { + ++i; + newName = QString("%1%2").arg(baseName).arg(QString::number(i)); + } else { + break; + } + } + + m_newDocumentUrl = QUrl(rootItem().url().toString() + "/" + newName); + + auto job = KIO::mkdir(QUrl(rootItem().url().toString() + "/" + newName)); + job->start(); } void FolderModel::rename(int row, const QString &name) diff --git a/model/foldermodel.h b/model/foldermodel.h index 9e4817a..ce1811c 100644 --- a/model/foldermodel.h +++ b/model/foldermodel.h @@ -61,6 +61,7 @@ class FolderModel : public QSortFilterProxyModel, public QQmlParserStatus Q_PROPERTY(QStringList filterMimeTypes READ filterMimeTypes WRITE setFilterMimeTypes NOTIFY filterMimeTypesChanged) Q_PROPERTY(QString selectedItemSize READ selectedItemSize NOTIFY selectedItemSizeChanged) Q_PROPERTY(bool showHiddenFiles READ showHiddenFiles WRITE setShowHiddenFiles NOTIFY showHiddenFilesChanged) + Q_PROPERTY(int currentIndex READ currentIndex NOTIFY currentIndexChanged) public: enum DataRole { @@ -113,11 +114,14 @@ public: int indexForKeyboardSearch(const QString &text, int startFromIndex = 0) const; KFileItem itemForIndex(const QModelIndex &index) const; + QModelIndex indexForUrl(const QUrl &url) const; KFileItem fileItem(int index) const; QList selectedUrls() const; + int currentIndex() const; + QString url() const; void setUrl(const QString &url); @@ -250,6 +254,8 @@ signals: void notification(const QString &message); void move(int x, int y, QList urls); + void currentIndexChanged(); + private slots: void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); void dragSelectedInternal(int x, int y); @@ -276,6 +282,7 @@ private: QItemSelectionModel *m_selectionModel; QItemSelection m_pinnedSelection; QString m_url; + QUrl m_newDocumentUrl; Status m_status; int m_sortMode; @@ -312,6 +319,8 @@ private: MimeAppManager *m_mimeAppManager; CFileSizeJob *m_sizeJob; + + int m_currentIndex; }; #endif // FOLDERMODEL_H diff --git a/qml/Desktop/Main.qml b/qml/Desktop/Main.qml index c3c4fc5..e722aad 100644 --- a/qml/Desktop/Main.qml +++ b/qml/Desktop/Main.qml @@ -47,6 +47,10 @@ Item { isDesktop: true sortMode: -1 viewAdapter: viewAdapter + + onCurrentIndexChanged: { + _folderView.currentIndex = dirModel.currentIndex + } } FM.ItemViewAdapter { diff --git a/qml/FolderListView.qml b/qml/FolderListView.qml index 283880f..a6914fd 100644 --- a/qml/FolderListView.qml +++ b/qml/FolderListView.qml @@ -53,7 +53,6 @@ ListView { signal keyPress(var event) - currentIndex: -1 clip: true cacheBuffer: width diff --git a/qml/FolderPage.qml b/qml/FolderPage.qml index 2b45ed6..0ad50be 100644 --- a/qml/FolderPage.qml +++ b/qml/FolderPage.qml @@ -164,6 +164,11 @@ Item { else dirModel.url = dirModel.homePath() } + + // For new folder rename. + onCurrentIndexChanged: { + _viewLoader.item.currentIndex = dirModel.currentIndex + } } Connections {