feat: select items after it has been pasted

This commit is contained in:
kate 2022-01-01 06:16:41 +08:00
parent 7c0188656b
commit 8319832a92
3 changed files with 68 additions and 27 deletions

View file

@ -144,32 +144,7 @@ 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();
auto it = m_dropTargetPositions.find(url.fileName());
if (it != m_dropTargetPositions.end()) {
const auto pos = it.value();
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();
}
});
});
connect(this, &QAbstractItemModel::rowsInserted, this, &FolderModel::onRowsInserted);
/*
* Dropped files may not actually show up as new files, e.g. when we overwrite
@ -972,6 +947,7 @@ void FolderModel::paste()
// Update paste action
if (QAction *paste = m_actionCollection.action(QStringLiteral("paste"))) {
QList<QUrl> urls = KUrlMimeData::urlsFromMimeData(mimeData);
const QString &currentUrl = rootItem().url().toLocalFile();
if (!urls.isEmpty()) {
if (!rootItem().isNull()) {
@ -979,6 +955,13 @@ void FolderModel::paste()
}
}
if (enable && !urls.isEmpty()) {
for (QUrl url : urls) {
m_needSelectUrls.append(QUrl::fromLocalFile(QString("%1/%2").arg(currentUrl)
.arg(url.fileName())));
}
}
paste->setEnabled(enable);
}
@ -990,6 +973,7 @@ void FolderModel::paste()
}
KIO::Job *job = KIO::paste(data, m_dirModel->dirLister()->url());
connect(job, &KIO::Job::finished, this, &FolderModel::delayUpdateNeedSelectUrls);
job->start();
// Clear system clipboard.
@ -1592,6 +1576,59 @@ void FolderModel::dragSelectedInternal(int x, int y)
}
}
void FolderModel::onRowsInserted(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();
auto it = m_dropTargetPositions.find(url.fileName());
if (it != m_dropTargetPositions.end()) {
const auto pos = it.value();
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();
}
});
}
void FolderModel::delayUpdateNeedSelectUrls()
{
QTimer::singleShot(100, this, &FolderModel::updateNeedSelectUrls);
}
void FolderModel::updateNeedSelectUrls()
{
QModelIndexList needSelectList;
for (const QUrl &url : m_needSelectUrls) {
const QModelIndex &idx = indexForUrl(url);
if (!idx.isValid())
continue;
needSelectList.append(idx);
}
m_needSelectUrls.clear();
for (const QModelIndex &idx : needSelectList) {
setSelected(idx.row());
}
}
bool FolderModel::isSupportThumbnails(const QString &mimeType) const
{
const QStringList supportsMimetypes = {"image/bmp", "image/png", "image/gif", "image/jpeg", "image/web",

View file

@ -260,6 +260,9 @@ signals:
private slots:
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
void dragSelectedInternal(int x, int y);
void onRowsInserted(const QModelIndex &parent, int first, int last);
void delayUpdateNeedSelectUrls();
void updateNeedSelectUrls();
private:
void invalidateIfComplete();
@ -284,6 +287,7 @@ private:
QItemSelection m_pinnedSelection;
QString m_url;
QUrl m_newDocumentUrl;
QList<QUrl> m_needSelectUrls;
Status m_status;
int m_sortMode;

View file

@ -45,7 +45,7 @@ Item {
Image {
id: _image
anchors.centerIn: parent
width: control.height * 0.64
width: 18
height: width
sourceSize: Qt.size(width, height)
smooth: false