diff --git a/monitor.cpp b/monitor.cpp index ea248be..a7183b1 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -9,34 +9,17 @@ Monitor::Monitor(QWidget *parent) : ui(new Ui::Monitor) { ui->setupUi(this); - QObject::connect(ui->downStairButton, &QPushButton::clicked, [&]() { - if (m_currentFloor > 0) { - m_currentFloor -= 1; - showParkinglot(); - } - if (m_currentFloor == 0) - ui->downStairButton->setEnabled(false); - if (m_currentFloor == m_parkinglots.size() - 2) - ui->upStairButton->setEnabled(true); - }); - QObject::connect(ui->upStairButton, &QPushButton::clicked, [&]() { - if (m_currentFloor < m_parkinglots.size() - 1) { - m_currentFloor += 1; - showParkinglot(); - } - if (m_currentFloor == m_parkinglots.size() - 1) - ui->upStairButton->setEnabled(false); - if (m_currentFloor == 1) - ui->downStairButton->setEnabled(true); - }); - if (m_currentFloor == 0) // 如果默认楼层就是顶层或者最下一层,把相应的按钮disable - ui->downStairButton->setEnabled(false); - else if (m_currentFloor == m_parkinglots.size() - 1) - ui->upStairButton->setEnabled(false); + ParkingLotItem *pk = new ParkingLotItem(Q_NULLPTR, this); + QObject::connect(ui->downStairButton, &QPushButton::clicked, pk, &ParkingLotItem::showDownStairFloor); + QObject::connect(ui->upStairButton, &QPushButton::clicked, pk, &ParkingLotItem::showUpStairFloor); + QObject::connect(pk, &ParkingLotItem::enableUpButton, ui->upStairButton, &QPushButton::setEnabled); + QObject::connect(pk, &ParkingLotItem::enableDownButton, ui->downStairButton, &QPushButton::setEnabled); + QObject::connect(pk, &ParkingLotItem::setCapacity, ui->capacity, &QLabel::setText); + QObject::connect(pk, &ParkingLotItem::setLoad, ui->load, &QLabel::setText); + pk->showParkingLot(); QGraphicsScene *scene = new QGraphicsScene(this); // 创建场景 ui->view->setScene(scene); - ParkingLotItem *pk = new ParkingLotItem(Q_NULLPTR); scene->addItem(pk); } @@ -44,18 +27,3 @@ Monitor::~Monitor() { delete ui; } - -void Monitor::showParkinglot() -{ - if (m_parkinglots.at(m_currentFloor) == nullptr) { - m_parkinglots[m_currentFloor] = new ParkingLotWidget(this, m_xmlPath.at(m_currentFloor)); // lazy - // ui->parkinglotView->addWidget(m_parkinglots.at(m_currentFloor)); // 把当前对象添加进layout - m_parkinglots[m_currentFloor]->move(10, 10); - } - ui->currentFloor->setText("
" + m_parkinglots.at(m_currentFloor)->getName() + "
"); // 文字 - - for (auto p = m_parkinglots.begin(); p != m_parkinglots.end(); p++) - if (*p != nullptr) - (*p)->hide(); - m_parkinglots.at(m_currentFloor)->show(); -} diff --git a/monitor.h b/monitor.h index 5c1e030..b4e9364 100644 --- a/monitor.h +++ b/monitor.h @@ -20,12 +20,6 @@ public: private: Ui::Monitor *ui; - // TODO: 动态修改 - std::vector m_parkinglots = {nullptr, nullptr}; // lazy - std::vector m_xmlPath;// = {"parkinglot_b1.xml", "parkinglot_f1.xml"}; - uint m_currentFloor = 1; // 默认为地上一层 - - void showParkinglot(void); }; #endif // MONITOR_H diff --git a/monitor.ui b/monitor.ui index 681b6e1..e1c1c3b 100644 --- a/monitor.ui +++ b/monitor.ui @@ -125,7 +125,7 @@ - + 100 @@ -152,7 +152,7 @@ - + 90 diff --git a/parkingLotItem.cpp b/parkingLotItem.cpp index 5e291cb..fcefbe3 100644 --- a/parkingLotItem.cpp +++ b/parkingLotItem.cpp @@ -3,7 +3,8 @@ #include #include -ParkingLotItem::ParkingLotItem(QGraphicsPixmapItem *parent): QGraphicsPixmapItem(parent) +ParkingLotItem::ParkingLotItem(QGraphicsPixmapItem *parent, QObject* objectParent): + QObject(objectParent), QGraphicsPixmapItem(parent) { // 找到xml文件路径 QString filePath = __FILE__; @@ -23,8 +24,8 @@ ParkingLotItem::ParkingLotItem(QGraphicsPixmapItem *parent): QGraphicsPixmapItem 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_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 delete widget; // 删除widget @@ -33,7 +34,6 @@ ParkingLotItem::ParkingLotItem(QGraphicsPixmapItem *parent): QGraphicsPixmapItem 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) @@ -43,4 +43,33 @@ void ParkingLotItem::showParkingLot(uint pos) return; } setPixmap(*m_pixmap.at(pos)); + emit setCapacity(QString::number(m_capacity.at(pos))); + emit setLoad(QString::number(m_capacity.at(pos) - m_num_of_cars.at(pos))); +} + +void ParkingLotItem::showParkingLot() +{ + showParkingLot(m_current_floor); +} + +void ParkingLotItem::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 ParkingLotItem::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); } diff --git a/parkingLotItem.h b/parkingLotItem.h index 4d45cf7..14669aa 100644 --- a/parkingLotItem.h +++ b/parkingLotItem.h @@ -7,14 +7,18 @@ #include #include "car.h" -class ParkingLotItem: public QGraphicsPixmapItem +class ParkingLotItem: public QObject, public QGraphicsPixmapItem { + Q_OBJECT public: - ParkingLotItem(QGraphicsPixmapItem* parent); + explicit ParkingLotItem(QGraphicsPixmapItem* itemParent, QObject* objectParent); void showParkingLot(uint pos); + void showParkingLot(void); + void showDownStairFloor(void); + void showUpStairFloor(void); private: QVector m_pixmap; // 每一层的pixmap - QVector m_max_capacity; // 每一层的容量 + QVector m_capacity; // 每一层的容量 QVector m_num_of_cars; // 每一层当前停的车数量 QVector > m_cars; // 停在每一层的车 QVector >m_waitting; // 在每个入口等待的车 @@ -22,6 +26,12 @@ private: QStringList m_name; // 每一层的名字 const uint m_num_of_layer = 2; // 总层数 const uint m_num_of_entry = 2; // 入口数 + uint m_current_floor = 1; // 当前楼层,默认为1楼(按照从下往上递增,值与实际楼层无关) +signals: + void enableUpButton(bool); + void enableDownButton(bool); + void setCapacity(const QString&); + void setLoad(const QString&); }; #endif // PARKINGLOTITEM_H