11from PyQt5 import QtWidgets
2- from PyQt5 .QtCore import QStringListModel , Qt , QRect , pyqtSignal
2+ from PyQt5 .QtCore import QStringListModel , Qt , QRect , pyqtSignal , QModelIndex , pyqtSlot
33from PyQt5 .QtGui import QStandardItemModel , QStandardItem
44from PyQt5 .QtWidgets import QInputDialog , QMessageBox , QMenu , QAction
55
66# External imports
77from labelvim .utils .config import ANNOTATION_TYPE , OBJECT_LIST_ACTION
8+ from labelvim .widgets .custom_delegets import CustomDelegate
89from enum import Enum
910
1011class CustomListViewWidget (QtWidgets .QListView ):
@@ -62,7 +63,7 @@ def set_label_list(self, label_list=[]):
6263 Args:
6364 label_list (list, optional): A list of label names to display. Defaults to an empty list.
6465 """
65- print (f"Label List in set label list: { label_list } " )
66+ # print(f"Label List in set label list: {label_list}")
6667 if isinstance (label_list , list ) and len (label_list ) > 0 :
6768 self .model .clear () # Clear the model before updating
6869 self .label_list = label_list
@@ -176,8 +177,8 @@ def mousePressEvent(self, event):
176177 event (QMouseEvent): The mouse event object.
177178 """
178179 index = self .indexAt (event .pos ())
179- print (f"Index: { index .row ()} " )
180- print (f"model row count { self .model .rowCount ()} " )
180+ # print(f"Index: {index.row()}")
181+ # print(f"model row count {self.model.rowCount()}")
181182 if index .isValid ():
182183 item = self .model .itemFromIndex (index )
183184 print (f"Item: { item .text ()} " )
@@ -230,6 +231,8 @@ def __init__(self, parent=None):
230231 # Set the geometry, model
231232 self .set_geometry ()
232233 self .set_model ()
234+ self .delegate = CustomDelegate (self )
235+ self .setItemDelegate (self .delegate )
233236 # Set the annotation type to None
234237 self .annotation_type = "Rectangle"
235238 # Connect the signals
@@ -387,6 +390,7 @@ def edit_label(self, index):
387390 # Start editing the item at the specified index
388391 self .edit (index )
389392
393+
390394 def on_data_changed (self , topLeft , bottomRight , roles ):
391395 """
392396 Slot that is triggered when the data in the model is changed.
@@ -438,7 +442,7 @@ def __init__(self, parent=None):
438442 self .set_label_list (self .label_list )
439443 # Connect the signals
440444 self .object_list_slot_receiver .connect (self .__receiver_action ) # Connect the signal to update the label list
441- # self.model.dataChanged.connect(self.on_data_changed ) # Connect the dataChanged signal
445+ # self.model.dataChanged.connect(self.handle_data_changed ) # Connect the dataChanged signal
442446
443447 def set_geometry (self ):
444448 """
@@ -455,7 +459,7 @@ def set_model(self):
455459 self .setModel (self .model )
456460 self .setUpdatesEnabled (True )
457461 # # Set the edit triggers to NoEditTriggers
458- # self.setEditTriggers(QtWidgets.QListView.NoEditTriggers)
462+ self .setEditTriggers (QtWidgets .QListView .NoEditTriggers )
459463
460464 def __receiver_action (self , data : any , action : Enum ):
461465 data = data [0 ]
@@ -493,15 +497,15 @@ def set_label_list(self, category_id: list = [], object_id: list = []):
493497 label_list (list, optional): A list of label names to display. Defaults to an empty list.
494498 """
495499 # Check if the label list is a non-empty list
496- if isinstance (category_id , list ) and len (category_id ) > 0 and isinstance (object_id , list ) and len (object_id ) > 0 :
500+ # if isinstance(category_id, list) and len(category_id) > 0 and isinstance(object_id, list) and len(object_id) > 0:
497501 # Clear the model before updating
498- self .clear_list ()
499- # Update the label list and model
500- self .category_id = category_id
501- self .object_id = object_id
502- object_list = [f"{ self .label_list [id ]} " for id in category_id ]
503- # update the model
504- self .model .setStringList (object_list )
502+ self .clear_list ()
503+ # Update the label list and model
504+ self .category_id = category_id
505+ self .object_id = object_id
506+ object_list = [f"{ self .label_list [id ]} " for id in category_id ]
507+ # update the model
508+ self .model .setStringList (object_list )
505509
506510 def clear_list (self ):
507511 """
@@ -534,6 +538,7 @@ def remove_label(self, data: list):
534538 Args:
535539 label (str): The label to remove.
536540 """
541+ print (f"Data To Be remoed: { data } " )
537542 category_id = [label ["category_id" ] for label in data ]
538543 object_id = [label ['id' ] for label in data ]
539544 self .object = {label ['id' ]: label ['category_id' ] for label in data }
@@ -555,6 +560,14 @@ def edit_label(self, object_id, category_id):
555560 object_list = [f"{ self .label_list [id ]} " for id in self .category_id ]
556561 self .model .setStringList (object_list )
557562
563+ def refresh_list (self , label_list : list ):
564+ """
565+ Refresh the list view
566+ """
567+ self .label_list = label_list
568+ object_list = [f"{ self .label_list [id ]} " for id in self .category_id ]
569+ self .model .setStringList (object_list )
570+
558571 def on_item_clicked (self , index ):
559572 """
560573 Emits the signal with the currently selected item's text.
@@ -563,8 +576,8 @@ def on_item_clicked(self, index):
563576 index (QModelIndex): The index of the current item.
564577 """
565578 # Emit the signal with the selected item's text
566- print (f"Index: { index .row ()} " )
567- print (f"Item: { self .model .data (index , Qt .DisplayRole )} " )
579+ # print(f"Index: {index.row()}")
580+ # print(f"Item: {self.model.data(index, Qt.DisplayRole)}")
568581 id = self .object_id [index .row ()]
569582 self .object_selection_notification_slot .emit (id )
570583
@@ -580,4 +593,56 @@ def mousePressEvent(self, event) -> None:
580593 print ("Clearing selection" )
581594 self .object_selection_notification_slot .emit (- 1 )
582595 # Call the parent class's mousePressEvent to handle normal clicks
583- super ().mousePressEvent (event )
596+ super ().mousePressEvent (event )
597+
598+ # def contextMenuEvent(self, event):
599+ # """
600+ # Handles the context menu event, providing an option to edit the selected item.
601+
602+ # Args:
603+ # event (QContextMenuEvent): The context menu event object.
604+ # """
605+ # # Get the index at the mouse position
606+ # index = self.indexAt(event.pos())
607+ # # Check if the index is valid
608+ # if index.isValid():
609+ # # Create a context menu
610+ # menu = QMenu(self)
611+ # # Create an action to edit the item
612+ # edit_action = QAction('Edit', self)
613+ # # Connect the action to the edit_label method
614+ # edit_action.triggered.connect(lambda: self.edit_label_at_index(index))
615+ # # Add the action to the menu
616+ # menu.addAction(edit_action)
617+ # # Show the context menu at the mouse position
618+ # menu.exec_(event.globalPos())
619+
620+ # def edit_label_at_index(self, index):
621+ # """
622+ # Triggers inline editing for the selected item.
623+
624+ # Args:
625+ # index (QModelIndex): The index of the item to be edited.
626+ # """
627+ # # Start editing the item at the specified index
628+ # self.edit(index)
629+
630+ # @pyqtSlot(QModelIndex, QModelIndex)
631+ # def handle_data_changed(self, top_left: QModelIndex, bottom_right: QModelIndex):
632+ # """
633+ # Slot to handle data changes in the list view.
634+
635+ # Args:
636+ # top_left (QModelIndex): The top-left index of the changed data.
637+ # bottom_right (QModelIndex): The bottom-right index of the changed data.
638+ # """
639+ # row = top_left.row()
640+ # # get index of the item and old data
641+
642+
643+
644+
645+
646+ # new_data = self.model.data(top_left, Qt.EditRole)
647+ # print(f"Item at row {row} was changed to '{new_data}'")
648+ # # Optionally, you can update your data model or UI here
0 commit comments