[Tux4kids-commits] r1398 - in tux4kids-admin/trunk: libtux4kidsadmin tux4kids-admin/src

Michał Świtakowski swistakers-guest at alioth.debian.org
Sat Aug 8 15:22:28 UTC 2009


Author: swistakers-guest
Date: 2009-08-08 15:22:28 +0000 (Sat, 08 Aug 2009)
New Revision: 1398

Modified:
   tux4kids-admin/trunk/libtux4kidsadmin/class.cpp
   tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
   tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
   tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp
   tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.cpp
   tux4kids-admin/trunk/tux4kids-admin/src/editClassDialog.cpp
   tux4kids-admin/trunk/tux4kids-admin/src/editClassDialog.h
   tux4kids-admin/trunk/tux4kids-admin/src/manageClassesWidget.cpp
Log:
cleanup and fixes in database, editing and deleting classes works

Modified: tux4kids-admin/trunk/libtux4kidsadmin/class.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/class.cpp	2009-08-08 14:11:14 UTC (rev 1397)
+++ tux4kids-admin/trunk/libtux4kidsadmin/class.cpp	2009-08-08 15:22:28 UTC (rev 1398)
@@ -28,12 +28,14 @@
 Class::Class(ClassPrivate &dd)
 		: d_ptr(&dd)
 {
+	d_ptr->q_ptr = this;
 }
 
 Class::Class(const Class& other)
 		: d_ptr(new ClassPrivate())
 {
 	*d_ptr = *other.d_ptr;
+	d_ptr->q_ptr = this;
 }
 
 Class::~Class()
