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

Michał Świtakowski swistakers-guest at alioth.debian.org
Sat Aug 8 20:35:48 UTC 2009


Author: swistakers-guest
Date: 2009-08-08 20:35:48 +0000 (Sat, 08 Aug 2009)
New Revision: 1399

Modified:
   tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
   tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
   tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp
   tux4kids-admin/trunk/libtux4kidsadmin/teacher.h
   tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.cpp
   tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.h
   tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.ui
   tux4kids-admin/trunk/tux4kids-admin/src/manageTeachersWidget.cpp
   tux4kids-admin/trunk/tux4kids-admin/src/manageTeachersWidget.h
   tux4kids-admin/trunk/tux4kids-admin/src/selectTeacherWidget.cpp
   tux4kids-admin/trunk/tux4kids-admin/src/selectTeacherWidget.h
   tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.cpp
   tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.h
Log:
adding, editing, deleting teachers works

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp	2009-08-08 20:35:48 UTC (rev 1399)
@@ -449,7 +449,9 @@
 
 	error = false;
 
-	insertTeacher.prepare("INSERT INTO teacher(first_name, last_name) VALUES(:first_name, :last_name);");
+	db.transaction();
+
+	insertTeacher.prepare("INSERT INTO teachers(first_name, last_name) VALUES(:first_name, :last_name);");
 	insertTeacher.bindValue(":first_name", newTeacher.firstName());
 	insertTeacher.bindValue(":last_name", newTeacher.lastName());
 	insertTeacher.exec();
@@ -475,6 +477,13 @@
 			return;
 		} else {
 			newTeacher.setId(getTeacherId.value(0).toInt());
+			getTeacherId.finish();
+			addTeacherClasses(newTeacher);
+			if (error) {
+				return;
+			}
+			db.commit();
+
 			Q_Q(SchoolDatabase);
 			emit q->teacherAdded(newTeacher);
 		}
@@ -493,6 +502,8 @@
 
 	error = false;
 
+	db.transaction();
+
 	updateTeacher.prepare("UPDATE teachers SET first_name = :first_name, last_name = :last_name WHERE id = :id;");
 	updateTeacher.bindValue(":first_name", updatedTeacher.firstName());
 	updateTeacher.bindValue(":last_name", updatedTeacher.lastName());
@@ -505,6 +516,18 @@
 		return;
 	}
 
+	deleteTeacherClasses(updatedTeacher.id());
+	if (error) {
+		return;
+	}
+
+	addTeacherClasses(updatedTeacher);
+	if (error) {
+		return;
+	}
+
+	db.commit();
+
 	Q_Q(SchoolDatabase);
 	emit q->teacherUpdated(updatedTeacher);
 }
@@ -535,6 +558,34 @@
 	emit q->teacherDeleted(deletedTeacher);
 }
 
+void SchoolDatabasePrivate::deleteTeacherClasses(int teacherId)
+{
+	QSqlQuery deleteClasses;
+	deleteClasses.prepare("DELETE FROM class_teachers WHERE id_teacher = :id_teacher;");
+	deleteClasses.bindValue(":id_teacher", teacherId);
+	deleteClasses.exec();
+	if (!deleteClasses.isActive()) {
+		error = true;
+		lastError = deleteClasses.lastError().text();
+	}
+}
+
+void SchoolDatabasePrivate::addTeacherClasses(Teacher &teacher)
+{
+	foreach(Class teacherClass, *teacher.classes()) {
+		QSqlQuery addTeacherClass;
+		addTeacherClass.prepare("INSERT INTO class_teachers(id_class, id_teacher) VALUES (:id_class, :id_teacher);");
+		addTeacherClass.bindValue(":id_class", teacherClass.id());
+		addTeacherClass.bindValue(":id_teacher", teacher.id());
+		addTeacherClass.exec();
+		if (!addTeacherClass.isActive()) {
+			error = true;
+			lastError = addTeacherClass.lastError().text();
+			break;
+		}
+	}
+}
+
 QList<Class> SchoolDatabasePrivate::classList() const
 {
 	QList<Class> result;
@@ -915,6 +966,7 @@
 		if (result) {
 			d->createTables();
 			d->createTriggers();
+			qDebug() << d->lastError;
 		}
 		return result;
 	}

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h	2009-08-08 20:35:48 UTC (rev 1399)
@@ -28,8 +28,10 @@
 
 	bool addClassTeachers(Class &newClass);
 	bool addClassStudents(Class &newClass);
+	void addTeacherClasses(Teacher &teacher);
 	bool deleteClassStudents(int classId);
 	bool deleteClassTeachers(int classId);
