Improve coding style

This commit is contained in:
Vladimir Golovnev (Glassez) 2020-11-16 10:02:11 +03:00
parent acad35c5bc
commit c41df9ffbd
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
147 changed files with 4454 additions and 2227 deletions

View file

@ -90,7 +90,8 @@ namespace
if (!torrent->hasMetadata())
return false;
for (int i = 0; i < torrent->filesCount(); ++i) {
for (int i = 0; i < torrent->filesCount(); ++i)
{
if (Utils::Misc::isPreviewable(Utils::Fs::fileExtension(torrent->fileName(i))))
return true;
}
@ -159,7 +160,8 @@ TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *mainWindow)
header()->setStretchLastSection(false);
// Default hidden columns
if (!columnLoaded) {
if (!columnLoaded)
{
setColumnHidden(TransferListModel::TR_ADD_DATE, true);
setColumnHidden(TransferListModel::TR_SEED_DATE, true);
setColumnHidden(TransferListModel::TR_UPLIMIT, true);
@ -181,8 +183,10 @@ TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *mainWindow)
//Ensure that at least one column is visible at all times
bool atLeastOne = false;
for (int i = 0; i < TransferListModel::NB_COLUMNS; ++i) {
if (!isColumnHidden(i)) {
for (int i = 0; i < TransferListModel::NB_COLUMNS; ++i)
{
if (!isColumnHidden(i))
{
atLeastOne = true;
break;
}
@ -275,7 +279,8 @@ void TransferListWidget::torrentDoubleClicked()
else
action = Preferences::instance()->getActionOnDblClOnTorrentDl();
switch (action) {
switch (action)
{
case TOGGLE_PAUSE:
if (torrent->isPaused())
torrent->resume();
@ -283,13 +288,15 @@ void TransferListWidget::torrentDoubleClicked()
torrent->pause();
break;
case PREVIEW_FILE:
if (torrentContainsPreviewableFiles(torrent)) {
if (torrentContainsPreviewableFiles(torrent))
{
auto *dialog = new PreviewSelectDialog(this, torrent);
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, &PreviewSelectDialog::readyToPreviewFile, this, &TransferListWidget::previewFile);
dialog->show();
}
else {
else
{
openDestinationFolder(torrent);
}
break;
@ -395,7 +402,8 @@ void TransferListWidget::deleteSelectedTorrents(const bool deleteLocalFiles)
const QVector<BitTorrent::TorrentHandle *> torrents = getSelectedTorrents();
if (torrents.empty()) return;
if (Preferences::instance()->confirmTorrentDeletion()) {
if (Preferences::instance()->confirmTorrentDeletion())
{
auto *dialog = new DeletionConfirmationDialog(this, torrents.size(), torrents[0]->name(), deleteLocalFiles);
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, &DeletionConfirmationDialog::accepted, this, [this, dialog]()
@ -406,7 +414,8 @@ void TransferListWidget::deleteSelectedTorrents(const bool deleteLocalFiles)
});
dialog->open();
}
else {
else
{
removeTorrents(torrents, deleteLocalFiles);
}
}
@ -416,7 +425,8 @@ void TransferListWidget::deleteVisibleTorrents()
const QVector<BitTorrent::TorrentHandle *> torrents = getVisibleTorrents();
if (torrents.empty()) return;
if (Preferences::instance()->confirmTorrentDeletion()) {
if (Preferences::instance()->confirmTorrentDeletion())
{
auto *dialog = new DeletionConfirmationDialog(this, torrents.size(), torrents[0]->name(), false);
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, &DeletionConfirmationDialog::accepted, this, [this, dialog]()
@ -427,7 +437,8 @@ void TransferListWidget::deleteVisibleTorrents()
});
dialog->open();
}
else {
else
{
removeTorrents(torrents, false);
}
}
@ -498,15 +509,18 @@ void TransferListWidget::openSelectedTorrentsFolder() const
#ifdef Q_OS_MACOS
// On macOS you expect both the files and folders to be opened in their parent
// folders prehilighted for opening, so we use a custom method.
for (BitTorrent::TorrentHandle *const torrent : asConst(getSelectedTorrents())) {
for (BitTorrent::TorrentHandle *const torrent : asConst(getSelectedTorrents()))
{
QString path = torrent->contentPath(true);
pathsList.insert(path);
}
MacUtils::openFiles(pathsList);
#else
for (BitTorrent::TorrentHandle *const torrent : asConst(getSelectedTorrents())) {
for (BitTorrent::TorrentHandle *const torrent : asConst(getSelectedTorrents()))
{
QString path = torrent->contentPath(true);
if (!pathsList.contains(path)) {
if (!pathsList.contains(path))
{
if (torrent->filesCount() == 1)
Utils::Gui::openFolderSelect(path);
else
@ -519,14 +533,17 @@ void TransferListWidget::openSelectedTorrentsFolder() const
void TransferListWidget::previewSelectedTorrents()
{
for (const BitTorrent::TorrentHandle *torrent : asConst(getSelectedTorrents())) {
if (torrentContainsPreviewableFiles(torrent)) {
for (const BitTorrent::TorrentHandle *torrent : asConst(getSelectedTorrents()))
{
if (torrentContainsPreviewableFiles(torrent))
{
auto *dialog = new PreviewSelectDialog(this, torrent);
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, &PreviewSelectDialog::readyToPreviewFile, this, &TransferListWidget::previewFile);
dialog->show();
}
else {
else
{
QMessageBox::critical(this, tr("Unable to preview"), tr("The selected torrent \"%1\" does not contain previewable files")
.arg(torrent->name()));
}
@ -536,7 +553,8 @@ void TransferListWidget::previewSelectedTorrents()
void TransferListWidget::setDlLimitSelectedTorrents()
{
QVector<BitTorrent::TorrentHandle *> torrentsList;
for (BitTorrent::TorrentHandle *const torrent : asConst(getSelectedTorrents())) {
for (BitTorrent::TorrentHandle *const torrent : asConst(getSelectedTorrents()))
{
if (torrent->isSeed())
continue;
torrentsList += torrent;
@ -544,8 +562,10 @@ void TransferListWidget::setDlLimitSelectedTorrents()
if (torrentsList.empty()) return;
int oldLimit = torrentsList.first()->downloadLimit();
for (BitTorrent::TorrentHandle *const torrent : asConst(torrentsList)) {
if (torrent->downloadLimit() != oldLimit) {
for (BitTorrent::TorrentHandle *const torrent : asConst(torrentsList))
{
if (torrent->downloadLimit() != oldLimit)
{
oldLimit = -1;
break;
}
@ -557,7 +577,8 @@ void TransferListWidget::setDlLimitSelectedTorrents()
, BitTorrent::Session::instance()->globalDownloadSpeedLimit());
if (!ok) return;
for (BitTorrent::TorrentHandle *const torrent : asConst(torrentsList)) {
for (BitTorrent::TorrentHandle *const torrent : asConst(torrentsList))
{
qDebug("Applying download speed limit of %ld Kb/s to torrent %s", (newLimit / 1024l), qUtf8Printable(torrent->hash()));
torrent->setDownloadLimit(newLimit);
}
@ -569,8 +590,10 @@ void TransferListWidget::setUpLimitSelectedTorrents()
if (torrentsList.empty()) return;
int oldLimit = torrentsList.first()->uploadLimit();
for (BitTorrent::TorrentHandle *const torrent : asConst(torrentsList)) {
if (torrent->uploadLimit() != oldLimit) {
for (BitTorrent::TorrentHandle *const torrent : asConst(torrentsList))
{
if (torrent->uploadLimit() != oldLimit)
{
oldLimit = -1;
break;
}
@ -582,7 +605,8 @@ void TransferListWidget::setUpLimitSelectedTorrents()
, BitTorrent::Session::instance()->globalUploadSpeedLimit());
if (!ok) return;
for (BitTorrent::TorrentHandle *const torrent : asConst(torrentsList)) {
for (BitTorrent::TorrentHandle *const torrent : asConst(torrentsList))
{
qDebug("Applying upload speed limit of %ld Kb/s to torrent %s", (newLimit / 1024l), qUtf8Printable(torrent->hash()));
torrent->setUploadLimit(newLimit);
}
@ -611,7 +635,8 @@ void TransferListWidget::setMaxRatioSelectedTorrents()
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, &QDialog::accepted, this, [dialog, torrents]()
{
for (BitTorrent::TorrentHandle *const torrent : torrents) {
for (BitTorrent::TorrentHandle *const torrent : torrents)
{
const qreal ratio = (dialog->useDefault()
? BitTorrent::TorrentHandle::USE_GLOBAL_RATIO : dialog->ratio());
torrent->setRatioLimit(ratio);
@ -626,7 +651,8 @@ void TransferListWidget::setMaxRatioSelectedTorrents()
void TransferListWidget::recheckSelectedTorrents()
{
if (Preferences::instance()->confirmTorrentRecheck()) {
if (Preferences::instance()->confirmTorrentRecheck())
{
QMessageBox::StandardButton ret = QMessageBox::question(this, tr("Recheck confirmation"), tr("Are you sure you want to recheck the selected torrent(s)?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (ret != QMessageBox::Yes) return;
}
@ -648,7 +674,8 @@ void TransferListWidget::displayDLHoSMenu(const QPoint&)
menu->setAttribute(Qt::WA_DeleteOnClose);
menu->setTitle(tr("Column visibility"));
for (int i = 0; i < m_listModel->columnCount(); ++i) {
for (int i = 0; i < m_listModel->columnCount(); ++i)
{
if (!BitTorrent::Session::instance()->isQueueingSystemEnabled() && (i == TransferListModel::TR_QUEUE_POSITION))
continue;
@ -661,7 +688,8 @@ void TransferListWidget::displayDLHoSMenu(const QPoint&)
connect(menu, &QMenu::triggered, this, [this](const QAction *action)
{
int visibleCols = 0;
for (int i = 0; i < TransferListModel::NB_COLUMNS; ++i) {
for (int i = 0; i < TransferListModel::NB_COLUMNS; ++i)
{
if (!isColumnHidden(i))
++visibleCols;
@ -687,7 +715,8 @@ void TransferListWidget::displayDLHoSMenu(const QPoint&)
void TransferListWidget::setSelectedTorrentsSuperSeeding(const bool enabled) const
{
for (BitTorrent::TorrentHandle *const torrent : asConst(getSelectedTorrents())) {
for (BitTorrent::TorrentHandle *const torrent : asConst(getSelectedTorrents()))
{
if (torrent->hasMetadata())
torrent->setSuperSeeding(enabled);
}
@ -730,10 +759,12 @@ void TransferListWidget::editTorrentTrackers()
const QVector<BitTorrent::TorrentHandle *> torrents = getSelectedTorrents();
QVector<BitTorrent::TrackerEntry> commonTrackers;
if (!torrents.empty()) {
if (!torrents.empty())
{
commonTrackers = torrents[0]->trackers();
for (const BitTorrent::TorrentHandle *torrent : torrents) {
for (const BitTorrent::TorrentHandle *torrent : torrents)
{
QSet<BitTorrent::TrackerEntry> trackerSet;
for (const BitTorrent::TrackerEntry &entry : asConst(torrent->trackers()))
@ -771,7 +802,8 @@ QStringList TransferListWidget::askTagsForSelection(const QString &dialogTitle)
{
QStringList tags;
bool invalid = true;
while (invalid) {
while (invalid)
{
bool ok = false;
invalid = false;
const QString tagsInput = AutoExpandableDialog::getText(
@ -779,9 +811,11 @@ QStringList TransferListWidget::askTagsForSelection(const QString &dialogTitle)
if (!ok || tagsInput.isEmpty())
return {};
tags = tagsInput.split(',', QString::SkipEmptyParts);
for (QString &tag : tags) {
for (QString &tag : tags)
{
tag = tag.trimmed();
if (!BitTorrent::Session::isValidTag(tag)) {
if (!BitTorrent::Session::isValidTag(tag))
{
QMessageBox::warning(this, tr("Invalid tag")
, tr("Tag name: '%1' is invalid").arg(tag));
invalid = true;
@ -793,7 +827,8 @@ QStringList TransferListWidget::askTagsForSelection(const QString &dialogTitle)
void TransferListWidget::applyToSelectedTorrents(const std::function<void (BitTorrent::TorrentHandle *const)> &fn)
{
for (const QModelIndex &index : asConst(selectionModel()->selectedRows())) {
for (const QModelIndex &index : asConst(selectionModel()->selectedRows()))
{
BitTorrent::TorrentHandle *const torrent = m_listModel->torrentHandle(mapToSource(index));
Q_ASSERT(torrent);
fn(torrent);
@ -812,7 +847,8 @@ void TransferListWidget::renameSelectedTorrent()
// Ask for a new Name
bool ok = false;
QString name = AutoExpandableDialog::getText(this, tr("Rename"), tr("New name:"), QLineEdit::Normal, torrent->name(), &ok);
if (ok && !name.isEmpty()) {
if (ok && !name.isEmpty())
{
name.replace(QRegularExpression("\r?\n|\r"), " ");
// Rename the torrent
m_listModel->setData(mi, name, Qt::DisplayRole);
@ -918,7 +954,8 @@ void TransferListWidget::displayListMenu(const QPoint &)
QSet<QString> tagsInAny;
QSet<QString> tagsInAll;
for (const QModelIndex &index : selectedIndexes) {
for (const QModelIndex &index : selectedIndexes)
{
// Get the file name
// Get handle and pause the torrent
const BitTorrent::TorrentHandle *torrent = m_listModel->torrentHandle(mapToSource(index));
@ -931,11 +968,13 @@ void TransferListWidget::displayListMenu(const QPoint &)
tagsInAny.unite(torrent->tags());
if (first) {
if (first)
{
firstAutoTMM = torrent->isAutoTMMEnabled();
tagsInAll = torrent->tags();
}
else {
else
{
tagsInAll.intersect(torrent->tags());
}
@ -944,21 +983,26 @@ void TransferListWidget::displayListMenu(const QPoint &)
if (torrent->hasMetadata())
oneHasMetadata = true;
if (!torrent->isSeed()) {
if (!torrent->isSeed())
{
oneNotSeed = true;
if (first) {
if (first)
{
sequentialDownloadMode = torrent->isSequentialDownload();
prioritizeFirstLast = torrent->hasFirstLastPiecePriority();
}
else {
else
{
if (sequentialDownloadMode != torrent->isSequentialDownload())
allSameSequentialDownloadMode = false;
if (prioritizeFirstLast != torrent->hasFirstLastPiecePriority())
allSamePrioFirstlast = false;
}
}
else {
if (!oneNotSeed && allSameSuperSeeding && torrent->hasMetadata()) {
else
{
if (!oneNotSeed && allSameSuperSeeding && torrent->hasMetadata())
{
if (first)
superSeedingMode = torrent->superSeeding();
else if (superSeedingMode != torrent->superSeeding())
@ -976,7 +1020,8 @@ void TransferListWidget::displayListMenu(const QPoint &)
else
needsPause = true;
if (torrent->isErrored() || torrent->hasMissingFiles()) {
if (torrent->isErrored() || torrent->hasMissingFiles())
{
// If torrent is in "errored" or "missing files" state
// it cannot keep further processing until you restart it.
needsStart = true;
@ -990,7 +1035,8 @@ void TransferListWidget::displayListMenu(const QPoint &)
if (oneHasMetadata && oneNotSeed && !allSameSequentialDownloadMode
&& !allSamePrioFirstlast && !allSameSuperSeeding && !allSameCategory
&& needsStart && needsForce && needsPause && needsPreview && !allSameAutoTMM) {
&& needsStart && needsForce && needsPause && needsPreview && !allSameAutoTMM)
{
break;
}
}
@ -1023,11 +1069,13 @@ void TransferListWidget::displayListMenu(const QPoint &)
categoryMenu->addSeparator();
for (const QString &category : asConst(categories)) {
for (const QString &category : asConst(categories))
{
const QString escapedCategory = QString(category).replace('&', "&&"); // avoid '&' becomes accelerator key
QAction *cat = new QAction(UIThemeManager::instance()->getIcon("inode-directory"), escapedCategory, categoryMenu);
if (allSameCategory && (category == firstCategory)) {
if (allSameCategory && (category == firstCategory))
{
cat->setCheckable(true);
cat->setChecked(true);
}
@ -1056,7 +1104,8 @@ void TransferListWidget::displayListMenu(const QPoint &)
tagsMenu->addSeparator();
for (const QString &tag : asConst(tags)) {
for (const QString &tag : asConst(tags))
{
auto *action = new TriStateAction(tag, tagsMenu);
action->setCloseOnTriggered(false);
@ -1086,7 +1135,8 @@ void TransferListWidget::displayListMenu(const QPoint &)
listMenu->addAction(actionSetDownloadLimit);
listMenu->addAction(actionSetUploadLimit);
listMenu->addAction(actionSetMaxRatio);
if (!oneNotSeed && oneHasMetadata) {
if (!oneNotSeed && oneHasMetadata)
{
actionSuperSeedingMode->setCheckState(allSameSuperSeeding
? (superSeedingMode ? Qt::Checked : Qt::Unchecked)
: Qt::PartiallyChecked);
@ -1094,11 +1144,13 @@ void TransferListWidget::displayListMenu(const QPoint &)
}
listMenu->addSeparator();
bool addedPreviewAction = false;
if (needsPreview) {
if (needsPreview)
{
listMenu->addAction(actionPreviewFile);
addedPreviewAction = true;
}
if (oneNotSeed) {
if (oneNotSeed)
{
actionSequentialDownload->setCheckState(allSameSequentialDownloadMode
? (sequentialDownloadMode ? Qt::Checked : Qt::Unchecked)
: Qt::PartiallyChecked);
@ -1114,13 +1166,15 @@ void TransferListWidget::displayListMenu(const QPoint &)
if (addedPreviewAction)
listMenu->addSeparator();
if (oneHasMetadata) {
if (oneHasMetadata)
{
listMenu->addAction(actionForceRecheck);
listMenu->addAction(actionForceReannounce);
listMenu->addSeparator();
}
listMenu->addAction(actionOpenDestinationFolder);
if (BitTorrent::Session::instance()->isQueueingSystemEnabled() && oneNotSeed) {
if (BitTorrent::Session::instance()->isQueueingSystemEnabled() && oneNotSeed)
{
listMenu->addSeparator();
QMenu *queueMenu = listMenu->addMenu(tr("Queue"));
queueMenu->addAction(actionTopQueuePos);
@ -1142,7 +1196,8 @@ void TransferListWidget::currentChanged(const QModelIndex &current, const QModel
{
qDebug("CURRENT CHANGED");
BitTorrent::TorrentHandle *torrent = nullptr;
if (current.isValid()) {
if (current.isValid())
{
torrent = m_listModel->torrentHandle(mapToSource(current));
// Scroll Fix
scrollTo(current);
@ -1187,7 +1242,8 @@ void TransferListWidget::applyStatusFilter(int f)
{
m_sortFilterModel->setStatusFilter(static_cast<TorrentFilter::Type>(f));
// Select first item if nothing is selected
if (selectionModel()->selectedRows(0).empty() && (m_sortFilterModel->rowCount() > 0)) {
if (selectionModel()->selectedRows(0).empty() && (m_sortFilterModel->rowCount() > 0))
{
qDebug("Nothing is selected, selecting first row: %s", qUtf8Printable(m_sortFilterModel->index(0, TransferListModel::TR_NAME).data().toString()));
selectionModel()->setCurrentIndex(m_sortFilterModel->index(0, TransferListModel::TR_NAME), QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
}
@ -1205,7 +1261,8 @@ bool TransferListWidget::loadSettings()
void TransferListWidget::wheelEvent(QWheelEvent *event)
{
if (event->modifiers() & Qt::ShiftModifier) {
if (event->modifiers() & Qt::ShiftModifier)
{
// Shift + scroll = horizontal scroll
event->accept();