-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUiMain.py
More file actions
406 lines (346 loc) · 17.5 KB
/
UiMain.py
File metadata and controls
406 lines (346 loc) · 17.5 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import os
import sys
from PyQt6 import QtWidgets, uic
from XmlService import XmlService
from orm.model.WindowsEnvironment import WindowsEnvironment
from orm.service.WindowsEnvironmentService import WindowsEnvironmentService
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
uic.loadUi('ui.ui', self)
self.init_table()
self.findChild(QtWidgets.QPushButton, 'add_environment').clicked.connect(self.add_environment_callback)
self.findChild(QtWidgets.QPushButton, 'delete_environment').clicked.connect(self.delete_environment_callback)
self.init_maven_config()
self.init_node_config()
def init_table(self):
# 假设你的.ui文件中有一个ObjectName为"key_value_path"的按钮
self.table_key_value_path: QtWidgets.QTableWidget = self.findChild(QtWidgets.QTableWidget, 'key_value_path')
# 为按钮绑定事件处理函数
self.findChild(QtWidgets.QPushButton, 'add_row').clicked.connect(self.add_row_callback)
self.findChild(QtWidgets.QPushButton, 'delete_select_row').clicked.connect(self.delete_select_rows_callback)
self.load_table_data()
# 为表格的单元格内容改变事件绑定事件处理函数
# PyQt 表格中内容发生变化时,同步更新 SQLite3 数据库中的数据,你需要连接表格的编辑信号到一个槽函数,然后在这个槽函数中执行数据库的更新操作。
# 在 PyQt 的 QTableWidget 中,可以使用 itemChanged 信号来监听单元格内容的更改。
self.table_key_value_path.itemChanged.connect(self.on_item_changed)
def load_table_data(self):
assert type(self.table_key_value_path) is QtWidgets.QTableWidget, "获取表格错误"
# 清除表格现有数据
self.table_key_value_path.setRowCount(0)
self.table_key_value_path.clearContents()
# 设置表格的列数和行数
self.table_key_value_path.setColumnCount(4) # 设置列数
self.table_key_value_path.setHorizontalHeaderLabels(['id', 'key', 'value', 'add_path'])
self.table_key_value_path.setRowCount(WindowsEnvironment.select().count()) # 设置行数
# 从数据库加载数据
query = WindowsEnvironment.select()
index = 0
for record in query:
self.table_key_value_path.setItem(index, 0, QtWidgets.QTableWidgetItem(str(record.id)))
self.table_key_value_path.setItem(index, 1, QtWidgets.QTableWidgetItem(record.key))
self.table_key_value_path.setItem(index, 2, QtWidgets.QTableWidgetItem(record.value))
self.table_key_value_path.setItem(index, 3, QtWidgets.QTableWidgetItem(record.add_path))
index += 1
self.adjust_table_size()
def adjust_table_size(self):
"""
根据表格内容,调整表格大小
:return:
"""
for i in range(self.table_key_value_path.columnCount()):
self.table_key_value_path.resizeColumnToContents(i)
for i in range(self.table_key_value_path.rowCount()):
self.table_key_value_path.resizeRowToContents(i)
def add_row_callback(self):
# 在数据库中添加新行
WindowsEnvironment.create(key="", value="", add_path="")
# 更新表格
self.load_table_data()
def delete_select_rows_callback(self):
selected_items = self.table_key_value_path.selectedItems()
if not selected_items:
return
selected_rows = {item.row() for item in selected_items}
for row in selected_rows:
# 获取 ID 并删除数据库记录
id_item = self.table_key_value_path.item(row, 0)
if id_item is not None:
record_id = int(id_item.text())
WindowsEnvironment.delete().where(WindowsEnvironment.id == record_id).execute()
# 更新表格
self.load_table_data()
def on_item_changed(self, item):
row = item.row()
column = item.column()
new_value = item.text()
# 获取该行对应的数据库记录的 ID
id_item = self.table_key_value_path.item(row, 0)
if id_item is None:
return
record_id = int(id_item.text())
# 更新数据库
self.update_database(record_id, column, new_value)
def update_database(self, record_id, column, new_value):
# 使用 Peewee 更新数据库
field_map = {1: WindowsEnvironment.key, 2: WindowsEnvironment.value,
3: WindowsEnvironment.add_path}
field = field_map.get(column)
if field is not None:
query = WindowsEnvironment.update({field: new_value}).where(
WindowsEnvironment.id == record_id)
query.execute()
def add_environment_callback(self):
# 从数据库加载数据
windows_environments = WindowsEnvironment.select()
# 设置系统环境变量
for windows_environment in windows_environments:
WindowsEnvironmentService.set_system_env_var(windows_environment.key, windows_environment.value)
sys_path = WindowsEnvironmentService.get_path()
for windows_environment in windows_environments:
if sys_path.find(windows_environment.path_value) == -1:
sys_path = sys_path + windows_environment.path_value
WindowsEnvironmentService.set_system_env_var("PATH", sys_path)
# 通知系统环境变量已经改变
WindowsEnvironmentService.broadcast_environment_change()
def delete_environment_callback(self):
# 从数据库加载数据
windows_environments = WindowsEnvironment.select()
# 删除系统环境变量
for windows_environment in windows_environments:
WindowsEnvironmentService.delete_system_env_var(windows_environment.key)
sys_path = WindowsEnvironmentService.get_path()
for windows_environment in windows_environments:
if sys_path.find(windows_environment.path_value) != -1:
sys_path = sys_path.replace(windows_environment.path_value, "")
WindowsEnvironmentService.set_system_env_var("PATH", sys_path)
# 通知系统环境变量已经改变
WindowsEnvironmentService.broadcast_environment_change()
def on_local_repository_changed(self):
result = self.local_repository.text()
# 修改
self.xml_service.data["settings"]["localRepository"] = result
# 保存
self.xml_service.write_xml(self.xml_service.data)
def on_proxy_enable_changed(self):
# 获取当前状态
result = self.proxy_enable.isChecked()
print(f"当前状态{result}")
if result:
# 修改
self.xml_service.data["settings"]["proxies"] = {
'proxy': {
'id': 'optional',
'active': 'true',
'protocol': 'http',
'host': '127.0.0.1',
'port': '10809'
}
}
# 保存
self.xml_service.write_xml(self.xml_service.data)
self.xml_service.read_xml()
# proxy_id
self.proxy_id = self.findChild(QtWidgets.QLineEdit, 'proxy_id')
self.proxy_id.setText(self.xml_service.data["settings"]["proxies"]["proxy"]["id"])
self.proxy_id.textChanged.connect(self.on_proxy_id_changed)
# proxy_active
self.proxy_active = self.findChild(QtWidgets.QLineEdit, 'proxy_active')
self.proxy_active.setText(self.xml_service.data["settings"]["proxies"]["proxy"]["active"])
self.proxy_active.textChanged.connect(self.on_proxy_active_changed)
# proxy_protocol
self.proxy_protocol = self.findChild(QtWidgets.QLineEdit, 'proxy_protocol')
self.proxy_protocol.setText(self.xml_service.data["settings"]["proxies"]["proxy"]["protocol"])
self.proxy_protocol.textChanged.connect(self.on_proxy_protocol_changed)
# proxy_host
self.proxy_host = self.findChild(QtWidgets.QLineEdit, 'proxy_host')
self.proxy_host.setText(self.xml_service.data["settings"]["proxies"]["proxy"]["host"])
self.proxy_host.textChanged.connect(self.on_proxy_host_changed)
# proxy_port
self.proxy_port = self.findChild(QtWidgets.QLineEdit, 'proxy_port')
self.proxy_port.setText(self.xml_service.data["settings"]["proxies"]["proxy"]["port"])
self.proxy_port.textChanged.connect(self.on_proxy_port_changed)
if result is False:
# 修改
self.xml_service.data["settings"]["proxies"] = None
# 保存
self.xml_service.write_xml(self.xml_service.data)
def on_proxy_id_changed(self):
result = self.proxy_id.text()
# 修改
self.xml_service.data["settings"]["proxies"]["proxy"]["id"] = result
# 保存
self.xml_service.write_xml(self.xml_service.data)
def on_proxy_active_changed(self):
result = self.proxy_active.isChecked()
# 修改
self.xml_service.data["settings"]["proxies"]["active"] = result
# 保存
self.xml_service.write_xml(self.xml_service.data)
def on_proxy_protocol_changed(self):
result = self.proxy_protocol.currentText()
# 修改
self.xml_service.data["settings"]["proxies"]["proxy"]["protocol"] = result
# 保存
self.xml_service.write_xml(self.xml_service.data)
def on_proxy_host_changed(self):
result = self.proxy_host.text()
# 修改
self.xml_service.data["settings"]["proxies"]["proxy"]["host"] = result
# 保存
self.xml_service.write_xml(self.xml_service.data)
def on_proxy_port_changed(self):
result = self.proxy_port.text()
# 修改
self.xml_service.data["settings"]["proxies"]["proxy"]["port"] = result
# 保存
self.xml_service.write_xml(self.xml_service.data)
def on_mirror_id_changed(self):
result = self.mirror_id.text()
# 修改
self.xml_service.data["settings"]["mirrors"]["mirror"]["id"] = result
# 保存
self.xml_service.write_xml(self.xml_service.data)
def on_mirror_name_changed(self):
result = self.mirror_name.text()
# 修改
self.xml_service.data["settings"]["mirrors"]["mirror"]["name"] = result
# 保存
self.xml_service.write_xml(self.xml_service.data)
def on_mirror_url_changed(self):
result = self.mirror_url.text()
# 修改
self.xml_service.data["settings"]["mirrors"]["mirror"]["url"] = result
# 保存
self.xml_service.write_xml(self.xml_service.data)
def on_mirror_mirrorOf_changed(self):
result = self.mirror_mirrorOf.text()
# 修改
self.xml_service.data["settings"]["mirrors"]["mirror"]["mirrorOf"] = result
# 保存
self.xml_service.write_xml(self.xml_service.data)
def init_maven_config(self):
# M2_HOME
try:
filename = WindowsEnvironmentService.get_system_env_var("M2_HOME") + r"\conf\settings.xml"
except TypeError:
self.findChild(QtWidgets.QLineEdit, 'local_repository').setText(
r"目前用户没有设置M2_HOME环境变量!!此处设置全部不生效")
return
# 如果判断文件,不存在则返回
if not os.path.exists(filename):
return
# 将源文件,备份为settings.xml.bak
if not os.path.exists(filename + ".bak"):
import shutil
shutil.copyfile(filename, filename + ".bak")
self.xml_service = XmlService(filename)
self.xml_service.read_xml()
# local_repository
self.local_repository = self.findChild(QtWidgets.QLineEdit, 'local_repository')
# 回显
try:
if self.xml_service.data["settings"]["localRepository"] is not None:
self.local_repository.setText(self.xml_service.data["settings"]["localRepository"])
except KeyError:
# 赋予默认值
self.local_repository.setText(r"目前用户没有设置本地仓库的位置,将使用maven的默认值")
self.on_local_repository_changed
# 绑定事件
self.local_repository.textChanged.connect(self.on_local_repository_changed)
self.proxy_enable = self.findChild(QtWidgets.QCheckBox, 'proxy_checkbox')
assert type(self.proxy_enable) is QtWidgets.QCheckBox, f"获取复选框错误:{type(self.proxy_enable)}"
# 配置文件中存在,代理数据,则打勾✔
try:
if self.xml_service.data["settings"]["proxies"]["proxy"] is not None:
self.proxy_enable.setChecked(True)
else:
self.proxy_enable.setChecked(False)
except Exception:
self.proxy_enable.setChecked(False)
# 设置回调
self.proxy_enable.stateChanged.connect(self.on_proxy_enable_changed)
if self.proxy_enable.isChecked():
# proxy_id
self.proxy_id = self.findChild(QtWidgets.QLineEdit, 'proxy_id')
self.proxy_id.setText(self.xml_service.data["settings"]["proxies"]["proxy"]["id"])
self.proxy_id.textChanged.connect(self.on_proxy_id_changed)
# proxy_active
self.proxy_active = self.findChild(QtWidgets.QLineEdit, 'proxy_active')
self.proxy_active.setText(self.xml_service.data["settings"]["proxies"]["proxy"]["active"])
self.proxy_active.textChanged.connect(self.on_proxy_active_changed)
# proxy_protocol
self.proxy_protocol = self.findChild(QtWidgets.QLineEdit, 'proxy_protocol')
self.proxy_protocol.setText(self.xml_service.data["settings"]["proxies"]["proxy"]["protocol"])
self.proxy_protocol.textChanged.connect(self.on_proxy_protocol_changed)
# proxy_host
self.proxy_host = self.findChild(QtWidgets.QLineEdit, 'proxy_host')
self.proxy_host.setText(self.xml_service.data["settings"]["proxies"]["proxy"]["host"])
self.proxy_host.textChanged.connect(self.on_proxy_host_changed)
# proxy_port
self.proxy_port = self.findChild(QtWidgets.QLineEdit, 'proxy_port')
self.proxy_port.setText(self.xml_service.data["settings"]["proxies"]["proxy"]["port"])
self.proxy_port.textChanged.connect(self.on_proxy_port_changed)
# mirror_id
self.mirror_id = self.findChild(QtWidgets.QLineEdit, 'mirror_id')
self.mirror_id.setText(self.xml_service.data["settings"]["mirrors"]["mirror"]["id"])
self.mirror_id.textChanged.connect(self.on_mirror_id_changed)
# mirror_name
self.mirror_name = self.findChild(QtWidgets.QLineEdit, 'mirror_name')
self.mirror_name.setText(self.xml_service.data["settings"]["mirrors"]["mirror"]["name"])
self.mirror_name.textChanged.connect(self.on_mirror_name_changed)
# mirror_url
self.mirror_url = self.findChild(QtWidgets.QLineEdit, 'mirror_url')
self.mirror_url.setText(self.xml_service.data["settings"]["mirrors"]["mirror"]["url"])
self.mirror_url.textChanged.connect(self.on_mirror_url_changed)
# mirror_mirrorOf
self.mirror_mirrorOf = self.findChild(QtWidgets.QLineEdit, 'mirror_mirrorOf')
self.mirror_mirrorOf.setText(self.xml_service.data["settings"]["mirrors"]["mirror"]["mirrorOf"])
self.mirror_mirrorOf.textChanged.connect(self.on_mirror_mirrorOf_changed)
def on_npmrc_changed(self):
result = self.npmrc.toPlainText()
# 修改
filename = WindowsEnvironmentService.get_system_env_var("NODE_HOME") + r"\etc\npmrc"
with open(filename, 'w') as f:
f.write(result)
def init_node_config(self):
# NODE_HOME
self.npmrc = self.findChild(QtWidgets.QPlainTextEdit, 'npmrc_config')
print(type(self.npmrc))
try:
filename = WindowsEnvironmentService.get_system_env_var("NODE_HOME") + r"\etc\npmrc"
except TypeError:
self.npmrc.setPlainText(r"目前用户没有设置NODE_HOME环境变量!!此处设置全部不生效")
return
# 如果判断文件,不存在则新建文件,并初始化内容
if not os.path.exists(filename):
with open(filename, 'w') as f:
f.write(r'prefix="D:/DevelopmentTools/Softare/GreenSoftWare/a-language/NodeJS/data/node_global"')
f.write(r'cache="D:/DevelopmentTools/Softare/GreenSoftWare/a-language/NodeJS/data/node_cache"')
f.write(r'registry="https://registry.npmmirror.com/"')
# 将源文件,备份为settings.xml.bak
if not os.path.exists(filename + ".bak"):
import shutil
shutil.copyfile(filename, filename + ".bak")
# 回显
with open(filename, 'r') as f:
self.npmrc.setPlainText(f.read())
# 绑定事件
self.npmrc.textChanged.connect(self.on_npmrc_changed)
def admin_running():
import ctypes
import sys
import os
is_admin = ctypes.windll.shell32.IsUserAnAdmin()
if not is_admin:
print([sys.argv[0]])
# 以管理员身份运行脚本
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable,
" ".join([os.path.abspath(sys.argv[0])] + sys.argv[1:]), None, 1)
# 退出,非管理员运行的脚本
exit(0)
# admin_running()
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())