From d09b0d5f72dad4f885e2d309c6c15c01ab2649fd Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Tue, 10 Sep 2013 17:48:16 +0300 Subject: [PATCH] Fixed rounding bugs with floating numbers. --- src/mainwindow.cpp | 6 ++++-- src/misc.cpp | 4 +++- src/properties/peerlistdelegate.h | 4 +++- src/properties/propertieswidget.cpp | 10 ++++++---- src/transferlistdelegate.h | 18 ++++++++++-------- src/webui/btjson.cpp | 8 ++++++-- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 5f98e355c..dd68f5e9e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1135,9 +1135,11 @@ void MainWindow::updateGUI() { html += ""; #else // OSes such as Windows do not support html here - QString html =tr("DL speed: %1 KiB/s", "e.g: Download speed: 10 KiB/s").arg(QString::number(QBtSession::instance()->getPayloadDownloadRate()/1024., 'f', 1)); + /* 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 */ + 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 += tr("UP speed: %1 KiB/s", "e.g: Upload speed: 10 KiB/s").arg(QString::number(QBtSession::instance()->getPayloadUploadRate()/1024., 'f', 1)); + 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)); #endif systrayIcon->setToolTip(html); // tray icon } diff --git a/src/misc.cpp b/src/misc.cpp index a0adc6cbd..05b07453a 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -241,7 +241,9 @@ QString misc::friendlyUnit(qreal val, bool is_speed) { if (i == 0) ret = QString::number((long)val) + " " + QCoreApplication::translate("misc", units[0].source, units[0].comment); else - ret = QString::number(val, 'f', 1) + " " + QCoreApplication::translate("misc", units[i].source, units[i].comment); + /* 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 */ + ret = QString::number((int)(val*10)/10.0, 'f', 1) + " " + QCoreApplication::translate("misc", units[i].source, units[i].comment); if (is_speed) ret += QCoreApplication::translate("misc", "/s", "per second"); return ret; diff --git a/src/properties/peerlistdelegate.h b/src/properties/peerlistdelegate.h index fd66636ec..735ceaa56 100644 --- a/src/properties/peerlistdelegate.h +++ b/src/properties/peerlistdelegate.h @@ -67,7 +67,9 @@ public: case PROGRESS:{ QItemDelegate::drawBackground(painter, opt, index); qreal progress = index.data().toDouble(); - QItemDelegate::drawDisplay(painter, opt, opt.rect, QString::number(progress*100., 'f', 1)+"%"); + /* 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 */ + QItemDelegate::drawDisplay(painter, opt, opt.rect, QString::number((int)((progress*100.0)*10)/10.0, 'f', 1)+"%"); break; } default: diff --git a/src/properties/propertieswidget.cpp b/src/properties/propertieswidget.cpp index a99721eda..de83ccce2 100644 --- a/src/properties/propertieswidget.cpp +++ b/src/properties/propertieswidget.cpp @@ -354,15 +354,17 @@ void PropertiesWidget::loadDynamicData() { std::vector avail; h.piece_availability(avail); pieces_availability->setAvailability(avail); - avail_average_lbl->setText(QString::number(h.distributed_copies(), 'f', 3)); + /* 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 */ + avail_average_lbl->setText(QString::number((int)(h.distributed_copies()*1000)/1000.0, 'f', 3)); } else { showPiecesAvailability(false); } // Progress qreal progress = h.progress()*100.; - if (progress > 99.94 && progress < 100.) - progress = 99.9; - progress_lbl->setText(QString::number(progress, 'f', 1)+"%"); + /* 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 */ + progress_lbl->setText(QString::number((int)(progress*10)/10.0, 'f', 1)+"%"); } else { showPiecesAvailability(false); showPiecesDownloaded(false); diff --git a/src/transferlistdelegate.h b/src/transferlistdelegate.h index 778d2e770..101ec470f 100644 --- a/src/transferlistdelegate.h +++ b/src/transferlistdelegate.h @@ -134,7 +134,9 @@ public: const qlonglong limit = index.data().toLongLong(); opt.displayAlignment = Qt::AlignRight; if (limit > 0) - QItemDelegate::drawDisplay(painter, opt, opt.rect, QString::number(limit/1024., 'f', 1) + " " + tr("KiB/s", "KiB/second (.i.e per second)")); + /* 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 */ + QItemDelegate::drawDisplay(painter, opt, opt.rect, QString::number((int)((limit/1024.)*10)/10.0, 'f', 1) + " " + tr("KiB/s", "KiB/second (.i.e per second)")); else QItemDelegate::drawDisplay(painter, opt, opt.rect, QString::fromUtf8("∞")); break; @@ -160,7 +162,9 @@ public: if (ratio > QBtSession::MAX_RATIO) QItemDelegate::drawDisplay(painter, opt, opt.rect, QString::fromUtf8("∞")); else - QItemDelegate::drawDisplay(painter, opt, opt.rect, QString::number(ratio, 'f', 2)); + /* 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 */ + QItemDelegate::drawDisplay(painter, opt, opt.rect, QString::number((int)(ratio*100)/100.0, 'f', 2)); break; } case TorrentModelItem::TR_PRIORITY: { @@ -177,13 +181,11 @@ public: } case TorrentModelItem::TR_PROGRESS:{ QStyleOptionProgressBarV2 newopt; - qreal progress = index.data().toDouble()*100.; - // We don't want to display 100% unless - // the torrent is really complete - if (progress > 99.94 && progress < 100.) - progress = 99.9; + qreal progress = index.data().toDouble()*100.; newopt.rect = opt.rect; - newopt.text = QString::number(progress, 'f', 1)+"%"; + /* 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 */ + newopt.text = QString::number((int)(progress*10)/10.0, 'f', 1)+"%"; newopt.progress = (int)progress; newopt.maximum = 100; newopt.minimum = 0; diff --git a/src/webui/btjson.cpp b/src/webui/btjson.cpp index 3fd68eb7b..2957bb148 100644 --- a/src/webui/btjson.cpp +++ b/src/webui/btjson.cpp @@ -143,7 +143,9 @@ static JsonDict toJson(const QTorrentHandle& h) leechs += " ("+QString::number(h.num_incomplete())+")"; ret.add(KEY_TORRENT_LEECHS, leechs); const qreal ratio = QBtSession::instance()->getRealRatio(h.hash()); - ret.add(KEY_TORRENT_RATIO, (ratio > 100.) ? QString::fromUtf8("∞") : QString::number(ratio, 'f', 1)); + /* 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 */ + ret.add(KEY_TORRENT_RATIO, (ratio > 100.) ? QString::fromUtf8("∞") : QString::number((int)(ratio*10)/10.0, 'f', 1)); QString eta; QString state; if (h.is_paused()) { @@ -311,7 +313,9 @@ QString btjson::getPropertiesForTorrent(const QString& hash) 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())) + ")")); const qreal ratio = QBtSession::instance()->getRealRatio(h.hash()); - data.add(KEY_PROP_RATIO, ratio > 100. ? QString::fromUtf8("∞") : QString::number(ratio, 'f', 1)); + /* 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 */ + data.add(KEY_PROP_RATIO, ratio > 100. ? QString::fromUtf8("∞") : QString::number((int)(ratio*10)/10.0, 'f', 1)); } catch(const std::exception& e) { qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what(); return QString();