Show
Ignore:
Timestamp:
06/14/08 23:21:57 (6 months ago)
Author:
flynd
Message:

dataChanged signal cannot be sent between beginInsertRow and endInsertRow. Revised signalling when adding and removing users in groups to fix this. This fixes the empty rows sometimes seen first in groups after users have been added.

Location:
trunk/qt4-gui/src/contactlist
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/qt4-gui/src/contactlist/contactgroup.cpp

    r6283 r6309  
    133133void ContactGroup::addUser(ContactUser* user, ContactListModel::SubGroupType subGroup) 
    134134{ 
     135  // Signal that we are about to add a row 
     136  emit beginInsert(this, rowCount()); 
     137 
    135138  myUsers.append(user); 
    136139  if (user->visibility()) 
     
    139142  myEvents += user->numEvents(); 
    140143  myBars[subGroup]->updateNumEvents(user->numEvents()); 
     144 
     145  // Signal that we're done adding 
     146  emit endInsert(); 
     147 
     148  // Update group and bar as counters may have changed 
    141149  emit barDataChanged(myBars[subGroup], subGroup); 
    142  
    143150  emit dataChanged(this); 
    144151} 
     
    146153void ContactGroup::removeUser(ContactUser* user, ContactListModel::SubGroupType subGroup) 
    147154{ 
     155  // Signal that we are about to remove a row 
     156  emit beginRemove(this, indexOf(user)); 
     157 
    148158  myUsers.removeAll(user); 
    149159  if (user->visibility()) 
     
    152162  myEvents -= user->numEvents(); 
    153163  myBars[subGroup]->updateNumEvents(-user->numEvents()); 
     164 
     165  // Signal that we're done removing 
     166  emit endRemove(); 
     167 
     168  // Update group and bar as counters may have changed 
    154169  emit barDataChanged(myBars[subGroup], subGroup); 
    155  
    156170  emit dataChanged(this); 
    157171} 
  • trunk/qt4-gui/src/contactlist/contactgroup.h

    r6283 r6309  
    181181   */ 
    182182  void barDataChanged(ContactBar* bar, int row); 
     183 
     184  /** 
     185   * Signal emitted before a user is added 
     186   * 
     187   * @param group The affected group (always sent as this) 
     188   * @param row Row number for the new user 
     189   */ 
     190  void beginInsert(ContactGroup* group, int row); 
     191 
     192  /** 
     193   * Signal emitted after a user has been added 
     194   */ 
     195  void endInsert(); 
     196 
     197  /** 
     198   * Signal emitted before a user is removed 
     199   * 
     200   * @param group The affected group (always sent as this) 
     201   * @param row Row number for the user to be removed 
     202   */ 
     203  void beginRemove(ContactGroup* gorup, int row); 
     204 
     205  /** 
     206   * Segnal emitted after a user has been removed 
     207   */ 
     208  void endRemove(); 
    183209 
    184210private: 
  • trunk/qt4-gui/src/contactlist/contactlist.cpp

    r6283 r6309  
    6363  connect(group, SIGNAL(barDataChanged(ContactBar*, int)), 
    6464      SLOT(barDataChanged(ContactBar*, int))); 
     65  connect(group, SIGNAL(beginInsert(ContactGroup*, int)), 
     66      SLOT(groupBeginInsert(ContactGroup*, int))); 
     67  connect(group, SIGNAL(endInsert()), SLOT(groupEndInsert())); 
     68  connect(group, SIGNAL(beginRemove(ContactGroup*, int)), 
     69      SLOT(groupBeginRemove(ContactGroup*, int))); 
     70  connect(group, SIGNAL(endRemove()), SLOT(groupEndRemove())); 
    6571  return group; 
    6672} 
     
    236242    return; 
    237243 
    238   int groupRow = (group->groupId() < SystemGroupOffset 
    239       ? myUserGroups.indexOf(group) 
    240       : myUserGroups.size() + group->groupId() - SystemGroupOffset); 
    241  
    242   emit dataChanged(createIndex(groupRow, 0, group), createIndex(groupRow, myColumnCount - 1, group)); 
     244  int row = groupRow(group); 
     245  emit dataChanged(createIndex(row, 0, group), createIndex(row, myColumnCount - 1, group)); 
    243246} 
    244247 
     
    249252 
    250253  emit dataChanged(createIndex(row, 0, bar), createIndex(row, myColumnCount - 1, bar)); 
     254} 
     255 
     256void ContactListModel::groupBeginInsert(ContactGroup* group, int row) 
     257{ 
     258  beginInsertRows(createIndex(groupRow(group), 0, group), row, row); 
     259} 
     260 
     261void ContactListModel::groupEndInsert() 
     262{ 
     263  endInsertRows(); 
     264} 
     265 
     266void ContactListModel::groupBeginRemove(ContactGroup* group, int row) 
     267{ 
     268  beginRemoveRows(createIndex(groupRow(group), 0, group), row, row); 
     269} 
     270 
     271void ContactListModel::groupEndRemove() 
     272{ 
     273  endRemoveRows(); 
    251274} 
    252275 
     
    261284 
    262285  // Add all groups 
    263   beginInsertRows(QModelIndex(), 0, gUserManager.NumGroups() + 1); 
    264  
    265286  ContactGroup* newGroup = createGroup(0, tr("Other Users")); 
    266287  myUserGroups.append(newGroup); 
     
    277298  FOR_EACH_GROUP_END 
    278299 
    279   endInsertRows(); 
    280  
    281300  // Add all users 
    282301  FOR_EACH_USER_START(LOCK_R) 
     
    299318  } 
    300319  return 0; 
     320} 
     321 
     322int ContactListModel::groupRow(ContactGroup* group) const 
     323{ 
     324  unsigned short groupId = group->groupId(); 
     325 
     326  if (groupId < SystemGroupOffset) 
     327    return myUserGroups.indexOf(group); 
     328  else if (groupId <= SystemGroupOffset + NUM_GROUPS_SYSTEM_ALL) 
     329    return myUserGroups.size() + groupId - SystemGroupOffset; 
     330  else 
     331    return -1; 
    301332} 
    302333 
     
    322353    bool shouldBeMember = (gid != 0 && licqUser->GetInGroup(GROUPS_USER, gid)) || 
    323354        (gid == 0 && licqUser->GetGroups().empty() && !licqUser->IgnoreList()); 
    324     updateUserGroup(user, group, i, shouldBeMember); 
     355    updateUserGroup(user, group, shouldBeMember); 
    325356  } 
    326357 
     
    328359  for (unsigned long i = 0; i < NUM_GROUPS_SYSTEM_ALL; ++i) 
    329360  { 
    330     updateUserGroup(user, mySystemGroups[i], myUserGroups.size()+i, licqUser->GetInGroup(GROUPS_SYSTEM, i)); 
    331   } 
    332 } 
    333  
    334 void ContactListModel::updateUserGroup(ContactUserData* user, ContactGroup* group, int groupRow, bool shouldBeMember) 
     361    updateUserGroup(user, mySystemGroups[i], licqUser->GetInGroup(GROUPS_SYSTEM, i)); 
     362  } 
     363} 
     364 
     365void ContactListModel::updateUserGroup(ContactUserData* user, ContactGroup* group, bool shouldBeMember) 
    335366{ 
    336367  ContactUser* member = group->user(user); 
     
    340371    return; 
    341372 
     373  // Adding or removing the user is enough here, signals will be sent from group 
    342374  if (shouldBeMember) 
    343   { 
    344     // Add user to group 
    345     beginInsertRows(createIndex(groupRow, 0, group), group->rowCount(), group->rowCount()); 
    346375    new ContactUser(user, group); 
    347     endInsertRows(); 
    348   } 
    349376  else 
    350   { 
    351     // Remove user from the group 
    352     int index = group->indexOf(member); 
    353     beginRemoveRows(createIndex(groupRow, 0, group), index, index); 
    354377    delete member; 
    355     endRemoveRows(); 
    356   } 
    357378} 
    358379 
     
    367388    ContactGroup* group = u->group(); 
    368389    int pos = group->indexOf(u); 
    369     int groupRow = (group->groupId() < SystemGroupOffset ? myUserGroups.indexOf(group) : myUserGroups.size() + group->groupId() - SystemGroupOffset); 
    370     beginRemoveRows(createIndex(groupRow, 0, group), pos, pos); 
     390    beginRemoveRows(createIndex(groupRow(group), 0, group), pos, pos); 
    371391    delete u; 
    372392    endRemoveRows(); 
     
    436456      return QModelIndex(); 
    437457  } 
    438   int groupRow = (group->groupId() < SystemGroupOffset ? myUserGroups.indexOf(group) : myUserGroups.size() + group->groupId() - SystemGroupOffset); 
    439   return createIndex(groupRow, 0, group); 
     458  return createIndex(groupRow(group), 0, group); 
    440459} 
    441460 
  • trunk/qt4-gui/src/contactlist/contactlist.h

    r6283 r6309  
    376376 
    377377  /** 
     378   * A group is about to add a user 
     379   * Will send beginInsertRow signal for the user 
     380   * 
     381   * @param group The group that's adding the user 
     382   * @param row The row the new user will have in the group 
     383   */ 
     384  void groupBeginInsert(ContactGroup* group, int row); 
     385 
     386  /** 
     387   * A group has finished adding a user 
     388   * Will send endInsertRow signal 
     389   */ 
     390  void groupEndInsert(); 
     391 
     392  /** 
     393   * A group is about to remove a user 
     394   * Will send beginRemoveRow signal for the user 
     395   * 
     396   * @param group The group that's removing the user 
     397   * @param row The row of the user about to be removed 
     398   */ 
     399  void groupBeginRemove(ContactGroup* group, int row); 
     400 
     401  /** 
     402   * A group has finished removing a user 
     403   * Will send endRemoveRow signal 
     404   */ 
     405  void groupEndRemove(); 
     406 
     407  /** 
    378408   * Update the user membership in all groups 
    379409   * This is triggered by the user data object when its group membership (may) have changed 
     
    408438   * @param user The user to update 
    409439   * @param group The group to check 
    410    * @param groupRow The row index of the group 
    411440   * @param shouldBeMember True to add the user if missing or false to remove if member 
    412441   */ 
    413   void updateUserGroup(ContactUserData* user, ContactGroup* group, int groupRow, bool shouldBeMember); 
     442  void updateUserGroup(ContactUserData* user, ContactGroup* group, bool shouldBeMember); 
     443 
     444  /** 
     445   * Get model row for a group 
     446   * 
     447   * @param group A group object 
     448   * @return Row in model or -1 if not found 
     449   */ 
     450  int groupRow(ContactGroup* group) const; 
    414451 
    415452  QList<ContactGroup*> myUserGroups;