Advertisement
Guest User

Untitled

a guest
Jan 29th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ListItem: public QObject {
  2.   Q_OBJECT
  3.  
  4. public:
  5.     enum Roles {
  6.       IdRole = Qt::UserRole+1,
  7.       NameRole,
  8.       NumQuestionsRole,
  9.       NumQuestionsNoCasesRole
  10.     };
  11.   ListItem(QObject* parent = 0) : QObject(parent) {}
  12.   explicit ListItem(const int &id, const QString &name, const int &numQuestions, const int &numQuestionNoCases, QObject *parent = 0);
  13.   ~ListItem() {}
  14.   QVariant data(int role) const;
  15.   QHash<int, QByteArray> roleNames() const;
  16.   inline int id() const { return m_id; }
  17.   inline QString name() const { return m_name; }
  18.   inline int num_q() const { return m_numQuestions; }
  19.   inline int num_qnc() const { return m_numQuestionsNoCases; }
  20.  
  21. signals:
  22.   void dataChanged();
  23.  
  24. private:
  25.   int m_id;
  26.   QString m_name;
  27.   int m_numQuestions;
  28.   int m_numQuestionsNoCases;
  29. };
  30.  
  31. class QmlAbstractListModel : public QAbstractListModel
  32. {
  33.     Q_OBJECT
  34. public:
  35. //    explicit QmlAbstractListModel(ListItem* prototype, QObject* parent = 0);
  36.     explicit QmlAbstractListModel(QObject* parent = 0);
  37.     ~QmlAbstractListModel();
  38.     int rowCount(const QModelIndex &parent = QModelIndex()) const;
  39.     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  40.     virtual void appendRow(ListItem* item);
  41.     virtual void appendRows(const QList<ListItem*> &items);
  42.     void insertRow(int row, ListItem* item);
  43.     bool removeRow(int row, const QModelIndex &parent = QModelIndex());
  44.     bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
  45.     ListItem* takeRow(int row);
  46.     ListItem* find(const int &id) const;
  47.     QModelIndex indexFromItem( const ListItem* item) const;
  48.     void clear();
  49.   QList<ListItem*> m_list;
  50.  
  51. private:
  52. //  ListItem* m_prototype;
  53.  
  54. private slots:
  55.     void handleItemChange();
  56.    
  57. };
  58.  
  59. class QmlSubjectListModel : public QmlAbstractListModel
  60. {
  61.     Q_OBJECT
  62. public:
  63.     explicit QmlSubjectListModel(QObject *parent = 0);
  64.  
  65. private:
  66.     void readSqlData();
  67.     void reload();
  68.     void appendRow(ListItem *item);
  69.     void appendRows(const QList<ListItem *> &items);
  70. };
  71.  
  72. void QmlSubjectListModel::readSqlData()
  73. {
  74.     DEBUG_BLOCK
  75.  
  76.     QSqlDatabase db=QSqlDatabase::database(QUESTIONSDB);
  77.  
  78.     if (db.isOpen())
  79.     {
  80.         DBQuery q(db);
  81.         q.setForwardOnly(true);
  82.  
  83.         q.exec(QString("SELECT s.id, s.name, COUNT(q.subjectId), "
  84.                        "COUNT(NULLIF(q.caseId, 0)) FROM "
  85.                        "tSubjects AS s, tQuestions AS q WHERE "
  86.                        "s.name <> 'Fallbeschreibung' AND "
  87.                        "s.name <> 'root' AND s.id = q.subjectId "
  88.                        "GROUP BY q.subjectId ORDER BY s.name ASC"));
  89.  
  90.         while(q.next()) {
  91.  
  92.             ListItem *item=new ListItem( q.value(0).toInt(),    //id
  93.                                  q.value(1).toString(), //name
  94.                                          q.value(2).toInt(),    //number of questions
  95.                                          q.value(2).toInt()-q.value(3).toInt()); //number of questions excluding cases
  96.             appendRow(item);
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement