----------------------------------MinimizeResult.cpp---------------------------------
#include"MinimizeResult.h"
MinimizeResultTree::MinimizeResultTree(QWidget *parent,Qt::WindowFlags f):QWidget(parent,f)
{
Up_Left_x=0;
Up_Left_y=20;
IncreaceMent=20;
FontSize=14;
PenWidth=2;
LeftMargin=QPoint(Up_Left_x,Up_Left_y);
IncreaceMent=20;
SetLength=0;
BoxLength=0;
}
//return the last char of the spicifyed string
QChar MinimizeResultTree::GetLastState_InASet(int LayerNumber,int SetNumber,QList<QStringList> MinimizeResult)
{
return (MinimizeResult.at(LayerNumber).at(SetNumber).at(MinimizeResult.at(LayerNumber).at(SetNumber).size()-1));
}
QSize MinimizeResultTree::minimumSizeHint()const
{
return QSize(400, 400);
}
QSize MinimizeResultTree::sizeHint()const
{
return QSize(400, 400);
}
//state to pain the tree
void MinimizeResultTree::paintEvent(QPaintEvent *event)
{
QString str="0,12345678;1,12345;2,678;3,12345;4,67;5,8;6,123;7,45;8,67;9,8;";
QFile file("../file.txt");
QDataStream _stream(&file);
_stream.setByteOrder(QDataStream::LittleEndian);
if(file.open(QIODevice::WriteOnly))
{
int length=str.length();
for(int i=0;i<length;i++)
{
int a=str.data().toAscii();
_stream<<a;
}
}
file.close();
file.open(QIODevice::ReadOnly);
QDataStream stream(&file);
stream.setByteOrder(QDataStream::LittleEndian);
QList<QStringList> MinimizeResult;
QStringList CurrLayer;QString CurrSet;
int LastState;int CurrInput;int LastInput;int LastSetNumber;int CurrLayerSetsCount=0;;
CurrSet="";
LastSetNumber=-1;
while(!stream.atEnd())
{
stream>>CurrInput;
while(CurrInput!=','&&!stream.atEnd())
{
LastInput=CurrInput;
stream>>CurrInput;
}
LastSetNumber++;
CurrLayerSetsCount++;
stream>>CurrInput;
while(!stream.atEnd()&&CurrInput!=';')
{
CurrSet+=QString(QChar(CurrInput));
LastInput=CurrInput;
stream>>CurrInput;
}
if(LastSetNumber==0)
LastState=LastInput;
CurrLayer<<CurrSet;
CurrSet.clear();
if(0==QString::compare(QString(QChar::fromAscii(LastInput)),QString(QChar::fromAscii(LastState))))
{
CurrLayer<<QString::number(CurrLayerSetsCount);
MinimizeResult<<CurrLayer;
CurrLayer.clear();
CurrLayerSetsCount=0;
}
}
QPainter painter;
QPen pen(Qt::black);
painter.begin(this);
pen.setWidth(PenWidth);
painter.setPen(pen);
QFont font("Arial",FontSize,QFont::Bold);
painter.setFont(font);
painter.fillRect(event->rect(), QColor(224,224,224));
int MaxSetNumber=MinimizeResult.at(MinimizeResult.count()-1).count();//the biggest set in the tree
int MaxStateNumber=MinimizeResult.at(0).at(0).count();//return the state number
MaxBoxLength=FontSize*(MaxStateNumber*1.5+0.5*MaxSetNumber)+IncreaceMent*MaxStateNumber;//the biggest Length of the set
QString BoxString;//the string after modified added comma
int LayerNumber=0;
int SetNumber=0;
int LastState_InALayer;
int Flag_Line_Connected=1;//需记录左边的线的点
QLine CurrLineLeftLine;//remeber the first left bar line to insert the Horizontal Line
SpaceToBar=5;
while(LayerNumber<MinimizeResult.count())
{
LastState_InALayer=0;//no number in the last layer is set to some one's father.
while(SetNumber<MinimizeResult.at(LayerNumber).count()-1)
{
SetLength=GetCurrSetLength(MinimizeResult.at(LayerNumber).at(SetNumber));//get current setLength
BoxLength=MinimizeResult.at(LayerNumber).at(SetNumber).size()*MaxBoxLength/MaxStateNumber;//get the box length,according to the state the set has.
BoxString=GetCurrBoxString(MinimizeResult.at(LayerNumber).at(SetNumber));//get the string to show
if(LayerNumber!=MinimizeResult.count()-1)//if it is not at the end of the layer
painter.drawLine(SetBarQLine(QPoint(LeftMargin.x(),LeftMargin.y()+SpaceToBar),BoxLength));
if(LayerNumber!=0)//if it is not the first layer
painter.drawLine(SetBarQLine(QPoint(LeftMargin.x(),LeftMargin.y()-2*FontSize+PenWidth),BoxLength));
//draw text
painter.drawText(SetBarQLine(LeftMargin,BoxLength).x1()-SetLength/2+2*PenWidth,
SetBarQLine(LeftMargin,BoxLength).y1(),BoxString);
if(Flag_Line_Connected==1)//we have just draw a HoriLine,so we have to remeber the next first left line
{
CurrLineLeftLine=SetBarQLine(QPoint(LeftMargin.x(),LeftMargin.y()-3*FontSize),BoxLength);
Flag_Line_Connected=0;//set the first left line is not set again unless the
}
LeftMargin=SetLeftMargin(LeftMargin,BoxLength);
if(LayerNumber!=0)//it is not the first layer and we find the last number in the last layer
if(GetLastState_InASet(LayerNumber,SetNumber,MinimizeResult)
==GetLastState_InASet(LayerNumber-1,LastState_InALayer,MinimizeResult))
{
painter.drawLine(SetLineQLine(CurrLineLeftLine,SetBarQLine(QPoint(LeftMargin.x()
-BoxLength,LeftMargin.y()-3*FontSize),BoxLength)));//we draw the HoriLine
LastState_InALayer++;
Flag_Line_Connected=1;//to set the first left line of the HoriLine
}
SetNumber++;
}
Flag_Line_Connected=1;
SetNumber=0; LayerNumber++;
LeftMargin=SetLeftMargin(QPoint(Up_Left_x,LeftMargin.y()+3*(FontSize+PenWidth)),0);
}
}
//set the BarLine using the leftMargin and the boxLength
QLine MinimizeResultTree::SetBarQLine(QPoint Point,int BoxLength) { return
QLine(Point.x()+BoxLength/2,Point.y()+PenWidth*2,Point.x()+BoxLength/2,Point.y()+2*PenWidth+FontSize);
}
//set the HoriLine using the first left line and the last right line
QLine MinimizeResultTree::SetLineQLine(QLine LeftVLine ,QLine RightVLine) {
return QLine(LeftVLine.x1(),LeftVLine.y1()+FontSize,RightVLine.x1(),RightVLine.y1()+FontSize);
}
//set the text point unsing the firstMargin and the setLength ,BoxLength
QPoint MinimizeResultTree::SetTextPoint(QPoint LeftMargin,int BoxLength,int
SetLength) { return
QPoint(LeftMargin.x()+(BoxLength-SetLength)/2,LeftMargin.y()); }
//set the leftMargin add the LastBoxLength
QPoint MinimizeResultTree::SetLeftMargin(QPoint Point,int BoxLength) {
return QPoint(Point.x()+BoxLength,Point.y());
}
//set the string after added comma
QString MinimizeResultTree::GetCurrBoxString(QString String)
{
QString Result="";
Result+=QString("{");
Result+=QString(String.at(0));
int i=1;
while(i<String.size())
{
Result+=QString(",");
Result+=QString(String.at(i++));
}
Result+=QString("}");
return Result;
}
//return the special char specified
int MinimizeResultTree::GetCurrSetLength(QString String)
{
return FontSize*(String.count()+1)/2+FontSize*String.count();
}
//return the boxlength of the current string
int MinimizeResultTree::GetCurrBoxLength(int StateNumber)
{
return (StateNumber*MaxBoxLength/MaxStateNumber);
}