Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

GraphLine.cpp

Go to the documentation of this file.
00001 //
00002 // GraphLine.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "GraphLine.h"
00026 #include "GraphItem.h"
00027 
00028 #ifdef _DEBUG
00029 #undef THIS_FILE
00030 static char THIS_FILE[]=__FILE__;
00031 #define new DEBUG_NEW
00032 #endif
00033 
00034 #define MIN_GRID_SIZE_HORZ      16
00035 #define TOP_MARGIN                      10
00036 
00037 
00039 // CLineGraph construction
00040 
00041 CLineGraph::CLineGraph()
00042 {
00043         m_bShowAxis             = TRUE;
00044         m_bShowGrid             = TRUE;
00045         m_bShowLegend   = TRUE;
00046         m_crBack                = 0;
00047         m_crGrid                = RGB( 0, 0, 128 );
00048         m_nMinGridVert  = 32;
00049 
00050         m_nSpeed                = 100;
00051         m_nScale                = 2;
00052         m_nMaximum              = 0;
00053         m_nUpdates              = 0;
00054         m_tLastScale    = 0;
00055 }
00056 
00057 CLineGraph::~CLineGraph()
00058 {
00059         ClearItems();
00060 }
00061 
00063 // CLineGraph list access
00064 
00065 void CLineGraph::AddItem(CGraphItem* pItem)
00066 {
00067         m_pItems.AddTail( pItem );
00068 }
00069 
00070 POSITION CLineGraph::GetItemIterator() const
00071 {
00072         return m_pItems.GetHeadPosition();
00073 }
00074 
00075 CGraphItem* CLineGraph::GetNextItem(POSITION& pos) const
00076 {
00077         return (CGraphItem*)m_pItems.GetNext( pos );
00078 }
00079 
00080 int CLineGraph::GetItemCount() const
00081 {
00082         return m_pItems.GetCount();
00083 }
00084 
00085 void CLineGraph::RemoveItem(CGraphItem* pItem)
00086 {
00087         POSITION pos = m_pItems.Find( pItem );
00088         if ( pos != NULL ) m_pItems.RemoveAt( pos );
00089         delete pItem;
00090 }
00091 
00092 void CLineGraph::ClearItems()
00093 {
00094         for ( POSITION pos = GetItemIterator() ; pos ; )
00095         {
00096                 delete GetNextItem( pos );
00097         }
00098         m_pItems.RemoveAll();
00099 }
00100 
00102 // CLineGraph create default
00103 
00104 void CLineGraph::CreateDefaults()
00105 {
00106         AddItem( new CGraphItem( GRC_TOTAL_BANDWIDTH_IN, 0, RGB( 0, 255, 0 ) ) );
00107         AddItem( new CGraphItem( GRC_TOTAL_BANDWIDTH_OUT, 0, RGB( 255, 0, 0 ) ) );
00108 }
00109 
00111 // CLineGraph update
00112 
00113 BOOL CLineGraph::Update()
00114 {
00115         DWORD tNow = GetTickCount();
00116 
00117         if ( tNow - m_tLastScale > 10000 )
00118         {
00119                 m_tLastScale = tNow;
00120                 ResetMaximum();
00121         }
00122 
00123         for ( POSITION pos = GetItemIterator() ; pos ; )
00124         {
00125                 CGraphItem* pItem = GetNextItem( pos );
00126                 DWORD nValue = pItem->Update();
00127                 m_nMaximum = max( m_nMaximum, nValue );
00128         }
00129 
00130         m_nUpdates++;
00131         return m_pItems.GetCount();
00132 }
00133 
00135 // CLineGraph clear
00136 
00137 void CLineGraph::Clear()
00138 {
00139         for ( POSITION pos = GetItemIterator() ; pos ; )
00140         {
00141                 GetNextItem( pos )->Clear();
00142         }
00143 
00144         m_nMaximum = 0;
00145 }
00146 
00148 // CLineGraph serialize
00149 
00150 void CLineGraph::Serialize(CArchive& ar)
00151 {
00152         if ( ar.IsStoring() )
00153         {
00154                 ar << m_bShowAxis;
00155                 ar << m_bShowGrid;
00156                 ar << m_bShowLegend;
00157                 ar << m_nSpeed;
00158                 ar << m_nScale;
00159 
00160                 ar.WriteCount( GetItemCount() );
00161 
00162                 for ( POSITION pos = GetItemIterator() ; pos ; )
00163                 {
00164                         GetNextItem( pos )->Serialize( ar );
00165                 }
00166         }
00167         else
00168         {
00169                 ar >> m_bShowAxis;
00170                 ar >> m_bShowGrid;
00171                 ar >> m_bShowLegend;
00172                 ar >> m_nSpeed;
00173                 ar >> m_nScale;
00174 
00175                 for ( int nCount = ar.ReadCount() ; nCount > 0 ; nCount-- )
00176                 {
00177                         CGraphItem* pItem = new CGraphItem();
00178                         pItem->Serialize( ar );
00179                         m_pItems.AddTail( pItem );
00180                 }
00181         }
00182 }
00183 
00185 // CLineGraph maximum rescale
00186 
00187 void CLineGraph::ResetMaximum(BOOL bForce)
00188 {
00189         DWORD nMaximum = 0;
00190 
00191         for ( POSITION pos = GetItemIterator() ; pos ; )
00192         {
00193                 CGraphItem* pItem = GetNextItem( pos );
00194                 DWORD nValue = pItem->GetMaximum();
00195                 nMaximum = max( nMaximum, nValue );
00196         }
00197 
00198         if ( nMaximum || bForce ) m_nMaximum = nMaximum;
00199 }
00200 
00202 // CLineGraph paint
00203 
00204 void CLineGraph::Paint(CDC* pDC, CRect* pRect)
00205 {
00206         if ( m_pGridPen.m_hObject == NULL ) m_pGridPen.CreatePen( PS_SOLID, 1, m_crGrid );
00207 
00208         DWORD nWidth = (DWORD)pRect->Width() / m_nScale + 2;
00209 
00210         if ( pRect->Width() > 64 )
00211         {
00212                 for ( POSITION pos = GetItemIterator() ; pos ; )
00213                 {
00214                         CGraphItem* pItem = GetNextItem( pos );
00215                         pItem->SetHistory( nWidth );
00216                 }
00217         }
00218 
00219         pDC->FillSolidRect( pRect, m_crBack );
00220 
00221         if ( m_pItems.IsEmpty() || m_nMaximum == 0 ) return;
00222 
00223         CFont* pOldFont = (CFont*)pDC->SelectObject( &theApp.m_gdiFont );
00224         pDC->SetBkMode( TRANSPARENT );
00225 
00226         if ( m_bShowGrid ) PaintGrid( pDC, pRect );
00227 
00228         for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
00229         {
00230                 CGraphItem* pItem = (CGraphItem*)m_pItems.GetNext( pos );
00231 
00232                 DWORD nPoints   = min( pItem->m_nLength, nWidth );
00233                 POINT* pPoints  = new POINT[ nPoints ];
00234 
00235                 for ( DWORD nPos = 0 ; nPos < nPoints ; nPos++ )
00236                 {
00237                         DWORD nValue = pItem->GetValueAt( nPos );
00238 
00239                         nValue = pRect->bottom - nValue * ( pRect->Height() - TOP_MARGIN ) / m_nMaximum;
00240 
00241                         pPoints[ nPos ].x = pRect->right - nPos * m_nScale - 1;
00242                         pPoints[ nPos ].y = nValue + 4;
00243                 }
00244 
00245                 pItem->MakeGradient( m_crBack );
00246 
00247                 CPen* pOldPen = (CPen*)pDC->SelectObject( &pItem->m_pPen[3] );
00248 
00249                 for ( int nLayer = 4 ; nLayer ; nLayer-- )
00250                 {
00251                         pDC->Polyline( pPoints, nPoints );
00252 
00253                         if ( nLayer > 1 )
00254                         {
00255                                 for ( DWORD nPos = 0 ; nPos < nPoints ; nPos++ ) pPoints[ nPos ].y --;
00256                                 pDC->SelectObject( &pItem->m_pPen[ nLayer - 2 ] );
00257                         }
00258                 }
00259 
00260                 pDC->SelectObject( pOldPen );
00261 
00262                 delete [] pPoints;
00263         }
00264 
00265         if ( m_bShowLegend ) PaintLegend( pDC, pRect );
00266 
00267         pDC->SelectObject( pOldFont );
00268 }
00269 
00270 void CLineGraph::PaintGrid(CDC* pDC, CRect* pRect)
00271 {
00272         CPen* pOldPen = (CPen*)pDC->SelectObject( &m_pGridPen );
00273 
00274         if ( pRect->Height() <= TOP_MARGIN ) return;
00275 
00276         DWORD nScale = max( m_nScale, DWORD(MIN_GRID_SIZE_HORZ) );
00277         DWORD nCount = pRect->Width() / nScale + 1;
00278         DWORD nTimeB = nScale / m_nScale;
00279 
00280         int nX = pRect->right + m_nScale - ( m_nUpdates % nTimeB ) * m_nScale;
00281 
00282         for ( DWORD nPos = 0 ; nPos < nCount ; nPos++, nX -= nScale )
00283         {
00284                 pDC->MoveTo( nX, pRect->top );
00285                 pDC->LineTo( nX, pRect->bottom );
00286         }
00287 
00288         BOOL bVolume = FALSE;
00289 
00290         if ( m_bShowAxis )
00291         {
00292                 for ( POSITION pos = GetItemIterator() ; pos && ! bVolume ; )
00293                 {
00294                         CGraphItem* pItem       = GetNextItem( pos );
00295                         GRAPHITEM* pDesc        = pItem->GetItemDesc( pItem->m_nCode );
00296                         if ( pDesc && pDesc->m_nUnits == 1 ) bVolume = TRUE;
00297                 }
00298                 pDC->SetTextColor( RGB( 193, 196, 255 ) );
00299         }
00300 
00301         nScale = m_nMinGridVert * m_nMaximum / ( pRect->Height() - TOP_MARGIN );
00302         if ( ! nScale  ) nScale = 1;
00303 
00304         int nOldY = pRect->bottom;
00305 
00306         for ( DWORD nPos = 1 ; ; nPos++ )
00307         {
00308                 int nY = pRect->bottom - nScale * nPos * ( pRect->Height() - TOP_MARGIN ) / m_nMaximum;
00309                 if ( nY < 0 || nY >= nOldY - 4 ) break;
00310                 nOldY = nY;
00311 
00312                 pDC->MoveTo( pRect->left, nY );
00313                 pDC->LineTo( pRect->right, nY );
00314 
00315                 if ( m_bShowAxis )
00316                 {
00317                         CString strValue;
00318                         if ( bVolume )
00319                                 strValue = Settings.SmartVolume( nScale * nPos, FALSE, TRUE );
00320                         else
00321                                 strValue.Format( _T("%lu"), nScale * nPos );
00322                         pDC->ExtTextOut( pRect->left + 4, nY + 1, 0, NULL, strValue, NULL );
00323                 }
00324         }
00325 
00326         pDC->SelectObject( pOldPen );
00327 }
00328 
00329 void CLineGraph::PaintLegend(CDC* pDC, CRect* pRect)
00330 {
00331         int nHeight     = pDC->GetTextExtent( _T("Cy") ).cy;
00332         int nLeft       = pRect->left + ( pRect->Width() > 128 ) ? 64 : 0;
00333         int nTop        = pRect->top + 1;
00334 
00335         for ( POSITION pos = GetItemIterator() ; pos ; nTop += nHeight )
00336         {
00337                 CGraphItem* pItem = GetNextItem( pos );
00338                 pDC->SetTextColor( pItem->m_nColour );
00339                 pDC->ExtTextOut( nLeft, nTop, 0, NULL, _T("• ") + pItem->m_sName, NULL );
00340         }
00341 }

Generated on Thu Dec 15 10:39:43 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2