diff --git a/DataStructureProject.pro b/DataStructureProject.pro index 8423d43..2d9db8f 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -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 \ diff --git a/DataStructureProject.pro.user b/DataStructureProject.pro.user index 1c71e97..7154b36 100644 --- a/DataStructureProject.pro.user +++ b/DataStructureProject.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -529,14 +529,17 @@ 13 14 - -1 + 2 - - - %{buildDir} - Custom Executable + DataStructureProject - ProjectExplorer.CustomExecutableRunConfiguration + Qt4ProjectManager.Qt4RunConfiguration:D:/wming/Documents/DataStructureProject/DataStructureProject.pro + true + + DataStructureProject.pro + false + + D:/wming/Documents/build-DataStructureProject-Desktop_Qt_5_9_3_MinGW_32bit-Debug 3768 false true diff --git a/cars.cpp b/cars.cpp index 727fc41..65e66a3 100644 --- a/cars.cpp +++ b/cars.cpp @@ -1,9 +1,17 @@ #include "cars.h" #include +#include 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); +} diff --git a/cars.h b/cars.h index b9a528a..9b1606c 100644 --- a/cars.h +++ b/cars.h @@ -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: diff --git a/cars.h.autosave b/cars.h.autosave deleted file mode 100644 index b9a528a..0000000 --- a/cars.h.autosave +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef CARS_H -#define CARS_H - -#include -#include -#include - -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 diff --git a/mainwindow.cpp b/mainwindow.cpp index 895aae0..cf0d2d6 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,30 +1,38 @@ #include "mainwindow.h" #include "ui_mainwindow.h" #include "monitor.h" +#include "cars.h" #include #include #include #include +#include +#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) { diff --git a/mainwindow.h b/mainwindow.h index 7099819..b035943 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -3,6 +3,7 @@ #include #include +#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 diff --git a/parkinglot.cpp b/parkinglot.cpp new file mode 100644 index 0000000..b741c42 --- /dev/null +++ b/parkinglot.cpp @@ -0,0 +1,271 @@ +#include "parkinglot.h" +#include +#include +#include +#include +#include +#include +#include + +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()) { // 节点 + 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); + } +} diff --git a/parkinglot.h b/parkinglot.h new file mode 100644 index 0000000..cb41a12 --- /dev/null +++ b/parkinglot.h @@ -0,0 +1,24 @@ +#ifndef PARKINGLOT_H +#define PARKINGLOT_H +#include +#include +#include +#include +#include +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 diff --git a/parkinglot_f1.xml b/parkinglot_f1.xml new file mode 100644 index 0000000..09baa80 --- /dev/null +++ b/parkinglot_f1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/redcar.jpg b/redcar.jpg deleted file mode 100644 index 8d596e4..0000000 Binary files a/redcar.jpg and /dev/null differ diff --git a/redcar.psd b/redcar.psd deleted file mode 100644 index 6a1c34b..0000000 Binary files a/redcar.psd and /dev/null differ diff --git a/road.qrc b/road.qrc new file mode 100644 index 0000000..687f91e --- /dev/null +++ b/road.qrc @@ -0,0 +1,3 @@ + + +