#include"countPage.h"
#include<algorithm>
#include<qmessagebox.h>
#include<qdatetime.h>
#include<qcombobox.h>
#include<qdatetimeedit.h>
#include<qpushbutton.h>
#include<qtable.h>
#include<qradiobutton.h>
#include<qlabel.h>
#include<qhbuttongroup.h>
#include<qlayout.h>
using namespace std;
countPage::countPage(std::vector<std::vector<QString> > &content1,QWidget *parent)
:QWidget(parent),content(content1)
{
vertical=new QVBoxLayout(this);
displayH=new QHBoxLayout(vertical);
displayTable=new QTable(30,5,this);
displayTable->setMaximumSize(350,400);
displayH->addWidget(displayTable);
displayLabel=new QLabel(this);
displayH->addWidget(displayLabel);
chooseH=new QHBoxLayout(vertical);
fromLabel=new QLabel("from",this);
fromLabel->setMaximumSize(40,30);
from=new QDateEdit(this);
toLabel=new QLabel("to",this);
toLabel->setMaximumSize(40,30);
to=new QDateEdit(this);
QDate max(2100,1,1);
QDate min(2000,1,1);
from->setRange(min,max);
to->setRange(min,max);
from->setDate(QDate::currentDate());
to->setDate(QDate::currentDate());
chooseH->addWidget(fromLabel);
chooseH->addWidget(from);
chooseH->addWidget(toLabel);
chooseH->addWidget(to);
measureTypeLabel=new QLabel("measureType",this);
measureTypeLabel->setMaximumSize(90,30);
measureType=new QComboBox(this);
measureType->insertItem("field");
measureType->insertItem("special");
measureType->insertItem("handman");
measureType->insertItem("type");
chooseH->addWidget(measureTypeLabel);
chooseH->addWidget(measureType);
typeGroup=new QHButtonGroup("type",this);
income=new QRadioButton("income",typeGroup);
output=new QRadioButton("output",typeGroup);
none=new QRadioButton("none",typeGroup);
typeGroup->setButton(0);
typeGroup->setRadioButtonExclusive(true);
countH=new QHBoxLayout(vertical);
count=new QPushButton("count",this);
countH->addWidget(typeGroup);
countH->addWidget(count);
connect(count,SIGNAL(clicked()),this,SLOT(countBegin()));
}
bool countPage::fieldCompare(vector<QString> ¶meter1,vector<QString> ¶meter2)
{
if(parameter1[1]>parameter2[1]) return true;
else return false;
}
bool countPage::specialCompare(vector<QString> ¶meter1,vector<QString> ¶meter2)
{
if(parameter1[4]>parameter2[4]) return true;
else return false;
}
bool countPage::handmanCompare(vector<QString> ¶meter1,vector<QString> ¶meter2)
{
if(parameter1[8]>parameter2[8]) return true;
else return false;
}
bool countPage::typeCompare(vector<QString> ¶meter1,vector<QString> ¶meter2)
{
if(parameter1[2]>parameter2[2]) return true;
else return false;
}
void countPage::countBegin()
{
if(validation()==true)
{
dealData();
}
}
bool countPage::validation()
{
if(measureType->currentText()!="type"&&typeGroup->selected()==none)
{
QMessageBox::information(this,"Error","You must choose a type");
return false;
}
if(from->date()>to->date())
{
QMessageBox::information(this,"Error","it's a wrong date choose");
return false;
}
return true;
}
void countPage::dealData()
{
clearTable();
int WAY;
if(measureType->currentText()=="field")
{
sort(content.begin(),content.end(),fieldCompare);
WAY=3;
}
else if(measureType->currentText()=="special")
{
sort(content.begin(),content.end(),specialCompare);
WAY=4;
}
else if(measureType->currentText()=="handman")
{
sort(content.begin(),content.end(),handmanCompare);
WAY=8;
}
else if(measureType->currentText()=="type")
{
sort(content.begin(),content.end(),typeCompare);
WAY=2;
}
struct row
{
QString name;
int n;
int incomeTotal;
int outputTotal;
int pureTotal;
};
row tableGang;
tableGang.name="";
tableGang.n=0;
tableGang.incomeTotal=0;
tableGang.outputTotal=0;
tableGang.pureTotal=0;
int i=0;
vector<vector<QString> >::iterator temp;
if(WAY==2)
{
for(temp=content.begin();temp!=content.end()&&i<29;temp++)
{
if(from->date()<=QDate::fromString((*temp)[1])&&to->date()>=QDate::fromString((*temp)[1])&&tableGang.name!=""&&(*temp)[WAY]!=tableGang.name)
{
displayTable->setText(i,0,tableGang.name);
displayTable->setText(i,1,QString::number(tableGang.n));
displayTable->setText(i,2,QString::number(tableGang.incomeTotal));
displayTable->setText(i,3,QString::number(tableGang.outputTotal));
displayTable->setText(i,4,QString::number(tableGang.pureTotal));
i++;
tableGang.name=(*temp)[WAY];
tableGang.n=1;
tableGang.incomeTotal=(*temp)[5].toInt();
tableGang.outputTotal=(*temp)[6].toInt();
tableGang.pureTotal=tableGang.incomeTotal-tableGang.outputTotal;
}
else if(from->date()<=QDate::fromString((*temp)[1])&&to->date()>=QDate::fromString((*temp)[1]))
{
tableGang.name=(*temp)[WAY];
++tableGang.n;
tableGang.incomeTotal+=(*temp)[5].toInt();
tableGang.outputTotal+=(*temp)[6].toInt();
tableGang.pureTotal=tableGang.incomeTotal-tableGang.outputTotal;
}
}
}
else
{
for(temp=content.begin();temp!=content.end()&&i<29;temp++)
{
if(from->date()<=QDate::fromString((*temp)[1])&&to->date()>=QDate::fromString((*temp)[1])&&tableGang.name!=""&&(*temp)[WAY]!=tableGang.name&&(*temp)[2]==getType())
{
displayTable->setText(i,0,tableGang.name);
displayTable->setText(i,1,QString::number(tableGang.n));
displayTable->setText(i,2,QString::number(tableGang.incomeTotal));
displayTable->setText(i,3,QString::number(tableGang.outputTotal));
displayTable->setText(i,4,QString::number(tableGang.pureTotal));
i++;
tableGang.name=(*temp)[WAY];
tableGang.n=1;
tableGang.incomeTotal=(*temp)[5].toInt();
tableGang.outputTotal=(*temp)[6].toInt();
tableGang.pureTotal=tableGang.incomeTotal-tableGang.outputTotal;
}
else if(from->date()<=QDate::fromString((*temp)[1])&&to->date()>=QDate::fromString((*temp)[1])&&(*temp)[2]==getType())
{
tableGang.name=(*temp)[WAY];
++tableGang.n;
tableGang.incomeTotal+=(*temp)[5].toInt();
tableGang.outputTotal+=(*temp)[6].toInt();
tableGang.pureTotal=tableGang.incomeTotal-tableGang.outputTotal;
}
}
}
displayTable->setText(i,0,tableGang.name);
displayTable->setText(i,1,QString::number(tableGang.n));
displayTable->setText(i,2,QString::number(tableGang.incomeTotal));
displayTable->setText(i,3,QString::number(tableGang.outputTotal));
displayTable->setText(i,4,QString::number(tableGang.pureTotal));
}
QString countPage::getType()
{
if(typeGroup->selected()==income)return QString("income");
if(typeGroup->selected()==output)return QString("output");
if(typeGroup->selected()==none)return QString("none");
}
void countPage::clearTable()
{
int i,j;
for(i=0;i<30;i++)
for(j=0;j<5;j++)
displayTable->setText(i,j,"");
}