Browse Source

Remove usage of QTextStream

Other classes already provide rich methods so avoid another
layer of QTextStream which slow things down (slightly).

PR #17180.
adaptive-webui-19844
Chocobo1 3 years ago committed by GitHub
parent
commit
eddeda7bab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 85
      src/app/cmdoptions.cpp
  2. 9
      src/base/torrentfileswatcher.cpp
  3. 45
      src/gui/properties/piecesbar.cpp
  4. 23
      src/gui/search/searchwidget.cpp
  5. 1
      src/gui/utils.cpp
  6. 3
      src/gui/utils.h

85
src/app/cmdoptions.cpp

@ -35,7 +35,6 @@
#include <QDebug> #include <QDebug>
#include <QFileInfo> #include <QFileInfo>
#include <QProcessEnvironment> #include <QProcessEnvironment>
#include <QTextStream>
#if defined(Q_OS_WIN) && !defined(DISABLE_GUI) #if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
#include <QMessageBox> #include <QMessageBox>
@ -525,59 +524,57 @@ QString wrapText(const QString &text, int initialIndentation = USAGE_TEXT_COLUMN
QString makeUsage(const QString &prgName) QString makeUsage(const QString &prgName)
{ {
QString text; const QString indentation {USAGE_INDENTATION, u' '};
QTextStream stream(&text, QIODevice::WriteOnly);
QString indentation = QString(USAGE_INDENTATION, u' ');
stream << QObject::tr("Usage:") << '\n' const QString text = QObject::tr("Usage:") + u'\n'
<< indentation << prgName << u" [options] [(<filename> | <url>)...]"_qs << '\n' + indentation + prgName + u" [options] [(<filename> | <url>)...]" + u'\n'
<< QObject::tr("Options:") << '\n' + QObject::tr("Options:") + u'\n'
#if !defined(Q_OS_WIN) || defined(DISABLE_GUI) #if !defined(Q_OS_WIN) || defined(DISABLE_GUI)
<< SHOW_VERSION_OPTION.usage() << wrapText(QObject::tr("Display program version and exit")) << '\n' + SHOW_VERSION_OPTION.usage() + wrapText(QObject::tr("Display program version and exit")) + u'\n'
#endif #endif
<< SHOW_HELP_OPTION.usage() << wrapText(QObject::tr("Display this help message and exit")) << '\n' + SHOW_HELP_OPTION.usage() + wrapText(QObject::tr("Display this help message and exit")) + u'\n'
<< WEBUI_PORT_OPTION.usage(QObject::tr("port")) + WEBUI_PORT_OPTION.usage(QObject::tr("port"))
<< wrapText(QObject::tr("Change the Web UI port")) + wrapText(QObject::tr("Change the Web UI port"))
<< '\n' + u'\n'
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
<< NO_SPLASH_OPTION.usage() << wrapText(QObject::tr("Disable splash screen")) << '\n' + NO_SPLASH_OPTION.usage() + wrapText(QObject::tr("Disable splash screen")) + u'\n'
#elif !defined(Q_OS_WIN) #elif !defined(Q_OS_WIN)
<< DAEMON_OPTION.usage() << wrapText(QObject::tr("Run in daemon-mode (background)")) << '\n' + DAEMON_OPTION.usage() + wrapText(QObject::tr("Run in daemon-mode (background)")) + u'\n'
#endif #endif
//: Use appropriate short form or abbreviation of "directory" //: Use appropriate short form or abbreviation of "directory"
<< PROFILE_OPTION.usage(QObject::tr("dir")) + PROFILE_OPTION.usage(QObject::tr("dir"))
<< wrapText(QObject::tr("Store configuration files in <dir>")) << '\n' + wrapText(QObject::tr("Store configuration files in <dir>")) + u'\n'
<< CONFIGURATION_OPTION.usage(QObject::tr("name")) + CONFIGURATION_OPTION.usage(QObject::tr("name"))
<< wrapText(QObject::tr("Store configuration files in directories qBittorrent_<name>")) << '\n' + wrapText(QObject::tr("Store configuration files in directories qBittorrent_<name>")) + u'\n'
<< RELATIVE_FASTRESUME.usage() + RELATIVE_FASTRESUME.usage()
<< wrapText(QObject::tr("Hack into libtorrent fastresume files and make file paths relative " + wrapText(QObject::tr("Hack into libtorrent fastresume files and make file paths relative "
"to the profile directory")) << '\n' "to the profile directory")) + u'\n'
<< Option::padUsageText(QObject::tr("files or URLs")) + Option::padUsageText(QObject::tr("files or URLs"))
<< wrapText(QObject::tr("Download the torrents passed by the user")) << '\n' + wrapText(QObject::tr("Download the torrents passed by the user")) + u'\n'
<< '\n' + u'\n'
<< wrapText(QObject::tr("Options when adding new torrents:"), 0) << '\n' + wrapText(QObject::tr("Options when adding new torrents:"), 0) + u'\n'
<< SAVE_PATH_OPTION.usage(QObject::tr("path")) << wrapText(QObject::tr("Torrent save path")) << '\n' + SAVE_PATH_OPTION.usage(QObject::tr("path")) + wrapText(QObject::tr("Torrent save path")) + u'\n'
<< PAUSED_OPTION.usage() << wrapText(QObject::tr("Add torrents as started or paused")) << '\n' + PAUSED_OPTION.usage() + wrapText(QObject::tr("Add torrents as started or paused")) + u'\n'
<< SKIP_HASH_CHECK_OPTION.usage() << wrapText(QObject::tr("Skip hash check")) << '\n' + SKIP_HASH_CHECK_OPTION.usage() + wrapText(QObject::tr("Skip hash check")) + u'\n'
<< CATEGORY_OPTION.usage(QObject::tr("name")) + CATEGORY_OPTION.usage(QObject::tr("name"))
<< wrapText(QObject::tr("Assign torrents to category. If the category doesn't exist, it will be " + wrapText(QObject::tr("Assign torrents to category. If the category doesn't exist, it will be "
"created.")) << '\n' "created.")) + u'\n'
<< SEQUENTIAL_OPTION.usage() << wrapText(QObject::tr("Download files in sequential order")) << '\n' + SEQUENTIAL_OPTION.usage() + wrapText(QObject::tr("Download files in sequential order")) + u'\n'
<< FIRST_AND_LAST_OPTION.usage() + FIRST_AND_LAST_OPTION.usage()
<< wrapText(QObject::tr("Download first and last pieces first")) << '\n' + wrapText(QObject::tr("Download first and last pieces first")) + u'\n'
<< SKIP_DIALOG_OPTION.usage() + SKIP_DIALOG_OPTION.usage()
<< wrapText(QObject::tr("Specify whether the \"Add New Torrent\" dialog opens when adding a " + wrapText(QObject::tr("Specify whether the \"Add New Torrent\" dialog opens when adding a "
"torrent.")) << '\n' "torrent.")) + u'\n'
<< '\n' + u'\n'
<< wrapText(QObject::tr("Option values may be supplied via environment variables. For option named " + wrapText(QObject::tr("Option values may be supplied via environment variables. For option named "
"'parameter-name', environment variable name is 'QBT_PARAMETER_NAME' (in upper " "'parameter-name', environment variable name is 'QBT_PARAMETER_NAME' (in upper "
"case, '-' replaced with '_'). To pass flag values, set the variable to '1' or " "case, '-' replaced with '_'). To pass flag values, set the variable to '1' or "
"'TRUE'. For example, to disable the splash screen: "), 0) << "\n" "'TRUE'. For example, to disable the splash screen: "), 0) + u'\n'
<< u"QBT_NO_SPLASH=1 "_qs << prgName << '\n' + u"QBT_NO_SPLASH=1 " + prgName + u'\n'
<< wrapText(QObject::tr("Command line parameters take precedence over environment variables"), 0) << '\n'; + wrapText(QObject::tr("Command line parameters take precedence over environment variables"), 0) + u'\n';
return text; return text;
} }

9
src/base/torrentfileswatcher.cpp

@ -41,7 +41,6 @@
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
#include <QSet> #include <QSet>
#include <QTextStream>
#include <QThread> #include <QThread>
#include <QTimer> #include <QTimer>
#include <QVariant> #include <QVariant>
@ -522,9 +521,11 @@ void TorrentFilesWatcher::Worker::processFolder(const Path &path, const Path &wa
QFile file {filePath.data()}; QFile file {filePath.data()};
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
QTextStream str {&file}; while (!file.atEnd())
while (!str.atEnd()) {
emit magnetFound(BitTorrent::MagnetUri(str.readLine()), addTorrentParams); const auto line = QString::fromLatin1(file.readLine()).trimmed();
emit magnetFound(BitTorrent::MagnetUri(line), addTorrentParams);
}
file.close(); file.close();
Utils::Fs::removeFile(filePath); Utils::Fs::removeFile(filePath);

45
src/gui/properties/piecesbar.cpp

@ -34,7 +34,6 @@
#include <QHelpEvent> #include <QHelpEvent>
#include <QPainter> #include <QPainter>
#include <QPainterPath> #include <QPainterPath>
#include <QTextStream>
#include <QToolTip> #include <QToolTip>
#include "base/bittorrent/torrent.h" #include "base/bittorrent/torrent.h"
@ -88,25 +87,29 @@ namespace
class DetailedTooltipRenderer class DetailedTooltipRenderer
{ {
public: public:
DetailedTooltipRenderer(QTextStream &stream, const QString &header) DetailedTooltipRenderer(QString &string, const QString &header)
: m_stream(stream) : m_string(string)
{ {
m_stream << header m_string += header
<< R"(<table style="width:100%; padding: 3px; vertical-align: middle;">)"; + uR"(<table style="width:100%; padding: 3px; vertical-align: middle;">)";
} }
~DetailedTooltipRenderer() ~DetailedTooltipRenderer()
{ {
m_stream << "</table>"; m_string += u"</table>";
} }
void operator()(const QString &size, const Path &path) void operator()(const QString &size, const Path &path)
{ {
m_stream << R"(<tr><td style="white-space:nowrap">)" << size << "</td><td>" << path.toString() << "</td></tr>"; m_string += uR"(<tr><td style="white-space:nowrap">)"
+ size
+ u"</td><td>"
+ path.toString()
+ u"</td></tr>";
} }
private: private:
QTextStream &m_stream; QString &m_string;
}; };
} }
@ -252,17 +255,16 @@ void PiecesBar::showToolTip(const QHelpEvent *e)
return; return;
QString toolTipText; QString toolTipText;
QTextStream stream(&toolTipText, QIODevice::WriteOnly);
const bool showDetailedInformation = QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier); const bool showDetailedInformation = QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier);
if (showDetailedInformation && m_torrent->hasMetadata()) if (showDetailedInformation && m_torrent->hasMetadata())
{ {
const BitTorrent::TorrentInfo torrentInfo = m_torrent->info(); const BitTorrent::TorrentInfo torrentInfo = m_torrent->info();
const int imagePos = e->pos().x() - borderWidth; const int imagePos = e->pos().x() - borderWidth;
if ((imagePos >=0) && (imagePos < m_image.width())) if ((imagePos >= 0) && (imagePos < m_image.width()))
{ {
stream << "<html><body>"; const PieceIndexToImagePos transform {torrentInfo, m_image};
PieceIndexToImagePos transform {torrentInfo, m_image}; const int pieceIndex = transform.pieceIndex(imagePos);
int pieceIndex = transform.pieceIndex(imagePos);
const QVector<int> files {torrentInfo.fileIndicesForPiece(pieceIndex)}; const QVector<int> files {torrentInfo.fileIndicesForPiece(pieceIndex)};
QString tooltipTitle; QString tooltipTitle;
@ -278,27 +280,28 @@ void PiecesBar::showToolTip(const QHelpEvent *e)
tooltipTitle = tr("File in these pieces"); tooltipTitle = tr("File in these pieces");
} }
DetailedTooltipRenderer renderer(stream, tooltipTitle); toolTipText.reserve(files.size() * 128);
toolTipText += u"<html><body>";
DetailedTooltipRenderer renderer {toolTipText, tooltipTitle};
for (int f : files) for (const int f : files)
{ {
const Path filePath = torrentInfo.filePath(f); const Path filePath = torrentInfo.filePath(f);
renderer(Utils::Misc::friendlyUnit(torrentInfo.fileSize(f)), filePath); renderer(Utils::Misc::friendlyUnit(torrentInfo.fileSize(f)), filePath);
} }
stream << "</body></html>"; toolTipText += u"</body></html>";
} }
} }
else else
{ {
stream << simpleToolTipText(); toolTipText += simpleToolTipText();
if (showDetailedInformation) // metadata are not available at this point if (showDetailedInformation) // metadata are not available at this point
stream << '\n' << tr("Wait until metadata become available to see detailed information"); toolTipText += u'\n' + tr("Wait until metadata become available to see detailed information");
else else
stream << '\n' << tr("Hold Shift key for detailed information"); toolTipText += u'\n' + tr("Hold Shift key for detailed information");
} }
stream.flush();
QToolTip::showText(e->globalPos(), toolTipText, this); QToolTip::showText(e->globalPos(), toolTipText, this);
} }

