class TranscriptionSearchDialog(SearchDialog): def __init__(self, corpus, recents, blankValue, wildcard): super().__init__() print('recents', recents) print('blankValue', blankValue) print('wildcard', wildcard) self.corpus = corpus self.recents = recents self.blankValue = blankValue self.wildcard = wildcard self.setWindowTitle('Search') self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint) layout = QVBoxLayout() #Set up up top layout self.topLayout = QHBoxLayout() explanation = QLabel() text = ('Enter the transcription you want to match in your corpus.') explanation.setText(text) explanation.setFont(QFont('Arial', 16)) self.topLayout.addWidget(explanation) layout.addLayout(self.topLayout) #Set up config tabs self.configTabs = QTabWidget() self.configTabs.addTab(TranscriptionConfigTab(1), 'Config 1') self.configTabs.addTab(TranscriptionConfigTab(2), 'Config 2') layout.addWidget(self.configTabs) # Add "global" handshape options (as checkboxes) self.globalOptionsLayout = QHBoxLayout() self.setupGlobalOptions() layout.addLayout(self.globalOptionsLayout) #Add hand image self.infoPanel = QHBoxLayout() self.handImage = HandShapeImage(getMediaFilePath('hand.JPG')) self.infoPanel.addWidget(self.handImage) self.transcriptionInfo = TranscriptionInfo() self.infoPanel.addLayout(self.transcriptionInfo) layout.addLayout(self.infoPanel) #Connects some slots and signals for k in [0, 1]: for slot in self.configTabs.widget(k).hand1Transcription.slots[1:]: slot.slotSelectionChanged.connect( self.handImage.useNormalImage) slot.slotSelectionChanged.connect( self.handImage.transcriptionSlotChanged) slot.slotSelectionChanged.connect( self.transcriptionInfo.transcriptionSlotChanged) slot.changeValidatorState(True) for slot in self.configTabs.widget(k).hand2Transcription.slots[1:]: slot.slotSelectionChanged.connect( self.handImage.useReverseImage) slot.slotSelectionChanged.connect( self.handImage.transcriptionSlotChanged) slot.slotSelectionChanged.connect( self.transcriptionInfo.transcriptionSlotChanged) slot.changeValidatorState(True) buttonLayout = QHBoxLayout() blankOptionsButton = QPushButton('Search options...') blankOptionsButton.clicked.connect(self.showSearchOptions) buttonLayout.addWidget(blankOptionsButton) showRecents = QPushButton('Show recent searches...') showRecents.clicked.connect(self.recentSearches) ok = QPushButton('Search') ok.clicked.connect(self.accept) cancel = QPushButton('Cancel') cancel.clicked.connect(self.reject) buttonLayout.addWidget(showRecents) buttonLayout.addWidget(ok) buttonLayout.addWidget(cancel) layout.addLayout(buttonLayout) self.setLayout(layout) self.showMaximized() def showSearchOptions(self): dialog = TranscriptionsSearchOptionsDialog(self.blankValue, self.wildcard) if dialog.exec_(): self.blankValue = dialog.blankOptionSelection self.wildcard = dialog.wildcard def recentSearches(self): result = self.showRecentSearches() if result is not None: results = result.recentData.segmentedTranscription results = [[results[0], results[1]], [results[2], results[3]]] for config in [0, 1]: for hand in [0, 1]: widget = getattr(self.configTabs.widget(config), 'hand{}Transcription'.format(hand + 1)) for n, symbol in enumerate(results[config][hand]): slot = getattr(widget, 'slot{}'.format(n + 1)) slot.setText(symbol) def setupGlobalOptions(self): self.globalOptionsWidgets = list() globalOptionsLabel = QLabel('Global handshape options:') globalOptionsLabel.setFont(QFont(FONT_NAME, FONT_SIZE)) self.globalOptionsLayout.addWidget(globalOptionsLabel) for option in GLOBAL_OPTIONS: widget = QCheckBox(option.title()) option += 'CheckBox' setattr(self, option, widget) widget = getattr(self, option) self.globalOptionsLayout.addWidget(widget) self.globalOptionsWidgets.append(widget) def generateRegEx(self): expressions = list() for transcription in self.transcriptions: regex = list() for slot in transcription.slots: symbol = slot.text() if not symbol or symbol == ' ': if self.blankValue == 'literal' or self.blankValue == 'both': symbol = '_' elif self.blankValue == 'wildcard': symbol = '.' if symbol == self.wildcard: symbol = '.' if symbol in ['?', '*', '$', '^', '+']: symbol = '\\' + symbol regex.append(symbol) regex = ''.join(regex) expressions.append(regex) self.regularExpressions = expressions def generateTranscriptions(self): self.transcriptions = list() self.transcriptions.append( self.configTabs.widget(0).hand1Transcription) self.transcriptions.append( self.configTabs.widget(0).hand2Transcription) self.transcriptions.append( self.configTabs.widget(1).hand1Transcription) self.transcriptions.append( self.configTabs.widget(1).hand2Transcription) def generateGlobalOptions(self): for option in GLOBAL_OPTIONS: setattr(self, option, getattr(self, option + 'CheckBox').isChecked())
class ParameterDialog(QDialog): def __init__(self, model, checkStrategy=None): super().__init__() self.setWindowFlags(Qt.WindowStaysOnTopHint) self.setWindowTitle('Select Parameters') self.adjustedHeight = self.frameGeometry().height() self.adjustedWidth = self.frameGeometry().width() self.adjustedPos = self.pos() self.model = model self.model.itemChanged.connect(self.updateDisplayTree) self.treeView = ParameterTreeView() self.treeView.setModel(self.model) self.treeView.clicked.connect(self.updateDisplayTree) self.displayTree = anytree.Node('Selected Parameters', parent=None) self.displayTreeWidget = QTextEdit() self.displayTreeWidget.setReadOnly(True) layout = QVBoxLayout() self.selectionLayout = QVBoxLayout() self.parameterLayout = QHBoxLayout() self.selectionLayout.addWidget(self.displayTreeWidget) self.parameterLayout.addWidget(self.treeView) self.parameterLayout.addLayout(self.selectionLayout) self.buildDisplayTree(self.model.invisibleRootItem(), self.displayTree) self.generateDisplayTreeText() terminalNodesLayout = QVBoxLayout() self.terminalNodesLabel = QTextEdit('No parameters selected') self.terminalNodesLabel.setReadOnly(True) terminalNodesLayout.addWidget(self.terminalNodesLabel) self.parameterLayout.addLayout(terminalNodesLayout) self.updateTerminalNodes() buttonLayout = QGridLayout() okButton = QPushButton('OK') cancelButton = QPushButton('Cancel') resetButton = QPushButton('Reset to default values') clearButton = QPushButton('Clear all check boxes') buttonLayout.addWidget(resetButton, 0, 0) buttonLayout.addWidget(clearButton, 0, 1) buttonLayout.addWidget(okButton, 1, 0) buttonLayout.addWidget(cancelButton, 1, 1) okButton.clicked.connect(self.accept) cancelButton.clicked.connect(self.reject) resetButton.clicked.connect(self.reset) clearButton.clicked.connect(self.clear) layout.addLayout(self.parameterLayout) layout.addLayout(buttonLayout) self.setLayout(layout) self.resize(self.adjustedHeight, self.adjustedWidth) self.move(self.adjustedPos) def saveParameters(self): def traverse(item): item.parameter.is_checked = item.checkState() if item.isEditable(): item.parameter.name = item.text() for m in range(item.rowCount()): newItem = item.child(m) traverse(newItem) output = list() for j in range(self.model.invisibleRootItem().rowCount()): item = self.model.item(j) traverse(item) output.append(self.model.item(j).parameter) return output def buildDisplayTree(self, modelItem, displayNode): canBeChecked = modelItem.isCheckable() if (not canBeChecked) or (canBeChecked and modelItem.checkState()): #display anything that cannot be checked (e.g. parent items like Quality or Location) #or display anything that could be checked and actually is newDisplayNode = anytree.Node(modelItem.text(), parent=displayNode) for n in range(modelItem.rowCount()): child = modelItem.child(n) self.buildDisplayTree(child, newDisplayNode) def generateDisplayTreeText(self): treeText = list() for pre, fill, node in anytree.RenderTree(self.displayTree): treeText.append("{}{}".format(pre, node.name)) treeText = '\n'.join(treeText) self.displayTreeWidget.setText(treeText) def clear(self): continue_ = self.showWarning() if continue_: def traverse(item): item.parameter.is_checked = False if item.isCheckable(): item.setCheckState(False) for m in range(item.rowCount()): newItem = item.child(m) traverse(newItem) for j in range(self.model.invisibleRootItem().rowCount()): item = self.model.item(j) traverse(item) self.terminalNodesLabel.setText('No parameters selected') def reset(self): continue_ = self.showWarning() if continue_: def traverse(item): item.parameter.is_checked = item.parameter.is_default if item.isCheckable(): item.setCheckState(item.parameter.is_default) if item.isEditable(): item.setText('(Specify)') for m in range(item.rowCount()): newItem = item.child(m) traverse(newItem) for j in range(self.model.invisibleRootItem().rowCount()): item = self.model.item(j) traverse(item) def showWarning(self): alert = QMessageBox() alert.setWindowFlags(Qt.WindowStaysOnTopHint) alert.setWindowTitle('Warning') alert.setText( 'This will erase your currently selected parameters. This action cannot be undone. Continue?' ) alert.addButton('OK', QMessageBox.AcceptRole) alert.addButton('Cancel', QMessageBox.RejectRole) alert.exec_() role = alert.buttonRole(alert.clickedButton()) if role == QMessageBox.AcceptRole: return True else: return False def closeEvent(self, e): self.close() def accept(self): self.saveParameters() self.adjustedHeight = self.frameGeometry().height() self.adjustedWidth = self.frameGeometry().width() self.adjustedPos = self.pos() self.hide() def reject(self): self.adjustedHeight = self.frameGeometry().height() self.adjustedWidth = self.frameGeometry().width() self.adjustedPos = self.pos() self.hide() def updateDisplayTree(self): self.displayTree = anytree.Node('Selected Parameters', parent=None) self.buildDisplayTree(self.model.invisibleRootItem(), self.displayTree) self.generateDisplayTreeText() self.updateTerminalNodes() def updateTerminalNodes(self): text = list() for pre, fill, node in anytree.RenderTree(self.displayTree): if node.is_leaf: text.append(' : '.join([node.parent.name, node.name])) text = '\n'.join(text) self.terminalNodesLabel.setText(text)