-
Notifications
You must be signed in to change notification settings - Fork 3
/
carlist.cpp
133 lines (116 loc) · 3.41 KB
/
carlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "carlist.h"
#include "ui_carlist.h"
#include "QDebug"
CarList* CarList::m_instance = nullptr;
CarList::CarList(QWidget *parent) :
QDialog(parent),
ui(new Ui::CarList)
{
ui->setupUi(this);
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
setWindowTitle("车辆列表");
setGeometry(parent->x() + parent->width(), y() + parent->height() / 2, 400, 450);
connect(ui->tableView, &QTableView::clicked, this, &CarList::mouseEvent);
hide();
}
CarList::~CarList()
{
delete ui;
}
void CarList::setAdapter(Adapter *adapter)
{
ui->tableView->setModel(adapter);
ui->tableView->setColumnWidth(0, 80);
ui->tableView->setColumnWidth(1, 40);
ui->tableView->setColumnWidth(2, 80);
ui->tableView->setColumnWidth(3, 80);
ui->tableView->setColumnWidth(4, 70);
ui->tableView->setColumnWidth(5, 40);
// ui->tableView->resizeColumnsToContents();
// ui->tableView->resizeRowsToContents();
m_carList = adapter->getCarList();
}
CarList *CarList::newInstance(QWidget *parent)
{
if (m_instance == nullptr)
m_instance = new CarList(parent);
return m_instance;
}
CarList *CarList::getInstance()
{
return m_instance;
}
void CarList::closeEvent(QCloseEvent *e)
{
emit onCloseButtonClicked();
e->accept();
}
void CarList::mouseEvent(const QModelIndex &index)
{
qDebug() << index.row() << " " << index.column();
if (index.column() == 5)
m_carList->at(index.row())->go();
}
Adapter::Adapter(QObject *parent, QList<Car*> *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 6;
}
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())->getPlateNumber();
case 1: switch (m_list->at(index.row())->getColor()) {
case Car::Color::Red: return "红";
case Car::Color::Yellow: return "黄";
case Car::Color::Pink: return "粉";
default: return "其他";
}
case 2: return m_list->at(index.row())->getPosition();
case 3:
if (m_list->at(index.row())->getStatus() == Car::waiting)
return "正在等待";
else
return m_list->at(index.row())->getStartTime().toString("hh:mm");
case 4: return m_list->at(index.row())->getFee();
case 5: return "离开";
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();
}
QList<Car *> *Adapter::getCarList()
{
return m_list;
}
void Adapter::update()
{
beginResetModel();
endResetModel();
}
void sort(QList<Car*>::iterator s, QList<Car*>::iterator e,
std::function<bool (const Car *, const Car *)> comp)
{
Q_UNUSED(s);
Q_UNUSED(e);
Q_UNUSED(comp);
return ;
}