weekmodel.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "weekmodel.h"
  2. #include "timeslot.h"
  3. #include <QtCore/QDebug>
  4. QHash<int, QByteArray> WeekModel::roleNames()
  5. {
  6. QHash<int, QByteArray> roles;
  7. roles[DayNameRole] = "dayName";
  8. return roles;
  9. }
  10. WeekModel::WeekModel(QObject *parent) :
  11. QAbstractListModel(parent), m_days()
  12. {
  13. for (int i = 0; i < DAYS_IN_WEEK; i++) {
  14. Day* day = new Day(DAY_NAMES[i], this);
  15. m_days.append(day);
  16. qDebug() << "Created day" << day->dayName();
  17. }
  18. setRoleNames(WeekModel::roleNames());
  19. }
  20. WeekModel::~WeekModel()
  21. {
  22. for (int i = 0; i < m_days.count(); i++) {
  23. Day* day = m_days.at(i);
  24. m_days[i] = 0;
  25. delete day;
  26. day = 0;
  27. }
  28. m_days.clear();
  29. }
  30. int WeekModel::rowCount(const QModelIndex &parent) const
  31. {
  32. return m_days.count();
  33. }
  34. QVariant WeekModel::data(const QModelIndex &index, int role) const
  35. {
  36. if (index.isValid()) {
  37. int row = index.row();
  38. // qDebug() << "Requested day for column" << row;
  39. if (row >= 0 && row < m_days.count()) {
  40. Day* day = m_days[row];
  41. if (role == DayNameRole){
  42. return QVariant(day->dayName());
  43. } else {
  44. return QVariant("ERR: Unknown role for weekmodel");
  45. }
  46. } else {
  47. return QVariant("ERR: Invalid index");
  48. }
  49. } else {
  50. return QVariant("ERR: Invalid index");
  51. }
  52. return QVariant("ERR: other");
  53. }
  54. QVariant WeekModel::headerData( int section, Qt::Orientation orientation, int role) const
  55. {
  56. return QVariant("HEADER");
  57. }
  58. Qt::ItemFlags WeekModel::flags( const QModelIndex & index) const
  59. {
  60. return Qt::ItemIsEnabled;
  61. }
  62. // For editing
  63. bool WeekModel::setData( const QModelIndex & index, const QVariant & value, int role)
  64. {
  65. return true;
  66. }
  67. QObject* WeekModel::day(int index) const
  68. {
  69. qDebug() << "Requesting day for index" << index;
  70. if (index >= 0 && index < m_days.count()) {
  71. return m_days.at(index);
  72. }
  73. return 0;
  74. }