Commit 09e65e02 authored by Andoni Jimenez's avatar Andoni Jimenez
Browse files

Solve key events. Dynamic sizepolicy. Multiple comments supported.

parent 30ec33ec
Loading
Loading
Loading
Loading
+50 −19
Original line number Diff line number Diff line
@@ -97,15 +97,23 @@ class Ui(QtWidgets.QMainWindow):
                    print(f"type of {id}:{name} not specified")
                    continue
                
                # size policy
                if wcount%wcols == 0: # first column
                    wpol = QtWidgets.QSizePolicy(policy.Preferred, policy.Preferred)
                elif wcols-(wcount%wcols) == 1: # last column
                    wpol = QtWidgets.QSizePolicy(policy.Maximum, policy.Preferred)
                else:
                    wpol = QtWidgets.QSizePolicy(policy.Minimum, policy.Maximum)
                
                gb = newGroupBox(id, name, wpol)
                # COMMENTBOX
                if tconf['type'] == 'text':
                    gb = newGroupBox(id, name, QtWidgets.QSizePolicy(policy.Maximum, policy.Ignored))
                    
                    widget = QtWidgets.QPlainTextEdit(None)
                    widget.setObjectName(f"tb_{id}")
                    widget.setSizePolicy(QtWidgets.QSizePolicy(policy.Expanding, policy.Minimum))
                    widget.setPlaceholderText(f"Press [{tconf['shortcut']}] to insert comments. Press [Enter] to save, and [Esc] to discard.")
                    widget.installEventFilter(self) # Set enter as save row
                    addTextBoxTexts(widget, tconf)
                    widget.installEventFilter(self) # Set enter as save
                    
                    gb.layout().addWidget(widget)
                    self.commentBox = widget 
                    self.tb[id] = widget
@@ -113,11 +121,9 @@ class Ui(QtWidgets.QMainWindow):
                elif tconf['type'] == 'checkbox':
                    if 'ncolumns' in tconf.keys():
                        cols = tconf['ncolumns']
                    
                    gb = newGroupBox(id, name, QtWidgets.QSizePolicy(policy.Minimum, policy.Maximum))
                    
                    if 'elements' in tconf and len(tconf['elements']) > 0:
                        self.cb[id] = {}
                        
                    for element in tconf['elements']:
                        try:
                            eid = element['id']
@@ -143,8 +149,6 @@ class Ui(QtWidgets.QMainWindow):
                    if 'ncolumns' in tconf.keys():
                        cols = tconf['ncolumns']*2 # *2 -> shortcut and name in 2 rows
                    
                    gb = newGroupBox(id, name, QtWidgets.QSizePolicy(policy.Preferred, policy.Preferred))
                    
                    gbb = QtWidgets.QButtonGroup()
                    gbb.setObjectName(f"gbb_{id}")
                    
@@ -229,7 +233,7 @@ class Ui(QtWidgets.QMainWindow):
        # DONE 5 - output file keys have to be dinamic (related to input file and COLUMNS). also, newEntry func
        # DONE 6 - what if multiple same named widgets? add groupid to widgetid?
        # 6.5 - what if multiple same named widgets? outputfile names should include widgetgroup id?
        # 7 - multiple commentBox not supported (yes, but events need to be solved)
        # DONE 7 - multiple commentBox not supported (yes, but events need to be solved)
        # 8 - If filename not in input file, detect images (currently fails)
        # DONE 9 - Shortcuts not working
        
@@ -549,21 +553,33 @@ class Ui(QtWidgets.QMainWindow):

    # Keyboard shortcuts helpers

    def eventFilter(self, obj:QObject, event:QEvent) -> bool:
        if event.type() == QEvent.KeyPress and obj is self.commentBox:
            if event.key() == (Qt.Key_Return or Qt.Key_Enter) and \
                                self.commentBox.hasFocus():
    def eventFilter(self, obj:QObject, event) -> bool:
        if event.type() == QEvent.KeyPress: # and obj is self.commentBox:
            if event.key() == (Qt.Key.Key_Return or Qt.Key.Key_Enter): # and self.commentBox.hasFocus():
                # If TextBox move focus
                for _, tb in self.tb.items():
                    if tb.hasFocus():
                        tb.parent().focusNextChild()
                        #tb.clearFocus()
                        #self.fileList.setFocus()
                        return True
                # save if not in TextBox
                self.pb_save.animateClick()
        return super().eventFilter(obj, event)

    def keyPressEvent(self, event:QEvent) -> None:
        key = event.key()
        # if key == Qt.Key_F1 and not self.commentBox.hasFocus():
        #     self.commentBox.setFocus()

        if key == Qt.Key_F1 and not self.commentBox.hasFocus():
            self.commentBox.setFocus()
        # if key == Qt.Key_Escape and self.commentBox.hasFocus():
        #     self.commentBox.setPlainText('')
        #     self.fileList.setFocus()
        
        if key == Qt.Key_Escape and self.commentBox.hasFocus():
            self.commentBox.setPlainText('')
        if key == Qt.Key.Key_Escape:
            for _, tb in self.tb.items():
                if tb.hasFocus():
                    tb.setPlainText('')
            self.fileList.setFocus()

    # Resizing helpers
@@ -592,6 +608,21 @@ def addToolTip(widget: QtWidgets.QWidget, element:dict) -> None:
    except KeyError as e:
        print(f"KeyError: {e}")
        
def addTextBoxTexts(widget: QtWidgets.QPlainTextEdit, element:dict) -> None:
    shortcut = None
    try:
        shortcut = element['shortcut']
    except KeyError as e:
        print(f"KeyError: {e}")
    finally:
        if shortcut is None:
            widget.setPlaceholderText(f"Press [Enter] to save, and [Esc] to discard.")
        else:
            widget.setPlaceholderText(f"Press [{shortcut}] to insert comments. Press [Enter] to save, and [Esc] to discard.")
            # add shortcut 
            ws = QtWidgets.QShortcut(shortcut, widget)
            ws.activated.connect(widget.setFocus)

def addCheckBoxTexts(widget: QtWidgets.QCheckBox, element:dict) -> None:
    id, name, shortcut = None, None, None
    try:
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ def getOptions(version):
    #                    or '--path' in sys.argv
    #                    or '--version' in sys.argv), type=dir_file,
    #                    help="Image or list to classify. Not required if path or path + group are given.\n")
    parser.add_argument('-s', '--savefile', type=dir_file,
    parser.add_argument('-s', '--savefile',
                        #required=not('--version' in sys.argv),
                        default='output.csv',
                        help="CSV file to load and export changes. If does not exists, a new one is created.\n")