File #zchsyl18-3486 - CPP - Sourcecode
Uploaded by chexov - 26/07/2010 22:02 - 80 Views
Source code
#include "Theme.h"Theme::Theme()
{fId = generateId();
fName = "Theme Id#" + QString::number(fId);
fType = lecture;
fHours = 2;
fIndex = -1;
QPair<quint32,quint32> rangeStud;
fRangeStud = rangeStud;
}Theme::Theme(const Theme &_other)
{*this = _other;
}Theme::Theme(quint32 _id, QString _name, int _index)
{fId = _id;
fName = _name;
fIndex = _index;
fType = lecture;
fHours = -1;
QPair<quint32,quint32> rangeStud;
fRangeStud = rangeStud;
}Theme & Theme::operator=(const Theme &_other)
{fId = _other.fId;
fName = _other.fName;
fType = _other.fType;
fHours = _other.fHours;
fIndex = _other.fIndex;
fRangeStud = _other.fRangeStud;
return *this;
}bool Theme::operator<(const Theme & _theme)
{if(fName < _theme.fName)
return true;
elsereturn false;
}Theme::~Theme()
{}quint32 Theme::Id()
{return fId;
}void Theme::setName(QString _name)
{fName = _name;
}bool Theme::setType(QString _type)
{bool flag = true;
if( (_type == "lecture") || (_type == "Лекционное") )
fType = lecture;
else if( (_type == "practical") || (_type == "Практическое") )
fType = practical;
else if( (_type == "lectureAndPractical") || (_type == "Лекционно-практическое") )
fType = lectureAndPractical;
elseflag=false;
return flag;
}QString Theme::getTypeString()
{if(fType == lecture)
return "Лекционное";
else if(fType == practical)
return "Практическое";
else if(fType == lectureAndPractical)
return "Лекционно-практическое";
elsereturn "";
}bool Theme::setHours(int _hours)
{bool flag = true;
if( (_hours >= 2) && (_hours%2 == 0) )
fHours = _hours;
elseflag = false;
return flag;
}bool Theme::setRangeStud(quint32 _min, quint32 _max)
{bool flag = true;
if(_min <= _max)
if( (_min >= 3) && (_min <= 15) && (_max >= 3) && (_max <= 15) )
{fRangeStud.first = _min;
fRangeStud.second = _max;
}elseflag = false;
elseflag = false;
return flag;
}QString Theme::getName()
{return fName;
}QString Theme::getType()
{if(fType == lecture)
return "lecture";
else if(fType == practical)
return "practical";
else if(fType == lectureAndPractical)
return "lectureAndPractical";
elsereturn "";
}quint32 Theme::getHours()
{return fHours;
}QPair<quint32,quint32> Theme::getRangeStud()
{return fRangeStud;
}quint32 Theme::generateId()
{quint32 newId = 1;
QList<quint32> idList = this->loadIds();
if(idList.size() != 0)
{foreach(quint32 id, idList)
if(id >= newId)
newId = id + 1;
}return newId;
}void Theme::addId(quint32 _id)
{QList<quint32> idList = loadIds();
idList << _id;
saveIds(idList);
}void Theme::saveIds(QList<quint32> _idList)
{QDomDocument doc("Ids");
QDomElement domElement = doc.createElement("Ids");
doc.appendChild(domElement);
foreach(quint32 id, _idList)
{QDomElement idElement = doc.createElement("Id");
QDomAttr domAttr = doc.createAttribute("value");
domAttr.setValue(QString::number(id));
idElement.setAttributeNode(domAttr);
domElement.appendChild(idElement);
}QFile file("XML Files/tempThemesIds.xml");
if(file.open(QIODevice::WriteOnly))
{QTextStream(&file) << doc.toString();
file.close();
}}QList<quint32> Theme::loadIds()
{QDomDocument domDoc;QList<quint32> idList;
QFile file("XML Files/tempThemesIds.xml");
//Если файл есть на диске и к нему имеется доступ - откроем для чтенияif(file.open(QIODevice::ReadOnly))
{//Если структура XML верная - считаем файлif(domDoc.setContent(&file))
{//Преобразуем QDomDocument к QDomElementQDomElement domElement = domDoc.documentElement();
collectIds(domElement, idList); //Сбор всех идентификаторов
}file.close();
}return idList;
}void Theme::collectIds(const QDomNode& _node, QList<quint32>& _idList)
{QDomNode domNode = _node.firstChild();
while(!domNode.isNull())
{if(domNode.isElement())
{QDomElement domElement = domNode.toElement();
if(!domElement.isNull())
{if(domElement.tagName() == "Id")
_idList << domElement.attribute("value").toUInt();
}}collectIds(domNode, _idList);
domNode = domNode.nextSibling();
}}void Theme::deleteId(quint32 _id)
{QList<quint32> idList = loadIds();
idList.removeAt(idList.indexOf(_id));
saveIds(idList);
}int Theme::getIndex()
{return fIndex;
}
