mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-27 06:54:20 +00:00
Convert more accurately decimal numbers to text.
This commit is contained in:
parent
4d3672f894
commit
441d1d08ba
@ -1139,20 +1139,16 @@ void MainWindow::updateGUI() {
|
|||||||
html += "qBittorrent";
|
html += "qBittorrent";
|
||||||
html += "</div>";
|
html += "</div>";
|
||||||
html += "<div style='vertical-align: baseline; height: 18px;'>";
|
html += "<div style='vertical-align: baseline; height: 18px;'>";
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
html += "<img src=':/Icons/skin/download.png'/> "+tr("DL speed: %1 KiB/s", "e.g: Download speed: 10 KiB/s").arg(misc::accurateDoubleToString(QBtSession::instance()->getPayloadDownloadRate()/1024., 1));
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
html += "<img src=':/Icons/skin/download.png'/> "+tr("DL speed: %1 KiB/s", "e.g: Download speed: 10 KiB/s").arg(QString::number((int)((QBtSession::instance()->getPayloadDownloadRate()/1024.)*10)/10.0, 'f', 1));
|
|
||||||
html += "</div>";
|
html += "</div>";
|
||||||
html += "<div style='vertical-align: baseline; height: 18px;'>";
|
html += "<div style='vertical-align: baseline; height: 18px;'>";
|
||||||
html += "<img src=':/Icons/skin/seeding.png'/> "+tr("UP speed: %1 KiB/s", "e.g: Upload speed: 10 KiB/s").arg(QString::number((int)((QBtSession::instance()->getPayloadUploadRate()/1024.)*10)/10.0, 'f', 1));
|
html += "<img src=':/Icons/skin/seeding.png'/> "+tr("UP speed: %1 KiB/s", "e.g: Upload speed: 10 KiB/s").arg(misc::accurateDoubleToString(QBtSession::instance()->getPayloadUploadRate()/1024., 1));
|
||||||
html += "</div>";
|
html += "</div>";
|
||||||
#else
|
#else
|
||||||
// OSes such as Windows do not support html here
|
// OSes such as Windows do not support html here
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
QString html =tr("DL speed: %1 KiB/s", "e.g: Download speed: 10 KiB/s").arg(misc::accurateDoubleToString(QBtSession::instance()->getPayloadDownloadRate()/1024., 1));
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
QString html =tr("DL speed: %1 KiB/s", "e.g: Download speed: 10 KiB/s").arg(QString::number((int)((QBtSession::instance()->getPayloadDownloadRate()/1024.)*10)/10.0, 'f', 1));
|
|
||||||
html += "\n";
|
html += "\n";
|
||||||
html += tr("UP speed: %1 KiB/s", "e.g: Upload speed: 10 KiB/s").arg(QString::number((int)((QBtSession::instance()->getPayloadUploadRate()/1024.)*10)/10.0, 'f', 1));
|
html += tr("UP speed: %1 KiB/s", "e.g: Upload speed: 10 KiB/s").arg(misc::accurateDoubleToString(QBtSession::instance()->getPayloadUploadRate()/1024., 1));
|
||||||
#endif
|
#endif
|
||||||
systrayIcon->setToolTip(html); // tray icon
|
systrayIcon->setToolTip(html); // tray icon
|
||||||
}
|
}
|
||||||
|
22
src/misc.cpp
22
src/misc.cpp
@ -30,6 +30,8 @@
|
|||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
@ -38,6 +40,7 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
#include <QLocale>
|
||||||
|
|
||||||
#ifdef DISABLE_GUI
|
#ifdef DISABLE_GUI
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
@ -255,9 +258,7 @@ QString misc::friendlyUnit(qreal val, bool is_speed) {
|
|||||||
if (i == 0)
|
if (i == 0)
|
||||||
ret = QString::number((long)val) + " " + QCoreApplication::translate("misc", units[0].source, units[0].comment);
|
ret = QString::number((long)val) + " " + QCoreApplication::translate("misc", units[0].source, units[0].comment);
|
||||||
else
|
else
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
ret = accurateDoubleToString(val, 1) + " " + QCoreApplication::translate("misc", units[i].source, units[i].comment);
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
ret = QString::number((int)(val*10)/10.0, 'f', 1) + " " + QCoreApplication::translate("misc", units[i].source, units[i].comment);
|
|
||||||
if (is_speed)
|
if (is_speed)
|
||||||
ret += QCoreApplication::translate("misc", "/s", "per second");
|
ret += QCoreApplication::translate("misc", "/s", "per second");
|
||||||
return ret;
|
return ret;
|
||||||
@ -572,3 +573,18 @@ bool misc::naturalSort(QString left, QString right, bool &result) { // uses less
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
QString misc::accurateDoubleToString(double n, int precision) {
|
||||||
|
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
||||||
|
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 The problem manifests when
|
||||||
|
** the number has more digits after the decimal than we want AND the digit after
|
||||||
|
** our 'wanted' is >= 5. In this case our last digit gets rounded up. So for each
|
||||||
|
** precision we add an extra 0 behind 1 in the below algorithm.
|
||||||
|
** However this, approach has a drawback. eg (99,99, 2) returns 99.98 and (99.99, 3)
|
||||||
|
** returns 99.989. This is a minor issue because mostly we want to use precision 1 or 2
|
||||||
|
** and the double fed into this function will have more decimal places than required
|
||||||
|
** precision anyway.*/
|
||||||
|
|
||||||
|
double prec = std::pow(10.0, precision);
|
||||||
|
return QLocale::system().toString((int)(n*(int)prec)/prec, 'f', precision);
|
||||||
|
}
|
||||||
|
@ -108,6 +108,7 @@ namespace misc
|
|||||||
QList<bool> boolListfromStringList(const QStringList &l);
|
QList<bool> boolListfromStringList(const QStringList &l);
|
||||||
|
|
||||||
QString toQString(time_t t);
|
QString toQString(time_t t);
|
||||||
|
QString accurateDoubleToString(double n, int precision);
|
||||||
|
|
||||||
#ifndef DISABLE_GUI
|
#ifndef DISABLE_GUI
|
||||||
bool naturalSort(QString left, QString right, bool& result);
|
bool naturalSort(QString left, QString right, bool& result);
|
||||||
|
@ -65,9 +65,7 @@ class PreviewListDelegate: public QItemDelegate {
|
|||||||
QStyleOptionProgressBarV2 newopt;
|
QStyleOptionProgressBarV2 newopt;
|
||||||
qreal progress = index.data().toDouble()*100.;
|
qreal progress = index.data().toDouble()*100.;
|
||||||
newopt.rect = opt.rect;
|
newopt.rect = opt.rect;
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
newopt.text = misc::accurateDoubleToString(progress, 1);
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
newopt.text = QString::number((int)(progress*10)/10.0, 'f', 1)+"%";
|
|
||||||
newopt.progress = (int)progress;
|
newopt.progress = (int)progress;
|
||||||
newopt.maximum = 100;
|
newopt.maximum = 100;
|
||||||
newopt.minimum = 0;
|
newopt.minimum = 0;
|
||||||
|
@ -67,9 +67,7 @@ public:
|
|||||||
case PROGRESS:{
|
case PROGRESS:{
|
||||||
QItemDelegate::drawBackground(painter, opt, index);
|
QItemDelegate::drawBackground(painter, opt, index);
|
||||||
qreal progress = index.data().toDouble();
|
qreal progress = index.data().toDouble();
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
QItemDelegate::drawDisplay(painter, opt, opt.rect, misc::accurateDoubleToString(progress*100.0, 1)+"%");
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
QItemDelegate::drawDisplay(painter, opt, opt.rect, QString::number((int)((progress*100.0)*10)/10.0, 'f', 1)+"%");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
@ -336,9 +336,7 @@ void PropertiesWidget::loadDynamicData() {
|
|||||||
reannounce_lbl->setText(h.next_announce());
|
reannounce_lbl->setText(h.next_announce());
|
||||||
// Update ratio info
|
// Update ratio info
|
||||||
const qreal ratio = QBtSession::instance()->getRealRatio(h.hash());
|
const qreal ratio = QBtSession::instance()->getRealRatio(h.hash());
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
shareRatio->setText(ratio > QBtSession::MAX_RATIO ? QString::fromUtf8("∞") : misc::accurateDoubleToString(ratio*100, 2));
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
shareRatio->setText(ratio > QBtSession::MAX_RATIO ? QString::fromUtf8("∞") : QString::number((int)(ratio*100)/100.0, 'f', 2));
|
|
||||||
if (!h.is_seed()) {
|
if (!h.is_seed()) {
|
||||||
showPiecesDownloaded(true);
|
showPiecesDownloaded(true);
|
||||||
// Downloaded pieces
|
// Downloaded pieces
|
||||||
@ -355,17 +353,13 @@ void PropertiesWidget::loadDynamicData() {
|
|||||||
std::vector<int> avail;
|
std::vector<int> avail;
|
||||||
h.piece_availability(avail);
|
h.piece_availability(avail);
|
||||||
pieces_availability->setAvailability(avail);
|
pieces_availability->setAvailability(avail);
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
avail_average_lbl->setText(misc::accurateDoubleToString(h.distributed_copies(), 3));
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
avail_average_lbl->setText(QString::number((int)(h.distributed_copies()*1000)/1000.0, 'f', 3));
|
|
||||||
} else {
|
} else {
|
||||||
showPiecesAvailability(false);
|
showPiecesAvailability(false);
|
||||||
}
|
}
|
||||||
// Progress
|
// Progress
|
||||||
qreal progress = h.progress()*100.;
|
qreal progress = h.progress()*100.;
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
progress_lbl->setText(misc::accurateDoubleToString(progress, 1)+"%");
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
progress_lbl->setText(QString::number((int)(progress*10)/10.0, 'f', 1)+"%");
|
|
||||||
} else {
|
} else {
|
||||||
showPiecesAvailability(false);
|
showPiecesAvailability(false);
|
||||||
showPiecesDownloaded(false);
|
showPiecesDownloaded(false);
|
||||||
|
@ -78,9 +78,7 @@ public:
|
|||||||
QStyleOptionProgressBarV2 newopt;
|
QStyleOptionProgressBarV2 newopt;
|
||||||
qreal progress = index.data().toDouble()*100.;
|
qreal progress = index.data().toDouble()*100.;
|
||||||
newopt.rect = opt.rect;
|
newopt.rect = opt.rect;
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
newopt.text = misc::accurateDoubleToString(progress, 1);
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
newopt.text = QString::number((int)(progress*10)/10.0, 'f', 1)+"%";
|
|
||||||
newopt.progress = (int)progress;
|
newopt.progress = (int)progress;
|
||||||
newopt.maximum = 100;
|
newopt.maximum = 100;
|
||||||
newopt.minimum = 0;
|
newopt.minimum = 0;
|
||||||
|
@ -67,13 +67,13 @@ void StatsDialog::updateUI() {
|
|||||||
// Global ratio
|
// Global ratio
|
||||||
ui->labelGlobalRatio->setText(
|
ui->labelGlobalRatio->setText(
|
||||||
( atd > 0 && atu > 0 ) ?
|
( atd > 0 && atu > 0 ) ?
|
||||||
QString::number( (qreal)atu / (qreal)atd, 'f', 2) :
|
misc::accurateDoubleToString((qreal)atu / (qreal)atd, 2) :
|
||||||
"-"
|
"-"
|
||||||
);
|
);
|
||||||
// Cache hits
|
// Cache hits
|
||||||
ui->labelCacheHits->setText(
|
ui->labelCacheHits->setText(
|
||||||
( cache.blocks_read > 0 && cache.blocks_read_hit > 0 ) ?
|
( cache.blocks_read > 0 && cache.blocks_read_hit > 0 ) ?
|
||||||
QString("%L1\%").arg(100. * (qreal)cache.blocks_read_hit / (qreal)cache.blocks_read, 0, 'f', 2) :
|
misc::accurateDoubleToString(100. * (qreal)cache.blocks_read_hit / (qreal)cache.blocks_read, 2) :
|
||||||
"-"
|
"-"
|
||||||
);
|
);
|
||||||
// Buffers size
|
// Buffers size
|
||||||
@ -91,12 +91,12 @@ void StatsDialog::updateUI() {
|
|||||||
peers += (*iBegin).status().num_peers;
|
peers += (*iBegin).status().num_peers;
|
||||||
ui->labelWriteStarve->setText(
|
ui->labelWriteStarve->setText(
|
||||||
( ss.disk_write_queue > 0 && peers > 0 ) ?
|
( ss.disk_write_queue > 0 && peers > 0 ) ?
|
||||||
QString("%L1\%").arg(100. * (qreal)ss.disk_write_queue / (qreal)peers, 0, 'f', 2) :
|
misc::accurateDoubleToString(100. * (qreal)ss.disk_write_queue / (qreal)peers, 2) :
|
||||||
QString("0\%")
|
QString("0\%")
|
||||||
);
|
);
|
||||||
ui->labelReadStarve->setText(
|
ui->labelReadStarve->setText(
|
||||||
( ss.disk_read_queue > 0 && peers > 0 ) ?
|
( ss.disk_read_queue > 0 && peers > 0 ) ?
|
||||||
QString("%L1\%").arg(100. * (qreal)ss.disk_read_queue / (qreal)peers, 0, 'f', 2) :
|
misc::accurateDoubleToString(100. * (qreal)ss.disk_read_queue / (qreal)peers, 2) :
|
||||||
QString("0\%")
|
QString("0\%")
|
||||||
);
|
);
|
||||||
// Disk queues
|
// Disk queues
|
||||||
|
@ -138,9 +138,7 @@ public:
|
|||||||
case TorrentModelItem::TR_DLLIMIT:{
|
case TorrentModelItem::TR_DLLIMIT:{
|
||||||
QItemDelegate::drawBackground(painter, opt, index);
|
QItemDelegate::drawBackground(painter, opt, index);
|
||||||
const qlonglong limit = index.data().toLongLong();
|
const qlonglong limit = index.data().toLongLong();
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
QItemDelegate::drawDisplay(painter, opt, opt.rect, limit > 0 ? misc::accurateDoubleToString(limit/1024., 1) + " " + tr("KiB/s", "KiB/second (.i.e per second)") : QString::fromUtf8("∞"));
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
QItemDelegate::drawDisplay(painter, opt, opt.rect, limit > 0 ? QString::number((int)((limit/1024.)*10)/10.0, 'f', 1) + " " + tr("KiB/s", "KiB/second (.i.e per second)") : QString::fromUtf8("∞"));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TorrentModelItem::TR_TIME_ELAPSED: {
|
case TorrentModelItem::TR_TIME_ELAPSED: {
|
||||||
@ -160,9 +158,7 @@ public:
|
|||||||
case TorrentModelItem::TR_RATIO:{
|
case TorrentModelItem::TR_RATIO:{
|
||||||
QItemDelegate::drawBackground(painter, opt, index);
|
QItemDelegate::drawBackground(painter, opt, index);
|
||||||
const qreal ratio = index.data().toDouble();
|
const qreal ratio = index.data().toDouble();
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
QItemDelegate::drawDisplay(painter, opt, opt.rect, ratio > QBtSession::MAX_RATIO ? QString::fromUtf8("∞") : misc::accurateDoubleToString(ratio, 2));
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
QItemDelegate::drawDisplay(painter, opt, opt.rect, ratio > QBtSession::MAX_RATIO ? QString::fromUtf8("∞") : QString::number((int)(ratio*100)/100.0, 'f', 2));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TorrentModelItem::TR_PRIORITY: {
|
case TorrentModelItem::TR_PRIORITY: {
|
||||||
@ -179,9 +175,7 @@ public:
|
|||||||
QStyleOptionProgressBarV2 newopt;
|
QStyleOptionProgressBarV2 newopt;
|
||||||
qreal progress = index.data().toDouble()*100.;
|
qreal progress = index.data().toDouble()*100.;
|
||||||
newopt.rect = opt.rect;
|
newopt.rect = opt.rect;
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
newopt.text = misc::accurateDoubleToString(progress, 1);
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
newopt.text = QString::number((int)(progress*10)/10.0, 'f', 1)+"%";
|
|
||||||
newopt.progress = (int)progress;
|
newopt.progress = (int)progress;
|
||||||
newopt.maximum = 100;
|
newopt.maximum = 100;
|
||||||
newopt.minimum = 0;
|
newopt.minimum = 0;
|
||||||
|
@ -143,9 +143,7 @@ static JsonDict toJson(const QTorrentHandle& h)
|
|||||||
leechs += " ("+QString::number(h.num_incomplete())+")";
|
leechs += " ("+QString::number(h.num_incomplete())+")";
|
||||||
ret.add(KEY_TORRENT_LEECHS, leechs);
|
ret.add(KEY_TORRENT_LEECHS, leechs);
|
||||||
const qreal ratio = QBtSession::instance()->getRealRatio(h.hash());
|
const qreal ratio = QBtSession::instance()->getRealRatio(h.hash());
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
ret.add(KEY_TORRENT_RATIO, (ratio > 100.) ? QString::fromUtf8("∞") : misc::accurateDoubleToString(ratio, 1));
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
ret.add(KEY_TORRENT_RATIO, (ratio > 100.) ? QString::fromUtf8("∞") : QString::number((int)(ratio*10)/10.0, 'f', 1));
|
|
||||||
QString eta;
|
QString eta;
|
||||||
QString state;
|
QString state;
|
||||||
if (h.is_paused()) {
|
if (h.is_paused()) {
|
||||||
@ -307,9 +305,7 @@ QString btjson::getPropertiesForTorrent(const QString& hash)
|
|||||||
data.add(KEY_PROP_TIME_ELAPSED, elapsed_txt);
|
data.add(KEY_PROP_TIME_ELAPSED, elapsed_txt);
|
||||||
data.add(KEY_PROP_CONNECT_COUNT, QString(QString::number(h.num_connections()) + " (" + tr("%1 max", "e.g. 10 max").arg(QString::number(h.connections_limit())) + ")"));
|
data.add(KEY_PROP_CONNECT_COUNT, QString(QString::number(h.num_connections()) + " (" + tr("%1 max", "e.g. 10 max").arg(QString::number(h.connections_limit())) + ")"));
|
||||||
const qreal ratio = QBtSession::instance()->getRealRatio(h.hash());
|
const qreal ratio = QBtSession::instance()->getRealRatio(h.hash());
|
||||||
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
|
data.add(KEY_PROP_RATIO, ratio > 100. ? QString::fromUtf8("∞") : misc::accurateDoubleToString(ratio, 1));
|
||||||
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */
|
|
||||||
data.add(KEY_PROP_RATIO, ratio > 100. ? QString::fromUtf8("∞") : QString::number((int)(ratio*10)/10.0, 'f', 1));
|
|
||||||
} catch(const std::exception& e) {
|
} catch(const std::exception& e) {
|
||||||
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what();
|
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what();
|
||||||
return QString();
|
return QString();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user