多层停车场'

This commit is contained in:
wmj committed 2017-12-17 12:14:53 +08:00
1 parent 44681472d0
commit 91c77a42b5
14 files changed
+226 -260

No files matched your search

+6 -7
View File
@@ -27,24 +27,23 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp \
monitor.cpp \
cars.cpp \
parkinglot.cpp \
ParkingLotWidget.cpp \
Road.cpp \
ParkingSpaceWidget.cpp
ParkingSpaceWidget.cpp \
monitor.cpp
HEADERS += \
mainwindow.h \
monitor.h \
cars.h \
parkinglot.h \
ParkingLotWidget.h \
Road.h \
ParkingSpaceWidget.h
ParkingSpaceWidget.h \
monitor.h
FORMS += \
mainwindow.ui
mainwindow.ui \
monitor.ui
RESOURCES += \
cars.qrc \
+19 -6
View File
@@ -10,7 +10,6 @@
ParkingLotWidget::ParkingLotWidget(QWidget *parent, const QString& xml) : QWidget(parent)
{
resize(1000, 1000);
parse_xml(xml);
setLayout(layout);
}
@@ -37,6 +36,7 @@ void ParkingLotWidget::parse_xml(const QString & xml)
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::critical(this, QObject::tr("Error"),
QObject::tr("Cannot read file %1").arg(xml));
return;
}
QString errorStr;
@@ -47,15 +47,23 @@ void ParkingLotWidget::parse_xml(const QString & xml)
QMessageBox::critical(this, QObject::tr("Error"),
QObject::tr("Parse error at line %1, column %2: %3")
.arg(errorLine).arg(errorColumn).arg(errorStr));
return;
}
QDomElement element = doc.documentElement().firstChild().toElement();
QString name = element.tagName();
if (element.tagName().contains("Layout"))
QDomElement element = doc.documentElement();
if (element.tagName() != "parkinglot") {
qDebug() << this -> objectName() << "布局文件错误: 根标签不是parkinglot";
return;
}
this->name = element.attribute("name");
QDomElement childElement = element.firstChildElement();
QString name = childElement.tagName();
if (childElement.tagName().contains("Layout"))
{
layout = parseLayout(element);
layout = parseLayout(childElement);
} else
{
qDebug() << this -> objectName() << "布局文件错误";
qDebug() << this -> objectName() << "布局文件错误: 没有布局标签";
return;
}
}
@@ -96,3 +104,8 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element)
layout->setSpacing(0);
return layout;
}
QString ParkingLotWidget::getName() const
{
return name;
}
+5 -1
View File
@@ -13,14 +13,18 @@ class ParkingLotWidget: public QWidget
Q_OBJECT
public:
explicit ParkingLotWidget(QWidget* parent, const QString& xml);
void paintEvent(QPaintEvent* event) override;
int addSpace(ParkingSpaceWidget* sp);
~ParkingLotWidget();
QString getName() const;
protected:
void paintEvent(QPaintEvent* event) override;
private:
std::vector<ParkingSpaceWidget*> m_spaceList;
void parse_xml(const QString& xml);
QBoxLayout *parseLayout(const QDomElement& element);
QBoxLayout *layout = nullptr;
QString name;
};
#endif
+2 -1
View File
@@ -13,8 +13,9 @@ public:
enum direction{N, S, E, W};
ParkingSpaceWidget(ParkingLotWidget* parent, const QString &dir);
static QBoxLayout* makeParkingSapceGroup(ParkingLotWidget* parent, const QString& dir, int n, const QString &expend);
void paintEvent(QPaintEvent* event) override;
~ParkingSpaceWidget();
protected:
void paintEvent(QPaintEvent* event) override;
private:
int number;
direction dir;
+2
View File
@@ -1,5 +1,6 @@
#include "Road.h"
#include <QPainter>
#include <QPaintEvent>
Road::Road(QWidget* parent, const int length, const direction dir)
:QWidget(parent), length(length), dir(dir)
@@ -22,6 +23,7 @@ Road::Road(QWidget* parent, const int length, const direction dir)
void Road::paintEvent(QPaintEvent * event)
{
event->ignore();
QPainter painter(this);
/*painter.setPen(Qt::blue);
painter.drawRect(0, 0, width() - 1, height() - 1);*/
+4
View File
@@ -1,9 +1,13 @@
#include "mainwindow.h"
#include <QApplication>
#include <QString>
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug() << QCoreApplication::applicationDirPath();
MainWindow w;
w.show();
+4 -19
View File
@@ -4,29 +4,14 @@
#include <QDialog>
#include <QLabel>
#include <QScrollArea>
#include "parkinglot.h"
#include "ParkingLotWidget.h"
#include "monitor.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
QMainWindow(parent)
{
resize(1800, 800);
// ui->setupUi(this);
QScrollArea* scrollArea = new QScrollArea(this);
scrollArea->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
scrollArea->setWidgetResizable(true);
// scrollArea->resize(1800, 800);
// monitor = new Monitor(this);
// Cars *car = new Cars(this);
ParkingLotWidget *pk = new ParkingLotWidget(this, "parkinglot_f1.xml");
// pk->show();
// pk->creatMap();
/*QLabel *label = new QLabel(this);
label->resize(1000, 1000);
label->setPixmap(pk->creatMap2());*/
scrollArea->resize(pk->width(), this->height());
scrollArea->setWidget(pk);
Monitor *monitor = new Monitor(this);
resize(monitor->size());
}
MainWindow::~MainWindow()
+50 -11
View File
@@ -1,17 +1,56 @@
#include "monitor.h"
#include <QPainter>
#include "ui_monitor.h"
#include <QObject>
#include <QDebug>
Monitor::Monitor(QWidget *parent) : QWidget(parent)
Monitor::Monitor(QWidget *parent) :
QWidget(parent),
ui(new Ui::Monitor)
{
resize(800, 600);
ui->setupUi(this);
QObject::connect(ui->downStairButton, QPushButton::clicked, [&]() {
qDebug() << "下一层按钮";
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, [&]() {
qDebug() << "上一层按钮";
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);
showParkinglot();
}
void Monitor::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.drawLine(80, 100, 650, 500);
painter.setPen(Qt::red);
painter.drawRect(10, 10, 100, 400);
painter.setPen(QPen(Qt::green, 5));
painter.setBrush(Qt::blue);
painter.drawEllipse(50, 150, 400, 200);
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));
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();
//ui->rootLayout->replaceWidget(ui->parkingLotView, m_parkinglots.at(m_currentFloor));
//ui->parkingLotView = m_parkinglots.at(m_currentFloor);
}
+17 -6
View File
@@ -2,19 +2,30 @@
#define MONITOR_H
#include <QWidget>
#include <vector>
#include <QString>
#include "ParkingLotWidget.h"
namespace Ui {
class Monitor;
}
class Monitor : public QWidget
{
Q_OBJECT
public:
explicit Monitor(QWidget *parent = nullptr);
explicit Monitor(QWidget *parent = 0);
~Monitor();
protected:
void paintEvent(QPaintEvent *);
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; // 默认为地上一层
signals:
public slots:
void showParkinglot(void);
};
#endif // MONITOR_H
+102
View File
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Monitor</class>
<widget class="QWidget" name="Monitor">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1080</width>
<height>800</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>220</x>
<y>60</y>
<width>811</width>
<height>581</height>
</rect>
</property>
<layout class="QHBoxLayout" name="rootLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item>
<widget class="QWidget" name="parkingLotView" native="true">
<property name="maximumSize">
<size>
<width>1000</width>
<height>800</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item>
<widget class="QLabel" name="currentFloor">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="upStairButton">
<property name="text">
<string>上一层</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="downStairButton">
<property name="text">
<string>下一层</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
-186
View File
@@ -1,186 +0,0 @@
#include "parkinglot.h"
#include <QDomDocument>
#include <QFile>
#include <QMessageBox>
#include <QPainter>
#include <QWidget>
#include <QDebug>
#include <algorithm>
ParkingLot::ParkingLot(const QString &fname, QWidget* parent)
{
map = new QPixmap(1000, 1000);
this->parent = parent;
QFile file(fname);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::critical(parent, QObject::tr("Error"),
QObject::tr("Cannot read file %1").arg(fname));
}
QString errorStr;
int errorLine;
int errorColumn;
doc = new QDomDocument;
if (!doc -> setContent(&file, false, &errorStr, &errorLine, &errorColumn)) {
QMessageBox::critical(parent, QObject::tr("Error"),
QObject::tr("Parse error at line %1, column %2: %3")
.arg(errorLine).arg(errorColumn).arg(errorStr));
}
}
QPixmap ParkingLot::creatMap2() const
{
QDomElement root =doc -> documentElement();
if (root.tagName() != "parkinglot") {
QMessageBox::critical(parent, QObject::tr("Error"),
QObject::tr("Not a parkinglot file"));
}
return parseLayout(root.firstChild().toElement());
}
QPixmap *ParkingLot::getPixmap() const
{
return map;
}
ParkingLot::~ParkingLot()
{
// delete painter;
delete doc;
delete map;
}
QPixmap drawRoad(const QDomElement& roadXML) {
QPixmap result(800, 600);
if (roadXML.tagName() != "road") {
qDebug() << "XML is not a road";
return result.copy(0, 0, 0, 0);
}
result.fill(QColor(200, 200, 200));
int x = 0, y = 0;
QPainter painter(&result);
painter.setBrush(Qt::white);
painter.setPen(Qt::white);
enum {vertical, horizontal} direction;
const int length = roadXML.attribute("length").toInt();
qDebug() << "length:" << length;
if (roadXML.attribute("direction") == "horizontal")
direction = horizontal;
else
direction = vertical;
if (direction == horizontal) {
while (x < length) {
painter.drawRect(x + 5, 20, 40, 10);
x += 50;
}
} else {
while (y < length) {
painter.drawRect(20, y + 5, 10, 40);
y += 50;
}
}
if (direction == horizontal)
return result.copy(0, 0, length, 50);
else
return result.copy(0, 0, 50, length);
}
QPixmap drawRoom(const QDomElement& roomXML) {
QPixmap result(800, 600);
if (roomXML.tagName() != "room") {
qDebug() << "XML节点不是车位";
return result.copy(0, 0, 0, 0);
}
QPixmap temp(60, 40);
result.fill(QColor(200, 200, 200));
QPainter painter(&result), t_painter(&temp);
painter.setPen(Qt::white);
painter.setBrush(Qt::white);
t_painter.setPen(Qt::white);
t_painter.setBrush(Qt::white);
const auto direction = roomXML.attribute("direction").at(0).unicode();
const auto n = roomXML.attribute("number").toInt();
auto x = 0, y = 0;
enum {down, right} expendDirection;
if (roomXML.attribute("expendDirection") == "down")
expendDirection = down;
else
expendDirection = right;
t_painter.drawRect(0, 0, 60, 5);
t_painter.drawRect(0, 5, 10, 35);
t_painter.drawRect(10, 35, 50, 5);
t_painter.end();
switch(direction) {
case 'E':
// temp = temp;
break;
case 'S':
temp = temp.transformed(QMatrix().rotate(270));
break;
case 'W':
temp = temp.transformed(QMatrix().rotate(180));
break;
case 'N':
temp = temp.transformed(QMatrix().rotate(90));
break;
default:
qDebug() << "未知方向" << direction;
break;
}
// tPainter.begin(&temp);
for (auto i = 0; i < n; i++) {
painter.drawPixmap(x, y, temp.width(), temp.height(), temp);
if (expendDirection == down)
y += temp.height();
else
x += temp.width();
}
if (expendDirection == down)
return result.copy(0, 0, temp.width(), y);
else
return result.copy(0, 0, temp.height(), x);
}
QPixmap ParkingLot::parseLayout(const QDomElement& layout) const
{
QPixmap result(2000, 2000), temp;
result.fill(QColor(200, 200, 200));
QPainter painter(&result);
painter.setPen(Qt::black);
auto x = 0, y = 0, max_x = 0, max_y = 0;
enum {vertical, horizontal} direction;
if (layout.tagName() == "vLayout")
direction = vertical;
else if (layout.tagName() == "hLayout")
direction = horizontal;
else {
qDebug() << "错误的布局名称" << layout.tagName();
return result;
}
QDomElement child = layout.firstChild().toElement();
while(!child.isNull()) {
const QString name = child.tagName();
if (name == "road") {
temp = drawRoad(child);
} else if (name == "room") {
temp = drawRoom(child);
} else if (name == "vLayout" || name == "hLayout")
temp = parseLayout(child);
painter.drawPixmap(x, y, temp.width(), temp.height(), temp);
if (direction == horizontal) {
x += temp.width();
max_y = std::max(max_y, temp.height());
} else {
y += temp.height();
max_x = std::max(max_x, temp.width());
}
child = child.nextSibling().toElement();
}
if (direction == horizontal) {
return result.copy(0, 0, x, max_y);
} else {
return result.copy(0, 0, max_x, y);
}
}
-22
View File
@@ -1,22 +0,0 @@
#ifndef PARKINGLOT_H
#define PARKINGLOT_H
#include <QWidget>
#include <QPainter>
#include <QDomDocument>
class ParkingLot
{
public:
explicit ParkingLot(const QString& fname, QWidget *parent = nullptr);
void creatMap();
QPixmap creatMap2() const;
QPixmap* getPixmap() const;
~ParkingLot();
private:
// bool vertical;
QPixmap* map;
QDomDocument* doc;
QWidget* parent;
QPixmap parseLayout(const QDomElement& layout) const;
};
#endif // PARKINGLOT_H
+14
View File
@@ -0,0 +1,14 @@
<parkinglot roadWidth="50px" name="地下一层">
<vLayout>
<road length="700" direction="horizontal" />
<hLayout>
<road length="600" direction="vertical" />
<room number="15" direction="W" expendDirection="down"/>
<room number="15" direction="E" expendDirection="down"/>
<road length="600" direction="vertical" />
<room number="15" direction="W" expendDirection="down"/>
<room number="15" direction="E" expendDirection="down"/>
</hLayout>
<road length="700" direction="horizontal" />
</vLayout>
</parkinglot>
+1 -1
View File
@@ -1,4 +1,4 @@
<parkinglot roadWidth="50px">
<parkinglot roadWidth="50px" name="一层">
<vLayout>
<road length="700" direction="horizontal" />
<hLayout>