#include "navigation.h"
#include "sightbutton.h"
#include "mainwindow.h"
#include "drawpath.h"
navigation::navigation(QWidget *parent):QDialog(parent)
{
setupUi (this);
setAttribute(Qt::WA_DeleteOnClose); //在close()的时候delete()窗口
setWindowFlags(Qt::FramelessWindowHint); //设置窗口无边框
move (parent->width ()-this->width (),30); //设置对话框位置
fromEditFocus=true;
}
void navigation::closeWindow ()
{
close();
}
void navigation::timerEvent (QTimerEvent *event)
{
//如果是关闭窗口 ,则让对话框缓缓退出主窗口再关闭
if(event->timerId ()==closeTimerId)
{
if((this->y ()+this->height ())>0)
{
this->move (this->x (),this->y ()-10);
}
else
{
this->killTimer (closeTimerId);
QDialog::close ();
}
return;
}
//如果是显示窗口,则让对话框缓缓进入主窗口
if(event->timerId ()==showTimerId)
{
if(this->y ()<20)
{
this->move (this->x (),this->y ()+10);
}
else
{
this->killTimer (showTimerId);
}
}
}
void navigation::close ()
{
closeTimerId=startTimer(10); //设置时间间隔
}
void navigation::show ()
{
initSightList();
this->move (this->x (),0-this->height ());
QDialog::show ();
showTimerId=startTimer(10);
}
void navigation::initSightList ()
{
//初始化ListWidget控件景点列表
QList<sightButton *> allSightButtons=this->parentWidget ()->findChildren<sightButton *>();
QListIterator<sightButton *>ibuttons((allSightButtons));
while(ibuttons.hasNext ())
{
sightButton *sb=ibuttons.next ();
this->sightList->addItem (sb->toolTip ());
}
}
void navigation::onSightList (QListWidgetItem *lwi)
{
if(fromEditFocus==true)
{
fromEdit->setText (lwi->text ());
fromEditFocus=false;
}
else
{
toEdit->setText (lwi->text ());
fromEditFocus=true;
}
}
void navigation::onNavigation ()
{
sightButton *fromButton=NULL;
sightButton *toButton=NULL;
//获得主窗口所有景点按钮的QList
QList<sightButton *> allSightButtons=this->parentWidget ()->findChildren<sightButton *>();
QListIterator<sightButton *>ibuttons((allSightButtons));
while(ibuttons.hasNext ())
{
sightButton *sb=ibuttons.next ();
if(fromEdit->text ()==sb->toolTip ()) //如果fromEdit的文本等于这个景点的toolTip则将fromButton指向这个景点
{
fromButton=sb;
}
else if(toEdit->text()==sb->toolTip ()) //如果toEdit~~
{
toButton=sb;
}
}
if(fromButton==NULL||toButton==NULL)
{
return;
}
MainWindow *mainwindow=static_cast<MainWindow*>(this->parentWidget ());
DrawPath *drawpath=mainwindow->findChild<DrawPath *>("centralWidget");
int source;
int dest;
source=fromButton->toolTip ().left (fromButton->toolTip ().indexOf (" ")).toInt ()-1;
dest=toButton->toolTip ().left (fromButton->toolTip ().indexOf (" ")).toInt ()-1;
this->initVertex ();
this->findShortPath (source,dest);
drawpath->setPathLength (allVertex[dest].distance);
drawpath->setDrawLine (pathPoint,true);
}
void navigation::initVertex ()
{
allVertex.clear ();
vertex beimen(QPoint(1050,270)); //设置顶点与加入顶点
beimen.addNeighborVertex (10);
beimen.addNeighborVertex (11);
vertex zuqiucang(QPoint(1110,390));
zuqiucang.addNeighborVertex (13);
zuqiucang.addNeighborVertex (16);
vertex yiyan(QPoint(1020,470));
yiyan.addNeighborVertex (16);
yiyan.addNeighborVertex (22);
vertex yijiao(QPoint(890,360));
yijiao.addNeighborVertex (12);
vertex yunhu(QPoint(870,480));
yunhu.addNeighborVertex (15);
yunhu.addNeighborVertex (22);
vertex jinhu(QPoint(620,390));
jinhu.addNeighborVertex (18);
jinhu.addNeighborVertex (15);
vertex youyongchi(QPoint(380,170));
youyongchi.addNeighborVertex (21);
vertex wangqiting(QPoint(340,260));
wangqiting.addNeighborVertex (19);
vertex tushuguan(QPoint(710,510));
tushuguan.addNeighborVertex (17);
tushuguan.addNeighborVertex (22);
vertex yishitang(QPoint(820,200));
yishitang.addNeighborVertex (10);
yishitang.addNeighborVertex (14);
vertex v10(QPoint(920,220));
v10.addNeighborVertex (0);
v10.addNeighborVertex (9);
vertex v11(QPoint(980,320));
v11.addNeighborVertex (0);
v11.addNeighborVertex (12);
v11.addNeighborVertex (13);
vertex v12(QPoint(900,340));
v12.addNeighborVertex (11);
v12.addNeighborVertex (3);
v12.addNeighborVertex (14);
v12.addNeighborVertex (16);
v12.addNeighborVertex (15);
vertex v13(QPoint(1080,380));
v13.addNeighborVertex (1);
v13.addNeighborVertex (11);
v13.addNeighborVertex (16);
vertex v14(QPoint(840,290));
v14.addNeighborVertex (12);
v14.addNeighborVertex (20);
v14.addNeighborVertex (9);
vertex v15(QPoint(750,380));
v15.addNeighborVertex (5);
v15.addNeighborVertex (4);
v15.addNeighborVertex (18);
v15.addNeighborVertex (17);
v15.addNeighborVertex (12);
vertex v16(QPoint(1020,450));
v16.addNeighborVertex (2);
v16.addNeighborVertex (13);
vertex v17(QPoint(680,450));
v17.addNeighborVertex (8);
v17.addNeighborVertex (15);
vertex v18(QPoint(600,370));
v18.addNeighborVertex (5);
v18.addNeighborVertex (15);
v18.addNeighborVertex (19);
vertex v19(QPoint(380,250));
v19.addNeighborVertex (7);
v19.addNeighborVertex (21);
vertex v20(QPoint(500,150));
v20.addNeighborVertex (14);
v20.addNeighborVertex (21);
vertex v21(QPoint(420,190));
v21.addNeighborVertex (6);
v21.addNeighborVertex (19);
v21.addNeighborVertex (20);
vertex v22(QPoint(940,500));
v22.addNeighborVertex (4);
v22.addNeighborVertex (2);
v22.addNeighborVertex (8);
allVertex.append (beimen);
allVertex.append (zuqiucang);
allVertex.append (yiyan);
allVertex.append (yijiao);
allVertex.append (yunhu);
allVertex.append (jinhu);
allVertex.append (youyongchi);
allVertex.append (wangqiting);
allVertex.append (tushuguan);
allVertex.append (yishitang);
allVertex.append (v10);
allVertex.append (v11);
allVertex.append (v12);
allVertex.append (v13);
allVertex.append (v14);
allVertex.append (v15);
allVertex.append (v16);
allVertex.append (v17);
allVertex.append (v18);
allVertex.append (v19);
allVertex.append (v20);
allVertex.append (v21);
allVertex.append (v22);
}
void navigation::findShortPath (int source,int dest)
{
pathPoint.clear ();
allVertex[source].setLastVertex (source); //设置源点的上一个顶点为它本身
int count=allVertex.size ();
int v,w;
bool *found=new bool[count];
for(v=0;v<count;v++)
{
found[v]=false; //设置全部的顶点都没有被找到或说是上色
if(allVertex[source].neighborVertex.contains (v)) //一开始只初始化源点的临点的距离
{
allVertex[v].distance=length(allVertex[source].point,allVertex[v].point);
allVertex[v].setLastVertex (source); //设置各临点的上一个顶点为源点
}
}
found[source]=true;
allVertex[source].distance=0;
for(int i=0;i<count;i++)
{
double min=10000;
for(w=0;w<count;w++)
{
if(!found[w])
{
if(allVertex[w].distance<min)
{
v=w;
min=allVe