+	void deleteTeacherClasses(int teacherId);
 
 	QList<Class> classList() const;
 	QList<Class> classListNoJoin() const;

Modified: tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp	2009-08-08 20:35:48 UTC (rev 1399)
@@ -14,12 +14,19 @@
 
 /***************************** Teacher *********************/
 
+Teacher::Teacher()
+		: d_ptr(new TeacherPrivate())
+{
+	d_ptr->q_ptr = this;
+}
+
 Teacher::Teacher(QString firstName, QString lastName)
 		: d_ptr(new TeacherPrivate())
 {
 	Q_D(Teacher);
 	d->firstName = firstName;
 	d->lastName = lastName;
+	d_ptr->q_ptr = this;
 }
 
 Teacher::Teacher(TeacherPrivate &dd)

Modified: tux4kids-admin/trunk/libtux4kidsadmin/teacher.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/teacher.h	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/libtux4kidsadmin/teacher.h	2009-08-08 20:35:48 UTC (rev 1399)
@@ -13,6 +13,7 @@
 class LIBTUX4KIDSADMIN_SHARED_EXPORT Teacher
 {
 public:
+	Teacher();
 	Teacher(QString firstName, QString lastName);
 	Teacher(const Teacher &other);
 	~Teacher();

Modified: tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.cpp	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.cpp	2009-08-08 20:35:48 UTC (rev 1399)
@@ -1,14 +1,22 @@
 #include "editTeacherDialog.h"
 #include "ui_editTeacherDialog.h"
+#include "selectClassWidget.h"
 
 #include <QPushButton>
 
-EditTeacherDialog::EditTeacherDialog(QWidget *parent) :
+EditTeacherDialog::EditTeacherDialog(SchoolDatabase *schoolDatabase, QWidget *parent) :
 		QDialog(parent),
-		m_ui(new Ui::EditTeacherDialog)
+		m_ui(new Ui::EditTeacherDialog),
+		m_schoolDatabase(schoolDatabase)
 {
 	m_ui->setupUi(this);
 
+	m_selectClassWidget = new SelectClassWidget();
+	m_selectClassWidget->setClassTableModel(&m_classTableModel);
+	m_ui->verticalLayout->insertWidget(1, m_selectClassWidget);
+
+	m_classTableModel.setSchoolDatabase(m_schoolDatabase);
+
 	connect(m_ui->firstNameEdit, SIGNAL(textEdited(QString)), this, SLOT(validate()));
 	connect(m_ui->lastNameEdit, SIGNAL(textEdited(QString)), this, SLOT(validate()));
 	connect(m_ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(accept()));
@@ -35,3 +43,31 @@
 		m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
 	}
 }
+
+Teacher EditTeacherDialog::teacher()
+{
+	editedTeacher.setFirstName(m_ui->firstNameEdit->text());
+	editedTeacher.setLastName(m_ui->lastNameEdit->text());
+	*editedTeacher.classes() = m_classTableModel.selectedClasses();
+	return editedTeacher;
+}
+
+void EditTeacherDialog::setTeacher(Teacher &teacher)
+{
+	editedTeacher = teacher;
+	m_ui->firstNameEdit->setText(editedTeacher.firstName());
+	m_ui->lastNameEdit->setText(editedTeacher.lastName());
+	m_classTableModel.setSelectedClasses(*editedTeacher.classes());
+
+	validate();
+}
+
+void EditTeacherDialog::clear()
+{
+	m_ui->firstNameEdit->clear();
+	m_ui->lastNameEdit->clear();
+	m_classTableModel.clearSelection();
+
+	validate();
+}
+

Modified: tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.h
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.h	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.h	2009-08-08 20:35:48 UTC (rev 1399)
@@ -2,22 +2,37 @@
 #define EDITTEACHERDIALOG_H
 
 #include <QtGui/QDialog>
+#include <QPointer>
 
+#include "schoolDatabase.h"
+#include "classTableModel.h"
+#include "teacher.h"
+
 namespace Ui {
 	class EditTeacherDialog;
 }
 
