Browse Source

Convert more accurately decimal numbers to text.

adaptive-webui-19844
sledgehammer999 11 years ago
parent
commit
441d1d08ba
  1. 12
      src/mainwindow.cpp
  2. 22
      src/misc.cpp
  3. 1
      src/misc.h
  4. 4
      src/previewlistdelegate.h
  5. 4
      src/properties/peerlistdelegate.h
  6. 12
      src/properties/propertieswidget.cpp
  7. 4
      src/properties/proplistdelegate.h
  8. 8
      src/statsdialog.cpp
  9. 12
      src/transferlistdelegate.h
  10. 8
      src/webui/btjson.cpp

12
src/mainwindow.cpp

@ -1139,20 +1139,16 @@ void MainWindow::updateGUI() { @@ -1139,20 +1139,16 @@ void MainWindow::updateGUI() {
html += "qBittorrent";
html += "</div>";
html += "<div style='vertical-align: baseline; height: 18px;'>";
/* 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 */
html += "<img src=':/Icons/skin/download.png'/>&nbsp;"+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 += "<img src=':/Icons/skin/download.png'/>&nbsp;"+tr("DL speed: %1 KiB/s", "e.g: Download speed: 10 KiB/s").arg(misc::accurateDoubleToString(QBtSession::instance()->getPayloadDownloadRate()/1024., 1));
html += "</div>";
html += "<div style='vertical-align: baseline; height: 18px;'>";
html += "<img src=':/Icons/skin/seeding.png'/>&nbsp;"+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'/>&nbsp;"+tr("UP speed: %1 KiB/s", "e.g: Upload speed: 10 KiB/s").arg(misc::accurateDoubleToString(QBtSession::instance()->getPayloadUploadRate()/1024., 1));
html += "</div>";
#else
// 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
** 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));
QString html =tr("DL speed: %1 KiB/s", "e.g: Download speed: 10 KiB/s").arg(misc::accurateDoubleToString(QBtSession::instance()->getPayloadDownloadRate()/1024., 1));
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
systrayIcon->setToolTip(html); // tray icon
}

22
src/misc.cpp

