1
1
from PyQt5 import QtWidgets
2
- from PyQt5 .QtCore import QStringListModel , Qt , QRect , pyqtSignal
2
+ from PyQt5 .QtCore import QStringListModel , Qt , QRect , pyqtSignal , QModelIndex , pyqtSlot
3
3
from PyQt5 .QtGui import QStandardItemModel , QStandardItem
4
4
from PyQt5 .QtWidgets import QInputDialog , QMessageBox , QMenu , QAction
5
5
6
6
# External imports
7
7
from labelvim .utils .config import ANNOTATION_TYPE , OBJECT_LIST_ACTION
8
+ from labelvim .widgets .custom_delegets import CustomDelegate
8
9
from enum import Enum
9
10
10
11
class CustomListViewWidget (QtWidgets .QListView ):
@@ -62,7 +63,7 @@ def set_label_list(self, label_list=[]):
62
63
Args:
63
64
label_list (list, optional): A list of label names to display. Defaults to an empty list.
64
65
"""
65
- print (f"Label List in set label list: { label_list } " )
66
+ # print(f"Label List in set label list: {label_list}")
66
67
if isinstance (label_list , list ) and len (label_list ) > 0 :
67
68
self .model .clear () # Clear the model before updating
68
69
self .label_list = label_list
@@ -176,8 +177,8 @@ def mousePressEvent(self, event):
176
177
event (QMouseEvent): The mouse event object.
177
178
"""
178
179
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()}")
181
182
if index .isValid ():
182
183
item = self .model .itemFromIndex (index )
183
184
print (f"Item: { item .text ()} " )
@@ -230,6 +231,8 @@ def __init__(self, parent=None):
230
231
# Set the geometry, model
231
232
self .set_geometry ()
232
233
self .set_model ()
234
+ self .delegate = CustomDelegate (self )
235
+ self .setItemDelegate (self .delegate )
233
236
# Set the annotation type to None
234
237
self .annotation_type = "Rectangle"
235
238
# Connect the signals
@@ -387,6 +390,7 @@ def edit_label(self, index):
387
390
# Start editing the item at the specified index
388
391
self .edit (index )
389
392
393
+
390
394
def on_data_changed (self , topLeft , bottomRight , roles ):
391
395
"""
392
396
Slot that is triggered when the data in the model is changed.
@@ -438,7 +442,7 @@ def __init__(self, parent=None):
438
442
self .set_label_list (self .label_list )
439
443
# Connect the signals
440
444
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
442
446
443
447
def set_geometry (self ):
444
448
"""
@@ -455,7 +459,7 @@ def set_model(self):
455
459
self .setModel (self .model )
456
460
self .setUpdatesEnabled (True )
457
461
# # Set the edit triggers to NoEditTriggers
458
- # self.setEditTriggers(QtWidgets.QListView.NoEditTriggers)
462
+ self .setEditTriggers (QtWidgets .QListView .NoEditTriggers )
459
463
460
464
def __receiver_action (self , data : any , action : Enum ):
461
465
data = data [0 ]
@@ -493,15 +497,15 @@ def set_label_list(self, category_id: list = [], object_id: list = []):
493
497
label_list (list, optional): A list of label names to display. Defaults to an empty list.
494
498
"""
495
499
# 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:
497
501
# 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 )
505
509
506
510
def clear_list (self ):
507
511
"""
@@ -534,6 +538,7 @@ def remove_label(self, data: list):
534
538
Args:
535
539
label (str): The label to remove.
536
540
"""
541
+ print (f"Data To Be remoed: { data } " )
537
542
category_id = [label ["category_id" ] for label in data ]
538
543
object_id = [label ['id' ] for label in data ]
539
544
self .object = {label ['id' ]: label ['category_id' ] for label in data }
@@ -555,6 +560,14 @@ def edit_label(self, object_id, category_id):
555
560
object_list = [f"{ self .label_list [id ]} " for id in self .category_id ]
556
561
self .model .setStringList (object_list )
557
562
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
+
558
571
def on_item_clicked (self , index ):
559
572
"""
560
573
Emits the signal with the currently selected item's text.
@@ -563,8 +576,8 @@ def on_item_clicked(self, index):
563
576
index (QModelIndex): The index of the current item.
564
577
"""
565
578
# 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)}")
568
581
id = self .object_id [index .row ()]
569
582
self .object_selection_notification_slot .emit (id )
570
583
@@ -580,4 +593,56 @@ def mousePressEvent(self, event) -> None:
580
593
print ("Clearing selection" )
581
594
self .object_selection_notification_slot .emit (- 1 )
582
595
# 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