Skip to content

Commit

Permalink
Fill students table in teacher view
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaled-Waled committed Mar 24, 2023
1 parent f82bf12 commit 8e320fb
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 56 deletions.
48 changes: 27 additions & 21 deletions controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,38 @@ void Controller::createStudentView()
QObject::connect(studentView, &StudentView::requestLogout, this, &Controller::handleLogout);
}

void Controller::createAdminView()
{
adminView = new AdminView();
adminView->show();
QObject::connect(adminView, &AdminView::requestLogout, this, &Controller::handleLogout);
QObject::connect(adminView, &AdminView::requestAllStudents, [this](){
adminView->placeUsersInTable(model->getAllStudents());
});
QObject::connect(adminView, &AdminView::requestAllTeachers, [this](){
adminView->placeUsersInTable(model->getAllTeachers());
});
}

void Controller::createTeacherView()
{
teacherView = new TeacherView();
teacherView->show();
sendTeacherDataToView();
teacherView->fillStudentsTable(model->getStudentsWithCourse(user->course));
QObject::connect(teacherView, &TeacherView::requestLogout, this, &Controller::handleLogout);
QObject::connect(teacherView, &TeacherView::requestTeacherData, this, &Controller::sendTeacherDataToView);
}
void Controller::sendStudentDataToView()
{
Student* student = (Student*)user;
studentView->fillStudentData(student);
}

void Controller::sendTeacherDataToView()
{
Teacher* teacher = (Teacher*)user;
teacherView->fillTeacherData(teacher);
}
void Controller::handleChangeCourse(QString newCourse)
{
model->changeStudentCourse(user->email,newCourse);
Expand Down Expand Up @@ -104,23 +130,3 @@ void Controller::handleLogout()
adminView = nullptr;
teacherView = nullptr;
}

void Controller::createAdminView()
{
adminView = new AdminView();
adminView->show();
QObject::connect(adminView, &AdminView::requestLogout, this, &Controller::handleLogout);
QObject::connect(adminView, &AdminView::requestAllStudents, [this](){
adminView->placeUsersInTable(model->getAllStudents());
});
QObject::connect(adminView, &AdminView::requestAllTeachers, [this](){
adminView->placeUsersInTable(model->getAllTeachers());
});
}

void Controller::createTeacherView()
{
teacherView = new TeacherView();
teacherView->show();
QObject::connect(teacherView, &TeacherView::requestLogout, this, &Controller::handleLogout);
}
1 change: 1 addition & 0 deletions controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private slots:
void handleLogin();
void handleLogout();
void sendStudentDataToView();
void sendTeacherDataToView();
void handleChangeCourse(QString);

};
Expand Down
26 changes: 26 additions & 0 deletions model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,29 @@ std::vector<Teacher> Model::getAllTeachers()

return result;
}

std::vector<Student> Model::getStudentsWithCourse(QString course)
{
std::vector<Student> result;
QSqlQuery query(database);
query.prepare(QString("Select * from %1 where course = '%2'").arg("students", course));
executeQuery(query);

while (query.next())
{
Student* ptr = new Student();

ptr->firstName = query.value(0).toString();
ptr->lastName = query.value(1).toString();
ptr->email = query.value(2).toString();
ptr->gender = true;
ptr->age = std::atoi(query.value(4).toString().toStdString().c_str());
ptr->course = query.value(5).toString();
ptr->password = query.value(6).toString();

Student student = Student(*ptr);
delete(ptr);
result.push_back(student);
}
return result;
}
1 change: 1 addition & 0 deletions model.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Model
void changeStudentCourse(QString, QString);
std::vector<Student> getAllStudents();
std::vector<Teacher> getAllTeachers();
std::vector<Student> getStudentsWithCourse(QString);
};

#endif // MODEL_H
35 changes: 35 additions & 0 deletions teacherview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ TeacherView::TeacherView(QWidget *parent) :
ui(new Ui::TeacherView)
{
ui->setupUi(this);
setWindowTitle("Student details");

ui->le_fname->setDisabled(true);
ui->le_lname->setDisabled(true);
ui->le_course->setDisabled(true);
ui->le_age->setDisabled(true);
ui->le_email->setDisabled(true);

ui->tableWidget_students->setColumnCount(4);
ui->tableWidget_students->setHorizontalHeaderLabels({"First name", "Last name", "E-Mail", "Course"});
}

