-
Notifications
You must be signed in to change notification settings - Fork 0
/
QListWidgetTest.py
executable file
·90 lines (72 loc) · 3.07 KB
/
QListWidgetTest.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
import os
import sys
import collections
import json
import datetime
from PyQt5 import QtWidgets, uic, QtCore, QtGui, QtTest
from PyQt5.QtWidgets import QMessageBox, QFileDialog
class ListWidget(QtWidgets.QMainWindow):
def __init__(self):
super(ListWidget, self).__init__()
uic.loadUi('listWidgetTest.ui', self)
self.roiDict = {}
self.pb_save_pos.clicked.connect(self.generatePositionDict)
self.pb_roiList_import.clicked.connect(self.importROIDict)
self.pb_roiList_export.clicked.connect(self.exportROIDict)
self.pb_roiList_clear.clicked.connect(self.clearROIList)
self.sampleROI_List.itemClicked.connect(self.showROIPos)
#self.sampleROI_List.itemClicked.connect(lambda: print(
#(self.roiDict[self.sampleROI_List.currentItem().text()])))
self.show()
def getTime(self):
return datetime.datetime.now()
def generatePositionDict(self):
#roi = {'now': self.getTime()}
roi = str(self.sampleROI_List.count())
roi_name = 'ROI' + str(self.sampleROI_List.count())
self.roiDict[roi_name] = roi
self.sampleROI_List.addItem(roi_name)
item = self.sampleROI_List.item(self.sampleROI_List.count()-1)
item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)
def applyDictWithLabel(self):
label_ = {}
for idx in range(self.sampleROI_List.count()):
label = self.sampleROI_List.item(idx).text()
label_[label] = idx
self.roiDict['user_labels'] = label_
print(self.roiDict)
def exportROIDict(self):
self.applyDictWithLabel()
file_name = QFileDialog().getSaveFileName(self, "Save Parameter File",
'hxn_zp_roi_list.json',
'json file(*json)')
if file_name:
with open(file_name[0], 'w') as fp:
json.dump(self.roiDict, fp, indent=4)
else:
pass
def importROIDict(self):
file_name = QFileDialog().getOpenFileName(self, "Open Parameter File",
' ', 'json file(*json)')
if file_name:
self.roiDict = {}
with open(file_name[0], 'r') as fp:
self.roiDict = json.load(fp)
print(self.roiDict['user_labels'])
self.sampleROI_List.clear()
for num,items in enumerate(self.roiDict['user_labels']):
self.sampleROI_List.addItem(items)
item = self.sampleROI_List.item(num)
item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)
else:
pass
def clearROIList(self):
self.sampleROI_List.clear()
def showROIPos(self,item):
item_num = self.sampleROI_List.row(item)
print(self.roiDict[f'ROI{item_num}'])
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = ListWidget()
window.show()
sys.exit(app.exec_())