Remove usage of QTextStream

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

PR #17180.
This commit is contained in:
Chocobo1
2022-06-09 11:18:41 +08:00
committed by GitHub
parent ca07540675
commit eddeda7bab
6 changed files with 80 additions and 82 deletions

View File

@@ -34,7 +34,6 @@
#include <QHelpEvent>
#include <QPainter>
#include <QPainterPath>
#include <QTextStream>
#include <QToolTip>
#include "base/bittorrent/torrent.h"
@@ -88,25 +87,29 @@ namespace
class DetailedTooltipRenderer
{
public:
DetailedTooltipRenderer(QTextStream &stream, const QString &header)
: m_stream(stream)
DetailedTooltipRenderer(QString &string, const QString &header)
: m_string(string)
{
m_stream << header
<< R"(<table style="width:100%; padding: 3px; vertical-align: middle;">)";
m_string += header
+ uR"(<table style="width:100%; padding: 3px; vertical-align: middle;">)";
}
~DetailedTooltipRenderer()
{
m_stream << "</table>";
m_string += u"</table>";
}
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:
QTextStream &m_stream;
QString &m_string;
};
}
@@ -252,17 +255,16 @@ void PiecesBar::showToolTip(const QHelpEvent *e)
return;
QString toolTipText;
QTextStream stream(&toolTipText, QIODevice::WriteOnly);
const bool showDetailedInformation = QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier);
if (showDetailedInformation && m_torrent->hasMetadata())
{
const BitTorrent::TorrentInfo torrentInfo = m_torrent->info();
const int imagePos = e->pos().x() - borderWidth;
if ((imagePos >=0) && (imagePos < m_image.width()))
if ((imagePos >= 0) && (imagePos < m_image.width()))
{
stream << "<html><body>";
PieceIndexToImagePos transform {torrentInfo, m_image};
int pieceIndex = transform.pieceIndex(imagePos);
const PieceIndexToImagePos transform {torrentInfo, m_image};
const int pieceIndex = transform.pieceIndex(imagePos);
const QVector<int> files {torrentInfo.fileIndicesForPiece(pieceIndex)};
QString tooltipTitle;
@@ -278,27 +280,28 @@ void PiecesBar::showToolTip(const QHelpEvent *e)
tooltipTitle = tr("File in these pieces");
}
DetailedTooltipRenderer renderer(stream, tooltipTitle);
toolTipText.reserve(files.size() * 128);
toolTipText += u"<html><body>";
for (int f : files)
DetailedTooltipRenderer renderer {toolTipText, tooltipTitle};
for (const int f : files)
{
const Path filePath = torrentInfo.filePath(f);
renderer(Utils::Misc::friendlyUnit(torrentInfo.fileSize(f)), filePath);
}
stream << "</body></html>";
toolTipText += u"</body></html>";
}
}
else
{
stream << simpleToolTipText();
toolTipText += simpleToolTipText();
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
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);
}