Skip to content

Commit 9cfffce

Browse files
committed
- 添加拖拽打开文件功能
拖拽单个文件夹/单个图片到文件列表,可以直接打开文件夹/图片 - bug修复 修复了打开空文件夹造成的闪退问题
1 parent 335ba33 commit 9cfffce

File tree

3 files changed

+103
-8
lines changed

3 files changed

+103
-8
lines changed

ISAT/widgets/canvas.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(self, mainwindow):
1616
super(AnnotationScene, self).__init__()
1717
self.mainwindow = mainwindow
1818
self.image_item: QtWidgets.QGraphicsPixmapItem = None
19+
self.mask_item: QtWidgets.QGraphicsPixmapItem = None
1920
self.image_data = None
2021
self.current_graph: Polygon = None
2122
self.current_line: Line = None
@@ -63,6 +64,14 @@ def load_image(self, image_path: str):
6364
self.setSceneRect(self.image_item.boundingRect())
6465
self.change_mode_to_view()
6566

67+
def unload_image(self):
68+
self.clear()
69+
self.setSceneRect(QtCore.QRectF())
70+
self.mainwindow.polygons.clear()
71+
self.image_item = None
72+
self.mask_item = None
73+
self.current_graph = None
74+
6675
def change_mode_to_create(self):
6776
if self.image_item is None:
6877
return
@@ -948,14 +957,16 @@ def update_mask(self):
948957
mask_image = QtGui.QImage(mask_image[:], mask_image.shape[1], mask_image.shape[0], mask_image.shape[1] * 3,
949958
QtGui.QImage.Format_RGB888)
950959
mask_pixmap = QtGui.QPixmap(mask_image)
951-
self.mask_item.setPixmap(mask_pixmap)
960+
if self.mask_item is not None:
961+
self.mask_item.setPixmap(mask_pixmap)
952962
else:
953963
mask_image = np.zeros(self.image_data.shape, dtype=np.uint8)
954964
mask_image = cv2.addWeighted(self.image_data, 1, mask_image, 0, 0)
955965
mask_image = QtGui.QImage(mask_image[:], mask_image.shape[1], mask_image.shape[0], mask_image.shape[1] * 3,
956966
QtGui.QImage.Format_RGB888)
957967
mask_pixmap = QtGui.QPixmap(mask_image)
958-
self.mask_item.setPixmap(mask_pixmap)
968+
if self.mask_item is not None:
969+
self.mask_item.setPixmap(mask_pixmap)
959970

960971
def backspace(self):
961972
if self.mode == STATUSMode.CREATE:

ISAT/widgets/files_dock_widget.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def __init__(self, mainwindow):
1414
self.listWidget.clicked.connect(self.listwidget_doubleclick)
1515
self.lineEdit_jump.returnPressed.connect(self.mainwindow.jump_to)
1616

