Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

usertreewidget.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "usertreewidget.h"
  2. UserTreeWidget::UserTreeWidget(QWidget *parent) : QTreeWidget(parent)
  3. {
  4. QTreeWidgetItem* header = headerItem();
  5. int c = 0;
  6. header->setText(c++, "Login");
  7. header->setText(c++, "IP");
  8. header->setText(c++, "Location");
  9. header->setText(c++, "State");
  10. header->setText(c++, "Promo");
  11. header->setText(c++, "Data");
  12. header->setText(c++, "ID");
  13. setRootIsDecorated(false);
  14. setSortingEnabled(true);
  15. setStyleSheet("QHeaderView::section\n{\ncolor: black;\n}");
  16. connect(this, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(itemDoubleClicked(QTreeWidgetItem*,int)));
  17. sortByColumn(0, Qt::AscendingOrder);
  18. setSelectionMode(QAbstractItemView::ExtendedSelection);
  19. setContextMenuPolicy(Qt::CustomContextMenu);
  20. }
  21. NetSoul::User UserTreeWidget::getSelectedUser()
  22. {
  23. if(!currentItem())
  24. return NetSoul::User();
  25. return qvariant_cast<NetSoul::User>(currentItem()->data(0, Qt::UserRole));
  26. }
  27. NetSoul::Users UserTreeWidget::getSelectedUsers()
  28. {
  29. NetSoul::Users users;
  30. foreach(QTreeWidgetItem* item, selectedItems())
  31. users.append(qvariant_cast<NetSoul::User>(item->data(0, Qt::UserRole)));
  32. return users;
  33. }
  34. void UserTreeWidget::updateUser(NetSoul::User user)
  35. {
  36. bool found = false;
  37. for(int i = 0; i < topLevelItemCount(); ++i)
  38. {
  39. QTreeWidgetItem* item = topLevelItem(i);
  40. if(item->text(0) == user.login)
  41. {
  42. found = true;
  43. if(item->data(1, Qt::UserRole).toString() == user.id || item->data(1, Qt::UserRole).isNull())
  44. {
  45. if(user.state != NetSoul::UserStateNotSet)
  46. {
  47. if(user.state == NetSoul::Actif || user.state == NetSoul::Login)
  48. item->setIcon(0, QIcon(":/epimafia/icon_green.jpg"));
  49. else if(user.state == NetSoul::Away)
  50. item->setIcon(0, QIcon(":/epimafia/icon_blue.jpg"));
  51. else if(user.state == NetSoul::Lock)
  52. item->setIcon(0, QIcon(":/epimafia/icon_red.jpg"));
  53. else
  54. {
  55. for(int i = 0; i < topLevelItemCount(); ++i)
  56. {
  57. QTreeWidgetItem* an = topLevelItem(i);
  58. if(an != item && an->text(0) == user.login)
  59. {
  60. delete item;
  61. return;
  62. }
  63. }
  64. item->setIcon(0, QIcon(":/epimafia/icon.jpg"));
  65. for(int i = 1; i < 7; ++i)
  66. item->setText(i, "");
  67. return;
  68. }
  69. if(user.state != NetSoul::UnknownState)
  70. item->setText(3, NetSoul::getStringFromUserState(user.state));
  71. else
  72. item->setText(3, user.stateString);
  73. }
  74. QVariant v;
  75. v.setValue(user);
  76. item->setData(0, Qt::UserRole, v);
  77. item->setData(1, Qt::UserRole, user.id);
  78. if(!user.ip.isNull())
  79. item->setText(1, user.ip);
  80. if(!user.location.isNull())
  81. item->setText(2, user.location);
  82. if(!user.promo.isNull())
  83. item->setText(4, user.promo);
  84. if(!user.data.isEmpty())
  85. item->setText(5, user.data);
  86. if(!user.id.isNull())
  87. item->setText(6, user.id);
  88. return;
  89. }
  90. }
  91. }
  92. if(found)
  93. addUser(user);
  94. }
  95. void UserTreeWidget::updateUsers(NetSoul::Users users)
  96. {
  97. foreach (NetSoul::User user, users)
  98. updateUser(user);
  99. }
  100. void UserTreeWidget::addUser(NetSoul::User user)
  101. {
  102. QTreeWidgetItem* item = new QTreeWidgetItem();
  103. int c = 0;
  104. item->setData(1, Qt::UserRole, user.id);
  105. item->setText(c++, user.login);
  106. item->setText(c++, user.ip);
  107. item->setText(c++, user.location);
  108. if(user.state != NetSoul::UnknownState)
  109. item->setText(c++, NetSoul::getStringFromUserState(user.state));
  110. else
  111. item->setText(c++, user.stateString);
  112. item->setText(c++, user.promo);
  113. item->setText(c++, user.data);
  114. item->setText(c++, user.id);
  115. if(user.state == NetSoul::Actif || user.state == NetSoul::Login)
  116. item->setIcon(0, QIcon(":/epimafia/icon_green.jpg"));
  117. else if(user.state == NetSoul::Away)
  118. item->setIcon(0, QIcon(":/epimafia/icon_blue.jpg"));
  119. else if(user.state == NetSoul::Lock)
  120. item->setIcon(0, QIcon(":/epimafia/icon_red.jpg"));
  121. else
  122. item->setIcon(0, QIcon(":/epimafia/icon.jpg"));
  123. QVariant v;
  124. v.setValue(user);
  125. item->setData(0, Qt::UserRole, v);
  126. addTopLevelItem(item);
  127. }
  128. void UserTreeWidget::addUsers(NetSoul::Users users)
  129. {
  130. foreach(NetSoul::User user, users)
  131. addUser(user);
  132. }
  133. void UserTreeWidget::remove(QStringList logins)
  134. {
  135. for(int i = 0; i < topLevelItemCount(); ++i)
  136. {
  137. QTreeWidgetItem* item = topLevelItem(i);
  138. if(logins.contains(item->text(0)))
  139. {
  140. delete item;
  141. --i;
  142. }
  143. }
  144. }
  145. void UserTreeWidget::filter(QString text)
  146. {
  147. for(int i = 0; i < topLevelItemCount(); ++i)
  148. topLevelItem(i)->setHidden(!itemContains(topLevelItem(i), text));
  149. }
  150. void UserTreeWidget::resetUsers()
  151. {
  152. for(int i = 0; i < topLevelItemCount(); ++i)
  153. topLevelItem(i)->setIcon(0, QIcon(":/epimafia/icon.jpg"));
  154. }
  155. void UserTreeWidget::itemDoubleClicked(QTreeWidgetItem *item, int)
  156. {
  157. emit userSelected(qvariant_cast<NetSoul::User>(item->data(0, Qt::UserRole)));
  158. }
  159. bool UserTreeWidget::itemContains(QTreeWidgetItem *item, QString text)
  160. {
  161. for(int i = 0; i < item->columnCount(); ++i)
  162. if(item->text(i).contains(text))
  163. return true;
  164. return false;
  165. }