From f4874096aa22dca118a8202eb7ff630fd3f3a122 Mon Sep 17 00:00:00 2001 From: wmj Date: Mon, 18 Dec 2017 16:57:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=98=BE=E7=A4=BA=E5=81=9C?= =?UTF-8?q?=E8=BD=A6=E5=9C=BA=E7=9A=84=E6=A0=B7=E5=BC=8F=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E7=8E=B0=E5=9C=A8=E4=B8=8D=E7=94=A8=E5=86=8D=E6=8A=8A?= =?UTF-8?q?xml=E5=A4=8D=E5=88=B6=E5=88=B0=E8=BE=93=E5=87=BA=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataStructureProject.pro | 6 ++-- ParkingLotWidget.cpp | 22 ++++++++++++-- boundary.cpp | 32 +++++++++++++++++++++ boundary.h | 20 +++++++++++++ main.cpp | 1 + mainwindow.h | 2 +- mainwindow.ui | 2 +- monitor.cpp | 25 ++++++++++++---- monitor.h | 2 +- monitor.ui | 62 ++++++++++++++++++++++++++++++++++------ parkinglot_f1.xml | 61 ++++++++++++++++++++++++++++++--------- 11 files changed, 201 insertions(+), 34 deletions(-) create mode 100644 boundary.cpp create mode 100644 boundary.h diff --git a/DataStructureProject.pro b/DataStructureProject.pro index f0fe90f..f9806e4 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -31,7 +31,8 @@ SOURCES += \ ParkingLotWidget.cpp \ Road.cpp \ ParkingSpaceWidget.cpp \ - monitor.cpp + monitor.cpp \ + boundary.cpp HEADERS += \ mainwindow.h \ @@ -39,7 +40,8 @@ HEADERS += \ ParkingLotWidget.h \ Road.h \ ParkingSpaceWidget.h \ - monitor.h + monitor.h \ + boundary.h FORMS += \ mainwindow.ui \ diff --git a/ParkingLotWidget.cpp b/ParkingLotWidget.cpp index 7a06a98..a15ff92 100644 --- a/ParkingLotWidget.cpp +++ b/ParkingLotWidget.cpp @@ -1,5 +1,6 @@ #include "ParkingLotWidget.h" #include "ParkingSpaceWidget.h" +#include "boundary.h" #include #include #include @@ -11,7 +12,7 @@ ParkingLotWidget::ParkingLotWidget(QWidget *parent, const QString& xml) : QWidget(parent) { parse_xml(xml); - setLayout(layout); + setLayout(layout); } @@ -74,6 +75,7 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) layout = new QHBoxLayout; else layout = new QVBoxLayout; + qDebug() << "处理" << element.tagName(); QDomElement child = element.firstChild().toElement(); while (!child.isNull()) @@ -81,6 +83,7 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) QString name = child.tagName(); if (name == "road") { + qDebug() << "处理road"; const int l = child.attribute("length").toInt(); if (child.attribute("direction") == "vertical") layout->addWidget(new Road(this, l, Road::vertical)); @@ -89,13 +92,28 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) } else if (name == "room") { + qDebug() << "处理room"; const auto n = child.attribute("number", "1").toInt(); layout->addLayout(ParkingSpaceWidget::makeParkingSapceGroup( this, child.attribute("direction"), n, child.attribute("expendDirection"))); } else if ((name.at(0) == 'v' || name.at(0) == 'h' )&& name.contains("Layout")) layout->addLayout(parseLayout(child)); - else + else if (name == "space") { + qDebug() << "处理space"; + QSpacerItem* sw = new QSpacerItem(0, 0); + if (element.tagName() == "hLayout") + sw->changeSize(child.attribute("length").toInt(), 0, QSizePolicy::Fixed, QSizePolicy::Minimum); + else + sw->changeSize(0, child.attribute("length").toInt(), QSizePolicy::Minimum, QSizePolicy::Fixed); + layout->addSpacerItem(sw); + } else if (name == "boundary") { + qDebug() << "处理boundary"; + int l = child.attribute("length").toInt(); + Boundary *b = new Boundary(child.attribute("direction"), l, this); + layout->addWidget(b); + } + else qDebug() << this->objectName() << "错误的xml标签"; child = child.nextSiblingElement(); } diff --git a/boundary.cpp b/boundary.cpp new file mode 100644 index 0000000..f93eade --- /dev/null +++ b/boundary.cpp @@ -0,0 +1,32 @@ +#include "boundary.h" +#include +#include + +Boundary::Boundary(const QString &dir, int l, QWidget *parent): QWidget(parent), length(l) +{ + if (dir == "horizontal") + vertical = false; + else + vertical = true; + if (vertical) { + resize(5, length); + setMaximumSize(5, length); + setMinimumSize(5, length); + } else { + resize(length, 5); + setMaximumSize(length, 5); + setMinimumSize(length, 5); + } +} + +void Boundary::paintEvent(QPaintEvent *) { + QPainter p(this); + p.setPen(Qt::black); + p.setBrush(Qt::black); + if (vertical) { + p.drawRect(0, 0, 5, length); + } else { + p.drawRect(0, 0, length, 5); + } + p.setPen(Qt::red); +} diff --git a/boundary.h b/boundary.h new file mode 100644 index 0000000..e10f7bd --- /dev/null +++ b/boundary.h @@ -0,0 +1,20 @@ +#ifndef BOUNDARY_H +#define BOUNDARY_H + +#include +#include + +class Boundary : public QWidget +{ + Q_OBJECT +public: + explicit Boundary(const QString& dir, int length, QWidget *parent = nullptr); + +private: + bool vertical; + int length; +protected: + void paintEvent(QPaintEvent *); +}; + +#endif // BOUNDARY_H diff --git a/main.cpp b/main.cpp index 3e3e1c2..9ddeb0c 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,7 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); qDebug() << QCoreApplication::applicationDirPath(); + qDebug() << __FILE__; MainWindow w; w.show(); diff --git a/mainwindow.h b/mainwindow.h index b035943..a4ef49e 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -19,7 +19,7 @@ public: ~MainWindow(); private: - Ui::MainWindow *ui; + Ui::MainWindow *ui = nullptr; QAction *action; Monitor *monitor; }; diff --git a/mainwindow.ui b/mainwindow.ui index fa3e268..d537613 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -7,7 +7,7 @@ 0 0 1800 - 800 + 900 diff --git a/monitor.cpp b/monitor.cpp index f182c48..2651244 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -34,6 +34,19 @@ Monitor::Monitor(QWidget *parent) : ui->downStairButton->setEnabled(false); else if (m_currentFloor == m_parkinglots.size() - 1) ui->upStairButton->setEnabled(false); + + + QString filePath = __FILE__; + QStringList list = filePath.split("\\"); + QString result; + for (int i = 0; i < list.size() - 1; i++) + result.append(list.at(i)).append("\\"); + QStringList l = {"parkinglot_f1.xml", "parkinglot_b1.xml"}; + for (int i = 0; i < 2; i++) { + QString t(result); + m_xmlPath.push_back(t.append(l.at(i))); + } + showParkinglot(); } @@ -44,13 +57,15 @@ Monitor::~Monitor() 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() + "
"); + if (m_parkinglots.at(m_currentFloor) == nullptr) { + m_parkinglots[m_currentFloor] = new ParkingLotWidget(this, m_xmlPath.at(m_currentFloor)); // lazy + // ui->parkinglotView->addWidget(m_parkinglots.at(m_currentFloor)); // 把当前对象添加进layout + m_parkinglots[m_currentFloor]->move(10, 10); + } + 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 eb190c2..5c1e030 100644 --- a/monitor.h +++ b/monitor.h @@ -22,7 +22,7 @@ private: Ui::Monitor *ui; // TODO: 动态修改 std::vector m_parkinglots = {nullptr, nullptr}; // lazy - std::vector m_xmlPath = {"parkinglot_b1.xml", "parkinglot_f1.xml"}; + std::vector m_xmlPath;// = {"parkinglot_b1.xml", "parkinglot_f1.xml"}; uint m_currentFloor = 1; // 默认为地上一层 void showParkinglot(void); diff --git a/monitor.ui b/monitor.ui index d731576..af549f4 100644 --- a/monitor.ui +++ b/monitor.ui @@ -7,7 +7,7 @@ 0 0 1080 - 800 + 900
@@ -27,14 +27,11 @@ QLayout::SetDefaultConstraint - - - - 1000 - 800 - + + + 0 - + @@ -79,6 +76,55 @@ + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + 共有车位 + + + + + + + 100 + + + + + + + + + + + 剩余 + + + + + + + 90 + + + + + diff --git a/parkinglot_f1.xml b/parkinglot_f1.xml index 127d657..01f4020 100644 --- a/parkinglot_f1.xml +++ b/parkinglot_f1.xml @@ -1,20 +1,53 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - \ No newline at end of file