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

Michał Świtakowski swistakers-guest at alioth.debian.org
Fri Jul 31 11:15:33 UTC 2009


Author: swistakers-guest
Date: 2009-07-31 11:15:32 +0000 (Fri, 31 Jul 2009)
New Revision: 1302

Modified:
   tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp
   tux4kids-admin/trunk/libtux4kidsadmin/teacher.h
   tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.cpp
   tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.h
   tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.cpp
   tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.h
Log:
models support selection

Modified: tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp	2009-07-31 04:02:06 UTC (rev 1301)
+++ tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp	2009-07-31 11:15:32 UTC (rev 1302)
@@ -82,3 +82,16 @@
 	return *this;
 }
 
+bool Teacher::operator==(const Teacher &other) const
+{
+	Q_D(const Teacher);
+
+	if (d->id >= 0 && other.d_ptr->id >= 0) {
+		return d->id == other.d_ptr->id;
+	} else {
+		return d->firstName == other.d_ptr->firstName
+			&& d->lastName == other.d_ptr->lastName;
+	}
+}
+
+

Modified: tux4kids-admin/trunk/libtux4kidsadmin/teacher.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/teacher.h	2009-07-31 04:02:06 UTC (rev 1301)
+++ tux4kids-admin/trunk/libtux4kidsadmin/teacher.h	2009-07-31 11:15:32 UTC (rev 1302)
@@ -21,6 +21,7 @@
 	void setLastName(QString lastName);
 	void setId(int id);
 	Teacher &operator=(const Teacher &other);
+	bool operator==(const Teacher &other) const;
 
 protected:
 

Modified: tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.cpp	2009-07-31 04:02:06 UTC (rev 1301)
+++ tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.cpp	2009-07-31 11:15:32 UTC (rev 1302)
@@ -6,6 +6,16 @@
 {
 }
 
+int ClassTableModel::columnCount(const QModelIndex &parent) const
+{
+	return 2;
+}
+
+int ClassTableModel::rowCount(const QModelIndex &parent) const
+{
+	return m_classes.size();
+}
+
 QVariant ClassTableModel::data(const QModelIndex &index, int role) const
 {
 	if (!index.isValid())
@@ -22,23 +32,45 @@
 		case ClassName:
 			return m_classes.at(index.row()).name();
 		}
+	} else if (role == Qt::CheckStateRole) {
+		if (index.column() == ClassSelected) {
+			if (m_classesSelection[index.row()]) {
+				return Qt::Checked;
+			} else {
+				return Qt::Unchecked;
+			}
+		}
 	}
-
 	return QVariant();
 }
 
-int ClassTableModel::columnCount(const QModelIndex &parent) const
+Qt::ItemFlags ClassTableModel::flags(const QModelIndex &index) const
 {
-	return 2;
+	Qt::ItemFlags result;
+	result = Qt::ItemIsEnabled
+			| Qt::ItemIsSelectable;
+	if (index.column() == ClassSelected) {
+		result |= (Qt::ItemIsUserCheckable | Qt::ItemIsEditable);
+	}
+	return result;
 }
 
-int ClassTableModel::rowCount(const QModelIndex &parent) const
+bool ClassTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
 {
-	return m_classes.size();
+	if (role == Qt::CheckStateRole && index.column() == ClassSelected) {
+		m_classesSelection[index.row()] = !m_classesSelection[index.row()];
+		emit dataChanged(index, index);
+		return true;
+	}
+	return false;
 }
 
 void ClassTableModel::setSchoolDatabase(SchoolDatabase *schoolDatabase)
 {
 	m_schoolDatabase = schoolDatabase;
+	m_classes = m_schoolDatabase->classList();
+
+
+	reset();
 }
 

Modified: tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.h
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.h	2009-07-31 04:02:06 UTC (rev 1301)
+++ tux4kids-admin/trunk/tux4kids-admin/src/classTableModel.h	2009-07-31 11:15:32 UTC (rev 1302)
@@ -16,6 +16,8 @@
 	QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
 	int columnCount(const QModelIndex &parent = QModelIndex()) const;
 	int rowCount(const QModelIndex &parent = QModelIndex()) const;
+	Qt::ItemFlags flags(const QModelIndex &index) const;
+	bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
 
 	void setSchoolDatabase(SchoolDatabase *schoolDatabase);
 private:
