Skip to content

Commit

Permalink
fix: [workspace] select issue
Browse files Browse the repository at this point in the history
keep select file item state after resort files.

Log: fix select issue
Bug: https://pms.uniontech.com/bug-view-252921.html
  • Loading branch information
Lighto-Ku committed Apr 29, 2024
1 parent 6d31e97 commit 9cf2446
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,14 @@ void FileViewModel::onWorkFinish(int visiableCount, int totalCount)
closeCursorTimer();
}

void FileViewModel::onDataChanged(int first, int last)
{
QModelIndex firstIndex = index(first, 0, rootIndex());
QModelIndex lastIndex = index(last, 0, rootIndex());

Q_EMIT dataChanged(firstIndex, lastIndex);
}

void FileViewModel::connectRootAndFilterSortWork(RootInfo *root, const bool refresh)
{
if (filterSortWorker.isNull())
Expand Down Expand Up @@ -907,6 +915,7 @@ void FileViewModel::initFilterSortWork()
connect(filterSortWorker.data(), &FileSortWorker::insertFinish, this, &FileViewModel::onInsertFinish, Qt::QueuedConnection);
connect(filterSortWorker.data(), &FileSortWorker::removeRows, this, &FileViewModel::onRemove, Qt::QueuedConnection);
connect(filterSortWorker.data(), &FileSortWorker::removeFinish, this, &FileViewModel::onRemoveFinish, Qt::QueuedConnection);
connect(filterSortWorker.data(), &FileSortWorker::dataChanged, this, &FileViewModel::onDataChanged, Qt::QueuedConnection);
connect(filterSortWorker.data(), &FileSortWorker::requestFetchMore, this, [this]() {
canFetchFiles = true;
fetchingUrl = rootUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public Q_SLOTS:
void onSetCursorWait();
void onHiddenSettingChanged(bool value);
void onWorkFinish(int visiableCount, int totalCount);
void onDataChanged(int first, int last);

private:
void connectRootAndFilterSortWork(RootInfo *root, const bool refresh = false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ void FileSortWorker::handleResort(const Qt::SortOrder order, const ItemRoles sor
auto opt = setSortAgruments(order, sortRole, /*istree ? false :*/ isMixDirAndFile);
switch (opt) {
case FileSortWorker::SortOpt::kSortOptOtherChanged:
return filterAndSortFiles(current);
return resortCurrent(false);
case FileSortWorker::SortOpt::kSortOptOnlyOrderChanged:
return filterAndSortFiles(current, false, true);
return resortCurrent(true);
default:
return;
}
Expand Down Expand Up @@ -833,6 +833,23 @@ void FileSortWorker::filterAndSortFiles(const QUrl &dir, const bool fileter, con
}
}

void FileSortWorker::resortCurrent(const bool reverse)
{
if (isCanceled)
return;

QList<QUrl> visibleList;

// 执行排序
if (istree)
visibleList = sortAllTreeFilesByParent(current, reverse);
else {
visibleList = sortTreeFiles(visibleTreeChildren.contains(current)? visibleTreeChildren[current] : visibleChildren, reverse);
}

resortVisibleChildren(visibleList);
}

QList<QUrl> FileSortWorker::filterFilesByParent(const QUrl &dir, const bool byInfo)
{
// 先排深度是0的url
Expand Down Expand Up @@ -1237,34 +1254,24 @@ int FileSortWorker::findStartPos(const QList<QUrl> &list, const QUrl &parent)
return pos < 0 ? pos : pos + 1;
}

void FileSortWorker::resortVisibleChildren(const QList<QUrl> &fileUrls)
{
if (isCanceled)
return;

int count = setVisibleChildren(0, fileUrls, InsertOpt::kInsertOptForce, -1);
if (count > 0)
Q_EMIT dataChanged(0, count - 1);
}

void FileSortWorker::insertVisibleChildren(const int startPos, const QList<QUrl> &filterUrls,
const InsertOpt opt, const int endPos)
{
if (isCanceled)
return;

Q_EMIT insertRows(startPos, filterUrls.length());
{
QList<QUrl> visibleList;
if (opt == InsertOpt::kInsertOptForce) {
visibleList = filterUrls;
} else {
auto tmp = getChildrenUrls();
visibleList.append(tmp.mid(0, startPos));
visibleList.append(filterUrls);
if (opt == InsertOpt::kInsertOptReplace) {
visibleList.append(tmp.mid(endPos != -1 ? endPos : startPos + filterUrls.length()));
} else if (opt == InsertOpt::kInsertOptAppend) {
visibleList.append(tmp.mid(startPos));
}
}

if (isCanceled)
return;

QWriteLocker lk(&locker);
visibleChildren = visibleList;
}
setVisibleChildren(startPos, filterUrls, opt, endPos);
Q_EMIT insertFinish();
}

Expand Down Expand Up @@ -1632,3 +1639,28 @@ int FileSortWorker::indexOfVisibleChild(const QUrl &itemUrl)
QReadLocker lk(&locker);
return visibleChildren.indexOf(itemUrl);
}

int FileSortWorker::setVisibleChildren(const int startPos, const QList<QUrl> &filterUrls, const FileSortWorker::InsertOpt opt, const int endPos)
{
QList<QUrl> visibleList;
if (opt == InsertOpt::kInsertOptForce) {
visibleList = filterUrls;
} else {
auto tmp = getChildrenUrls();
visibleList.append(tmp.mid(0, startPos));
visibleList.append(filterUrls);
if (opt == InsertOpt::kInsertOptReplace) {
visibleList.append(tmp.mid(endPos != -1 ? endPos : startPos + filterUrls.length()));
} else if (opt == InsertOpt::kInsertOptAppend) {
visibleList.append(tmp.mid(startPos));
}
}

if (isCanceled)
return -1;

QWriteLocker lk(&locker);
visibleChildren = visibleList;

return visibleList.length();
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class FileSortWorker : public QObject
void removeFinish();
void requestFetchMore();
void selectAndEditFile(const QUrl &url);
void dataChanged(int first, int last);

void requestSetIdel(int visiableCount, int totalCount);
void updateRow(int row);
Expand Down Expand Up @@ -158,6 +159,7 @@ public slots:
void filterAllFilesOrdered();
void filterAndSortFiles(const QUrl &dir, const bool fileter = false,
const bool reverse = false);
void resortCurrent(const bool reverse);
QList<QUrl> filterFilesByParent(const QUrl &dir, const bool byInfo = false);
void filterTreeDirFiles(const QUrl &parent, const bool byInfo = false);

Expand All @@ -179,6 +181,7 @@ public slots:
int findStartPos(const QUrl &parent);
int findStartPos(const QList<QUrl> &list, const QUrl &parent);

void resortVisibleChildren(const QList<QUrl> &fileUrls);
void insertVisibleChildren(const int startPos, const QList<QUrl> &filterUrls,
const InsertOpt opt = InsertOpt::kInsertOptAppend, const int endPos = -1);
void removeVisibleChildren(const int startPos, const int size);
Expand All @@ -196,6 +199,8 @@ public slots:
int8_t getDepth(const QUrl &url);
int findRealShowIndex(const QUrl &preItemUrl);
int indexOfVisibleChild(const QUrl &itemUrl);
int setVisibleChildren(const int startPos, const QList<QUrl> &filterUrls,
const InsertOpt opt = InsertOpt::kInsertOptAppend, const int endPos = -1);

private:
QUrl current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ bool SelectHelper::select(const QList<QUrl> &urls)
return true;
}

void SelectHelper::saveSelectedFilesList(const QUrl &current, const QList<QUrl> &urls)
{
currentSelectedFile = current;
selectedFiles = urls;
}

void SelectHelper::resortSelectFiles()
{
if (selectedFiles.isEmpty() || !currentSelectedFile.isValid())
return;

select(selectedFiles);
view->selectionModel()->setCurrentIndex(view->model()->getIndexByUrl(currentSelectedFile), QItemSelectionModel::Select);

/// Clean
currentSelectedFile = QUrl();
selectedFiles.clear();
}

void SelectHelper::caculateSelection(const QRect &rect, QItemSelection *selection)
{
if (view->isIconViewMode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class SelectHelper : public QObject
void selection(const QRect &rect, QItemSelectionModel::SelectionFlags flags);
bool select(const QList<QUrl> &urls);

void saveSelectedFilesList(const QUrl &current, const QList<QUrl> &urls);
void resortSelectFiles();
private:
void caculateSelection(const QRect &rect, QItemSelection *selection);
void caculateIconViewSelection(const QRect &rect, QItemSelection *selection);
Expand All @@ -46,6 +48,9 @@ class SelectHelper : public QObject
QModelIndex currentPressedIndex;
QItemSelection currentSelection;
QItemSelection lastSelection;

QList<QUrl> selectedFiles {};
QUrl currentSelectedFile {};
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ void FileView::stopWork()
model()->stopTraversWork();
}

void FileView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
DListView::dataChanged(topLeft, bottomRight, roles);

d->selectHelper->resortSelectFiles();
}

int FileView::getColumnWidth(const int &column) const
{
if (d->headerView)
Expand Down Expand Up @@ -429,6 +436,15 @@ void FileView::onHeaderHiddenChanged(const QString &roleName, const bool isHidde

void FileView::onSortIndicatorChanged(int logicalIndex, Qt::SortOrder order)
{
auto selectedUrls = selectedUrlList();

if (!selectedUrls.isEmpty()) {
const QUrl curFile = model()->data(currentIndex(), ItemRoles::kItemUrlRole).toUrl();
d->selectHelper->saveSelectedFilesList(curFile, selectedUrlList());
}

clearSelection();

model()->sort(logicalIndex, order);

const QUrl &url = rootUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class FileView final : public DListView, public DFMBASE_NAMESPACE::AbstractBaseV
void setModel(QAbstractItemModel *model) override;
void stopWork();

void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
const QVector<int> &roles = QVector<int>()) override;
QModelIndex indexAt(const QPoint &pos) const override;
virtual QRect visualRect(const QModelIndex &index) const override;
void setIconSize(const QSize &size);
Expand Down

0 comments on commit 9cf2446

Please sign in to comment.