-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcpc_mainwindow.py
313 lines (255 loc) · 11.8 KB
/
dhcpc_mainwindow.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import sys, typing, threading
from tkinter.messagebox import RETRY
from PyQt6.QtWidgets import QMainWindow, QApplication, QWidget, QHBoxLayout, QFormLayout, QComboBox, QLineEdit, QPushButton, QTableView,\
QVBoxLayout, QMessageBox, QProgressBar, QHeaderView, QCheckBox, QTableWidget, QTableWidgetItem, QAbstractButton, QStyleOptionHeader,\
QStyle, QStylePainter
from PyQt6.QtCore import QAbstractTableModel, Qt, QModelIndex, QVariant, pyqtSignal, QEvent, QSize
from PyQt6.QtGui import QStandardItem
from dhcpc_fsm_model import DhcpcBindsEntryInfo, NetCardInfo, DHCPC_PKT_CFG
from dhcpc_control import DhcpcControlCenter
from dhcpc_gui_sup import DHCPC_PKT_MODEL, DHCPC_PKT_TABLEWIDGET
class UserInputWidget(QWidget):
userBindGenSignal = pyqtSignal([str,str])
def __init__(self, parent=None, model=None, progressbar=None):
super().__init__(parent)
self.cfgpkt = None
self.start_new_nums = 1
self.show_nums = self.start_new_nums
self.model = model
self.progressbar = progressbar
self.userBindGenSignal[str,str].connect(model.add_user)
self.main_layout = QFormLayout(self)
self.btn_layout = QHBoxLayout()
self.dhcpc_num_lineed = QLineEdit()
self.netcard_cbox = QComboBox()
# input相关data
self.netcard_info = NetCardInfo()
netcards = self.NetCardNameList()
self.netcard_cbox.addItems(netcards)
self.netcard_cbox.setCurrentIndex(-1)
self.start_btn = QPushButton("开始")
self.clear_btn = QPushButton("清理")
self.detailcfg_btn = QPushButton("配置")
self.detail_chs = QCheckBox("详细模式")
#DHCPC控制部分
self.ctrl = DhcpcControlCenter()
#布局设置
self.btn_layout.addWidget(self.start_btn)
self.btn_layout.addWidget(self.clear_btn)
self.main_layout.addRow(self.detail_chs, self.detailcfg_btn)
self.main_layout.addRow("网卡:", self.netcard_cbox)
self.main_layout.addRow("DHCPC数量:", self.dhcpc_num_lineed)
self.main_layout.addRow(self.btn_layout)
# 信号槽
self.start_btn.clicked.connect(self.onClickStartBtn)
self.clear_btn.clicked.connect(self.onClickClearBtn)
self.detail_chs.stateChanged.connect(self.onChooseDetailModel)
self.detailcfg_btn.clicked.connect(self.onClickDetailCfgBtn)
# 初始状态
self.clear_btn.setEnabled(False)
self.detailcfg_btn.setEnabled(False)
self.dhcpc_num_lineed.setText(str(self.show_nums))
# 回调设置
DhcpcControlCenter.set_nty_userbind_gen_cb(self.userbind_entryinfo_gen_cb)
def userbind_entryinfo_gen_cb(self, entryinfo:DhcpcBindsEntryInfo):
self.userBindGenSignal.emit(entryinfo.ip, entryinfo.mac)
def NetCardNameList(self):
return self.netcard_info.NetCardNameList()
def onChooseDetailModel(self):
if (self.detail_chs.checkState() == Qt.CheckState.Checked):
self.detailcfg_btn.setEnabled(True)
else:
self.detailcfg_btn.setEnabled(False)
def get_current_mode(self):
if (self.detail_chs.checkState() == Qt.CheckState.Checked):
return 1
return 0
def onClickDetailCfgBtn(self):
self.detail_mode_cfg()
self.cfgpkt.show()
def detail_mode_cfg(self):
if (self.cfgpkt is None):
self.cfgpkt = DHCPC_PKT_TABLEWIDGET(10, 4)
self.cfgpkt.setCornerButtonEnabled(False)
header = QHeaderView(Qt.Orientation.Horizontal)
header.setDefaultAlignment(Qt.AlignmentFlag.AlignRight)
header.setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
self.cfgpkt.setHorizontalHeader(header)
self.cfgpkt.setHorizontalHeaderLabels(["7", "15", "23", "31"])
self.cfgpkt.setVerticalHeaderLabels(["" for _ in range(0, self.cfgpkt.rowCount())])
self.cfgpkt.setItem(0, 4, QTableWidgetItem().setFlags(Qt.ItemFlag.NoItemFlags))
self.cfgpkt.setSpan(1, 0, 1, 4)
self.cfgpkt.setSpan(2, 0, 1, 2)
self.cfgpkt.setSpan(2, 2, 1, 2)
for row in range(3, self.cfgpkt.rowCount()):
self.cfgpkt.setSpan(row, 0, 1, 4)
#设置选中行背景色 self.cfgpkt.setStyleSheet("selection-background-color: red")
# 设置背景色
self.cfgpkt.setStyleSheet("background: rgb(100, 188, 238)")
header.setStyleSheet("QHeaderView::section{background: rgb(100, 188, 238)}")
self.cfgpkt.verticalHeader().setStyleSheet("QHeaderView::section{background: rgb(100, 188, 238)}")
# self.cfgpkt.setStyleSheet('QTableCornerButton::section{background: rgb(100, 188, 238)}')
# self.cfgpkt.setStyleSheet('QTableCornerButton::section{background-color:rgba(255,255,255,255)}')
# 显示行列标题
# self.cfgpkt.horizontalHeader().setVisible(False)
self.cfgpkt.verticalHeader().setVisible(False)
self.cfgpkt.resize(self.cfgpkt.columnWidth(0) * (self.cfgpkt.columnCount() + 1), self.cfgpkt.rowHeight(0) * (self.cfgpkt.rowCount() ))
self.cfgpkt.other()
def onClickClearBtn(self):
self.model.clearALL()
self.ctrl.clearALL()
self.clear_btn.setEnabled(False)
self.start_btn.setEnabled(True)
self.detail_chs.setEnabled(True)
def onClickStartBtn(self):
if (self.input_check() == False):
return
self.detail_chs.setEnabled(False)
self.start_btn.setEnabled(False)
self.clear_btn.setEnabled(True)
self.progressbar.setMaximum(self.start_new_nums)
if (self.get_current_mode() == 1):
self.detail_mode_cfg()
self.ctrl.cfg_init(self.netcard_cbox.currentText(), self.start_new_nums, self.get_current_mode(), self.gen_pkt_cfg())
self.ctrl.async_dhcpc_start()
def gen_pkt_cfg(self):
if self.get_current_mode() == 0:
return None
pkt_cfg = DHCPC_PKT_CFG(self.cfgpkt.write2dhcpc_cfg())
return pkt_cfg
# self.pr_thread_info()
def pr_thread_info(self):
length = len(threading.enumerate())
logger.debug('### START ### 当前运行的线程数为:%d' % length)
logger.debug(threading.enumerate())
logger.debug('### END ###')
def user_input_dhcpcnum(self):
if (self.dhcpc_num_lineed.text()):
return int(self.dhcpc_num_lineed.text())
return 0
def input_check(self) -> bool:
if (self.netcard_cbox.currentIndex() == -1):
QMessageBox.information(None, "提示", "必须选择一个网卡")
return False
start_new_nums = self.user_input_dhcpcnum()
if (start_new_nums <= 0 or start_new_nums > 1000):
QMessageBox.information(None, "提示", "DHCP客户端数量限制为[1, 1000]")
return False
if (start_new_nums != 1 and self.get_current_mode() == 1):
QMessageBox.information(None, "提示", "特殊模式只支持1个客户端")
return False
self.start_new_nums = start_new_nums
self.show_nums = self.start_new_nums
# if (not self.model.empty()): # 如果有数据
# if (self.clear_btn.isEnabled()): # 如果没有可以按下(有数据)
# self.show_nums += start_new_nums
# self.dhcpc_num_lineed.setText(str(self.show_nums))
return True
class DhcpcBindsTableModel(QAbstractTableModel):
def __init__(self, parent, map, progressbar, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.currencyMap = map
self.progressbar = progressbar
def rowCount(self, parent:QModelIndex):
return parent.isValid() if 0 else len(self.currencyMap)
def columnCount(self, parent:QModelIndex):
return parent.isValid() if 0 else 2
def data(self, index, role):
if not index.isValid():
return QVariant()
elif (index.row() >= len(self.currencyMap) or index.row() < 0):
return QVariant()
elif role != Qt.ItemDataRole.DisplayRole:
return QVariant()
key = list(self.currencyMap.keys())[index.row()]
contact = self.currencyMap[key]
if (index.column() == 0):
return contact.ip
elif (index.column() == 1):
return contact.mac
return QVariant()
def headerData(self, col, orientation, role):
if orientation == Qt.Orientation.Horizontal and role == Qt.ItemDataRole.DisplayRole:
if col == 0:
return "IP"
elif col == 1:
return "MAC"
return None
def add_user(self, ip, mac):
if self.currencyMap.get(mac) is not None:
# QMessageBox.information(self, "Duplicate Name", "XXX")
return
self.currencyMap[mac] = DhcpcBindsEntryInfo(ip, mac)
self.beginInsertRows(QModelIndex(), 0, 0)
self.endInsertRows()
self.progressbar.setValue(self.size())
# def removeEntry(self):
# temp = QTableView(currentWidget())
# proxy = QSortFilterProxyModel(temp.model())
# selectionModel = temp.selectionModel()
# indexes = selectionModel.selectedRows()
# for index in indexes:
# row = proxy.mapToSource(index).row()
# table.removeRows(row, 1, QModelIndex())
# if (table.rowCount(QModelIndex()) == 0)
# insertTab(0, newAddressTab, tr("Address Book"))
# def removeRows(self, int position, int rows, QModelIndex index):
# beginRemoveRows(QModelIndex(), position, position + rows - 1)
# endRemoveRows()
# return True
def clearALL(self):
for i in range(0, len(self.currencyMap)):
self.beginRemoveRows(QModelIndex(), 0, 0)
self.endRemoveRows()
self.currencyMap = {}
self.progressbar.setValue(self.size())
def empty(self):
return False if self.currencyMap else True
def size(self):
return len(self.currencyMap)
class UserOutputWidget(QWidget):
def __init__(self, parent=None, progressbar=None):
super().__init__(parent)
self.main_layout = QFormLayout(self)
self.view = QTableView(self)
self.view.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
# 布局
self.main_layout.addRow(self.view)
self.main_layout.addRow("BIND进度:", progressbar)
def tableview_setmodel(self, model):
self.view.setModel(model)
class DhcpcMainWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.main_layout = QHBoxLayout(self)
# 进度条
self.progressbar = QProgressBar(self)
self.progressbar.setMinimum(0)
self.model = DhcpcBindsTableModel(None, {}, self.progressbar)
#输出部分
self.output_part = UserOutputWidget(self, self.progressbar)
self.output_part.tableview_setmodel(self.model)
#输入部分
self.input_part = UserInputWidget(self, self.model, self.progressbar)
# 布局
self.main_layout.addWidget(self.input_part)
self.main_layout.addWidget(self.output_part)
class DhcpcMainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("DHCPC")
self.setCentralWidget(DhcpcMainWidget())
self.resize(700, 600)
# self.resize(200, 200)
self.setMaximumSize(700, 600)
self.setMinimumSize(700, 600)
def closeEvent(self, event):
event.accept()
sys.exit(0) # 退出程序
def main():
a = QApplication(sys.argv)
win = DhcpcMainWindow()
win.show()
sys.exit(a.exec())
if __name__ == "__main__":
main()