画停车场

This commit is contained in:
wmj committed 2017-12-13 20:18:14 +08:00
1 parent 59f914b6c0
commit b65105a950
13 files changed
+362 -51

No files matched your search

+6 -3
View File
@@ -5,6 +5,7 @@
#-------------------------------------------------
QT += core gui
QT += xml
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
@@ -27,15 +28,17 @@ SOURCES += \
main.cpp \
mainwindow.cpp \
monitor.cpp \
cars.cpp
cars.cpp \
parkinglot.cpp
HEADERS += \
mainwindow.h \
monitor.h \
cars.h
cars.h \
parkinglot.h
FORMS += \
mainwindow.ui
RESOURCES += \
cars.qrc
cars.qrc \
+10 -7
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.4.1, 2017-12-11T15:06:03. -->
<!-- Written by QtCreator 4.4.1, 2017-12-13T20:10:27. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@@ -529,14 +529,17 @@
<value type="int">13</value>
<value type="int">14</value>
</valuelist>
<value type="int" key="PE.EnvironmentAspect.Base">-1</value>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.CustomExecutableRunConfiguration.Arguments"></value>
<value type="QString" key="ProjectExplorer.CustomExecutableRunConfiguration.Executable"></value>
<value type="QString" key="ProjectExplorer.CustomExecutableRunConfiguration.WorkingDirectory">%{buildDir}</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Custom Executable</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">DataStructureProject</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:D:/wming/Documents/DataStructureProject/DataStructureProject.pro</value>
<value type="bool" key="QmakeProjectManager.QmakeRunConfiguration.UseLibrarySearchPath">true</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"></value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">DataStructureProject.pro</value>
<value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix">false</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory"></value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default">D:/wming/Documents/build-DataStructureProject-Desktop_Qt_5_9_3_MinGW_32bit-Debug</value>
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
+10 -2
View File
@@ -1,9 +1,17 @@
#include "cars.h"
#include <QtGlobal>
#include <QPainter>
Cars::Cars(QWidget *parent) : QWidget(parent)
{
m_color = rand() % 3;
m_color = Color(rand() % 3);
resize(200, 130);
}
void Cars::paintEvent(QPaintEvent *event) {
QPainter painter(this);
QPixmap pix(":/cars/yellow");
painter.drawPixmap(0, 0, 200, 130, pix);
painter.setPen(QPen(Qt::red, 5));
painter.drawRect(0, 0, 200, 130);
}
+6 -5
View File
@@ -8,14 +8,15 @@
class Cars : public QWidget
{
Q_OBJECT
public:
enum Color { Pink, Red, Yellow };
explicit Cars(QWidget *parent = nullptr);
explicit Cars(Color color, QWidget *parenet = nullptr) {}
void paintEvent(QPaintEvent *event);
private:
enum Color m_color;
Color m_color;
QPixmap m_pixmap;
QString m_number;
public:
enum Color { Pink, Red, Yellow }
explicit Cars(QWidget *parent = nullptr);
explicit Cars(Color color, QWidget *parenet = nullptr);
signals:
public slots:
-24
View File
@@ -1,24 +0,0 @@
#ifndef CARS_H
#define CARS_H
#include <QWidget>
#include <QPixmap>
#include <QString>
class Cars : public QWidget
{
Q_OBJECT
private:
enum Color m_color;
QPixmap m_pixmap;
QString m_number;
public:
enum Color { Pink, Red, Yellow }
explicit Cars(QWidget *parent = nullptr);
explicit Cars(Color color, QWidget *parenet = nullptr);
signals:
public slots:
};
#endif // CARS_H
+17 -9
View File
@@ -1,30 +1,38 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "monitor.h"
#include "cars.h"
#include <QDialog>
#include <QLabel>
#include <QImage>
#include <QPainter>
#include <QScrollArea>
#include "parkinglot.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
resize(1800, 800);
//ui->setupUi(this);
montior = new Monitor(this);
QPixmap pix(":/cars/yellow");
QPainter painter(this);
painter.drawPixmap(0,0,pix);
QPixmap pix2=pix.scaled(QSize(50,50));
painter.drawPixmap(0,0,pix2);
// 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);
ParkingLot *pk = new ParkingLot("D:\\wming\\Documents\\DataStructureProject\\parkinglot_f1.xml", this);
// pk->creatMap();
QLabel *label = new QLabel(this);
label->resize(1000, 1000);
label->setPixmap(pk->creatMap2());
scrollArea->setWidget(label);
}
MainWindow::~MainWindow()
{
delete ui;
delete montior;
delete monitor;
}
void MainWindow::open(void) {
+2 -1
View File
@@ -3,6 +3,7 @@
#include <QMainWindow>
#include <QAction>
#include "monitor.h"
namespace Ui {
class MainWindow;
@@ -20,7 +21,7 @@ public:
private:
Ui::MainWindow *ui;
QAction *action;
Monitor *montor;
Monitor *monitor;
};
#endif // MAINWINDOW_H
+271
View File
@@ -0,0 +1,271 @@
#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));
}
// map->resize(1000, 1000);
// painter = new QPainter(map);
}
void ParkingLot::creatMap()
{/*
// painter = new QPainter(map);
QDomElement root =doc -> documentElement();
if (root.tagName() != "parkinglot") {
QMessageBox::critical(parent, QObject::tr("Error"),
QObject::tr("Not a parkinglot file"));
}
if (root.attribute("layout") == "vertical")
vertical = true;
else if (root.attribute("layout") == "horizontal")
vertical = false;
else
qDebug() << "XML文件错误:" << root.attribute("layout");
int currentX = 0, X = 0;
int currentY = 0, Y = 0;
int maxWidthOfColumn = 0;
int maxHeightOfLine = 0;
QPixmap roadPix(":/road/line");
QPixmap roomPix(":/road/room");
QPixmap *toDraw;
QMatrix rotate;
QDomNode line = root.firstChild();
while (!line.isNull()) { // <column>节点
QDomElement space = line.firstChild().toElement();
while(!space.isNull()) {
if (space.tagName() == "road") {
toDraw = &roadPix;
} else if (space.tagName() == "room") {
toDraw = &roomPix;
} else {
qDebug() << "文件错误" << space.tagName();
}
int n = space.attribute("number", "1").toInt();
painter->save();
switch(space.attribute("direction").at(0).unicode()) {
case 'E':
rotate.rotate(180);
break;
case 'S':
rotate =rotate.rotate(90);
break;
case 'W':
rotate =rotate.rotate(0);
break;
case 'N':
rotate =rotate.rotate(270);
break;
default:
qDebug() << "错误的方向" << space.attribute("direction").at(0).unicode();
}
for (int i = 0; i < n; i++) {
painter->drawPixmap(currentX, currentY, toDraw -> width(), toDraw -> height(),
toDraw->transformed(rotate));
if (vertical) {
currentY += toDraw -> height();
maxWidthOfColumn = std::max(maxWidthOfColumn, toDraw -> width());
} else {
currentX += toDraw -> width();
maxHeightOfLine = std::max(maxHeightOfLine, toDraw -> height());
}
}
painter->restore();
space = space.nextSibling().toElement();
}
if (vertical) {
Y = std::max(Y, currentY);
currentY = 0;
currentX += maxWidthOfColumn;
X = currentX;
maxWidthOfColumn = 0;
} else {
X = std::max(X, currentX);
currentX = 0;
currentY += maxHeightOfLine;
Y = currentY;
maxHeightOfLine = 0;
}
line = line.nextSibling();
}*/
}
QPixmap ParkingLot::creatMap2()
{
QDomElement root =doc -> documentElement();
if (root.tagName() != "parkinglot") {
QMessageBox::critical(parent, QObject::tr("Error"),
QObject::tr("Not a parkinglot file"));
}
qDebug() << "!!!!";
return parseLayout(root.firstChild().toElement());
}
QPixmap *ParkingLot::getPixmap()
{
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);
}
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);
QPainter painter(&result), tPainter(&temp);
painter.setPen(Qt::white);
painter.setBrush(Qt::white);
tPainter.setPen(Qt::white);
tPainter.setBrush(Qt::white);
ushort direction = roomXML.attribute("direction").at(0).unicode();
int n = roomXML.attribute("number").toInt();
int x = 0, y = 0;
enum {down, right} expendDirection;
if (roomXML.attribute("expendDirection") == "down")
expendDirection = down;
else
expendDirection = right;
tPainter.drawRect(0, 0, 60, 5);
tPainter.drawRect(0, 5, 10, 35);
tPainter.drawRect(10, 35, 50, 5);
tPainter.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 (int 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)
{
QPixmap result(2000, 2000), temp;
QPainter painter(&result);
painter.setPen(Qt::black);
int x = 0, y = 0, maxX = 0, maxY = 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()) {
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();
maxY = std::max(maxY, temp.height());
} else {
y += temp.height();
maxX = std::max(maxX, temp.width());
}
child = child.nextSibling().toElement();
}
if (direction == horizontal) {
return result.copy(0, 0, x, maxY);
} else {
return result.copy(0, 0, maxX, y);
}
}
+24
View File
@@ -0,0 +1,24 @@
#ifndef PARKINGLOT_H
#define PARKINGLOT_H
#include <QWidget>
#include <QString>
#include <QPainter>
#include <QBitmap>
#include <QDomDocument>
class ParkingLot
{
public:
explicit ParkingLot(const QString& fname, QWidget *parent = nullptr);
void creatMap();
QPixmap creatMap2();
QPixmap* getPixmap();
~ParkingLot();
private:
bool vertical;
QPixmap* map;
QDomDocument* doc;
QWidget* parent;
QPixmap parseLayout(const QDomElement& layout);
};
#endif // PARKINGLOT_H
+13
View File
@@ -0,0 +1,13 @@
<parkinglot roadWidth="50px">
<vLayout>
<road length="800" direction="horizontal" />
<hLayout>
<room number="12" direction="E" expendDirection="down"/>
<road length="900" direction="vertical" />
<room number="12" direction="W" expendDirection="down"/>
<room number="12" direction="E" expendDirection="down"/>
<road length="900" direction="vertical" />
</hLayout>
<road length="800" direction="horizontal" />
</vLayout>
</parkinglot>
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

BIN
View File
Binary file not shown.
+3
View File
@@ -0,0 +1,3 @@
<RCC>
<qresource prefix="/road"/>
</RCC>