//MainWidget.cpp
//最短路径算法,和界面的实现
#include "mainwindow.h"
#include <qdebug.h>
#include <QToolBar>
#include <QtAlgorithms>
#include <iostream>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// mapWidget = new MapWidget;
// painter = new QPainter();
dj = new MainWindow::DijkstraFindPath();
dj->CreateGraph ();
scene = new QGraphicsScene;
scene->setSceneRect (-, -, , );
initScene();
view = new QGraphicsView;
view->setScene (scene);
view->setMinimumSize (, );
view->show ();
setCentralWidget (view);
createToolBar (); //实现一个工具栏
// setCentralWidget (mapWidget);
// setMinimumSize (600, 400); //设置最小尺寸
}
MainWindow::DijkstraFindPath::DijkstraFindPath()
{
mgraph.vexnum = ; //初始化点数目
for (int i = ; i < mgraph.vexnum; i++) //初始化点编号
mgraph.vexs.push_back (i);
mgraph.arcnum = ; //暂定
for (int i = ; i < mgraph.vexnum; i++) {
for (int j = ; j < mgraph.vexnum; j++) {
if (i == j)
mgraph.arcs[i][j].adj = ;
else
mgraph.arcs[i][j].adj = INF;
// mgraph.arcs[i][j].info = "";
}
}
}
void MainWindow::DijkstraFindPath::CreateGraph ()
{
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //6 - 5
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //6 - 10
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //5 - 4
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //4 - 3
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //3 - 2
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //3 - 22
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //2 - 1
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//23 - 22
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //6 - 23
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //23 - 4
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //1 - 一食堂
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//一食堂-操场
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//一食堂-祁通1
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//祁通1-祁通2(路口)
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //一食堂-岔路口
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //岔路-7
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//一食堂到岔路(通向7号楼的)
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//岔路-祁通大道
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//祁通大道-图书馆
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//祁通大道-祁通2
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//祁通2-方肇周
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //西大门-祁通1
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//祁通1-祁通2
// mgraph.arcs[25][12].adj = mgraph.arcs[12][25].adj = 108; //西大门-西操
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //1 - 操场
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //10 - 9
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //9 - 8
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //8 - 7
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //7 - 图书馆
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //10 - 13
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//13 - 12
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//13 - 16
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//12 - 15
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//16 - 15
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//15 - 14
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//16 - 19
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //19 - 18
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //18 - 17
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//14 - 17
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //17 - 二超
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ; //二超 - 二食堂
mgraph.arcs[][].adj = mgraph.arcs[][].adj = ;//二食堂-图书馆
//以下处理细节
mgraph.arcnum = ;
}
void MainWindow::DijkstraFindPath::dijkstra (int startPos)
{
for (int i = ; i < mgraph.vexnum; i++) d[i] = INF;
for (int i = ; i < mgraph.vexnum; i++) used[i] = false;
for (int i = ; i < mgraph.vexnum; i++) prev[i] = -;
d[startPos] = ;
while (true) {
int v = -;
for (int u = ; u < mgraph.vexnum; u++) {
if (!used[u] && (v == - || d[u] < d[v])) v = u;
}
if (v == -) break;
used[v] = true;
for (int u = ; u < mgraph.vexnum; u++) {
if (d[u] > d[v] + mgraph.arcs[v][u].adj) {
d[u] = d[v] + mgraph.arcs[v][u].adj;
prev[u] = v;
}
}
}
}
QVector<int> MainWindow::DijkstraFindPath::get_Path (int endPos)
{
QVector<int> path;
for ( ; endPos != -; endPos = prev[endPos]) {
// std::cout << "EndPos: " << endPos << ", ";
path.push_back (endPos);
}
std::reverse(path.begin (), path.end ());
return path;
}
void MainWindow::initScene ()
{
QGraphicsPixmapItem *item =
scene->addPixmap (QPixmap("NanTong.jpg"));
// item->setFlag (QGraphicsItem::ItemIsMovable);
item->setPos (-, -);
}
MainWindow::~MainWindow()
{
}
void MainWindow::createToolBar ()
{
QToolBar *toolBar = addToolBar ("Tool");
startLabel = new QLabel(tr("起点: "));
startComboBox = new QComboBox;
startComboBox->addItem (tr("公寓6号楼")); //
startComboBox->addItem (tr("公寓5号楼")); //
startComboBox->addItem (tr("公寓4号楼")); //
startComboBox->addItem (tr("公寓3号楼")); //
startComboBox->addItem (tr("公寓2号楼")); //
startComboBox->addItem (tr("公寓1号楼")); //
startComboBox->addItem (tr("公寓10号楼")); //
startComboBox->addItem (tr("公寓9号楼")); //
startComboBox->addItem (tr("公寓8号楼")); //
startComboBox->addItem (tr("公寓7号楼")); //
startComboBox->addItem (tr("图书馆")); //
startComboBox->addItem (tr("一食堂")); startComboBox->addItem (tr("西操场")); //11 12
startComboBox->addItem (tr("公寓23号楼")); startComboBox->addItem (tr("公寓13号楼")); //13 14
startComboBox->addItem (tr("公寓22号楼")); startComboBox->addItem (tr("公寓12号楼")); //15 16
startComboBox->addItem (tr("公寓楼16")); startComboBox->addItem (tr("公寓楼15")); //17 18
startComboBox->addItem (tr("公寓楼14")); startComboBox->addItem (tr("公寓楼19")); //19 20
startComboBox->addItem (tr("公寓楼18")); startComboBox->addItem (tr("公寓楼17")); //21 22
startComboBox->addItem (tr("二超")); startComboBox->addItem (tr("二食堂")); //23 24
startComboBox->addItem (tr("西大门")); startComboBox->addItem (tr("方肇周教学楼")); //25 26
endLabel = new QLabel(tr("\t终点: "));
endComboBox = new QComboBox;
endComboBox->addItem (tr("公寓6号楼"));
endComboBox->addItem (tr("公寓5号楼"));
endComboBox->addItem (tr("公寓4号楼"));
endComboBox->addItem (tr("公寓3号楼"));
endComboBox->addItem (tr("公寓2号楼"));
endComboBox->addItem (tr("公寓1号楼"));
endComboBox->addItem (tr("公寓10号楼"));
endComboBox->addItem (tr("公寓9号楼"));
endComboBox->addItem (tr("公寓8号楼"));
endComboBox->addItem (tr("公寓7号楼"));
endComboBox->addItem (tr("图书馆"));
endComboBox->addItem (tr("一食堂")); endComboBox->addItem (tr("西操场"));
endComboBox->addItem (tr("公寓23号楼"));endComboBox->addItem (tr("公寓13号楼"));
endComboBox->addItem (tr("公寓22号楼"));endComboBox->addItem (tr("公寓12号楼"));
endComboBox->addItem (tr("公寓楼16")); endComboBox->addItem (tr("公寓楼15"));
endComboBox->addItem (tr("公寓楼14")); endComboBox->addItem (tr("公寓楼19"));
endComboBox->addItem (tr("公寓楼18")); endComboBox->addItem (tr("公寓楼17"));
endComboBox->addItem (tr("二超")); endComboBox->addItem (tr("二食堂"));
endComboBox->addItem (tr("西大门")); endComboBox->addItem (tr("方肇周教学楼"));
connect (startComboBox, SIGNAL(activated(int)), this, SLOT(setStartStation()));
connect (endComboBox, SIGNAL(activated(int)), this, SLOT(setEndStation()));
findPathBtn = new QToolButton;
findPathBtn->setText (tr("\t\t绘制最短路径"));
connect (findPathBtn, SIGNAL(clicked(bool)), this, SLOT(FindPath()));
clearBtn = new QToolButton;
clearBtn->setText (tr("\t\t清除"));
connect (clearBtn, SIGNAL(clicked(bool)), this, SLOT(Clear()));
toolBar->addWidget (startLabel);
toolBar->addWidget (startComboBox);
toolBar->addWidget (endLabel);
toolBar->addWidget (endComboBox);
toolBar->addWidget (findPathBtn);
toolBar->addWidget (clearBtn);
}
void MainWindow::setStart(int X, int Y) {
startX = X; startY = Y;
// qDebug() << X << ", " << Y;
}
void MainWindow::setEnd (int X, int Y)
{
endX = X; endY = Y;
}
void MainWindow::setStartStation ()
{
switch (startComboBox->currentIndex ()) {
case :
setStart(-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart(-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
case :
setStart (-, -); break;
default:
break;
}
}
void MainWindow::setEndStation ()
{
switch (endComboBox->currentIndex ()) {
case :
setEnd(-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
default:
break;
}
}
void MainWindow::setNextPos (int index)
{
switch (index) {
case :
setEnd(-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
case :
setEnd (-, -); break;
default:
break;
}
}
void MainWindow::FindPath ()
{
//Demo 在图片上绘线 在原有基础上 (+700, +440);
QVector<QPoint> v;
dj->dijkstra (startComboBox->currentIndex ());
//设置下一处的终点
nextPath = dj->get_Path (endComboBox->currentIndex ());
//准备绘制
Clear ();
//将路线绘制下来
QGraphicsPathItem *item = new QGraphicsPathItem();
QPen pen;
pen.setWidth ();
pen.setColor (Qt::red);
item->setPen (pen);
item->setFlag (QGraphicsItem::ItemIsPanel);
// qDebug() << startX << " " << startY << " " << endX << " " << endY;
// qDebug() << "Hello World !";
//设置起点
// v << QPoint(startX + 700, startY + 440);
for (int i = ; i < nextPath.size (); i++) {
qDebug() << nextPath[i] << " , ";
}
scene->addItem (item);
QPainterPath pa; //path
// setNextPos (nextPath[1]);
// item->setLine (startX + 700, startY + 440, endX + 700, endY + 440);
pa.moveTo (startX + , startY + );
for (int i = ; i < nextPath.size() ; i++) {
setNextPos (nextPath[i]);
pa.lineTo (endX + , endY + );
}
item->setPath (pa);
}
void MainWindow::Clear ()
{
QList<QGraphicsItem*> listItem = scene->items ();
while (!listItem.empty ())
{
scene->removeItem (listItem.at ());
listItem.removeAt ();
}
QGraphicsPixmapItem *item =
scene->addPixmap (QPixmap("NanTong.jpg"));
// item->setFlag (QGraphicsItem::ItemIsMovable);
item->setPos (-, -);
}
void MainWindow::paintEvent (QPaintEvent *)
{
}