Guest User

Untitled

a guest
Aug 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include "contactlist.h"
  2. #include <QtGui/QApplication>
  3. #include<qalgorithms.h>
  4. #include<QDebug>
  5. ContactList::ContactList(QObject *parent): QAbstractListModel(parent)
  6. {
  7.     QHash<int, QByteArray> roles;
  8.     roles[NameRole] = "name";
  9.     roles[StatusRole] = "status";
  10.     roles[ReceivedRole]="received";
  11.     setRoleNames(roles);
  12.     showHide=0;
  13.     indexNotOnline=0;
  14. }
  15.  
  16. void ContactList::showHideContacts(bool value)
  17. {
  18.     if(rowCount()==contactList.count()&&value==0) return;
  19.     qDebug()<<"IIIIIIII";
  20.     if(value) showHide=true;
  21.     else showHide=false;
  22.    // rowCount();
  23.     int i=0;
  24.     if(showHide==0)
  25.     {   qDebug()<<"true";
  26.         fakeAdd();
  27.        // return contactList.count();
  28.     }
  29.  
  30.     else {
  31.         qDebug()<<"false";
  32.        fakeDelete();
  33.        // return contactList.count();
  34.        // return i;
  35.     }
  36.     dataChanged(index(0),index(rowCount()));
  37.  
  38. }
  39.  
  40. void ContactList::addContact(const Contact &contact,QString str)
  41. {
  42.     beginInsertRows(QModelIndex(), rowCount(), rowCount());
  43.     if(str=="up") contactList.push_front(contact);
  44.     else if(str=="down") contactList.push_back(contact);
  45.     endInsertRows();
  46.     sortContacts();
  47.    // showHideContacts();
  48. }
  49.  
  50. bool thanOnline(const Contact &s1, const Contact &s2)
  51. {
  52.     if(s1.getStatus() != s2.getStatus())
  53.         return s1.getStatus() > s2.getStatus();
  54.     return s1.getUserName() < s2.getUserName();
  55. }
  56.  
  57. void ContactList::sortContacts()
  58. {
  59.     qSort(contactList.begin(), contactList.end(), thanOnline);
  60.     emit dataChanged(index(0),index(rowCount()-1));
  61. }
  62.  
  63. Contact ContactList::getContact(QModelIndex index)
  64. {
  65.     return contactList.at(index.row());
  66. }
  67. void ContactList::fakeAdd()
  68. {
  69.    // if(rowCount()==contactList.count()) return;
  70.     int i=0;
  71.     for(i;i<contactList.count();++i)
  72.         if (contactList.at(i).getStatus()=="") break;
  73.     indexNotOnline=i;
  74.     qDebug()<<contactList.count();
  75.     beginInsertRows(QModelIndex(), i, contactList.count());
  76.  
  77.     endInsertRows();
  78.  
  79. }
  80.  
  81. void ContactList::fakeDelete()
  82. {
  83.     int i=0;
  84.     for(i;i<contactList.count();++i)
  85.         if (contactList.at(i).getStatus()=="") break;
  86.     qDebug()<<i<<"INDEEEX";
  87.     indexNotOnline=i;
  88.     beginRemoveRows(QModelIndex(), i, contactList.count());
  89.  
  90.     endRemoveRows();
  91.     qDebug()<<i<<contactList.count()<<"deleted";
  92. }
  93.  
  94. QModelIndex ContactList::getContactIndex(QString jid)
  95. {
  96.     for(int i=0;i<rowCount();++i)
  97.         if(contactList.at(i).getJID()==jid) return index(i,0);
  98.     return index(-1,-1);
  99. }
  100.  
  101. int ContactList::rowCount(const QModelIndex & parent)const {
  102.     if(showHide==1)
  103.         return indexNotOnline-1;
  104.     else
  105.    return contactList.count();
  106.  
  107. }
  108.  
  109. QVariant ContactList::data(const QModelIndex & index, int role) const {
  110.     if (index.row() < 0 || index.row() > contactList.count())
  111.         return QVariant();
  112.     const Contact &contact = contactList[index.row()];
  113.     if (role == NameRole)
  114.         return contact.getUserName();
  115.     else if (role == StatusRole)
  116.         return contact.getStatus();
  117.     else if (role == ReceivedRole)
  118.         return contact.getMReceivedState();
  119.     return QVariant();
  120. }
  121.  
  122. Qt::ItemFlags ContactList::flags(const QModelIndex &index) const
  123. {
  124.     if (!index.isValid())
  125.         return Qt::ItemIsEnabled;
  126.  
  127.     return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
  128. }
  129.  
  130. bool ContactList::setData(const QModelIndex &index,
  131.                               const Contact &value, int role)
  132. {
  133.     if (index.isValid() && role == Qt::EditRole) {
  134.         contactList.replace(index.row(), value);
  135.         sortContacts();
  136.        // showHideContacts();
  137.         return true;
  138.     }
  139.     return false;
  140. }
  141.  
  142. bool ContactList::setCountMessaageForContact(const QModelIndex index,
  143.                                               const int value, int role)
  144. {
  145.     if (index.isValid() && role == Qt::EditRole) {
  146.         contactList[index.row()].setMessageCount(value);
  147.         emit dataChanged(index, index);
  148.         return true;
  149.     }
  150.     return false;
  151. }
  152.  
  153. void ContactList::delContact(const QModelIndex & index) {
  154.     beginRemoveRows(QModelIndex(), index.row(), index.row());
  155.     contactList.removeAt(index.row());
  156.     endRemoveRows();
  157.     //showHideContacts();
  158. }
Add Comment
Please, Sign In to add comment