eventhistory.cpp 2.5 KB

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