diff --git a/DataStructureProject.pro b/DataStructureProject.pro index 15353a8..96a94f1 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -33,7 +33,6 @@ SOURCES += \ ParkingSpaceWidget.cpp \ monitor.cpp \ boundary.cpp \ - carmananger.cpp \ car.cpp \ path.cpp \ parkingLotManager.cpp \ @@ -50,7 +49,6 @@ HEADERS += \ ParkingSpaceWidget.h \ monitor.h \ boundary.h \ - carmananger.h \ car.h \ path.h \ parkingLotManager.h \ diff --git a/car.cpp b/car.cpp index dd35a46..8ba105d 100644 --- a/car.cpp +++ b/car.cpp @@ -32,7 +32,7 @@ Car::Car(ParkingLotManager * manager, QGraphicsItem *parent, int dir, Car::Color } m_pic = m_pic.scaled(M_WID, M_LEN); this->setPixmap(m_pic); - setTransformOriginPoint(M_WID, M_LEN/2); //设置旋转中心 + setTransformOriginPoint(M_WID/2, M_LEN/2); //设置旋转中心 connect(posAni,QPropertyAnimation::finished,this,followPath); connect(this, &Car::queueHead, manager, &ParkingLotManager::requestIn); connect(this, &Car::exit, manager, &ParkingLotManager::leave); @@ -101,7 +101,7 @@ void Car::setPath(Path *path) m_target = path->getNext(); m_current = m_target; m_target.action = Road::none; - this->setPos(m_target.point); + this->setPos(m_target.point - QPoint(20, 30)); this->setRotation(m_target.dir+90); } @@ -121,6 +121,8 @@ void Car::moveTo(QPointF target) qreal dy = m_current.point.y()-m_target.point.y(); qreal dis = qSqrt(qPow(dx,2)+qPow(dy,2)); qreal dur = dis/0.3; + target.setX(target.x() - 20); + target.setY(target.y() - 30); posAni->setDuration(dur); posAni->setEndValue(target); posAni->start(); @@ -155,6 +157,9 @@ void Car::followPath() m_current = m_target; m_target = m_path->getNext(); moveTo(m_target.point); + } else { + delete m_path; + m_path = nullptr; } } diff --git a/carmananger.cpp b/carmananger.cpp deleted file mode 100644 index bae05a9..0000000 --- a/carmananger.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "carmananger.h" - -CarMananger::CarMananger() -{ - -} diff --git a/carmananger.h b/carmananger.h deleted file mode 100644 index 54da76b..0000000 --- a/carmananger.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef CARMANANGER_H -#define CARMANANGER_H - -#include "car.h" -#include "path.h" - - -class CarMananger -{ -public: - CarMananger(); -}; - -#endif // CARMANANGER_H diff --git a/parkingLotManager.cpp b/parkingLotManager.cpp index 9bd8440..e5362eb 100644 --- a/parkingLotManager.cpp +++ b/parkingLotManager.cpp @@ -49,7 +49,6 @@ ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scen widget->hide(); m_widgets.push_back(widget); m_scene->addWidget(widget); - m_scene->setSceneRect(widget->rect()); // 固定位置 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); @@ -81,6 +80,7 @@ void ParkingLotManager::showParkingLot(uint pos) else c->show(); } + m_scene->setSceneRect(m_widgets[pos]->rect()); // 固定位置 emit setCapacity(QString::number(m_capacity.at(pos))); emit setLoad(QString::number(m_capacity.at(pos) - m_num_of_cars[pos])); emit setLayerName(m_name.at(pos)); @@ -200,6 +200,8 @@ void ParkingLotManager::requestIn(Car* car) car->followPath(); car->setStartTime(QTime::currentTime()); car->setStatus(Car::parking); + m_num_of_cars[l] += 1; + emit setLoad(QString::number(m_capacity.at(m_current_floor) - m_num_of_cars[m_current_floor])); } else { Log::i("请求失败,车位已满"); } @@ -212,10 +214,11 @@ 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])); // 更新剩余车位 car->hide(); m_all_cars.removeOne(car); delete car; + m_num_of_cars[l] -= 1; + emit setLoad(QString::number(m_capacity.at(m_current_floor) - m_num_of_cars[m_current_floor])); } void ParkingLotManager::addCar(int entry) @@ -223,6 +226,9 @@ void ParkingLotManager::addCar(int entry) Log::i("生成车"); if (entry == -1) entry = (rand() % m_num_of_entry); + + entry = 0; + Car *car = new Car(this); car->setEntryNum(entry + 1); m_waitting[entry].append(car); diff --git a/path.cpp b/path.cpp index 3355a71..56a1d5b 100644 --- a/path.cpp +++ b/path.cpp @@ -2,7 +2,12 @@ Path::Path() { + // path.append(PathPoint()); +} +Path::Path(int) +{ + path.append(PathPoint()); } //用PathPoint数组初始化Path diff --git a/path.h b/path.h index 42a1661..4b5ae8a 100644 --- a/path.h +++ b/path.h @@ -10,9 +10,9 @@ struct PathPoint { QPointF point; qreal dir = 0; - Road::Action action; + Road::Action action = Road::Action::none; int pointId; // 为了做直角生成的辅助节点的id为-1 - PathPoint() {} + PathPoint(): point(QPoint(0, 0)), pointId(-2) {} PathPoint(QPointF p, qreal d, Road::Action a=Road::Action::none, int id=-1): point(p), action(a), pointId(id) { while (d < 0 || d > 360) { @@ -36,6 +36,7 @@ class Path { public: Path(); + Path(int); Path(PathPoint points[], int i); PathPoint getPoint(int i); void addPoint(int i, const PathPoint &point); @@ -46,8 +47,6 @@ public: private: QList path; - int len; - int i; }; #endif // PATH_H