From c0bc0c24be8df13cb4a21bfd8ded8e07e65ebc98 Mon Sep 17 00:00:00 2001 From: w-mj Date: Tue, 26 Dec 2017 10:13:49 +0800 Subject: [PATCH] Log --- car.h | 2 ++ logindialog.cpp | 1 + logwindow.cpp | 21 +++++++++++ logwindow.h | 3 ++ logwindow.ui | 82 ++++++++++++++++++++++++++++--------------- monitor.cpp | 14 ++++---- parkingLotManager.cpp | 37 ++++++++++++++----- 7 files changed, 116 insertions(+), 44 deletions(-) diff --git a/car.h b/car.h index b884e0b..797aebd 100644 --- a/car.h +++ b/car.h @@ -80,6 +80,8 @@ public: void requestSpace(void); void go(void); + bool requested = false; + private: Status m_status; Color m_color; //车身颜色 N为0, 顺时针递增 diff --git a/logindialog.cpp b/logindialog.cpp index be6ebc3..4f3298c 100644 --- a/logindialog.cpp +++ b/logindialog.cpp @@ -65,6 +65,7 @@ void LoginDialog::ok() f.write(psd.toStdString().c_str()); first = false; } + f.close(); } else { ui->hint->setText("两次密码不匹配"); } diff --git a/logwindow.cpp b/logwindow.cpp index f41450a..965e1c1 100644 --- a/logwindow.cpp +++ b/logwindow.cpp @@ -3,6 +3,7 @@ #include #include +#include LogWindow* LogWindow::m_instance = nullptr; @@ -18,6 +19,24 @@ LogWindow::LogWindow(QWidget *parent) : setGeometry(parent->x() + parent->width(), y(), 400, 450); this->hide(); setFocusPolicy(Qt::NoFocus); + connect(ui->save, &QPushButton::clicked, this, &LogWindow::save); + connect(ui->clear, &QPushButton::clicked, this, &LogWindow::clear); +} + +void LogWindow::clear() +{ + ui->text->clear(); +} + +void LogWindow::save() +{ + QFile f(QString("Log_%1.log").arg(QTime::currentTime().toString("hh_mm_ss"))); + if (f.open(QFile::WriteOnly| QIODevice::Text)) { + i("保存文件"); + f.write(buffer.toStdString().c_str()); + buffer.clear(); + } + f.close(); } LogWindow::~LogWindow() @@ -35,6 +54,8 @@ LogWindow *LogWindow::newInstance(QWidget *parent) void LogWindow::i(QString text) { getInstance()->text->append(QTime::currentTime().toString("hh:mm:ss")+":"+text); + getInstance()->buffer.append(QTime::currentTime().toString("hh:mm:ss")+":"+text); + getInstance()->buffer.append("\n"); } void LogWindow::closeEvent(QCloseEvent *e) diff --git a/logwindow.h b/logwindow.h index 28a109b..95679e8 100644 --- a/logwindow.h +++ b/logwindow.h @@ -26,9 +26,12 @@ signals: private: static LogWindow* m_instance; explicit LogWindow(QWidget *parent = 0); + void clear(void); + void save(void); Ui::LogWindow *ui; QTextEdit * text; + QString buffer; }; #endif // LOGWINDOW_H diff --git a/logwindow.ui b/logwindow.ui index b388fe1..baeb606 100644 --- a/logwindow.ui +++ b/logwindow.ui @@ -13,41 +13,65 @@ Form - + 0 0 - 400 - 450 + 401 + 451 - - true - - - - - 0 - 0 - 398 - 448 - - - - - - 0 - 0 - 400 - 450 - - - - true - - - + + + + + + + 清除 + + + + + + + 保存 + + + + + + + + + true + + + + + 0 + 0 + 397 + 414 + + + + + + 0 + 0 + 400 + 411 + + + + true + + + + + + diff --git a/monitor.cpp b/monitor.cpp index 71d0ce9..69a0a71 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -37,12 +37,14 @@ Monitor::Monitor(MainWindow *parent) : ui->sequence->hide(); QObject::connect(ui->addCar, &QPushButton::clicked, [this, pk]() { QString p = this->ui->plateNumBox->text(); - if(checkPlate(p) == false) { - QMessageBox::warning(this, "警告", "车牌不合规则"); - } else if (pk->checkSame(p)) { - QMessageBox::warning(this, "警告", "车牌重复"); - } else { - pk->addCar(p, ui->selectColor->currentIndex(), -1); + if (p.size() != 0) { + if(checkPlate(p) == false) { + QMessageBox::warning(this, "警告", "车牌不合规则"); + } else if (pk->checkSame(p)) { + QMessageBox::warning(this, "警告", "车牌重复"); + } else { + pk->addCar(p, ui->selectColor->currentIndex(), -1); + } } }); QObject::connect(ui->random, &QPushButton::clicked, pk, &ParkingLotManager::addCarR); diff --git a/parkingLotManager.cpp b/parkingLotManager.cpp index f197be8..4002ce8 100644 --- a/parkingLotManager.cpp +++ b/parkingLotManager.cpp @@ -238,7 +238,7 @@ void ParkingLotManager::requestOut(Car *car, int exit) else p = m_graph[car->getCurrentFloor()]-> findPath(NODE_TYPE::space, car->getNum(), NODE_TYPE::stair, exit); - if (m_pool.size() < m_max) + if (GET_SITUATION(car->getCurrentFloor(), car->getNum()-1) != BANNED) m_pool.append(qMakePair(car->getCurrentFloor(), car->getNum() - 1)); m_widgets[car->getCurrentFloor()]-> getSpaceList()[car->getNum() - 1]->setSituation(ParkingSpaceWidget::free); @@ -281,6 +281,7 @@ void ParkingLotManager::requestBack(Car *car) void ParkingLotManager::requestIn(Car* car) { int entry = car->getEntryNum(); + car->requested = true; if (!m_pool.empty()) { uint l = m_pool.first().first; int n = m_pool.first().second + 1; @@ -328,13 +329,12 @@ void ParkingLotManager::leave(Car* car) void ParkingLotManager::addCarR() { - Log::i("生成车"); - int entry = (rand() % m_num_of_entry); if (m_waitting[entry].size() > 10) return; Car *car = new Car(this); + Log::i(QString("生成车:").arg(car->getPlateNumber())); car->setEntryNum(entry + 1); m_waitting[entry].append(car); Path *p = m_graph[1]->findPath(NODE_TYPE::entry, entry + 1, NODE_TYPE::queueHead, entry + 1); @@ -368,6 +368,7 @@ void ParkingLotManager::addCar(QString plate, int color, int entry) car->setStatus(Car::waiting); m_all_cars.append(car); m_scene->addItem(car); + Log::i(QString("生成车:").arg(car->getPlateNumber())); if (m_current_floor != 1) car->hide(); } @@ -405,12 +406,13 @@ void ParkingLotManager::generatePool(bool sequence, int max) for (int l = m_num_of_layer - 1; l >= 0; l--) { for (int n = 0; n < m_capacity[l]; n++) { if (max-- == 0) - return; + goto OUT; if (GET_SITUATION(l, n) == FREE) { m_pool.append(qMakePair(l, n)); } } } +OUT: if (!sequence) std::random_shuffle(m_pool.begin(), m_pool.end()); Log::i(QString("生成车位池成功,当前共有") + QString::number(m_pool.size()) + "个空车位"); @@ -424,27 +426,44 @@ void ParkingLotManager::periodWork() for (auto c: m_all_cars) { c->leaveProbability(2); } + callIn(); } void ParkingLotManager::callIn() { for (uint i = 0 ; i < m_num_of_entry; i++) { - if (m_waitting[lastInEntry].size() == 0) - lastInEntry = (lastInEntry + 1) % m_num_of_entry ; - else { + if (m_waitting[lastInEntry].size() != 0) { Log::i(QString("从%1召唤车").arg(lastInEntry)); Car *c = m_waitting[lastInEntry][0]; - c->requestSpace(); + if (c->requested) + c->requestSpace(); } + lastInEntry = (lastInEntry + 1) % m_num_of_entry ; } } void ParkingLotManager::setMax(int max) { int n = m_max; + int mmax = max; m_max = max; Log::i(QString("设置最大容量%1").arg(max)); - generatePool(m_sequence, max); + for (int l = m_num_of_layer - 1; l >= 0; l--) { + for (int n = 0; n < m_capacity[l]; n++) { + if (max > 0) { + if (GET_SITUATION(l, n) != OCCUPIED) { + m_widgets.at(l)->getSpaceList().at(n)->setSituation(FREE); + m_widgets.at(l)->getSpaceList().at(n)->update(); + } + max--; + } + else { + m_widgets.at(l)->getSpaceList().at(n)->setSituation(BANNED); + m_widgets.at(l)->getSpaceList().at(n)->update(); + } + } + } + generatePool(m_sequence, mmax); if (max - n == 1) callIn(); else if (max - n >= 2) {