implement the `speed' tab.

This commit is contained in:
Charles Kerr 2007-08-22 19:49:24 +00:00
parent 62bca7d2f2
commit 08354cb792
3 changed files with 226 additions and 28 deletions

View File

@ -18,15 +18,22 @@
* $Id$
*/
#include <iostream>
#include <wx/dcclient.h>
#include <wx/dcmemory.h>
#include <wx/event.h>
#include <wx/gbsizer.h>
#include <wx/stattext.h>
#include <wx/intl.h>
#include <libtransmission/transmission.h>
#include "foreach.h"
#include "speed-stats.h"
#define SNAPSHOT_PIXEL_WIDTH 8
BEGIN_EVENT_TABLE( SpeedStats, wxPanel )
EVT_SIZE( SpeedStats::OnSize )
EVT_PAINT( SpeedStats::OnPaint )
END_EVENT_TABLE()
//EVT_PAINT( SpeedStats::OnPaint )
SpeedStats :: SpeedStats( wxWindow * parent,
wxWindowID id,
@ -34,30 +41,167 @@ SpeedStats :: SpeedStats( wxWindow * parent,
const wxSize & size,
long style,
const wxString & name ):
wxPanel( parent, id, pos, size, style, name )
wxPanel( parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name ),
myBitmap( 0 ),
myTorrent( 0 ),
myMaxSpeed( -1 ),
myHistory( 0 )
{
myColors[BACKGROUND] = wxColour( 0, 0, 0 ); // black
myColors[FRAME] = wxColour( 34, 139, 34 ); // forest green
myColors[ALL_UP] = wxColour( 255, 0, 0 );
myColors[TORRENT_UP] = wxColour( 255, 255, 0 );
myColors[ALL_DOWN] = wxColour( 255, 0, 255 );
myColors[TORRENT_DOWN] = wxColour( 0, 255, 128 );
}
SpeedStats :: ~SpeedStats()
{
delete myBitmap;
}
void
SpeedStats :: Update( tr_handle_t * WXUNUSED(handle) )
SpeedStats :: OnSize( wxSizeEvent& event )
{
delete myBitmap;
const wxSize size = event.GetSize();
myBitmap = new wxBitmap( size.GetWidth(), size.GetHeight() );
myHistory = size.GetWidth() / SNAPSHOT_PIXEL_WIDTH;
}
void
SpeedStats :: OnPaint( wxPaintEvent& WXUNUSED(event) )
{
#if 0
int w, h;
mySpeedPanel->GetSize( &w, &h );
wxMemoryDC dc;
wxBitmap bitmap( w, h );
dc.SelectObject( bitmap );
const int draw_width = myBitmap->GetWidth();
const int draw_height = myBitmap->GetHeight();
wxColour backgroundColor = *wxBLACK;
dc.SetBrush( wxBrush( backgroundColor ) );
dc.SetPen( wxBrush( backgroundColor ) );
dc.DrawRectangle( 0, 0, w, h );
const int top = (((int)myMaxSpeed + 11) / 10) * 10;
const double y_scale = (double)draw_height / top;
const int num_bars = 4;
const int per_bar = top / num_bars;
std::cerr << "paint" << std::endl;
#endif
// clear
wxMemoryDC memDC( *myBitmap );
memDC.SetBackground( myColors[BACKGROUND] );
memDC.Clear( );
// draw the frame
memDC.SetPen( wxPen ( myColors[FRAME] ) );
memDC.SetTextForeground( myColors[FRAME] );
const int fontsize = 10;
const int dely = int( draw_height / num_bars );
wxString xstr;
memDC.SetFont( wxFont ( fontsize, wxFONTFAMILY_SWISS,
wxFONTSTYLE_NORMAL,
wxFONTWEIGHT_NORMAL ) );
for( int i=0; i<=num_bars; ++i )
{
const int y = (int)(draw_height - (i*dely+0.5));
// line
memDC.DrawLine( wxCoord(0), wxCoord(draw_height - (i*dely+0.5)),
wxCoord(draw_width), wxCoord(draw_height - (i*dely+0.5)) );
xstr.Printf( _("%d KiB/s"), (per_bar*i) );
memDC.DrawText( xstr, wxCoord(0), wxCoord(y+2) );
}
const int n = myStats.size( );
if( n )
{
wxPoint * points = new wxPoint[ n ];
int x = draw_width - (n * SNAPSHOT_PIXEL_WIDTH);
for( int i=0; i<n; ++i ) {
points[i].x = x;
x += SNAPSHOT_PIXEL_WIDTH;
}
// torrent upload
for( int i=0; i<n; ++i )
points[i].y = draw_height - 10 - int(myStats[i].torrentUp * y_scale);
memDC.SetPen( wxPen ( myColors[TORRENT_UP] ) );
memDC.DrawLines( n, points, 0, 0 );
// torrent download
for( int i=0; i<n; ++i )
points[i].y = draw_height - int(myStats[i].torrentDown * y_scale);
memDC.SetPen( wxPen ( myColors[TORRENT_DOWN] ) );
memDC.DrawLines( n, points, 0, 0 );
// all upload
for( int i=0; i<n; ++i )
points[i].y = draw_height - int(myStats[i].torrentUp * y_scale);
memDC.SetPen( wxPen ( myColors[ALL_UP] ) );
memDC.DrawLines( n, points, 0, 0 );
// all download
for( int i=0; i<n; ++i )
points[i].y = draw_height - int(myStats[i].torrentDown * y_scale);
memDC.SetPen( wxPen ( myColors[ALL_DOWN] ) );
memDC.DrawLines( n, points, 0, 0 );
delete [] points;
}
wxPaintDC dc( this );
dc.Blit( 0, 0, draw_width, draw_height, &memDC, 0, 0 );
memDC.SelectObject( wxNullBitmap );
}
void
SpeedStats :: SetTorrent( tr_torrent_t * tor )
{
if( tor != myTorrent )
{
myTorrent = tor;
myMaxSpeed = 0;
foreach( stats_t, myStats, it )
{
it->torrentUp = 0;
it->torrentDown = 0;
myMaxSpeed = std::max( myMaxSpeed, it->allUp );
myMaxSpeed = std::max( myMaxSpeed, it->allDown );
}
}
}
void
SpeedStats :: Pulse( tr_handle_t * handle )
{
// add a new record
float allUp, allDown;
tr_torrentRates( handle, &allDown, &allUp );
Speed s;
s.time = time( NULL );
s.allUp = s.time % 30;//allUp;
s.allDown = s.time % 15;//allDown;
//if( myTorrent ) {
//const tr_stat_t * stat = tr_torrentStat( myTorrent );
s.torrentUp = s.time % 40;//stat->rateUpload;
s.torrentDown = s.time % 25;//stat->rateDownload;
//}
myStats.push_back( s );
// age off old data
const int eraseCount = myStats.size() - myHistory;
if( eraseCount > 0 )
myStats.erase( myStats.begin(),
myStats.begin() + eraseCount );
// update max
myMaxSpeed = std::max( myMaxSpeed, s.allUp );
myMaxSpeed = std::max( myMaxSpeed, s.allDown );
myMaxSpeed = std::max( myMaxSpeed, s.torrentUp );
myMaxSpeed = std::max( myMaxSpeed, s.torrentDown );
Refresh( false );
}

View File

@ -21,31 +21,81 @@
#ifndef __XMISSION_SPEED_STATS_H__
#define __XMISSION_SPEED_STATS_H__
#include <ctime>
#include <vector>
#include <wx/colour.h>
#include <wx/panel.h>
#include <libtransmission/transmission.h>
extern "C"
{
struct tr_torrent_s;
}
class SpeedStats: public wxPanel
{
public:
SpeedStats( wxWindow * parent,
wxWindowID id = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = wxTAB_TRAVERSAL,
const wxString& name = _T("panel"));
wxWindowID id = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = wxTAB_TRAVERSAL,
const wxString& name = _T("panel"));
virtual ~SpeedStats() {}
void Update( tr_handle_t * handle );
virtual ~SpeedStats();
public:
void OnPaint( wxPaintEvent& );
void SetTorrent( struct tr_torrent_s * );
void Pulse( tr_handle_t * handle );
private:
DECLARE_EVENT_TABLE()
virtual void OnSize( wxSizeEvent& event );
void OnPaint( wxPaintEvent& );
DECLARE_EVENT_TABLE()
private:
wxBitmap * myBitmap;
struct tr_torrent_s * myTorrent;
struct Speed
{
time_t time;
double torrentUp;
double torrentDown;
double allUp;
double allDown;
Speed(): time(0),
torrentUp(0), torrentDown(0),
allUp(0), allDown(0) {}
};
typedef std::vector<Speed> stats_t;
stats_t myStats;
double myMaxSpeed;
int myHistory;
enum {
BACKGROUND,
FRAME,
TORRENT_UP,
TORRENT_DOWN,
ALL_UP,
ALL_DOWN,
N_COLORS
};
wxColour myColors[N_COLORS];
};
#endif

View File

@ -380,7 +380,7 @@ void MyFrame :: OnOpen( wxCommandEvent& WXUNUSED(event) )
bool MyApp::OnInit()
{
handle = tr_init( "gui" );
handle = tr_init( "wx" );
wxCmdLineParser cmdParser( cmdLineDesc, argc, argv );
if( cmdParser.Parse ( ) )
@ -437,6 +437,9 @@ MyFrame :: OnTorrentListSelectionChanged( TorrentListCtrl* list,
{
assert( list == myTorrentList );
mySelectedTorrents.assign( torrents.begin(), torrents.end() );
tr_torrent_t * tor = mySelectedTorrents.empty() ? NULL : mySelectedTorrents.front();
mySpeedStats->SetTorrent( tor );
}
void
@ -454,7 +457,7 @@ MyFrame :: OnPulse(wxTimerEvent& WXUNUSED(event) )
RefreshFilterCounts( );
ApplyCurrentFilter( );
mySpeedStats->Update( handle );
mySpeedStats->Pulse( handle );
float down, up;
tr_torrentRates( handle, &down, &up );
@ -489,6 +492,7 @@ MyFrame :: MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size
myPulseTimer( this, ID_Pulse ),
myLogoIcon( transmission_xpm ),
myTrayIconIcon( systray_xpm ),
mySpeedStats( 0 ),
myFilterFlags( ~0 ),
myExitTime( 0 )
{