2008-11-15 09:29:34 -05:00
# include <math.h>
2008-11-15 17:23:16 -05:00
# include <QTimer>
2008-11-15 09:29:34 -05:00
# include <QWheelEvent>
# include "rsiface/rsiface.h"
# include "rsiface/rspeers.h"
# include <QTableView>
# include "TrustView.h"
using namespace std ;
TrustView : : TrustView ( )
{
setupUi ( this ) ;
trustTableTW - > setMouseTracking ( true ) ;
trustInconsistencyCB - > setEnabled ( false ) ;
zoomHS - > setValue ( 100 ) ;
QObject : : connect ( zoomHS , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( updateZoom ( int ) ) ) ;
QObject : : connect ( updatePB , SIGNAL ( clicked ( ) ) , this , SLOT ( update ( ) ) ) ;
QObject : : connect ( trustTableTW , SIGNAL ( cellClicked ( int , int ) ) , this , SLOT ( selectCell ( int , int ) ) ) ;
2008-11-15 17:23:16 -05:00
updatePB - > setToolTip ( QString ( " This table normaly auto-updates every 10 seconds. " ) ) ;
QTimer * timer = new QTimer ;
QObject : : connect ( timer , SIGNAL ( timeout ( ) ) , this , SLOT ( update ( ) ) ) ;
timer - > start ( 10000 ) ;
2008-11-15 09:29:34 -05:00
update ( ) ;
}
void TrustView : : wheelEvent ( QWheelEvent * e )
{
if ( e - > modifiers ( ) & Qt : : ShiftModifier )
{
if ( e - > delta ( ) > 0 )
zoomHS - > setValue ( zoomHS - > value ( ) - 5 ) ;
else
zoomHS - > setValue ( zoomHS - > value ( ) + 5 ) ;
}
}
void TrustView : : selectCell ( int row , int col )
{
static int last_row = - 1 ;
static int last_col = - 1 ;
if ( last_row > - 1 )
{
int col_s , row_s ;
getCellSize ( zoomHS - > value ( ) , col_s , row_s ) ;
trustTableTW - > setColumnWidth ( last_col , col_s ) ;
trustTableTW - > setRowHeight ( last_row , row_s ) ;
}
if ( row ! = last_row | | col ! = last_col )
{
trustTableTW - > setColumnWidth ( col , _base_cell_width ) ;
trustTableTW - > setRowHeight ( row , _base_cell_height ) ;
}
last_col = col ;
last_row = row ;
}
void TrustView : : getCellSize ( int z , int & col_s , int & row_s ) const
{
col_s = max ( 10 , ( int ) rint ( z / 100.0 * _base_cell_width ) ) ;
row_s = max ( 10 , ( int ) rint ( z / 100.0 * _base_cell_height ) ) ;
}
void TrustView : : updateZoom ( int z )
{
int col_s , row_s ;
getCellSize ( z , col_s , row_s ) ;
for ( int i = 0 ; i < trustTableTW - > columnCount ( ) ; + + i )
trustTableTW - > setColumnWidth ( i , col_s ) ;
for ( int i = 0 ; i < trustTableTW - > rowCount ( ) ; + + i )
trustTableTW - > setRowHeight ( i , row_s ) ;
2008-11-15 17:23:16 -05:00
// cout << "updated zoom" << endl;
2008-11-15 09:29:34 -05:00
}
int TrustView : : getRowColId ( const string & name )
{
static map < string , int > nameToRow ;
map < string , int > : : const_iterator itpr ( nameToRow . find ( name ) ) ;
int i ;
if ( itpr = = nameToRow . end ( ) )
{
i = trustTableTW - > columnCount ( ) ;
2008-11-15 17:23:16 -05:00
// cout << " -> peer not in table. Creating entry # " << i << endl ;
2008-11-15 09:29:34 -05:00
trustTableTW - > insertColumn ( i ) ;
trustTableTW - > insertRow ( i ) ;
nameToRow [ name ] = i ;
trustTableTW - > setHorizontalHeaderItem ( i , new QTableWidgetItem ( QString ( name . c_str ( ) ) ) ) ;
trustTableTW - > setVerticalHeaderItem ( i , new QTableWidgetItem ( QString ( name . c_str ( ) ) ) ) ;
trustTableTW - > setColumnWidth ( i , _base_cell_width ) ;
trustTableTW - > setRowHeight ( i , _base_cell_height ) ;
}
else
i = ( * itpr ) . second ;
return i ;
}
void TrustView : : update ( )
{
// collect info.
std : : list < std : : string > neighs ;
if ( ! rsPeers - > getOthersList ( neighs ) )
return ;
neighs . push_back ( rsPeers - > getOwnId ( ) ) ;
trustTableTW - > setSortingEnabled ( false ) ;
RsPeerDetails details ;
// Fill everything
for ( list < string > : : const_iterator it1 ( neighs . begin ( ) ) ; it1 ! = neighs . end ( ) ; + + it1 )
{
if ( ! rsPeers - > getPeerDetails ( * it1 , details ) )
continue ;
2008-11-15 17:23:16 -05:00
// cout << "treating neigh = " << details.name << endl ;
// cout << " signers = " ;
2008-11-15 09:29:34 -05:00
int i = getRowColId ( details . name ) ;
for ( list < string > : : const_iterator it2 ( details . signers . begin ( ) ) ; it2 ! = details . signers . end ( ) ; + + it2 )
{
2008-11-15 17:23:16 -05:00
// cout << *it2 << " " ;
2008-11-15 09:29:34 -05:00
// Signers are identified by there name, so if we have twice the same signers, this gets crappy.
int j = getRowColId ( * it2 ) ;
QString trr ( ( i = = j ) ? " Self " : " Trust " ) ;
if ( trustTableTW - > item ( i , j ) = = NULL )
trustTableTW - > setItem ( i , j , new QTableWidgetItem ( trr ) ) ;
else
trustTableTW - > item ( i , j ) - > setText ( trr ) ;
}
2008-11-15 17:23:16 -05:00
// cout << endl ;
2008-11-15 09:29:34 -05:00
}
// assign colors
2008-11-16 15:17:36 -05:00
vector < int > ni ( trustTableTW - > rowCount ( ) , 0 ) ;
vector < int > nj ( trustTableTW - > columnCount ( ) , 0 ) ;
2008-11-15 09:29:34 -05:00
for ( int i = 0 ; i < trustTableTW - > rowCount ( ) ; + + i )
for ( int j = 0 ; j < trustTableTW - > columnCount ( ) ; + + j )
{
QTableWidgetItem * i_ij ( trustTableTW - > item ( i , j ) ) ;
QTableWidgetItem * i_ji ( trustTableTW - > item ( j , i ) ) ;
QColor color ;
// check bidirectional trust
//
if ( i_ij ! = NULL )
{
2008-11-16 15:17:36 -05:00
+ + ni [ i ] ;
+ + nj [ j ] ;
2008-11-15 09:29:34 -05:00
if ( i_ji = = NULL )
{
i_ij - > setBackgroundColor ( Qt : : yellow ) ;
i_ij - > setToolTip ( trustTableTW - > horizontalHeaderItem ( i ) - > text ( ) + QString ( " is trusted (one way) by " ) + trustTableTW - > verticalHeaderItem ( j ) - > text ( ) ) ;
2008-11-16 15:17:36 -05:00
i_ij - > setText ( QString ( " Half " ) ) ;
2008-11-15 09:29:34 -05:00
}
else
{
if ( i = = j )
{
i_ij - > setBackgroundColor ( Qt : : red ) ;
i_ij - > setToolTip ( trustTableTW - > horizontalHeaderItem ( i ) - > text ( ) + QString ( " trusts himself " ) ) ;
}
else
{
i_ij - > setBackgroundColor ( Qt : : green ) ;
i_ij - > setToolTip ( trustTableTW - > horizontalHeaderItem ( i ) - > text ( ) + " and " + trustTableTW - > verticalHeaderItem ( j ) - > text ( ) + QString ( " trust each others " ) ) ;
2008-11-16 15:17:36 -05:00
i_ij - > setText ( QString ( " Full " ) ) ;
2008-11-15 09:29:34 -05:00
}
}
}
}
2008-11-16 15:17:36 -05:00
for ( int i = 0 ; i < trustTableTW - > rowCount ( ) ; + + i )
trustTableTW - > verticalHeaderItem ( i ) - > setToolTip ( trustTableTW - > verticalHeaderItem ( i ) - > text ( ) + " is trusted by " + QString : : number ( ni [ i ] ) + " peers, including him(her)self. " ) ;
for ( int j = 0 ; j < trustTableTW - > columnCount ( ) ; + + j )
trustTableTW - > horizontalHeaderItem ( j ) - > setToolTip ( trustTableTW - > horizontalHeaderItem ( j ) - > text ( ) + " trusts " + QString : : number ( nj [ j ] ) + " peers, including him(her)self. " ) ;
2008-11-15 09:29:34 -05:00
}