#include "usertreewidget.h" UserTreeWidget::UserTreeWidget(QWidget *parent) : QTreeWidget(parent) { QTreeWidgetItem* header = headerItem(); int c = 0; header->setText(c++, "Login"); header->setText(c++, "IP"); header->setText(c++, "Location"); header->setText(c++, "State"); header->setText(c++, "Promo"); header->setText(c++, "Data"); header->setText(c++, "ID"); setRootIsDecorated(false); setSortingEnabled(true); setStyleSheet("QHeaderView::section\n{\ncolor: black;\n}"); connect(this, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(itemDoubleClicked(QTreeWidgetItem*,int))); sortByColumn(0, Qt::AscendingOrder); setSelectionMode(QAbstractItemView::ExtendedSelection); setContextMenuPolicy(Qt::CustomContextMenu); } NetSoul::User UserTreeWidget::getSelectedUser() { if(!currentItem()) return NetSoul::User(); return qvariant_cast(currentItem()->data(0, Qt::UserRole)); } NetSoul::Users UserTreeWidget::getSelectedUsers() { NetSoul::Users users; foreach(QTreeWidgetItem* item, selectedItems()) users.append(qvariant_cast(item->data(0, Qt::UserRole))); return users; } void UserTreeWidget::updateUser(NetSoul::User user) { bool found = false; for(int i = 0; i < topLevelItemCount(); ++i) { QTreeWidgetItem* item = topLevelItem(i); if(item->text(0) == user.login) { found = true; if(item->data(1, Qt::UserRole).toString() == user.id || item->data(1, Qt::UserRole).isNull()) { if(user.state != NetSoul::UserStateNotSet) { if(user.state == NetSoul::Actif || user.state == NetSoul::Login) item->setIcon(0, QIcon(":/epimafia/icon_green.jpg")); else if(user.state == NetSoul::Away) item->setIcon(0, QIcon(":/epimafia/icon_blue.jpg")); else if(user.state == NetSoul::Lock) item->setIcon(0, QIcon(":/epimafia/icon_red.jpg")); else { for(int i = 0; i < topLevelItemCount(); ++i) { QTreeWidgetItem* an = topLevelItem(i); if(an != item && an->text(0) == user.login) { delete item; return; } } item->setIcon(0, QIcon(":/epimafia/icon.jpg")); for(int i = 1; i < 7; ++i) item->setText(i, ""); return; } if(user.state != NetSoul::UnknownState) item->setText(3, NetSoul::getStringFromUserState(user.state)); else item->setText(3, user.stateString); } QVariant v; v.setValue(user); item->setData(0, Qt::UserRole, v); item->setData(1, Qt::UserRole, user.id); if(!user.ip.isNull()) item->setText(1, user.ip); if(!user.location.isNull()) item->setText(2, user.location); if(!user.promo.isNull()) item->setText(4, user.promo); if(!user.data.isEmpty()) item->setText(5, user.data); if(!user.id.isNull()) item->setText(6, user.id); return; } } } if(found) addUser(user); } void UserTreeWidget::updateUsers(NetSoul::Users users) { foreach (NetSoul::User user, users) updateUser(user); } void UserTreeWidget::addUser(NetSoul::User user) { QTreeWidgetItem* item = new QTreeWidgetItem(); int c = 0; item->setData(1, Qt::UserRole, user.id); item->setText(c++, user.login); item->setText(c++, user.ip); item->setText(c++, user.location); if(user.state != NetSoul::UnknownState) item->setText(c++, NetSoul::getStringFromUserState(user.state)); else item->setText(c++, user.stateString); item->setText(c++, user.promo); item->setText(c++, user.data); item->setText(c++, user.id); if(user.state == NetSoul::Actif || user.state == NetSoul::Login) item->setIcon(0, QIcon(":/epimafia/icon_green.jpg")); else if(user.state == NetSoul::Away) item->setIcon(0, QIcon(":/epimafia/icon_blue.jpg")); else if(user.state == NetSoul::Lock) item->setIcon(0, QIcon(":/epimafia/icon_red.jpg")); else item->setIcon(0, QIcon(":/epimafia/icon.jpg")); QVariant v; v.setValue(user); item->setData(0, Qt::UserRole, v); addTopLevelItem(item); } void UserTreeWidget::addUsers(NetSoul::Users users) { foreach(NetSoul::User user, users) addUser(user); } void UserTreeWidget::remove(QStringList logins) { for(int i = 0; i < topLevelItemCount(); ++i) { QTreeWidgetItem* item = topLevelItem(i); if(logins.contains(item->text(0))) { delete item; --i; } } } void UserTreeWidget::filter(QString text) { for(int i = 0; i < topLevelItemCount(); ++i) topLevelItem(i)->setHidden(!itemContains(topLevelItem(i), text)); } void UserTreeWidget::resetUsers() { for(int i = 0; i < topLevelItemCount(); ++i) topLevelItem(i)->setIcon(0, QIcon(":/epimafia/icon.jpg")); } void UserTreeWidget::itemDoubleClicked(QTreeWidgetItem *item, int) { emit userSelected(qvariant_cast(item->data(0, Qt::UserRole))); } bool UserTreeWidget::itemContains(QTreeWidgetItem *item, QString text) { for(int i = 0; i < item->columnCount(); ++i) if(item->text(i).contains(text)) return true; return false; }