feat: new folder new interaction, no popup dialog

This commit is contained in:
kateleet 2021-12-26 22:38:12 +08:00
parent add106c57d
commit f7ff4e1c77
5 changed files with 64 additions and 4 deletions

View file

@ -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<QUrl> 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)

View file

@ -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<QUrl> 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<QUrl> 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

View file

@ -47,6 +47,10 @@ Item {
isDesktop: true
sortMode: -1
viewAdapter: viewAdapter
onCurrentIndexChanged: {
_folderView.currentIndex = dirModel.currentIndex
}
}
FM.ItemViewAdapter {

View file

@ -53,7 +53,6 @@ ListView {
signal keyPress(var event)
currentIndex: -1
clip: true
cacheBuffer: width

View file

@ -164,6 +164,11 @@ Item {
else
dirModel.url = dirModel.homePath()
}
// For new folder rename.
onCurrentIndexChanged: {
_viewLoader.item.currentIndex = dirModel.currentIndex
}
}
Connections {