def btn_save_clicked(self): ''' When save is clicked 1. Prompt the user for the name and type of the object 2. save the object ''' data = prompt_saving() if len(self.current_result_point_indices) == 0: self.writeMessage("There are no points to save") else: import json a = [] if not os.path.isfile(self.data_fname): self.largest_seg_id = self.largest_seg_id + 1 segment = Segment(id=self.largest_seg_id, data_file_name=self.current_data_file_name, segment_name=data["seg_name"], indices=self.current_result_point_indices, type_class=(data["type_class"], 1)) self.addSegmentationItem(segment) entry = segment.json() a.append(entry) with open(self.data_fname, mode='w') as f: f.write(json.dumps(a, indent=2)) else: with open(self.data_fname) as feedsjson: feeds = json.load(feedsjson) self.largest_seg_id = len(feeds) segment = Segment(id=self.largest_seg_id, data_file_name=self.current_data_file_name, segment_name=data["seg_name"], indices=self.current_result_point_indices, type_class=(data["type_class"], 1)) self.addSegmentationItem(segment) entry = segment.json() feeds.append(entry) with open(self.data_fname, mode='w') as f: f.write(json.dumps(feeds, indent=2)) self.lowerScene.clear() # added by Star: unselect points, resume their color and size to original ones self.resume_selected_points()
def btn_delete_clicked(self): ''' When delete is clicked 1. Prompt the user for the name of the save/object 2. Delete the object from the segmentation_list 3. Delete the object from segments.json 4. Reorder the remaining objects 5. Delete segments.json if there are no remaining saves ''' # prompt the user for a save to delete and pass in the available segment_name's name = prompt_deleting([s.segment_name for s in self.segmentations]) import json with open(self.data_fname, mode='r') as f: segmentations = json.load(f) # data to be saved in segments.json data = [] # initialize removed_id to be an out-of-bounds value (will be updated in the loop) removed_id = len(segmentations) for segment in segmentations: segment_dict = json.loads(segment) seg = Segment(**segment_dict) if seg.segment_name != name: # reorder remaining Segments if deletion has already been performed if seg.id > removed_id: seg.id -= 1 # https://doc.qt.io/qtforpython/PySide2/QtWidgets/QListWidgetItem.html self.segmentation_list.item(seg.id).setText( "{} | {} | {}".format(seg.id, seg.segment_name, seg.type_class)) # save the new id in the backend self.segmentations[seg.id].id -= 1 entry = seg.json() data.append(entry) else: # remove the selected Segment from backend self.segmentations.pop(seg.id) # remove the selected Segment from frontend (https://doc.qt.io/archives/qt-4.8/qlistwidget.html#takeItem) self.segmentation_list.takeItem(seg.id) # save the removed_id to reorder subsequent Segments removed_id = seg.id # delete segments.json if no Segments remain if not data: os.remove(self.data_fname) # otherwise, save the remaining Segments else: with open(self.data_fname, mode='w') as f: f.write(json.dumps(data, indent=2))
def btn_combine_clicked(self): data = prompt_saving() if len(self.multi_segments_point_indices) == 0: self.writeMessage( "No segments are selected to combine. Please apply first.") return # reuse the codes in btn_save_clicked. Can be refractored later. import json a = [] if not os.path.isfile(self.data_fname): self.largest_seg_id = self.largest_seg_id + 1 segment = Segment(id=self.largest_seg_id, data_file_name=self.current_data_file_name, segment_name=data["seg_name"], indices=self.multi_segments_point_indices, type_class=(data["type_class"], 1)) self.addSegmentationItem(segment) entry = segment.json() a.append(entry) with open(self.data_fname, mode='w') as f: f.write(json.dumps(a, indent=2)) else: with open(self.data_fname) as feedsjson: feeds = json.load(feedsjson) self.largest_seg_id = len(feeds) segment = Segment(id=self.largest_seg_id, data_file_name=self.current_data_file_name, segment_name=data["seg_name"], indices=self.multi_segments_point_indices, type_class=(data["type_class"], 1)) self.addSegmentationItem(segment) entry = segment.json() feeds.append(entry) with open(self.data_fname, mode='w') as f: f.write(json.dumps(feeds, indent=2))