source: trunk/qt/formatter.cc @ 10955

Last change on this file since 10955 was 10955, checked in by charles, 12 years ago

(trunk T) #3045 "units" -- modify the formatter functions based on feedback from BMW

File size: 3.2 KB
Line 
1/*
2 * This file Copyright (C) 2010 Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id$
11 */
12
13#include <iostream>
14
15#include <libtransmission/transmission.h>
16#include <libtransmission/utils.h> // tr_formatter
17
18#include "formatter.h"
19#include "speed.h"
20
21/***
22****  Constants
23***/
24
25const int     Formatter :: speed_K     = 1000;
26const QString Formatter :: speed_K_str = "kB/s";
27const QString Formatter :: speed_M_str = "MB/s";
28const QString Formatter :: speed_G_str = "GB/s";
29const QString Formatter :: speed_T_str = "TB/s";
30
31const int     Formatter :: size_K     = 1000;
32const QString Formatter :: size_K_str = "kB";
33const QString Formatter :: size_M_str = "MB";
34const QString Formatter :: size_G_str = "GB";
35const QString Formatter :: size_T_str = "TB";
36
37const int     Formatter :: mem_K     = 1024;
38const QString Formatter :: mem_K_str = "KiB";
39const QString Formatter :: mem_M_str = "MiB";
40const QString Formatter :: mem_G_str = "GiB";
41const QString Formatter :: mem_T_str = "TiB";
42
43/***
44****
45***/
46
47Speed
48Speed :: fromKBps( double KBps )
49{
50    return KBps * Formatter::speed_K;
51}
52
53/***
54****
55***/
56
57QString
58Formatter :: memToString( double bytes )
59{
60    if( !bytes )
61        return tr( "None" );
62    else {
63        char buf[128];
64        tr_formatter_mem_B( buf, bytes, sizeof( buf ) );
65        return buf;
66    }
67}
68
69QString
70Formatter :: sizeToString( double bytes )
71{
72    if( !bytes )
73        return tr( "None" );
74    else {
75        char buf[128];
76        tr_formatter_size_B( buf, bytes, sizeof( buf ) );
77        return buf;
78    }
79}
80
81QString
82Formatter :: speedToString( const Speed& speed )
83{
84    if( speed.isZero( ) )
85        return tr( "None" );
86    else {
87        char buf[128];
88        tr_formatter_speed_KBps( buf, speed.Bps( ), sizeof( buf ) );
89        return buf;
90    }
91}
92
93QString
94Formatter :: percentToString( double x )
95{
96    char buf[128];
97    return QString( tr_strpercent( buf, x, sizeof(buf) ) );
98}
99
100QString
101Formatter :: ratioToString( double ratio )
102{
103    char buf[128];
104    return QString::fromUtf8( tr_strratio( buf, sizeof(buf), ratio, "\xE2\x88\x9E" ) );
105}
106
107QString
108Formatter :: timeToString( int seconds )
109{
110    int days, hours, minutes;
111    QString d, h, m, s;
112    QString str;
113
114    if( seconds < 0 )
115        seconds = 0;
116
117    days = seconds / 86400;
118    hours = ( seconds % 86400 ) / 3600;
119    minutes = ( seconds % 3600 ) / 60;
120    seconds %= 60;
121
122    d = tr( "%Ln day(s)", 0, days );
123    h = tr( "%Ln hour(s)", 0, hours );
124    m = tr( "%Ln minute(s)", 0, minutes );
125    s = tr( "%Ln second(s)", 0, seconds );
126
127    if( days )
128    {
129        if( days >= 4 || !hours )
130            str = d;
131        else
132            str = tr( "%1, %2" ).arg( d ).arg( h );
133    }
134    else if( hours )
135    {
136        if( hours >= 4 || !minutes )
137            str = h;
138        else
139            str = tr( "%1, %2" ).arg( h ).arg( m );
140    }
141    else if( minutes )
142    {
143        if( minutes >= 4 || !seconds )
144            str = m;
145        else
146            str = tr( "%1, %2" ).arg( m ).arg( s );
147    }
148    else
149    {
150        str = s;
151    }
152
153    return str;
154}
Note: See TracBrowser for help on using the repository browser.