class NuggetListWidget(QWidget): def __init__(self, interactive_matching_widget): super(NuggetListWidget, self).__init__(interactive_matching_widget) self.interactive_matching_widget = interactive_matching_widget self.layout = QVBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) self.layout.setSpacing(10) # top widget self.top_widget = QWidget() self.top_layout = QHBoxLayout(self.top_widget) self.top_layout.setContentsMargins(0, 0, 0, 0) self.top_layout.setSpacing(10) self.layout.addWidget(self.top_widget) self.description = QLabel( "Below you see a list of guessed matches for you to confirm or correct." ) self.description.setFont(LABEL_FONT) self.top_layout.addWidget(self.description) self.stop_button = QPushButton("Continue With Next Attribute") self.stop_button.setFont(BUTTON_FONT) self.stop_button.clicked.connect(self._stop_button_clicked) self.stop_button.setMaximumWidth(240) self.top_layout.addWidget(self.stop_button) # nugget list self.nugget_list = CustomScrollableList(self, NuggetListItemWidget) self.layout.addWidget(self.nugget_list) def update_nuggets(self, nuggets): max_start_chars = max([ nugget[CachedContextSentenceSignal]["start_char"] for nugget in nuggets ]) self.nugget_list.update_item_list(nuggets, max_start_chars) def _stop_button_clicked(self): self.interactive_matching_widget.main_window.give_feedback_task( {"message": "stop-interactive-matching"}) def enable_input(self): self.stop_button.setEnabled(True) self.nugget_list.enable_input() def disable_input(self): self.stop_button.setDisabled(True) self.nugget_list.disable_input()
class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle('LazyDictionary') self.setWindowIcon(QIcon('book.png')) self.setFixedHeight(100) self.setFixedWidth(300) self.label = QLabel('word count: 0', self) self.label.setFont(QFont('san serif', 15)) self.label.move(90, 0) self.button() self.oneClick = True def button(self): self.btn0 = QPushButton('Start', self) self.btn0.setGeometry(0, 30, 150, 70) self.btn0.setStyleSheet('background-color:green') self.btn0.setFont(QFont('san serif', 15)) self.btn1 = QPushButton('Stop', self) self.btn1.setGeometry(150, 30, 150, 70) self.btn1.setStyleSheet('background-color:grey') self.btn1.setFont(QFont('san serif', 15)) self.btn0.clicked.connect(self.clickButton0) self.btn1.clicked.connect(self.clickButton1) def clickButton0(self): self.btn0.setStyleSheet('background-color:grey') self.btn1.setStyleSheet('background-color:red') if self.oneClick: main.start() self.oneClick = False def clickButton1(self): self.btn1.setStyleSheet('background-color:grey') self.btn0.setStyleSheet('background-color:green') main.end() self.oneClick = True def checkCount(self): text = 'word count: ' + str(htmlparse.wordCount) self.label.setText(text)
class DocumentBaseViewerWidget(MainWindowContent): def __init__(self, main_window): super(DocumentBaseViewerWidget, self).__init__(main_window, "Document Base") # controls self.controls = QWidget() self.controls_layout = QHBoxLayout(self.controls) self.controls_layout.setContentsMargins(0, 0, 0, 0) self.controls_layout.setSpacing(10) self.controls_layout.setAlignment(Qt.AlignmentFlag.AlignLeft) self.layout.addWidget(self.controls) self.create_document_base_button = QPushButton( "Create a new Document Base") self.create_document_base_button.setFont(BUTTON_FONT) self.create_document_base_button.clicked.connect( self.main_window.show_document_base_creator_widget_task) self.controls_layout.addWidget(self.create_document_base_button) self.load_and_run_default_preprocessing_phase_button = QPushButton( "Preprocess the Document Base") self.load_and_run_default_preprocessing_phase_button.setFont( BUTTON_FONT) self.load_and_run_default_preprocessing_phase_button.clicked.connect( self.main_window.load_and_run_default_preprocessing_phase_task) self.controls_layout.addWidget( self.load_and_run_default_preprocessing_phase_button) self.load_and_run_default_matching_phase_button = QPushButton( "Match the Nuggets to the Attributes") self.load_and_run_default_matching_phase_button.setFont(BUTTON_FONT) self.load_and_run_default_matching_phase_button.clicked.connect( self.main_window.load_and_run_default_matching_phase_task) self.controls_layout.addWidget( self.load_and_run_default_matching_phase_button) self.save_table_button = QPushButton("Export the Table to CSV") self.save_table_button.setFont(BUTTON_FONT) self.save_table_button.clicked.connect( self.main_window.save_table_to_csv_task) self.controls_layout.addWidget(self.save_table_button) # documents self.documents = MainWindowContentSection(self, "Documents:") self.layout.addWidget(self.documents) self.num_documents = QLabel("number of documents: -") self.num_documents.setFont(LABEL_FONT) self.documents.layout.addWidget(self.num_documents) self.num_nuggets = QLabel("number of nuggets: -") self.num_nuggets.setFont(LABEL_FONT) self.documents.layout.addWidget(self.num_nuggets) # attributes self.attributes = MainWindowContentSection(self, "Attributes:") self.layout.addWidget(self.attributes) self.add_attribute_button = QPushButton("Add Attribute") self.add_attribute_button.setFont(BUTTON_FONT) self.add_attribute_button.clicked.connect( self.main_window.add_attribute_task) self.attributes_list = CustomScrollableList(self, AttributeWidget, self.add_attribute_button) self.attributes.layout.addWidget(self.attributes_list) def update_document_base(self, document_base): # update documents self.num_documents.setText( f"number of documents: {len(document_base.documents)}") self.num_nuggets.setText( f"number of nuggets: {len(document_base.nuggets)}") # update attributes self.attributes_list.update_item_list(document_base.attributes, document_base) def enable_input(self): self.create_document_base_button.setEnabled(True) if self.main_window.document_base is not None: self.load_and_run_default_preprocessing_phase_button.setEnabled( True) self.load_and_run_default_matching_phase_button.setEnabled(True) self.save_table_button.setEnabled(True) self.add_attribute_button.setEnabled(True) self.attributes_list.enable_input() def disable_input(self): self.create_document_base_button.setDisabled(True) self.load_and_run_default_preprocessing_phase_button.setDisabled(True) self.load_and_run_default_matching_phase_button.setDisabled(True) self.save_table_button.setDisabled(True) self.add_attribute_button.setDisabled(True) self.attributes_list.disable_input()
class DocumentBaseCreatorWidget(MainWindowContent): def __init__(self, main_window) -> None: super(DocumentBaseCreatorWidget, self).__init__(main_window, "Create Document Base") self.documents = MainWindowContentSection(self, "Documents:") self.layout.addWidget(self.documents) self.documents_explanation = QLabel( "Enter the path of the directory that contains the documents as .txt files." ) self.documents_explanation.setFont(LABEL_FONT) self.documents.layout.addWidget(self.documents_explanation) self.path_widget = QFrame() self.path_layout = QHBoxLayout(self.path_widget) self.path_layout.setContentsMargins(20, 0, 20, 0) self.path_layout.setSpacing(10) self.path_widget.setStyleSheet("background-color: white") self.path_widget.setFixedHeight(40) self.documents.layout.addWidget(self.path_widget) self.path = QLineEdit() self.path.setFont(CODE_FONT_BOLD) self.path.setStyleSheet("border: none") self.path_layout.addWidget(self.path) self.edit_path_button = QPushButton() self.edit_path_button.setIcon(QIcon("aset_ui/resources/folder.svg")) self.edit_path_button.setFlat(True) self.edit_path_button.clicked.connect(self._edit_path_button_clicked) self.path_layout.addWidget(self.edit_path_button) self.attributes = MainWindowContentSection(self, "Attributes:") self.layout.addWidget(self.attributes) self.labels_explanation = QLabel("Enter the attribute names.") self.labels_explanation.setFont(LABEL_FONT) self.attributes.layout.addWidget(self.labels_explanation) self.create_attribute_button = QPushButton("New Attribute") self.create_attribute_button.setFont(BUTTON_FONT) self.create_attribute_button.clicked.connect( self._create_attribute_button_clicked) self.attribute_names = [] self.attributes_list = CustomScrollableList( self, AttributeCreatorWidget, self.create_attribute_button) self.attributes.layout.addWidget(self.attributes_list) self.buttons_widget = QWidget() self.buttons_layout = QHBoxLayout(self.buttons_widget) self.buttons_layout.setContentsMargins(0, 0, 0, 0) self.buttons_layout.setSpacing(10) self.buttons_layout.setAlignment(Qt.AlignmentFlag.AlignRight) self.attributes.layout.addWidget(self.buttons_widget) self.cancel_button = QPushButton("Cancel") self.cancel_button.setFont(BUTTON_FONT) self.cancel_button.clicked.connect(self._cancel_button_clicked) self.buttons_layout.addWidget(self.cancel_button) self.create_document_base_button = QPushButton("Create Document Base") self.create_document_base_button.setFont(BUTTON_FONT) self.create_document_base_button.clicked.connect( self._create_document_base_button_clicked) self.buttons_layout.addWidget(self.create_document_base_button) def enable_input(self): self.edit_path_button.setEnabled(True) self.create_attribute_button.setEnabled(True) self.cancel_button.setEnabled(True) self.create_document_base_button.setEnabled(True) self.attributes_list.enable_input() def disable_input(self): self.edit_path_button.setDisabled(True) self.create_attribute_button.setDisabled(True) self.cancel_button.setDisabled(True) self.create_document_base_button.setDisabled(True) self.attributes_list.disable_input() def initialize_for_new_document_base(self): self.path.setText("") self.attribute_names = [] self.attributes_list.update_item_list([]) def delete_attribute(self, attribute_name): self.attribute_names = [] for attribute_widget in self.attributes_list.item_widgets[:self. attributes_list . num_visible_item_widgets]: self.attribute_names.append(attribute_widget.name.text()) self.attribute_names.remove(attribute_name) self.attributes_list.update_item_list(self.attribute_names) self.attributes_list.last_item_widget().name.setFocus() def _edit_path_button_clicked(self): path = str( QFileDialog.getExistingDirectory( self, "Choose a directory of text files.")) if path != "": path = f"{path}/*.txt" self.path.setText(path) def _create_attribute_button_clicked(self): self.attribute_names = [] for attribute_widget in self.attributes_list.item_widgets[:self. attributes_list . num_visible_item_widgets]: self.attribute_names.append(attribute_widget.name.text()) self.attribute_names.append("") self.attributes_list.update_item_list(self.attribute_names) self.attributes_list.last_item_widget().name.setFocus() def _cancel_button_clicked(self): self.main_window.show_start_menu_widget() self.main_window.enable_global_input() def _create_document_base_button_clicked(self): self.attribute_names = [] for attribute_widget in self.attributes_list.item_widgets[:self. attributes_list . num_visible_item_widgets]: self.attribute_names.append(attribute_widget.name.text()) self.main_window.create_document_base_task(self.path.text(), self.attribute_names)
class DocumentWidget(QWidget): def __init__(self, interactive_matching_widget): super(DocumentWidget, self).__init__(interactive_matching_widget) self.interactive_matching_widget = interactive_matching_widget self.layout = QVBoxLayout() self.layout.setContentsMargins(0, 0, 0, 0) self.layout.setSpacing(10) self.setLayout(self.layout) self.document = None self.current_nugget = None self.base_formatted_text = "" self.idx_mapper = {} self.nuggets_in_order = [] self.text_edit = QTextEdit() self.layout.addWidget(self.text_edit) self.text_edit.setReadOnly(True) self.text_edit.setFrameStyle(0) self.text_edit.setFont(CODE_FONT) self.text_edit.setHorizontalScrollBarPolicy( Qt.ScrollBarPolicy.ScrollBarAlwaysOff) self.text_edit.setText("") self.buttons_widget = QWidget() self.buttons_widget_layout = QHBoxLayout(self.buttons_widget) self.layout.addWidget(self.buttons_widget) self.left_button = QPushButton("Skip Left") self.left_button.setFont(BUTTON_FONT) self.left_button.clicked.connect(self._left_button_clicked) self.buttons_widget_layout.addWidget(self.left_button) self.right_button = QPushButton("Skip Right") self.right_button.setFont(BUTTON_FONT) self.right_button.clicked.connect(self._right_button_clicked) self.buttons_widget_layout.addWidget(self.right_button) self.match_button = QPushButton("Confirm Match") self.match_button.setFont(BUTTON_FONT) self.match_button.clicked.connect(self._match_button_clicked) self.buttons_widget_layout.addWidget(self.match_button) self.no_match_button = QPushButton("There Is No Match") self.no_match_button.setFont(BUTTON_FONT) self.no_match_button.clicked.connect(self._no_match_button_clicked) self.buttons_widget_layout.addWidget(self.no_match_button) def _left_button_clicked(self): idx = self.nuggets_in_order.index(self.current_nugget) if idx > 0: self.current_nugget = self.nuggets_in_order[idx - 1] self._highlight_current_nugget() def _right_button_clicked(self): idx = self.nuggets_in_order.index(self.current_nugget) if idx < len(self.nuggets_in_order) - 1: self.current_nugget = self.nuggets_in_order[idx + 1] self._highlight_current_nugget() def _match_button_clicked(self): self.interactive_matching_widget.main_window.give_feedback_task({ "message": "is-match", "nugget": self.current_nugget }) def _no_match_button_clicked(self): self.interactive_matching_widget.main_window.give_feedback_task({ "message": "no-match-in-document", "nugget": self.current_nugget }) def _highlight_current_nugget(self): mapped_start_char = self.idx_mapper[self.current_nugget.start_char] mapped_end_char = self.idx_mapper[self.current_nugget.end_char] formatted_text = ( f"{self.base_formatted_text[:mapped_start_char]}" f"<span style='background-color: #FFFF00'><b>" f"{self.base_formatted_text[mapped_start_char:mapped_end_char]}</span></b>" f"{self.base_formatted_text[mapped_end_char:]}") self.text_edit.setText("") self.text_edit.textCursor().insertHtml(formatted_text) def update_document(self, nugget): self.document = nugget.document self.current_nugget = nugget self.nuggets_in_order = list( sorted(self.document.nuggets, key=lambda x: x.start_char)) if self.nuggets_in_order != []: self.idx_mapper = {} char_list = [] end_chars = [] next_start_char = 0 next_nugget_idx = 0 for idx, char in enumerate(list(self.document.text)): if idx == next_start_char: if end_chars == []: char_list += list("<b>") end_chars.append( self.nuggets_in_order[next_nugget_idx].end_char) next_nugget_idx += 1 if next_nugget_idx < len(self.nuggets_in_order): next_start_char = self.nuggets_in_order[ next_nugget_idx].start_char else: next_start_char = -1 while idx in end_chars: end_chars.remove(idx) if end_chars == []: char_list += list("</b>") self.idx_mapper[idx] = len(char_list) char_list.append(char) self.base_formatted_text = "".join(char_list) else: self.idx_mapper = {} for idx in range(len(self.document.text)): self.idx_mapper[idx] = idx self.base_formatted_text = "" self._highlight_current_nugget() scroll_cursor = QTextCursor(self.text_edit.document()) scroll_cursor.setPosition(nugget.start_char) self.text_edit.setTextCursor(scroll_cursor) self.text_edit.ensureCursorVisible() def enable_input(self): self.left_button.setEnabled(True) self.right_button.setEnabled(True) self.match_button.setEnabled(True) self.no_match_button.setEnabled(True) def disable_input(self): self.left_button.setDisabled(True) self.right_button.setDisabled(True) self.match_button.setDisabled(True) self.no_match_button.setDisabled(True)
class Ui_TrainPanel(object): def setupUi(self, TrainPanel): self.parent = TrainPanel self.mainLayout = QHBoxLayout(TrainPanel) self.bigFont = QLabel().font() self.bigFont.setPointSize(13) self.actionsLayout = QHBoxLayout(TrainPanel) # self.checkRecording = QCheckBox(TrainPanel) self.calibrateButton = QPushButton("Calibrate", parent=TrainPanel) self.calibrateButton.setFont(self.bigFont) self.calibrateButton.setStyleSheet(CustomQStyles.buttonStyle) self.calibrateButton.setMinimumHeight(50) self.calibrateButton.setMaximumWidth(170) self.calibrateButton.setMinimumWidth(120) self.sessionButton = QPushButton("Session", parent=TrainPanel) self.sessionButton.setFont(self.bigFont) self.sessionButton.setStyleSheet(CustomQStyles.outlineButtonStyle) self.sessionButton.setMinimumHeight(50) self.sessionButton.setMaximumWidth(170) self.sessionButton.setMinimumWidth(120) self.listFiles = QListWidget(TrainPanel) self.listFiles.setFont(self.bigFont) self.label = QLabel('or select', parent=TrainPanel) self.subjectLayout = QVBoxLayout(TrainPanel) print("init") self.box1 = QGroupBox(parent=TrainPanel) self.box2 = QGroupBox(parent=TrainPanel) self.wizard = CalibrateWizard(parent=TrainPanel) self.setPatientsBox(TrainPanel) # right panel self.setActionsBox(TrainPanel) # left panel self.mainLayout.addWidget(self.box1, stretch=2) self.mainLayout.addWidget(self.box2, stretch=3) # self.mainLayout.setAlignment(self.box2, Qt.Alignment.AlignCenter) # Display progress bar, checkbox - to record new gestures or not, start train def setActionsBox(self, TrainPanel): hLayout = QVBoxLayout() hLayout.addWidget(self.calibrateButton) hLayout.addWidget(self.sessionButton) hLayout.setAlignment(self.calibrateButton, Qt.Alignment.AlignCenter) hLayout.setAlignment(self.sessionButton, Qt.Alignment.AlignCenter) self.box2.setLayout(hLayout) # Display list of subjects, or new subject def setPatientsBox(self, TrainPanel): # Button to create new patient self.addPatient = QPushButton('Create Patient') self.addPatient.setFixedSize(120, 35) self.addPatient.setStyleSheet(CustomQStyles.outlineButtonStyle) self.label.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding) self.label.setAlignment(QtCore.Qt.Alignment.AlignCenter) self.loadPatientList() self.listFiles.setStyleSheet(CustomQStyles.listStyle) self.listFiles.setFocusPolicy(Qt.FocusPolicy.NoFocus) self.subjectLayout.addWidget(self.addPatient) self.subjectLayout.setAlignment(self.addPatient, Qt.Alignment.AlignCenter) self.subjectLayout.addWidget(self.label) self.subjectLayout.addWidget(self.listFiles) self.box1.setLayout(self.subjectLayout) def loadPatientList(self): self.listFiles.clear() self.parent.patients.clear() files = [ f for f in listdir(PATIENTS_PATH) if isfile(join(PATIENTS_PATH, f)) ] for x, ind in zip(files, range(0, len(files))): item = QListWidgetItem(x.split('.')[0]) item.setTextAlignment(Qt.Alignment.AlignHCenter) self.listFiles.addItem(item) with open(PATIENTS_PATH + x, 'r') as f: person_dict = json.load(f) patient = Patient(person_dict['Name'], person_dict['Age'], person_dict['Exercises']) self.parent.patients.append(patient) print(self.parent.patients) def deleteItemsOfLayout(self, layout): if layout is not None: while layout.count(): item = layout.takeAt(0) widget = item.widget() if widget is not None: widget.setParent(None) else: self.deleteItemsOfLayout(item.layout())