diff --git a/DataStructureProject.pro b/DataStructureProject.pro index aa3e3be..15353a8 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -39,7 +39,9 @@ SOURCES += \ parkingLotManager.cpp \ parkinglotgraph.cpp \ logwindow.cpp \ - carlist.cpp + carlist.cpp \ + logindialog.cpp \ + utils.cpp HEADERS += \ mainwindow.h \ @@ -54,13 +56,16 @@ HEADERS += \ parkingLotManager.h \ parkinglotgraph.h \ logwindow.h \ - carlist.h + carlist.h \ + logindialog.h \ + utils.h FORMS += \ mainwindow.ui \ monitor.ui \ logwindow.ui \ - carlist.ui + carlist.ui \ + logindialog.ui RESOURCES += \ cars.qrc \ diff --git a/ParkingSpaceWidget.cpp b/ParkingSpaceWidget.cpp index 7963e83..dda1e3d 100644 --- a/ParkingSpaceWidget.cpp +++ b/ParkingSpaceWidget.cpp @@ -9,6 +9,8 @@ #include #include #include "ParkingLotWidget.h" +#include "logindialog.h" +#include "logwindow.h" ParkingSpaceWidget::ParkingSpaceWidget(ParkingLotWidget* parent, const QString &dir): QWidget(parent) { @@ -141,18 +143,23 @@ ParkingSpaceWidget::direction ParkingSpaceWidget::getDir() const void ParkingSpaceWidget::mouseDoubleClickEvent(QMouseEvent *) { - if (m_situation == banned) { - qDebug() << "禁用解除" << number; - emit ban(false, number); - m_situation = free; - } else if (m_situation == free) { - qDebug() << "禁用车位" << number; - emit ban(true, number); - m_situation = banned; + if (REQUIRE_PREMISSION) { + Log::i("请求权限成功"); + if (m_situation == banned) { + qDebug() << "禁用解除" << number; + emit ban(false, number); + m_situation = free; + } else if (m_situation == free) { + qDebug() << "禁用车位" << number; + emit ban(true, number); + m_situation = banned; + } else { + qDebug() << "车位已被占用"; + } + update(); } else { - qDebug() << "车位已被占用"; + Log::i("请求权限失败"); } - update(); } ParkingSpaceWidget::~ParkingSpaceWidget() diff --git a/logindialog.cpp b/logindialog.cpp new file mode 100644 index 0000000..be6ebc3 --- /dev/null +++ b/logindialog.cpp @@ -0,0 +1,87 @@ +#include "logindialog.h" +#include "ui_logindialog.h" +#include "logwindow.h" +#include + +LoginDialog::LoginDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::LoginDialog) +{ + ui->setupUi(this); + QFile f("QCore.dll"); + if (f.open(QFile::ReadOnly| QIODevice::ReadOnly | QIODevice::Text)) { + QTextStream in(&f); + in >> id; + in >> psd; + if (id.size() != 0 && psd.size() != 0) + first = false; + } + hide(); + if (first) { + setWindowTitle("第一次使用,请注册"); + ui->repeat->show(); + ui->repeat_label->show(); + } + else { + setWindowTitle("登陆"); + ui->repeat->hide(); + ui->repeat_label->hide(); + } + QPalette p(QPalette::WindowText,Qt::red); + ui->hint->setPalette(p); + setModal(true); + connect(ui->ok, &QPushButton::clicked, this, &LoginDialog::ok); + connect(ui->cancel, &QPushButton::clicked, this, &LoginDialog::cancel); +} + + +bool LoginDialog::getPermission() +{ + if (login) + return true; + return exec(); +} + +LoginDialog::~LoginDialog() +{ + delete ui; +} + +void LoginDialog::ok() +{ + if (first) { + if (!(ui->id->text().size() && ui->psd->text().size() && ui->repeat->text().size())) + ui->hint->setText("所有选项均必填"); + else if (ui->psd->text() == ui->repeat->text()) { + Log::i("注册登陆成功"); + id = ui->id->text(); + psd = ui->psd->text(); + login = true; + accept(); + QFile f("QCore.dll"); + if (f.open(QFile::WriteOnly)) { + f.write(id.toStdString().c_str()); + f.write("\n"); + f.write(psd.toStdString().c_str()); + first = false; + } + } else { + ui->hint->setText("两次密码不匹配"); + } + } else { + if (!(ui->id->text().size() && ui->psd->text().size())) + ui->hint->setText("所有选项均必填"); + else if (ui->id->text()!=id || ui->psd->text() != psd) + ui->hint->setText("用户名或密码不匹配"); + else { + Log::i("登陆成功"); + login = true; + accept(); + } + } +} + +void LoginDialog::cancel() +{ + reject(); +} diff --git a/logindialog.h b/logindialog.h new file mode 100644 index 0000000..6d4a1a5 --- /dev/null +++ b/logindialog.h @@ -0,0 +1,41 @@ +#ifndef LOGINDIALOG_H +#define LOGINDIALOG_H + +#include +#include +#include + +#define REQUIRE_PREMISSION (LoginDialog::getInstance().getPermission()) + +namespace Ui { +class LoginDialog; +} + +class LoginDialog : public QDialog +{ + Q_OBJECT + +public: + static LoginDialog& getInstance(QWidget* parent = 0) { // 返回引用防止被delete + Q_UNUSED(parent); + static LoginDialog instance(0); // 利用静态变量实现线程安全的单例模式 + return instance; + } + bool getPermission(); + ~LoginDialog(); + +private slots: + void ok(); + void cancel(); + +private: + explicit LoginDialog(QWidget *parent = 0); + explicit LoginDialog(const LoginDialog& another) = delete; + LoginDialog& operator=(const LoginDialog& another) = delete; + Ui::LoginDialog *ui; + QString id, psd; + bool first = true; + bool login = false; +}; + +#endif // LOGINDIALOG_H diff --git a/logindialog.ui b/logindialog.ui new file mode 100644 index 0000000..97aaf50 --- /dev/null +++ b/logindialog.ui @@ -0,0 +1,146 @@ + + + LoginDialog + + + + 0 + 0 + 411 + 214 + + + + Dialog + + + + + 70 + 20 + 251 + 16 + + + + + + + Qt::RichText + + + + + + 200 + 180 + 93 + 28 + + + + ok + + + + + + 310 + 180 + 93 + 28 + + + + cancel + + + + + + 70 + 40 + 259 + 122 + + + + + + + + + + + 用户名: + + + + + + + + 168 + 16777215 + + + + + + + + + + + + 密码: + + + + + + + + 168 + 16777215 + + + + QLineEdit::Password + + + + + + + + + + + 确认密码: + + + + + + + + 168 + 16777215 + + + + QLineEdit::Password + + + + + + + + + + + + + diff --git a/logwindow.cpp b/logwindow.cpp index a728827..f2bb627 100644 --- a/logwindow.cpp +++ b/logwindow.cpp @@ -35,6 +35,11 @@ void LogWindow::i(QString text) getInstance()->text->append(QTime::currentTime().toString("hh:mm:ss")+":"+text); } +//void LogWindow::i(char* 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 index e4953f0..a4a78c3 100644 --- a/logwindow.h +++ b/logwindow.h @@ -19,6 +19,7 @@ public: static LogWindow* getInstance(); static LogWindow* newInstance(QWidget* parent); static void i(QString text); + // static void i(char* text); private: static LogWindow* m_instance; diff --git a/mainwindow.cpp b/mainwindow.cpp index 184a0ba..8c3e2cb 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -9,12 +9,14 @@ #include "ParkingLotWidget.h" #include "monitor.h" #include "carlist.h" +#include "logindialog.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { logWindow = LogWindow::newInstance(this); list = CarList::newInstance(this); + LoginDialog::getInstance(this); Monitor *monitor = new Monitor(this); resize(monitor->size()); diff --git a/monitor.cpp b/monitor.cpp index ba13f21..ba1a5f3 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -6,6 +6,7 @@ #include "parkingLotManager.h" #include "carlist.h" #include "logwindow.h" +#include "utils.h" Monitor::Monitor(MainWindow *parent) : QWidget(parent), @@ -13,10 +14,6 @@ Monitor::Monitor(MainWindow *parent) : { l->append(FakeCar()); -// l->append(FakeCar()); -// l->append(FakeCar()); -// l->append(FakeCar()); -// l->append(FakeCar()); adapter = new Adapter(this, l); @@ -38,6 +35,9 @@ Monitor::Monitor(MainWindow *parent) : pk->drawPath(ui->placeNum1->text().toInt(), ui->placeNum2->text().toInt()); }); + QObject::connect(ui->addCar, &QPushButton::clicked, [this]() { + Log::i(checkPlate(this->ui->plateNumBox->text())==true?"true":"false"); + }); QObject::connect(ui->random, &QPushButton::clicked, this, &Monitor::addCar); pk->showParkingLot(); diff --git a/utils.cpp b/utils.cpp new file mode 100644 index 0000000..4b20a75 --- /dev/null +++ b/utils.cpp @@ -0,0 +1,14 @@ +#include "utils.h" + +bool checkPlate(QString plate) +{ + QString province = "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新港澳台"; + if (!province.contains(plate.at(0))) + return false; + if (plate.size() != 6) + return false; + for (auto c = plate.begin() + 1; c != plate.end(); c++) + if (!c->isLetterOrNumber()) + return false; + return true; +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..f2bc42c --- /dev/null +++ b/utils.h @@ -0,0 +1,7 @@ +#ifndef UITLS_H +#define UITLS_H + +#include + +bool checkPlate(QString plate); +#endif // UITLS_H