eventhistory.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * wpa_gui - EventHistory class
  3. * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include <QHeaderView>
  9. #include <QScrollBar>
  10. #include "eventhistory.h"
  11. int EventListModel::rowCount(const QModelIndex &) const
  12. {
  13. return msgList.count();
  14. }
  15. int EventListModel::columnCount(const QModelIndex &) const
  16. {
  17. return 2;
  18. }
  19. QVariant EventListModel::data(const QModelIndex &index, int role) const
  20. {
  21. if (!index.isValid())
  22. return QVariant();
  23. if (role == Qt::DisplayRole)
  24. if (index.column() == 0) {
  25. if (index.row() >= timeList.size())
  26. return QVariant();
  27. return timeList.at(index.row());
  28. } else {
  29. if (index.row() >= msgList.size())
  30. return QVariant();
  31. return msgList.at(index.row());
  32. }
  33. else
  34. return QVariant();
  35. }
  36. QVariant EventListModel::headerData(int section, Qt::Orientation orientation,
  37. int role) const
  38. {
  39. if (role != Qt::DisplayRole)
  40. return QVariant();
  41. if (orientation == Qt::Horizontal) {
  42. switch (section) {
  43. case 0:
  44. return QString(tr("Timestamp"));
  45. case 1:
  46. return QString(tr("Message"));
  47. default:
  48. return QVariant();
  49. }
  50. } else
  51. return QString("%1").arg(section);
  52. }
  53. void EventListModel::addEvent(QString time, QString msg)
  54. {
  55. beginInsertRows(QModelIndex(), msgList.size(), msgList.size() + 1);
  56. timeList << time;
  57. msgList << msg;
  58. endInsertRows();
  59. }
  60. EventHistory::EventHistory(QWidget *parent, const char *, bool, Qt::WindowFlags)
  61. : QDialog(parent)
  62. {
  63. setupUi(this);
  64. connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
  65. eventListView->setItemsExpandable(false);
  66. eventListView->setRootIsDecorated(false);
  67. elm = new EventListModel(parent);
  68. eventListView->setModel(elm);
  69. }
  70. EventHistory::~EventHistory()
  71. {
  72. destroy();
  73. delete elm;
  74. }
  75. void EventHistory::languageChange()
  76. {
  77. retranslateUi(this);
  78. }
  79. void EventHistory::addEvents(WpaMsgList msgs)
  80. {
  81. WpaMsgList::iterator it;
  82. for (it = msgs.begin(); it != msgs.end(); it++)
  83. addEvent(*it);
  84. }
  85. void EventHistory::addEvent(WpaMsg msg)
  86. {
  87. bool scroll = true;
  88. if (eventListView->verticalScrollBar()->value() <
  89. eventListView->verticalScrollBar()->maximum())
  90. scroll = false;
  91. elm->addEvent(msg.getTimestamp().toString("yyyy-MM-dd hh:mm:ss.zzz"),
  92. msg.getMsg());
  93. if (scroll)
  94. eventListView->scrollToBottom();
  95. }