LogModel.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "LogModel.h"
  2. LogEntry::LogEntry(const QString & nmessage)
  3. : message(nmessage)
  4. , time(QTime::currentTime())
  5. {
  6. }
  7. LogEntry::~LogEntry()
  8. {
  9. }
  10. LogModel::LogModel(QObject *parent)
  11. : QAbstractTableModel(parent)
  12. {
  13. }
  14. LogModel::~LogModel()
  15. {
  16. }
  17. void LogModel::log(const QString & message)
  18. {
  19. beginInsertRows(QModelIndex(), rowCount(), rowCount());
  20. m_entries << LogEntry(message);
  21. endInsertRows();
  22. }
  23. int LogModel::rowCount(const QModelIndex &parent) const
  24. {
  25. if (!parent.isValid()) {
  26. return m_entries.count();
  27. }
  28. return 0;
  29. }
  30. int LogModel::columnCount(const QModelIndex &parent) const
  31. {
  32. if (!parent.isValid()) {
  33. return ColumnCount;
  34. }
  35. return 0;
  36. }
  37. QVariant LogModel::data(const QModelIndex &index, int role) const
  38. {
  39. if (index.isValid() && role == Qt::DisplayRole) {
  40. LogEntry entry(m_entries.at(index.row()));
  41. switch (index.column()) {
  42. case TimeColumn:
  43. return entry.time.toString();
  44. break;
  45. case MessageColumn:
  46. return entry.message;
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. return QVariant();
  53. }