Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "contactlist.h"
- #include <QtGui/QApplication>
- #include<qalgorithms.h>
- #include<QDebug>
- ContactList::ContactList(QObject *parent): QAbstractListModel(parent)
- {
- QHash<int, QByteArray> roles;
- roles[NameRole] = "name";
- roles[StatusRole] = "status";
- roles[ReceivedRole]="received";
- setRoleNames(roles);
- showHide=0;
- indexNotOnline=0;
- }
- void ContactList::showHideContacts(bool value)
- {
- if(rowCount()==contactList.count()&&value==0) return;
- qDebug()<<"IIIIIIII";
- if(value) showHide=true;
- else showHide=false;
- // rowCount();
- int i=0;
- if(showHide==0)
- { qDebug()<<"true";
- fakeAdd();
- // return contactList.count();
- }
- else {
- qDebug()<<"false";
- fakeDelete();
- // return contactList.count();
- // return i;
- }
- dataChanged(index(0),index(rowCount()));
- }
- void ContactList::addContact(const Contact &contact,QString str)
- {
- beginInsertRows(QModelIndex(), rowCount(), rowCount());
- if(str=="up") contactList.push_front(contact);
- else if(str=="down") contactList.push_back(contact);
- endInsertRows();
- sortContacts();
- // showHideContacts();
- }
- bool thanOnline(const Contact &s1, const Contact &s2)
- {
- if(s1.getStatus() != s2.getStatus())
- return s1.getStatus() > s2.getStatus();
- return s1.getUserName() < s2.getUserName();
- }
- void ContactList::sortContacts()
- {
- qSort(contactList.begin(), contactList.end(), thanOnline);
- emit dataChanged(index(0),index(rowCount()-1));
- }
- Contact ContactList::getContact(QModelIndex index)
- {
- return contactList.at(index.row());
- }
- void ContactList::fakeAdd()
- {
- // if(rowCount()==contactList.count()) return;
- int i=0;
- for(i;i<contactList.count();++i)
- if (contactList.at(i).getStatus()=="") break;
- indexNotOnline=i;
- qDebug()<<contactList.count();
- beginInsertRows(QModelIndex(), i, contactList.count());
- endInsertRows();
- }
- void ContactList::fakeDelete()
- {
- int i=0;
- for(i;i<contactList.count();++i)
- if (contactList.at(i).getStatus()=="") break;
- qDebug()<<i<<"INDEEEX";
- indexNotOnline=i;
- beginRemoveRows(QModelIndex(), i, contactList.count());
- endRemoveRows();
- qDebug()<<i<<contactList.count()<<"deleted";
- }
- QModelIndex ContactList::getContactIndex(QString jid)
- {
- for(int i=0;i<rowCount();++i)
- if(contactList.at(i).getJID()==jid) return index(i,0);
- return index(-1,-1);
- }
- int ContactList::rowCount(const QModelIndex & parent)const {
- if(showHide==1)
- return indexNotOnline-1;
- else
- return contactList.count();
- }
- QVariant ContactList::data(const QModelIndex & index, int role) const {
- if (index.row() < 0 || index.row() > contactList.count())
- return QVariant();
- const Contact &contact = contactList[index.row()];
- if (role == NameRole)
- return contact.getUserName();
- else if (role == StatusRole)
- return contact.getStatus();
- else if (role == ReceivedRole)
- return contact.getMReceivedState();
- return QVariant();
- }
- Qt::ItemFlags ContactList::flags(const QModelIndex &index) const
- {
- if (!index.isValid())
- return Qt::ItemIsEnabled;
- return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
- }
- bool ContactList::setData(const QModelIndex &index,
- const Contact &value, int role)
- {
- if (index.isValid() && role == Qt::EditRole) {
- contactList.replace(index.row(), value);
- sortContacts();
- // showHideContacts();
- return true;
- }
- return false;
- }
- bool ContactList::setCountMessaageForContact(const QModelIndex index,
- const int value, int role)
- {
- if (index.isValid() && role == Qt::EditRole) {
- contactList[index.row()].setMessageCount(value);
- emit dataChanged(index, index);
- return true;
- }
- return false;
- }
- void ContactList::delContact(const QModelIndex & index) {
- beginRemoveRows(QModelIndex(), index.row(), index.row());
- contactList.removeAt(index.row());
- endRemoveRows();
- //showHideContacts();
- }
Add Comment
Please, Sign In to add comment