From 37f3e02d886a2760dfbfd22dc33504db28913d21 Mon Sep 17 00:00:00 2001 From: wmj Date: Tue, 19 Dec 2017 17:24:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8scene=E6=94=B9=E5=86=99=E5=81=9C?= =?UTF-8?q?=E8=BD=A6=E8=BD=A6=E5=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataStructureProject.pro | 8 ++- ParkingLotWidget.cpp | 4 ++ ParkingLotWidget.h | 1 + carmananger.cpp | 6 ++ carmananger.h | 11 +++ monitor.cpp | 24 ++----- monitor.ui | 151 +++++++++++++++++++++++++++++++++------ parkingLotItem.cpp | 46 ++++++++++++ parkingLotItem.h | 27 +++++++ 9 files changed, 236 insertions(+), 42 deletions(-) create mode 100644 carmananger.cpp create mode 100644 carmananger.h create mode 100644 parkingLotItem.cpp create mode 100644 parkingLotItem.h diff --git a/DataStructureProject.pro b/DataStructureProject.pro index f9806e4..dfb149b 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -32,7 +32,9 @@ SOURCES += \ Road.cpp \ ParkingSpaceWidget.cpp \ monitor.cpp \ - boundary.cpp + boundary.cpp \ + carmananger.cpp \ + parkingLotItem.cpp HEADERS += \ mainwindow.h \ @@ -41,7 +43,9 @@ HEADERS += \ Road.h \ ParkingSpaceWidget.h \ monitor.h \ - boundary.h + boundary.h \ + carmananger.h \ + parkingLotItem.h FORMS += \ mainwindow.ui \ diff --git a/ParkingLotWidget.cpp b/ParkingLotWidget.cpp index a15ff92..0919f9d 100644 --- a/ParkingLotWidget.cpp +++ b/ParkingLotWidget.cpp @@ -12,7 +12,10 @@ ParkingLotWidget::ParkingLotWidget(QWidget *parent, const QString& xml) : QWidget(parent) { parse_xml(xml); + // resize(783, 879); + // resize(640, 480); setLayout(layout); + adjustSize(); // 调整大小至刚刚可以装下所有元素 } @@ -75,6 +78,7 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) layout = new QHBoxLayout; else layout = new QVBoxLayout; + layout->setSizeConstraint(QLayout::SetFixedSize); qDebug() << "处理" << element.tagName(); QDomElement child = element.firstChild().toElement(); diff --git a/ParkingLotWidget.h b/ParkingLotWidget.h index 23df63f..91585a5 100644 --- a/ParkingLotWidget.h +++ b/ParkingLotWidget.h @@ -14,6 +14,7 @@ class ParkingLotWidget: public QWidget public: explicit ParkingLotWidget(QWidget* parent, const QString& xml); int addSpace(ParkingSpaceWidget* sp); + int getCapacity() {return m_spaceList.size(); } ~ParkingLotWidget(); QString getName() const; diff --git a/carmananger.cpp b/carmananger.cpp new file mode 100644 index 0000000..bae05a9 --- /dev/null +++ b/carmananger.cpp @@ -0,0 +1,6 @@ +#include "carmananger.h" + +CarMananger::CarMananger() +{ + +} diff --git a/carmananger.h b/carmananger.h new file mode 100644 index 0000000..57c8cd1 --- /dev/null +++ b/carmananger.h @@ -0,0 +1,11 @@ +#ifndef CARMANANGER_H +#define CARMANANGER_H + + +class CarMananger +{ +public: + CarMananger(); +}; + +#endif // CARMANANGER_H \ No newline at end of file diff --git a/monitor.cpp b/monitor.cpp index 2651244..ea248be 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -2,14 +2,14 @@ #include "ui_monitor.h" #include #include +#include "parkingLotItem.h" Monitor::Monitor(QWidget *parent) : QWidget(parent), ui(new Ui::Monitor) { ui->setupUi(this); - QObject::connect(ui->downStairButton, QPushButton::clicked, [&]() { - qDebug() << "下一层按钮"; + QObject::connect(ui->downStairButton, &QPushButton::clicked, [&]() { if (m_currentFloor > 0) { m_currentFloor -= 1; showParkinglot(); @@ -19,8 +19,7 @@ Monitor::Monitor(QWidget *parent) : if (m_currentFloor == m_parkinglots.size() - 2) ui->upStairButton->setEnabled(true); }); - QObject::connect(ui->upStairButton, QPushButton::clicked, [&]() { - qDebug() << "上一层按钮"; + QObject::connect(ui->upStairButton, &QPushButton::clicked, [&]() { if (m_currentFloor < m_parkinglots.size() - 1) { m_currentFloor += 1; showParkinglot(); @@ -35,19 +34,10 @@ Monitor::Monitor(QWidget *parent) : else if (m_currentFloor == m_parkinglots.size() - 1) ui->upStairButton->setEnabled(false); - - 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_f1.xml", "parkinglot_b1.xml"}; - for (int i = 0; i < 2; i++) { - QString t(result); - m_xmlPath.push_back(t.append(l.at(i))); - } - - showParkinglot(); + QGraphicsScene *scene = new QGraphicsScene(this); // 创建场景 + ui->view->setScene(scene); + ParkingLotItem *pk = new ParkingLotItem(Q_NULLPTR); + scene->addItem(pk); } Monitor::~Monitor() diff --git a/monitor.ui b/monitor.ui index af549f4..681b6e1 100644 --- a/monitor.ui +++ b/monitor.ui @@ -10,16 +10,34 @@ 900 + + + 0 + 0 + + + + + 1080 + 900 + + + + + 1080 + 900 + + Form - 220 - 60 - 811 - 581 + 13 + 10 + 1061 + 881 @@ -27,27 +45,29 @@ QLayout::SetDefaultConstraint - - - 0 + + + + 0 + 0 + - - - - - - Qt::Horizontal - - + - 40 - 20 + 783 + 879 - + + + 783 + 879 + + + - + QLayout::SetDefaultConstraint @@ -93,13 +113,19 @@ + + Qt::RightToLeft + - 共有车位 + 共有车位: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - + 100 @@ -111,13 +137,22 @@ + + + 0 + 0 + + - 剩余 + 剩余: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - + 90 @@ -138,6 +173,76 @@ + + + + 添加车辆: + + + Qt::AutoText + + + false + + + + + + + + + + 0 + 0 + + + + + + + + + + 车牌号 + + + + + + + + + + + + + + 确定添加 + + + + + + + 随机生成 + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + diff --git a/parkingLotItem.cpp b/parkingLotItem.cpp new file mode 100644 index 0000000..5e291cb --- /dev/null +++ b/parkingLotItem.cpp @@ -0,0 +1,46 @@ +#include "parkingLotItem.h" +#include "ParkingLotWidget.h" +#include +#include + +ParkingLotItem::ParkingLotItem(QGraphicsPixmapItem *parent): QGraphicsPixmapItem(parent) +{ + // 找到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=" <size().height(); + widget->render(m_pixmap[i]); // 渲染pixmap + m_max_capacity.push_back(widget->getCapacity()); // 获得这一层的最大容量 + m_cars[i].resize(m_max_capacity.at(i)); // 初始化一层的车位 + m_name.push_back(widget->getName()); // 这一层的名字 + m_num_of_cars.push_back(0); // 初始停车数量为0 + delete widget; // 删除widget + + QPainter painter(m_pixmap[i]); + painter.setPen(Qt::red); + painter.drawRect(0, 0, m_pixmap[i]->width() - 1, m_pixmap[i]->height() - 1); + } + showParkingLot(1); +} + +void ParkingLotItem::showParkingLot(uint pos) +{ + if (pos >= m_num_of_layer) { + qDebug() << "ParkingLotItem: 层数错误"; + return; + } + setPixmap(*m_pixmap.at(pos)); +} diff --git a/parkingLotItem.h b/parkingLotItem.h new file mode 100644 index 0000000..a51bbe9 --- /dev/null +++ b/parkingLotItem.h @@ -0,0 +1,27 @@ +#ifndef PARKINGLOTITEM_H +#define PARKINGLOTITEM_H + +#include +#include +#include +#include +#include "cars.h" + +class ParkingLotItem: public QGraphicsPixmapItem +{ +public: + ParkingLotItem(QGraphicsPixmapItem* parent); + void showParkingLot(uint pos); +private: + QVector m_pixmap; // 每一层的pixmap + QVector m_max_capacity; // 每一层的容量 + QVector m_num_of_cars; // 每一层当前停的车数量 + QVector > m_cars; // 停在每一层的车 + QVector >m_waitting; // 在每个入口等待的车 + QStringList m_xml_path; // 每一层xml文件地址 + QStringList m_name; // 每一层的名字 + const uint m_num_of_layer = 2; // 总层数 + const uint m_num_of_entry = 2; // 入口数 +}; + +#endif // PARKINGLOTITEM_H