把停车场和ui连接起来

This commit is contained in:
wmj committed 2017-12-20 11:39:53 +08:00
1 parent b7d5ce3a5e
commit 200da757d7
5 files changed
+56 -55

No files matched your search

+8 -40
View File
@@ -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("<center>" + m_parkinglots.at(m_currentFloor)->getName() + "</center>"); // 文字
for (auto p = m_parkinglots.begin(); p != m_parkinglots.end(); p++)
if (*p != nullptr)
(*p)->hide();
m_parkinglots.at(m_currentFloor)->show();
}
-6
View File
@@ -20,12 +20,6 @@ public:
private:
Ui::Monitor *ui;
// TODO: 动态修改
std::vector<ParkingLotWidget*> m_parkinglots = {nullptr, nullptr}; // lazy
std::vector<QString> m_xmlPath;// = {"parkinglot_b1.xml", "parkinglot_f1.xml"};
uint m_currentFloor = 1; // 默认为地上一层
void showParkinglot(void);
};
#endif // MONITOR_H
+2 -2
View File
@@ -125,7 +125,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="allSpace">
<widget class="QLabel" name="capacity">
<property name="text">
<string>100</string>
</property>
@@ -152,7 +152,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="leftSpace">
<widget class="QLabel" name="load">
<property name="text">
<string>90</string>
</property>
+33 -4
View File
@@ -3,7 +3,8 @@
#include <QDebug>
#include <QPainter>
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=" <<widget->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);
}
+13 -3
View File
@@ -7,14 +7,18 @@
#include <QQueue>
#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<QPixmap*> m_pixmap; // 每一层的pixmap
QVector<int> m_max_capacity; // 每一层的容量
QVector<int> m_capacity; // 每一层的容量
QVector<int> m_num_of_cars; // 每一层当前停的车数量
QVector<QVector<Car*> > m_cars; // 停在每一层的车
QVector<QQueue<Car*> >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