add graph. fixes more isses

This commit is contained in:
Liran BN 2014-10-07 17:26:11 +03:00
parent 63c5ec9cc7
commit a87c5abb2c
7 changed files with 359 additions and 117 deletions

View file

@ -53,6 +53,8 @@ void coursesTableManager::insertJceCoursesIntoTable()
*/ */
void coursesTableManager::setCoursesList(QString &html) void coursesTableManager::setCoursesList(QString &html)
{ {
if (gp != NULL)
gp->~GradePage();
gp = new GradePage(html); gp = new GradePage(html);
} }
/** /**
@ -229,7 +231,9 @@ double coursesTableManager::getAvg()
void coursesTableManager::showGraph() void coursesTableManager::showGraph()
{ {
if (gp != NULL) if (gp != NULL)
this->graph->show(); {
this->graph->showGraph(gp);
}
} }
@ -247,12 +251,13 @@ void coursesTableManager::influnceCourseChanged(bool ignoreCourseStatus)
} }
else else
{ {
for (gradeCourse *c: *gp->getCourses()) if (this->gp != NULL)
{ for (gradeCourse *c: *gp->getCourses())
if (!(isCourseAlreadyInserted(c->getSerialNum()))) {
if (c->getPoints() == 0) if (!(isCourseAlreadyInserted(c->getSerialNum())))
addRow(c); if (c->getPoints() == 0)
} addRow(c);
}
} }
} }

View file

@ -172,6 +172,7 @@ void MainScreen::on_ratesButton_clicked()
} }
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
} }
bool MainScreen::checkIfValidDates() bool MainScreen::checkIfValidDates()
{ {
bool flag = false; bool flag = false;
@ -188,29 +189,35 @@ bool MainScreen::checkIfValidDates()
} }
return flag; return flag;
} }
void MainScreen::on_checkBoxCoursesInfluence_toggled(bool checked) void MainScreen::on_checkBoxCoursesInfluence_toggled(bool checked)
{ {
qDebug() << Q_FUNC_INFO << "only main courses toggeled" << checked; qDebug() << Q_FUNC_INFO << "only main courses toggeled" << checked;
this->userLoginSetting->setInfluenceCourseOnly(checked); this->userLoginSetting->setInfluenceCourseOnly(checked);
this->courseTableMgr->influnceCourseChanged(checked); this->courseTableMgr->influnceCourseChanged(checked);
} }
void MainScreen::on_spinBoxCoursesFromYear_valueChanged(int arg1) void MainScreen::on_spinBoxCoursesFromYear_valueChanged(int arg1)
{ {
ui->spinBoxCoursesFromYear->setValue(arg1); ui->spinBoxCoursesFromYear->setValue(arg1);
} }
void MainScreen::on_spinBoxCoursesToYear_valueChanged(int arg1) void MainScreen::on_spinBoxCoursesToYear_valueChanged(int arg1)
{ {
ui->spinBoxCoursesToYear->setValue(arg1); ui->spinBoxCoursesToYear->setValue(arg1);
} }
void MainScreen::on_spinBoxCoursesFromSemester_valueChanged(int arg1) void MainScreen::on_spinBoxCoursesFromSemester_valueChanged(int arg1)
{ {
ui->spinBoxCoursesFromSemester->setValue(arg1%4); ui->spinBoxCoursesFromSemester->setValue(arg1%4);
} }
void MainScreen::on_spinBoxCoursesToSemester_valueChanged(int arg1) void MainScreen::on_spinBoxCoursesToSemester_valueChanged(int arg1)
{ {
ui->spinBoxCoursesToSemester->setValue(arg1%4); ui->spinBoxCoursesToSemester->setValue(arg1%4);
} }
void MainScreen::on_coursesTable_itemChanged(QTableWidgetItem *item) void MainScreen::on_coursesTable_itemChanged(QTableWidgetItem *item)
{ {
if (this->courseTableMgr->changes(item->text(),item->row(),item->column())) if (this->courseTableMgr->changes(item->text(),item->row(),item->column()))
@ -221,17 +228,20 @@ void MainScreen::on_coursesTable_itemChanged(QTableWidgetItem *item)
QMessageBox::critical(this,tr("Error"),tr("Missmatching data")); QMessageBox::critical(this,tr("Error"),tr("Missmatching data"));
} }
} }
void MainScreen::on_clearTableButton_clicked() void MainScreen::on_clearTableButton_clicked()
{ {
qDebug() << Q_FUNC_INFO << "in: " << ui->tabWidget->currentWidget()->objectName(); qDebug() << Q_FUNC_INFO << "in: " << ui->tabWidget->currentWidget()->objectName();
courseTableMgr->clearTable(); courseTableMgr->clearTable();
ui->avgLCD->display(courseTableMgr->getAvg()); ui->avgLCD->display(courseTableMgr->getAvg());
} }
void MainScreen::on_graphButton_clicked() void MainScreen::on_graphButton_clicked()
{ {
courseTableMgr->showGraph(); courseTableMgr->showGraph();
} }
//EVENTS ON CALENDAR TAB //EVENTS ON CALENDAR TAB
void MainScreen::on_getCalendarBtn_clicked() void MainScreen::on_getCalendarBtn_clicked()
{ {

View file

@ -1,153 +1,226 @@
#include "gradePage.h" #include "gradePage.h"
static int maxYear = 0;
static int minYear = 9999;
GradePage::GradePage(QString html) : Page() GradePage::GradePage(QString html) : Page()
{ {
courses = new std::list<gradeCourse*>(); courses = new std::list<gradeCourse*>();
tempHtml = getString(html); tempHtml = getString(html);
coursesListInit(tempHtml); coursesListInit(tempHtml);
} }
GradePage::~GradePage() GradePage::~GradePage()
{ {
for(Course* c : *courses) for(Course* c : *courses)
delete c; delete c;
delete courses; delete courses;
} }
void GradePage::removeCourse(QString courseSerialID) void GradePage::removeCourse(QString courseSerialID)
{ {
for(gradeCourse* c : *courses) for(gradeCourse* c : *courses)
{ {
if (c->getSerialNum() == courseSerialID.toInt()) if (c->getSerialNum() == courseSerialID.toInt())
{ {
courses->remove(c); courses->remove(c);
delete c; delete c;
return; return;
} }
} }
} }
void GradePage::coursesListInit(QString &linesTokinzedString) void GradePage::coursesListInit(QString &linesTokinzedString)
{ {
std::list<QString> stringHolder; std::list<QString> stringHolder;
QString temp; QString temp;
gradeCourse* cTemp = NULL; gradeCourse* cTemp = NULL;
char* tok; char* tok;
char* textToTok = strdup(linesTokinzedString.toStdString().c_str()); char* textToTok = strdup(linesTokinzedString.toStdString().c_str());
tok = strtok(textToTok,"\n"); tok = strtok(textToTok,"\n");
while (tok != NULL) //putting every line in a string holder before parsing it while (tok != NULL) //putting every line in a string holder before parsing it
{ {
temp = tok; temp = tok;
stringHolder.push_back(temp); stringHolder.push_back(temp);
tok = strtok(NULL, "\n"); tok = strtok(NULL, "\n");
} }
for (QString temp: stringHolder) for (QString temp: stringHolder)
{ {
cTemp = lineToCourse(temp); cTemp = lineToCourse(temp);
if (cTemp != NULL) if (cTemp != NULL)
courses->push_back(cTemp); courses->push_back(cTemp);
} }
} }
gradeCourse* GradePage::lineToCourse(QString line) gradeCourse* GradePage::lineToCourse(QString line)
{ {
gradeCourse *tempC = NULL; gradeCourse *tempC = NULL;
QString templinearray[COURSE_FIELDS];//[serial,name,type,points,hours,grade,additions] QString templinearray[COURSE_FIELDS];//[serial,name,type,points,hours,grade,additions]
int serial,year,semester,courseNumInList; int serial,year,semester,courseNumInList;
double points,hours,grade; double points,hours,grade;
QString name,type, additions; QString name,type, additions;
QString tempS = ""; QString tempS = "";
int i = 0; int i = 0;
char* tok; char* tok;
char* cLine = strdup(line.toStdString().c_str()); char* cLine = strdup(line.toStdString().c_str());
tok = strtok(cLine, "\t"); tok = strtok(cLine, "\t");
while(tok != NULL) while(tok != NULL)
{ {
tempS = tok; tempS = tok;
if (i == gradeCourse::CourseScheme::SERIAL) //we need to extract the serial manually if (i == gradeCourse::CourseScheme::SERIAL) //we need to extract the serial manually
{ {
tempS = ""; tempS = "";
char *tokTemp; char *tokTemp;
tokTemp = tok; tokTemp = tok;
while (!(isdigit((int)*tokTemp))) //getting to serial number starting pointer while (!(isdigit((int)*tokTemp))) //getting to serial number starting pointer
tokTemp++; tokTemp++;
while (isdigit((int)*tokTemp)) //serial number while (isdigit((int)*tokTemp)) //serial number
{ {
tempS += QString(*tokTemp); tempS += QString(*tokTemp);
tokTemp++; tokTemp++;
} }
templinearray[i] = tempS.trimmed(); templinearray[i] = tempS.trimmed();
templinearray[i+1] = QString(tokTemp).trimmed(); templinearray[i+1] = QString(tokTemp).trimmed();
i +=2; //skipping on serial and course name i +=2; //skipping on serial and course name
} }
else else
{ {
templinearray[i] = tempS.trimmed(); templinearray[i] = tempS.trimmed();
i++; i++;
} }
if (i == COURSE_FIELDS) if (i == COURSE_FIELDS)
break; break;
tok=strtok(NULL, "\t"); tok=strtok(NULL, "\t");
} }
if (templinearray[0] == "") //empty parsing if (templinearray[0] == "") //empty parsing
{ {
qCritical() << Q_FUNC_INFO << "empty parsing"; qCritical() << Q_FUNC_INFO << "empty parsing";
return NULL; return NULL;
} }
year = templinearray[gradeCourse::CourseScheme::YEAR].toInt(); year = templinearray[gradeCourse::CourseScheme::YEAR].toInt();
semester = templinearray[gradeCourse::CourseScheme::SEMESTER].toInt(); semester = templinearray[gradeCourse::CourseScheme::SEMESTER].toInt();
courseNumInList = templinearray[gradeCourse::CourseScheme::COURSE_NUMBER_IN_LIST].toInt(); courseNumInList = templinearray[gradeCourse::CourseScheme::COURSE_NUMBER_IN_LIST].toInt();
serial = templinearray[gradeCourse::CourseScheme::SERIAL].toInt(); serial = templinearray[gradeCourse::CourseScheme::SERIAL].toInt();
name = templinearray[gradeCourse::CourseScheme::NAME]; name = templinearray[gradeCourse::CourseScheme::NAME];
type = templinearray[gradeCourse::CourseScheme::TYPE]; type = templinearray[gradeCourse::CourseScheme::TYPE];
points = templinearray[gradeCourse::CourseScheme::POINTS].toDouble(); points = templinearray[gradeCourse::CourseScheme::POINTS].toDouble();
hours = templinearray[gradeCourse::CourseScheme::HOURS].toDouble(); hours = templinearray[gradeCourse::CourseScheme::HOURS].toDouble();
if (isGradedYet(templinearray[gradeCourse::CourseScheme::GRADE])) if (isGradedYet(templinearray[gradeCourse::CourseScheme::GRADE]))
grade = templinearray[gradeCourse::CourseScheme::GRADE].toDouble(); grade = templinearray[gradeCourse::CourseScheme::GRADE].toDouble();
else else
grade = NO_GRADE_YET; grade = NO_GRADE_YET;
additions = templinearray[gradeCourse::CourseScheme::ADDITION]; additions = templinearray[gradeCourse::CourseScheme::ADDITION];
tempC = new gradeCourse(year,semester,courseNumInList,serial,name,type,points,hours,grade,additions); if (year >= maxYear)
return tempC; maxYear = year;
if (year <= minYear)
minYear = year;
tempC = new gradeCourse(year,semester,courseNumInList,serial,name,type,points,hours,grade,additions);
return tempC;
} }
//checking if one of the chars inside grade is not a number //checking if one of the chars inside grade is not a number
bool GradePage::isGradedYet(QString grade) bool GradePage::isGradedYet(QString grade)
{ {
if (strlen(grade.toStdString().c_str()) <= 1) if (strlen(grade.toStdString().c_str()) <= 1)
return false;
for (char c: grade.toStdString())
{
if (c == '\0')
break;
if (((!isdigit((int)c)) && (!isspace((int)c)))) //48 = 0, 57 = 9
return false; return false;
for (char c: grade.toStdString())
{
if (c == '\0')
break;
if (((!isdigit((int)c)) && (!isspace((int)c)))) //48 = 0, 57 = 9
return false;
} }
return true; return true;
} }
/**
* @brief GradePage::getAvg getting avg
* @return - gpa avg of all courses
*/
double GradePage::getAvg() double GradePage::getAvg()
{ {
double avg = 0; double avg = 0;
double points = 0; double points = 0;
for(gradeCourse* c : *courses) for (gradeCourse* c : *courses)
{ {
if ((c->getGrade() != 0)) if ((c->getGrade() != 0))
{ {
avg += c->getGrade() * c->getPoints(); avg += c->getGrade() * c->getPoints();
points += c->getPoints(); points += c->getPoints();
} }
} }
avg /= points; avg /= points;
return avg; return avg;
}
/**
* @brief GradePage::getAvg getting avg of given year
* @param year - year (yyyy)
* @return - gpa avg of given year
*/
double GradePage::getAvg(int year)
{
double avg = 0;
double points = 0;
for (gradeCourse* c : *courses)
{
if ((c->getGrade() != 0) && (c->getYear() == year))
{
avg += c->getGrade() * c->getPoints();
points += c->getPoints();
}
}
if (points != 0)
avg /= points;
else
avg=0;
return avg;
}
/**
* @brief GradePage::getAvg
* @param year - year (yyyy)
* @param semester - semeser (1-3)
* @return -gpa avg of given year in given semester
*/
double GradePage::getAvg(int year, int semester)
{
double avg = 0;
double points = 0;
for (gradeCourse* c : *courses)
{
if ((c->getGrade() != 0) && (c->getYear() == year) && (c->getSemester() == semester))
{
avg += c->getGrade() * c->getPoints();
points += c->getPoints();
}
}
if (points != 0)
avg /= points;
else
avg=0;
return avg;
}
/**
* @brief GradePage::getMinYearInList
* @return the minimal year inside courses list
*/
int GradePage::getMinYearInList()
{
return minYear;
}
int GradePage::getMaxYearInList()
{
return maxYear;
} }

