#include "parkingLotManager.h"
#include "ParkingLotWidget.h"
#include "ParkingSpaceWidget.h"
#include "path.h"
#include <QDebug>
#include <QPainter>
#include <QPoint>
#include <QRect>
#include <QGraphicsScene>
#include <QApplication>
#include "logwindow.h"
#include "car.h"
#include "carlist.h"
#define GET_SITUATION(l, n) (m_widgets[l]->getSpaceList().at(n)->getSituation())
#define BANNED (ParkingSpaceWidget::Situation::banned)
#define OCCUPIED (ParkingSpaceWidget::Situation::occupied)
#define FREE (ParkingSpaceWidget::Situation::free)
#define NODE_TYPE ParkingLotGraph::Node::Type
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=" <<widget->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);
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);
QObject::connect(widget, &ParkingLotWidget::banParkingSpace, [i, this](bool b, int n){banSpace(b, i, n);});
}
generatePool(true);
Adapter *adapter = new Adapter(objectParent, &m_all_cars); // 注册model
CarList::getInstance()->setAdapter(adapter);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &ParkingLotManager::periodWork);
QTimer* priceTimer = new QTimer(this);
connect(priceTimer, &QTimer::timeout, this, &ParkingLotManager::requestPrice);
priceTimer->start(1000);
}
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();
qDebug()<<(QString("LINE:%1").arg(__LINE__));
m_widgets.at(pos)->show();
for (QGraphicsPixmapItem* p: m_graph_pixmap)
if (m_scene->items().contains(p))
m_scene->removeItem(p);
if (m_showGraph) {
qDebug()<<(QString("LINE:%1").arg(__LINE__));
m_scene->addItem(m_graph_pixmap.at(m_current_floor));
}
for (auto c: m_all_cars) {
if (c->getCurrentFloor() != static_cast<int>(pos))
c->hide();
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));
}
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);
}
ParkingLotGraph::Node::Type getType(int n) {
if (n > 0)
return ParkingLotGraph::Node::Type::space;
else
return ParkingLotGraph::Node::Type::act;
}
void ParkingLotManager::drawPath(int n1, int n2)
{
if (n1 < -8 || n1 > 104 || n2 < -8 || n2 > 104)
return ;
QPen pen(QColor(242, 23, 242));
pen.setWidth(3);
Path *p = m_graph[m_current_floor]->findPath(getType(n1), n1, getType(n2), n2);
for(int i = 1; i < p->pointsCount(); i++) {
QLineF line(p->getPoint(i-1).point, p->getPoint(i).point);
QPointF dir(qCos(qDegreesToRadians(p->getPoint(i-1).dir)) * 10,
qSin(qDegreesToRadians(p->getPoint(i-1).dir)) * 10);
QLineF dirLine(p->getPoint(i-1).point, p->getPoint(i-1).point + dir);
m_scene->addLine(line);
m_scene->addLine(dirLine, pen);
m_scene->addText(QString::number(p->getPoint(i-1).dir))->moveBy(p->getPoint(i-1).point.x(),
p->getPoint(i-1).point.y());
}
}
void ParkingLotManager::drawPath(Path* p)
{
QPen pen(QColor(242, 23, 242));
pen.setWidth(3);
for(int i = 1; i < p->pointsCount(); i++) {
QLineF line(p->getPoint(i-1).point, p->getPoint(i).point);
QPointF dir(qCos(qDegreesToRadians(p->getPoint(i-1).dir)) * 10,
qSin(qDegreesToRadians(p->getPoint(i-1).dir)) * 10);
QLineF dirLine(p->getPoint(i-1).point, p->getPoint(i-1).point + dir);
m_scene->addLine(line);
m_scene->addLine(dirLine, pen);
m_scene->addText(QString::number(p->getPoint(i-1).dir))->moveBy(p->getPoint(i-1).point.x(),
p->getPoint(i-1).point.y());
}
}
bool ParkingLotManager::checkSame(const QString &plate)
{
for (auto c: m_all_cars)
if (c->getPlateNumber() == plate)
return true;
return false;
}
QString ParkingLotManager::getParkingLotName(int pos)
{
return m_widgets.at(pos)->getName();
}
QPair<QString, int> ParkingLotManager::search(const QString& plate)
{
for (auto c: m_all_cars) {
if (c->getPlateNumber() == plate) {
if (c->getStatus() == Car::Status::parking ||
c->getStatus() == Car::Status::moving)
return qMakePair(m_widgets.at(c->getTargetFloor())->getName(),
c->getNum());
else if (c->getStatus() == Car::Status::waiting)
return qMakePair(QString("0"), 0);
}
}
return qMakePair(QString("-1"), -1);
}
bool comp2(const Car* c1, const Car* c2) {
return c1->getPlateNumber() < c2->getPlateNumber();
}
int comp(const QString& p1, const Car* c2) {
return strcmp(p1.toStdString().c_str(), c2->getPlateNumber().toStdString().c_str());
}
QPair<QString, int> ParkingLotManager::biSearch(const QString &plate)
{
sort(m_all_cars.begin(), m_all_cars.end(), &comp2);
int left = 0, right = m_all_cars.size(), mid = m_all_cars.size() / 2;
while (left != right) {
int flag = comp(plate, m_all_cars.at(mid));
if (flag == -1) {
right = mid;
} else if (flag == 1) {
left = mid;
} else if (flag == 0) {
Car* c = m_all_cars.at(mid);
if (c->getStatus() == Car::Status::parking ||
c->getStatus() == Car::Status::mov
没有合适的资源?快使用搜索试试~ 我知道了~
数据结构课设基于c++的校园停车导航系统源码.zip
共43个文件
cpp:15个
h:14个
ui:5个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 137 浏览量
2023-11-14
21:35:09
上传
评论 2
收藏 179KB ZIP 举报
温馨提示
【项目介绍】 数据结构课设基于c++的校园停车导航系统源码.zip数据结构课设基于c++的校园停车导航系统源码.zip数据结构课设基于c++的校园停车导航系统源码.zip数据结构课设基于c++的校园停车导航系统源码.zip数据结构课设基于c++的校园停车导航系统源码.zip数据结构课设基于c++的校园停车导航系统源码.zip数据结构课设基于c++的校园停车导航系统源码.zip数据结构课设基于c++的校园停车导航系统源码.zip数据结构课 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通。 2、项目适用人群:计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等计算机相关专业的在校学生、专业老师、行业内企业员工下载使用。 3、项目可用于:项目本身具有较高的学习借鉴价值,不仅适用于小白学习进阶,也可用于专业人员二次开发。当然也可作为毕设项目、课程设计、课程大作业、初期项目立项演示等。 4、如果基础还行,或者热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载使用,相互学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
数据结构课设基于c++的校园停车导航系统源码.zip (43个子文件)
logindialog.h 916B
boundary.h 332B
cars.qrc 201B
logwindow.cpp 2KB
carlist.ui 586B
mainwindow.h 635B
monitor.h 646B
carlist.h 1KB
logindialog.ui 3KB
car.h 3KB
monitor.cpp 5KB
parkingLotManager.h 3KB
ParkingLotWidget.h 1KB
mainwindow.cpp 2KB
parkinglotgraph.cpp 13KB
ParkingSpaceWidget.h 945B
parkinglot_f1.xml 2KB
ParkingSpaceWidget.cpp 4KB
ParkingLotWidget.cpp 5KB
carlist.cpp 3KB
main.cpp 1KB
Road.cpp 2KB
road.qrc 45B
DataStructureProject.pro 2KB
mainwindow.ui 1KB
logwindow.h 644B
utils.cpp 435B
parkingLotManager.cpp 16KB
path.h 1KB
redcar.png 45KB
yellowcar.png 47KB
parkinglot_b1.xml 1KB
parkinglotgraph.h 2KB
car.cpp 9KB
.gitignore 391B
boundary.cpp 734B
pinkcar.png 49KB
logindialog.cpp 2KB
Road.h 775B
utils.h 103B
logwindow.ui 2KB
monitor.ui 13KB
path.cpp 2KB
共 43 条
- 1
资源评论
北航程序员小C
- 粉丝: 2222
- 资源: 1823
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功