Improve coding style

This commit is contained in:
Vladimir Golovnev (Glassez)
2020-11-16 10:02:11 +03:00
parent acad35c5bc
commit c41df9ffbd
147 changed files with 4454 additions and 2227 deletions

View File

@@ -108,9 +108,12 @@ void PluginSelectDialog::dropEvent(QDropEvent *event)
event->acceptProposedAction();
QStringList files;
if (event->mimeData()->hasUrls()) {
for (const QUrl &url : asConst(event->mimeData()->urls())) {
if (!url.isEmpty()) {
if (event->mimeData()->hasUrls())
{
for (const QUrl &url : asConst(event->mimeData()->urls()))
{
if (!url.isEmpty())
{
if (url.scheme().compare("file", Qt::CaseInsensitive) == 0)
files << url.toLocalFile();
else
@@ -118,13 +121,15 @@ void PluginSelectDialog::dropEvent(QDropEvent *event)
}
}
}
else {
else
{
files = event->mimeData()->text().split('\n');
}
if (files.isEmpty()) return;
for (const QString &file : asConst(files)) {
for (const QString &file : asConst(files))
{
qDebug("dropped %s", qUtf8Printable(file));
startAsyncOp();
m_pluginManager->installPlugin(file);
@@ -134,11 +139,13 @@ void PluginSelectDialog::dropEvent(QDropEvent *event)
// Decode if we accept drag 'n drop or not
void PluginSelectDialog::dragEnterEvent(QDragEnterEvent *event)
{
for (const QString &mime : asConst(event->mimeData()->formats())) {
for (const QString &mime : asConst(event->mimeData()->formats()))
{
qDebug("mimeData: %s", qUtf8Printable(mime));
}
if (event->mimeData()->hasFormat(QLatin1String("text/plain")) || event->mimeData()->hasFormat(QLatin1String("text/uri-list"))) {
if (event->mimeData()->hasFormat(QLatin1String("text/plain")) || event->mimeData()->hasFormat(QLatin1String("text/uri-list")))
{
event->acceptProposedAction();
}
}
@@ -153,11 +160,13 @@ void PluginSelectDialog::togglePluginState(QTreeWidgetItem *item, int)
{
PluginInfo *plugin = m_pluginManager->pluginInfo(item->text(PLUGIN_ID));
m_pluginManager->enablePlugin(plugin->name, !plugin->enabled);
if (plugin->enabled) {
if (plugin->enabled)
{
item->setText(PLUGIN_STATE, tr("Yes"));
setRowColor(m_ui->pluginsTree->indexOfTopLevelItem(item), "green");
}
else {
else
{
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(m_ui->pluginsTree->indexOfTopLevelItem(item), "red");
}
@@ -189,14 +198,17 @@ void PluginSelectDialog::on_closeButton_clicked()
void PluginSelectDialog::on_actionUninstall_triggered()
{
bool error = false;
for (QTreeWidgetItem *item : asConst(m_ui->pluginsTree->selectedItems())) {
for (QTreeWidgetItem *item : asConst(m_ui->pluginsTree->selectedItems()))
{
int index = m_ui->pluginsTree->indexOfTopLevelItem(item);
Q_ASSERT(index != -1);
QString id = item->text(PLUGIN_ID);
if (m_pluginManager->uninstallPlugin(id)) {
if (m_pluginManager->uninstallPlugin(id))
{
delete item;
}
else {
else
{
error = true;
// Disable it instead
m_pluginManager->enablePlugin(id, false);
@@ -213,16 +225,19 @@ void PluginSelectDialog::on_actionUninstall_triggered()
void PluginSelectDialog::enableSelection(bool enable)
{
for (QTreeWidgetItem *item : asConst(m_ui->pluginsTree->selectedItems())) {
for (QTreeWidgetItem *item : asConst(m_ui->pluginsTree->selectedItems()))
{
int index = m_ui->pluginsTree->indexOfTopLevelItem(item);
Q_ASSERT(index != -1);
QString id = item->text(PLUGIN_ID);
m_pluginManager->enablePlugin(id, enable);
if (enable) {
if (enable)
{
item->setText(PLUGIN_STATE, tr("Yes"));
setRowColor(index, "green");
}
else {
else
{
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(index, "red");
}
@@ -233,7 +248,8 @@ void PluginSelectDialog::enableSelection(bool enable)
void PluginSelectDialog::setRowColor(const int row, const QString &color)
{
QTreeWidgetItem *item = m_ui->pluginsTree->topLevelItem(row);
for (int i = 0; i < m_ui->pluginsTree->columnCount(); ++i) {
for (int i = 0; i < m_ui->pluginsTree->columnCount(); ++i)
{
item->setData(i, Qt::ForegroundRole, QColor(color));
}
}
@@ -243,7 +259,8 @@ QVector<QTreeWidgetItem*> PluginSelectDialog::findItemsWithUrl(const QString &ur
QVector<QTreeWidgetItem*> res;
res.reserve(m_ui->pluginsTree->topLevelItemCount());
for (int i = 0; i < m_ui->pluginsTree->topLevelItemCount(); ++i) {
for (int i = 0; i < m_ui->pluginsTree->topLevelItemCount(); ++i)
{
QTreeWidgetItem *item = m_ui->pluginsTree->topLevelItem(i);
if (url.startsWith(item->text(PLUGIN_URL), Qt::CaseInsensitive))
res << item;
@@ -254,7 +271,8 @@ QVector<QTreeWidgetItem*> PluginSelectDialog::findItemsWithUrl(const QString &ur
QTreeWidgetItem *PluginSelectDialog::findItemWithID(const QString &id)
{
for (int i = 0; i < m_ui->pluginsTree->topLevelItemCount(); ++i) {
for (int i = 0; i < m_ui->pluginsTree->topLevelItemCount(); ++i)
{
QTreeWidgetItem *item = m_ui->pluginsTree->topLevelItem(i);
if (id == item->text(PLUGIN_ID))
return item;
@@ -278,20 +296,24 @@ void PluginSelectDialog::addNewPlugin(const QString &pluginName)
item->setText(PLUGIN_NAME, plugin->fullName);
item->setText(PLUGIN_URL, plugin->url);
item->setText(PLUGIN_ID, plugin->name);
if (plugin->enabled) {
if (plugin->enabled)
{
item->setText(PLUGIN_STATE, tr("Yes"));
setRowColor(m_ui->pluginsTree->indexOfTopLevelItem(item), "green");
}
else {
else
{
item->setText(PLUGIN_STATE, tr("No"));
setRowColor(m_ui->pluginsTree->indexOfTopLevelItem(item), "red");
}
// Handle icon
if (QFile::exists(plugin->iconPath)) {
if (QFile::exists(plugin->iconPath))
{
// Good, we already have the icon
item->setData(PLUGIN_NAME, Qt::DecorationRole, QIcon(plugin->iconPath));
}
else {
else
{
// Icon is missing, we must download it
using namespace Net;
DownloadManager::instance()->download(
@@ -318,7 +340,8 @@ void PluginSelectDialog::finishAsyncOp()
void PluginSelectDialog::finishPluginUpdate()
{
--m_pendingUpdates;
if ((m_pendingUpdates == 0) && !m_updatedPlugins.isEmpty()) {
if ((m_pendingUpdates == 0) && !m_updatedPlugins.isEmpty())
{
m_updatedPlugins.sort(Qt::CaseInsensitive);
QMessageBox::information(this, tr("Search plugin update"), tr("Plugins installed or updated: %1").arg(m_updatedPlugins.join(", ")));
m_updatedPlugins.clear();
@@ -344,7 +367,8 @@ void PluginSelectDialog::askForPluginUrl()
tr("URL:"), QLineEdit::Normal, defaultUrl, &ok
);
while (ok && !url.isEmpty() && !url.endsWith(".py")) {
while (ok && !url.isEmpty() && !url.endsWith(".py"))
{
QMessageBox::warning(this, tr("Invalid link"), tr("The link doesn't seem to point to a search engine plugin."));
url = AutoExpandableDialog::getText(
this, tr("New search engine plugin URL"),
@@ -352,7 +376,8 @@ void PluginSelectDialog::askForPluginUrl()
);
}
if (ok && !url.isEmpty()) {
if (ok && !url.isEmpty())
{
startAsyncOp();
m_pluginManager->installPlugin(url);
}
@@ -364,7 +389,8 @@ void PluginSelectDialog::askForLocalPlugin()
nullptr, tr("Select search plugins"), QDir::homePath(),
tr("qBittorrent search plugin") + QLatin1String(" (*.py)")
);
for (const QString &path : pathsList) {
for (const QString &path : pathsList)
{
startAsyncOp();
m_pluginManager->installPlugin(path);
}
@@ -372,7 +398,8 @@ void PluginSelectDialog::askForLocalPlugin()
void PluginSelectDialog::iconDownloadFinished(const Net::DownloadResult &result)
{
if (result.status != Net::DownloadStatus::Success) {
if (result.status != Net::DownloadStatus::Success)
{
qDebug("Could not download favicon: %s, reason: %s", qUtf8Printable(result.url), qUtf8Printable(result.errorString));
return;
}
@@ -384,8 +411,10 @@ void PluginSelectDialog::iconDownloadFinished(const Net::DownloadResult &result)
// Detect a non-decodable icon
QList<QSize> sizes = icon.availableSizes();
bool invalid = (sizes.isEmpty() || icon.pixmap(sizes.first()).isNull());
if (!invalid) {
for (QTreeWidgetItem *item : asConst(findItemsWithUrl(result.url))) {
if (!invalid)
{
for (QTreeWidgetItem *item : asConst(findItemsWithUrl(result.url)))
{
QString id = item->text(PLUGIN_ID);
PluginInfo *plugin = m_pluginManager->pluginInfo(id);
if (!plugin) continue;
@@ -394,14 +423,16 @@ void PluginSelectDialog::iconDownloadFinished(const Net::DownloadResult &result)
.arg(SearchPluginManager::pluginsLocation()
, id
, result.url.endsWith(".ico", Qt::CaseInsensitive) ? "ico" : "png");
if (QFile::copy(filePath, iconPath)) {
if (QFile::copy(filePath, iconPath))
{
// This 2nd check is necessary. Some favicons (eg from piratebay)
// decode fine without an ext, but fail to do so when appending the ext
// from the url. Probably a Qt bug.
QIcon iconWithExt(iconPath);
QList<QSize> sizesExt = iconWithExt.availableSizes();
bool invalidExt = (sizesExt.isEmpty() || iconWithExt.pixmap(sizesExt.first()).isNull());
if (invalidExt) {
if (invalidExt)
{
Utils::Fs::forceRemove(iconPath);
continue;
}
@@ -418,12 +449,14 @@ void PluginSelectDialog::iconDownloadFinished(const Net::DownloadResult &result)
void PluginSelectDialog::checkForUpdatesFinished(const QHash<QString, PluginVersion> &updateInfo)
{
finishAsyncOp();
if (updateInfo.isEmpty()) {
if (updateInfo.isEmpty())
{
QMessageBox::information(this, tr("Search plugin update"), tr("All your plugins are already up to date."));
return;
}
for (auto i = updateInfo.cbegin(); i != updateInfo.cend(); ++i) {
for (auto i = updateInfo.cbegin(); i != updateInfo.cend(); ++i)
{
startAsyncOp();
++m_pendingUpdates;
m_pluginManager->updatePlugin(i.key());

View File

@@ -101,8 +101,10 @@ SearchJobWidget::SearchJobWidget(SearchHandler *searchHandler, QWidget *parent)
// Ensure that at least one column is visible at all times
bool atLeastOne = false;
for (int i = 0; i < SearchSortModel::DL_LINK; ++i) {
if (!m_ui->resultsBrowser->isColumnHidden(i)) {
for (int i = 0; i < SearchSortModel::DL_LINK; ++i)
{
if (!m_ui->resultsBrowser->isColumnHidden(i))
{
atLeastOne = true;
break;
}
@@ -218,7 +220,8 @@ void SearchJobWidget::downloadTorrents()
void SearchJobWidget::openTorrentPages() const
{
const QModelIndexList rows {m_ui->resultsBrowser->selectionModel()->selectedRows()};
for (const QModelIndex &rowIndex : rows) {
for (const QModelIndex &rowIndex : rows)
{
const QString descrLink = m_proxyModel->data(
m_proxyModel->index(rowIndex.row(), SearchSortModel::DESC_LINK)).toString();
if (!descrLink.isEmpty())
@@ -246,7 +249,8 @@ void SearchJobWidget::copyField(const int column) const
const QModelIndexList rows {m_ui->resultsBrowser->selectionModel()->selectedRows()};
QStringList list;
for (const QModelIndex &rowIndex : rows) {
for (const QModelIndex &rowIndex : rows)
{
const QString field = m_proxyModel->data(
m_proxyModel->index(rowIndex.row(), column)).toString();
if (!field.isEmpty())
@@ -273,10 +277,12 @@ void SearchJobWidget::downloadTorrent(const QModelIndex &rowIndex)
const QString siteUrl = m_proxyModel->data(
m_proxyModel->index(rowIndex.row(), SearchSortModel::ENGINE_URL)).toString();
if (torrentUrl.startsWith("magnet:", Qt::CaseInsensitive)) {
if (torrentUrl.startsWith("magnet:", Qt::CaseInsensitive))
{
addTorrentToSession(torrentUrl);
}
else {
else
{
SearchDownloadHandler *downloadHandler = m_searchHandler->manager()->downloadTorrent(siteUrl, torrentUrl);
connect(downloadHandler, &SearchDownloadHandler::downloadFinished, this, &SearchJobWidget::addTorrentToSession);
connect(downloadHandler, &SearchDownloadHandler::downloadFinished, downloadHandler, &SearchDownloadHandler::deleteLater);
@@ -418,7 +424,8 @@ void SearchJobWidget::contextMenuEvent(QContextMenuEvent *event)
QString SearchJobWidget::statusText(SearchJobWidget::Status st)
{
switch (st) {
switch (st)
{
case Status::Ongoing:
return tr("Searching...");
case Status::Finished:
@@ -455,7 +462,8 @@ void SearchJobWidget::displayToggleColumnsMenu(const QPoint &)
menu->setAttribute(Qt::WA_DeleteOnClose);
menu->setTitle(tr("Column visibility"));
for (int i = 0; i < SearchSortModel::DL_LINK; ++i) {
for (int i = 0; i < SearchSortModel::DL_LINK; ++i)
{
QAction *myAct = menu->addAction(m_searchListModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
myAct->setCheckable(true);
myAct->setChecked(!m_ui->resultsBrowser->isColumnHidden(i));
@@ -465,7 +473,8 @@ void SearchJobWidget::displayToggleColumnsMenu(const QPoint &)
connect(menu, &QMenu::triggered, this, [this](const QAction *action)
{
int visibleCols = 0;
for (int i = 0; i < SearchSortModel::DL_LINK; ++i) {
for (int i = 0; i < SearchSortModel::DL_LINK; ++i)
{
if (!m_ui->resultsBrowser->isColumnHidden(i))
++visibleCols;
@@ -506,7 +515,8 @@ void SearchJobWidget::searchFailed()
void SearchJobWidget::appendSearchResults(const QVector<SearchResult> &results)
{
for (const SearchResult &result : results) {
for (const SearchResult &result : results)
{
// Add item to search result list
int row = m_searchListModel->rowCount();
m_searchListModel->insertRow(row);
@@ -543,7 +553,8 @@ CachedSettingValue<SearchJobWidget::NameFilteringMode> &SearchJobWidget::nameFil
void SearchJobWidget::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
switch (event->key())
{
case Qt::Key_Enter:
case Qt::Key_Return:
downloadTorrents();

View File

@@ -54,10 +54,12 @@ void SearchSortModel::setNameFilter(const QString &searchTerm)
{
m_searchTerm = searchTerm;
if ((searchTerm.length() > 2)
&& searchTerm.startsWith(QLatin1Char('"')) && searchTerm.endsWith(QLatin1Char('"'))) {
&& searchTerm.startsWith(QLatin1Char('"')) && searchTerm.endsWith(QLatin1Char('"')))
{
m_searchTermWords = QStringList(m_searchTerm.mid(1, m_searchTerm.length() - 2));
}
else {
else
{
m_searchTermWords = searchTerm.split(QLatin1Char(' '), QString::SkipEmptyParts);
}
}
@@ -112,9 +114,11 @@ qint64 SearchSortModel::maxSize() const
bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
switch (sortColumn()) {
switch (sortColumn())
{
case NAME:
case ENGINE_URL: {
case ENGINE_URL:
{
const QString strL = left.data().toString();
const QString strR = right.data().toString();
const int result = Utils::String::naturalCompare(strL, strR, Qt::CaseInsensitive);
@@ -130,29 +134,34 @@ bool SearchSortModel::filterAcceptsRow(const int sourceRow, const QModelIndex &s
{
const QAbstractItemModel *const sourceModel = this->sourceModel();
if (m_isNameFilterEnabled && !m_searchTerm.isEmpty()) {
if (m_isNameFilterEnabled && !m_searchTerm.isEmpty())
{
const QString name = sourceModel->data(sourceModel->index(sourceRow, NAME, sourceParent), UnderlyingDataRole).toString();
for (const QString &word : asConst(m_searchTermWords)) {
for (const QString &word : asConst(m_searchTermWords))
{
if (!name.contains(word, Qt::CaseInsensitive))
return false;
}
}
if ((m_minSize > 0) || (m_maxSize >= 0)) {
if ((m_minSize > 0) || (m_maxSize >= 0))
{
const qlonglong size = sourceModel->data(sourceModel->index(sourceRow, SIZE, sourceParent), UnderlyingDataRole).toLongLong();
if (((m_minSize > 0) && (size < m_minSize))
|| ((m_maxSize > 0) && (size > m_maxSize)))
return false;
}
if ((m_minSeeds > 0) || (m_maxSeeds >= 0)) {
if ((m_minSeeds > 0) || (m_maxSeeds >= 0))
{
const int seeds = sourceModel->data(sourceModel->index(sourceRow, SEEDS, sourceParent), UnderlyingDataRole).toInt();
if (((m_minSeeds > 0) && (seeds < m_minSeeds))
|| ((m_maxSeeds > 0) && (seeds > m_maxSeeds)))
return false;
}
if ((m_minLeeches > 0) || (m_maxLeeches >= 0)) {
if ((m_minLeeches > 0) || (m_maxLeeches >= 0))
{
const int leeches = sourceModel->data(sourceModel->index(sourceRow, LEECHES, sourceParent), UnderlyingDataRole).toInt();
if (((m_minLeeches > 0) && (leeches < m_minLeeches))
|| ((m_maxLeeches > 0) && (leeches > m_maxLeeches)))

View File

@@ -63,7 +63,8 @@ namespace
{
QString statusIconName(SearchJobWidget::Status st)
{
switch (st) {
switch (st)
{
case SearchJobWidget::Status::Ongoing:
return QLatin1String("task-ongoing");
case SearchJobWidget::Status::Finished:
@@ -148,14 +149,16 @@ SearchWidget::SearchWidget(MainWindow *mainWindow)
bool SearchWidget::eventFilter(QObject *object, QEvent *event)
{
if (object == m_ui->tabWidget->tabBar()) {
if (object == m_ui->tabWidget->tabBar())
{
// Close tabs when middle-clicked
if (event->type() != QEvent::MouseButtonRelease)
return false;
const auto mouseEvent = static_cast<QMouseEvent *>(event);
const int tabIndex = m_ui->tabWidget->tabBar()->tabAt(mouseEvent->pos());
if ((mouseEvent->button() == Qt::MiddleButton) && (tabIndex >= 0)) {
if ((mouseEvent->button() == Qt::MiddleButton) && (tabIndex >= 0))
{
closeTab(tabIndex);
return true;
}
@@ -175,7 +178,8 @@ void SearchWidget::fillCatCombobox()
tmpList << qMakePair(SearchPluginManager::categoryFullName(cat), cat);
std::sort(tmpList.begin(), tmpList.end(), [](const QStrPair &l, const QStrPair &r) { return (QString::localeAwareCompare(l.first, r.first) < 0); });
for (const QStrPair &p : asConst(tmpList)) {
for (const QStrPair &p : asConst(tmpList))
{
qDebug("Supported category: %s", qUtf8Printable(p.second));
m_ui->comboCategory->addItem(p.first, p.second);
}
@@ -216,14 +220,16 @@ QString SearchWidget::selectedPlugin() const
void SearchWidget::selectActivePage()
{
if (SearchPluginManager::instance()->allPlugins().isEmpty()) {
if (SearchPluginManager::instance()->allPlugins().isEmpty())
{
m_ui->stackedPages->setCurrentWidget(m_ui->emptyPage);
m_ui->lineEditSearchPattern->setEnabled(false);
m_ui->comboCategory->setEnabled(false);
m_ui->selectPlugin->setEnabled(false);
m_ui->searchButton->setEnabled(false);
}
else {
else
{
m_ui->stackedPages->setCurrentWidget(m_ui->searchPage);
m_ui->lineEditSearchPattern->setEnabled(true);
m_ui->comboCategory->setEnabled(true);
@@ -254,11 +260,13 @@ void SearchWidget::selectMultipleBox(int index)
void SearchWidget::toggleFocusBetweenLineEdits()
{
if (m_ui->lineEditSearchPattern->hasFocus() && m_currentSearchTab) {
if (m_ui->lineEditSearchPattern->hasFocus() && m_currentSearchTab)
{
m_currentSearchTab->lineEditSearchResultsFilter()->setFocus();
m_currentSearchTab->lineEditSearchResultsFilter()->selectAll();
}
else {
else
{
m_ui->lineEditSearchPattern->setFocus();
m_ui->lineEditSearchPattern->selectAll();
}
@@ -284,14 +292,17 @@ void SearchWidget::giveFocusToSearchInput()
// Function called when we click on search button
void SearchWidget::on_searchButton_clicked()
{
if (!Utils::ForeignApps::pythonInfo().isValid()) {
if (!Utils::ForeignApps::pythonInfo().isValid())
{
m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Please install Python to use the Search Engine."));
return;
}
if (m_activeSearchTab) {
if (m_activeSearchTab)
{
m_activeSearchTab->cancelSearch();
if (!m_isNewQueryString) {
if (!m_isNewQueryString)
{
m_ui->searchButton->setText(tr("Search"));
return;
}
@@ -301,7 +312,8 @@ void SearchWidget::on_searchButton_clicked()
const QString pattern = m_ui->lineEditSearchPattern->text().trimmed();
// No search pattern entered
if (pattern.isEmpty()) {
if (pattern.isEmpty())
{
QMessageBox::critical(this, tr("Empty search pattern"), tr("Please type a search pattern first"));
return;
}
@@ -344,10 +356,12 @@ void SearchWidget::tabStatusChanged(QWidget *tab)
m_ui->tabWidget->setTabIcon(tabIndex, UIThemeManager::instance()->getIcon(
statusIconName(static_cast<SearchJobWidget *>(tab)->status())));
if ((tab == m_activeSearchTab) && (m_activeSearchTab->status() != SearchJobWidget::Status::Ongoing)) {
if ((tab == m_activeSearchTab) && (m_activeSearchTab->status() != SearchJobWidget::Status::Ongoing))
{
Q_ASSERT(m_activeSearchTab->status() != SearchJobWidget::Status::Ongoing);
if (m_mainWindow->isNotificationsEnabled() && (m_mainWindow->currentTabWidget() != this)) {
if (m_mainWindow->isNotificationsEnabled() && (m_mainWindow->currentTabWidget() != this))
{
if (m_activeSearchTab->status() == SearchJobWidget::Status::Error)
m_mainWindow->showNotificationBaloon(tr("Search Engine"), tr("Search has failed"));
else