17+
self.setAcceptDrops(True)
18+
1719
def generate_item_and_itemwidget(self, file_name):
1820
item = QtWidgets.QListWidgetItem()
1921
item.setSizeHint(QtCore.QSize(200, 30))
@@ -58,3 +60,88 @@ def listwidget_doubleclick(self):
5860
row = self.listWidget.currentRow()
5961
self.mainwindow.current_index = row
6062
self.mainwindow.show_image(row)
63+
64+
def dragEnterEvent(self, event):
65+
if event.mimeData().hasUrls():
66+
event.accept()
67+
else:
68+
event.ignore()
69+
70+
def dropEvent(self, event):
71+
if len(event.mimeData().urls()) != 1:
72+
QtWidgets.QMessageBox.warning(self, 'Warning', 'Only support one path or dir.')
73+
return
74+
# 这里与mainwindow.opend_dir逻辑一致
75+
path = event.mimeData().urls()[0].toLocalFile()
76+
if os.path.isdir(path):
77+
dir = path
78+
# 等待sam线程退出,并清空特征缓存
79+
if self.mainwindow.use_segment_anything:
80+
self.mainwindow.seganythread.wait()
81+
self.mainwindow.seganythread.results_dict.clear()
82+
83+
self.mainwindow.files_list.clear()
84+
self.mainwindow.files_dock_widget.listWidget.clear()
85+
86+
files = []
87+
suffixs = tuple(
88+
['{}'.format(fmt.data().decode('ascii').lower()) for fmt in QtGui.QImageReader.supportedImageFormats()])
89+
for f in os.listdir(dir):
90+
if f.lower().endswith(suffixs):
91+
# f = os.path.join(dir, f)
92+
files.append(f)
93+
files = sorted(files)
94+
self.mainwindow.files_list = files
95+
96+
self.mainwindow.files_dock_widget.update_widget()
97+
98+
self.mainwindow.current_index = 0
99+
100+
self.mainwindow.image_root = dir
101+
self.mainwindow.actionOpen_dir.setStatusTip("Image root: {}".format(self.mainwindow.image_root))
102+
103+
self.mainwindow.label_root = dir
104+
self.mainwindow.actionSave_dir.setStatusTip("Label root: {}".format(self.mainwindow.label_root))
105+
106+
if os.path.exists(os.path.join(dir, 'isat.yaml')):
107+
# load setting yaml
108+
self.mainwindow.config_file = os.path.join(dir, 'isat.yaml')
109+
self.mainwindow.reload_cfg()
110+
111+
self.mainwindow.show_image(self.mainwindow.current_index)
112+
113+
if os.path.isfile(path):
114+
# 等待sam线程退出,并清空特征缓存
115+
if self.mainwindow.use_segment_anything:
116+
self.mainwindow.seganythread.wait()
117+
self.mainwindow.seganythread.results_dict.clear()
118+
119+
self.mainwindow.files_list.clear()
120+
self.mainwindow.files_dock_widget.listWidget.clear()
121+
122+
suffixs = tuple(
123+
['{}'.format(fmt.data().decode('ascii').lower()) for fmt in QtGui.QImageReader.supportedImageFormats()])
124+
125+
dir, file = os.path.split(path)
126+
files = []
127+
if path.lower().endswith(suffixs):
128+
files = [file]
129+
130+
self.mainwindow.files_list = files
131+
132+
self.mainwindow.files_dock_widget.update_widget()
133+
134+
self.mainwindow.current_index = 0
135+
136+
self.mainwindow.image_root = dir
137+
self.mainwindow.actionOpen_dir.setStatusTip("Image root: {}".format(self.mainwindow.image_root))
138+
139+
self.mainwindow.label_root = dir
140+
self.mainwindow.actionSave_dir.setStatusTip("Label root: {}".format(self.mainwindow.label_root))
141+
142+
if os.path.exists(os.path.join(dir, 'isat.yaml')):
143+
# load setting yaml
144+
self.mainwindow.config_file = os.path.join(dir, 'isat.yaml')
145+
self.mainwindow.reload_cfg()
146+
147+
self.mainwindow.show_image(self.mainwindow.current_index)

ISAT/widgets/mainwindow.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,9 @@ def update_group_display(self):
704704

705705
def show_image(self, index:int, zoomfit:bool=True):
706706
self.reset_action()
707+
self.scene.cancel_draw()
708+
self.scene.unload_image()
709+
self.annos_dock_widget.listWidget.clear()
707710
self.change_bit_map_to_label()
708711
#
709712
self.files_dock_widget.label_prev_state.setStyleSheet("background-color: {};".format('#999999'))
@@ -714,21 +717,15 @@ def show_image(self, index:int, zoomfit:bool=True):
714717
self.load_finished = False
715718
self.saved = True
716719
if not -1 < index < len(self.files_list):
717-
self.scene.clear()
718-
self.scene.setSceneRect(QtCore.QRectF())
719720
return
720721
try:
721-
self.polygons.clear()
722-
self.annos_dock_widget.listWidget.clear()
723-
self.scene.cancel_draw()
724722
file_path = os.path.join(self.image_root, self.files_list[index])
725723
image_data = Image.open(file_path)
726724

727725
self.png_palette = image_data.getpalette()
728726
if self.png_palette is not None and file_path.endswith('.png'):
729727
self.statusbar.showMessage('This is a label file.')
730728
self.can_be_annotated = False
731-
732729
else:
733730
self.can_be_annotated = True
734731

0 commit comments

Comments
 (0)