一点微小的工作
This commit is contained in:
@@ -9,7 +9,7 @@ class Road: public QWidget
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
enum direction{ horizontal, vertical};
|
enum direction{ horizontal, vertical};
|
||||||
enum Action{entry, stair, exit, queueHead, none};
|
enum Action{entry = 1, stair, exit, queueHead, none};
|
||||||
Road(ParkingLotWidget* parent, const int length, direction dir);
|
Road(ParkingLotWidget* parent, const int length, direction dir);
|
||||||
void paintEvent(QPaintEvent* event) override;
|
void paintEvent(QPaintEvent* event) override;
|
||||||
uint getNumber() const;
|
uint getNumber() const;
|
||||||
|
|||||||
+39
-4
@@ -15,6 +15,8 @@
|
|||||||
#define OCCUPIED (ParkingSpaceWidget::Situation::occupied)
|
#define OCCUPIED (ParkingSpaceWidget::Situation::occupied)
|
||||||
#define FREE (ParkingSpaceWidget::Situation::free)
|
#define FREE (ParkingSpaceWidget::Situation::free)
|
||||||
|
|
||||||
|
#define NODE_TYPE ParkingLotGraph::Node::Type
|
||||||
|
|
||||||
|
|
||||||
ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scene):
|
ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scene):
|
||||||
QObject(objectParent), m_scene(scene)
|
QObject(objectParent), m_scene(scene)
|
||||||
@@ -103,7 +105,7 @@ ParkingLotGraph::Node::Type getType(int n) {
|
|||||||
if (n > 0)
|
if (n > 0)
|
||||||
return ParkingLotGraph::Node::Type::space;
|
return ParkingLotGraph::Node::Type::space;
|
||||||
else
|
else
|
||||||
return ParkingLotGraph::Node::Type::entry;
|
return ParkingLotGraph::Node::Type::act;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParkingLotManager::drawPath(int n1, int n2)
|
void ParkingLotManager::drawPath(int n1, int n2)
|
||||||
@@ -123,20 +125,53 @@ void ParkingLotManager::drawPath(int n1, int n2)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ParkingLotManager::RequestSpace ParkingLotManager::request(uint entry)
|
void ParkingLotManager::requestOut(Car *car, int exit = -1)
|
||||||
|
{
|
||||||
|
if (exit != -1)
|
||||||
|
exit = rand() % m_num_of_entry;
|
||||||
|
Path *p; // TODO:请求出口
|
||||||
|
}
|
||||||
|
|
||||||
|
ParkingLotManager::RequestSpace ParkingLotManager::requestIn(Car* car)
|
||||||
{
|
{
|
||||||
if (!m_pool.empty()) {
|
if (!m_pool.empty()) {
|
||||||
int l = m_pool.first().first;
|
int l = m_pool.first().first;
|
||||||
int n = m_pool.first().second;
|
int n = m_pool.first().second;
|
||||||
m_pool.removeAt(0);
|
m_pool.removeAt(0);
|
||||||
Path* p = m_graph[l]->finaPath(ParkingLotGraph::Node::Type::entry, entry,
|
Path* p;
|
||||||
ParkingLotGraph::Node::Type::space, n);
|
if (l == m_current_floor)
|
||||||
|
p = m_graph[l]->finaPath(NODE_TYPE::queueHead, entry, NODE_TYPE::space, n);
|
||||||
|
else {
|
||||||
|
p = m_graph[m_current_floor]->findPath(NODE_TYPE::queueHead, entry, NODE_TYPE::stair, entry);
|
||||||
|
}
|
||||||
|
m_waitting.at(entry).removeFirst(); // 等候队列中的第一个移除
|
||||||
|
m_cars.at(l).at(n) = car;
|
||||||
Log::i(QString("分配第") + l + "层" + n + "号车位,剩余" + QString::number(m_pool.size()) + "个空车位");
|
Log::i(QString("分配第") + l + "层" + n + "号车位,剩余" + QString::number(m_pool.size()) + "个空车位");
|
||||||
|
|
||||||
return RequestSpace(l, n, p);
|
return RequestSpace(l, n, p);
|
||||||
|
} else {
|
||||||
|
Log::i("请求失败,车位已满");
|
||||||
}
|
}
|
||||||
return RequestSpace();
|
return RequestSpace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ParkingLotManager::leave(Car* car)
|
||||||
|
{
|
||||||
|
m_pool.append(qMakePair(l, n));
|
||||||
|
Log::i(QString("有车离开,当前共有%1个空车位").arg(m_pool.size()));
|
||||||
|
emit carLeave();
|
||||||
|
emit setLoad(QString::number(m_capacity.at(l) - m_num_of_cars[l])); // 更新剩余车位
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParkingLotManager::addCar(int entry)
|
||||||
|
{
|
||||||
|
if (entry == -1)
|
||||||
|
entry = rand() % m_num_of_entry;
|
||||||
|
Car *car = new Car(0, 0); // TODO: 修改构造函数
|
||||||
|
m_waitting.at(entry).append(car);
|
||||||
|
// TODO: 添加路径
|
||||||
|
}
|
||||||
|
|
||||||
void ParkingLotManager::showMargin(bool enable)
|
void ParkingLotManager::showMargin(bool enable)
|
||||||
{
|
{
|
||||||
// qDebug() << "show margin" << enable;
|
// qDebug() << "show margin" << enable;
|
||||||
|
|||||||
+8
-1
@@ -28,7 +28,12 @@ public:
|
|||||||
void showDownStairFloor(void);
|
void showDownStairFloor(void);
|
||||||
void showUpStairFloor(void);
|
void showUpStairFloor(void);
|
||||||
void drawPath(int n1, int n2);
|
void drawPath(int n1, int n2);
|
||||||
RequestSpace request(uint entry);
|
|
||||||
|
public slots:
|
||||||
|
void requestIn(Car* car);
|
||||||
|
void requestOut(Car* car, int exit = -1);
|
||||||
|
void leave(Car* car);
|
||||||
|
void addCar(int entry = -1);
|
||||||
|
|
||||||
void showMargin(bool enable);
|
void showMargin(bool enable);
|
||||||
void showGraph(bool enable);
|
void showGraph(bool enable);
|
||||||
@@ -60,6 +65,8 @@ signals:
|
|||||||
void setLoad(const QString&);
|
void setLoad(const QString&);
|
||||||
void setLayerName(const QString&);
|
void setLayerName(const QString&);
|
||||||
void showMarginSignal(bool);
|
void showMarginSignal(bool);
|
||||||
|
|
||||||
|
void carLeave(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PARKINGLOTITEM_H
|
#endif // PARKINGLOTITEM_H
|
||||||
|
|||||||
+12
-11
@@ -50,15 +50,14 @@ ParkingLotGraph::ParkingLotGraph(const ParkingLotWidget* pkl): pk(pkl)
|
|||||||
if (r->getAction() != Road::entry) {
|
if (r->getAction() != Road::entry) {
|
||||||
m_roadNodeList[2*i + r->getActionPos() - 1]->setAction(r->getAction()); // 添加动作
|
m_roadNodeList[2*i + r->getActionPos() - 1]->setAction(r->getAction()); // 添加动作
|
||||||
// 把有动作的节点添加到dict里,方便查找
|
// 把有动作的节点添加到dict里,方便查找
|
||||||
m_actionList.insert(m_roadNodeList[2*i + r->getActionPos() - 1]->getId(),
|
m_actionList.append(m_roadNodeList[2*i + r->getActionPos() - 1]);
|
||||||
m_roadNodeList[2*i + r->getActionPos() - 1]);
|
|
||||||
} else {
|
} else {
|
||||||
// 当前点为入口
|
// 当前点为入口
|
||||||
m_roadNodeList[2*i + r->getActionPos() - 1]->setAction(r->getAction());
|
m_roadNodeList[2*i + r->getActionPos() - 1]->setAction(r->getAction());
|
||||||
// 另一个点为队首
|
// 另一个点为队首
|
||||||
m_roadNodeList[2*i + 2 - r->getActionPos()]->setAction(Road::Action::queueHead);
|
m_roadNodeList[2*i + 2 - r->getActionPos()]->setAction(Road::Action::queueHead);
|
||||||
m_actionList.insert(m_roadNodeList[2 * i]->getId(), m_roadNodeList[2 * i]);
|
m_actionList.append(m_roadNodeList[2 * i]);
|
||||||
m_actionList.insert(m_roadNodeList[2 * i + 1]->getId(), m_roadNodeList[2 * i + 1]);
|
m_actionList.append(m_roadNodeList[2 * i + 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_roadNodeList[2*i]->addPath(m_roadNodeList[2*i + 1]); // 一条路的两个端点连接起来
|
m_roadNodeList[2*i]->addPath(m_roadNodeList[2*i + 1]); // 一条路的两个端点连接起来
|
||||||
@@ -170,12 +169,14 @@ void ParkingLotGraph::paint(QGraphicsScene *scene)
|
|||||||
uint ParkingLotGraph::getNodeId(ParkingLotGraph::Node::Type t, int n) {
|
uint ParkingLotGraph::getNodeId(ParkingLotGraph::Node::Type t, int n) {
|
||||||
if (t == Node::Type::space) {
|
if (t == Node::Type::space) {
|
||||||
return m_spaceList.at(n - 1)->getId();
|
return m_spaceList.at(n - 1)->getId();
|
||||||
} else if(t == Node::Type::entry) {
|
} else if (t == Node::Type::act) {
|
||||||
int k = 0;
|
return m_actionList.at(n - 1)->getId();
|
||||||
for (Node* node: m_actionList.values())
|
} else {
|
||||||
if (node->action==Road::Action::entry)
|
Road::Action act = static_cast<Road::Action>(t);
|
||||||
if (++k == n)
|
for (auto n: m_actionList) {
|
||||||
return node->id;
|
if (n->action == act && --n == 0)
|
||||||
|
return n->id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -229,7 +230,7 @@ Path *ParkingLotGraph::finaPath(ParkingLotGraph::Node::Type t1, int n1, ParkingL
|
|||||||
uint n = id2;
|
uint n = id2;
|
||||||
QPoint lastP;
|
QPoint lastP;
|
||||||
while (n != id1) {
|
while (n != id1) {
|
||||||
// 相邻两个点的xy都不想等,添加一个辅助顶点构成矩形
|
// 相邻两个点的xy都不相等,添加一个辅助顶点构成矩形
|
||||||
if (m_all[n]->data.x() != m_all[prev[n]]->data.x() &&
|
if (m_all[n]->data.x() != m_all[prev[n]]->data.x() &&
|
||||||
m_all[n]->data.y() != m_all[prev[n]]->data.y()) {
|
m_all[n]->data.y() != m_all[prev[n]]->data.y()) {
|
||||||
QPoint tp;
|
QPoint tp;
|
||||||
|
|||||||
+2
-2
@@ -16,7 +16,7 @@ class ParkingLotGraph
|
|||||||
public:
|
public:
|
||||||
struct Node{
|
struct Node{
|
||||||
public:
|
public:
|
||||||
enum Type{road, space, entry};
|
enum Type{entry = 1, stair, exit, queueHead, space, road, act};
|
||||||
Node(const QPoint& p, Type t, uint n, int idd) : number(n), id(idd), type(t), data(p) {}
|
Node(const QPoint& p, Type t, uint n, int idd) : number(n), id(idd), type(t), data(p) {}
|
||||||
void addPath(Node* another);
|
void addPath(Node* another);
|
||||||
uint number;
|
uint number;
|
||||||
@@ -49,7 +49,7 @@ private:
|
|||||||
QVector<Node* > m_all;
|
QVector<Node* > m_all;
|
||||||
QVector<Node* > m_spaceList;
|
QVector<Node* > m_spaceList;
|
||||||
QVector<Node* > m_roadNodeList;
|
QVector<Node* > m_roadNodeList;
|
||||||
QMap<uint, Node*> m_actionList;
|
QVector<Node* > m_actionList;
|
||||||
QPixmap* m_pixmap = nullptr;
|
QPixmap* m_pixmap = nullptr;
|
||||||
void generatePixmap();
|
void generatePixmap();
|
||||||
const ParkingLotWidget* pk;
|
const ParkingLotWidget* pk;
|
||||||
|
|||||||
Reference in New Issue
Block a user