diff --git a/DataStructureProject.pro b/DataStructureProject.pro index 489678b..f817db9 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -33,9 +33,10 @@ SOURCES += \ monitor.cpp \ boundary.cpp \ carmananger.cpp \ - parkingLotItem.cpp \ car.cpp \ - path.cpp + path.cpp \ + parkingLotManager.cpp \ + parkinglotgraph.cpp HEADERS += \ mainwindow.h \ @@ -45,9 +46,10 @@ HEADERS += \ monitor.h \ boundary.h \ carmananger.h \ - parkingLotItem.h \ car.h \ - path.h + path.h \ + parkingLotManager.h \ + parkinglotgraph.h FORMS += \ mainwindow.ui \ diff --git a/ParkingLotWidget.cpp b/ParkingLotWidget.cpp index b73c5f9..82768a8 100644 --- a/ParkingLotWidget.cpp +++ b/ParkingLotWidget.cpp @@ -25,6 +25,12 @@ int ParkingLotWidget::addSpace(ParkingSpaceWidget *sp) return m_spaceList.size(); } +int ParkingLotWidget::addRoad(Road *r) +{ + m_roadList.push_back(r); + return m_roadList.size(); +} + ParkingLotWidget::~ParkingLotWidget() { } @@ -126,3 +132,13 @@ QString ParkingLotWidget::getName() const { return name; } + +const QVector& ParkingLotWidget::getSpaceList() const +{ + return m_spaceList; +} + +const QVector& ParkingLotWidget::getRoadList() const +{ + return m_roadList; +} diff --git a/ParkingLotWidget.h b/ParkingLotWidget.h index 6952e45..8737c2c 100644 --- a/ParkingLotWidget.h +++ b/ParkingLotWidget.h @@ -4,9 +4,11 @@ #include #include #include -#include +#include +#include "Road.h" #include "ParkingSpaceWidget.h" class ParkingSpaceWidget; // 先声明类,防止循环include +class Road; class ParkingLotWidget: public QWidget { @@ -14,12 +16,16 @@ class ParkingLotWidget: public QWidget public: explicit ParkingLotWidget(QWidget* parent, const QString& xml); int addSpace(ParkingSpaceWidget* sp); + int addRoad(Road* r); int getCapacity() {return m_spaceList.size(); } ~ParkingLotWidget(); QString getName() const; + const QVector& getSpaceList() const; + const QVector& getRoadList() const; private: - std::vector m_spaceList; + QVector m_spaceList; + QVector m_roadList; void parse_xml(const QString& xml); QBoxLayout *parseLayout(const QDomElement& element); QBoxLayout *layout = nullptr; diff --git a/ParkingSpaceWidget.cpp b/ParkingSpaceWidget.cpp index fbaf14b..60071d4 100644 --- a/ParkingSpaceWidget.cpp +++ b/ParkingSpaceWidget.cpp @@ -57,13 +57,19 @@ QBoxLayout *ParkingSpaceWidget::makeParkingSapceGroup(ParkingLotWidget* parent, return layout; } -void ParkingSpaceWidget::paintEvent(QPaintEvent * ) +void ParkingSpaceWidget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setPen(Qt::black); - painter.drawText(20, 25, QString::number(number)); + // painter.drawText(20, 25, QString::number(number)); + QString p; + p.append(QString::number(pos().x())); + p.append(","); + p.append(QString::number(pos().y())); + painter.setPen(Qt::white); painter.setBrush(Qt::white); + painter.save(); painter.translate(30, 20); switch (dir) { case E: @@ -85,9 +91,23 @@ void ParkingSpaceWidget::paintEvent(QPaintEvent * ) /*painter.setPen(Qt::blue); painter.setBrush(Qt::NoBrush); painter.drawRect(0, 0, this->width() - 1, this->height() - 1);*/ + + painter.restore(); + painter.setPen(Qt::black); + painter.drawText(0, 25, p); painter.end(); } +int ParkingSpaceWidget::getNumber() const +{ + return number; +} + +ParkingSpaceWidget::direction ParkingSpaceWidget::getDir() const +{ + return dir; +} + ParkingSpaceWidget::~ParkingSpaceWidget() { } diff --git a/ParkingSpaceWidget.h b/ParkingSpaceWidget.h index 9d4e651..414b767 100644 --- a/ParkingSpaceWidget.h +++ b/ParkingSpaceWidget.h @@ -14,6 +14,9 @@ public: ParkingSpaceWidget(ParkingLotWidget* parent, const QString &dir); static QBoxLayout* makeParkingSapceGroup(ParkingLotWidget* parent, const QString& dir, int n, const QString &expend); ~ParkingSpaceWidget(); + direction getDir() const; + int getNumber() const; + protected: void paintEvent(QPaintEvent* ) override; private: diff --git a/Road.cpp b/Road.cpp index 3b4e652..beb6f74 100644 --- a/Road.cpp +++ b/Road.cpp @@ -2,7 +2,8 @@ #include #include -Road::Road(QWidget* parent, const int length, const direction dir) + +Road::Road(ParkingLotWidget* parent, const int length, const direction dir) :QWidget(parent), length(length), dir(dir) { switch(dir) @@ -19,6 +20,7 @@ Road::Road(QWidget* parent, const int length, const direction dir) break; } // setContentsMargins(0, 0, 0, 0); + number = parent->addRoad(this); } void Road::paintEvent(QPaintEvent * event) @@ -42,8 +44,26 @@ void Road::paintEvent(QPaintEvent * event) y += 50; } } + painter.setPen(Qt::red); + painter.setBrush(Qt::NoBrush); + painter.drawRect(0, 0, width() - 1, height() - 1); } Road::~Road() { } + +uint Road::getNumber() const +{ + return number; +} + +int Road::getLength() const +{ + return length; +} + +Road::direction Road::getDir() const +{ + return dir; +} diff --git a/Road.h b/Road.h index d0a2abb..3059a28 100644 --- a/Road.h +++ b/Road.h @@ -1,15 +1,24 @@ #pragma once #include +#include "ParkingLotWidget.h" + +class ParkingLotWidget; + class Road: public QWidget { Q_OBJECT public: enum direction{ horizontal, vertical}; - Road(QWidget* parent, const int length, direction dir); + Road(ParkingLotWidget* parent, const int length, direction dir); void paintEvent(QPaintEvent* event) override; - ~Road(); + uint getNumber() const; + int getLength() const; + direction getDir() const; + ~Road(); + private: - const int length; + const int length; direction dir; + uint number; }; diff --git a/monitor.cpp b/monitor.cpp index d56751d..927c838 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -2,29 +2,36 @@ #include "ui_monitor.h" #include #include -#include "parkingLotItem.h" +#include "parkingLotManager.h" Monitor::Monitor(QWidget *parent) : QWidget(parent), ui(new Ui::Monitor) { ui->setupUi(this); - ParkingLotItem *pk = new ParkingLotItem(Q_NULLPTR, this); - QObject::connect(ui->downStairButton, &QPushButton::clicked, pk, &ParkingLotItem::showDownStairFloor); - QObject::connect(ui->upStairButton, &QPushButton::clicked, pk, &ParkingLotItem::showUpStairFloor); - QObject::connect(pk, &ParkingLotItem::enableUpButton, ui->upStairButton, &QPushButton::setEnabled); - QObject::connect(pk, &ParkingLotItem::enableDownButton, ui->downStairButton, &QPushButton::setEnabled); - QObject::connect(pk, &ParkingLotItem::setCapacity, ui->capacity, &QLabel::setText); - QObject::connect(pk, &ParkingLotItem::setLoad, ui->load, &QLabel::setText); - QObject::connect(pk, &ParkingLotItem::setLayerName, ui->layerName, &QLabel::setText); - - pk->showParkingLot(); QGraphicsScene *scene = new QGraphicsScene(this); // 创建场景 + ParkingLotManager *pk = new ParkingLotManager(this, scene); + QObject::connect(ui->downStairButton, &QPushButton::clicked, pk, &ParkingLotManager::showDownStairFloor); + QObject::connect(ui->upStairButton, &QPushButton::clicked, pk, &ParkingLotManager::showUpStairFloor); + QObject::connect(pk, &ParkingLotManager::enableUpButton, ui->upStairButton, &QPushButton::setEnabled); + QObject::connect(pk, &ParkingLotManager::enableDownButton, ui->downStairButton, &QPushButton::setEnabled); + QObject::connect(pk, &ParkingLotManager::setCapacity, ui->capacity, &QLabel::setText); + QObject::connect(pk, &ParkingLotManager::setLoad, ui->load, &QLabel::setText); + QObject::connect(pk, &ParkingLotManager::setLayerName, ui->layerName, &QLabel::setText); + + QObject::connect(ui->drawPath, &QPushButton::clicked, [=](){ + pk->drawPath(ui->placeNum1->text().toUInt(), ui->placeNum2->text().toUInt()); + }); + pk->showParkingLot(); ui->view->setScene(scene); - scene->addItem(pk); + // scene->addItem(pk); } Monitor::~Monitor() { delete ui; } + + + + diff --git a/monitor.ui b/monitor.ui index 95de97b..c7c1c46 100644 --- a/monitor.ui +++ b/monitor.ui @@ -67,7 +67,7 @@ - + QLayout::SetDefaultConstraint @@ -243,6 +243,34 @@ + + + + + + 1 + + + + + + + + + + 104 + + + + + + + 画线 + + + + + diff --git a/parkingLotItem.cpp b/parkingLotItem.cpp deleted file mode 100644 index 8953bd4..0000000 --- a/parkingLotItem.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "parkingLotItem.h" -#include "ParkingLotWidget.h" -#include -#include - -ParkingLotItem::ParkingLotItem(QGraphicsPixmapItem *parent, QObject* objectParent): - QObject(objectParent), QGraphicsPixmapItem(parent) -{ - // 找到xml文件路径 - QString filePath = __FILE__; - QStringList list = filePath.split("\\"); - QString result; - for (int i = 0; i < list.size() - 1; i++) - result.append(list.at(i)).append("\\"); - QStringList l = {"parkinglot_b1.xml", "parkinglot_f1.xml"}; - for (int i = 0; i < 2; i++) { - QString t(result); - m_xml_path.push_back(t.append(l.at(i))); - } - m_cars.resize(m_num_of_layer); // 停在每一层的车 - m_waitting.resize(m_num_of_entry); // 在每个入口等待的车 - for (uint i = 0; i < m_num_of_layer; i++) { - ParkingLotWidget* widget = new ParkingLotWidget(Q_NULLPTR, m_xml_path.at(i)); // 生成一个widget - m_pixmap.push_back(new QPixmap(widget->size())); // 创建一层的pixmap - qDebug() << "widget size x=" << widget->size().width() << " y=" <size().height(); - widget->render(m_pixmap[i]); // 渲染pixmap - m_capacity.push_back(widget->getCapacity()); // 获得这一层的最大容量 - m_cars[i].resize(m_capacity.at(i)); // 初始化一层的车位 - m_name.push_back(widget->getName()); // 这一层的名字 - m_num_of_cars.push_back(0); // 初始停车数量为0 - delete widget; // 删除widget - - QPainter painter(m_pixmap[i]); - painter.setPen(Qt::red); - painter.drawRect(0, 0, m_pixmap[i]->width() - 1, m_pixmap[i]->height() - 1); - } -} - -void ParkingLotItem::showParkingLot(uint pos) -{ - if (pos >= m_num_of_layer) { - qDebug() << "ParkingLotItem: 层数错误"; - return; - } - setPixmap(*m_pixmap.at(pos)); - emit setCapacity(QString::number(m_capacity.at(pos))); - emit setLoad(QString::number(m_capacity.at(pos) - m_num_of_cars.at(pos))); - emit setLayerName(m_name.at(pos)); -} - -void ParkingLotItem::showParkingLot() -{ - showParkingLot(m_current_floor); -} - -void ParkingLotItem::showDownStairFloor() -{ - if (m_current_floor == m_num_of_layer - 1) // 当前是最顶层 - emit enableUpButton(true); - if (m_current_floor == 1) - emit enableDownButton(false); // 到底 - if (m_current_floor > 0) - --m_current_floor; - showParkingLot(m_current_floor); -} - -void ParkingLotItem::showUpStairFloor() -{ - if (m_current_floor == 0) - emit enableDownButton(true); - if (m_current_floor == m_num_of_layer - 2) - emit enableUpButton(false); - if (m_current_floor < m_num_of_layer - 1) - ++ m_current_floor; - showParkingLot(m_current_floor); -} diff --git a/parkingLotManager.cpp b/parkingLotManager.cpp new file mode 100644 index 0000000..e95a64f --- /dev/null +++ b/parkingLotManager.cpp @@ -0,0 +1,157 @@ +#include "parkingLotManager.h" +#include "ParkingLotWidget.h" +#include "ParkingSpaceWidget.h" +#include +#include +#include +#include +#include + +//bool inRect(const QRect& r, const QPoint& p) { +// if (p.x() > r.x() && p.x() < r.x() + r.width() && +// p.y() > r.y() && p.y() < r.y() + r.height()) +// return true; +// else +// return false; +//} + +ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scene): + QObject(objectParent), m_scene(scene) +{ + // 找到xml文件路径 + QString filePath = __FILE__; + QStringList list = filePath.split("\\"); + QString result; + for (int i = 0; i < list.size() - 1; i++) + result.append(list.at(i)).append("\\"); + QStringList l = {"parkinglot_b1.xml", "parkinglot_f1.xml"}; + for (int i = 0; i < 2; i++) { + QString t(result); + m_xml_path.push_back(t.append(l.at(i))); + } + m_cars.resize(m_num_of_layer); // 停在每一层的车 + m_waitting.resize(m_num_of_entry); // 在每个入口等待的车 + for (uint i = 0; i < m_num_of_layer; i++) { + ParkingLotWidget* widget = new ParkingLotWidget(Q_NULLPTR, m_xml_path.at(i)); // 生成一个widget + // m_pixmap.push_back(new QPixmap(widget->size())); // 创建一层的pixmap + // qDebug() << "widget size x=" << widget->size().width() << " y=" <size().height(); + // widget->render(m_pixmap[i]); // 渲染pixmap + m_capacity.push_back(widget->getCapacity()); // 获得这一层的最大容量 + m_cars[i].resize(m_capacity.at(i)); // 初始化一层的车位 + m_name.push_back(widget->getName()); // 这一层的名字 + m_num_of_cars.push_back(0); // 初始停车数量为0 + widget->hide(); + m_widgets.push_back(widget); + m_scene->addWidget(widget); + if (i == 1) + m_graph.push_back(new ParkingLotGraph(widget)); + else + m_graph.push_back(nullptr); + } +} + +void ParkingLotManager::showParkingLot(uint pos) +{ + if (pos >= m_num_of_layer) { + qDebug() << "ParkingLotManager: 层数错误"; + return; + } + for (uint i = 0; i < m_num_of_layer; i++) + m_widgets.at(i)->hide(); + m_widgets.at(pos)->show(); + emit setCapacity(QString::number(m_capacity.at(pos))); + emit setLoad(QString::number(m_capacity.at(pos) - m_num_of_cars.at(pos))); + emit setLayerName(m_name.at(pos)); + if (pos == 1) + m_graph[1]->paint(m_scene); +} + +void ParkingLotManager::showParkingLot() +{ + showParkingLot(m_current_floor); +} + +void ParkingLotManager::showDownStairFloor() +{ + if (m_current_floor == m_num_of_layer - 1) // 当前是最顶层 + emit enableUpButton(true); + if (m_current_floor == 1) + emit enableDownButton(false); // 到底 + if (m_current_floor > 0) + --m_current_floor; + showParkingLot(m_current_floor); +} + +void ParkingLotManager::showUpStairFloor() +{ + if (m_current_floor == 0) + emit enableDownButton(true); + if (m_current_floor == m_num_of_layer - 2) + emit enableUpButton(false); + if (m_current_floor < m_num_of_layer - 1) + ++ m_current_floor; + showParkingLot(m_current_floor); +} + +void ParkingLotManager::drawPath(uint n1, uint n2) +{ +// class Turtle { +// QPoint p; +// QGraphicsScene* s; +// public: +// Turtle(QPoint start, QGraphicsScene* scence): p(start), s(scence) {} +// void goX(uint n) { +// QPoint a = p; +// a.setX(p.x() + n); +// s->addLine(QLine(p, a)); +// p = a; +// } +// void goY(uint n) { +// QPoint a = p; +// a.setY(p.y() + n); +// s->addLine(QLine(p, a)); +// p = a; +// } +// QPoint getP() const {return p;} +// int x() const {return p.x();} +// int y() const {return p.y();} +// }; +// ParkingSpaceWidget* space1 = m_widgets.at(m_current_floor)->getSpaceList().at(n1 - 1); +// ParkingSpaceWidget* space2 = m_widgets.at(m_current_floor)->getSpaceList().at(n2 - 1); +// QPoint start = space1->pos(), end = space2->pos(); +// start.setX(start.x() + space1->width() / 2); +// start.setY(start.y() + space1->height() / 2); // 设定为车位的中点 +// end.setX(end.x() + space2->width() / 2); +// end.setY(end.y() + space2->height() / 2); // 设定为车位的中点 +// Turtle turtle(start, m_scene); +// switch(space1->getDir()) { +// case ParkingSpaceWidget::N: turtle.goY(-45); break; +// case ParkingSpaceWidget::S: turtle.goY(45); break; +// case ParkingSpaceWidget::E: turtle.goX(45); break; +// case ParkingSpaceWidget::W: turtle.goX(-45); break; +// } // 从车位里走出来 +// int n = 0, Point; +// while (turtle.x() != end.x() || turtle.y() != end.y()) { +// Road* road; +// for (Road* r: m_widgets.at(m_current_floor)->m_roadList) +// if (inRect(*r, turtle.getP())) { +// road = r; +// break; +// } // 找到当前点在哪条路上 + +//// if (turtle.x() != end.x() && turtle.y() != end.y()) { +//// switch(space1->getDir()) { +//// case ParkingSpaceWidget::N: +//// case ParkingSpaceWidget::S: turtle.goX(end.x() - turtle.x()); break; +//// case ParkingSpaceWidget::E: +//// case ParkingSpaceWidget::W: turtle.goY(end.y() - turtle.y()); break; +//// } +//// } else if (turtle.x() == end.x()) +//// turtle.goY(end.y() - turtle.y()); +//// else +//// turtle.goX(end.x() - turtle.x()); +//// if (n++ == 100) +//// break; +// } +// qDebug() << n; +} diff --git a/parkingLotItem.h b/parkingLotManager.h similarity index 74% rename from parkingLotItem.h rename to parkingLotManager.h index 11677c6..c233b58 100644 --- a/parkingLotItem.h +++ b/parkingLotManager.h @@ -1,27 +1,33 @@ #ifndef PARKINGLOTITEM_H #define PARKINGLOTITEM_H -#include #include #include #include +#include #include "car.h" +#include "ParkingLotWidget.h" +#include "parkinglotgraph.h" -class ParkingLotItem: public QObject, public QGraphicsPixmapItem +class ParkingLotManager: public QObject { Q_OBJECT public: - explicit ParkingLotItem(QGraphicsPixmapItem* itemParent, QObject* objectParent); + explicit ParkingLotManager(QObject* objectParent, QGraphicsScene* scene); void showParkingLot(uint pos); void showParkingLot(void); void showDownStairFloor(void); void showUpStairFloor(void); + void drawPath(uint n1, uint n2); + private: - QVector m_pixmap; // 每一层的pixmap + QGraphicsScene* m_scene; + QVector m_widgets; // 每一层的widget QVector m_capacity; // 每一层的容量 QVector m_num_of_cars; // 每一层当前停的车数量 QVector > m_cars; // 停在每一层的车 QVector >m_waitting; // 在每个入口等待的车 + QVector m_graph; QStringList m_xml_path; // 每一层xml文件地址 QStringList m_name; // 每一层的名字 const uint m_num_of_layer = 2; // 总层数 diff --git a/parkinglot_f1.xml b/parkinglot_f1.xml index 01f4020..cd2c7a7 100644 --- a/parkinglot_f1.xml +++ b/parkinglot_f1.xml @@ -12,31 +12,30 @@ - - + + - - + + - + - + - + - - + + - - + diff --git a/parkinglotgraph.cpp b/parkinglotgraph.cpp new file mode 100644 index 0000000..85bb86c --- /dev/null +++ b/parkinglotgraph.cpp @@ -0,0 +1,163 @@ +#include "parkinglotgraph.h" +#include "Road.h" +#include +#include +#include +#include +#include +#include + +bool inRect(const QWidget* r, const QPoint& p) { + QPoint ppp = r->pos(); + int w = r->width(); + int h = r->height(); + if (p.x() > r->pos().x() && p.x() < r->pos().x() + r->width() && + p.y() > r->pos().y() && p.y() < r->pos().y() + r->height()) + return true; + else + return false; +} + +QPoint toCenter(const QWidget* w) { + QPoint p; + p.setX(w->pos().x() + w->width() / 2); + p.setY(w->pos().y() + w->height() / 2); + return p; +} + +ParkingLotGraph::ParkingLotGraph(const ParkingLotWidget* pk) +{ + QVector spaces = pk->getSpaceList(); + QVector roads = pk->getRoadList(); + m_roadNodeList.resize(roads.size() * 2); // 每一条路有两个节点,分别为2i和2i+1 + for (int i = 0; i < roads.size(); i++) { // 初始化每条路的节点 + QPoint p; + Road* r = roads.at(i); + if (r->getDir() == Road::horizontal) { + p.setX(r->pos().x() + 5); + p.setY(r->pos().y() + r->height() / 2); + m_roadNodeList[2*i] = new Node(p); + p.setX(r->pos().x() + r->width() - 5); + m_roadNodeList[2*i + 1] = new Node(p); + } else { + p.setY(r->pos().y() + 5); + p.setX(r->pos().x() + r->width() / 2); + m_roadNodeList[2*i] = new Node(p); + p.setY(r->pos().y() + r->height() - 5); + m_roadNodeList[2*i+1] = new Node(p); + } + } + for (int i = 0; i < roads.size(); i++) { // 再遍历路,找到路与路的交点 + Road* r = roads.at(i); + QRect rrr = r->rect(); + QPoint p = r->pos(); + if (r->getDir() == Road::horizontal) { + p.setY(p.y() + r->height() / 2); + p.setX(p.x() - 30); // 向外找 + for (int j = 0; j < roads.size(); j++) { + if (i != j && inRect(roads.at(j), p)) { + m_roadNodeList[2*i]->addPath(m_roadNodeList[2*j]); + m_roadNodeList[2*i]->addPath(m_roadNodeList[2*j + 1]); + break; + } + } + p.setX(p.x() + r->width() + 60); // 补偿x减掉的30 + for (int j = 0; j < roads.size(); j++) { + if (i != j && inRect(roads.at(j), p)) { + m_roadNodeList[2*i+1]->addPath(m_roadNodeList[2*j]); + m_roadNodeList[2*i+1]->addPath(m_roadNodeList[2*j + 1]); + break; + } + } + } else { + p.setX(p.x() + r->width() / 2); + p.setY(p.y() - 30); // 向外找 + for (int j = 0; j < roads.size(); j++) { + if (i != j && inRect(roads.at(j), p)) { + m_roadNodeList[2*i]->addPath(m_roadNodeList[2*j]); + m_roadNodeList[2*i]->addPath(m_roadNodeList[2*j + 1]); + break; + } + } + p.setY(p.y() + r->height() + 60); + for (int j = 0; j < roads.size(); j++) { + if (i != j && inRect(roads.at(j), p)) { + m_roadNodeList[2*i+1]->addPath(m_roadNodeList[2*j]); + m_roadNodeList[2*i+1]->addPath(m_roadNodeList[2*j + 1]); + break; + } + } + } + } + for(ParkingSpaceWidget* sp : spaces) { + QPoint p = toCenter(sp), offset(0, 0); + Node* spaceNode = new Node(p); + m_spaceList.push_back(spaceNode); + switch(sp->getDir()) { + case ParkingSpaceWidget::N: offset.setY(-1);break; + case ParkingSpaceWidget::S: offset.setY(1);break; + case ParkingSpaceWidget::E: offset.setX(1);break; + case ParkingSpaceWidget::W: offset.setX(-1);break; + } + while(inRect(sp, p)) p += offset; // 直到走出这个widget + p+=offset*5; // 再走两步 + for (int i = 0; i < roads.size(); i++) { // 遍历所有路,找到与这个点相连的路,添加路径 + Road* r = roads.at(i); + if (inRect(r, p)) { + m_roadNodeList[2*i]->addPath(spaceNode); + m_roadNodeList[2*i + 1]->addPath(spaceNode); + break; + } + } + } + +} + +void ParkingLotGraph::paint(QGraphicsScene *scene) +{ + QVector vis(m_spaceList.size() + m_roadNodeList.size()); + QStack stack; + QPen pen(Qt::blue); + qDebug() << "共有" << QString::number(m_spaceList.size()) << "个车位节点"; + qDebug() << "共有" << QString::number(m_roadNodeList.size()) << "个路节点"; + for (int i = 0; i < m_roadNodeList.size(); i++) { + qDebug() << i << " " << m_roadNodeList.at(i)->adjacent.size(); + } + stack.push(m_spaceList.first()); + while (!stack.empty()) { + Node* n = stack.pop(); + if (vis.indexOf(n) != -1) + continue; + vis.push_back(n); + // qDebug() << "关联节点共有" << QString::number(n->adjacent.size()); + scene->addEllipse(n->data.x(), n->data.y(), 5, 5, pen); + for(Node* adj: n->adjacent) { + if (vis.indexOf(adj) != -1) + continue; + stack.push(adj); + scene->addLine(n->data.x(), n->data.y(), adj->data.x(), adj->data.y(), pen); + } + } +} + +ParkingLotGraph::~ParkingLotGraph() +{ + for (Node* n: m_spaceList) + delete n; + for (Node* n: m_roadNodeList) + delete n; +} + +void ParkingLotGraph::Node::addPath(ParkingLotGraph::Node *another) +{ + for (Node* adj: adjacent) + if (adj == another) + return; + adjacent.push_back(another); + another->adjacent.push_back(this); + int dx = data.x() - another->data.x(); + int dy = data.y() - another->data.y(); + float w = qSqrt(dx * dx + dy * dy); + weight.push_back(w); + another->weight.push_back(w); +} diff --git a/parkinglotgraph.h b/parkinglotgraph.h new file mode 100644 index 0000000..32d9938 --- /dev/null +++ b/parkinglotgraph.h @@ -0,0 +1,28 @@ +#ifndef PARKINGLOTGRAPH_H +#define PARKINGLOTGRAPH_H + +#include "Road.h" +#include "ParkingSpaceWidget.h" +#include +#include +#include + +class ParkingLotGraph +{ +public: + struct Node{ + QPoint data; + QVector adjacent; + QVector weight; + Node(QPoint p): data(p) {} + void addPath(Node* another); + }; + ParkingLotGraph(const ParkingLotWidget*); + void paint(QGraphicsScene* scene); + ~ParkingLotGraph(); +private: + QVector m_spaceList; + QVector m_roadNodeList; +}; + +#endif // PARKINGLOTGRAPH_H