diff --git a/ParkingLotWidget.cpp b/ParkingLotWidget.cpp index d341c99..1a64fdc 100644 --- a/ParkingLotWidget.cpp +++ b/ParkingLotWidget.cpp @@ -80,7 +80,7 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) else layout = new QVBoxLayout; layout->setSizeConstraint(QLayout::SetFixedSize); - qDebug() << "处理" << element.tagName(); + // qDebug() << "处理" << element.tagName(); QDomElement child = element.firstChild().toElement(); while (!child.isNull()) @@ -88,7 +88,7 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) QString name = child.tagName(); if (name == "road") { - qDebug() << "处理road"; + // qDebug() << "处理road"; Road *r; const int l = child.attribute("length").toInt(); if (child.attribute("direction") == "vertical") @@ -107,7 +107,7 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) } else if (name == "room") { - qDebug() << "处理room"; + // qDebug() << "处理room"; const auto n = child.attribute("number", "1").toInt(); layout->addLayout(ParkingSpaceWidget::makeParkingSapceGroup( this, child.attribute("direction"), n, child.attribute("expendDirection"))); @@ -115,7 +115,7 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) else if ((name.at(0) == 'v' || name.at(0) == 'h' )&& name.contains("Layout")) layout->addLayout(parseLayout(child)); else if (name == "space") { - qDebug() << "处理space"; + // qDebug() << "处理space"; QSpacerItem* sw = new QSpacerItem(0, 0); if (element.tagName() == "hLayout") sw->changeSize(child.attribute("length").toInt(), 0, QSizePolicy::Fixed, QSizePolicy::Minimum); @@ -123,7 +123,7 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) sw->changeSize(0, child.attribute("length").toInt(), QSizePolicy::Minimum, QSizePolicy::Fixed); layout->addSpacerItem(sw); } else if (name == "boundary") { - qDebug() << "处理boundary"; + // qDebug() << "处理boundary"; int l = child.attribute("length").toInt(); Boundary *b = new Boundary(child.attribute("direction"), l, this); layout->addWidget(b); @@ -152,3 +152,9 @@ const QVector& ParkingLotWidget::getRoadList() const { return m_roadList; } + +void ParkingLotWidget::showMarginSlot(bool b) +{ + // qDebug() << "Parkinglot Widget: show margin " << b; + emit showMargain(b); +} diff --git a/ParkingLotWidget.h b/ParkingLotWidget.h index 8737c2c..b46a92b 100644 --- a/ParkingLotWidget.h +++ b/ParkingLotWidget.h @@ -22,6 +22,7 @@ public: QString getName() const; const QVector& getSpaceList() const; const QVector& getRoadList() const; + void showMarginSlot(bool b); private: QVector m_spaceList; @@ -30,6 +31,9 @@ private: QBoxLayout *parseLayout(const QDomElement& element); QBoxLayout *layout = nullptr; QString name; + +signals: + void showMargain(bool); }; #endif diff --git a/ParkingSpaceWidget.cpp b/ParkingSpaceWidget.cpp index 7f0cbc1..650708d 100644 --- a/ParkingSpaceWidget.cpp +++ b/ParkingSpaceWidget.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "ParkingLotWidget.h" ParkingSpaceWidget::ParkingSpaceWidget(ParkingLotWidget* parent, const QString &dir): QWidget(parent) @@ -37,6 +38,10 @@ ParkingSpaceWidget::ParkingSpaceWidget(ParkingLotWidget* parent, const QString & } number = parent->addSpace(this); setToolTip(QString::number(number)); + QObject::connect(parent, &ParkingLotWidget::showMargain, [this](bool b){ + this->m_showMargin = b; + update(); + }); } QBoxLayout *ParkingSpaceWidget::makeParkingSapceGroup(ParkingLotWidget* parent, const QString& dir, int n, const QString & expend) @@ -88,9 +93,11 @@ void ParkingSpaceWidget::paintEvent(QPaintEvent *) painter.drawRect(0, 0, 60, 5); painter.drawRect(0, 5, 10, 35); painter.drawRect(10, 35, 50, 5); - /*painter.setPen(Qt::blue); - painter.setBrush(Qt::NoBrush); - painter.drawRect(0, 0, this->width() - 1, this->height() - 1);*/ + if (m_showMargin) { + painter.setPen(Qt::blue); + painter.setBrush(Qt::NoBrush); + painter.drawRect(0, 0, width() - 1, height() - 1); + } //painter.restore(); //painter.setPen(Qt::black); diff --git a/ParkingSpaceWidget.h b/ParkingSpaceWidget.h index 414b767..f63dc31 100644 --- a/ParkingSpaceWidget.h +++ b/ParkingSpaceWidget.h @@ -4,6 +4,7 @@ #include #include #include "ParkingLotWidget.h" +#include "MyEvent.h" class ParkingLotWidget; class ParkingSpaceWidget: public QWidget @@ -20,6 +21,7 @@ public: protected: void paintEvent(QPaintEvent* ) override; private: + bool m_showMargin = false; int number; direction dir; // QLabel *m_float_window = nullptr; diff --git a/Road.cpp b/Road.cpp index fe7afa8..b196e85 100644 --- a/Road.cpp +++ b/Road.cpp @@ -21,6 +21,10 @@ Road::Road(ParkingLotWidget* parent, const int length, const direction dir) } // setContentsMargins(0, 0, 0, 0); number = parent->addRoad(this); + QObject::connect(parent, &ParkingLotWidget::showMargain, [this](bool b){ + this->m_showMargin = b; + update(); + }); } void Road::paintEvent(QPaintEvent * event) @@ -44,9 +48,11 @@ void Road::paintEvent(QPaintEvent * event) y += 50; } } - //painter.setPen(Qt::red); - //painter.setBrush(Qt::NoBrush); - //painter.drawRect(0, 0, width() - 1, height() - 1); + if (m_showMargin) { + painter.setPen(Qt::blue); + painter.setBrush(Qt::NoBrush); + painter.drawRect(0, 0, width() - 1, height() - 1); + } } Road::~Road() diff --git a/Road.h b/Road.h index 0ef4b8e..eb1a81b 100644 --- a/Road.h +++ b/Road.h @@ -28,5 +28,6 @@ private: uint number; Action action = none; short actionPos = 0; // 1为左上端,2为右下端 + bool m_showMargin = false; }; diff --git a/monitor.cpp b/monitor.cpp index 7259c6c..388da7b 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -18,7 +18,8 @@ Monitor::Monitor(QWidget *parent) : 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->showMargin, &QCheckBox::toggled, pk, &ParkingLotManager::showMargin); + QObject::connect(ui->showGraph, &QCheckBox::toggled, pk, &ParkingLotManager::showGraph); QObject::connect(ui->drawPath, &QPushButton::clicked, [=](){ pk->drawPath(ui->placeNum1->text().toInt(), ui->placeNum2->text().toInt()); }); diff --git a/monitor.ui b/monitor.ui index c7c1c46..0b331f3 100644 --- a/monitor.ui +++ b/monitor.ui @@ -34,7 +34,7 @@ - 13 + 10 10 1061 881 @@ -67,7 +67,7 @@ - + QLayout::SetDefaultConstraint @@ -243,6 +243,31 @@ + + + + DEBUG: + + + + + + + + + 显示布局边界 + + + + + + + 显示图 + + + + + diff --git a/parkingLotManager.cpp b/parkingLotManager.cpp index 59c262f..73a6027 100644 --- a/parkingLotManager.cpp +++ b/parkingLotManager.cpp @@ -2,11 +2,13 @@ #include "ParkingLotWidget.h" #include "ParkingSpaceWidget.h" #include "path.h" +#include "MyEvent.h" #include #include #include #include #include +#include ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scene): QObject(objectParent), m_scene(scene) @@ -24,7 +26,7 @@ ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scen } m_cars.resize(m_num_of_layer); // 停在每一层的车 m_waitting.resize(m_num_of_entry); // 在每个入口等待的车 - for (uint i = 0; i < m_num_of_layer; i++) { + 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(); @@ -36,10 +38,9 @@ ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scen 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); + m_graph.push_back(new ParkingLotGraph(widget)); + m_graph_pixmap.push_back(new QGraphicsPixmapItem(*m_graph.last()->getPixmap())); + QObject::connect(this, &ParkingLotManager::showMarginSignal, widget, &ParkingLotWidget::showMarginSlot); } } @@ -52,11 +53,15 @@ void ParkingLotManager::showParkingLot(uint pos) for (uint i = 0; i < m_num_of_layer; i++) m_widgets.at(i)->hide(); m_widgets.at(pos)->show(); + for (QGraphicsPixmapItem* p: m_graph_pixmap) + if (m_scene->items().contains(p)) + m_scene->removeItem(p); + if (m_showGraph) { + m_scene->addItem(m_graph_pixmap.at(m_current_floor)); + } 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() @@ -101,3 +106,22 @@ void ParkingLotManager::drawPath(int n1, int n2) m_scene->addLine(line); } } + +void ParkingLotManager::showMargin(bool enable) +{ + // qDebug() << "show margin" << enable; + emit showMarginSignal(enable); +} + +void ParkingLotManager::showGraph(bool enable) +{ + // qDebug() << "show graph" << enable; + m_showGraph = enable; + showParkingLot(); +} + +void ParkingLotManager::clearScene() +{ + qDebug() << "清空scene"; + m_scene->clear(); +} diff --git a/parkingLotManager.h b/parkingLotManager.h index 612027b..efa90c6 100644 --- a/parkingLotManager.h +++ b/parkingLotManager.h @@ -20,6 +20,9 @@ public: void showUpStairFloor(void); void drawPath(int n1, int n2); + void showMargin(bool enable); + void showGraph(bool enable); + private: QGraphicsScene* m_scene; QVector m_widgets; // 每一层的widget @@ -28,17 +31,21 @@ private: QVector > m_cars; // 停在每一层的车 QVector >m_waitting; // 在每个入口等待的车 QVector m_graph; + QVector m_graph_pixmap; QStringList m_xml_path; // 每一层xml文件地址 QStringList m_name; // 每一层的名字 const uint m_num_of_layer = 2; // 总层数 const uint m_num_of_entry = 2; // 入口数 uint m_current_floor = 1; // 当前楼层,默认为1楼(按照从下往上递增,值与实际楼层无关) + bool m_showGraph = false; // 是否显示图 + void clearScene(); signals: void enableUpButton(bool); void enableDownButton(bool); void setCapacity(const QString&); void setLoad(const QString&); void setLayerName(const QString&); + void showMarginSignal(bool); }; #endif // PARKINGLOTITEM_H diff --git a/parkinglot_f1.xml b/parkinglot_f1.xml index d7ec0d0..6055f8b 100644 --- a/parkinglot_f1.xml +++ b/parkinglot_f1.xml @@ -45,7 +45,7 @@ - + diff --git a/parkinglotgraph.cpp b/parkinglotgraph.cpp index b4dcdab..a859ef4 100644 --- a/parkinglotgraph.cpp +++ b/parkinglotgraph.cpp @@ -7,6 +7,7 @@ #include #include #include +#include bool inRect(const QWidget* r, const QPoint& p) { if (p.x() > r->pos().x() && p.x() < r->pos().x() + r->width() && @@ -252,10 +253,49 @@ Path *ParkingLotGraph::finaPath(ParkingLotGraph::Node::Type t1, int n1, ParkingL ParkingLotGraph::~ParkingLotGraph() { - for (Node* n: m_spaceList) - delete n; - for (Node* n: m_roadNodeList) + for (Node* n: m_all) delete n; + delete m_pixmap; +} + +QPixmap *ParkingLotGraph::getPixmap() +{ + if (m_pixmap == nullptr) + generatePixmap(); + return m_pixmap; +} + +void ParkingLotGraph::generatePixmap() +{ + m_pixmap = new QPixmap(pk->size()); + m_pixmap->fill(Qt::transparent); + QPainter painter(m_pixmap); + QVector vis(m_spaceList.size() + m_roadNodeList.size()); + QStack stack; + painter.setPen(Qt::red); +// 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()); + if (n->action != Road::Action::none) { + painter.setBrush(Qt::red); + painter.drawEllipse(n->data.x(), n->data.y(), 5, 5); + painter.setBrush(Qt::NoBrush); + } else + painter.drawEllipse(n->data.x(), n->data.y(), 5, 5); + for(Node* adj: n->adjacent) { + if (vis.indexOf(adj) != -1) + continue; + stack.push(adj); + painter.drawLine(n->data.x(), n->data.y(), adj->data.x(), adj->data.y()); + } + } } diff --git a/parkinglotgraph.h b/parkinglotgraph.h index 35d1ae0..ff41ac2 100644 --- a/parkinglotgraph.h +++ b/parkinglotgraph.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "Road.h" class ParkingLotGraph @@ -38,15 +39,20 @@ public: return no; } void paint(QGraphicsScene* scene); + Path* finaPath(Node::Type t1, int n1, Node::Type t2, int n2); uint getNodeId(Node::Type t, int n); ~ParkingLotGraph(); + QPixmap *getPixmap(); + private: - QVector m_all; - QVector m_spaceList; - QVector m_roadNodeList; - QMap m_actionList; - const ParkingLotWidget* pk; + QVector m_all; + QVector m_spaceList; + QVector m_roadNodeList; + QMap m_actionList; + QPixmap* m_pixmap = nullptr; + void generatePixmap(); + const ParkingLotWidget* pk; }; #endif // PARKINGLOTGRAPH_H