<s> import sys <EOL> from PySide import QtGui , QtCore <EOL> FIRST_COLUMN = <NUM_LIT:0> <EOL> class UserPresence : <EOL> OFFLINE = <NUM_LIT:0> <EOL> ONLINE = <NUM_LIT> <EOL> class Object : <EOL> GROUP = <NUM_LIT:1> <EOL> USER = <NUM_LIT:2> <EOL> class User ( ) : <EOL> def __init__ ( self , uri , nickname , group = None ) : <EOL> self . _type = Object . USER <EOL> self . uri = uri <EOL> self . nickname = nickname <EOL> self . group = group <EOL> def get_type ( self ) : <EOL> return self . _type <EOL> def get_display_name ( self ) : <EOL> return self . nickname <EOL> class Group ( ) : <EOL> def __init__ ( self , gid , gname , users = None ) : <EOL> self . _type = Object . GROUP <EOL> self . gid = gid <EOL> self . gname = gname <EOL> self . user_list = [ ] <EOL> if users : <EOL> for user in users : <EOL> self . add_user ( user ) <EOL> def add_user ( self , user ) : <EOL> if user not in self . user_list : <EOL> self . user_list . append ( user ) <EOL> def count ( self ) : <EOL> return len ( self . user_list ) <EOL> def get_user_by_row ( self , row ) : <EOL> return self . user_list [ row ] <EOL> def get_user_by_uri ( self , uri ) : <EOL> for user in self . user_list : <EOL> if user . uri == uri : <EOL> return user <EOL> def get_display_name ( self ) : <EOL> return self . gname <EOL> def get_type ( self ) : <EOL> return self . _type <EOL> class GroupAgent : <EOL> def __init__ ( self , groups = None ) : <EOL> self . group_list = [ ] <EOL> if groups : <EOL> for group in groups : <EOL> self . add_group ( group ) <EOL> def add_group ( self , group ) : <EOL> self . group_list . append ( group ) <EOL> def count ( self ) : <EOL> return len ( self . group_list ) <EOL> def get_group_by_row ( self , row ) : <EOL> return self . group_list [ row ] <EOL> def index ( self , group ) : <EOL> return self . group_list . index ( group ) <EOL> def get_user_by_uri ( self , uri ) : <EOL> for group in self . group_list : <EOL> user = group . get_user_by_uri ( uri ) <EOL> if user : <EOL> return user <EOL> class Model ( QtCore . QAbstractItemModel ) : <EOL> COLUMN_COUNT = <NUM_LIT:1> <EOL> def __init__ ( self , group_agent ) : <EOL> QtCore . QAbstractItemModel . __init__ ( self ) <EOL> self . group_agent = group_agent <EOL> def columnCount ( self , parent_idx ) : <EOL> if not parent_idx . isValid ( ) : <EOL> return self . COLUMN_COUNT <EOL> parent_obj = parent_idx . internalPointer ( ) <EOL> return parent_obj . count ( ) <EOL> def rowCount ( self , parent_idx ) : <EOL> if not parent_idx . isValid ( ) : <EOL> return self . group_agent . count ( ) <EOL> parent_obj = parent_idx . internalPointer ( ) <EOL> if parent_obj . get_type ( ) == Object . GROUP : <EOL> return parent_obj . count ( ) <EOL> return <NUM_LIT:0> <EOL> def index ( self , row , column , parent_idx ) : <EOL> assert column != None <EOL> if not parent_idx . isValid ( ) : <EOL> group = self . group_agent . get_group_by_row ( row ) <EOL> return self . createIndex ( row , column , group ) <EOL> parent_obj = parent_idx . internalPointer ( ) <EOL> if parent_obj . get_type ( ) == Object . GROUP : <EOL> item = parent_obj . get_user_by_row ( row ) <EOL> return self . createIndex ( row , column , item ) <EOL> return QtCore . QModelIndex ( ) <EOL> def data ( self , index , role = QtCore . Qt . DisplayRole ) : <EOL> if not index . isValid ( ) : <EOL> return QtCore . QVariant ( ) <EOL> obj = index . internalPointer ( ) <EOL> obj_type = obj . get_type ( ) <EOL> if role == QtCore . Qt . DisplayRole : <EOL> if obj . get_type ( ) in ( Object . GROUP , Object . USER ) : <EOL> return QtCore . QVariant ( obj . get_display_name ( ) ) <EOL> elif role == QtCore . Qt . UserRole : <EOL> if obj_type == Object . GROUP : <EOL> return QtCore . QVariant ( obj . gid ) <EOL> elif obj_type == Object . USER : <EOL> return QtCore . QVariant ( obj . uri ) <EOL> elif role == QtCore . Qt . ToolTipRole : <EOL> if obj_type == Object . USER : <EOL> tool_tip = "<STR_LIT>" % ( obj . get_display_name ( ) , obj . uri ) <EOL> return QtCore . QVariant ( tool_tip ) <EOL> elif role == QtCore . Qt . DecorationRole : <EOL> if obj_type == Object . USER : <EOL> return QtGui . QPixmap ( "<STR_LIT>" ) <EOL> return QtCore . QVariant ( ) <EOL> def parent ( self , child_index ) : <EOL> if not child_index . isValid ( ) : <EOL> return QtCore . QModelIndex ( ) <EOL> obj = child_index . internalPointer ( ) <EOL> if obj . get_type ( ) == Object . USER : <EOL> parent_obj = obj . group <EOL> row = self . group_agent . index ( parent_obj ) <EOL> return self . createIndex ( row , FIRST_COLUMN , parent_obj ) <EOL> return QtCore . QModelIndex ( ) <EOL> def flags ( self , index ) : <EOL> if not index . isValid ( ) : <EOL> return QtCore . Qt . NoItemFlags <EOL> return QtCore . Qt . ItemIsEnabled | QtCore . Qt . ItemIsSelectable <EOL> def create_group_agent ( ) : <EOL> group_a = Group ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for i in [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] : <EOL> group_a . add_user ( User ( i [ <NUM_LIT:0> ] , i [ <NUM_LIT:1> ] , group_a ) ) <EOL> group_b = Group ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> user = User ( '<STR_LIT>' , '<STR_LIT>' , group_b ) <EOL> group_b . add_user ( user ) <EOL> ga = GroupAgent ( ) <EOL> ga . add_group ( group_a ) <EOL> ga . add_group ( group_b ) <EOL> return ga <EOL> class App ( QtGui . QWidget ) : <EOL> def __init__ ( self ) : <EOL> QtGui . QWidget . __init__ ( self ) <EOL> x , y , w , h = <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:200> <EOL> self . setGeometry ( x , y , w , h ) <EOL> self . tv = QtGui . QTreeView ( self ) <EOL> self . ga = create_group_agent ( ) <EOL> self . model = Model ( self . ga ) <EOL> self . tv . setHeaderHidden ( True ) <EOL> self . tv . setModel ( self . model ) <EOL> self . selection_model = self . tv . selectionModel ( ) <EOL> self . selection_model . currentRowChanged . connect ( self . current_row_changed ) <EOL> user = self . ga . get_user_by_uri ( "<STR_LIT>" ) <EOL> user . nickname = "<STR_LIT:foo>" <EOL> def current_row_changed ( self , current_idx , prev_idx ) : <EOL> assert prev_idx != None <EOL> item = current_idx . internalPointer ( ) <EOL> if item . get_type ( ) != Object . USER : <EOL> return <EOL> qa = QtGui . QApplication ( sys . argv ) <EOL> app = App ( ) <EOL> app . show ( ) <EOL> assert app != None <EOL> qa . exec_ ( ) </s>
