Skip to content

Commit

Permalink
Rework how properties dialogs are handled
Browse files Browse the repository at this point in the history
1. Use hasString as a key instead of id
2. SHow and activate all existing dialogs instead of only the last one
3. Fix potential crash in finished() slot in case of dialog does not exist in mTorrentsDialogs
  • Loading branch information
equeim committed Jan 7, 2024
1 parent 4e33e07 commit 9ced7ef
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/ui/screens/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ namespace tremotesf {
TorrentsView mTorrentsView{&mTorrentsProxyModel};

MainWindowSideBar mSideBar{mViewModel.rpc(), &mTorrentsProxyModel};
std::unordered_map<int, TorrentPropertiesDialog*> mTorrentsDialogs{};
std::unordered_map<QString, TorrentPropertiesDialog*> mTorrentsDialogs{};

QAction mShowHideAppAction{};
//: Button / menu item to connect to server
Expand Down Expand Up @@ -953,24 +953,20 @@ namespace tremotesf {

void showTorrentsPropertiesDialogs() {
const QModelIndexList selectedRows(mTorrentsView.selectionModel()->selectedRows());

for (QModelIndexList::size_type i = 0, max = selectedRows.size(); i < max; i++) {
Torrent* torrent = mTorrentsModel.torrentAtIndex(mTorrentsProxyModel.sourceIndex(selectedRows.at(i)));
const int id = torrent->data().id;
if (mTorrentsDialogs.find(id) != mTorrentsDialogs.end()) {
if (i == (max - 1)) {
TorrentPropertiesDialog* dialog = mTorrentsDialogs[id];
showAndRaiseWindow(dialog);
activateWindow(dialog);
}
for (const auto& index : selectedRows) {
auto* const torrent = mTorrentsModel.torrentAtIndex(mTorrentsProxyModel.sourceIndex(index));
const auto hashString = torrent->data().hashString;
const auto existingDialog = mTorrentsDialogs.find(hashString);
if (existingDialog != mTorrentsDialogs.end()) {
showAndRaiseWindow(existingDialog->second);
activateWindow(existingDialog->second);
} else {
auto dialog = new TorrentPropertiesDialog(torrent, mViewModel.rpc(), mWindow);
dialog->setAttribute(Qt::WA_DeleteOnClose);
mTorrentsDialogs.insert({id, dialog});
QObject::connect(dialog, &TorrentPropertiesDialog::finished, this, [id, this] {
mTorrentsDialogs.erase(mTorrentsDialogs.find(id));
mTorrentsDialogs.emplace(hashString, dialog);
QObject::connect(dialog, &TorrentPropertiesDialog::finished, this, [hashString, this] {
mTorrentsDialogs.erase(hashString);
});

dialog->show();
}
}
Expand Down

0 comments on commit 9ced7ef

Please sign in to comment.