00001 #include <QDomElement>
00002 #include <QDebug>
00003 #include <QtAlgorithms>
00004 #include "datachapter.h"
00005 #include "datahash.h"
00006
00007 bool DataChapter::loadDataNode(const QDomNode data)
00008 {
00009 chapterName = data.toElement().attribute("name");
00010 chapterId = data.toElement().attribute("chapterId").toUInt();
00011
00012 QDomNode node = data.firstChild();
00013 while(!node.isNull())
00014 {
00015 if(node.nodeName() == "exercise")
00016 {
00017 DataExercise *ex = new DataExercise();
00018 if(!ex->loadDataNode(node))
00019 {
00020 qDebug() << "error loading dataexercise";
00021 delete ex;
00022 return false;
00023 }
00024 exerciseList.append(ex);
00025 connect(ex, SIGNAL(changed()), this, SLOT(dataChanged()));
00026
00027
00028 }
00029 else if(node.nodeName() != "#comment")
00030 {
00031 qDebug() << "Unknown element in section tag:" << node.nodeName();
00032 }
00033 node = node.nextSibling();
00034 }
00035 return true;
00036 }
00037
00038 QDomElement DataChapter::saveDataNode(QDomDocument* xml)
00039 {
00040 QDomElement section = xml->createElement("chapter");
00041 section.setAttribute("name", chapterName);
00042 section.setAttribute("chapterId", QString::number(chapterId));
00043
00044 for(int i = 0; i < exerciseList.size(); i++)
00045 {
00046 section.appendChild(exerciseList[i]->saveDataNode(xml));
00047 }
00048 return section;
00049 }
00050
00051 void DataChapter::hash(unsigned int &startValue)
00052 {
00053
00054
00055 DataHash::hash(chapterName, startValue);
00056 DataHash::hash(&chapterId, sizeof(chapterId), startValue);
00057
00058 for(int i = 0; i < exerciseList.size(); i++)
00059 {
00060 exerciseList[i]->hash(startValue);
00061 }
00062 }
00063
00064 void DataChapter::addExercise(DataExercise *ex)
00065 {
00066 exerciseList.append(ex);
00067 }
00068
00069 DataChapter::DataChapter()
00070 {
00071 }
00072
00073 DataChapter::~DataChapter()
00074 {
00075 qDeleteAll(exerciseList);
00076 exerciseList.clear();
00077 }
00078
00079 DataExercise* DataChapter::exercise(QString exerciseName)
00080 {
00081 for(int i = 0; i < exerciseList.size(); i++)
00082 {
00083 if(exerciseList.value(i)->name() == exerciseName)
00084 {
00085 return exerciseList.value(i);
00086 }
00087 }
00088 return NULL;
00089 }