From 88c3ffbef8dfec4e492c5f934d03c2bc15bd4e16 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 19 Jun 2019 11:26:17 +0800 Subject: [PATCH] Avoid creating unnecessary event loops This is part 2. --- src/gui/optionsdialog.cpp | 14 +++--- src/gui/search/searchjobwidget.cpp | 45 ++++++++++-------- src/gui/search/searchjobwidget.h | 2 +- src/gui/torrentcategorydialog.cpp | 17 ++++--- src/gui/transferlistwidget.cpp | 73 +++++++++++++++++------------- 5 files changed, 85 insertions(+), 66 deletions(-) diff --git a/src/gui/optionsdialog.cpp b/src/gui/optionsdialog.cpp index 6d957a714..84d2e9e9a 100644 --- a/src/gui/optionsdialog.cpp +++ b/src/gui/optionsdialog.cpp @@ -1754,14 +1754,16 @@ bool OptionsDialog::isAlternativeWebUIPathValid() void OptionsDialog::on_banListButton_clicked() { - // call dialog window - if (BanListOptionsDialog(this).exec() == QDialog::Accepted) - enableApplyButton(); + auto dialog = new BanListOptionsDialog(this); + dialog->setAttribute(Qt::WA_DeleteOnClose); + connect(dialog, &QDialog::accepted, this, &OptionsDialog::enableApplyButton); + dialog->open(); } void OptionsDialog::on_IPSubnetWhitelistButton_clicked() { - // call dialog window - if (IPSubnetWhitelistOptionsDialog(this).exec() == QDialog::Accepted) - enableApplyButton(); + auto dialog = new IPSubnetWhitelistOptionsDialog(this); + dialog->setAttribute(Qt::WA_DeleteOnClose); + connect(dialog, &QDialog::accepted, this, &OptionsDialog::enableApplyButton); + dialog->open(); } diff --git a/src/gui/search/searchjobwidget.cpp b/src/gui/search/searchjobwidget.cpp index 25206a8d2..6dbc80ce6 100644 --- a/src/gui/search/searchjobwidget.cpp +++ b/src/gui/search/searchjobwidget.cpp @@ -405,39 +405,44 @@ void SearchJobWidget::saveSettings() const Preferences::instance()->setSearchTabHeaderState(header()->saveState()); } -void SearchJobWidget::displayToggleColumnsMenu(const QPoint&) +void SearchJobWidget::displayToggleColumnsMenu(const QPoint &) { - QMenu hideshowColumn(this); - hideshowColumn.setTitle(tr("Column visibility")); - QList actions; + auto menu = new QMenu(this); + menu->setAttribute(Qt::WA_DeleteOnClose); + menu->setTitle(tr("Column visibility")); + for (int i = 0; i < SearchSortModel::DL_LINK; ++i) { - QAction *myAct = hideshowColumn.addAction(m_searchListModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); + QAction *myAct = menu->addAction(m_searchListModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); myAct->setCheckable(true); myAct->setChecked(!m_ui->resultsBrowser->isColumnHidden(i)); - actions.append(myAct); + myAct->setData(i); } - int visibleCols = 0; - for (int i = 0; i < SearchSortModel::DL_LINK; ++i) { - if (!m_ui->resultsBrowser->isColumnHidden(i)) - ++visibleCols; - if (visibleCols > 1) - break; - } + connect(menu, &QMenu::triggered, this, [this](const QAction *action) + { + int visibleCols = 0; + for (int i = 0; i < SearchSortModel::DL_LINK; ++i) { + if (!m_ui->resultsBrowser->isColumnHidden(i)) + ++visibleCols; + + if (visibleCols > 1) + break; + } + + const int col = action->data().toInt(); - // Call menu - QAction *act = hideshowColumn.exec(QCursor::pos()); - if (act) { - int col = actions.indexOf(act); - Q_ASSERT(col >= 0); - Q_ASSERT(visibleCols > 0); if ((!m_ui->resultsBrowser->isColumnHidden(col)) && (visibleCols == 1)) return; + m_ui->resultsBrowser->setColumnHidden(col, !m_ui->resultsBrowser->isColumnHidden(col)); + if ((!m_ui->resultsBrowser->isColumnHidden(col)) && (m_ui->resultsBrowser->columnWidth(col) <= 5)) m_ui->resultsBrowser->resizeColumnToContents(col); + saveSettings(); - } + }); + + menu->popup(QCursor::pos()); } void SearchJobWidget::searchFinished(bool cancelled) diff --git a/src/gui/search/searchjobwidget.h b/src/gui/search/searchjobwidget.h index 777db8737..88f4c1a3f 100644 --- a/src/gui/search/searchjobwidget.h +++ b/src/gui/search/searchjobwidget.h @@ -99,7 +99,7 @@ private: void updateFilter(); void filterSearchResults(const QString &name); void showFilterContextMenu(const QPoint &); - void displayToggleColumnsMenu(const QPoint&); + void displayToggleColumnsMenu(const QPoint &); void onItemDoubleClicked(const QModelIndex &index); void searchFinished(bool cancelled); void searchFailed(); diff --git a/src/gui/torrentcategorydialog.cpp b/src/gui/torrentcategorydialog.cpp index e7d269af9..5a8a52d2f 100644 --- a/src/gui/torrentcategorydialog.cpp +++ b/src/gui/torrentcategorydialog.cpp @@ -89,13 +89,16 @@ void TorrentCategoryDialog::editCategory(QWidget *parent, const QString &categor Q_ASSERT(Session::instance()->categories().contains(categoryName)); - TorrentCategoryDialog dialog(parent); - dialog.setCategoryNameEditable(false); - dialog.setCategoryName(categoryName); - dialog.setSavePath(Session::instance()->categories()[categoryName]); - if (dialog.exec() == TorrentCategoryDialog::Accepted) { - Session::instance()->editCategory(categoryName, dialog.savePath()); - } + auto dialog = new TorrentCategoryDialog(parent); + dialog->setAttribute(Qt::WA_DeleteOnClose); + dialog->setCategoryNameEditable(false); + dialog->setCategoryName(categoryName); + dialog->setSavePath(Session::instance()->categories()[categoryName]); + connect(dialog, &TorrentCategoryDialog::accepted, parent, [dialog, categoryName]() + { + Session::instance()->editCategory(categoryName, dialog->savePath()); + }); + dialog->open(); } void TorrentCategoryDialog::setCategoryNameEditable(bool editable) diff --git a/src/gui/transferlistwidget.cpp b/src/gui/transferlistwidget.cpp index e548780b5..c7b6c6098 100644 --- a/src/gui/transferlistwidget.cpp +++ b/src/gui/transferlistwidget.cpp @@ -688,17 +688,22 @@ void TransferListWidget::setMaxRatioSelectedTorrents() useGlobalValue = (torrents[0]->ratioLimit() == BitTorrent::TorrentHandle::USE_GLOBAL_RATIO) && (torrents[0]->seedingTimeLimit() == BitTorrent::TorrentHandle::USE_GLOBAL_SEEDING_TIME); - UpDownRatioDialog dlg(useGlobalValue, currentMaxRatio, BitTorrent::TorrentHandle::MAX_RATIO, + auto dialog = new UpDownRatioDialog(useGlobalValue, currentMaxRatio, BitTorrent::TorrentHandle::MAX_RATIO, currentMaxSeedingTime, BitTorrent::TorrentHandle::MAX_SEEDING_TIME, this); - if (dlg.exec() != QDialog::Accepted) return; - - for (BitTorrent::TorrentHandle *const torrent : torrents) { - qreal ratio = (dlg.useDefault() ? BitTorrent::TorrentHandle::USE_GLOBAL_RATIO : dlg.ratio()); - torrent->setRatioLimit(ratio); - - int seedingTime = (dlg.useDefault() ? BitTorrent::TorrentHandle::USE_GLOBAL_SEEDING_TIME : dlg.seedingTime()); - torrent->setSeedingTimeLimit(seedingTime); - } + dialog->setAttribute(Qt::WA_DeleteOnClose); + connect(dialog, &QDialog::accepted, this, [this, dialog, torrents]() + { + for (BitTorrent::TorrentHandle *const torrent : torrents) { + const qreal ratio = (dialog->useDefault() + ? BitTorrent::TorrentHandle::USE_GLOBAL_RATIO : dialog->ratio()); + torrent->setRatioLimit(ratio); + + const int seedingTime = (dialog->useDefault() + ? BitTorrent::TorrentHandle::USE_GLOBAL_SEEDING_TIME : dialog->seedingTime()); + torrent->setSeedingTimeLimit(seedingTime); + } + }); + dialog->open(); } void TransferListWidget::recheckSelectedTorrents() @@ -721,41 +726,45 @@ void TransferListWidget::reannounceSelectedTorrents() // hide/show columns menu void TransferListWidget::displayDLHoSMenu(const QPoint&) { - QMenu hideshowColumn(this); - hideshowColumn.setTitle(tr("Column visibility")); - QList actions; + auto menu = new QMenu(this); + menu->setAttribute(Qt::WA_DeleteOnClose); + menu->setTitle(tr("Column visibility")); + for (int i = 0; i < m_listModel->columnCount(); ++i) { - if (!BitTorrent::Session::instance()->isQueueingSystemEnabled() && (i == TransferListModel::TR_PRIORITY)) { - actions.append(nullptr); + if (!BitTorrent::Session::instance()->isQueueingSystemEnabled() && (i == TransferListModel::TR_PRIORITY)) continue; - } - QAction *myAct = hideshowColumn.addAction(m_listModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); + + QAction *myAct = menu->addAction(m_listModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); myAct->setCheckable(true); myAct->setChecked(!isColumnHidden(i)); - actions.append(myAct); + myAct->setData(i); } - int visibleCols = 0; - for (int i = 0; i < TransferListModel::NB_COLUMNS; ++i) { - if (!isColumnHidden(i)) - ++visibleCols; - if (visibleCols > 1) - break; - } + connect(menu, &QMenu::triggered, this, [this](const QAction *action) + { + int visibleCols = 0; + for (int i = 0; i < TransferListModel::NB_COLUMNS; ++i) { + if (!isColumnHidden(i)) + ++visibleCols; + + if (visibleCols > 1) + break; + } + + const int col = action->data().toInt(); - // Call menu - QAction *act = hideshowColumn.exec(QCursor::pos()); - if (act) { - int col = actions.indexOf(act); - Q_ASSERT(col >= 0); - Q_ASSERT(visibleCols > 0); if (!isColumnHidden(col) && visibleCols == 1) return; + setColumnHidden(col, !isColumnHidden(col)); + if (!isColumnHidden(col) && columnWidth(col) <= 5) resizeColumnToContents(col); + saveSettings(); - } + }); + + menu->popup(QCursor::pos()); } void TransferListWidget::toggleSelectedTorrentsSuperSeeding() const