- // Get all distinct directories from 'files' and open a tab
- // for each directory. If the "split view" option is enabled, two
- // directories are shown inside one tab (see openDirectories()).
- QList<QUrl> dirs;
- for (const QUrl& url : files) {
- const QUrl dir(url.adjusted(QUrl::RemoveFilename));
- if (!dirs.contains(dir)) {
- dirs.append(dir);
+ // Get all distinct directories from 'files'.
+ QList<QUrl> dirsThatNeedToBeOpened;
+ QList<QUrl> dirsThatWereAlreadyOpen;
+ for (const QUrl &file : files) {
+ const QUrl dir(file.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash));
+ if (dirsThatNeedToBeOpened.contains(dir) || dirsThatWereAlreadyOpen.contains(dir)) {
+ continue;
+ }
+
+ // The selecting of files that we do later will not work in views that already have items selected.
+ // So we check if dir is already open and clear the selection if it is. BUG: 417230
+ // We also make sure the view will be activated.
+ auto viewIndex = viewShowingItem(file);
+ if (viewIndex.has_value()) {
+ viewContainerAt(viewIndex.value())->view()->clearSelection();
+ activateViewContainerAt(viewIndex.value());
+ dirsThatWereAlreadyOpen.append(dir);
+ } else {
+ dirsThatNeedToBeOpened.append(dir);