From 1b69823b7257d432d08c4a2fc3123c5d5d03edfc Mon Sep 17 00:00:00 2001 From: w-mj Date: Sat, 23 Dec 2017 20:17:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=AA=97=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataStructureProject.pro | 9 ++++--- ParkingLotWidget.h | 1 + ParkingSpaceWidget.cpp | 25 +++++++++++++++--- ParkingSpaceWidget.h | 9 ++++++- logwindow.cpp | 41 ++++++++++++++++++++++++++++++ logwindow.h | 31 ++++++++++++++++++++++ logwindow.ui | 55 ++++++++++++++++++++++++++++++++++++++++ mainwindow.cpp | 15 +++++++++++ mainwindow.h | 4 +++ monitor.cpp | 8 +++--- monitor.h | 5 +++- monitor.ui | 20 ++++++++++++++- parkingLotManager.cpp | 53 +++++++++++++++++++++++++++++++------- parkingLotManager.h | 6 ++++- 14 files changed, 259 insertions(+), 23 deletions(-) create mode 100644 logwindow.cpp create mode 100644 logwindow.h create mode 100644 logwindow.ui diff --git a/DataStructureProject.pro b/DataStructureProject.pro index a8c944e..9a7ffab 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -37,7 +37,8 @@ SOURCES += \ car.cpp \ path.cpp \ parkingLotManager.cpp \ - parkinglotgraph.cpp + parkinglotgraph.cpp \ + logwindow.cpp HEADERS += \ mainwindow.h \ @@ -50,11 +51,13 @@ HEADERS += \ car.h \ path.h \ parkingLotManager.h \ - parkinglotgraph.h + parkinglotgraph.h \ + logwindow.h FORMS += \ mainwindow.ui \ - monitor.ui + monitor.ui \ + logwindow.ui RESOURCES += \ cars.qrc \ diff --git a/ParkingLotWidget.h b/ParkingLotWidget.h index 7512132..98f63c8 100644 --- a/ParkingLotWidget.h +++ b/ParkingLotWidget.h @@ -36,6 +36,7 @@ private: signals: void showMargain(bool); + void banParkingSpace(bool, uint); }; #endif diff --git a/ParkingSpaceWidget.cpp b/ParkingSpaceWidget.cpp index 0de202c..7963e83 100644 --- a/ParkingSpaceWidget.cpp +++ b/ParkingSpaceWidget.cpp @@ -44,6 +44,7 @@ ParkingSpaceWidget::ParkingSpaceWidget(ParkingLotWidget* parent, const QString & this->m_showMargin = b; update(); }); + QObject::connect(this, &ParkingSpaceWidget::ban, parent, &ParkingLotWidget::banParkingSpace); } QBoxLayout *ParkingSpaceWidget::makeParkingSapceGroup(ParkingLotWidget* parent, const QString& dir, int n, const QString & expend) @@ -100,7 +101,7 @@ void ParkingSpaceWidget::paintEvent(QPaintEvent *) painter.setBrush(Qt::NoBrush); painter.drawRect(0, 0, width() - 1, height() - 1); } - if (m_banned) { + if (m_situation == banned) { QPen p; p.setBrush(Qt::black); p.setWidth(4); @@ -118,6 +119,16 @@ void ParkingSpaceWidget::paintEvent(QPaintEvent *) painter.end(); } +ParkingSpaceWidget::Situation ParkingSpaceWidget::getSituation() const +{ + return m_situation; +} + +void ParkingSpaceWidget::setSituation(const Situation &value) +{ + m_situation = value; +} + int ParkingSpaceWidget::getNumber() const { return number; @@ -130,11 +141,17 @@ ParkingSpaceWidget::direction ParkingSpaceWidget::getDir() const void ParkingSpaceWidget::mouseDoubleClickEvent(QMouseEvent *) { - if (m_banned) + if (m_situation == banned) { qDebug() << "禁用解除" << number; - else + emit ban(false, number); + m_situation = free; + } else if (m_situation == free) { qDebug() << "禁用车位" << number; - m_banned = !m_banned; + emit ban(true, number); + m_situation = banned; + } else { + qDebug() << "车位已被占用"; + } update(); } diff --git a/ParkingSpaceWidget.h b/ParkingSpaceWidget.h index 4915b56..18e5856 100644 --- a/ParkingSpaceWidget.h +++ b/ParkingSpaceWidget.h @@ -11,6 +11,7 @@ class ParkingSpaceWidget: public QWidget Q_OBJECT public: enum direction{N, S, E, W}; + enum Situation{banned, occupied, free}; ParkingSpaceWidget(ParkingLotWidget* parent, const QString &dir); static QBoxLayout* makeParkingSapceGroup(ParkingLotWidget* parent, const QString& dir, int n, const QString &expend); ~ParkingSpaceWidget(); @@ -18,13 +19,19 @@ public: void mouseDoubleClickEvent(QMouseEvent *event); int getNumber() const; + Situation getSituation() const; + void setSituation(const Situation &value); + protected: void paintEvent(QPaintEvent* ) override; private: bool m_showMargin = false; int number; direction dir; - bool m_banned = false; + Situation m_situation = free; + +signals: + void ban(bool, int); }; #endif diff --git a/logwindow.cpp b/logwindow.cpp new file mode 100644 index 0000000..153716a --- /dev/null +++ b/logwindow.cpp @@ -0,0 +1,41 @@ +#include "logwindow.h" +#include "ui_logwindow.h" + +#include + + +LogWindow* LogWindow::m_instance = nullptr; + +LogWindow::LogWindow(QWidget *parent) : + QWidget(parent, Qt::Dialog), + ui(new Ui::LogWindow) +{ + ui->setupUi(this); + text = ui->text; + setWindowTitle("日志窗口"); + setGeometry(parent->x() + parent->width(), y(), 400, 450); + this->hide(); + setFocusPolicy(Qt::NoFocus); +} + +LogWindow::~LogWindow() +{ + delete ui; +} + +LogWindow *LogWindow::getInstance(QWidget *parent) +{ + if (m_instance == nullptr) + m_instance = new LogWindow(parent); + return m_instance; +} + +void LogWindow::i(QString text) +{ + getInstance()->text->append(QTime::currentTime().toString("hh:mm:ss")+":"+text); +} + +LogWindow *LogWindow::getInstance() +{ + return m_instance; +} diff --git a/logwindow.h b/logwindow.h new file mode 100644 index 0000000..d936c9b --- /dev/null +++ b/logwindow.h @@ -0,0 +1,31 @@ +#ifndef LOGWINDOW_H +#define LOGWINDOW_H + +#include +#include + +#define Log LogWindow + +namespace Ui { +class LogWindow; +} + +class LogWindow : public QWidget +{ + Q_OBJECT + +public: + ~LogWindow(); + static LogWindow* getInstance(); + static LogWindow* getInstance(QWidget* parent); + static void i(QString text); + +private: + static LogWindow* m_instance; + explicit LogWindow(QWidget *parent = 0); + + Ui::LogWindow *ui; + QTextEdit * text; +}; + +#endif // LOGWINDOW_H diff --git a/logwindow.ui b/logwindow.ui new file mode 100644 index 0000000..b388fe1 --- /dev/null +++ b/logwindow.ui @@ -0,0 +1,55 @@ + + + LogWindow + + + + 0 + 0 + 400 + 450 + + + + Form + + + + + 0 + 0 + 400 + 450 + + + + true + + + + + 0 + 0 + 398 + 448 + + + + + + 0 + 0 + 400 + 450 + + + + true + + + + + + + + diff --git a/mainwindow.cpp b/mainwindow.cpp index d568e56..a73d71f 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -4,12 +4,15 @@ #include #include #include +#include +#include #include "ParkingLotWidget.h" #include "monitor.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { + logWindow = LogWindow::getInstance(this); Monitor *monitor = new Monitor(this); resize(monitor->size()); } @@ -18,6 +21,7 @@ MainWindow::~MainWindow() { delete ui; // delete monitor; + // delete logWindow; } void MainWindow::open(void) { @@ -25,3 +29,14 @@ void MainWindow::open(void) { dialog.setWindowTitle(tr("Hello, dialog!")); dialog.exec(); } + +void MainWindow::showLogWindow(bool enable) +{ + if (enable) { + qDebug() << x() << " " << y() << " " << width(); + logWindow->setGeometry(x()+width() + 15, y() + 35, 400, 450); + logWindow->show(); + setFocus(); + } else + logWindow->hide(); +} diff --git a/mainwindow.h b/mainwindow.h index a4ef49e..912527b 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -4,7 +4,9 @@ #include #include #include "monitor.h" +#include "logwindow.h" +class Monitor; namespace Ui { class MainWindow; } @@ -16,10 +18,12 @@ class MainWindow : public QMainWindow public: explicit MainWindow(QWidget *parent = 0); void open(void); + void showLogWindow(bool enable); ~MainWindow(); private: Ui::MainWindow *ui = nullptr; + LogWindow* logWindow; QAction *action; Monitor *monitor; }; diff --git a/monitor.cpp b/monitor.cpp index 388da7b..1056bf6 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -2,24 +2,26 @@ #include "ui_monitor.h" #include #include +#include #include "parkingLotManager.h" -Monitor::Monitor(QWidget *parent) : +Monitor::Monitor(MainWindow *parent) : QWidget(parent), ui(new Ui::Monitor) { ui->setupUi(this); QGraphicsScene *scene = new QGraphicsScene(this); // 创建场景 ParkingLotManager *pk = new ParkingLotManager(this, scene); - QObject::connect(ui->downStairButton, &QPushButton::clicked, pk, &ParkingLotManager::showDownStairFloor); - QObject::connect(ui->upStairButton, &QPushButton::clicked, pk, &ParkingLotManager::showUpStairFloor); QObject::connect(pk, &ParkingLotManager::enableUpButton, ui->upStairButton, &QPushButton::setEnabled); QObject::connect(pk, &ParkingLotManager::enableDownButton, ui->downStairButton, &QPushButton::setEnabled); QObject::connect(pk, &ParkingLotManager::setCapacity, ui->capacity, &QLabel::setText); QObject::connect(pk, &ParkingLotManager::setLoad, ui->load, &QLabel::setText); QObject::connect(pk, &ParkingLotManager::setLayerName, ui->layerName, &QLabel::setText); + QObject::connect(ui->downStairButton, &QPushButton::clicked, pk, &ParkingLotManager::showDownStairFloor); + QObject::connect(ui->upStairButton, &QPushButton::clicked, pk, &ParkingLotManager::showUpStairFloor); QObject::connect(ui->showMargin, &QCheckBox::toggled, pk, &ParkingLotManager::showMargin); QObject::connect(ui->showGraph, &QCheckBox::toggled, pk, &ParkingLotManager::showGraph); + QObject::connect(ui->showLog, &QCheckBox::toggled, parent, &MainWindow::showLogWindow); QObject::connect(ui->drawPath, &QPushButton::clicked, [=](){ pk->drawPath(ui->placeNum1->text().toInt(), ui->placeNum2->text().toInt()); }); diff --git a/monitor.h b/monitor.h index b4e9364..c91bac6 100644 --- a/monitor.h +++ b/monitor.h @@ -4,8 +4,11 @@ #include #include #include +#include #include "ParkingLotWidget.h" +class MainWindow; + namespace Ui { class Monitor; } @@ -15,7 +18,7 @@ class Monitor : public QWidget Q_OBJECT public: - explicit Monitor(QWidget *parent = 0); + explicit Monitor(MainWindow *parent = 0); ~Monitor(); private: diff --git a/monitor.ui b/monitor.ui index 0b331f3..1488c96 100644 --- a/monitor.ui +++ b/monitor.ui @@ -67,7 +67,7 @@ - + QLayout::SetDefaultConstraint @@ -230,6 +230,24 @@ + + + + + + 显示日志 + + + + + + + 显示总览列表 + + + + + diff --git a/parkingLotManager.cpp b/parkingLotManager.cpp index 885be3d..a8d5dc5 100644 --- a/parkingLotManager.cpp +++ b/parkingLotManager.cpp @@ -8,6 +8,13 @@ #include #include #include +#include "logwindow.h" + +#define GET_SITUATION(l, n) (m_widgets[l]->getSpaceList().at(n)->getSituation()) +#define BANNED (ParkingSpaceWidget::Situation::banned) +#define OCCUPIED (ParkingSpaceWidget::Situation::occupied) +#define FREE (ParkingSpaceWidget::Situation::free) + ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scene): QObject(objectParent), m_scene(scene) @@ -33,14 +40,16 @@ ParkingLotManager::ParkingLotManager(QObject* objectParent, QGraphicsScene* scen m_capacity.push_back(widget->getCapacity()); // 获得这一层的最大容量 m_cars[i].resize(m_capacity.at(i)); // 初始化一层的车位 m_name.push_back(widget->getName()); // 这一层的名字 - // m_num_of_cars.push_back(0); // 初始停车数量为0 + m_num_of_cars.push_back(0); // 初始停车数量为0 widget->hide(); m_widgets.push_back(widget); m_scene->addWidget(widget); m_graph.push_back(new ParkingLotGraph(widget)); m_graph_pixmap.push_back(new QGraphicsPixmapItem(*m_graph.last()->getPixmap())); QObject::connect(this, &ParkingLotManager::showMarginSignal, widget, &ParkingLotWidget::showMarginSlot); + QObject::connect(widget, &ParkingLotWidget::banParkingSpace, [i, this](bool b, int n){banSpace(b, i, n);}); } + generatePool(true); } void ParkingLotManager::showParkingLot(uint pos) @@ -59,7 +68,7 @@ void ParkingLotManager::showParkingLot(uint pos) m_scene->addItem(m_graph_pixmap.at(m_current_floor)); } emit setCapacity(QString::number(m_capacity.at(pos))); - emit setLoad(QString::number(m_capacity.at(pos) - m_cars.at(pos).size())); + emit setLoad(QString::number(m_capacity.at(pos) - m_num_of_cars[pos])); emit setLayerName(m_name.at(pos)); } @@ -116,13 +125,14 @@ void ParkingLotManager::drawPath(int n1, int n2) ParkingLotManager::RequestSpace ParkingLotManager::request(uint entry) { - for (int layer = m_num_of_layer - 1; layer >= 0; layer--) { - if (m_cars[layer].size() != m_capacity[layer]) { - int n = m_cars[layer].size(); - Path* p = m_graph[layer]->finaPath(ParkingLotGraph::Node::Type::entry, entry, - ParkingLotGraph::Node::Type::space, n); - return RequestSpace(layer, n, p); - } + if (!m_pool.empty()) { + int l = m_pool.first().first; + int n = m_pool.first().second; + m_pool.removeAt(0); + Path* p = m_graph[l]->finaPath(ParkingLotGraph::Node::Type::entry, entry, + ParkingLotGraph::Node::Type::space, n); + Log::i(QString("分配第") + l + "层" + n + "号车位,剩余" + QString::number(m_pool.size()) + "个空车位"); + return RequestSpace(l, n, p); } return RequestSpace(); } @@ -139,3 +149,28 @@ void ParkingLotManager::showGraph(bool enable) m_showGraph = enable; showParkingLot(); } + +void ParkingLotManager::banSpace(bool banned, int layer, int number) +{ + if (banned) { + m_pool.removeOne(qMakePair(layer, number)); + Log::i(QString("禁用成功,当前共有") + QString::number(m_pool.size()) + "个空车位"); + } else { + m_pool.append(qMakePair(layer, number)); + Log::i(QString("解禁成功,当前共有") + QString::number(m_pool.size()) + "个空车位"); + } +} + +void ParkingLotManager::generatePool(bool sequence) +{ + for (int l = m_num_of_layer - 1; l >= 0; l--) { + for (int n = 0; n < m_capacity[l]; n++) { + if (GET_SITUATION(l, n) == FREE) { + m_pool.append(qMakePair(l, n)); + } + } + } + if (!sequence) + std::random_shuffle(m_pool.begin(), m_pool.end()); + Log::i(QString("生成车位池成功,当前共有") + QString::number(m_pool.size()) + "个空车位"); +} diff --git a/parkingLotManager.h b/parkingLotManager.h index 2c2a81d..3cda7d7 100644 --- a/parkingLotManager.h +++ b/parkingLotManager.h @@ -32,23 +32,27 @@ public: void showMargin(bool enable); void showGraph(bool enable); + void banSpace(bool banned, int layer, int number); private: QGraphicsScene* m_scene; QVector m_widgets; // 每一层的widget QVector m_capacity; // 每一层的容量 - // QVector m_num_of_cars; // 每一层当前停的车数量 + QVector m_num_of_cars; // 每一层当前停的车数量 QVector > m_cars; // 停在每一层的车 QVector >m_waitting; // 在每个入口等待的车 QVector m_graph; QVector m_graph_pixmap; QStringList m_xml_path; // 每一层xml文件地址 QStringList m_name; // 每一层的名字 + QList> m_pool; // 车位池 const uint m_num_of_layer = 2; // 总层数 const uint m_num_of_entry = 2; // 入口数 uint m_current_floor = 1; // 当前楼层,默认为1楼(按照从下往上递增,值与实际楼层无关) bool m_showGraph = false; // 是否显示图 + void generatePool(bool sequence); + signals: void enableUpButton(bool); void enableDownButton(bool);