@@ -26,6 +28,7 @@
 	};
 	QPointer<SchoolDatabase> m_schoolDatabase;
 	QList<Class> m_classes;
+	QList<bool> m_classesSelection;
 };
 
 #endif // CLASSTABLEMODEL_H

Modified: tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.cpp	2009-07-31 04:02:06 UTC (rev 1301)
+++ tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.cpp	2009-07-31 11:15:32 UTC (rev 1302)
@@ -16,16 +16,72 @@
 	return 3;
 }
 
+QVariant TeacherTableModel::data(const QModelIndex &index, int role) const
+{
+	if (!index.isValid())
+		return QVariant();
+
+	if (index.row() >= rowCount())
+		return QVariant();
+
+	if (index.column() >= columnCount())
+		return QVariant();
+
+	if (role == Qt::DisplayRole) {
+		switch (index.column()) {
+		case TeacherFirstName:
+			return m_teachers.at(index.row()).firstName();
+		case TeacherLastName:
+			return m_teachers.at(index.row()).lastName();
+		}
+	} else if (role == Qt::CheckStateRole) {
+		if (index.column() == TeacherSelected) {
+			if (m_teachersSelection[index.row()]) {
+				return Qt::Checked;
+			} else {
+				return Qt::Unchecked;
+			}
+		}
+	}
+	return QVariant();
+}
+
+Qt::ItemFlags TeacherTableModel::flags(const QModelIndex &index) const
+{
+	Qt::ItemFlags result;
+	result = Qt::ItemIsEnabled
+			| Qt::ItemIsSelectable;
+	if (index.column() == TeacherSelected) {
+		result |= (Qt::ItemIsUserCheckable | Qt::ItemIsEditable);
+	}
+	return result;
+}
+
+bool TeacherTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+	if (role == Qt::CheckStateRole && index.column() == TeacherSelected) {
+		m_teachersSelection[index.row()] = !m_teachersSelection[index.row()];
+		emit dataChanged(index, index);
+		return true;
+	}
+	return false;
+}
+
 void TeacherTableModel::setSchoolDatabase(SchoolDatabase *schoolDatabase)
 {
 	m_schoolDatabase = schoolDatabase;
 	m_teachers = m_schoolDatabase->teacherList();
+	m_teachersSelection.clear();
+	for (int i = 0; i< m_teachers.size(); i++) {
+		m_teachersSelection.append(false);
+	}
 	connect(m_schoolDatabase, SIGNAL(teacherAdded(const Teacher &)),
 		this, SLOT(teacherAdded(const Teacher &)));
 	connect(m_schoolDatabase, SIGNAL(teacherUpdated(const Teacher &)),
 		this, SLOT(updateTeacher(const Teacher &)));
 	connect(m_schoolDatabase, SIGNAL(teacherDeleted(const Teacher &)),
 		this, SLOT(deleteTeacher(const Teacher &)));
+	reset();
 
 }
 
@@ -33,6 +89,7 @@
 {
 	beginInsertRows(QModelIndex(), m_teachers.size(), m_teachers.size());
 	m_teachers.append(teacher);
+	m_teachersSelection.append(false);
 	endInsertRows();
 }
 
@@ -48,5 +105,6 @@
 	int pos = m_teachers.indexOf(teacher);
 	beginRemoveRows(QModelIndex(), pos, pos);
 	m_teachers.removeAt(pos);
+	m_teachersSelection.removeAt(pos);
 	endRemoveRows();
 }

Modified: tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.h
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.h	2009-07-31 04:02:06 UTC (rev 1301)
+++ tux4kids-admin/trunk/tux4kids-admin/src/teacherTableModel.h	2009-07-31 11:15:32 UTC (rev 1302)
@@ -16,6 +16,9 @@
 	void setSchoolDatabase(SchoolDatabase *schoolDatabase);
 	int columnCount(const QModelIndex &parent = QModelIndex()) const;
 	int rowCount(const QModelIndex &parent = QModelIndex()) const;
+	QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
+	Qt::ItemFlags flags(const QModelIndex &index) const;
+	bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
 
 protected:
 
@@ -27,6 +30,7 @@
 
 	QPointer<SchoolDatabase> m_schoolDatabase;
 	QList<Teacher> m_teachers;
+	QList<bool> m_teachersSelection;
 
 private slots:
 	void addTeacher(const Teacher &teacher);




More information about the Tux4kids-commits mailing list