@ -30,6 +30,8 @@ @@ -30,6 +30,8 @@
#include "misc.h"
#include <cmath>
#include <QUrl>
#include <QDir>
#include <QFileInfo>
@ -38,6 +40,7 @@ @@ -38,6 +40,7 @@
#include <QDebug>
#include <QProcess>
#include <QSettings>
#include <QLocale>
#ifdef DISABLE_GUI
#include <QCoreApplication>
@ -255,9 +258,7 @@ QString misc::friendlyUnit(qreal val, bool is_speed) { @@ -255,9 +258,7 @@ 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
/* 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);
ret = accurateDoubleToString(val, 1) + " " + QCoreApplication::translate("misc", units[i].source, units[i].comment);
if (is_speed)
ret += QCoreApplication::translate("misc", "/s", "per second");
return ret;
@ -572,3 +573,18 @@ bool misc::naturalSort(QString left, QString right, bool &result) { // uses less @@ -572,3 +573,18 @@ bool misc::naturalSort(QString left, QString right, bool &result) { // uses less
return false;
}
#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);
}

1
src/misc.h

@ -108,6 +108,7 @@ namespace misc @@ -108,6 +108,7 @@ namespace misc
QList<bool> boolListfromStringList(const QStringList &l);
QString toQString(time_t t);
QString accurateDoubleToString(double n, int precision);
#ifndef DISABLE_GUI
bool naturalSort(QString left, QString right, bool& result);

4
src/previewlistdelegate.h

@ -65,9 +65,7 @@ class PreviewListDelegate: public QItemDelegate { @@ -65,9 +65,7 @@ class PreviewListDelegate: public QItemDelegate {
QStyleOptionProgressBarV2 newopt;
qreal progress = index.data().toDouble()*100.;
newopt.rect = opt.rect;
/* 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.text = misc::accurateDoubleToString(progress, 1);
newopt.progress = (int)progress;
newopt.maximum = 100;
newopt.minimum = 0;

4
src/properties/peerlistdelegate.h

@ -67,9 +67,7 @@ public: @@ -67,9 +67,7 @@ public:
case PROGRESS:{
QItemDelegate::drawBackground(painter, opt, index);
qreal progress = index.data().toDouble();
/* 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)+"%");
QItemDelegate::drawDisplay(painter, opt, opt.rect, misc::accurateDoubleToString(progress*100.0, 1)+"%");
break;
}
default:

12
src/properties/propertieswidget.cpp

@ -336,9 +336,7 @@ void PropertiesWidget::loadDynamicData() { @@ -336,9 +336,7 @@ void PropertiesWidget::loadDynamicData() {
reannounce_lbl->setText(h.next_announce());
// Update ratio info
const qreal ratio = QBtSession::instance()->getRealRatio(h.hash());
/* 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 */
shareRatio->setText(ratio > QBtSession::MAX_RATIO ? QString::fromUtf8("") : QString::number((int)(ratio*100)/100.0, 'f', 2));
shareRatio->setText(ratio > QBtSession::MAX_RATIO ? QString::fromUtf8("") : misc::accurateDoubleToString(ratio*100, 2));
if (!h.is_seed()) {
showPiecesDownloaded(true);
// Downloaded pieces
@ -355,17 +353,13 @@ void PropertiesWidget::loadDynamicData() { @@ -355,17 +353,13 @@ void PropertiesWidget::loadDynamicData() {
std::vector<int> avail;
h.piece_availability(avail);
pieces_availability->setAvailability(avail);
/* 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));
avail_average_lbl->setText(misc::accurateDoubleToString(h.distributed_copies(), 3));
} else {
showPiecesAvailability(false);
}
// Progress
qreal progress = h.progress()*100.;
/* 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)+"%");
progress_lbl->setText(misc::accurateDoubleToString(progress, 1)+"%");
} else {
showPiecesAvailability(false);
showPiecesDownloaded(false);

4
src/properties/proplistdelegate.h

@ -78,9 +78,7 @@ public: @@ -78,9 +78,7 @@ public:
QStyleOptionProgressBarV2 newopt;
qreal progress = index.data().toDouble()*100.;
newopt.rect = opt.rect;
/* 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.text = misc::accurateDoubleToString(progress, 1);
newopt.progress = (int)progress;
newopt.maximum = 100;
newopt.minimum = 0;

8
src/statsdialog.cpp

@ -67,13 +67,13 @@ void StatsDialog::updateUI() { @@ -67,13 +67,13 @@ void StatsDialog::updateUI() {
// Global ratio
ui->labelGlobalRatio->setText(
( atd > 0 && atu > 0 ) ?
QString::number( (qreal)atu / (qreal)atd, 'f', 2) :
misc::accurateDoubleToString((qreal)atu / (qreal)atd, 2) :
"-"
);
// Cache hits
ui->labelCacheHits->setText(
( 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
@ -91,12 +91,12 @@ void StatsDialog::updateUI() { @@ -91,12 +91,12 @@ void StatsDialog::updateUI() {
peers += (*iBegin).status().num_peers;
ui->labelWriteStarve->setText(
( 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\%")
);
ui->labelReadStarve->setText(
( 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\%")
);
// Disk queues

12
src/transferlistdelegate.h

@ -138,9 +138,7 @@ public: @@ -138,9 +138,7 @@ public:
case TorrentModelItem::TR_DLLIMIT:{
QItemDelegate::drawBackground(painter, opt, index);
const qlonglong limit = index.data().toLongLong();
/* 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, limit > 0 ? QString::number((int)((limit/1024.)*10)/10.0, 'f', 1) + " " + tr("KiB/s", "KiB/second (.i.e per second)") : QString::fromUtf8(""));
QItemDelegate::drawDisplay(painter, opt, opt.rect, limit > 0 ? misc::accurateDoubleToString(limit/1024., 1) + " " + tr("KiB/s", "KiB/second (.i.e per second)") : QString::fromUtf8(""));
break;
}
case TorrentModelItem::TR_TIME_ELAPSED: {
@ -160,9 +158,7 @@ public: @@ -160,9 +158,7 @@ public:
case TorrentModelItem::TR_RATIO:{
QItemDelegate::drawBackground(painter, opt, index);
const qreal ratio = index.data().toDouble();
/* 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, ratio > QBtSession::MAX_RATIO ? QString::fromUtf8("") : QString::number((int)(ratio*100)/100.0, 'f', 2));
QItemDelegate::drawDisplay(painter, opt, opt.rect, ratio > QBtSession::MAX_RATIO ? QString::fromUtf8("") : misc::accurateDoubleToString(ratio, 2));
break;
}
case TorrentModelItem::TR_PRIORITY: {
@ -179,9 +175,7 @@ public: @@ -179,9 +175,7 @@ public:
QStyleOptionProgressBarV2 newopt;
qreal progress = index.data().toDouble()*100.;
newopt.rect = opt.rect;
/* 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.text = misc::accurateDoubleToString(progress, 1);
newopt.progress = (int)progress;
newopt.maximum = 100;
newopt.minimum = 0;

8
src/webui/btjson.cpp

@ -143,9 +143,7 @@ static JsonDict toJson(const QTorrentHandle& h) @@ -143,9 +143,7 @@ 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());
/* 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));
ret.add(KEY_TORRENT_RATIO, (ratio > 100.) ? QString::fromUtf8("") : misc::accurateDoubleToString(ratio, 1));
QString eta;
QString state;
if (h.is_paused()) {
@ -307,9 +305,7 @@ QString btjson::getPropertiesForTorrent(const QString& hash) @@ -307,9 +305,7 @@ 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());
/* 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));
data.add(KEY_PROP_RATIO, ratio > 100. ? QString::fromUtf8("") : misc::accurateDoubleToString(ratio, 1));
} catch(const std::exception& e) {
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what();
return QString();

Loading…
Cancel
Save