jce-manager/main/CourseTab/coursestablemanager.cpp

253 lines
7.4 KiB
C++
Raw Normal View History

2014-09-08 15:54:52 +00:00
#include "coursestablemanager.h"
coursesTableManager::coursesTableManager(QTableWidget *ptr, user *usrPtr)
{
2014-10-07 15:00:59 +00:00
this->gp = NULL;
this->us = usrPtr;
this->courseTBL = ptr;
2014-09-08 15:54:52 +00:00
2014-10-07 15:00:59 +00:00
/*
2014-09-08 15:54:52 +00:00
* Initilizing Table
*/
2014-10-07 15:00:59 +00:00
courseTBL->setRowCount(0);
courseTBL->setColumnCount(COURSE_FIELDS);
QStringList mz;
mz << QObject::tr("Code") << QObject::tr("Name") << QObject::tr("Type") << QObject::tr("Points") << QObject::tr("Hours") << QObject::tr("Grade") << QObject::tr("Additions");
courseTBL->setHorizontalHeaderLabels(mz);
courseTBL->verticalHeader()->setVisible(true);
courseTBL->setSelectionMode(QAbstractItemView::SingleSelection);
courseTBL->setShowGrid(true);
courseTBL->setStyleSheet("QTableView {selection-background-color: red;}");
2014-09-08 15:54:52 +00:00
}
coursesTableManager::~coursesTableManager()
{
2014-10-07 15:00:59 +00:00
courseTBL = NULL;
delete gp;
gp = NULL;
2014-09-08 15:54:52 +00:00
}
/**
* @brief coursesTableManager::insertJceCoursesIntoTable phrasing the course list to rows in table
*/
void coursesTableManager::insertJceCoursesIntoTable()
{
2014-10-07 15:00:59 +00:00
for (gradeCourse *c: *gp->getCourses())
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
if (us->getInfluenceCourseOnly())
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
if (isCourseInfluence(c))
2014-09-08 15:54:52 +00:00
addRow(c);
2014-10-07 15:00:59 +00:00
}
else
addRow(c);
2014-09-08 15:54:52 +00:00
}
}
/**
* @brief coursesTableManager::setCoursesList creating courses list with given html page
* @param html
*/
void coursesTableManager::setCoursesList(QString &html)
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
gp = new GradePage(html);
2014-09-08 15:54:52 +00:00
}
/**
* @brief coursesTableManager::changes when user changes the table manually it updates it
* @param change string change
* @param row row index
* @param col col index
* @return if change has been done
*/
bool coursesTableManager::changes(QString change, int row, int col)
{
2014-10-07 15:00:59 +00:00
bool isNumFlag = true;
2014-09-08 15:54:52 +00:00
2014-10-07 15:00:59 +00:00
int serialCourse = courseTBL->item(row,gradeCourse::CourseScheme::SERIAL)->text().toInt();
for (gradeCourse *c: *gp->getCourses())
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
if (c->getSerialNum() == serialCourse)
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
switch (col)
2014-09-08 15:54:52 +00:00
{
case (gradeCourse::CourseScheme::NAME):
2014-10-07 15:00:59 +00:00
c->setName(change);
break;
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::TYPE):
2014-10-07 15:00:59 +00:00
c->setType(change);
break;
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::POINTS):
2014-10-07 15:00:59 +00:00
{
2014-09-08 15:54:52 +00:00
change.toDouble(&isNumFlag);
if (!isNumFlag)
2014-10-07 15:00:59 +00:00
{
2014-09-08 15:54:52 +00:00
courseTBL->item(row,col)->setText(QString::number(c->getPoints()));
2014-10-07 15:00:59 +00:00
}
2014-09-08 15:54:52 +00:00
else
2014-10-07 15:00:59 +00:00
c->setPoints(change.toDouble());
2014-09-08 15:54:52 +00:00
break;
2014-10-07 15:00:59 +00:00
}
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::HOURS):
2014-10-07 15:00:59 +00:00
{
2014-09-08 15:54:52 +00:00
change.toDouble(&isNumFlag);
if (!isNumFlag)
2014-10-07 15:00:59 +00:00
{
2014-09-08 15:54:52 +00:00
courseTBL->item(row,col)->setText(QString::number(c->getHours()));
2014-10-07 15:00:59 +00:00
}
2014-09-08 15:54:52 +00:00
else
2014-10-07 15:00:59 +00:00
c->setHours(change.toDouble());
2014-09-08 15:54:52 +00:00
break;
2014-10-07 15:00:59 +00:00
}
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::GRADE):
2014-10-07 15:00:59 +00:00
{
2014-09-08 15:54:52 +00:00
change.toDouble(&isNumFlag);
if (!isNumFlag)
2014-10-07 15:00:59 +00:00
{
2014-09-08 15:54:52 +00:00
courseTBL->item(row,col)->setText(QString::number(c->getGrade()));
2014-10-07 15:00:59 +00:00
}
2014-09-08 15:54:52 +00:00
else
2014-10-07 15:00:59 +00:00
{
2014-09-08 15:54:52 +00:00
if ((change.toDouble() >= 0) && (change.toDouble() <= 100))
2014-10-07 15:00:59 +00:00
c->setGrade(change.toDouble());
2014-09-08 15:54:52 +00:00
else
2014-10-07 15:00:59 +00:00
courseTBL->item(row,col)->setText(QString::number(c->getGrade()));
}
2014-09-08 15:54:52 +00:00
break;
2014-10-07 15:00:59 +00:00
}
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::ADDITION):
2014-10-07 15:00:59 +00:00
c->setAdditions(change);
break;
2014-09-08 15:54:52 +00:00
}
2014-10-07 15:00:59 +00:00
break;
2014-09-08 15:54:52 +00:00
}
}
2014-10-07 15:00:59 +00:00
return isNumFlag;
2014-09-08 15:54:52 +00:00
}
/**
* @brief coursesTableManager::addRow adds row with given information
* @param courseToAdd if exists, add its information to table
*/
void coursesTableManager::addRow(const gradeCourse *courseToAdd)
{
2014-10-07 15:00:59 +00:00
int i,j;
i = courseTBL->rowCount();
j = 0;
QTableWidgetItem *serial,*name,*type,*points,*hours,*grade,*addition;
const gradeCourse * c;
if (courseToAdd != NULL)
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
c = courseToAdd;
if (!isCourseAlreadyInserted(c->getSerialNum()))
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
courseTBL->setRowCount(courseTBL->rowCount()+1);
serial = new QTableWidgetItem(QString::number(c->getSerialNum()));
serial->setFlags(serial->flags() & ~Qt::ItemIsEditable);
points = new QTableWidgetItem(QString::number(c->getPoints()));
points->setFlags(serial->flags() & ~Qt::ItemIsEditable);
hours = new QTableWidgetItem(QString::number(c->getHours()));
hours->setFlags(serial->flags() & ~Qt::ItemIsEditable);
grade = new QTableWidgetItem(QString::number(c->getGrade()));
name = new QTableWidgetItem(c->getName());
name->setFlags(serial->flags() & ~Qt::ItemIsEditable);
type = new QTableWidgetItem(c->getType());
type->setFlags(serial->flags() & ~Qt::ItemIsEditable);
addition = new QTableWidgetItem(c->getAddidtions());
courseTBL->setItem(i,j++,serial);
courseTBL->setItem(i,j++,name);
courseTBL->setItem(i,j++,type);
courseTBL->setItem(i,j++,points);
courseTBL->setItem(i,j++,hours);
courseTBL->setItem(i,j++,grade);
courseTBL->setItem(i,j,addition);
2014-09-08 15:54:52 +00:00
}
}
2014-10-07 15:00:59 +00:00
else
2014-09-08 15:54:52 +00:00
{
}
2014-10-07 15:00:59 +00:00
courseTBL->resizeColumnsToContents();
2014-09-08 15:54:52 +00:00
}
double coursesTableManager::getAvg()
{
2014-10-07 15:00:59 +00:00
if (this->gp != NULL)
return gp->getAvg();
return 0;
2014-09-08 15:54:52 +00:00
}
void coursesTableManager::influnceCourseChanged(bool ignoreCourseStatus)
{
2014-10-07 15:00:59 +00:00
if (ignoreCourseStatus)
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
int i = 0;
while (i < courseTBL->rowCount())
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
if (courseTBL->item(i,gradeCourse::CourseScheme::POINTS)->text().compare("0") == 0)
courseTBL->removeRow(i--);
i++;
2014-09-08 15:54:52 +00:00
}
}
2014-10-07 15:00:59 +00:00
else
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
if (gp != NULL)
2014-09-08 15:54:52 +00:00
for (gradeCourse *c: *gp->getCourses())
2014-10-07 15:00:59 +00:00
{
2014-09-08 15:54:52 +00:00
if (!(isCourseAlreadyInserted(c->getSerialNum())))
2014-10-07 15:00:59 +00:00
if (c->getPoints() == 0)
addRow(c);
}
2014-09-08 15:54:52 +00:00
}
}
void coursesTableManager::clearTable()
{
2014-10-07 15:00:59 +00:00
if (courseTBL->rowCount() == 0)
return;
2014-09-08 15:54:52 +00:00
2014-10-07 15:00:59 +00:00
int i = 0; //starting point
while (courseTBL->rowCount() > i)
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
gp->removeCourse(courseTBL->item(i,gradeCourse::CourseScheme::SERIAL)->text());
courseTBL->removeRow(i);
2014-09-08 15:54:52 +00:00
}
2014-10-07 15:00:59 +00:00
gp = NULL;
courseTBL->repaint();
2014-09-08 15:54:52 +00:00
}
gradeCourse *coursesTableManager::getCourseByRow(int row)
{
2014-10-07 15:00:59 +00:00
QString courseSerial = courseTBL->item(row,gradeCourse::CourseScheme::SERIAL)->text();
for (gradeCourse *c: *gp->getCourses())
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
if (c->getSerialNum() == courseSerial.toDouble())
return c;
2014-09-08 15:54:52 +00:00
}
2014-10-07 15:00:59 +00:00
return NULL;
2014-09-08 15:54:52 +00:00
}
bool coursesTableManager::isCourseAlreadyInserted(double courseID)
{
2014-10-07 15:00:59 +00:00
int i=0;
for (i = 0; i < courseTBL->rowCount(); ++i)
2014-09-08 15:54:52 +00:00
{
2014-10-07 15:00:59 +00:00
QString courseSerial = courseTBL->item(i,gradeCourse::CourseScheme::SERIAL)->text();
if (QString::number(courseID) == courseSerial)
return true;
2014-09-08 15:54:52 +00:00
}
2014-10-07 15:00:59 +00:00
return false;
2014-09-08 15:54:52 +00:00
}
bool coursesTableManager::isCourseInfluence(const gradeCourse *courseToCheck)
{
2014-10-07 15:00:59 +00:00
if (courseToCheck->getPoints() > 0)
return true;
return false;
2014-09-08 15:54:52 +00:00
}