+class SelectClassWidget;
+
 class EditTeacherDialog : public QDialog
 {
 	Q_OBJECT
 public:
-	EditTeacherDialog(QWidget *parent = 0);
+	EditTeacherDialog(SchoolDatabase *schoolDatabase, QWidget *parent = 0);
 	~EditTeacherDialog();
 
+	Teacher teacher();
+	void setTeacher(Teacher &teacher);
+	void clear();
+
 private:
 	Ui::EditTeacherDialog *m_ui;
 
 	bool isValid() const;
+	QPointer<SchoolDatabase> m_schoolDatabase;
+	QPointer<SelectClassWidget> m_selectClassWidget;
+	ClassTableModel m_classTableModel;
+	Teacher editedTeacher;
 
 private slots:
 	void validate();

Modified: tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.ui
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.ui	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/tux4kids-admin/src/editTeacherDialog.ui	2009-08-08 20:35:48 UTC (rev 1399)
@@ -13,51 +13,43 @@
   <property name="windowTitle">
    <string>Dialog</string>
   </property>
-  <widget class="QWidget" name="formLayoutWidget">
-   <property name="geometry">
-    <rect>
-     <x>130</x>
-     <y>30</y>
-     <width>160</width>
-     <height>80</height>
-    </rect>
-   </property>
-   <layout class="QFormLayout" name="formLayout">
-    <item row="0" column="0">
-     <widget class="QLabel" name="label">
-      <property name="text">
-       <string>Firstname:</string>
-      </property>
-     </widget>
-    </item>
-    <item row="1" column="0">
-     <widget class="QLabel" name="label_2">
-      <property name="text">
-       <string>Last name:</string>
-      </property>
-     </widget>
-    </item>
-    <item row="0" column="1">
-     <widget class="QLineEdit" name="firstNameEdit"/>
-    </item>
-    <item row="1" column="1">
-     <widget class="QLineEdit" name="lastNameEdit"/>
-    </item>
-   </layout>
-  </widget>
-  <widget class="QDialogButtonBox" name="buttonBox">
-   <property name="geometry">
-    <rect>
-     <x>200</x>
-     <y>220</y>
-     <width>163</width>
-     <height>26</height>
-    </rect>
-   </property>
-   <property name="standardButtons">
-    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
-   </property>
-  </widget>
+  <layout class="QVBoxLayout" name="verticalLayout_2">
+   <item>
+    <layout class="QVBoxLayout" name="verticalLayout">
+     <item>
+      <layout class="QFormLayout" name="formLayout">
+       <item row="0" column="0">
+        <widget class="QLabel" name="label">
+         <property name="text">
+          <string>Firstname:</string>
+         </property>
+        </widget>
+       </item>
+       <item row="1" column="0">
+        <widget class="QLabel" name="label_2">
+         <property name="text">
+          <string>Last name:</string>
+         </property>
+        </widget>
+       </item>
+       <item row="0" column="1">
+        <widget class="QLineEdit" name="firstNameEdit"/>
+       </item>
+       <item row="1" column="1">
+        <widget class="QLineEdit" name="lastNameEdit"/>
+       </item>
+      </layout>
+     </item>
+    </layout>
+   </item>
+   <item>
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
  </widget>
  <resources/>
  <connections/>

Modified: tux4kids-admin/trunk/tux4kids-admin/src/manageTeachersWidget.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/manageTeachersWidget.cpp	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/tux4kids-admin/src/manageTeachersWidget.cpp	2009-08-08 20:35:48 UTC (rev 1399)
@@ -3,6 +3,7 @@
 #include "mainController.h"
 #include "editTeacherDialog.h"
 #include "selectTeacherWidget.h"
+#include "schoolData.h"
 
 #include <QDebug>
 #include <QTableView>
@@ -10,7 +11,8 @@
 ManageTeachersWidget::ManageTeachersWidget(MainController *mainController, QWidget *parent) :
 		QWidget(parent),
 		m_ui(new Ui::ManageTeachersWidget),
-		m_mainController(mainController)
+		m_mainController(mainController),
+		m_schoolDatabase(mainController->schoolData()->schoolDatabase())
 
 {
 	m_ui->setupUi(this);
@@ -37,31 +39,40 @@
 void ManageTeachersWidget::addClicked()
 {
 	if (m_addTeacherDialog == 0) {
-		m_addTeacherDialog = new EditTeacherDialog(this);
+		m_addTeacherDialog = new EditTeacherDialog(m_schoolDatabase, this);
 		connect(m_addTeacherDialog, SIGNAL(accepted()), this, SLOT(addAccepted()));
 		connect(m_addTeacherDialog, SIGNAL(rejected()), this, SLOT(addRejected()));
 	}
+	if (m_addTeacherDialog->isHidden()) {
+		m_addTeacherDialog->clear();
+	}
 	m_addTeacherDialog->showNormal();
 }
 
 void ManageTeachersWidget::editClicked()
 {
 	if (m_editTeacherDialog == 0) {
-		m_editTeacherDialog = new EditTeacherDialog(this);
+		m_editTeacherDialog = new EditTeacherDialog(m_schoolDatabase, this);
 		connect(m_editTeacherDialog, SIGNAL(accepted()), this, SLOT(editAccepted()));
 		connect(m_editTeacherDialog, SIGNAL(rejected()), this, SLOT(editRejected()));
 	}
+
+	QModelIndex teacherIndex = m_selectTeacherWidget->selectedTeacherIndex();
+	m_editTeacherDialog->setTeacher(m_mainController->teacherTableModel()->at(teacherIndex.row()));
 	m_editTeacherDialog->showNormal();
 }
 
 void ManageTeachersWidget::deleteClicked()
 {
-
+	QModelIndex teacherIndex = m_selectTeacherWidget->selectedTeacherIndex();
+	m_schoolDatabase->deleteTeacher(m_mainController->teacherTableModel()->at(teacherIndex.row()));
 }
 
 void ManageTeachersWidget::addAccepted()
 {
 	qDebug() << "add accepted";
+	Teacher addedTeacher = m_addTeacherDialog->teacher();
+	m_schoolDatabase->addTeacher(addedTeacher);
 }
 
 void ManageTeachersWidget::addRejected()
@@ -72,6 +83,8 @@
 void ManageTeachersWidget::editAccepted()
 {
 	qDebug() << "edit accepted";
+	Teacher updatedTeacher = m_editTeacherDialog->teacher();
+	m_schoolDatabase->updateTeacher(updatedTeacher);
 }
 
 void ManageTeachersWidget::editRejected()

Modified: tux4kids-admin/trunk/tux4kids-admin/src/manageTeachersWidget.h
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/manageTeachersWidget.h	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/tux4kids-admin/src/manageTeachersWidget.h	2009-08-08 20:35:48 UTC (rev 1399)
@@ -13,6 +13,7 @@
 class MainController;
 class EditTeacherDialog;
 class SelectTeacherWidget;
+class SchoolDatabase;
 
 class ManageTeachersWidget : public QWidget
 {
@@ -27,6 +28,7 @@
 	QPointer<EditTeacherDialog> m_addTeacherDialog;
 	QPointer<EditTeacherDialog> m_editTeacherDialog;
 	QPointer<SelectTeacherWidget> m_selectTeacherWidget;
+	QPointer<SchoolDatabase> m_schoolDatabase;
 
 	void setEditButtonsEnabled(bool enable);
 

Modified: tux4kids-admin/trunk/tux4kids-admin/src/selectTeacherWidget.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/selectTeacherWidget.cpp	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/tux4kids-admin/src/selectTeacherWidget.cpp	2009-08-08 20:35:48 UTC (rev 1399)
@@ -38,3 +38,9 @@
 	m_teacherTableProxyModel.setFilterRegExp(m_ui->searchEdit->text());
 }
 
+QModelIndex SelectTeacherWidget::selectedTeacherIndex()
+{
+	QModelIndex proxyIndex = m_ui->teacherTable->selectionModel()->selectedIndexes().first();
+	return m_teacherTableProxyModel.mapToSource(proxyIndex);
+}
+

Modified: tux4kids-admin/trunk/tux4kids-admin/src/selectTeacherWidget.h
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/selectTeacherWidget.h	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/tux4kids-admin/src/selectTeacherWidget.h	2009-08-08 20:35:48 UTC (rev 1399)
@@ -21,6 +21,7 @@
 	~SelectTeacherWidget();
 	void setTeacherTableModel(TeacherTableModel *teacherTableModel);
 	QTableView *teacherTable();
+	QModelIndex selectedTeacherIndex();
 
 private:
 	Ui::SelectTeacherWidget *m_ui;

Modified: tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.cpp	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.cpp	2009-08-08 20:35:48 UTC (rev 1399)
@@ -199,3 +199,8 @@
 	}
 }
 
+Teacher &TeacherTableModel::at(int i)
+{
+	return m_teachers[i];
+}
+

Modified: tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.h
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.h	2009-08-08 15:22:28 UTC (rev 1398)
+++ tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.h	2009-08-08 20:35:48 UTC (rev 1399)
@@ -24,6 +24,7 @@
 	QList<Teacher> selectedTeachers() const;
 	void setSelectedTeachers(const QList<Teacher> &selectedTeachers);
 	void clearSelection();
+	Teacher &at(int i);
 
 	enum TeacherField {
 		TeacherSelected = 0,




More information about the Tux4kids-commits mailing list