diff --git a/DataStructureProject.pro b/DataStructureProject.pro index 9a7ffab..aa3e3be 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -38,7 +38,8 @@ SOURCES += \ path.cpp \ parkingLotManager.cpp \ parkinglotgraph.cpp \ - logwindow.cpp + logwindow.cpp \ + carlist.cpp HEADERS += \ mainwindow.h \ @@ -52,12 +53,14 @@ HEADERS += \ path.h \ parkingLotManager.h \ parkinglotgraph.h \ - logwindow.h + logwindow.h \ + carlist.h FORMS += \ mainwindow.ui \ monitor.ui \ - logwindow.ui + logwindow.ui \ + carlist.ui RESOURCES += \ cars.qrc \ diff --git a/carlist.cpp b/carlist.cpp new file mode 100644 index 0000000..96f3ba2 --- /dev/null +++ b/carlist.cpp @@ -0,0 +1,95 @@ +#include "carlist.h" +#include "ui_carlist.h" +#include "QDebug" + +int FakeCar::n = 0; +CarList* CarList::m_instance = nullptr; + +CarList::CarList(QWidget *parent) : + QDialog(parent), + ui(new Ui::CarList) +{ + ui->setupUi(this); + setWindowTitle("车辆列表"); + setGeometry(parent->x() + parent->width(), y() + parent->height() / 2, 400, 450); + hide(); +} + +CarList::~CarList() +{ + delete ui; +} +void CarList::setAdapter(Adapter *adapter) +{ + ui->tableView->setModel(adapter); + ui->tableView->resizeColumnsToContents(); + ui->tableView->resizeRowsToContents(); +} + +CarList *CarList::newInstance(QWidget *parent) +{ + if (m_instance == nullptr) + m_instance = new CarList(parent); + return m_instance; +} + +CarList *CarList::getInstance() +{ + return m_instance; +} + + +FakeCar::FakeCar() +{ + plateNum = QString("吉H%1").arg(++n, 4, 10, QLatin1Char('0')); + startTime = QTime::currentTime(); + pos = QString("一层%1号").arg(n); + type = rand() % 3; +} + +Adapter::Adapter(QObject *parent, QList *list): QAbstractTableModel(parent), m_list(list) { + header << "车牌" << "车型" << "停放位置" << "驶入时间" << "已产生费用"; + timer = new QTimer(this); + QObject::connect(timer, &QTimer::timeout, this, &Adapter::update); + timer->start(1000); +} + +int Adapter::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return 5; +} + +int Adapter::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return m_list->size(); +} + +QVariant Adapter::data(const QModelIndex &index, int role) const +{ + if (role == Qt::DisplayRole) { + switch(index.column()) { + case 0: return m_list->at(index.row()).plateNum; + case 1: return m_list->at(index.row()).type; + case 2: return m_list->at(index.row()).pos; + case 3: return m_list->at(index.row()).startTime.toString("hh:mm"); + case 4: return QTime::currentTime().second() - m_list->at(index.row()).startTime.second(); + default: return QVariant(); + } + } + return QVariant(); +} + +QVariant Adapter::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role == Qt::DisplayRole && orientation == Qt::Horizontal) + return header.at(section); + return QVariant(); +} + +void Adapter::update() +{ + beginResetModel(); + endResetModel(); +} diff --git a/carlist.h b/carlist.h new file mode 100644 index 0000000..c009bbd --- /dev/null +++ b/carlist.h @@ -0,0 +1,56 @@ +#ifndef CARLIST_H +#define CARLIST_H + +#include +#include +#include +#include +#include +#include + +namespace Ui { +class CarList; +} + +struct FakeCar { + QString plateNum; + QTime startTime; + QString pos; + int type; + static int n; + FakeCar(); +}; + +class Adapter: public QAbstractTableModel { +public: + explicit Adapter(QObject* parent, QList* list); + int columnCount(const QModelIndex &parent = QModelIndex()) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QVariant headerData(int section, Qt::Orientation orientation, int role) const; + void update(); +private: + QStringList header; + QList* m_list; + QTimer *timer; +}; + +class CarList : public QDialog +{ + Q_OBJECT + +public: + ~CarList(); + void setAdapter(Adapter* adapter); + static CarList* newInstance(QWidget* parent); + static CarList* getInstance(); + + +private: + static CarList* m_instance; + explicit CarList(QWidget *parent = 0); + QList* m_adapter; + Ui::CarList *ui; +}; + +#endif // CARLIST_H diff --git a/carlist.ui b/carlist.ui new file mode 100644 index 0000000..821590c --- /dev/null +++ b/carlist.ui @@ -0,0 +1,29 @@ + + + CarList + + + + 0 + 0 + 400 + 450 + + + + Dialog + + + + + 0 + 0 + 400 + 450 + + + + + + + diff --git a/logwindow.cpp b/logwindow.cpp index 153716a..a728827 100644 --- a/logwindow.cpp +++ b/logwindow.cpp @@ -23,7 +23,7 @@ LogWindow::~LogWindow() delete ui; } -LogWindow *LogWindow::getInstance(QWidget *parent) +LogWindow *LogWindow::newInstance(QWidget *parent) { if (m_instance == nullptr) m_instance = new LogWindow(parent); diff --git a/logwindow.h b/logwindow.h index d936c9b..e4953f0 100644 --- a/logwindow.h +++ b/logwindow.h @@ -17,7 +17,7 @@ class LogWindow : public QWidget public: ~LogWindow(); static LogWindow* getInstance(); - static LogWindow* getInstance(QWidget* parent); + static LogWindow* newInstance(QWidget* parent); static void i(QString text); private: diff --git a/mainwindow.cpp b/mainwindow.cpp index a73d71f..a410455 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -8,11 +8,14 @@ #include #include "ParkingLotWidget.h" #include "monitor.h" +#include "carlist.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { - logWindow = LogWindow::getInstance(this); + logWindow = LogWindow::newInstance(this); + list = CarList::newInstance(this); + Monitor *monitor = new Monitor(this); resize(monitor->size()); } @@ -33,10 +36,19 @@ void MainWindow::open(void) { 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(); } + +void MainWindow::showListWindow(bool enable) +{ + if (enable) { + list->setGeometry(x()+width() + 15, y() + width() / 2, 400, 450); + list->show(); + setFocus(); + } else + list->hide(); +} diff --git a/mainwindow.h b/mainwindow.h index 912527b..2963bce 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -5,6 +5,7 @@ #include #include "monitor.h" #include "logwindow.h" +#include "carlist.h" class Monitor; namespace Ui { @@ -19,11 +20,13 @@ public: explicit MainWindow(QWidget *parent = 0); void open(void); void showLogWindow(bool enable); + void showListWindow(bool enable); ~MainWindow(); private: Ui::MainWindow *ui = nullptr; LogWindow* logWindow; + CarList *list; QAction *action; Monitor *monitor; }; diff --git a/monitor.cpp b/monitor.cpp index 1056bf6..ba13f21 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -4,11 +4,22 @@ #include #include #include "parkingLotManager.h" +#include "carlist.h" +#include "logwindow.h" Monitor::Monitor(MainWindow *parent) : QWidget(parent), ui(new Ui::Monitor) { + + l->append(FakeCar()); +// l->append(FakeCar()); +// l->append(FakeCar()); +// l->append(FakeCar()); +// l->append(FakeCar()); + + adapter = new Adapter(this, l); + ui->setupUi(this); QGraphicsScene *scene = new QGraphicsScene(this); // 创建场景 ParkingLotManager *pk = new ParkingLotManager(this, scene); @@ -22,15 +33,27 @@ Monitor::Monitor(MainWindow *parent) : 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->showList, &QCheckBox::toggled, parent, &MainWindow::showListWindow); QObject::connect(ui->drawPath, &QPushButton::clicked, [=](){ pk->drawPath(ui->placeNum1->text().toInt(), ui->placeNum2->text().toInt()); }); + + QObject::connect(ui->random, &QPushButton::clicked, this, &Monitor::addCar); + pk->showParkingLot(); ui->view->setScene(scene); - // scene->addItem(pk); + CarList::getInstance()->setAdapter(adapter); } Monitor::~Monitor() { delete ui; } + +void Monitor::addCar() +{ +// QModelIndex topLeft = adapter->index(0, 0); +// QModelIndex bottomRight = adapter->index(adapter->rowCount(), 6); +// emit adapter->dataChanged(topLeft, bottomRight); + l->append(FakeCar()); +} diff --git a/monitor.h b/monitor.h index c91bac6..05e00f8 100644 --- a/monitor.h +++ b/monitor.h @@ -6,6 +6,7 @@ #include #include #include "ParkingLotWidget.h" +#include "carlist.h" class MainWindow; @@ -22,7 +23,10 @@ public: ~Monitor(); private: + QList *l = new QList; Ui::Monitor *ui; + void addCar(); + Adapter *adapter; }; #endif // MONITOR_H diff --git a/monitor.ui b/monitor.ui index 1488c96..3a86d8d 100644 --- a/monitor.ui +++ b/monitor.ui @@ -242,7 +242,7 @@ - 显示总览列表 + 显示列表