TeacherView::~TeacherView()
Expand All @@ -18,3 +28,28 @@ void TeacherView::on_button_logout_clicked()
emit requestLogout();
}

void TeacherView::fillTeacherData(Teacher* teacher)
{
ui->le_fname->setText(teacher->firstName);
ui->le_lname->setText(teacher->lastName);
ui->le_course->setText(teacher->course);
ui->le_email->setText(teacher->email);
ui->le_age->setText(QString::fromStdString(std::to_string(teacher->age)));
}

void TeacherView::placeStudentInTable(Student student)
{
auto* table = ui->tableWidget_students;
table->insertRow(table->rowCount());

table->setItem(table->rowCount()-1, 0, new QTableWidgetItem(student.firstName));
table->setItem(table->rowCount()-1, 1, new QTableWidgetItem(student.lastName));
table->setItem(table->rowCount()-1, 2, new QTableWidgetItem(student.email));
table->setItem(table->rowCount()-1, 3, new QTableWidgetItem(student.course));
}

void TeacherView::fillStudentsTable(std::vector<Student> students)
{
for(auto student: students)
placeStudentInTable(student);
}
5 changes: 5 additions & 0 deletions teacherview.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define TEACHERVIEW_H

#include <QWidget>
#include "DTOs.h"

namespace Ui {
class TeacherView;
Expand All @@ -14,15 +15,19 @@ class TeacherView : public QWidget
public:
explicit TeacherView(QWidget *parent = nullptr);
~TeacherView();
void fillTeacherData(Teacher*);
void fillStudentsTable(std::vector<Student> students);

private slots:
void on_button_logout_clicked();

signals:
void requestLogout();
void requestTeacherData();

private:
Ui::TeacherView *ui;
void placeStudentInTable(Student);
};

#endif // TEACHERVIEW_H
93 changes: 58 additions & 35 deletions teacherview.ui
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,29 @@
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="0">
<layout class="QFormLayout" name="formLayout">
<item row="1" column="0">
<widget class="QLabel" name="label_fname">
<property name="text">
<string>First Name</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_lname">
<property name="text">
<string>Last name</string>
<item row="0" column="0">
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_age">
<property name="text">
<string>Age</string>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</widget>
</spacer>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_course">
<item row="1" column="0">
<widget class="QLabel" name="label_fname">
<property name="text">
<string>Course</string>
<string>First Name</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="le_fname"/>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="le_lname"/>
</item>
<item row="5" column="1">
<widget class="QLineEdit" name="le_age"/>
</item>
<item row="7" column="1">
<widget class="QLineEdit" name="le_course"/>
</item>
<item row="2" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
Expand All @@ -78,6 +61,16 @@
</property>
</spacer>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_lname">
<property name="text">
<string>Last name</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="le_lname"/>
</item>
<item row="4" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">
Expand All @@ -91,7 +84,17 @@
</property>
</spacer>
</item>
<item row="6" column="0">
<item row="7" column="0">
<widget class="QLabel" name="label_age">
<property name="text">
<string>Age</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLineEdit" name="le_age"/>
</item>
<item row="8" column="0">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -104,8 +107,18 @@
</property>
</spacer>
</item>
<item row="0" column="0">
<spacer name="verticalSpacer_4">
<item row="9" column="0">
<widget class="QLabel" name="label_course">
<property name="text">
<string>Course</string>
</property>
</widget>
</item>
<item row="9" column="1">
<widget class="QLineEdit" name="le_course"/>
</item>
<item row="10" column="0">
<spacer name="verticalSpacer_5">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
Expand All @@ -117,8 +130,18 @@
</property>
</spacer>
</item>
<item row="8" column="0">
<spacer name="verticalSpacer_5">
<item row="5" column="0">
<widget class="QLabel" name="label_email">
<property name="text">
<string>E-Mail</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLineEdit" name="le_email"/>
</item>
<item row="6" column="0">
<spacer name="verticalSpacer_6">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
Expand Down

0 comments on commit 8e320fb

Please sign in to comment.