Skip to content

Commit

Permalink
fix: Copying a large number of empty files and folders on a USB drive…
Browse files Browse the repository at this point in the history
… causes the system to freeze

It is because the efficiency of selecting a large number of files is low, and inserting items at this time will cause all events of QT to freeze.

Log: Copying a large number of empty files and folders on a USB drive causes the system to freeze
Bug: https://pms.uniontech.com/bug-view-236699.html
  • Loading branch information
liyigang1 committed Dec 27, 2023
1 parent a597233 commit 7176c8a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,36 +103,32 @@ void SelectHelper::selection(const QRect &rect, QItemSelectionModel::SelectionFl

void SelectHelper::select(const QList<QUrl> &urls)
{
QList<QModelIndex> indexes {};
for (const QUrl &url : urls) {
const QModelIndex &index = view->model()->getIndexByUrl(url);
indexes << index;
}

select(indexes);
}
if (urls.isEmpty())
return;

void SelectHelper::select(const QList<QModelIndex> &indexes)
{
QModelIndex firstIndex;
QModelIndex lastIndex;

const QModelIndex &root = view->rootIndex();
view->selectionModel()->clearSelection();
view->setCurrentIndex(QModelIndex());
for (const QModelIndex &index : indexes) {
QList<QModelIndex> indexes {};
QItemSelection selction;
for (const QUrl &url : urls) {
const QModelIndex &index = view->model()->getIndexByUrl(url);

if (!index.isValid() || index == root) {
continue;
}

view->selectionModel()->select(index, QItemSelectionModel::Select);
selction.merge(QItemSelection(index,index), QItemSelectionModel::Select);

if (!firstIndex.isValid())
firstIndex = index;

lastIndex = index;
}

view->selectionModel()->select(selction, QItemSelectionModel::Select);

if (lastIndex.isValid())
view->selectionModel()->setCurrentIndex(lastIndex, QItemSelectionModel::Select);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class SelectHelper : public QObject
void setSelection(const QItemSelection &selection);
void selection(const QRect &rect, QItemSelectionModel::SelectionFlags flags);
void select(const QList<QUrl> &urls);
void select(const QList<QModelIndex> &indexes);

private:
void caculateSelection(const QRect &rect, QItemSelection *selection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ QStringList WorkspaceHelper::getNameFilter(const quint64 windowId)

void WorkspaceHelper::laterRequestSelectFiles(const QList<QUrl> &urls)
{
QTimer::singleShot(qMax(urls.count() * (10 + urls.count() / 150), 200), this, [=] {
QTimer::singleShot(qMin(800, qMax(urls.count() * (10 + urls.count() / 150), 200)), this, [=] {
emit requestSelectFiles(urls);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,6 @@ void FileView::selectFiles(const QList<QUrl> &files) const
d->selectHelper->select(files);
}

void FileView::selectIndexes(const QList<QModelIndex> &indexes) const
{
d->selectHelper->select(indexes);
}

void FileView::setSelectionMode(const QAbstractItemView::SelectionMode mode)
{
if (d->enabledSelectionModes.contains(mode))
Expand Down Expand Up @@ -1925,17 +1920,8 @@ void FileView::updateSelectedUrl()
if (d->preSelectionUrls.isEmpty() || model()->currentState() != ModelState::kIdle)
return;

QList<QModelIndex> indexes {};
for (const QUrl &url : d->preSelectionUrls) {
const QModelIndex &index = model()->getIndexByUrl(url);
if (index.isValid())
indexes << index;
}

if (!indexes.isEmpty()) {
selectIndexes(indexes);
d->preSelectionUrls.clear();
}
selectFiles(d->preSelectionUrls);
d->preSelectionUrls.clear();
}

void FileView::updateListHeaderView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class FileView final : public DListView, public DFMBASE_NAMESPACE::AbstractBaseV
bool isSelected(const QModelIndex &index) const;
int selectedIndexCount() const;
void selectFiles(const QList<QUrl> &files) const;
void selectIndexes(const QList<QModelIndex> &indexes) const;
void setSelectionMode(const SelectionMode mode);
void reverseSelect() const;
void setEnabledSelectionModes(const QList<SelectionMode> &modes);
Expand Down

0 comments on commit 7176c8a

Please sign in to comment.