From ac082afd0098c691b4f81518d3244c8602535644 Mon Sep 17 00:00:00 2001 From: wmj Date: Thu, 14 Dec 2017 16:34:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A9=E7=94=A8widget=E9=87=8D=E6=9E=84parki?= =?UTF-8?q?nglot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataStructureProject.pro | 19 ++- DataStructureProject.pro.user | 253 +--------------------------------- ParkingLotWidget.cpp | 29 +--- ParkingLotWidget.h | 7 +- ParkingSpaceInfo.cpp | 39 +++--- ParkingSpaceInfo.h | 1 - Road.cpp | 3 + mainwindow.cpp | 15 +- parkinglot.cpp | 36 ++--- parkinglot.h | 6 +- parkinglot_f1.xml | 8 +- 11 files changed, 80 insertions(+), 336 deletions(-) diff --git a/DataStructureProject.pro b/DataStructureProject.pro index 2d9db8f..4740c4b 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -29,13 +29,28 @@ SOURCES += \ mainwindow.cpp \ monitor.cpp \ cars.cpp \ - parkinglot.cpp + parkinglot.cpp \ + cars.cpp \ + main.cpp \ + mainwindow.cpp \ + monitor.cpp \ + parkinglot.cpp \ + ParkingLotWidget.cpp \ + ParkingSpaceInfo.cpp \ + Road.cpp HEADERS += \ mainwindow.h \ monitor.h \ cars.h \ - parkinglot.h + parkinglot.h \ + cars.h \ + mainwindow.h \ + monitor.h \ + parkinglot.h \ + ParkingLotWidget.h \ + ParkingSpaceInfo.h \ + Road.h FORMS += \ mainwindow.ui diff --git a/DataStructureProject.pro.user b/DataStructureProject.pro.user index 45fcb18..2638905 100644 --- a/DataStructureProject.pro.user +++ b/DataStructureProject.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -58,253 +58,6 @@ ProjectExplorer.Project.Target.0 - - Desktop Qt 5.9.3 MinGW 32bit - Desktop Qt 5.9.3 MinGW 32bit - qt.593.win32_mingw53_kit - 0 - 0 - 0 - - D:/wming/Documents/build-DataStructureProject-Desktop_Qt_5_9_3_MinGW_32bit-Debug - - - true - qmake - - QtProjectManager.QMakeBuildStep - true - - false - false - false - - - true - Make - - Qt4ProjectManager.MakeStep - - false - - - - 2 - 构建 - - ProjectExplorer.BuildSteps.Build - - - - true - Make - - Qt4ProjectManager.MakeStep - - true - clean - - - 1 - 清ç�† - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Debug - - Qt4ProjectManager.Qt4BuildConfiguration - 2 - true - - - D:/wming/Documents/build-DataStructureProject-Desktop_Qt_5_9_3_MinGW_32bit-Release - - - true - qmake - - QtProjectManager.QMakeBuildStep - false - - false - false - false - - - true - Make - - Qt4ProjectManager.MakeStep - - false - - - - 2 - 构建 - - ProjectExplorer.BuildSteps.Build - - - - true - Make - - Qt4ProjectManager.MakeStep - - true - clean - - - 1 - 清ç�† - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Release - - Qt4ProjectManager.Qt4BuildConfiguration - 0 - true - - - D:/wming/Documents/build-DataStructureProject-Desktop_Qt_5_9_3_MinGW_32bit-Profile - - - true - qmake - - QtProjectManager.QMakeBuildStep - true - - false - true - false - - - true - Make - - Qt4ProjectManager.MakeStep - - false - - - - 2 - 构建 - - ProjectExplorer.BuildSteps.Build - - - - true - Make - - Qt4ProjectManager.MakeStep - - true - clean - - - 1 - 清ç�† - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Profile - - Qt4ProjectManager.Qt4BuildConfiguration - 0 - true - - 3 - - - 0 - 部署 - - ProjectExplorer.BuildSteps.Deploy - - 1 - 在本地部署 - - ProjectExplorer.DefaultDeployConfiguration - - 1 - - - false - false - 1000 - - true - - false - false - false - false - true - 0.01 - 10 - true - 1 - 25 - - 1 - true - false - true - valgrind - - 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - - 2 - - DataStructureProject - - 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 - false - false - true - - 1 - - - - ProjectExplorer.Project.Target.1 Desktop Qt 5.9.3 MSVC2017 64bit Desktop Qt 5.9.3 MSVC2017 64bit @@ -539,7 +292,7 @@ DataStructureProject.pro false - D:/wming/Documents/build-DataStructureProject-Desktop_Qt_5_9_3_MinGW_32bit-Debug + D:/wming/Documents/build-DataStructureProject-Desktop_Qt_5_9_3_MSVC2017_64bit-Debug 3768 false true @@ -552,7 +305,7 @@ ProjectExplorer.Project.TargetCount - 2 + 1 ProjectExplorer.Project.Updater.FileVersion diff --git a/ParkingLotWidget.cpp b/ParkingLotWidget.cpp index d7d8260..294475f 100644 --- a/ParkingLotWidget.cpp +++ b/ParkingLotWidget.cpp @@ -12,11 +12,7 @@ ParkingLotWidget::ParkingLotWidget(QWidget *parent, const QString& xml) : QWidge { resize(1000, 1000); parse_xml(xml); - if (rootLayoutDirection == horizontal) - setLayout(hBoxLayout); - else - setLayout(vBoxLayout); - + setLayout(layout); } @@ -48,30 +44,15 @@ void ParkingLotWidget::parse_xml(const QString & xml) } QDomElement element = doc.documentElement().firstChild().toElement(); QString name = element.tagName(); - if (element.tagName() == "vLayout") + if (element.tagName().contains("Layout")) { - rootLayoutDirection = vertical; - vBoxLayout = parseVbox(element); - } else if (element.tagName() == "hLayout") - { - rootLayoutDirection = horizontal; - hBoxLayout = parseHbox(element); + layout = parseLayout(element); } else { qDebug() << this -> objectName() << "²¼¾ÖÎļþ´íÎó"; } } -inline QHBoxLayout * ParkingLotWidget::parseHbox(const QDomElement & element) -{ - return static_cast(parseLayout(element)); -} - -inline QVBoxLayout * ParkingLotWidget::parseVbox(const QDomElement & element) -{ - return static_cast(parseLayout(element));; -} - QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) { QBoxLayout* layout; @@ -99,13 +80,13 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) this, child.attribute("direction"), n, child.attribute("expendDirection"))); } else if ((name.at(0) == 'v' || name.at(0) == 'h' )&& name.contains("Layout")) - layout->addLayout(parseHbox(child)); + layout->addLayout(parseLayout(child)); else qDebug() << this->objectName() << "´íÎóµÄxml±êÇ©"; child = child.nextSiblingElement(); } + layout->addStretch(); // Ìí¼ÓÉìËõ£¬ÊµÏÖ¾ÓÉÏ»ò¾Ó×ó¶ÔÆë layout->setMargin(0); layout->setSpacing(0); - layout->setContentsMargins(0, 0, 0, 0); return layout; } diff --git a/ParkingLotWidget.h b/ParkingLotWidget.h index a3b74d5..ac54e4e 100644 --- a/ParkingLotWidget.h +++ b/ParkingLotWidget.h @@ -1,7 +1,6 @@ #pragma once #include #include -#include #include #include class ParkingLotWidget: public QWidget @@ -13,11 +12,7 @@ public: ~ParkingLotWidget(); private: void parse_xml(const QString& xml); - inline QHBoxLayout* parseHbox(const QDomElement& element); - inline QVBoxLayout* parseVbox(const QDomElement& element); - enum {horizontal, vertical} rootLayoutDirection; QBoxLayout *parseLayout(const QDomElement& element); - QHBoxLayout *hBoxLayout = nullptr; - QVBoxLayout *vBoxLayout = nullptr; + QBoxLayout *layout = nullptr; }; diff --git a/ParkingSpaceInfo.cpp b/ParkingSpaceInfo.cpp index 292d4a5..cee0a39 100644 --- a/ParkingSpaceInfo.cpp +++ b/ParkingSpaceInfo.cpp @@ -2,10 +2,20 @@ #include #include -ParkingSpaceInfo::ParkingSpaceInfo(QWidget* parent, const direction dir) : QWidget(parent) +ParkingSpaceInfo::ParkingSpaceInfo(QWidget* parent, const QString &dir) { - this->dir = dir; - switch (dir) + direction d = {}; + const char c = dir.at(0).unicode(); + switch (c) + { + case 'N':d = N; break; + case 'E':d = E; break; + case 'W':d = W; break; + case 'S':d = S; break; + default: qDebug() << this->objectName() << "´íÎóµÄ·½Ïò"; + } + this->dir = d; + switch (this->dir) { case N: case S: @@ -21,20 +31,6 @@ ParkingSpaceInfo::ParkingSpaceInfo(QWidget* parent, const direction dir) : QWidg break; } } -ParkingSpaceInfo::ParkingSpaceInfo(QWidget* parent, const QString &dir) -{ - direction d = {}; - const char c = dir.at(0).unicode(); - switch (c) - { - case 'N':d = N; break; - case 'E':d = E; break; - case 'W':d = W; break; - case 'S':d = S; break; - default: qDebug() << this->objectName() << "´íÎóµÄ·½Ïò"; - } - this->ParkingSpaceInfo::ParkingSpaceInfo(parent, d); -} QBoxLayout *ParkingSpaceInfo::makeParkingSapceGroup(QWidget* parent, const QString& dir, int n, const QString & expend) { @@ -48,18 +44,16 @@ QBoxLayout *ParkingSpaceInfo::makeParkingSapceGroup(QWidget* parent, const QStri ParkingSpaceInfo *pk = new ParkingSpaceInfo(parent, dir); layout->addWidget(pk); } + layout->addStretch(); // ¾ÓÉÏ»ò¾Ó×ó¶ÔÆë layout->setMargin(0); layout->setSpacing(0); - layout->setContentsMargins(0, 0, 0, 0); return layout; } void ParkingSpaceInfo::paintEvent(QPaintEvent * event) { QPainter painter(this); - painter.setPen(Qt::blue); - // painter.drawRect(0, 0, this->width(), this->height()); - painter.setPen(Qt::red); + painter.setPen(Qt::white); painter.setBrush(Qt::white); painter.translate(30, 20); switch (dir) { @@ -79,6 +73,9 @@ void ParkingSpaceInfo::paintEvent(QPaintEvent * event) painter.drawRect(0, 0, 60, 5); painter.drawRect(0, 5, 10, 35); painter.drawRect(10, 35, 50, 5); + /*painter.setPen(Qt::blue); + painter.setBrush(Qt::NoBrush); + painter.drawRect(0, 0, this->width() - 1, this->height() - 1);*/ painter.end(); } diff --git a/ParkingSpaceInfo.h b/ParkingSpaceInfo.h index e0b8c4e..a9dbaa3 100644 --- a/ParkingSpaceInfo.h +++ b/ParkingSpaceInfo.h @@ -6,7 +6,6 @@ class ParkingSpaceInfo: public QWidget Q_OBJECT public: enum direction{N, S, E, W}; - ParkingSpaceInfo(QWidget* parent, direction dir); ParkingSpaceInfo(QWidget* parent, const QString &dir); static QBoxLayout* makeParkingSapceGroup(QWidget* parent, const QString& dir, int n, const QString &expend); void paintEvent(QPaintEvent* event) override; diff --git a/Road.cpp b/Road.cpp index 75f6db8..176c83d 100644 --- a/Road.cpp +++ b/Road.cpp @@ -17,11 +17,14 @@ Road::Road(QWidget* parent, const int length, const direction dir) setMaximumSize(50, length); break; } + // setContentsMargins(0, 0, 0, 0); } void Road::paintEvent(QPaintEvent * event) { QPainter painter(this); + /*painter.setPen(Qt::blue); + painter.drawRect(0, 0, width() - 1, height() - 1);*/ painter.setBrush(Qt::white); painter.setPen(Qt::white); auto x = 0, y = 0; diff --git a/mainwindow.cpp b/mainwindow.cpp index 27a07ca..1d414fc 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -5,6 +5,7 @@ #include #include #include "parkinglot.h" +#include "ParkingLotWidget.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), @@ -15,21 +16,23 @@ MainWindow::MainWindow(QWidget *parent) : QScrollArea* scrollArea = new QScrollArea(this); scrollArea->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)); scrollArea->setWidgetResizable(true); - scrollArea->resize(1800, 800); + // 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); + ParkingLotWidget *pk = new ParkingLotWidget(this, "D:\\wming\\Documents\\DataStructureProject\\parkinglot_f1.xml"); + // pk->show(); // pk->creatMap(); - QLabel *label = new QLabel(this); + /*QLabel *label = new QLabel(this); label->resize(1000, 1000); - label->setPixmap(pk->creatMap2()); - scrollArea->setWidget(label); + label->setPixmap(pk->creatMap2());*/ + scrollArea->resize(pk->width(), this->height()); + scrollArea->setWidget(pk); } MainWindow::~MainWindow() { delete ui; - delete monitor; + // delete monitor; } void MainWindow::open(void) { diff --git a/parkinglot.cpp b/parkinglot.cpp index 4f21ac5..a33651b 100644 --- a/parkinglot.cpp +++ b/parkinglot.cpp @@ -28,7 +28,7 @@ ParkingLot::ParkingLot(const QString &fname, QWidget* parent) } } -QPixmap ParkingLot::creatMap2() +QPixmap ParkingLot::creatMap2() const { QDomElement root =doc -> documentElement(); if (root.tagName() != "parkinglot") { @@ -93,24 +93,24 @@ QPixmap drawRoom(const QDomElement& roomXML) { } QPixmap temp(60, 40); result.fill(QColor(200, 200, 200)); - QPainter painter(&result), tPainter(&temp); + QPainter painter(&result), t_painter(&temp); painter.setPen(Qt::white); painter.setBrush(Qt::white); - tPainter.setPen(Qt::white); - tPainter.setBrush(Qt::white); + t_painter.setPen(Qt::white); + t_painter.setBrush(Qt::white); - ushort direction = roomXML.attribute("direction").at(0).unicode(); - int n = roomXML.attribute("number").toInt(); - int x = 0, y = 0; + 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; - tPainter.drawRect(0, 0, 60, 5); - tPainter.drawRect(0, 5, 10, 35); - tPainter.drawRect(10, 35, 50, 5); - tPainter.end(); + 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; @@ -129,7 +129,7 @@ QPixmap drawRoom(const QDomElement& roomXML) { break; } // tPainter.begin(&temp); - for (int i = 0; i < n; i++) { + for (auto i = 0; i < n; i++) { painter.drawPixmap(x, y, temp.width(), temp.height(), temp); if (expendDirection == down) y += temp.height(); @@ -148,7 +148,7 @@ QPixmap ParkingLot::parseLayout(const QDomElement& layout) const result.fill(QColor(200, 200, 200)); QPainter painter(&result); painter.setPen(Qt::black); - int x = 0, y = 0, maxX = 0, maxY = 0; + auto x = 0, y = 0, max_x = 0, max_y = 0; enum {vertical, horizontal} direction; if (layout.tagName() == "vLayout") direction = vertical; @@ -161,7 +161,7 @@ QPixmap ParkingLot::parseLayout(const QDomElement& layout) const QDomElement child = layout.firstChild().toElement(); while(!child.isNull()) { - QString name = child.tagName(); + const QString name = child.tagName(); if (name == "road") { temp = drawRoad(child); } else if (name == "room") { @@ -171,16 +171,16 @@ QPixmap ParkingLot::parseLayout(const QDomElement& layout) const painter.drawPixmap(x, y, temp.width(), temp.height(), temp); if (direction == horizontal) { x += temp.width(); - maxY = std::max(maxY, temp.height()); + max_y = std::max(max_y, temp.height()); } else { y += temp.height(); - maxX = std::max(maxX, temp.width()); + max_x = std::max(max_x, temp.width()); } child = child.nextSibling().toElement(); } if (direction == horizontal) { - return result.copy(0, 0, x, maxY); + return result.copy(0, 0, x, max_y); } else { - return result.copy(0, 0, maxX, y); + return result.copy(0, 0, max_x, y); } } diff --git a/parkinglot.h b/parkinglot.h index 029cd82..85bd61f 100644 --- a/parkinglot.h +++ b/parkinglot.h @@ -1,20 +1,18 @@ #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 creatMap2() const; QPixmap* getPixmap() const; ~ParkingLot(); private: - bool vertical; + // bool vertical; QPixmap* map; QDomDocument* doc; QWidget* parent; diff --git a/parkinglot_f1.xml b/parkinglot_f1.xml index 5826e8e..f14af82 100644 --- a/parkinglot_f1.xml +++ b/parkinglot_f1.xml @@ -2,16 +2,16 @@ - + - + - + - +