Changeset 2907
- Timestamp:
- Aug 22, 2007, 7:49:24 PM (15 years ago)
- Location:
- trunk/wx
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wx/speed-stats.cc
r2849 r2907 19 19 */ 20 20 21 #include <iostream> 21 #include <wx/dcclient.h> 22 #include <wx/dcmemory.h> 23 #include <wx/event.h> 22 24 #include <wx/gbsizer.h> 23 25 #include <wx/stattext.h> 24 26 #include <wx/intl.h> 27 #include <libtransmission/transmission.h> 28 #include "foreach.h" 25 29 #include "speed-stats.h" 26 30 31 #define SNAPSHOT_PIXEL_WIDTH 8 32 27 33 BEGIN_EVENT_TABLE( SpeedStats, wxPanel ) 34 EVT_SIZE( SpeedStats::OnSize ) 35 EVT_PAINT( SpeedStats::OnPaint ) 28 36 END_EVENT_TABLE() 29 //EVT_PAINT( SpeedStats::OnPaint )30 37 31 38 SpeedStats :: SpeedStats( wxWindow * parent, … … 35 42 long style, 36 43 const wxString & name ): 37 wxPanel( parent, id, pos, size, style, name ) 38 { 39 } 40 41 void 42 SpeedStats :: Update( tr_handle_t * WXUNUSED(handle) ) 43 { 44 wxPanel( parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name ), 45 myBitmap( 0 ), 46 myTorrent( 0 ), 47 myMaxSpeed( -1 ), 48 myHistory( 0 ) 49 { 50 myColors[BACKGROUND] = wxColour( 0, 0, 0 ); // black 51 myColors[FRAME] = wxColour( 34, 139, 34 ); // forest green 52 myColors[ALL_UP] = wxColour( 255, 0, 0 ); 53 myColors[TORRENT_UP] = wxColour( 255, 255, 0 ); 54 55 myColors[ALL_DOWN] = wxColour( 255, 0, 255 ); 56 myColors[TORRENT_DOWN] = wxColour( 0, 255, 128 ); 57 } 58 59 SpeedStats :: ~SpeedStats() 60 { 61 delete myBitmap; 62 } 63 64 void 65 SpeedStats :: OnSize( wxSizeEvent& event ) 66 { 67 delete myBitmap; 68 69 const wxSize size = event.GetSize(); 70 myBitmap = new wxBitmap( size.GetWidth(), size.GetHeight() ); 71 myHistory = size.GetWidth() / SNAPSHOT_PIXEL_WIDTH; 44 72 } 45 73 … … 47 75 SpeedStats :: OnPaint( wxPaintEvent& WXUNUSED(event) ) 48 76 { 49 #if 0 50 int w, h; 51 mySpeedPanel->GetSize( &w, &h ); 52 wxMemoryDC dc; 53 wxBitmap bitmap( w, h ); 54 dc.SelectObject( bitmap ); 55 56 wxColour backgroundColor = *wxBLACK; 57 dc.SetBrush( wxBrush( backgroundColor ) ); 58 dc.SetPen( wxBrush( backgroundColor ) ); 59 dc.DrawRectangle( 0, 0, w, h ); 60 61 std::cerr << "paint" << std::endl; 62 #endif 63 } 77 const int draw_width = myBitmap->GetWidth(); 78 const int draw_height = myBitmap->GetHeight(); 79 80 const int top = (((int)myMaxSpeed + 11) / 10) * 10; 81 const double y_scale = (double)draw_height / top; 82 const int num_bars = 4; 83 const int per_bar = top / num_bars; 84 85 // clear 86 wxMemoryDC memDC( *myBitmap ); 87 memDC.SetBackground( myColors[BACKGROUND] ); 88 memDC.Clear( ); 89 90 // draw the frame 91 92 memDC.SetPen( wxPen ( myColors[FRAME] ) ); 93 memDC.SetTextForeground( myColors[FRAME] ); 94 95 const int fontsize = 10; 96 const int dely = int( draw_height / num_bars ); 97 98 wxString xstr; 99 100 memDC.SetFont( wxFont ( fontsize, wxFONTFAMILY_SWISS, 101 wxFONTSTYLE_NORMAL, 102 wxFONTWEIGHT_NORMAL ) ); 103 104 for( int i=0; i<=num_bars; ++i ) 105 { 106 const int y = (int)(draw_height - (i*dely+0.5)); 107 108 // line 109 memDC.DrawLine( wxCoord(0), wxCoord(draw_height - (i*dely+0.5)), 110 wxCoord(draw_width), wxCoord(draw_height - (i*dely+0.5)) ); 111 112 xstr.Printf( _("%d KiB/s"), (per_bar*i) ); 113 memDC.DrawText( xstr, wxCoord(0), wxCoord(y+2) ); 114 } 115 116 const int n = myStats.size( ); 117 if( n ) 118 { 119 wxPoint * points = new wxPoint[ n ]; 120 121 int x = draw_width - (n * SNAPSHOT_PIXEL_WIDTH); 122 for( int i=0; i<n; ++i ) { 123 points[i].x = x; 124 x += SNAPSHOT_PIXEL_WIDTH; 125 } 126 127 // torrent upload 128 for( int i=0; i<n; ++i ) 129 points[i].y = draw_height - 10 - int(myStats[i].torrentUp * y_scale); 130 memDC.SetPen( wxPen ( myColors[TORRENT_UP] ) ); 131 memDC.DrawLines( n, points, 0, 0 ); 132 133 // torrent download 134 for( int i=0; i<n; ++i ) 135 points[i].y = draw_height - int(myStats[i].torrentDown * y_scale); 136 memDC.SetPen( wxPen ( myColors[TORRENT_DOWN] ) ); 137 memDC.DrawLines( n, points, 0, 0 ); 138 139 // all upload 140 for( int i=0; i<n; ++i ) 141 points[i].y = draw_height - int(myStats[i].torrentUp * y_scale); 142 memDC.SetPen( wxPen ( myColors[ALL_UP] ) ); 143 memDC.DrawLines( n, points, 0, 0 ); 144 145 // all download 146 for( int i=0; i<n; ++i ) 147 points[i].y = draw_height - int(myStats[i].torrentDown * y_scale); 148 memDC.SetPen( wxPen ( myColors[ALL_DOWN] ) ); 149 memDC.DrawLines( n, points, 0, 0 ); 150 151 delete [] points; 152 } 153 154 wxPaintDC dc( this ); 155 dc.Blit( 0, 0, draw_width, draw_height, &memDC, 0, 0 ); 156 memDC.SelectObject( wxNullBitmap ); 157 } 158 159 void 160 SpeedStats :: SetTorrent( tr_torrent_t * tor ) 161 { 162 if( tor != myTorrent ) 163 { 164 myTorrent = tor; 165 166 myMaxSpeed = 0; 167 foreach( stats_t, myStats, it ) 168 { 169 it->torrentUp = 0; 170 it->torrentDown = 0; 171 myMaxSpeed = std::max( myMaxSpeed, it->allUp ); 172 myMaxSpeed = std::max( myMaxSpeed, it->allDown ); 173 } 174 } 175 } 176 177 void 178 SpeedStats :: Pulse( tr_handle_t * handle ) 179 { 180 // add a new record 181 float allUp, allDown; 182 tr_torrentRates( handle, &allDown, &allUp ); 183 Speed s; 184 s.time = time( NULL ); 185 s.allUp = s.time % 30;//allUp; 186 s.allDown = s.time % 15;//allDown; 187 //if( myTorrent ) { 188 //const tr_stat_t * stat = tr_torrentStat( myTorrent ); 189 s.torrentUp = s.time % 40;//stat->rateUpload; 190 s.torrentDown = s.time % 25;//stat->rateDownload; 191 //} 192 myStats.push_back( s ); 193 194 // age off old data 195 const int eraseCount = myStats.size() - myHistory; 196 if( eraseCount > 0 ) 197 myStats.erase( myStats.begin(), 198 myStats.begin() + eraseCount ); 199 200 // update max 201 myMaxSpeed = std::max( myMaxSpeed, s.allUp ); 202 myMaxSpeed = std::max( myMaxSpeed, s.allDown ); 203 myMaxSpeed = std::max( myMaxSpeed, s.torrentUp ); 204 myMaxSpeed = std::max( myMaxSpeed, s.torrentDown ); 205 206 Refresh( false ); 207 } -
trunk/wx/speed-stats.h
r2849 r2907 22 22 #define __XMISSION_SPEED_STATS_H__ 23 23 24 #include <ctime> 25 #include <vector> 26 #include <wx/colour.h> 24 27 #include <wx/panel.h> 25 28 #include <libtransmission/transmission.h> 29 30 extern "C" 31 { 32 struct tr_torrent_s; 33 } 26 34 27 35 class SpeedStats: public wxPanel … … 30 38 31 39 SpeedStats( wxWindow * parent, 32 33 34 35 36 40 wxWindowID id = wxID_ANY, 41 const wxPoint& pos = wxDefaultPosition, 42 const wxSize& size = wxDefaultSize, 43 long style = wxTAB_TRAVERSAL, 44 const wxString& name = _T("panel")); 37 45 38 virtual ~SpeedStats() {} 39 40 void Update( tr_handle_t * handle ); 46 virtual ~SpeedStats(); 41 47 42 48 public: 43 49 44 void OnPaint( wxPaintEvent& ); 50 void SetTorrent( struct tr_torrent_s * ); 51 52 void Pulse( tr_handle_t * handle ); 45 53 46 54 private: 47 55 48 DECLARE_EVENT_TABLE() 56 virtual void OnSize( wxSizeEvent& event ); 57 58 void OnPaint( wxPaintEvent& ); 59 60 DECLARE_EVENT_TABLE() 61 62 private: 63 64 wxBitmap * myBitmap; 65 66 struct tr_torrent_s * myTorrent; 67 68 struct Speed 69 { 70 time_t time; 71 double torrentUp; 72 double torrentDown; 73 double allUp; 74 double allDown; 75 Speed(): time(0), 76 torrentUp(0), torrentDown(0), 77 allUp(0), allDown(0) {} 78 }; 79 80 typedef std::vector<Speed> stats_t; 81 82 stats_t myStats; 83 84 double myMaxSpeed; 85 86 int myHistory; 87 88 enum { 89 BACKGROUND, 90 FRAME, 91 TORRENT_UP, 92 TORRENT_DOWN, 93 ALL_UP, 94 ALL_DOWN, 95 N_COLORS 96 }; 97 98 wxColour myColors[N_COLORS]; 49 99 }; 50 100 -
trunk/wx/xmission.cc
r2849 r2907 381 381 bool MyApp::OnInit() 382 382 { 383 handle = tr_init( " gui" );383 handle = tr_init( "wx" ); 384 384 385 385 wxCmdLineParser cmdParser( cmdLineDesc, argc, argv ); … … 438 438 assert( list == myTorrentList ); 439 439 mySelectedTorrents.assign( torrents.begin(), torrents.end() ); 440 441 tr_torrent_t * tor = mySelectedTorrents.empty() ? NULL : mySelectedTorrents.front(); 442 mySpeedStats->SetTorrent( tor ); 440 443 } 441 444 … … 455 458 ApplyCurrentFilter( ); 456 459 457 mySpeedStats-> Update( handle );460 mySpeedStats->Pulse( handle ); 458 461 459 462 float down, up; … … 490 493 myLogoIcon( transmission_xpm ), 491 494 myTrayIconIcon( systray_xpm ), 495 mySpeedStats( 0 ), 492 496 myFilterFlags( ~0 ), 493 497 myExitTime( 0 )
Note: See TracChangeset
for help on using the changeset viewer.