View file

@ -17,23 +17,30 @@ class GradePage : public Page
{ {
public: public:
GradePage(QString html); GradePage(QString html);
~GradePage(); ~GradePage();
void removeCourse(QString courseSerialID); void removeCourse(QString courseSerialID);
double getAvg(); double getAvg();
double getAvg(int year);
double getAvg(int year, int semester);
std::list<gradeCourse*>* getCourses() { return courses; } int getMinYearInList();
int getMaxYearInList();
std::list<gradeCourse*>* getCourses() { return courses; }
private: private:
void coursesListInit(QString &linesTokinzedString); void coursesListInit(QString &linesTokinzedString);
gradeCourse* lineToCourse(QString line); gradeCourse* lineToCourse(QString line);
bool isGradedYet(QString grade); bool isGradedYet(QString grade);
std::list<gradeCourse*>* courses; std::list<gradeCourse*>* courses;
QString tempHtml;
QString tempHtml;
}; };

View file

@ -2,14 +2,148 @@
#include "ui_gradegraph.h" #include "ui_gradegraph.h"
gradegraph::gradegraph(QWidget *parent, GradePage *gpPTR) : gradegraph::gradegraph(QWidget *parent, GradePage *gpPTR) :
QDialog(parent), QDialog(parent),
ui(new Ui::gradegraph) ui(new Ui::gradegraph)
{ {
ui->setupUi(this); ui->setupUi(this);
this->gp = gpPTR; this->gp = gpPTR;
}
void gradegraph::showGraph(GradePage *gpPTR)
{
this->gp = gpPTR;
setVisualization();
setGraphsData();
this->show();
} }
gradegraph::~gradegraph() gradegraph::~gradegraph()
{ {
delete ui; delete ui;
}
void gradegraph::setGraphsData()
{
int minYearInList = gp->getMinYearInList(); //2012
int maxYearInList = gp->getMaxYearInList()+1; //2016
int xRangeForYear = (maxYearInList - minYearInList+2)*3; //6*3=18
QVector<double> SemesterialAvg(xRangeForYear),yearlyAvg(xRangeForYear),sem(xRangeForYear);
for (int yearCount=0,i=1; i<xRangeForYear; ++i)
{
double lastAvg = 0;
sem[i] = i;
if (i%4==1) //years
{
lastAvg = gp->getAvg(minYearInList+yearCount);
yearlyAvg[i] = lastAvg;
// add the text label at the top:
QCPItemText *textLabel = new QCPItemText(ui->graphwidget);
ui->graphwidget->addItem(textLabel);
textLabel->position->setCoords(i, lastAvg+1.5); // place position at center/top of axis rect
textLabel->setText(QString::number(lastAvg,'g',4));
textLabel->setFont(QFont(font().family(), 8)); // make font a bit larger
textLabel->setPen(QPen(Qt::black)); // show black border around text
yearCount++;
}
else
{
if (i+4 < xRangeForYear) //semesters
{
double avg = gp->getAvg(minYearInList+yearCount,(i-1)%4);
SemesterialAvg[i+4] = avg;
// add the text label at the top:
QCPItemText *textLabel = new QCPItemText(ui->graphwidget);
ui->graphwidget->addItem(textLabel);
textLabel->position->setCoords(i+4, avg+0.5); // place position at center/top of axis rect
textLabel->setText(QString::number(avg,'g',4));
textLabel->setFont(QFont(font().family(), 8)); // make font a bit larger
textLabel->setPen(QPen(Qt::black)); // show black border around text
}
yearlyAvg[i] = 0;
}
}
//yearly
ui->graphwidget->graph(0)->setData(sem,yearlyAvg);
//yearly
ui->graphwidget->graph(1)->setData(sem,SemesterialAvg);
}
/**
* @brief gradegraph::setvisualization set graph borders text, range and looking
*/
void gradegraph::setVisualization()
{
ui->graphwidget->axisRect()->setupFullAxesBox(true); //make the graph looks like a box
ui->graphwidget->addGraph(); //yearly and semesterial graphs
ui->graphwidget->addGraph();
ui->graphwidget->graph(0)->setName(tr("Yearly Average"));
ui->graphwidget->graph(0)->setLineStyle(QCPGraph::lsLine);
ui->graphwidget->graph(0)->setPen(QPen(Qt::GlobalColor::blue));
ui->graphwidget->graph(1)->setName(tr("Semesterial Average"));
ui->graphwidget->graph(1)->setLineStyle(QCPGraph::lsLine);
ui->graphwidget->graph(1)->setPen(QPen( QColor(Qt::GlobalColor::red)));
int minYearInList = gp->getMinYearInList();
int maxYearInList = gp->getMaxYearInList()+1;
int xRangeForYear = (maxYearInList - minYearInList+2)*3;
QVector<QString> xStrings(0);
for (int year=minYearInList,i = 0; i < xRangeForYear-1; ++i)
{
//set year x axe label to be yyyy A B C yyyy+1 A B C yyyy+2....
int semesterChar = i%4;
QString tempString;
switch (semesterChar)
{
case 1:
tempString = tr("A");
xStrings << tempString;
break;
case 2:
tempString = tr("B");
xStrings << tempString;
break;
case 3:
tempString = tr("C");
xStrings << tempString;
break;
case 0:
tempString = QString::number(year);
xStrings << tempString;
year++;
break;
}
}
ui->graphwidget->yAxis->setLabel(tr("AVG Grade"));
ui->graphwidget->yAxis->setTickLabelFont(QFont(QFont().family(), 8));
ui->graphwidget->yAxis->setRange(50,100);
ui->graphwidget->yAxis->setTickStep(2);
ui->graphwidget->yAxis->setAutoSubTicks(false);
ui->graphwidget->yAxis->setAutoTickStep(false);
ui->graphwidget->yAxis->setSubTickCount(5);
ui->graphwidget->xAxis->setLabel(tr("Years"));
ui->graphwidget->xAxis->setAutoTickLabels(false);
ui->graphwidget->xAxis->setTickVectorLabels(xStrings);
ui->graphwidget->xAxis->setTickLabelFont(QFont(QFont().family(), 7));
ui->graphwidget->xAxis->setAutoTickStep(false);
ui->graphwidget->xAxis->setTickStep(1);
ui->graphwidget->xAxis->setAutoSubTicks(false);
ui->graphwidget->xAxis->setSubTickCount(1);
ui->graphwidget->xAxis->setRange(1,xRangeForYear);
ui->graphwidget->legend->setVisible(true); //show graph name on top right
} }

View file

@ -15,11 +15,15 @@ class gradegraph : public QDialog
public: public:
gradegraph(QWidget *parent = 0, GradePage *gpPTR = 0); gradegraph(QWidget *parent = 0, GradePage *gpPTR = 0);
void showGraph(GradePage *gpPTR);
~gradegraph(); ~gradegraph();
private: private:
GradePage *gp; GradePage *gp;
void setVisualization();
void setGraphsData();
Ui::gradegraph *ui; Ui::gradegraph *ui;
}; };

View file

@ -6,14 +6,14 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>597</width> <width>624</width>
<height>461</height> <height>527</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Dialog</string> <string>Dialog</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item> <item>
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="sizePolicy"> <property name="sizePolicy">
@ -34,7 +34,16 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QCustomPlot" name="graphwidget" native="true"/> <widget class="QCustomPlot" name="graphwidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout"/>
<zorder></zorder>
</widget>
</item> </item>
</layout> </layout>
</widget> </widget>