Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

usertreewidget.cpp 5.9KB

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