diff --git a/DataStructureProject.pro b/DataStructureProject.pro index 062fa56..f0fe90f 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -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 \ diff --git a/ParkingLotWidget.cpp b/ParkingLotWidget.cpp index 67fa634..7a06a98 100644 --- a/ParkingLotWidget.cpp +++ b/ParkingLotWidget.cpp @@ -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; +} diff --git a/ParkingLotWidget.h b/ParkingLotWidget.h index 24eb3ac..23df63f 100644 --- a/ParkingLotWidget.h +++ b/ParkingLotWidget.h @@ -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 m_spaceList; void parse_xml(const QString& xml); QBoxLayout *parseLayout(const QDomElement& element); QBoxLayout *layout = nullptr; + QString name; }; #endif diff --git a/ParkingSpaceWidget.h b/ParkingSpaceWidget.h index d5fbc4f..8539d4b 100644 --- a/ParkingSpaceWidget.h +++ b/ParkingSpaceWidget.h @@ -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; diff --git a/Road.cpp b/Road.cpp index 176c83d..3b4e652 100644 --- a/Road.cpp +++ b/Road.cpp @@ -1,5 +1,6 @@ #include "Road.h" #include +#include 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);*/ diff --git a/main.cpp b/main.cpp index b48f94e..3e3e1c2 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,13 @@ #include "mainwindow.h" #include +#include +#include +#include int main(int argc, char *argv[]) { QApplication a(argc, argv); + qDebug() << QCoreApplication::applicationDirPath(); MainWindow w; w.show(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 036d000..ba56f98 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -4,29 +4,14 @@ #include #include #include -#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() diff --git a/monitor.cpp b/monitor.cpp index 84a38f1..f182c48 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -1,17 +1,56 @@ #include "monitor.h" -#include +#include "ui_monitor.h" +#include +#include -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("
" + m_parkinglots.at(m_currentFloor)->getName() + "
"); + 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); } diff --git a/monitor.h b/monitor.h index f16fce5..eb190c2 100644 --- a/monitor.h +++ b/monitor.h @@ -2,19 +2,30 @@ #define MONITOR_H #include +#include +#include +#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 m_parkinglots = {nullptr, nullptr}; // lazy + std::vector m_xmlPath = {"parkinglot_b1.xml", "parkinglot_f1.xml"}; + uint m_currentFloor = 1; // 默认为地上一层 -signals: - -public slots: + void showParkinglot(void); }; #endif // MONITOR_H diff --git a/monitor.ui b/monitor.ui new file mode 100644 index 0000000..d731576 --- /dev/null +++ b/monitor.ui @@ -0,0 +1,102 @@ + + + Monitor + + + + 0 + 0 + 1080 + 800 + + + + Form + + + + + 220 + 60 + 811 + 581 + + + + + QLayout::SetDefaultConstraint + + + + + + 1000 + 800 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + QLayout::SetDefaultConstraint + + + + + TextLabel + + + + + + + + + 上一层 + + + + + + + 下一层 + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + diff --git a/parkinglot.cpp b/parkinglot.cpp deleted file mode 100644 index a33651b..0000000 --- a/parkinglot.cpp +++ /dev/null @@ -1,186 +0,0 @@ -#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)); - } -} - -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); - } -} diff --git a/parkinglot.h b/parkinglot.h deleted file mode 100644 index 85bd61f..0000000 --- a/parkinglot.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef PARKINGLOT_H -#define PARKINGLOT_H -#include -#include -#include -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 diff --git a/parkinglot_b1.xml b/parkinglot_b1.xml new file mode 100644 index 0000000..e554476 --- /dev/null +++ b/parkinglot_b1.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/parkinglot_f1.xml b/parkinglot_f1.xml index f14af82..127d657 100644 --- a/parkinglot_f1.xml +++ b/parkinglot_f1.xml @@ -1,4 +1,4 @@ - +