From 326c99756fe3498091253af258a36a00acbee07d Mon Sep 17 00:00:00 2001 From: w-mj Date: Fri, 22 Dec 2017 20:04:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8C=E5=87=BB=E7=A6=81=E7=94=A8=E5=81=9C?= =?UTF-8?q?=E8=BD=A6=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ParkingLotWidget.cpp | 4 ++++ ParkingLotWidget.h | 2 ++ ParkingSpaceWidget.cpp | 23 +++++++++++++++++++++++ ParkingSpaceWidget.h | 3 ++- parkingLotManager.cpp | 23 +++++++++++++++-------- parkingLotManager.h | 16 +++++++++++++--- 6 files changed, 59 insertions(+), 12 deletions(-) diff --git a/ParkingLotWidget.cpp b/ParkingLotWidget.cpp index 055766b..77bbd6d 100644 --- a/ParkingLotWidget.cpp +++ b/ParkingLotWidget.cpp @@ -9,6 +9,7 @@ #include #include + ParkingLotWidget::ParkingLotWidget(QWidget *parent, const QString& xml) : QWidget(parent) { parse_xml(xml); @@ -16,6 +17,7 @@ ParkingLotWidget::ParkingLotWidget(QWidget *parent, const QString& xml) : QWidge // resize(640, 480); setLayout(layout); adjustSize(); // 调整大小至刚刚可以装下所有元素 + } @@ -160,3 +162,5 @@ void ParkingLotWidget::showMarginSlot(bool b) // qDebug() << "Parkinglot Widget: show margin " << b; emit showMargain(b); } + + diff --git a/ParkingLotWidget.h b/ParkingLotWidget.h index b46a92b..7512132 100644 --- a/ParkingLotWidget.h +++ b/ParkingLotWidget.h @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include "Road.h" #include "ParkingSpaceWidget.h" class ParkingSpaceWidget; // 先声明类,防止循环include diff --git a/ParkingSpaceWidget.cpp b/ParkingSpaceWidget.cpp index 650708d..0de202c 100644 --- a/ParkingSpaceWidget.cpp +++ b/ParkingSpaceWidget.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include "ParkingLotWidget.h" ParkingSpaceWidget::ParkingSpaceWidget(ParkingLotWidget* parent, const QString &dir): QWidget(parent) @@ -98,6 +100,17 @@ void ParkingSpaceWidget::paintEvent(QPaintEvent *) painter.setBrush(Qt::NoBrush); painter.drawRect(0, 0, width() - 1, height() - 1); } + if (m_banned) { + QPen p; + p.setBrush(Qt::black); + p.setWidth(4); + p.setCapStyle(Qt::RoundCap); + painter.setPen(p); + painter.setBrush(QColor(240, 172, 47)); + painter.setRenderHint(QPainter::Antialiasing, true); + painter.drawEllipse(15, 5, 26, 26); + painter.drawRect(17, 17, 24, 2); + } //painter.restore(); //painter.setPen(Qt::black); @@ -115,6 +128,16 @@ ParkingSpaceWidget::direction ParkingSpaceWidget::getDir() const return dir; } +void ParkingSpaceWidget::mouseDoubleClickEvent(QMouseEvent *) +{ + if (m_banned) + qDebug() << "禁用解除" << number; + else + qDebug() << "禁用车位" << number; + m_banned = !m_banned; + update(); +} + ParkingSpaceWidget::~ParkingSpaceWidget() { } diff --git a/ParkingSpaceWidget.h b/ParkingSpaceWidget.h index d900775..4915b56 100644 --- a/ParkingSpaceWidget.h +++ b/ParkingSpaceWidget.h @@ -15,6 +15,7 @@ public: static QBoxLayout* makeParkingSapceGroup(ParkingLotWidget* parent, const QString& dir, int n, const QString &expend); ~ParkingSpaceWidget(); direction getDir() const; + void mouseDoubleClickEvent(QMouseEvent *event); int getNumber() const; protected: @@ -23,7 +24,7 @@ private: bool m_showMargin = false; int number; direction dir; - // QLabel *m_float_window = nullptr; + bool m_banned = false; }; #endif diff --git a/parkingLotManager.cpp b/parkingLotManager.cpp index e6ba5b8..885be3d 100644 --- a/parkingLotManager.cpp +++ b/parkingLotManager.cpp @@ -33,7 +33,7 @@ ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scen 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 + // m_num_of_cars.push_back(0); // 初始停车数量为0 widget->hide(); m_widgets.push_back(widget); m_scene->addWidget(widget); @@ -59,7 +59,7 @@ void ParkingLotManager::showParkingLot(uint pos) 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 setLoad(QString::number(m_capacity.at(pos) - m_cars.at(pos).size())); emit setLayerName(m_name.at(pos)); } @@ -114,6 +114,19 @@ void ParkingLotManager::drawPath(int n1, int n2) } } +ParkingLotManager::RequestSpace ParkingLotManager::request(uint entry) +{ + for (int layer = m_num_of_layer - 1; layer >= 0; layer--) { + if (m_cars[layer].size() != m_capacity[layer]) { + int n = m_cars[layer].size(); + Path* p = m_graph[layer]->finaPath(ParkingLotGraph::Node::Type::entry, entry, + ParkingLotGraph::Node::Type::space, n); + return RequestSpace(layer, n, p); + } + } + return RequestSpace(); +} + void ParkingLotManager::showMargin(bool enable) { // qDebug() << "show margin" << enable; @@ -126,9 +139,3 @@ void ParkingLotManager::showGraph(bool enable) m_showGraph = enable; showParkingLot(); } - -void ParkingLotManager::clearScene() -{ - qDebug() << "清空scene"; - m_scene->clear(); -} diff --git a/parkingLotManager.h b/parkingLotManager.h index efa90c6..2c2a81d 100644 --- a/parkingLotManager.h +++ b/parkingLotManager.h @@ -14,11 +14,21 @@ class ParkingLotManager: public QObject Q_OBJECT public: explicit ParkingLotManager(QObject* objectParent, QGraphicsScene* scene); + struct RequestSpace { + bool success = false; + uint layer = 0; + uint number = 0; + Path* path = Q_NULLPTR; + RequestSpace() {} + RequestSpace(uint l, uint n, Path* p):success(true), layer(l), number(n), path(p) {} + RequestSpace(const RequestSpace& another) = default; + }; void showParkingLot(uint pos); void showParkingLot(void); void showDownStairFloor(void); void showUpStairFloor(void); void drawPath(int n1, int n2); + RequestSpace request(uint entry); void showMargin(bool enable); void showGraph(bool enable); @@ -27,9 +37,9 @@ private: QGraphicsScene* m_scene; QVector m_widgets; // 每一层的widget QVector m_capacity; // 每一层的容量 - QVector m_num_of_cars; // 每一层当前停的车数量 + // QVector m_num_of_cars; // 每一层当前停的车数量 QVector > m_cars; // 停在每一层的车 - QVector >m_waitting; // 在每个入口等待的车 + QVector >m_waitting; // 在每个入口等待的车 QVector m_graph; QVector m_graph_pixmap; QStringList m_xml_path; // 每一层xml文件地址 @@ -38,7 +48,7 @@ private: 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);