File #zchsyl18-3486 - CPP - Sourcecode

Uploaded by chexov - 26/07/2010 22:02 - 80 Views
Source code
  1. #include "Theme.h"
  2.  
  3. Theme::Theme()
  4. {
  5. 	fId = generateId();
  6. 	fName = "Theme Id#" + QString::number(fId);
  7. 	fType = lecture;
  8. 	fHours = 2;
  9. 	fIndex = -1;
  10. 	QPair<quint32,quint32> rangeStud;
  11. 	fRangeStud = rangeStud;
  12. }
  13.  
  14. Theme::Theme(const Theme &_other)
  15. {
  16. 	*this = _other;
  17. }
  18.  
  19. Theme::Theme(quint32 _id, QString _name, int _index)
  20. {
  21. 	fId = _id;
  22. 	fName = _name;
  23. 	fIndex = _index;
  24.  
  25. 	fType = lecture;
  26. 	fHours = -1;
  27. 	QPair<quint32,quint32> rangeStud;
  28. 	fRangeStud = rangeStud;
  29. }
  30.  
  31. Theme & Theme::operator=(const Theme &_other)
  32. {
  33. 	fId = _other.fId;
  34. 	fName = _other.fName;
  35. 	fType = _other.fType;
  36. 	fHours = _other.fHours;
  37. 	fIndex = _other.fIndex;
  38. 	fRangeStud = _other.fRangeStud;
  39.  
  40. 	return *this;
  41. }
  42.  
  43. bool Theme::operator<(const Theme & _theme)
  44. {
  45. 	if(fName < _theme.fName)
  46. 		return true;
  47. 	else
  48. 		return false;
  49. }
  50.  
  51. Theme::~Theme()
  52. {
  53.  
  54. }
  55.  
  56. quint32 Theme::Id()
  57. {
  58. 	return fId;
  59. }
  60.  
  61. void Theme::setName(QString _name)
  62. {
  63. 	fName = _name;
  64. }
  65.  
  66. bool Theme::setType(QString _type)
  67. {
  68. 	bool flag = true;
  69.  
  70. 	if(  (_type == "lecture") || (_type == "&#1051;&#1077;&#1082;&#1094;&#1080;&#1086;&#1085;&#1085;&#1086;&#1077;")  )
  71. 		fType = lecture;
  72. 	else if(  (_type == "practical") || (_type == "&#1055;&#1088;&#1072;&#1082;&#1090;&#1080;&#1095;&#1077;&#1089;&#1082;&#1086;&#1077;")  )
  73. 		fType = practical;
  74. 	else if(  (_type == "lectureAndPractical") || (_type == "&#1051;&#1077;&#1082;&#1094;&#1080;&#1086;&#1085;&#1085;&#1086;-&#1087;&#1088;&#1072;&#1082;&#1090;&#1080;&#1095;&#1077;&#1089;&#1082;&#1086;&#1077;")  )
  75. 		fType = lectureAndPractical;
  76. 	else
  77. 		flag=false;
  78.  
  79. 	return flag;
  80. }
  81.  
  82. QString Theme::getTypeString()
  83. {
  84. 	if(fType == lecture)
  85. 		return "&#1051;&#1077;&#1082;&#1094;&#1080;&#1086;&#1085;&#1085;&#1086;&#1077;";
  86. 	else if(fType == practical)
  87. 		return "&#1055;&#1088;&#1072;&#1082;&#1090;&#1080;&#1095;&#1077;&#1089;&#1082;&#1086;&#1077;";
  88. 	else if(fType == lectureAndPractical)
  89. 		return "&#1051;&#1077;&#1082;&#1094;&#1080;&#1086;&#1085;&#1085;&#1086;-&#1087;&#1088;&#1072;&#1082;&#1090;&#1080;&#1095;&#1077;&#1089;&#1082;&#1086;&#1077;";
  90. 	else
  91. 		return "";
  92. }
  93.  
  94. bool Theme::setHours(int _hours)
  95. {
  96. 	bool flag = true;
  97.  
  98. 	if(  (_hours >= 2) && (_hours%2 == 0)  )
  99. 		fHours = _hours;
  100. 	else
  101. 		flag = false;
  102.  
  103. 	return flag;
  104. }
  105.  
  106. bool Theme::setRangeStud(quint32 _min, quint32 _max)
  107. {
  108. 	bool flag = true;
  109.  
  110. 	if(_min <= _max)
  111. 		if(  (_min >= 3) && (_min <= 15) && (_max >= 3) && (_max <= 15)  )
  112. 		{
  113. 			fRangeStud.first = _min;
  114. 			fRangeStud.second = _max;
  115. 		}
  116. 		else
  117. 			flag = false;
  118. 	else
  119. 		flag = false;
  120.  
  121. 	return flag;
  122. }
  123.  
  124. QString Theme::getName()
  125. {
  126. 	return fName;
  127. }
  128.  
  129. QString Theme::getType()
  130. {
  131. 	if(fType == lecture)
  132. 		return "lecture";
  133. 	else if(fType == practical)
  134. 		return "practical";
  135. 	else if(fType == lectureAndPractical)
  136. 		return "lectureAndPractical";
  137. 	else
  138. 		return "";
  139. }
  140.  
  141. quint32 Theme::getHours()
  142. {
  143. 	return fHours;
  144. }
  145.  
  146. QPair<quint32,quint32> Theme::getRangeStud()
  147. {
  148. 	return fRangeStud;
  149. }
  150.  
  151. quint32 Theme::generateId()
  152. {
  153. 	quint32 newId = 1;
  154. 	QList<quint32> idList = this->loadIds();
  155.  
  156. 	if(idList.size() != 0)
  157. 	{
  158. 		foreach(quint32 id, idList)
  159. 			if(id >= newId)
  160. 				newId = id + 1;
  161. 	}
  162.  
  163. 	return newId;
  164. }
  165.  
  166. void Theme::addId(quint32 _id)
  167. {
  168. 	QList<quint32> idList = loadIds();
  169. 	idList << _id;
  170. 	saveIds(idList);
  171. }
  172.  
  173. void Theme::saveIds(QList<quint32> _idList)
  174. {
  175. 	QDomDocument doc("Ids");
  176. 	QDomElement domElement = doc.createElement("Ids");
  177. 	doc.appendChild(domElement);
  178.  
  179. 	foreach(quint32 id, _idList)
  180. 	{
  181. 		QDomElement idElement = doc.createElement("Id");
  182. 		QDomAttr domAttr = doc.createAttribute("value");
  183. 		domAttr.setValue(QString::number(id));
  184. 		idElement.setAttributeNode(domAttr);
  185. 		domElement.appendChild(idElement);
  186. 	}
  187.  
  188. 	QFile file("XML Files/tempThemesIds.xml");
  189. 	if(file.open(QIODevice::WriteOnly))
  190. 	{
  191. 		QTextStream(&file) << doc.toString();
  192. 		file.close();
  193. 	}
  194. }
  195.  
  196. QList<quint32> Theme::loadIds()
  197. {
  198. 	QDomDocument domDoc;
  199. 	QList<quint32> idList;
  200. 	QFile file("XML Files/tempThemesIds.xml");
  201.  
  202. 	//&#1045;&#1089;&#1083;&#1080; &#1092;&#1072;&#1081;&#1083; &#1077;&#1089;&#1090;&#1100; &#1085;&#1072; &#1076;&#1080;&#1089;&#1082;&#1077; &#1080; &#1082; &#1085;&#1077;&#1084;&#1091; &#1080;&#1084;&#1077;&#1077;&#1090;&#1089;&#1103; &#1076;&#1086;&#1089;&#1090;&#1091;&#1087; - &#1086;&#1090;&#1082;&#1088;&#1086;&#1077;&#1084; &#1076;&#1083;&#1103; &#1095;&#1090;&#1077;&#1085;&#1080;&#1103;
  203. 	if(file.open(QIODevice::ReadOnly))
  204. 	{
  205. 		//&#1045;&#1089;&#1083;&#1080; &#1089;&#1090;&#1088;&#1091;&#1082;&#1090;&#1091;&#1088;&#1072; XML &#1074;&#1077;&#1088;&#1085;&#1072;&#1103; - &#1089;&#1095;&#1080;&#1090;&#1072;&#1077;&#1084; &#1092;&#1072;&#1081;&#1083;
  206. 		if(domDoc.setContent(&file))
  207. 		{
  208. 			//&#1055;&#1088;&#1077;&#1086;&#1073;&#1088;&#1072;&#1079;&#1091;&#1077;&#1084; QDomDocument &#1082; QDomElement
  209. 			QDomElement domElement = domDoc.documentElement();
  210. 			collectIds(domElement, idList);	//&#1057;&#1073;&#1086;&#1088; &#1074;&#1089;&#1077;&#1093; &#1080;&#1076;&#1077;&#1085;&#1090;&#1080;&#1092;&#1080;&#1082;&#1072;&#1090;&#1086;&#1088;&#1086;&#1074;
  211. 		}
  212. 		file.close();
  213. 	}
  214.  
  215. 	return idList;
  216. }
  217.  
  218. void Theme::collectIds(const QDomNode& _node, QList<quint32>& _idList)
  219. {
  220. 	QDomNode domNode = _node.firstChild();
  221. 	while(!domNode.isNull())
  222. 	{
  223. 		if(domNode.isElement())
  224. 		{
  225. 			QDomElement domElement = domNode.toElement();
  226. 			if(!domElement.isNull())
  227. 			{
  228. 				if(domElement.tagName() == "Id")
  229. 					_idList << domElement.attribute("value").toUInt();
  230. 			}
  231. 		}
  232. 		collectIds(domNode, _idList);
  233. 		domNode = domNode.nextSibling();
  234. 	}
  235. }
  236.  
  237. void Theme::deleteId(quint32 _id)
  238. {
  239. 	QList<quint32> idList = loadIds();
  240. 	idList.removeAt(idList.indexOf(_id));
  241. 	saveIds(idList);
  242. }
  243.  
  244. int Theme::getIndex()
  245. {
  246. 	return fIndex;
  247. }