@@ -93,18 +95,15 @@
 Class &Class::operator=(const Class &other)
 {
 	if (this != &other) {
-		ClassPrivate *tmp = new ClassPrivate();
-		*tmp = *d_ptr;
-		tmp->q_ptr = this;
-		delete d_ptr;
-		d_ptr = tmp;
+		*d_ptr = *other.d_ptr;
+		d_ptr->q_ptr = this;
 	}
-
 	return *this;
 }
 
 bool Class::operator==(const Class &other) const
 {
+
 	Q_D(const Class);
 
 	if (d->id >= 0 && other.d_ptr->id >= 0) {

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp	2009-08-08 14:11:14 UTC (rev 1397)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp	2009-08-08 15:22:28 UTC (rev 1398)
@@ -382,6 +382,7 @@
 
 	error = false;
 
+	db.transaction();
 	updateClass.prepare("UPDATE classes SET name = :name WHERE id = :id;");
 	updateClass.bindValue(":name", updatedClass.name());
 	updateClass.bindValue(":id", updatedClass.id());
@@ -393,12 +394,26 @@
 		return;
 	}
 
+	if (!deleteClassStudents(updatedClass.id()))
+		return;
+
+	if (!deleteClassTeachers(updatedClass.id()))
+		return;
+
+	if (!addClassStudents(updatedClass))
+		return;
+
+	if (!addClassTeachers(updatedClass))
+		return;
+	db.commit();
+
 	Q_Q(SchoolDatabase);
 	emit q->classUpdated(updatedClass);
 }
 
 void SchoolDatabasePrivate::deleteClass(Class &deletedClass)
 {
+
 	if (!db.isOpen()) {
 		error = true;
 		lastError = QObject::tr("Database is not open");
@@ -531,18 +546,37 @@
 	}
 
 	result = classListNoJoin();
-	QList<Teacher> teachers = teacherListNoJoin();
+	joinStudentsToClass(result);
 	if (error) {
 		return QList<Class>();
 	}
 
+	joinTeachersToClass(result);
+	if (error) {
+		return QList<Class>();
+	}
+
+	return result;
+}
+
+void SchoolDatabasePrivate::joinTeachersToClass(QList<Class> &classes) const
+{
+	if (classes.empty()) {
+		return;
+	}
+
+	QList<Teacher> teachers = teacherListNoJoin();
+	if (error || teachers.empty()) {
+		return;
+	}
+
 	QSqlQuery classTeachers;
 	classTeachers.prepare("SELECT id_class, id_teacher FROM class_teachers ORDER BY id_class ASC;");
 	classTeachers.exec();
 	if (!classTeachers.isActive()) {
 		error = true;
 		lastError = classTeachers.lastError().text();
-		return QList<Class>();
+		return;
 	}
 
 	QSqlRecord classTeachersRecord = classTeachers.record();
@@ -552,31 +586,39 @@
 		int classId = classTeachers.value(classTeachersRecord.indexOf("id_class")).toInt();
 		if (oldClassId != classId) {
 			oldClassId = classId;
-			while(result.at(classIndex).id() != classId
-			      && classIndex < result.size() - 1) {
+			while(classes.at(classIndex).id() != classId
+			      && classIndex < classes.size() - 1) {
 				++classIndex;
 			}
-			if (result.at(classIndex).id() != classId) {
+			if (classes.at(classIndex).id() != classId) {
 				break;
 			}
 		}
+
 		int teacherId = classTeachers.value(classTeachersRecord.indexOf("id_teacher")).toInt();
 		int teacherIndex = 0;
-		while (teachers[teacherIndex].id() != teacherId) {
+		while (teachers.at(teacherIndex).id() != teacherId && teacherIndex < teachers.size() - 1) {
 			++teacherIndex;
 		}
 		if (teachers[teacherIndex].id() == teacherId) {
-			result[classIndex].teachers()->append(teachers.at(teacherIndex));
+			classes[classIndex].teachers()->append(teachers.at(teacherIndex));
 		}
 	}
+}
 
+void SchoolDatabasePrivate::joinStudentsToClass(QList<Class> &classes) const
+{
+	if (classes.empty()) {
+		return;
+	}
+
 	QSqlQuery students;
 	students.prepare("SELECT id, profile_name FROM students;");
 	students.exec();
 	if (!students.isActive()) {
 		error = true;
 		lastError = students.lastError().text();
-		return QList<Class>();
+		return;
 	}
 
 	QSqlQuery classStudents;
@@ -585,22 +627,22 @@
 	if (!classStudents.isActive()) {
 		error = true;
 		lastError = classStudents.lastError().text();
-		return QList<Class>();
+		return;
 	}
 
 	QSqlRecord classStudentsRecord = classStudents.record();
 	QSqlRecord studentsRecord = students.record();
-	oldClassId = -1;
-	classIndex = 0;
+	int oldClassId = -1;
+	int classIndex = 0;
 	while(classStudents.next()) {
 		int classId = classStudents.value(classStudentsRecord.indexOf("id_class")).toInt();
 		if (oldClassId != classId) {
 			oldClassId = classId;
-			while(result.at(classIndex).id() != classId
-			      && classIndex < result.size() - 1) {
+			while(classes.at(classIndex).id() != classId
+			      && classIndex < classes.size() - 1) {
 				++classIndex;
 			}
-			if (result.at(classIndex).id() != classId) {
+			if (classes.at(classIndex).id() != classId) {
 				break;
 			}
 		}
@@ -610,14 +652,13 @@
 		while (students.next()) {
 			if (students.value(studentsRecord.indexOf("id")).toInt()
 				== studentId) {
-				result[classIndex].students()->append(students.value(studentsRecord.indexOf("profile_name")).toString());
+				classes[classIndex].students()->append(students.value(studentsRecord.indexOf("profile_name")).toString());
 			}
 		}
 	}
-
-	return result;
 }
 
+
 QList<Class> SchoolDatabasePrivate::classListNoJoin() const
 {
 	QList<Class> result;
@@ -658,18 +699,32 @@
 	}
 
 	result = teacherListNoJoin();
-	QList<Class> classes = classListNoJoin();
+	joinClassesToTeachers(result);
 	if (error) {
 		return QList<Teacher>();
 	}
 
+	return result;
+}
+
+void SchoolDatabasePrivate::joinClassesToTeachers(QList<Teacher> &teachers) const
+{
+	if (teachers.empty()) {
+		return;
+	}
+
+	QList<Class> classes = classListNoJoin();
+	if (error || classes.empty()) {
+		return;
+	}
+
 	QSqlQuery classTeachers;
 	classTeachers.prepare("SELECT id_class, id_teacher FROM class_teachers ORDER BY id_teacher ASC;");
 	classTeachers.exec();
 	if (!classTeachers.isActive()) {
 		error = true;
 		lastError = classTeachers.lastError().text();
-		return QList<Teacher>();
+		return;
 	}
 
 	QSqlRecord classTeachersRecord = classTeachers.record();
@@ -680,25 +735,23 @@
 		int teacherId = classTeachers.value(classTeachersRecord.indexOf("id_teacher")).toInt();
 		if (oldTeacherId != teacherId) {
 			oldTeacherId = teacherId;
-			while(result.at(teacherIndex).id() != teacherId
-			      && teacherIndex < result.size() - 1) {
+			while(teachers.at(teacherIndex).id() != teacherId
+			      && teacherIndex < teachers.size() - 1) {
 				++teacherIndex;
 			}
-			if (result.at(teacherIndex).id() != teacherId) {
+			if (teachers.at(teacherIndex).id() != teacherId) {
 				break;
 			}
 		}
 		int classId = classTeachers.value(classTeachersRecord.indexOf("id_class")).toInt();
 		int classIndex = 0;
-		while (classes[classIndex].id() != classId) {
+		while (classes[classIndex].id() != classId && classIndex < classes.size() - 1) {
 			++classIndex;
 		}
 		if (classes[classIndex].id() == classId) {
-			result[teacherIndex].classes()->append(classes.at(classIndex));
+			teachers[teacherIndex].classes()->append(classes.at(classIndex));
 		}
 	}
-
-	return result;
 }
 
 QList<Teacher> SchoolDatabasePrivate::teacherListNoJoin() const
@@ -746,7 +799,7 @@
 	foreach(QString profileName, databaseStudents) {
 		toAddStudents.removeOne(profileName);
 	}
-	qDebug() << toAddStudents;
+
 	QStringList toDeleteStudents = databaseStudents;
 	foreach(QString profileName, existingStudents) {
 		toDeleteStudents.removeOne(profileName);

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h	2009-08-08 14:11:14 UTC (rev 1397)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h	2009-08-08 15:22:28 UTC (rev 1398)
@@ -38,6 +38,9 @@
 	void synchronizeStudents(const QList< QPointer<StudentDir> > &studentList);
 	QStringList studentList() const;
 	QStringList studentListNoJoin() const;
+	void joinStudentsToClass(QList<Class> &classes) const;
+	void joinTeachersToClass(QList<Class> &classes) const;
+	void joinClassesToTeachers(QList<Teacher> &teachers) const;
 
 	SchoolDatabase *q_ptr;
 

Modified: tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp	2009-08-08 14:11:14 UTC (rev 1397)
+++ tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp	2009-08-08 15:22:28 UTC (rev 1398)
@@ -25,12 +25,14 @@
 Teacher::Teacher(TeacherPrivate &dd)
 		: d_ptr(&dd)
 {
+	d_ptr->q_ptr = this;
 }
 
 Teacher::Teacher(const Teacher& other)
 		: d_ptr(new TeacherPrivate())
 {
 	*d_ptr = *other.d_ptr;
+	d_ptr->q_ptr = this;
 }
 
 Teacher::~Teacher()
@@ -78,13 +80,9 @@
 Teacher &Teacher::operator=(const Teacher &other)
 {
 	if (this != &other) {
-		TeacherPrivate *tmp = new TeacherPrivate();
-		*tmp = *d_ptr;
-		tmp->q_ptr = this;
-		delete d_ptr;
-		d_ptr = tmp;
+		*d_ptr = *other.d_ptr;
+		d_ptr->q_ptr = this;
 	}
-
 	return *this;
 }
 

Modified: tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.cpp	2009-08-08 14:11:14 UTC (rev 1397)
+++ tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.cpp	2009-08-08 15:22:28 UTC (rev 1398)
@@ -87,14 +87,10 @@
 	m_schoolDatabase = schoolDatabase;
 	m_classes = m_schoolDatabase->classList();
 	m_classesSelection.clear();
-	for (int i = 0; i < m_classes.size(); i++) {
+	for (int i = 0; i < m_classes.size(); ++i) {
 		m_classesSelection.append(false);
 	}
 
-	void classAdded(const Class &newClass);
-	void classUpdated(const Class &updatedClass);
-	void classDeleted(const Class &deletedClass);
-
 	connect(m_schoolDatabase, SIGNAL(classAdded(Class &)), this, SLOT(addClass(Class &)));
 	connect(m_schoolDatabase, SIGNAL(classUpdated(Class &)), this, SLOT(updateClass(Class &)));
 	connect(m_schoolDatabase, SIGNAL(classDeleted(Class &)), this, SLOT(deleteClass(Class &)));

Modified: tux4kids-admin/trunk/tux4kids-admin/src/editClassDialog.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/editClassDialog.cpp	2009-08-08 14:11:14 UTC (rev 1397)
+++ tux4kids-admin/trunk/tux4kids-admin/src/editClassDialog.cpp	2009-08-08 15:22:28 UTC (rev 1398)
@@ -52,28 +52,29 @@
 	}
 }
 
-Class EditClassDialog::getClass() const
+Class EditClassDialog::getClass()
 {
-	Class result;
+	editedClass.setName(m_ui->nameEdit->text());
+	*editedClass.students() = m_studentTableModel.selectedStudentsDirNames();
+	*editedClass.teachers() = m_teacherTableModel.selectedTeachers();
 
-	result.setName(m_ui->nameEdit->text());
-	*result.students() = m_studentTableModel.selectedStudentsDirNames();
-	*result.teachers() = m_teacherTableModel.selectedTeachers();
-
-	return result;
+	return editedClass;
 }
 
 void EditClassDialog::setClass(Class &newClass)
 {
-	qDebug() << "setting" << newClass.name();
-	foreach(Teacher t, *newClass.teachers()) {
-		qDebug() << t.firstName();
-	}
-	foreach(QString s, *newClass.students()) {
-		qDebug() << s;
-	}
-	m_ui->nameEdit->setText(newClass.name());
-	m_teacherTableModel.setSelectedTeachers(*newClass.teachers());
-	m_studentTableModel.setSelectedStudents(*newClass.students());
+	editedClass = newClass;
+	m_ui->nameEdit->setText(editedClass.name());
+	m_teacherTableModel.setSelectedTeachers(*editedClass.teachers());
+	m_studentTableModel.setSelectedStudents(*editedClass.students());
+	validate();
 }
 
+void EditClassDialog::clear()
+{
+	m_ui->nameEdit->clear();
+	m_teacherTableModel.clearSelection();
+	m_studentTableModel.clearSelection();
+	validate();
+}
+

Modified: tux4kids-admin/trunk/tux4kids-admin/src/editClassDialog.h
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/editClassDialog.h	2009-08-08 14:11:14 UTC (rev 1397)
+++ tux4kids-admin/trunk/tux4kids-admin/src/editClassDialog.h	2009-08-08 15:22:28 UTC (rev 1398)
@@ -23,8 +23,9 @@
 	EditClassDialog(SchoolData *schoolData, QWidget *parent = 0);
 	~EditClassDialog();
 
-	Class getClass() const;
+	Class getClass();
 	void setClass(Class & newClass);
+	void clear();
 
 private:
 	Ui::EditClassDialog *m_ui;
@@ -33,6 +34,7 @@
 	SchoolData *m_schoolData;
 	TeacherTableModel m_teacherTableModel;
 	StudentTableModel m_studentTableModel;
+	Class editedClass;
 
 	bool isValid() const;
 

Modified: tux4kids-admin/trunk/tux4kids-admin/src/manageClassesWidget.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/manageClassesWidget.cpp	2009-08-08 14:11:14 UTC (rev 1397)
+++ tux4kids-admin/trunk/tux4kids-admin/src/manageClassesWidget.cpp	2009-08-08 15:22:28 UTC (rev 1398)
@@ -24,6 +24,7 @@
 
 	connect(m_ui->addClassButton, SIGNAL(clicked()), this, SLOT(addClicked()));
 	connect(m_ui->editClassButton, SIGNAL(clicked()), this, SLOT(editClicked()));
+	connect(m_ui->deleteClassButton, SIGNAL(clicked()), this, SLOT(deleteClicked()));
 	connect(m_selectClassWidget->classTable()->selectionModel(),
 		SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
 		this, SLOT(setEditButtons()));
@@ -43,6 +44,9 @@
 		connect(m_addClassDialog, SIGNAL(accepted()), this, SLOT(addAccepted()));
 		connect(m_addClassDialog, SIGNAL(rejected()), this, SLOT(addRejected()));
 	}
+	if (m_addClassDialog->isHidden()) {
+		m_addClassDialog->clear();
+	}
 	m_addClassDialog->showNormal();
 }
 
@@ -61,7 +65,8 @@
 
 void ManageClassesWidget::deleteClicked()
 {
-
+	QModelIndex classIndex = m_selectClassWidget->selectedClassIndex();
+	m_schoolDatabase->deleteClass(m_mainController->classTableModel()->at(classIndex.row()));
 }
 
 void ManageClassesWidget::addAccepted()
@@ -82,6 +87,8 @@
 void ManageClassesWidget::editAccepted()
 {
 	qDebug() << "edit accepted";
+	Class editedClass = m_editClassDialog->getClass();
+	m_schoolDatabase->updateClass(editedClass);
 }
 
 void ManageClassesWidget::editRejected()




More information about the Tux4kids-commits mailing list