jce-manager/main/CourseTab/coursestablemanager.cpp

342 lines
10 KiB
C++
Raw Normal View History

2014-09-08 15:54:52 +00:00
#include "coursestablemanager.h"
2014-10-12 04:03:34 +00:00
/*
* TODO: revert gpa to origin
*
*/
2014-09-08 15:54:52 +00:00
coursesTableManager::coursesTableManager(QTableWidget *ptr, user *usrPtr)
{
2014-10-12 13:48:42 +00:00
this->gp = NULL;
this->us = usrPtr;
this->courseTBL = ptr;
2014-10-07 21:16:27 +00:00
2014-10-12 13:48:42 +00:00
/*
2014-10-07 21:16:27 +00:00
* Initilizing Table
*/
2014-10-12 13:48:42 +00:00
courseTBL->setRowCount(0);
courseTBL->setColumnCount(COURSE_FIELDS);
QStringList mz;
mz << QObject::tr("Number") << QObject::tr("Year") << QObject::tr("Semester") << QObject::tr("Serial") << QObject::tr("Name") << QObject::tr("Type") << QObject::tr("Points") << QObject::tr("Hours") << QObject::tr("Grade") << QObject::tr("Additions");
courseTBL->setHorizontalHeaderLabels(mz);
courseTBL->verticalHeader()->setVisible(false);
courseTBL->setSelectionMode(QAbstractItemView::SingleSelection);
courseTBL->setShowGrid(true);
courseTBL->setStyleSheet("QTableView {selection-background-color: red;}");
graph = new gradegraph(ptr);
2014-09-08 15:54:52 +00:00
}
coursesTableManager::~coursesTableManager()
{
2014-10-12 13:48:42 +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-12 13:48:42 +00:00
for (gradeCourse *c: gp->getCourses())
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +00:00
if (us->getInfluenceCourseOnly())
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +00:00
if (isCourseInfluence(c))
2014-10-07 21:16:27 +00:00
addRow(c);
2014-10-12 13:48:42 +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-12 13:48:42 +00:00
if (gp != NULL)
gp->~GradePage();
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-12 13:48:42 +00:00
bool isNumFlag = true;
if (courseTBL->item(row,gradeCourse::CourseScheme::SERIAL) == NULL)
return true;
2014-09-08 15:54:52 +00:00
2014-10-12 13:48:42 +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-12 13:48:42 +00:00
if (c->getSerialNum() == serialCourse)
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +00:00
switch (col)
2014-09-08 15:54:52 +00:00
{
2014-10-05 11:16:01 +00:00
case (gradeCourse::CourseScheme::COURSE_NUMBER_IN_LIST):
2014-10-12 13:48:42 +00:00
c->setCourseNumInList(change.toInt());
break;
2014-10-05 11:16:01 +00:00
case (gradeCourse::CourseScheme::YEAR):
2014-10-12 13:48:42 +00:00
c->setYear(change.toInt());
break;
2014-10-05 11:16:01 +00:00
case (gradeCourse::CourseScheme::SEMESTER):
2014-10-12 13:48:42 +00:00
c->setSemester(change.toInt());
break;
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::NAME):
2014-10-12 13:48:42 +00:00
c->setName(change);
break;
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::TYPE):
2014-10-12 13:48:42 +00:00
c->setType(change);
break;
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::POINTS):
2014-10-12 13:48:42 +00:00
{
2014-09-08 15:54:52 +00:00
change.toDouble(&isNumFlag);
if (!isNumFlag)
2014-10-12 13:48:42 +00:00
{
2014-09-08 15:54:52 +00:00
courseTBL->item(row,col)->setText(QString::number(c->getPoints()));
2014-10-12 13:48:42 +00:00
}
2014-09-08 15:54:52 +00:00
else
2014-10-12 13:48:42 +00:00
c->setPoints(change.toDouble());
2014-09-08 15:54:52 +00:00
break;
2014-10-12 13:48:42 +00:00
}
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::HOURS):
2014-10-12 13:48:42 +00:00
{
2014-09-08 15:54:52 +00:00
change.toDouble(&isNumFlag);
if (!isNumFlag)
2014-10-12 13:48:42 +00:00
{
2014-09-08 15:54:52 +00:00
courseTBL->item(row,col)->setText(QString::number(c->getHours()));
2014-10-12 13:48:42 +00:00
}
2014-09-08 15:54:52 +00:00
else
2014-10-12 13:48:42 +00:00
c->setHours(change.toDouble());
2014-09-08 15:54:52 +00:00
break;
2014-10-12 13:48:42 +00:00
}
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::GRADE):
2014-10-12 13:48:42 +00:00
{
2014-09-08 15:54:52 +00:00
change.toDouble(&isNumFlag);
if (!isNumFlag)
2014-10-12 13:48:42 +00:00
{
2014-09-08 15:54:52 +00:00
courseTBL->item(row,col)->setText(QString::number(c->getGrade()));
2014-10-12 13:48:42 +00:00
}
2014-09-08 15:54:52 +00:00
else
2014-10-12 13:48:42 +00:00
{
2014-09-08 15:54:52 +00:00
if ((change.toDouble() >= 0) && (change.toDouble() <= 100))
2014-10-12 13:48:42 +00:00
c->setGrade(change.toDouble());
2014-09-08 15:54:52 +00:00
else
2014-10-12 13:48:42 +00:00
courseTBL->item(row,col)->setText(QString::number(c->getGrade()));
}
2014-09-08 15:54:52 +00:00
break;
2014-10-12 13:48:42 +00:00
}
2014-09-08 15:54:52 +00:00
case (gradeCourse::CourseScheme::ADDITION):
2014-10-12 13:48:42 +00:00
c->setAdditions(change);
break;
2014-09-08 15:54:52 +00:00
}
2014-10-12 13:48:42 +00:00
break;
2014-09-08 15:54:52 +00:00
}
}
2014-10-12 13:48:42 +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-12 13:48:42 +00:00
int i=1,j=1;
2014-10-05 11:16:01 +00:00
2014-10-12 13:48:42 +00:00
j = 0;
QTableWidgetItem *number,*year,*semester,*serial,*name,*type,*points,*hours,*grade,*addition;
const gradeCourse * c;
if (courseToAdd != NULL)
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +00:00
c = courseToAdd;
if (!isCourseAlreadyInserted(c->getSerialNum()))
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +00:00
courseTBL->setRowCount(courseTBL->rowCount() + 1);
i = courseTBL->rowCount()-1;
number = new QTableWidgetItem();
number->setData(Qt::EditRole, c->getCourseNumInList());
number->setFlags(number->flags() & ~Qt::ItemIsEditable);
year = new QTableWidgetItem();
year->setData(Qt::EditRole,c->getYear());
year->setFlags(year->flags() & ~Qt::ItemIsEditable);
semester = new QTableWidgetItem();
semester->setData(Qt::EditRole,c->getSemester());
semester->setFlags(semester->flags() & ~Qt::ItemIsEditable);
serial = new QTableWidgetItem();
serial->setData(Qt::EditRole,c->getSerialNum());
serial->setFlags(serial->flags() & ~Qt::ItemIsEditable);
name = new QTableWidgetItem();
name->setData(Qt::EditRole,c->getName());
name->setFlags(name->flags() & ~Qt::ItemIsEditable);
type = new QTableWidgetItem();
type->setData(Qt::EditRole, c->getType());
type->setFlags(type->flags() & ~Qt::ItemIsEditable);
points = new QTableWidgetItem();
points->setData(Qt::EditRole, c->getPoints());
points->setFlags(points->flags() & ~Qt::ItemIsEditable);
hours = new QTableWidgetItem();
hours->setData(Qt::EditRole, c->getHours());
hours->setFlags(hours->flags() & ~Qt::ItemIsEditable);
grade = new QTableWidgetItem();
//grade->setData(Qt::EditRole,c->getGrade());
//BUG QT bug
//good for sorting, the problem is when you edit -> the cell indicator disappear
2014-10-12 13:48:42 +00:00
grade->setText(QString::number(c->getGrade()));
addition = new QTableWidgetItem();
addition->setData(Qt::EditRole,c->getAddidtions());
courseTBL->setItem(i,j++,number);
courseTBL->setItem(i,j++,year);
courseTBL->setItem(i,j++,semester);
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);
//FIXME : make it a function, add it when cell has changed
if(c->getGrade() < 55 && c->getGrade() != 0)
2014-10-12 02:52:07 +00:00
{
2014-10-12 13:48:42 +00:00
courseTBL->item(i, j)->setBackground(Qt::darkRed);
courseTBL->item(i,j)->setTextColor(Qt::white);
2014-10-12 02:52:07 +00:00
}
2014-10-12 13:48:42 +00:00
else if(55 <= c->getGrade() && c->getGrade() < 70 )
2014-10-12 02:52:07 +00:00
{
2014-10-12 13:48:42 +00:00
courseTBL->item(i, j)->setBackground(Qt::darkYellow);
courseTBL->item(i,j)->setTextColor(Qt::white);
2014-10-12 02:52:07 +00:00
}
2014-10-12 13:48:42 +00:00
// else if(70 < c->getGrade() && c->getGrade() <= 80 )
// courseTBL->item(i, j)->setBackground(Qt::darkGreen); //They Look Bad!!
// else if(c->getGrade() > 80)
// courseTBL->item(i, j)->setBackground(Qt::green);
2014-10-12 02:52:07 +00:00
2014-10-12 13:48:42 +00:00
j++;
2014-10-12 02:52:07 +00:00
2014-10-12 13:48:42 +00:00
courseTBL->setItem(i,j,addition);
2014-10-05 11:16:01 +00:00
2014-09-08 15:54:52 +00:00
}
}
2014-10-12 13:48:42 +00:00
else
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +00:00
qCritical() << Q_FUNC_INFO << " no course to load!";
2014-09-08 15:54:52 +00:00
}
2014-10-12 02:52:07 +00:00
2014-10-12 13:48:42 +00:00
courseTBL->resizeColumnsToContents();
courseTBL->resizeRowsToContents();
2014-09-08 15:54:52 +00:00
}
double coursesTableManager::getAvg()
{
2014-10-12 13:48:42 +00:00
if (this->gp != NULL)
return gp->getAvg();
return 0;
2014-09-08 15:54:52 +00:00
}
2014-10-05 11:16:01 +00:00
void coursesTableManager::showGraph()
{
2014-10-12 13:48:42 +00:00
qDebug() << Q_FUNC_INFO;
if (gp != NULL)
2014-10-07 14:26:11 +00:00
{
2014-10-12 13:48:42 +00:00
this->graph->showGraph(gp);
}
else
{
qWarning() << Q_FUNC_INFO << "open with null grade page";
2014-10-07 14:26:11 +00:00
}
2014-10-05 11:16:01 +00:00
}
2014-09-08 15:54:52 +00:00
void coursesTableManager::influnceCourseChanged(bool ignoreCourseStatus)
{
2014-10-12 13:48:42 +00:00
if (ignoreCourseStatus)
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +00:00
int i = 0;
while (i < courseTBL->rowCount())
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +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-12 13:48:42 +00:00
else
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +00:00
if (this->gp != NULL)
for (gradeCourse *c: gp->getCourses())
{
if (!(isCourseAlreadyInserted(c->getSerialNum())))
if (c->getPoints() == 0)
addRow(c);
}
2014-09-08 15:54:52 +00:00
}
}
void coursesTableManager::clearTable()
{
2014-10-12 13:48:42 +00:00
if (courseTBL->rowCount() == 0)
return;
2014-09-08 15:54:52 +00:00
2014-10-12 13:48:42 +00:00
int i = 0; //starting point
while (courseTBL->rowCount() > i)
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +00:00
gp->removeCourse(courseTBL->item(i,gradeCourse::CourseScheme::SERIAL)->text());
courseTBL->removeRow(i);
2014-09-08 15:54:52 +00:00
}
2014-10-12 13:48:42 +00:00
gp = NULL;
courseTBL->repaint();
2014-09-08 15:54:52 +00:00
}
gradeCourse *coursesTableManager::getCourseByRow(int row)
{
2014-10-12 13:48:42 +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-12 13:48:42 +00:00
if (c->getSerialNum() == courseSerial.toDouble())
return c;
2014-09-08 15:54:52 +00:00
}
2014-10-12 13:48:42 +00:00
return NULL;
2014-09-08 15:54:52 +00:00
}
bool coursesTableManager::isCourseAlreadyInserted(double courseID)
{
2014-10-12 13:48:42 +00:00
int i;
for (i = courseTBL->rowCount(); i >= 0; --i)
2014-09-08 15:54:52 +00:00
{
2014-10-12 13:48:42 +00:00
if (courseTBL->item(i,gradeCourse::CourseScheme::SERIAL) != NULL)
2014-10-05 11:16:01 +00:00
{
2014-10-12 13:48:42 +00:00
QString courseSerial = courseTBL->item(i,gradeCourse::CourseScheme::SERIAL)->text();
if (QString::number(courseID) == courseSerial)
return true;
2014-10-05 11:16:01 +00:00
}
2014-09-08 15:54:52 +00:00
}
2014-10-12 13:48:42 +00:00
return false;
2014-09-08 15:54:52 +00:00
}
bool coursesTableManager::isCourseInfluence(const gradeCourse *courseToCheck)
{
2014-10-12 13:48:42 +00:00
if (courseToCheck->getPoints() > 0)
return true;
return false;
2014-09-08 15:54:52 +00:00
}