23
src/gui/search/searchwidget.cpp

@ -46,7 +46,6 @@
#include <QObject> #include <QObject>
#include <QRegularExpression> #include <QRegularExpression>
#include <QShortcut> #include <QShortcut>
#include <QTextStream>
#include <QVector> #include <QVector>
#include "base/global.h" #include "base/global.h"
@ -93,22 +92,20 @@ SearchWidget::SearchWidget(MainWindow *mainWindow)
m_ui->setupUi(this); m_ui->setupUi(this);
m_ui->tabWidget->tabBar()->installEventFilter(this); m_ui->tabWidget->tabBar()->installEventFilter(this);
QString searchPatternHint; const QString searchPatternHint = u"<html><head/><body><p>"
QTextStream stream(&searchPatternHint, QIODevice::WriteOnly); + tr("A phrase to search for.") + u"<br>"
stream << "<html><head/><body><p>" + tr("Spaces in a search term may be protected by double quotes.")
<< tr("A phrase to search for.") << "<br>" + u"</p><p>"
<< tr("Spaces in a search term may be protected by double quotes.") + tr("Example:", "Search phrase example")
<< "</p><p>" + u"<br>"
<< tr("Example:", "Search phrase example") + tr("<b>foo bar</b>: search for <b>foo</b> and <b>bar</b>",
<< "<br>"
<< tr("<b>foo bar</b>: search for <b>foo</b> and <b>bar</b>",
"Search phrase example, illustrates quotes usage, a pair of " "Search phrase example, illustrates quotes usage, a pair of "
"space delimited words, individal words are highlighted") "space delimited words, individal words are highlighted")
<< "<br>" + u"<br>"
<< tr("<b>&quot;foo bar&quot;</b>: search for <b>foo bar</b>", + tr("<b>&quot;foo bar&quot;</b>: search for <b>foo bar</b>",
"Search phrase example, illustrates quotes usage, double quoted" "Search phrase example, illustrates quotes usage, double quoted"
"pair of space delimited words, the whole pair is highlighted") "pair of space delimited words, the whole pair is highlighted")
<< "</p></body></html>"; + u"</p></body></html>";
m_ui->lineEditSearchPattern->setToolTip(searchPatternHint); m_ui->lineEditSearchPattern->setToolTip(searchPatternHint);
#ifndef Q_OS_MACOS #ifndef Q_OS_MACOS

1
src/gui/utils.cpp

@ -42,6 +42,7 @@
#include <QProcess> #include <QProcess>
#include <QRegularExpression> #include <QRegularExpression>
#include <QScreen> #include <QScreen>
#include <QSize>
#include <QStyle> #include <QStyle>
#include <QThread> #include <QThread>
#include <QUrl> #include <QUrl>

3
src/gui/utils.h

@ -28,13 +28,12 @@
#pragma once #pragma once
#include <QSize>
#include "base/pathfwd.h" #include "base/pathfwd.h"
class QIcon; class QIcon;
class QPixmap; class QPixmap;
class QPoint; class QPoint;
class QSize;
class QWidget; class QWidget;
namespace Utils::Gui namespace Utils::Gui

Loading…
Cancel
Save