Changeset 13996
- Timestamp:
- Feb 9, 2013, 6:59:05 PM (9 years ago)
- Location:
- trunk/qt
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/qt/formatter.cc
r13978 r13996 25 25 namespace 26 26 { 27 28 29 27 unsigned int speed_K; 28 unsigned int mem_K; 29 unsigned int size_K; 30 30 } 31 31 … … 33 33 34 34 void 35 Formatter :: initUnits ()36 { 37 38 unitStrings[SPEED][B] = tr( "B/s");39 unitStrings[SPEED][KB] = tr( "kB/s");40 unitStrings[SPEED][MB] = tr( "MB/s");41 unitStrings[SPEED][GB] = tr( "GB/s");42 unitStrings[SPEED][TB] = tr( "TB/s");43 tr_formatter_speed_init(speed_K,44 unitStrings[SPEED][KB].toUtf8().constData(),45 unitStrings[SPEED][MB].toUtf8().constData(),46 unitStrings[SPEED][GB].toUtf8().constData(),47 unitStrings[SPEED][TB].toUtf8().constData());48 49 50 unitStrings[SIZE][B] = tr( "B");51 unitStrings[SIZE][KB] = tr( "kB");52 unitStrings[SIZE][MB] = tr( "MB");53 unitStrings[SIZE][GB] = tr( "GB");54 unitStrings[SIZE][TB] = tr( "TB");55 tr_formatter_size_init(size_K,56 unitStrings[SIZE][KB].toUtf8().constData(),57 unitStrings[SIZE][MB].toUtf8().constData(),58 unitStrings[SIZE][GB].toUtf8().constData(),59 unitStrings[SIZE][TB].toUtf8().constData());60 61 62 unitStrings[MEM][B] = tr( "B");63 unitStrings[MEM][KB] = tr( "KiB");64 unitStrings[MEM][MB] = tr( "MiB");65 unitStrings[MEM][GB] = tr( "GiB");66 unitStrings[MEM][TB] = tr( "TiB");67 tr_formatter_mem_init(mem_K,68 unitStrings[MEM][KB].toUtf8().constData(),69 unitStrings[MEM][MB].toUtf8().constData(),70 unitStrings[MEM][GB].toUtf8().constData(),71 unitStrings[MEM][TB].toUtf8().constData());35 Formatter :: initUnits () 36 { 37 speed_K = 1000; 38 unitStrings[SPEED][B] = tr ( "B/s"); 39 unitStrings[SPEED][KB] = tr ("kB/s"); 40 unitStrings[SPEED][MB] = tr ("MB/s"); 41 unitStrings[SPEED][GB] = tr ("GB/s"); 42 unitStrings[SPEED][TB] = tr ("TB/s"); 43 tr_formatter_speed_init (speed_K, 44 unitStrings[SPEED][KB].toUtf8 ().constData (), 45 unitStrings[SPEED][MB].toUtf8 ().constData (), 46 unitStrings[SPEED][GB].toUtf8 ().constData (), 47 unitStrings[SPEED][TB].toUtf8 ().constData ()); 48 49 size_K = 1000; 50 unitStrings[SIZE][B] = tr ( "B"); 51 unitStrings[SIZE][KB] = tr ("kB"); 52 unitStrings[SIZE][MB] = tr ("MB"); 53 unitStrings[SIZE][GB] = tr ("GB"); 54 unitStrings[SIZE][TB] = tr ("TB"); 55 tr_formatter_size_init (size_K, 56 unitStrings[SIZE][KB].toUtf8 ().constData (), 57 unitStrings[SIZE][MB].toUtf8 ().constData (), 58 unitStrings[SIZE][GB].toUtf8 ().constData (), 59 unitStrings[SIZE][TB].toUtf8 ().constData ()); 60 61 mem_K = 1024; 62 unitStrings[MEM][B] = tr ( "B"); 63 unitStrings[MEM][KB] = tr ("KiB"); 64 unitStrings[MEM][MB] = tr ("MiB"); 65 unitStrings[MEM][GB] = tr ("GiB"); 66 unitStrings[MEM][TB] = tr ("TiB"); 67 tr_formatter_mem_init (mem_K, 68 unitStrings[MEM][KB].toUtf8 ().constData (), 69 unitStrings[MEM][MB].toUtf8 ().constData (), 70 unitStrings[MEM][GB].toUtf8 ().constData (), 71 unitStrings[MEM][TB].toUtf8 ().constData ()); 72 72 } 73 73 … … 77 77 78 78 double 79 Speed :: KBps () const80 { 81 79 Speed :: KBps () const 80 { 81 return _Bps / (double)speed_K; 82 82 } 83 83 84 84 Speed 85 Speed :: fromKBps ( double KBps)86 { 87 return int( KBps * speed_K);85 Speed :: fromKBps (double KBps) 86 { 87 return int (KBps * speed_K); 88 88 } 89 89 … … 93 93 94 94 QString 95 Formatter :: memToString( int64_t bytes ) 96 { 97 if( bytes < 1 ) 98 return tr( "Unknown" ); 99 else if( !bytes ) 100 return tr( "None" ); 101 else { 102 char buf[128]; 103 tr_formatter_mem_B( buf, bytes, sizeof( buf ) ); 104 return QString::fromUtf8( buf ); 105 } 106 } 107 108 QString 109 Formatter :: sizeToString( int64_t bytes ) 110 { 111 if( bytes < 1 ) 112 return tr( "Unknown" ); 113 else if( !bytes ) 114 return tr( "None" ); 115 else { 116 char buf[128]; 117 tr_formatter_size_B( buf, bytes, sizeof( buf ) ); 118 return QString::fromUtf8( buf ); 119 } 120 } 121 122 QString 123 Formatter :: speedToString( const Speed& speed ) 124 { 125 char buf[128]; 126 tr_formatter_speed_KBps( buf, speed.KBps( ), sizeof( buf ) ); 127 return QString::fromUtf8( buf ); 128 } 129 130 QString 131 Formatter :: percentToString( double x ) 132 { 133 char buf[128]; 134 return QString( tr_strpercent( buf, x, sizeof(buf) ) ); 135 } 136 137 QString 138 Formatter :: ratioToString( double ratio ) 139 { 140 char buf[128]; 141 return QString::fromUtf8( tr_strratio( buf, sizeof(buf), ratio, "\xE2\x88\x9E" ) ); 142 } 143 144 QString 145 Formatter :: timeToString( int seconds ) 146 { 147 int days, hours, minutes; 148 QString d, h, m, s; 149 QString str; 150 151 if( seconds < 0 ) 152 seconds = 0; 153 154 days = seconds / 86400; 155 hours = ( seconds % 86400 ) / 3600; 156 minutes = ( seconds % 3600 ) / 60; 157 seconds %= 60; 158 159 d = tr( "%Ln day(s)", 0, days ); 160 h = tr( "%Ln hour(s)", 0, hours ); 161 m = tr( "%Ln minute(s)", 0, minutes ); 162 s = tr( "%Ln second(s)", 0, seconds ); 163 164 if( days ) 165 { 166 if( days >= 4 || !hours ) 167 str = d; 168 else 169 str = tr( "%1, %2" ).arg( d ).arg( h ); 170 } 171 else if( hours ) 172 { 173 if( hours >= 4 || !minutes ) 174 str = h; 175 else 176 str = tr( "%1, %2" ).arg( h ).arg( m ); 177 } 178 else if( minutes ) 179 { 180 if( minutes >= 4 || !seconds ) 181 str = m; 182 else 183 str = tr( "%1, %2" ).arg( m ).arg( s ); 184 } 185 else 186 { 187 str = s; 188 } 189 190 return str; 95 Formatter :: memToString (int64_t bytes) 96 { 97 if (bytes < 0) 98 return tr ("Unknown"); 99 100 if (!bytes) 101 return tr ("None"); 102 103 char buf[128]; 104 tr_formatter_mem_B (buf, bytes, sizeof (buf)); 105 return QString::fromUtf8 (buf); 106 } 107 108 QString 109 Formatter :: sizeToString (int64_t bytes) 110 { 111 if (bytes < 0) 112 return tr ("Unknown"); 113 114 if (!bytes) 115 return tr ("None"); 116 117 char buf[128]; 118 tr_formatter_size_B (buf, bytes, sizeof (buf)); 119 return QString::fromUtf8 (buf); 120 } 121 122 QString 123 Formatter :: speedToString (const Speed& speed) 124 { 125 char buf[128]; 126 tr_formatter_speed_KBps (buf, speed.KBps (), sizeof (buf)); 127 return QString::fromUtf8 (buf); 191 128 } 192 129 … … 196 133 static const QChar uploadSymbol (0x25B4); 197 134 198 return tr ( "%1 %2").arg(speedToString(uploadSpeed)).arg(uploadSymbol);135 return tr ("%1 %2").arg (speedToString (uploadSpeed)).arg (uploadSymbol); 199 136 } 200 137 … … 204 141 static const QChar downloadSymbol (0x25BE); 205 142 206 return tr( "%1 %2").arg(speedToString(downloadSpeed)).arg(downloadSymbol); 207 } 208 143 return tr ("%1 %2").arg (speedToString (downloadSpeed)).arg (downloadSymbol); 144 } 145 146 QString 147 Formatter :: percentToString (double x) 148 { 149 char buf[128]; 150 return QString (tr_strpercent (buf, x, sizeof (buf))); 151 } 152 153 QString 154 Formatter :: ratioToString (double ratio) 155 { 156 char buf[128]; 157 return QString::fromUtf8 (tr_strratio (buf, sizeof (buf), ratio, "\xE2\x88\x9E")); 158 } 159 160 QString 161 Formatter :: timeToString (int seconds) 162 { 163 int days, hours, minutes; 164 QString d, h, m, s; 165 QString str; 166 167 if (seconds < 0) 168 seconds = 0; 169 170 days = seconds / 86400; 171 hours = (seconds % 86400) / 3600; 172 minutes = (seconds % 3600) / 60; 173 seconds %= 60; 174 175 d = tr ("%Ln day (s)", 0, days); 176 h = tr ("%Ln hour (s)", 0, hours); 177 m = tr ("%Ln minute (s)", 0, minutes); 178 s = tr ("%Ln second (s)", 0, seconds); 179 180 if (days) 181 { 182 if (days >= 4 || !hours) 183 str = d; 184 else 185 str = tr ("%1, %2").arg (d).arg (h); 186 } 187 else if (hours) 188 { 189 if (hours >= 4 || !minutes) 190 str = h; 191 else 192 str = tr ("%1, %2").arg (h).arg (m); 193 } 194 else if (minutes) 195 { 196 if (minutes >= 4 || !seconds) 197 str = m; 198 else 199 str = tr ("%1, %2").arg (m).arg (s); 200 } 201 else 202 { 203 str = s; 204 } 205 206 return str; 207 } -
trunk/qt/formatter.h
r13978 r13996 1 1 /* 2 * This file Copyright 2 * This file Copyright(C) Mnemosyne LLC 3 3 * 4 4 * This program is free software; you can redistribute it and/or modify … … 24 24 class Formatter: public QObject 25 25 { 26 26 Q_OBJECT 27 27 28 28 public: 29 29 30 Formatter( ) {}31 virtual ~Formatter( ) {}30 Formatter() {} 31 virtual ~Formatter() {} 32 32 33 33 public: 34 34 35 static QString memToString( int64_t bytes);36 static QString sizeToString( int64_t bytes);37 static QString speedToString( const Speed& speed);38 static QString percentToString( double x);39 static QString ratioToString( double ratio);40 static QString timeToString( int seconds);41 static QString uploadSpeedToString(const Speed& up);42 static QString downloadSpeedToString(const Speed& down);35 static QString memToString (int64_t bytes); 36 static QString sizeToString (int64_t bytes); 37 static QString speedToString (const Speed& speed); 38 static QString percentToString (double x); 39 static QString ratioToString (double ratio); 40 static QString timeToString (int seconds); 41 static QString uploadSpeedToString(const Speed& up); 42 static QString downloadSpeedToString(const Speed& down); 43 43 44 44 public: 45 45 46 47 48 static QString unitStr( Type t, Size s) { return unitStrings[t][s]; }49 static void initUnits();46 typedef enum { B, KB, MB, GB, TB } Size; 47 typedef enum { SPEED, SIZE, MEM } Type; 48 static QString unitStr (Type t, Size s) { return unitStrings[t][s]; } 49 static void initUnits (); 50 50 51 51 private: 52 52 53 53 static QString unitStrings[3][5]; 54 54 }; 55 55
Note: See TracChangeset
for help on using the changeset viewer.