class SearchDialog(QDialog): def __init__(self, table=None): super().__init__(GlobalAccess().get_main_window()) if table is not None: assert (isinstance(table, QTableView)) self.table = table def exec_(self): self.init_ui() return super().exec_() def init_ui(self): self.setWindowModality(QtCore.Qt.WindowModal) self.setWindowIcon(QIcon(config.ICON)) self.setSizeGripEnabled(False) self.setModal(True) self.layout = QVBoxLayout(self) self.item_serach = QLineEdit() if self.table is not None: self.item_serach.setText(self.table.model().search) self.item_serach.selectAll() button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.button_ok = button_box.button(QDialogButtonBox.Ok) self.button_ok.clicked.connect(self.ok) self.button_cancel = button_box.button(QDialogButtonBox.Cancel) self.button_cancel.clicked.connect(self.cancel) self.layout.addWidget(self.item_serach) self.layout.addWidget(button_box) self.retranslate_ui() self.show() def ok(self): try: if self.table is not None: proxy_model = self.table.model() proxy_model.search = self.item_serach.text() proxy_model.apply_search() offset = proxy_model.search_offset if offset == -1 and proxy_model.search: QMessageBox.warning(self, _('Search'), _('The search has not given any results')) self.table.selectRow(offset) except Exception as e: logging.error(str(e)) def cancel(self): self.close() def retranslate_ui(self): self.setWindowTitle(_('Search')) self.button_ok.setText(_('OK')) self.button_cancel.setText(_('Cancel'))
def getData(self, currentAmount): amtTitle = QLabel('Total: $') amtTitle.setMaximumWidth(120) validator = QIntValidator(1, 99999, self) amtEdit = QLineEdit() amtEdit.setValidator(validator) amtEdit.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter) amtEdit.returnPressed.connect(self.okClicked) amtEdit.setText(str(currentAmount)) amtEdit.selectAll() okButton = QPushButton('OK') okButton.clicked.connect(self.okClicked) amtLayout = QHBoxLayout() amtLayout.addWidget(amtTitle) amtLayout.addWidget(amtEdit) amtLayout.addWidget(okButton) self.setLayout(amtLayout) retValue = self.exec() if retValue: return amtEdit.text() else: return 0
class MyWindow(QWidget): def __init__(self): super().__init__() vbox1 = QVBoxLayout() self.my_lineedit = QLineEdit("What type of photo are you looking for?") self.my_lineedit.setMinimumWidth(250) self.my_lineedit.selectAll() self.my_btn = QPushButton("Submit") self.my_lbl = QLabel('') self.my_btn.clicked.connect(self.on_submit) self.my_lineedit.returnPressed.connect(self.on_submit) vbox1.addWidget(self.my_lineedit) vbox1.addWidget(self.my_btn) vbox1.addWidget(self.my_lbl) self.setLayout(vbox1) gbox1 = QGroupBox('Photo Search') gbox1.setLayout(vbox1) self.my_list = [ "Pick a filter", 'None', 'Sepia', 'Negative', 'Grayscale', 'Thumbnail' ] # 0 1 2 3 4 5 self.my_combo_box = QComboBox() self.my_combo_box.addItems(self.my_list) self.my_label = QLabel("") vbox2 = QVBoxLayout() vbox2.addWidget(self.my_combo_box) vbox2.addWidget(self.my_label) self.setLayout(vbox2) self.my_combo_box.currentIndexChanged.connect(self.update_ui) gbox2 = QGroupBox("Filters") gbox2.setLayout(vbox2) mbox = QVBoxLayout() mbox.addWidget(gbox1) mbox.addWidget(gbox2) self.setLayout(mbox) self.setWindowTitle("CST 205 App") @Slot() def on_submit(self): your_photo = self.my_lineedit.text() self.my_lbl.setText(f'Your photo is {your_photo}') @Slot() def update_ui(self): my_text = self.my_combo_box.currentText() my_index = self.my_combo_box.currentIndex() self.my_label.setText(f'You chose {self.my_list[my_index]}.')
class TextInput(): def __init__(self, suggestion='', input_valid_callback = None): self.input_valid_callback = input_valid_callback self.menu = QMenu() self.lb = QWidgetAction(None) self.widget = QWidget(None) self.layout = QHBoxLayout() self.button = QPushButton() self.button.setText("Ok") self.le = QLineEdit() self.layout.addWidget(self.le) self.layout.addWidget(self.button) self.widget.setLayout(self.layout) self.button.pressed.connect(self.done) self.le.textEdited.connect(self.check_input) self.eventFilter = EnterKeyPressFilter() self.eventFilter.callback = self.done self.le.installEventFilter(self.eventFilter) self.lb.setDefaultWidget(self.widget) self.menu.addAction(self.lb) self.le.setText(suggestion) self.le.setFocus() self.le.selectAll() self.result = None def show(self, pos): self.menu.exec_(pos) return self.result def done(self): self.result = self.le.text() self.menu.close() def check_input(self): if self.input_valid_callback is not None: result = self.input_valid_callback(self.le.text()) if result: self.le.setStyleSheet('') else: self.le.setStyleSheet('background: pink') return None
class EditTextModal(QDialog): def __init__(self, oldText, windowTitle): QDialog.__init__(self) self.setGeometry(125, 100, 200, 0) self.setModal(True) self.setWindowTitle(windowTitle) self.oldText = oldText self.show() def getData(self): titleLabel = QLabel('Title: ') self.titleEdit = QLineEdit() self.titleEdit.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter) self.titleEditSs = self.titleEdit.styleSheet() self.titleEdit.textChanged.connect( lambda text: self.titleEdit.setStyleSheet(self.titleEditSs)) self.titleEdit.setMaxLength(20) self.titleEdit.returnPressed.connect(self.okClicked) self.titleEdit.setText(self.oldText) self.titleEdit.selectAll() okButton = QPushButton('OK') okButton.clicked.connect(self.okClicked) menuLayout = QVBoxLayout() topLine = QHBoxLayout() topLine.addWidget(titleLabel) topLine.addWidget(self.titleEdit) menuLayout.addLayout(topLine) menuLayout.addWidget(okButton) self.setLayout(menuLayout) retValue = self.exec() if retValue: return self.titleEdit.text() else: return 0 def okClicked(self): validator = re.compile('.*\S.*') if not re.match(validator, self.titleEdit.text()): self.titleEdit.setStyleSheet("border: 2px solid red;") else: self.accept() def cancelClicked(self): self.reject()
class Example(QWidget): def __init__(self): super().__init__() self.initUI() self.num = randint(1, 100) def initUI(self): self.setGeometry(300, 300, 300, 300) self.setWindowTitle("guess") self.setWindowIcon(QIcon('icon.jpg')) self.bt1 = QPushButton("我猜", self) self.bt1.setGeometry(115, 150, 70, 30) self.bt1.setToolTip('<b>点击这里猜游戏</b>') self.bt1.clicked.connect(self.showMessage) self.text = QLineEdit('在这里输入数字', self) self.text.selectAll() self.text.setFocus() self.text.setGeometry(80, 50, 150, 30) self.show() def showMessage(self): guessnumber = int(self.text.text()) print(self.num) if guessnumber > self.num: QMessageBox.about(self, '看结果', '猜大了!') self.text.setFocus() elif guessnumber < self.num: QMessageBox.about(self, '看结果', '猜小了!') self.text.setFocus() else: QMessageBox.about(self, '看结果', '答对了!进入下一轮!') self.num = randint(1, 100) self.text.clear() self.text.setFocus() def closeEvent(self, event): reply = QMessageBox.question(self, '确认', '确认退出吗', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: event.accept() else: event.ignore()
class SearchRouteList(QWidget): def __init__(self, routes): super().__init__() self.route_list = RouteList(routes) self.scroll_area = QScrollArea() self.scroll_area.setWidget(self.route_list) self.scroll_area.setWidgetResizable(False) self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.search_line = QLineEdit() self.search_line.setPlaceholderText('Search') self.search_line.textChanged.connect(self.search) self.search_line.returnPressed.connect(self.select_first_visible) layout = QVBoxLayout() layout.addWidget(self.search_line) layout.addWidget(self.scroll_area) self.setLayout(layout) def search(self): text_elements = self.search_line.text().lower().split(' ') for button in self.route_list.buttons: visible = all(text_element in button.text().lower() for text_element in text_elements) button.setVisible(visible) def select_first_visible(self): for button in self.route_list.buttons: if button.isVisible(): button.animateClick() return def select_route_with_name(self, name): for route, button in self.route_list.route_per_button.items(): if route.name == name: button.animateClick() return def set_search(self, text): self.search_line.setText(text) def current_search(self): return self.search_line.text() def focus(self): self.search_line.setFocus() self.search_line.selectAll()
class CellBase(Observation, QWidget): def __init__(self, subject, index): Observation.__init__(self, subject) QWidget.__init__(self) self.index = index self.selected = False self._initNameLabel() def _initNameLabel(self): self.nameLabel = QLineEdit(self) self.nameLabel.setWindowFlags(Qt.FramelessWindowHint) self.nameLabel.setStyleSheet("background: transparent; border: none;") self.nameLabel.setMaxLength(30) self.nameLabel.setContextMenuPolicy(Qt.NoContextMenu) self.nameLabel.textChanged.connect(self.onNameChanged) self.nameLabel.editingFinished.connect(self.onEditingNameFinished) def editNameResponder(self, e): if self.selected: self.nameLabel.setFocus() self.nameLabel.selectAll() def onNameChanged(self, name): pass def onEditingNameFinished(self): self.nameLabel.clearFocus() def setSelected(self, value): self.selected = value self.updateStyle() def updateStyle(self): pass def setName(self, name): self.nameLabel.setText(name) def name(self): return self.nameLabel.text() def delete(self): self.unregister() self.setParent(None) self.deleteLater()
def createEditor(self, parent, option, index): """ Create the editor widget for the table """ if index.column() == 0: # Customer Name Column cellWidget = QLineEdit(parent=parent) cellWidget.setText(index.data()) cellWidget.selectAll() else: # Time Column cellWidget = QTimeEdit(parent=parent) cellWidget.setTime( dt.datetime.strptime(index.data(), '%H:%M').time()) cellWidget.setAlignment(Qt.AlignCenter) return cellWidget
def _init_widgets(self): prompt_lbl = QLabel(self) prompt_lbl.setText(self.prompt_text) input_edt = QLineEdit(parent=self) input_edt.setText(self.initial_input_text) input_edt.selectAll() self.input_edt = input_edt prompt_input_lyt = QHBoxLayout() prompt_input_lyt.addWidget(prompt_lbl) prompt_input_lyt.addWidget(input_edt) self.main_layout.addLayout(prompt_input_lyt) buttons = QDialogButtonBox(parent=self) buttons.setStandardButtons(QDialogButtonBox.StandardButton.Cancel | QDialogButtonBox.StandardButton.Ok) buttons.accepted.connect(self._on_ok_clicked) buttons.rejected.connect(self.close) buttons_lyt = QHBoxLayout() buttons_lyt.addWidget(buttons) self.main_layout.addLayout(buttons_lyt)
class NodeChoiceWidget(QWidget): def __init__(self, flow, nodes): super(NodeChoiceWidget, self).__init__() self.flow = flow self.all_nodes = sort_nodes(nodes) # copy, no ref self.nodes = [] # 'current_nodes' are the currently selectable ones, they get recreated every time update_view() is called self.current_nodes = [] self.all_current_node_widgets = [] self.active_node_widget_index = -1 self.active_node_widget = None self.reset_list() self.node_widget_index_counter = 0 self.setMinimumWidth(260) self.setFixedHeight(450) self.main_layout = QVBoxLayout(self) self.main_layout.setAlignment(Qt.AlignTop) self.setLayout(self.main_layout) # adding all stuff to the layout self.search_line_edit = QLineEdit(self) self.search_line_edit.setPlaceholderText('search for node...') self.search_line_edit.textChanged.connect(self.update_view) self.layout().addWidget(self.search_line_edit) self.list_scroll_area = QScrollArea(self) self.list_scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.list_scroll_area.setHorizontalScrollBarPolicy( Qt.ScrollBarAsNeeded) self.list_scroll_area.setWidgetResizable(True) self.list_scroll_area_widget = QWidget() self.list_scroll_area.setWidget(self.list_scroll_area_widget) self.list_layout = QVBoxLayout() self.list_layout.setAlignment(Qt.AlignTop) self.list_scroll_area_widget.setLayout(self.list_layout) self.layout().addWidget(self.list_scroll_area) self.setFixedHeight(400) self.update_view('') try: f = open('resources/stylesheets/dark_node_choice_widget.txt') self.setStyleSheet(f.read()) f.close() except FileNotFoundError: pass self.search_line_edit.setFocus() def mousePressEvent(self, event): QWidget.mousePressEvent(self, event) event.accept() def keyPressEvent(self, event): if event.key() == Qt.Key_Escape: self.flow.hide_node_choice_widget() if event.key() == Qt.Key_Down: index = self.active_node_widget_index+1 if \ self.active_node_widget_index+1 < len(self.all_current_node_widgets) else 0 self.set_active_node_widget_index(index) if event.key() == Qt.Key_Up: index = self.active_node_widget_index-1 if \ self.active_node_widget_index-1 > -1 else len(self.all_current_node_widgets)-1 self.set_active_node_widget_index(index) if event.key() == Qt.Key_Return: if len(self.all_current_node_widgets) > 0: self.place_node(self.active_node_widget_index) def wheelEvent(self, event): QWidget.wheelEvent(self, event) event.accept() def refocus(self): self.search_line_edit.setFocus() self.search_line_edit.selectAll() def update_list(self, nodes): self.nodes = sort_nodes(nodes) def reset_list(self): self.nodes = self.all_nodes def update_view(self, text=''): text = text.lower() for i in reversed(range(self.list_layout.count())): self.list_layout.itemAt(i).widget().setParent(None) self.current_nodes.clear() self.all_current_node_widgets.clear() self.node_widget_index_counter = 0 # select valid elements from text # nodes nodes_names_dict = {} for n in self.nodes: nodes_names_dict[n] = n.title.lower() sorted_indices = self.get_sorted_dict_matching_search( nodes_names_dict, text) for n, index in sorted_indices.items(): self.current_nodes.append(n) # nodes if len(self.current_nodes) > 0: nodes_label = QLabel('Hover for description') nodes_label_font = QFont('Poppins') nodes_label_font.setPixelSize(15) nodes_label.setStyleSheet('color: #9bbf9dff; border: none;') nodes_label.setFont(nodes_label_font) self.list_layout.addWidget(nodes_label) for n in self.current_nodes: node_widget = self.create_list_item_widget(n) self.list_layout.addWidget(node_widget) self.all_current_node_widgets.append(node_widget) if len(self.all_current_node_widgets) > 0: self.set_active_node_widget_index(0) # self.setFixedWidth(self.minimumWidth()) # print(self.list_scroll_area_widget.width()) def get_sorted_dict_matching_search(self, items_dict, text): indices_dict = {} for item, name in items_dict.items( ): # the strings are already lowered here Debugger.debug(item, name, text) if name.__contains__(text): index = name.index(text) indices_dict[item] = index return { k: v for k, v in sorted(indices_dict.items(), key=lambda i: i[1]) } def create_list_item_widget(self, node): node_widget = NodeWidget(self, node) # , self.node_images[node]) node_widget.custom_focused_from_inside.connect( self.node_widget_focused_from_inside) node_widget.setObjectName('node_widget_' + str(self.node_widget_index_counter)) self.node_widget_index_counter += 1 node_widget.chosen.connect(self.node_widget_chosen) return node_widget def node_widget_focused_from_inside(self): self.set_active_node_widget_index( self.all_current_node_widgets.index(self.sender())) def set_active_node_widget_index(self, index): self.active_node_widget_index = index node_widget = self.all_current_node_widgets[index] if self.active_node_widget: self.active_node_widget.set_custom_focus(False) node_widget.set_custom_focus(True) self.active_node_widget = node_widget self.list_scroll_area.ensureWidgetVisible(self.active_node_widget) def node_widget_chosen( self ): # gets called when the user clicks on a node widget with the mouse self.flow.ignore_mouse_event = True # otherwise, it will receive a mouse press event index = int( self.sender().objectName()[self.sender().objectName().rindex('_') + 1:]) self.place_node(index) def place_node(self, index): node_index = index node = self.current_nodes[node_index] self.flow.place_node__cmd(node) self.flow.hide_node_choice_widget()
class OrganizationEditDialog(QDialog): def __init__(self, organization, is_new=False): super().__init__(GlobalAccess().get_main_window()) assert (isinstance(organization, Organization)) self.current_object = organization self.is_new = is_new def exec_(self): self.init_ui() self.set_values_from_model() return super().exec_() def init_ui(self): self.setWindowTitle(_('Team properties')) self.setWindowIcon(QIcon(config.ICON)) self.setSizeGripEnabled(False) self.setModal(True) self.layout = QFormLayout(self) self.label_name = QLabel(_('Name')) self.item_name = QLineEdit() self.item_name.textChanged.connect(self.check_name) self.layout.addRow(self.label_name, self.item_name) self.label_code = QLabel(_('Code')) self.item_code = QLineEdit() self.layout.addRow(self.label_code, self.item_code) self.label_country = QLabel(_('Country')) self.item_country = AdvComboBox() self.item_country.addItems(get_countries()) self.layout.addRow(self.label_country, self.item_country) self.label_region = QLabel(_('Region')) self.item_region = AdvComboBox() self.item_region.addItems(get_regions()) self.layout.addRow(self.label_region, self.item_region) self.label_contact = QLabel(_('Contact')) self.item_contact = QLineEdit() self.layout.addRow(self.label_contact, self.item_contact) def cancel_changes(): self.close() def apply_changes(): try: self.apply_changes_impl() except Exception as e: logging.error(str(e)) self.close() button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.button_ok = button_box.button(QDialogButtonBox.Ok) self.button_ok.setText(_('OK')) self.button_ok.clicked.connect(apply_changes) self.button_cancel = button_box.button(QDialogButtonBox.Cancel) self.button_cancel.setText(_('Cancel')) self.button_cancel.clicked.connect(cancel_changes) self.layout.addRow(button_box) self.show() def check_name(self): name = self.item_name.text() self.button_ok.setDisabled(False) if name and name != self.current_object.name: org = find(race().organizations, name=name) if org: self.button_ok.setDisabled(True) def set_values_from_model(self): self.item_name.setText(self.current_object.name) self.item_name.selectAll() self.item_code.setText(self.current_object.code) self.item_country.setCurrentText(self.current_object.country) self.item_region.setCurrentText(self.current_object.region) self.item_contact.setText(self.current_object.contact) def apply_changes_impl(self): org = self.current_object assert (isinstance(org, Organization)) if self.is_new: race().organizations.insert(0, org) org.name = self.item_name.text() org.code = self.item_code.text() org.country = self.item_country.currentText() org.region = self.item_region.currentText() org.contact = self.item_contact.text() Teamwork().send(org.to_dict())
class View(QMainWindow): """View component of the MVC application. Presents the state of the application as well as the available means of interaction. Receives updates about the state from the Model and informs Controller about user interactions. """ def __init__(self, model, controller): """Define GUI elements and their layout. Parameters ---------- model : QObject Model component of the MVC application. controller : QObject Controller component of the MVC application. """ super().__init__() self._model = model self._controller = controller self.segmentcursor = False self.togglecolors = {"#1f77b4": "m", "m": "#1f77b4"} self.setWindowTitle("biopeaks") self.setGeometry(50, 50, 1750, 750) self.setWindowIcon(QIcon(":/python_icon.png")) # Figure for biosignal. self.figure0 = Figure() self.canvas0 = FigureCanvas(self.figure0) # Enforce minimum height, otherwise resizing with self.splitter causes # mpl to throw an error because figure is resized to height 0. The # widget can still be fully collapsed with self.splitter. self.canvas0.setMinimumHeight(1) # in pixels self.ax00 = self.figure0.add_subplot(1, 1, 1) self.ax00.set_frame_on(False) self.figure0.subplots_adjust(left=0.04, right=0.98, bottom=0.25) self.line00 = None self.scat = None self.segmentspan = None # Figure for marker. self.figure1 = Figure() self.canvas1 = FigureCanvas(self.figure1) self.canvas1.setMinimumHeight(1) self.ax10 = self.figure1.add_subplot(1, 1, 1, sharex=self.ax00) self.ax10.get_xaxis().set_visible(False) self.ax10.set_frame_on(False) self.figure1.subplots_adjust(left=0.04, right=0.98) self.line10 = None # Figure for statistics. self.figure2 = Figure() self.canvas2 = FigureCanvas(self.figure2) self.canvas2.setMinimumHeight(1) self.ax20 = self.figure2.add_subplot(3, 1, 1, sharex=self.ax00) self.ax20.get_xaxis().set_visible(False) self.ax20.set_frame_on(False) self.line20 = None self.ax21 = self.figure2.add_subplot(3, 1, 2, sharex=self.ax00) self.ax21.get_xaxis().set_visible(False) self.ax21.set_frame_on(False) self.line21 = None self.ax22 = self.figure2.add_subplot(3, 1, 3, sharex=self.ax00) self.ax22.get_xaxis().set_visible(False) self.ax22.set_frame_on(False) self.line22 = None self.figure2.subplots_adjust(left=0.04, right=0.98) self.navitools = CustomNavigationToolbar(self.canvas0, self) # Peak editing. self.editcheckbox = QCheckBox("editable", self) self.editcheckbox.stateChanged.connect(self._model.set_peakseditable) # Peak saving during batch processing. self.savecheckbox = QCheckBox("save during batch processing", self) self.savecheckbox.stateChanged.connect(self._model.set_savebatchpeaks) # Peak auto-correction during batch processing. self.correctcheckbox = QCheckBox("correct during batch processing", self) self.correctcheckbox.stateChanged.connect( self._model.set_correctbatchpeaks) # Selection of stats for saving. self.periodcheckbox = QCheckBox("period", self) self.periodcheckbox.stateChanged.connect( lambda: self.select_stats("period")) self.ratecheckbox = QCheckBox("rate", self) self.ratecheckbox.stateChanged.connect( lambda: self.select_stats("rate")) self.tidalampcheckbox = QCheckBox("tidal amplitude", self) self.tidalampcheckbox.stateChanged.connect( lambda: self.select_stats("tidalamp")) # Channel selection. self.sigchanmenulabel = QLabel("biosignal") self.sigchanmenu = QComboBox(self) self.sigchanmenu.addItem("A1") self.sigchanmenu.addItem("A2") self.sigchanmenu.addItem("A3") self.sigchanmenu.addItem("A4") self.sigchanmenu.addItem("A5") self.sigchanmenu.addItem("A6") self.sigchanmenu.currentTextChanged.connect(self._model.set_signalchan) self._model.set_signalchan( self.sigchanmenu.currentText()) # initialize with default value self.markerchanmenulabel = QLabel("marker") self.markerchanmenu = QComboBox(self) self.markerchanmenu.addItem("none") self.markerchanmenu.addItem("I1") self.markerchanmenu.addItem("I2") self.markerchanmenu.addItem("A1") self.markerchanmenu.addItem("A2") self.markerchanmenu.addItem("A3") self.markerchanmenu.addItem("A4") self.markerchanmenu.addItem("A5") self.markerchanmenu.addItem("A6") self.markerchanmenu.currentTextChanged.connect( self._model.set_markerchan) self._model.set_markerchan(self.markerchanmenu.currentText()) # Processing mode. self.batchmenulabel = QLabel("mode") self.batchmenu = QComboBox(self) self.batchmenu.addItem("single file") self.batchmenu.addItem("multiple files") self.batchmenu.currentTextChanged.connect(self._model.set_batchmode) self.batchmenu.currentTextChanged.connect(self.toggle_options) self._model.set_batchmode(self.batchmenu.currentText()) self.toggle_options(self.batchmenu.currentText()) # Modality selection. self.modmenulabel = QLabel("modality") self.modmenu = QComboBox(self) self.modmenu.addItem("ECG") self.modmenu.addItem("PPG") self.modmenu.addItem("RESP") self.modmenu.currentTextChanged.connect(self._model.set_modality) self.modmenu.currentTextChanged.connect(self.toggle_options) self._model.set_modality(self.modmenu.currentText()) self.toggle_options(self.modmenu.currentText()) # Segment selection. This widget can be openend / set visible from # the menu and closed from within itself (see mapping of segmentermap). self.segmentermap = QSignalMapper(self) self.segmenter = QDockWidget("select a segment", self) self.segmenter.setFeatures( QDockWidget.NoDockWidgetFeatures ) # disable closing such that widget can only be closed by confirming selection or custom button regex = QRegExp( "[0-9]*\.?[0-9]{4}") # Limit number of decimals to four validator = QRegExpValidator(regex) self.startlabel = QLabel("start") self.startedit = QLineEdit() self.startedit.setValidator(validator) self.endlabel = QLabel("end") self.endedit = QLineEdit() self.endedit.setValidator(validator) segmentfromcursor = QAction(QIcon(":/mouse_icon.png"), "select with mouse", self) segmentfromcursor.triggered.connect(self.enable_segmentedit) self.startedit.addAction(segmentfromcursor, QLineEdit.TrailingPosition) self.endedit.addAction(segmentfromcursor, QLineEdit.TrailingPosition) self.previewedit = QPushButton("preview segment") lambdafn = lambda: self._model.set_segment( [self.startedit.text(), self.endedit.text()]) self.previewedit.clicked.connect(lambdafn) self.confirmedit = QPushButton("confirm segment") self.confirmedit.clicked.connect(self._controller.segment_dataset) self.confirmedit.clicked.connect(self.segmentermap.map) self.segmentermap.setMapping(self.confirmedit, 0) self.abortedit = QPushButton("abort segmentation") self.abortedit.clicked.connect(self.segmentermap.map) self.segmentermap.setMapping(self.abortedit, 2) # resets the segment to None self.segmenterlayout = QFormLayout() self.segmenterlayout.addRow(self.startlabel, self.startedit) self.segmenterlayout.addRow(self.endlabel, self.endedit) self.segmenterlayout.addRow(self.previewedit) self.segmenterlayout.addRow(self.confirmedit) self.segmenterlayout.addRow(self.abortedit) self.segmenterwidget = QWidget() self.segmenterwidget.setLayout(self.segmenterlayout) self.segmenter.setWidget(self.segmenterwidget) self.segmenter.setVisible(False) self.segmenter.setAllowedAreas(Qt.RightDockWidgetArea) self.addDockWidget(Qt.RightDockWidgetArea, self.segmenter) # Custom file dialog. regex = QRegExp("[1-9][0-9]") validator = QRegExpValidator(regex) self.signallabel = QLabel("biosignal column") self.signaledit = QLineEdit() self.signaledit.setValidator(validator) self.markerlabel = QLabel("marker column") self.markeredit = QLineEdit() self.markeredit.setValidator(validator) regex = QRegExp("[0-9]{2}") validator = QRegExpValidator(regex) self.headerrowslabel = QLabel("number of header rows") self.headerrowsedit = QLineEdit() self.headerrowsedit.setValidator(validator) regex = QRegExp("[0-9]{5}") validator = QRegExpValidator(regex) self.sfreqlabel = QLabel("sampling rate") self.sfreqedit = QLineEdit() self.sfreqedit.setValidator(validator) self.separatorlabel = QLabel("column separator") self.separatormenu = QComboBox(self) self.separatormenu.addItem("comma") self.separatormenu.addItem("tab") self.separatormenu.addItem("colon") self.separatormenu.addItem("space") self.continuecustomfile = QPushButton("continue loading file") self.continuecustomfile.clicked.connect(self.set_customheader) self.customfiledialog = QDialog() self.customfiledialog.setWindowTitle("custom file info") self.customfiledialog.setWindowIcon(QIcon(":/file_icon.png")) self.customfiledialog.setWindowFlags( Qt.WindowCloseButtonHint ) # remove help button by only setting close button self.customfilelayout = QFormLayout() self.customfilelayout.addRow(self.signallabel, self.signaledit) self.customfilelayout.addRow(self.markerlabel, self.markeredit) self.customfilelayout.addRow(self.separatorlabel, self.separatormenu) self.customfilelayout.addRow(self.headerrowslabel, self.headerrowsedit) self.customfilelayout.addRow(self.sfreqlabel, self.sfreqedit) self.customfilelayout.addRow(self.continuecustomfile) self.customfiledialog.setLayout(self.customfilelayout) # Layout. menubar = self.menuBar() signalmenu = menubar.addMenu("biosignal") openSignal = signalmenu.addMenu("load") openEDF = QAction("EDF", self) openEDF.triggered.connect(lambda: self._model.set_filetype("EDF")) openEDF.triggered.connect(self._controller.load_channels) openSignal.addAction(openEDF) openOpenSignals = QAction("OpenSignals", self) openOpenSignals.triggered.connect( lambda: self._model.set_filetype("OpenSignals")) openOpenSignals.triggered.connect(self._controller.load_channels) openSignal.addAction(openOpenSignals) openCustom = QAction("Custom", self) openCustom.triggered.connect( lambda: self._model.set_filetype("Custom")) openCustom.triggered.connect(lambda: self.customfiledialog.exec_()) openSignal.addAction(openCustom) segmentSignal = QAction("select segment", self) segmentSignal.triggered.connect(self.segmentermap.map) self.segmentermap.setMapping(segmentSignal, 1) signalmenu.addAction(segmentSignal) self.segmentermap.mapped.connect(self.toggle_segmenter) saveSignal = QAction("save", self) saveSignal.triggered.connect(self._controller.save_channels) signalmenu.addAction(saveSignal) peakmenu = menubar.addMenu("peaks") findPeaks = QAction("find", self) findPeaks.triggered.connect(self._controller.find_peaks) peakmenu.addAction(findPeaks) autocorrectPeaks = QAction("autocorrect", self) autocorrectPeaks.triggered.connect(self._controller.autocorrect_peaks) peakmenu.addAction(autocorrectPeaks) savePeaks = QAction("save", self) savePeaks.triggered.connect(self._controller.save_peaks) peakmenu.addAction(savePeaks) loadPeaks = QAction("load", self) loadPeaks.triggered.connect(self._controller.load_peaks) peakmenu.addAction(loadPeaks) statsmenu = menubar.addMenu("statistics") calculateStats = QAction("calculate", self) calculateStats.triggered.connect(self._controller.calculate_stats) statsmenu.addAction(calculateStats) saveStats = QAction("save", self) saveStats.triggered.connect(self._controller.save_stats) statsmenu.addAction(saveStats) self.statusBar = QStatusBar() self.setStatusBar(self.statusBar) self.progressBar = QProgressBar(self) self.progressBar.setRange(0, 1) self.statusBar.addPermanentWidget(self.progressBar) self.currentFile = QLabel() self.statusBar.addPermanentWidget(self.currentFile) self.centwidget = QWidget() # contains figures and navigationtoolbar self.setCentralWidget(self.centwidget) self.canvas0.setFocusPolicy( Qt.ClickFocus ) # only widgets (e.g. canvas) that currently have focus capture keyboard input self.canvas0.setFocus() self.canvas0.mpl_connect( "key_press_event", self._controller.edit_peaks ) # connect canvas to keyboard input for peak editing self.canvas0.mpl_connect( "button_press_event", self.get_xcursor) # connect canvas to mouse input for peak editing self.splitter = QSplitter( Qt.Vertical ) # arrange the three figure canvases in splitter object self.splitter.setOpaqueResize( False) # resizing gets very slow otherwise once axes are populated self.splitter.addWidget(self.canvas0) self.splitter.addWidget(self.canvas1) self.splitter.addWidget(self.canvas2) self.splitter.setChildrenCollapsible(False) self.vlayout0 = QVBoxLayout(self.centwidget) self.vlayout1 = QVBoxLayout() self.vlayoutA = QFormLayout() self.vlayoutB = QFormLayout() self.vlayoutC = QVBoxLayout() self.vlayoutD = QVBoxLayout() self.hlayout0 = QHBoxLayout() self.optionsgroupA = QGroupBox("processing options") self.vlayoutA.addRow(self.modmenulabel, self.modmenu) self.vlayoutA.addRow(self.batchmenulabel, self.batchmenu) self.optionsgroupA.setLayout(self.vlayoutA) self.optionsgroupB = QGroupBox("channels") self.vlayoutB.addRow(self.sigchanmenulabel, self.sigchanmenu) self.vlayoutB.addRow(self.markerchanmenulabel, self.markerchanmenu) self.optionsgroupB.setLayout(self.vlayoutB) self.optionsgroupC = QGroupBox("peaks") self.vlayoutC.addWidget(self.editcheckbox) self.vlayoutC.addWidget(self.savecheckbox) self.vlayoutC.addWidget(self.correctcheckbox) self.optionsgroupC.setLayout(self.vlayoutC) self.optionsgroupD = QGroupBox("select statistics for saving") self.vlayoutD.addWidget(self.periodcheckbox) self.vlayoutD.addWidget(self.ratecheckbox) self.vlayoutD.addWidget(self.tidalampcheckbox) self.optionsgroupD.setLayout(self.vlayoutD) self.vlayout1.addWidget(self.optionsgroupA) self.vlayout1.addWidget(self.optionsgroupB) self.vlayout1.addWidget(self.optionsgroupC) self.vlayout1.addWidget(self.optionsgroupD) self.optionsgroupwidget = QWidget() self.optionsgroupwidget.setLayout(self.vlayout1) self.optionsgroup = QDockWidget("configurations", self) self.optionsgroup.setAllowedAreas(Qt.LeftDockWidgetArea) self.toggleoptionsgroup = self.optionsgroup.toggleViewAction() self.toggleoptionsgroup.setText("show/hide configurations") menubar.addAction(self.toggleoptionsgroup) self.optionsgroup.setWidget(self.optionsgroupwidget) self.addDockWidget(Qt.LeftDockWidgetArea, self.optionsgroup) self.vlayout0.addWidget(self.splitter) self.hlayout0.addWidget(self.navitools) self.vlayout0.addLayout(self.hlayout0) # Subscribe to updates from the Model. self._model.signal_changed.connect(self.plot_signal) self._model.marker_changed.connect(self.plot_marker) self._model.peaks_changed.connect(self.plot_peaks) self._model.period_changed.connect(self.plot_period) self._model.rate_changed.connect(self.plot_rate) self._model.tidalamp_changed.connect(self.plot_tidalamp) self._model.path_changed.connect(self.display_path) self._model.segment_changed.connect(self.plot_segment) self._model.status_changed.connect(self.display_status) self._model.progress_changed.connect(self.display_progress) self._model.model_reset.connect(self.reset_plot) def plot_signal(self, signal): """Plot the biosignal. Receives updates in signal from Model. Parameters ---------- signal : ndarray of float Vector representing the biosignal. See Also -------- model.Model.signal """ self.ax00.clear() self.ax00.relim() self.navitools.update() # reset navitools history self.line00 = self.ax00.plot(self._model.sec, signal, zorder=1) self.ax00.set_xlabel("seconds", fontsize="large", fontweight="heavy") self.canvas0.draw() def plot_peaks(self, peaks): """Plot the extrema. Receives updates in peaks from Model. Parameters ---------- peaks : ndarray of int Vector representing the extrema. See Also -------- model.Model.peaks """ if self.ax00.collections: # self.scat is listed in ax.collections self.ax00.collections[0].remove() self.scat = self.ax00.scatter(self._model.sec[peaks], self._model.signal[peaks], c="m", zorder=2) self.canvas0.draw() def plot_segment(self, segment): """Show preview of segment. Receives updates in segment from Model. Parameters ---------- segment : list of float The start and end of the segment in seconds. See Also -------- model.Model.segment """ if segment is None: # if an invalid segment has been selected reset the segmenter interface self.toggle_segmenter(1) return if self.ax00.patches: # self.segementspan is listed in ax.patches self.ax00.patches[0].remove() self.segmentspan = self.ax00.axvspan(segment[0], segment[1], color="m", alpha=0.25) self.canvas0.draw() self.confirmedit.setEnabled(True) def plot_marker(self, marker): """Plot the marker channel. Receives updates in marker from Model. Parameters ---------- marker : list of ndarray Seconds element is vector representing the marker channel and first element is a vector representing the seconds associated with each sample in the marker channel. See Also -------- model.Model.marker """ self.ax10.clear() self.ax10.relim() self.line10 = self.ax10.plot(marker[0], marker[1]) self.canvas1.draw() def plot_period(self, period): """Plot instantaneous period. Receives updates in period from Model. Parameters ---------- period : ndarray of float Vector representing the instantaneous period. See Also -------- model.Model.periodintp """ self.ax20.clear() self.ax20.relim() self.navitools.home() if self._model.savestats["period"]: self.line20 = self.ax20.plot(self._model.sec, period, c="m") else: self.line20 = self.ax20.plot(self._model.sec, period) self.ax20.set_ylim(bottom=min(period), top=max(period)) self.ax20.set_title("period", pad=0, fontweight="heavy") self.ax20.grid(True, axis="y") self.navitools.update() self.canvas2.draw() def plot_rate(self, rate): """Plot instantaneous rate. Receives updates in rate from Model. Parameters ---------- rate : ndarray of float Vector representing the instantaneous rate. See Also -------- model.Model.rateintp """ self.ax21.clear() self.ax21.relim() self.navitools.home() if self._model.savestats["rate"]: self.line21 = self.ax21.plot(self._model.sec, rate, c="m") else: self.line21 = self.ax21.plot(self._model.sec, rate) self.ax21.set_ylim(bottom=min(rate), top=max(rate)) self.ax21.set_title("rate", pad=0, fontweight="heavy") self.ax21.grid(True, axis="y") self.navitools.update() self.canvas2.draw() def plot_tidalamp(self, tidalamp): """Plot instantaneous tidal amplitude. Receives updates in tidal amplitude from Model. Parameters ---------- tidalamp : ndarray of float Vector representing the instantaneous tidal amplitude. See Also -------- model.Model.tidalampintp """ self.ax22.clear() self.ax22.relim() self.navitools.home() if self._model.savestats["tidalamp"]: self.line22 = self.ax22.plot(self._model.sec, tidalamp, c="m") else: self.line22 = self.ax22.plot(self._model.sec, tidalamp) self.ax22.set_ylim(bottom=min(tidalamp), top=max(tidalamp)) self.ax22.set_title("amplitude", pad=0, fontweight="heavy") self.ax22.grid(True, axis="y") self.navitools.update() self.canvas2.draw() def display_path(self, path): """Display the path to the current dataset. Receives update in path from Model. Parameters ---------- path : str The path to the file containing the current dataset. See Also -------- model.Model.rpathsignal """ self.currentFile.setText(path) def display_status(self, status): """Display a status message. Receives updates in status message from Model. Parameters ---------- status : str A status message. See Also -------- model.Model.status """ self.statusBar.showMessage(status) def display_progress(self, progress): """Display task progress. Receives updates in progress from Model. Parameters ---------- progress : int Integer indicating the current task progress. See Also -------- model.Model.progress, controller.Worker, controller.threaded """ self.progressBar.setRange( 0, progress) # indicates busy state if progress is 0 def toggle_segmenter(self, visibility_state): """Toggle visibility of segmenter widget. Parameters ---------- visibility_state : int Update in state of the segmenter widget's visibility. """ if not self._model.loaded: return if visibility_state == 1: # open segmenter when called from signalmenu or clear segmenter upon selection of invalid segment self.segmenter.setVisible(True) self.confirmedit.setEnabled(False) self.startedit.clear() self.endedit.clear() elif visibility_state == 0: # close segmenter after segment has been confirmed self.segmenter.setVisible(False) elif visibility_state == 2: # close segmenter after segmentation has been aborted (reset segment) self._model.set_segment([0, 0]) self.segmenter.setVisible(False) if self.ax00.patches: self.ax00.patches[0].remove() self.canvas0.draw() def enable_segmentedit(self): """Associate cursor position with a specific segmenter text field. Regulate if cursor position is associated with editing the start or end of a segment. """ self.editcheckbox.setChecked( False) # disable peak editing to avoid interference if self.startedit.hasFocus(): self.segmentcursor = "start" elif self.endedit.hasFocus(): self.segmentcursor = "end" def get_xcursor(self, mouse_event): """Retrieve input to segmenter text fields from cursor position. Retrieve the start or end of a segment in seconds from the current cursor position. Parameters ---------- mouse_event : MouseEvent Event containing information about the current cursor position in data coordinates. See Also -------- matplotlib.backend_bases.MouseEvent """ if mouse_event.button != 1: # 1 = left mouse button return if self.segmentcursor == "start": self.startedit.selectAll() self.startedit.insert("{:.2f}".format( mouse_event.xdata)) # limit number of decimal places to two elif self.segmentcursor == "end": self.endedit.selectAll() self.endedit.insert("{:.2f}".format(mouse_event.xdata)) self.segmentcursor = False # disable segment cursor again after value has been set def set_customheader(self): """Populate the customheader with inputs from the customfiledialog.""" mandatoryfields = self.signaledit.text() and self.headerrowsedit.text( ) and self.sfreqedit.text( ) # check if one of the mandatory fields is missing if not mandatoryfields: self._model.status = ( "Please provide values for 'biosignal column'" ", 'number of header rows' and 'sampling" " rate'.") return seps = {"comma": ",", "tab": "\t", "colon": ":", "space": " "} self._model.customheader = dict.fromkeys( self._model.customheader, None ) # reset header here since it cannot be reset in controller.load_chanels self._model.customheader["signalidx"] = int(self.signaledit.text()) self._model.customheader["skiprows"] = int(self.headerrowsedit.text()) self._model.customheader["sfreq"] = int(self.sfreqedit.text()) self._model.customheader["separator"] = seps[ self.separatormenu.currentText()] if self.markeredit.text(): # not mandatory self._model.customheader["markeridx"] = int(self.markeredit.text()) self.customfiledialog.done(QDialog.Accepted) # close the dialog window self._controller.load_channels() # move on to file selection def select_stats(self, statistic): """Select statistics to be saved. Parameters ---------- statistic : str The selected statistic. """ self._model.savestats[ statistic] ^= True # toggle boolean with xor operator line = None if statistic == "period": if self.line20: line = self.line20[0] elif statistic == "rate": if self.line21: line = self.line21[0] elif statistic == "tidalamp": if self.line22: line = self.line22[0] if line: line.set_color(self.togglecolors[line.get_color()]) self.canvas2.draw() def toggle_options(self, state): """Toggle availability of configuration options. Based on current state. Parameters ---------- state : str The aspect of the current state to which the availability of configuration options needs to be adapted. """ if state in ["ECG", "PPG"]: self.tidalampcheckbox.setEnabled(False) self.tidalampcheckbox.setChecked(False) self.ax22.set_visible(False) self.canvas2.draw() elif state == "RESP": self.tidalampcheckbox.setEnabled(True) self.ax22.set_visible(True) self.canvas2.draw() elif state == "multiple files": self.editcheckbox.setEnabled(False) self.editcheckbox.setChecked(False) self.savecheckbox.setEnabled(True) self.correctcheckbox.setEnabled(True) self.markerchanmenu.setEnabled(False) elif state == "single file": self.editcheckbox.setEnabled(True) self.markerchanmenu.setEnabled(True) self.savecheckbox.setEnabled(False) self.savecheckbox.setChecked(False) self.correctcheckbox.setEnabled(False) self.correctcheckbox.setChecked(False) def reset_plot(self): """Reset plot elements associated with the current dataset.""" self.ax00.clear() self.ax00.relim() self.line00 = None self.scat = None self.segmentspan = None self.ax10.clear() self.ax10.relim() self.line10 = None self.ax20.clear() self.ax20.relim() self.line20 = None self.ax21.clear() self.ax21.relim() self.line21 = None self.ax22.clear() self.ax22.relim() self.line22 = None self.canvas0.draw() self.canvas1.draw() self.canvas2.draw() self.navitools.update() self.currentFile.clear()
class ResultWidget(QWidget): def __init__(self, route=None): super().__init__() layout = QVBoxLayout() self.route = route self.thread_pool = QThreadPool() layout_send = QHBoxLayout() self.send_button = QPushButton('Send') self.send_button.clicked.connect(self.make_request) self.search_line = QLineEdit() self.search_line.setPlaceholderText('Search') self.search_line.textChanged.connect(self.search_result_reset) self.search_line.returnPressed.connect(self.search_result) self.response_status_label = QLabel() self.response_status_label.setFont(FONT_ROUTE) self.response_status_label.hide() self.elapsed_time_label = QLabel() self.elapsed_time_label.setFont(FONT_ROUTE) self.elapsed_time_label.hide() self.search_summary_label = QLabel() self.search_summary_label.setFont(FONT_ROUTE) self.search_summary_label.hide() if route is not None: layout_send.addWidget(self.send_button) layout_send.addWidget(self.response_status_label) layout_send.addWidget(self.elapsed_time_label) layout_send.addStretch(1) layout_send.addWidget(self.search_summary_label) layout_send.addWidget(self.search_line) layout.addLayout(layout_send) self.result_text_edit = ResultTextEdit() self.result_text_edit.setReadOnly(True) self.result_text_edit.setFont(TEXT_FONT) self.result_text_edit.setContextMenuPolicy(Qt.NoContextMenu) self.result_text_edit.search.connect(self.focus) self.shortcut = QShortcut(QKeySequence("Ctrl+Return"), self, self.make_request) self.result_text_edit.setUndoRedoEnabled(False) self.highlighter = TextHighlighter(self.result_text_edit.document()) layout.addWidget(self.result_text_edit) if route is not None: saved_result = load_request_result(route) self.result_text_edit.setPlainText(saved_result) self.setLayout(layout) def goto(self, to): c = self.result_text_edit.textCursor() c.movePosition(to, QTextCursor.MoveAnchor, 1) self.result_text_edit.setTextCursor(c) def search_result_reset(self): self.goto(QTextCursor.Start) string_format = QTextCharFormat() string_format.setBackground(QColor('#668B8B')) extras = [] self.search_positions = [] while True: extra = QTextEdit.ExtraSelection() found = self.result_text_edit.find(self.search_line.text()) if not found: break extra.cursor = self.result_text_edit.textCursor() extra.format = string_format self.search_positions.append(extra.cursor.position()) extras.append(extra) self.result_text_edit.setExtraSelections(extras) self.goto(QTextCursor.Start) self.search_result() def search_result(self): p = self.result_text_edit.palette() p.setColor(QPalette.Highlight, QColor("#ee799f")) self.result_text_edit.setPalette(p) search_settings = QTextDocument.FindFlags() mod = QApplication.keyboardModifiers() if (mod & Qt.ShiftModifier) != 0: search_settings |= QTextDocument.FindBackward r = self.result_text_edit.find(self.search_line.text(), search_settings) if not r: if (mod & Qt.ShiftModifier) != 0: self.goto(QTextCursor.End) else: self.goto(QTextCursor.Start) self.result_text_edit.find(self.search_line.text(), search_settings) if self.search_line.text() == '': self.search_summary_label.hide() return current_position = self.result_text_edit.textCursor().position() try: current_index = self.search_positions.index(current_position) except ValueError: current_index = -1 self.search_summary_label.show() self.search_summary_label.setText( '%s/%s' % (current_index + 1, len(self.search_positions))) def focus(self): self.search_line.setFocus() self.search_line.selectAll() def make_request(self): self.response_status_label.hide() self.elapsed_time_label.hide() self.result_text_edit.setPlainText('Loading..') try: group_values, route_values = get_parameter_values_for_route( self.route) request = self.route.get_request(group_values, route_values) worker = RequestWorker( request.method, request.url, params=request.params, headers=request.headers, json=request.json, ) worker.signals.result.connect(self.set_result) self.thread_pool.start(worker) except CaribouException as e: self.result_text_edit.setPlainText(str(e)) except Exception: self.result_text_edit.setPlainText(traceback.format_exc()) def set_result(self, text, status_code, elapsed_time): if status_code == 0: self.response_status_label.hide() self.elapsed_time_label.hide() else: p = self.response_status_label.palette() if status_code == 200: self.response_status_label.setText(str(status_code) + ' OK') p.setColor(QPalette.WindowText, QColor('#1FDA9A')) else: self.response_status_label.setText(str(status_code) + ' ERROR') p.setColor(QPalette.WindowText, QColor('#DB3340')) self.response_status_label.setPalette(p) self.response_status_label.show() self.elapsed_time_label.setText('%s ms' % int(elapsed_time * 1000)) self.elapsed_time_label.show() self.result_text_edit.setUpdatesEnabled(False) self.result_text_edit.setPlainText(text) self.result_text_edit.setUpdatesEnabled(True) save_request_result(self.route, text) persist_storage()
class View(QMainWindow): def __init__(self, model, controller): super().__init__() self._model = model self._controller = controller self.segmentcursor = False self.togglecolors = {"#1f77b4": "m", "m": "#1f77b4"} ################################################################# # define GUI layout and connect input widgets to external slots # ################################################################# self.setWindowTitle("biopeaks") self.setGeometry(50, 50, 1750, 750) self.setWindowIcon(QIcon(":/python_icon.png")) # figure0 for signal self.figure0 = Figure() self.canvas0 = FigureCanvas(self.figure0) # Enforce minimum height, otherwise resizing with self.splitter causes # mpl to throw an error because figure is resized to height 0. The # widget can still be fully collapsed with self.splitter- self.canvas0.setMinimumHeight(1) # in pixels self.ax00 = self.figure0.add_subplot(1, 1, 1) self.ax00.set_frame_on(False) self.figure0.subplots_adjust(left=0.04, right=0.98, bottom=0.25) self.line00 = None self.scat = None self.segmentspan = None # figure1 for marker self.figure1 = Figure() self.canvas1 = FigureCanvas(self.figure1) self.canvas1.setMinimumHeight(1) self.ax10 = self.figure1.add_subplot(1, 1, 1, sharex=self.ax00) self.ax10.get_xaxis().set_visible(False) self.ax10.set_frame_on(False) self.figure1.subplots_adjust(left=0.04, right=0.98) self.line10 = None # figure2 for statistics self.figure2 = Figure() self.canvas2 = FigureCanvas(self.figure2) self.canvas2.setMinimumHeight(1) self.ax20 = self.figure2.add_subplot(3, 1, 1, sharex=self.ax00) self.ax20.get_xaxis().set_visible(False) self.ax20.set_frame_on(False) self.line20 = None self.ax21 = self.figure2.add_subplot(3, 1, 2, sharex=self.ax00) self.ax21.get_xaxis().set_visible(False) self.ax21.set_frame_on(False) self.line21 = None self.ax22 = self.figure2.add_subplot(3, 1, 3, sharex=self.ax00) self.ax22.get_xaxis().set_visible(False) self.ax22.set_frame_on(False) self.line22 = None self.figure2.subplots_adjust(left=0.04, right=0.98) # navigation bar self.navitools = CustomNavigationToolbar(self.canvas0, self) # peak editing self.editcheckbox = QCheckBox("editable", self) self.editcheckbox.stateChanged.connect(self._model.set_peakseditable) # peak saving batch self.savecheckbox = QCheckBox("save during batch processing", self) self.savecheckbox.stateChanged.connect(self._model.set_savebatchpeaks) # peak auto-correction batch self.correctcheckbox = QCheckBox("correct during batch processing", self) self.correctcheckbox.stateChanged.connect( self._model.set_correctbatchpeaks) # selecting stats for saving self.periodcheckbox = QCheckBox("period", self) self.periodcheckbox.stateChanged.connect( lambda: self.select_stats("period")) self.ratecheckbox = QCheckBox("rate", self) self.ratecheckbox.stateChanged.connect( lambda: self.select_stats("rate")) self.tidalampcheckbox = QCheckBox("tidal amplitude", self) self.tidalampcheckbox.stateChanged.connect( lambda: self.select_stats("tidalamp")) # channel selection self.sigchanmenulabel = QLabel("biosignal") self.sigchanmenu = QComboBox(self) self.sigchanmenu.addItem("A1") self.sigchanmenu.addItem("A2") self.sigchanmenu.addItem("A3") self.sigchanmenu.addItem("A4") self.sigchanmenu.addItem("A5") self.sigchanmenu.addItem("A6") self.sigchanmenu.currentTextChanged.connect(self._model.set_signalchan) # initialize with default value self._model.set_signalchan(self.sigchanmenu.currentText()) self.markerchanmenulabel = QLabel("marker") self.markerchanmenu = QComboBox(self) self.markerchanmenu.addItem("none") self.markerchanmenu.addItem("I1") self.markerchanmenu.addItem("I2") self.markerchanmenu.addItem("A1") self.markerchanmenu.addItem("A2") self.markerchanmenu.addItem("A3") self.markerchanmenu.addItem("A4") self.markerchanmenu.addItem("A5") self.markerchanmenu.addItem("A6") self.markerchanmenu.currentTextChanged.connect( self._model.set_markerchan) # initialize with default value self._model.set_markerchan(self.markerchanmenu.currentText()) # processing mode (batch or single file) self.batchmenulabel = QLabel("mode") self.batchmenu = QComboBox(self) self.batchmenu.addItem("single file") self.batchmenu.addItem("multiple files") self.batchmenu.currentTextChanged.connect(self._model.set_batchmode) self.batchmenu.currentTextChanged.connect(self.toggle_options) # initialize with default value self._model.set_batchmode(self.batchmenu.currentText()) self.toggle_options(self.batchmenu.currentText()) # modality selection self.modmenulabel = QLabel("modality") self.modmenu = QComboBox(self) self.modmenu.addItem("ECG") self.modmenu.addItem("PPG") self.modmenu.addItem("RESP") self.modmenu.currentTextChanged.connect(self._model.set_modality) self.modmenu.currentTextChanged.connect(self.toggle_options) # initialize with default value self._model.set_modality(self.modmenu.currentText()) self.toggle_options(self.modmenu.currentText()) # segment selection; this widget can be openend / set visible from # the menu and closed from within itself (see mapping of segmentermap); # it provides utilities to select a segment from the signal self.segmentermap = QSignalMapper(self) self.segmenter = QDockWidget("select a segment", self) # disable closing such that widget can only be closed by confirming # selection or custom button self.segmenter.setFeatures(QDockWidget.NoDockWidgetFeatures) # Limit number of decimals to four. regex = QRegExp("[0-9]*\.?[0-9]{4}") validator = QRegExpValidator(regex) self.startlabel = QLabel("start") self.startedit = QLineEdit() self.startedit.setValidator(validator) self.endlabel = QLabel("end") self.endedit = QLineEdit() self.endedit.setValidator(validator) segmentfromcursor = QAction(QIcon(":/mouse_icon.png"), "select with mouse", self) segmentfromcursor.triggered.connect(self.enable_segmentedit) self.startedit.addAction(segmentfromcursor, QLineEdit.TrailingPosition) self.endedit.addAction(segmentfromcursor, QLineEdit.TrailingPosition) self.previewedit = QPushButton("preview segment") lambdafn = lambda: self._model.set_segment( [self.startedit.text(), self.endedit.text()]) self.previewedit.clicked.connect(lambdafn) self.confirmedit = QPushButton("confirm segment") self.confirmedit.clicked.connect(self._controller.segment_signal) self.confirmedit.clicked.connect(self.segmentermap.map) self.segmentermap.setMapping(self.confirmedit, 0) self.abortedit = QPushButton("abort segmentation") self.abortedit.clicked.connect(self.segmentermap.map) # reset the segment to None self.segmentermap.setMapping(self.abortedit, 2) self.segmenterlayout = QFormLayout() self.segmenterlayout.addRow(self.startlabel, self.startedit) self.segmenterlayout.addRow(self.endlabel, self.endedit) self.segmenterlayout.addRow(self.previewedit) self.segmenterlayout.addRow(self.confirmedit) self.segmenterlayout.addRow(self.abortedit) self.segmenterwidget = QWidget() self.segmenterwidget.setLayout(self.segmenterlayout) self.segmenter.setWidget(self.segmenterwidget) self.segmenter.setVisible(False) self.segmenter.setAllowedAreas(Qt.RightDockWidgetArea) self.addDockWidget(Qt.RightDockWidgetArea, self.segmenter) # Set up dialog to gather user input for custom files. regex = QRegExp("[1-9][0-9]") validator = QRegExpValidator(regex) self.signallabel = QLabel("biosignal column") self.signaledit = QLineEdit() self.signaledit.setValidator(validator) self.markerlabel = QLabel("marker column") self.markeredit = QLineEdit() self.markeredit.setValidator(validator) regex = QRegExp("[0-9]{2}") validator = QRegExpValidator(regex) self.headerrowslabel = QLabel("number of header rows") self.headerrowsedit = QLineEdit() self.headerrowsedit.setValidator(validator) regex = QRegExp("[0-9]{5}") validator = QRegExpValidator(regex) self.sfreqlabel = QLabel("sampling rate") self.sfreqedit = QLineEdit() self.sfreqedit.setValidator(validator) self.separatorlabel = QLabel("column separator") self.separatormenu = QComboBox(self) self.separatormenu.addItem("comma") self.separatormenu.addItem("tab") self.separatormenu.addItem("colon") self.separatormenu.addItem("space") self.continuecustomfile = QPushButton("continue loading file") self.continuecustomfile.clicked.connect(self.set_customheader) self.customfiledialog = QDialog() self.customfiledialog.setWindowTitle("custom file info") self.customfiledialog.setWindowIcon(QIcon(":/file_icon.png")) self.customfiledialog.setWindowFlags( Qt.WindowCloseButtonHint ) # remove help button by only setting close button self.customfilelayout = QFormLayout() self.customfilelayout.addRow(self.signallabel, self.signaledit) self.customfilelayout.addRow(self.markerlabel, self.markeredit) self.customfilelayout.addRow(self.separatorlabel, self.separatormenu) self.customfilelayout.addRow(self.headerrowslabel, self.headerrowsedit) self.customfilelayout.addRow(self.sfreqlabel, self.sfreqedit) self.customfilelayout.addRow(self.continuecustomfile) self.customfiledialog.setLayout(self.customfilelayout) # set up menubar menubar = self.menuBar() # signal menu signalmenu = menubar.addMenu("biosignal") openSignal = signalmenu.addMenu("load") openEDF = QAction("EDF", self) openEDF.triggered.connect(lambda: self._model.set_filetype("EDF")) openEDF.triggered.connect(self._controller.get_fpaths) openSignal.addAction(openEDF) openOpenSignals = QAction("OpenSignals", self) openOpenSignals.triggered.connect( lambda: self._model.set_filetype("OpenSignals")) openOpenSignals.triggered.connect(self._controller.get_fpaths) openSignal.addAction(openOpenSignals) openCustom = QAction("Custom", self) openCustom.triggered.connect( lambda: self._model.set_filetype("Custom")) openCustom.triggered.connect(lambda: self.customfiledialog.exec_()) openSignal.addAction(openCustom) segmentSignal = QAction("select segment", self) segmentSignal.triggered.connect(self.segmentermap.map) self.segmentermap.setMapping(segmentSignal, 1) signalmenu.addAction(segmentSignal) self.segmentermap.mapped.connect(self.toggle_segmenter) saveSignal = QAction("save", self) saveSignal.triggered.connect(self._controller.get_wpathsignal) signalmenu.addAction(saveSignal) # peak menu peakmenu = menubar.addMenu("peaks") findPeaks = QAction("find", self) findPeaks.triggered.connect(self._controller.find_peaks) peakmenu.addAction(findPeaks) autocorrectPeaks = QAction("autocorrect", self) autocorrectPeaks.triggered.connect(self._controller.autocorrect_peaks) peakmenu.addAction(autocorrectPeaks) savePeaks = QAction("save", self) savePeaks.triggered.connect(self._controller.get_wpathpeaks) peakmenu.addAction(savePeaks) loadPeaks = QAction("load", self) loadPeaks.triggered.connect(self._controller.get_rpathpeaks) peakmenu.addAction(loadPeaks) # stats menu statsmenu = menubar.addMenu("statistics") calculateStats = QAction("calculate", self) calculateStats.triggered.connect(self._controller.calculate_stats) statsmenu.addAction(calculateStats) saveStats = QAction("save", self) saveStats.triggered.connect(self._controller.get_wpathstats) statsmenu.addAction(saveStats) # set up status bar to display error messages and current file path self.statusBar = QStatusBar() self.setStatusBar(self.statusBar) self.progressBar = QProgressBar(self) self.progressBar.setRange(0, 1) self.statusBar.addPermanentWidget(self.progressBar) self.currentFile = QLabel() self.statusBar.addPermanentWidget(self.currentFile) # set up the central widget containing the plot and navigationtoolbar self.centwidget = QWidget() self.setCentralWidget(self.centwidget) # connect canvas0 to keyboard and mouse input for peak editing; # only widgets (e.g. canvas) that currently have focus capture # keyboard input: "You must enable keyboard focus for a widget if # it processes keyboard events." self.canvas0.setFocusPolicy(Qt.ClickFocus) self.canvas0.setFocus() self.canvas0.mpl_connect("key_press_event", self._controller.edit_peaks) self.canvas0.mpl_connect("button_press_event", self.get_xcursor) # arrange the three figure canvases in splitter object self.splitter = QSplitter(Qt.Vertical) # setting opaque resizing to false is important, since resizing gets # very slow otherwise once axes are populated self.splitter.setOpaqueResize(False) self.splitter.addWidget(self.canvas0) self.splitter.addWidget(self.canvas1) self.splitter.addWidget(self.canvas2) self.splitter.setChildrenCollapsible(False) # define GUI layout self.vlayout0 = QVBoxLayout(self.centwidget) self.vlayout1 = QVBoxLayout() self.vlayoutA = QFormLayout() self.vlayoutB = QFormLayout() self.vlayoutC = QVBoxLayout() self.vlayoutD = QVBoxLayout() self.hlayout0 = QHBoxLayout() self.optionsgroupA = QGroupBox("processing options") self.vlayoutA.addRow(self.modmenulabel, self.modmenu) self.vlayoutA.addRow(self.batchmenulabel, self.batchmenu) self.optionsgroupA.setLayout(self.vlayoutA) self.optionsgroupB = QGroupBox("channels") self.vlayoutB.addRow(self.sigchanmenulabel, self.sigchanmenu) self.vlayoutB.addRow(self.markerchanmenulabel, self.markerchanmenu) self.optionsgroupB.setLayout(self.vlayoutB) self.optionsgroupC = QGroupBox("peaks") self.vlayoutC.addWidget(self.editcheckbox) self.vlayoutC.addWidget(self.savecheckbox) self.vlayoutC.addWidget(self.correctcheckbox) self.optionsgroupC.setLayout(self.vlayoutC) self.optionsgroupD = QGroupBox("select statistics for saving") self.vlayoutD.addWidget(self.periodcheckbox) self.vlayoutD.addWidget(self.ratecheckbox) self.vlayoutD.addWidget(self.tidalampcheckbox) self.optionsgroupD.setLayout(self.vlayoutD) self.vlayout1.addWidget(self.optionsgroupA) self.vlayout1.addWidget(self.optionsgroupB) self.vlayout1.addWidget(self.optionsgroupC) self.vlayout1.addWidget(self.optionsgroupD) self.optionsgroupwidget = QWidget() self.optionsgroupwidget.setLayout(self.vlayout1) self.optionsgroup = QDockWidget("configurations", self) self.optionsgroup.setAllowedAreas(Qt.LeftDockWidgetArea) self.toggleoptionsgroup = self.optionsgroup.toggleViewAction() self.toggleoptionsgroup.setText("show/hide configurations") menubar.addAction(self.toggleoptionsgroup) self.optionsgroup.setWidget(self.optionsgroupwidget) self.addDockWidget(Qt.LeftDockWidgetArea, self.optionsgroup) self.vlayout0.addWidget(self.splitter) self.hlayout0.addWidget(self.navitools) self.vlayout0.addLayout(self.hlayout0) ############################################## # connect output widgets to external signals # ############################################## self._model.signal_changed.connect(self.plot_signal) self._model.marker_changed.connect(self.plot_marker) self._model.peaks_changed.connect(self.plot_peaks) self._model.period_changed.connect(self.plot_period) self._model.rate_changed.connect(self.plot_rate) self._model.tidalamp_changed.connect(self.plot_tidalamp) self._model.path_changed.connect(self.display_path) self._model.segment_changed.connect(self.plot_segment) self._model.status_changed.connect(self.display_status) self._model.progress_changed.connect(self.display_progress) self._model.model_reset.connect(self.reset_plot) ########### # methods # ########### def plot_signal(self, value): self.ax00.clear() self.ax00.relim() # reset navitools history self.navitools.update() self.line00 = self.ax00.plot(self._model.sec, value, zorder=1) self.ax00.set_xlabel("seconds", fontsize="large", fontweight="heavy") self.canvas0.draw() # print("plot_signal listening") # print(self.ax0.collections, self.ax0.patches, self.ax0.artists) def plot_peaks(self, value): # self.scat is listed in ax.collections if self.ax00.collections: self.ax00.collections[0].remove() self.scat = self.ax00.scatter(self._model.sec[value], self._model.signal[value], c="m", zorder=2) self.canvas0.draw() # print("plot_peaks listening") # print(self.ax0.collections, self.ax0.patches, self.ax0.artists) def plot_segment(self, value): # If an invalid signal has been selected reset the segmenter interface. if value is None: self.toggle_segmenter(1) return if self.ax00.patches: # self.segementspan is listed in ax.patches self.ax00.patches[0].remove() self.segmentspan = self.ax00.axvspan(value[0], value[1], color="m", alpha=0.25) self.canvas0.draw() self.confirmedit.setEnabled(True) # print(self.ax0.collections, self.ax0.patches, self.ax0.artists) def plot_marker(self, value): self.ax10.clear() self.ax10.relim() self.line10 = self.ax10.plot(value[0], value[1]) self.canvas1.draw() # print("plot_marker listening") def plot_period(self, value): self.ax20.clear() self.ax20.relim() self.navitools.home() if self._model.savestats["period"]: self.line20 = self.ax20.plot(self._model.sec, value, c="m") else: self.line20 = self.ax20.plot(self._model.sec, value) self.ax20.set_ylim(bottom=min(value), top=max(value)) self.ax20.set_title("period", pad=0, fontweight="heavy") self.ax20.grid(True, axis="y") self.navitools.update() self.canvas2.draw() # print("plot_period listening") def plot_rate(self, value): self.ax21.clear() self.ax21.relim() self.navitools.home() if self._model.savestats["rate"]: self.line21 = self.ax21.plot(self._model.sec, value, c="m") else: self.line21 = self.ax21.plot(self._model.sec, value) self.ax21.set_ylim(bottom=min(value), top=max(value)) self.ax21.set_title("rate", pad=0, fontweight="heavy") self.ax21.grid(True, axis="y") self.navitools.update() self.canvas2.draw() # print("plot_rate listening") def plot_tidalamp(self, value): self.ax22.clear() self.ax22.relim() self.navitools.home() if self._model.savestats["tidalamp"]: self.line22 = self.ax22.plot(self._model.sec, value, c="m") else: self.line22 = self.ax22.plot(self._model.sec, value) self.ax22.set_ylim(bottom=min(value), top=max(value)) self.ax22.set_title("amplitude", pad=0, fontweight="heavy") self.ax22.grid(True, axis="y") self.navitools.update() self.canvas2.draw() # print("plot_tidalamp listening") def display_path(self, value): self.currentFile.setText(value) def display_status(self, status): # display status until new status is set self.statusBar.showMessage(status) def display_progress(self, value): # if value is 0, the progressbar indicates a busy state self.progressBar.setRange(0, value) def toggle_segmenter(self, value): if not self._model.loaded: return # Open segmenter when called from signalmenu or clear segmenter # upon selection of invalid segment. if value == 1: self.segmenter.setVisible(True) self.confirmedit.setEnabled(False) self.startedit.clear() self.endedit.clear() if self.ax00.patches: self.ax00.patches[0].remove() self.canvas0.draw() # Close segmenter after segment has been confirmed. elif value == 0: self.segmenter.setVisible(False) if self.ax00.patches: self.ax00.patches[0].remove() self.canvas0.draw() # Close segmenter after segmentation has been aborted (reset # segment). elif value == 2: self._model.set_segment([0, 0]) # This will reset the model to None self.segmenter.setVisible(False) if self.ax00.patches: self.ax00.patches[0].remove() self.canvas0.draw() def enable_segmentedit(self): # disable peak editing to avoid interference self.editcheckbox.setChecked(False) if self.startedit.hasFocus(): self.segmentcursor = "start" elif self.endedit.hasFocus(): self.segmentcursor = "end" def set_customheader(self): """Populate the customheader with inputs from the customfiledialog""" # Check if one of the mandatory fields is missing. mandatoryfields = self.signaledit.text() and self.headerrowsedit.text( ) and self.sfreqedit.text() if not mandatoryfields: self._model.status = ( "Please provide values for 'biosignal column'" ", 'number of header rows' and 'sampling" " rate'.") return seps = {"comma": ",", "tab": "\t", "colon": ":", "space": " "} self._model.customheader = dict.fromkeys( self._model.customheader, None ) # reset header here since it cannot be reset in controller.get_fpaths() self._model.customheader["signalidx"] = int(self.signaledit.text()) self._model.customheader["skiprows"] = int(self.headerrowsedit.text()) self._model.customheader["sfreq"] = int(self.sfreqedit.text()) self._model.customheader["separator"] = seps[ self.separatormenu.currentText()] if self.markeredit.text(): # not mandatory self._model.customheader["markeridx"] = int(self.markeredit.text()) self.customfiledialog.done(QDialog.Accepted) # close the dialog window self._controller.get_fpaths() # move on to file selection def get_xcursor(self, event): # event.button 1 corresponds to left mouse button if event.button != 1: return # limit number of decimal places to two if self.segmentcursor == "start": self.startedit.selectAll() self.startedit.insert("{:.2f}".format(event.xdata)) elif self.segmentcursor == "end": self.endedit.selectAll() self.endedit.insert("{:.2f}".format(event.xdata)) # disable segment cursor again after value has been set self.segmentcursor = False def select_stats(self, event): """ select or deselect statistics to be saved; toggle boolean with xor operator ^=, toggle color with dictionary """ self._model.savestats[event] ^= True line = None if event == "period": if self.line20: line = self.line20[0] elif event == "rate": if self.line21: line = self.line21[0] elif event == "tidalamp": if self.line22: line = self.line22[0] if line: line.set_color(self.togglecolors[line.get_color()]) self.canvas2.draw() def toggle_options(self, event): if event in ["ECG", "PPG"]: self.tidalampcheckbox.setEnabled(False) self.tidalampcheckbox.setChecked(False) self.ax22.set_visible(False) self.canvas2.draw() elif event == "RESP": self.tidalampcheckbox.setEnabled(True) self.ax22.set_visible(True) self.canvas2.draw() elif event == "multiple files": self.editcheckbox.setEnabled(False) self.editcheckbox.setChecked(False) self.savecheckbox.setEnabled(True) self.correctcheckbox.setEnabled(True) self.markerchanmenu.setEnabled(False) elif event == "single file": self.editcheckbox.setEnabled(True) self.markerchanmenu.setEnabled(True) self.savecheckbox.setEnabled(False) self.savecheckbox.setChecked(False) self.correctcheckbox.setEnabled(False) self.correctcheckbox.setChecked(False) def reset_plot(self): self.ax00.clear() self.ax00.relim() self.line00 = None self.scat = None self.segmentspan = None self.ax10.clear() self.ax10.relim() self.line10 = None self.ax20.clear() self.ax20.relim() self.line20 = None self.ax21.clear() self.ax21.relim() self.line21 = None self.ax22.clear() self.ax22.relim() self.line22 = None self.canvas0.draw() self.canvas1.draw() self.canvas2.draw() self.navitools.update() self.currentFile.clear()
class BibDialog(QDialog): def __init__(self, text=''): super().__init__(GlobalAccess().get_main_window()) self.text = text self.person = None self.tmp_person = None def exec_(self): self.init_ui() return super().exec_() def init_ui(self): self.setWindowTitle(_('Bib or Name')) self.setWindowIcon(QIcon(config.ICON)) self.setSizeGripEnabled(False) self.setModal(True) self.layout = QFormLayout(self) if self.text: self.label_text = QLabel(self.text) self.layout.addRow(self.label_text) self.label_bib_or_name = QLabel(_('Bib or Name')) self.item_bib_or_name = QLineEdit() self.item_bib_or_name.selectAll() self.item_bib_or_name.textChanged.connect(self.show_person_info) self.layout.addRow(self.label_bib_or_name, self.item_bib_or_name) self.label_person_info = QLabel('') self.layout.addRow(self.label_person_info) def cancel_changes(): self.close() def apply_changes(): try: self.apply_changes_impl() except Exception as e: logging.error(str(e)) self.close() button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.button_ok = button_box.button(QDialogButtonBox.Ok) self.button_ok.setText(_('OK')) self.button_ok.clicked.connect(apply_changes) self.button_cancel = button_box.button(QDialogButtonBox.Cancel) self.button_cancel.setText(_('Cancel')) self.button_cancel.clicked.connect(cancel_changes) self.layout.addRow(button_box) self.show() def show_person_info(self): bib_or_name = self.item_bib_or_name.text() # type: str self.label_person_info.setText('') person = None if bib_or_name: if bib_or_name.isdigit(): person = memory.find(memory.race().persons, bib=int(bib_or_name)) else: for p in memory.race().persons: if bib_or_name.lower() in p.full_name.lower(): person = p break if person: info = person.full_name if person.group: info = '{}\n{}: {}'.format(info, _('Group'), person.group.name) if person.card_number: info = '{}\n{}: {}'.format(info, _('Card'), person.card_number) self.label_person_info.setText(info) else: self.label_person_info.setText(_('not found')) self.tmp_person = person def get_person(self): return self.person def found_person(self): return self.person is not None def apply_changes_impl(self): self.person = self.tmp_person
class MyWindow(QWidget): def __init__(self): super().__init__() vbox = QVBoxLayout() hbox = QHBoxLayout() #-- QLineEdit widget to search for an image self.my_lineedit = QLineEdit("Enter Key Words To Search Images..") self.my_lineedit.setMinimumWidth(250) self.my_lineedit.selectAll() #---QComboBox to allo allow user to select image manipulations self.my_manipulation = [ "Pick a manupulation", "Sepia", "Negative", "Grayscale", "Thumbnail", "None" ] self.my_combo_box = QComboBox() self.my_combo_box.addItems(self.my_manipulation) self.my_combo_box.currentIndexChanged.connect(self.update_ui) #---button to display images self.my_btn = QPushButton("Search or press Return") self.my_btn.clicked.connect(self.on_submit) self.my_lineedit.returnPressed.connect(self.on_submit) #--- picture label self.image_label = QLabel() #---adding widgets to the box layout hbox.addWidget(self.my_combo_box) vbox.addWidget(self.my_lineedit) vbox.addWidget(self.my_btn) vbox.addWidget(self.image_label) #---outer layer main_layout = QHBoxLayout() #add previous inner layouts main_layout.addLayout(vbox) main_layout.addLayout(hbox) #---set outer layout as a main layout of the widget self.setLayout(main_layout) #---window title and background color self.setWindowTitle("Pixabay Search Engine") p = self.palette() p.setColor(self.backgroundRole(), Qt.darkCyan) self.setPalette(p) #---when the button is pressed we begin to load our new directory #---search if the input string matches with ouir records #---counts the matches for a better result and sort the matches @Slot() def on_submit(self): your_picture = self.my_lineedit.text().lower() search(your_picture) #---dysplay the best match image to the image widget pixmap = QPixmap('image_name.jpg') pixmap = pixmap.scaled(500, 500, Qt.KeepAspectRatio) self.image_label.setPixmap(pixmap) @Slot() def update_ui(self): #---update the manipuled image by the index chosen my_index = self.my_combo_box.currentIndex() #--call the manipulation function and updates the image pixmap = manipulation(my_index) pixmap = pixmap.scaled(500, 500, Qt.KeepAspectRatio) self.image_label.setPixmap(pixmap)
class ScaleDialogPQ(QDialog): ''' Dialog for obtaining scaling information from the user. Validates that the resulting width and height values are not smaller than the specified minimums. ''' def __init__(self, scale, width, height, minwidth, minheight, autoscale, parent=None): ''' Creates a scaling dialog, with scale as the current scaling value which gives a pixmap of size width and height. The minimum acceptable width and heights are given by minwidth and minheight. Values are assumed to be in units of pixels. The value of autoscale sets the default value of "Scale image to fir window frame". ''' super(ScaleDialogPQ, self).__init__(parent) self.__scale = float(scale) self.__pixwidth = float(width) self.__inchwidth = float(width) / float(self.physicalDpiX()) self.__pixheight = float(height) self.__inchheight = float(height) / float(self.physicalDpiY()) self.__minpixwidth = int(minwidth) self.__minpixheight = int(minheight) self.__autoscale = bool(autoscale) self.FLTSTR_FORMAT = "%#.3f" self.setWindowTitle(self.tr("Image Size Scaling")) # auto-scaling option at the top autoscalelabel = QLabel(self.tr("Scale image to fit window frame?"), self) autoscalelabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.__autoyesbtn = QRadioButton(self.tr("&Yes"), self) self.__autonobtn = QRadioButton(self.tr("&No"), self) autoscalebtngrp = QButtonGroup(self) autoscalebtngrp.addButton(self.__autoyesbtn) autoscalebtngrp.addButton(self.__autonobtn) # put the manual scaling settings into their own box self.__grpbox = QGroupBox(self.tr("Fixed scaling"), self) # create the widgets going inside this group box messagelabel = QLabel( self.tr("Scaling factor (both horiz. and vert.) for the image"), self.__grpbox) messagelabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) scalelabel = QLabel(self.tr("&Scale: "), self.__grpbox) self.__scaleedit = QLineEdit(self.FLTSTR_FORMAT % self.__scale, self.__grpbox) scalelabel.setBuddy(self.__scaleedit) widthbegin = QLabel(self.tr("Width: "), self.__grpbox) self.__pixwidthlabel = QLabel(str(int(self.__pixwidth + 0.5)), self.__grpbox) widthmiddle = QLabel(self.tr("pixels, or"), self.__grpbox) self.__inchwidthlabel = QLabel(self.FLTSTR_FORMAT % self.__inchwidth, self.__grpbox) widthend = QLabel(self.tr("inches on the screen"), self.__grpbox) minwidthlabel = QLabel(self.tr("(must not be less than %d pixels)" % \ self.__minpixwidth), self.__grpbox) heightbegin = QLabel(self.tr("Height:"), self.__grpbox) self.__pixheightlabel = QLabel(str(int(self.__pixheight + 0.5)), self.__grpbox) heightmiddle = QLabel(self.tr("pixels, or"), self.__grpbox) self.__inchheightlabel = QLabel(self.FLTSTR_FORMAT % self.__inchheight, self.__grpbox) heightend = QLabel(self.tr("inches on the screen"), self.__grpbox) minheightlabel = QLabel(self.tr("(must not be less than %d pixels)" % \ self.__minpixheight), self.__grpbox) # layout the widgets in this group box layout = QGridLayout() layout.addWidget(messagelabel, 0, 0, 1, 5) layout.addWidget(scalelabel, 1, 0, 1, 1) layout.addWidget(self.__scaleedit, 1, 1, 1, 4) layout.addWidget(widthbegin, 2, 0, 1, 1) layout.addWidget(self.__pixwidthlabel, 2, 1, 1, 1) layout.addWidget(widthmiddle, 2, 2, 1, 1) layout.addWidget(self.__inchwidthlabel, 2, 3, 1, 1) layout.addWidget(widthend, 2, 4, 1, 1) layout.addWidget(minwidthlabel, 3, 1, 1, 4) layout.addWidget(heightbegin, 4, 0, 1, 1) layout.addWidget(self.__pixheightlabel, 4, 1, 1, 1) layout.addWidget(heightmiddle, 4, 2, 1, 1) layout.addWidget(self.__inchheightlabel, 4, 3, 1, 1) layout.addWidget(heightend, 4, 4, 1, 1) layout.addWidget(minheightlabel, 5, 1, 1, 4) # assign this layout to the group box self.__grpbox.setLayout(layout) # layout the widgets in the dialog (outside the group box) layout = QGridLayout() layout.addWidget(autoscalelabel, 0, 0, 1, 1) layout.addWidget(self.__autoyesbtn, 0, 1, 1, 1) layout.addWidget(self.__autonobtn, 0, 2, 1, 1) layout.addWidget(self.__grpbox, 1, 0, 1, 3) buttonbox = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel | QDialogButtonBox.Reset, Qt.Horizontal, self) layout.addWidget(buttonbox, 2, 0, 1, 3) self.setLayout(layout) # The OK button is not the default here in Qt4.2 okbutton = buttonbox.button(QDialogButtonBox.Ok) okbutton.setDefault(True) resetbutton = buttonbox.button(QDialogButtonBox.Reset) self.__autoyesclicked = self.__autoyesbtn.clicked self.__autoyesclicked.connect(self.setAutoScale) self.__autonoclicked = self.__autonobtn.clicked self.__autonoclicked.connect(self.unsetAutoScale) self.__scaletextchanged = self.__scaleedit.textChanged self.__scaletextchanged.connect(self.updateValues) self.__buttonboxaccepted = buttonbox.accepted self.__buttonboxaccepted.connect(self.checkValues) self.__buttonboxrejected = buttonbox.rejected self.__buttonboxrejected.connect(self.reject) self.__resetbuttonclicked = resetbutton.clicked self.__resetbuttonclicked.connect(self.resetValues) # initialize the state from autoscale if self.__autoscale: self.__autoyesbtn.setChecked(True) self.setAutoScale(True) else: self.__autonobtn.setChecked(True) self.unsetAutoScale(True) def setAutoScale(self, checked): if checked: self.__grpbox.setEnabled(False) def unsetAutoScale(self, checked): if checked: self.__grpbox.setEnabled(True) self.__scaleedit.setFocus() self.__scaleedit.selectAll() def updateValues(self, newstring): try: newscale = float(newstring) if (newscale < 0.0001) or (newscale > 10000.0): raise OverflowError() newval = self.__pixwidth * newscale / self.__scale self.__pixwidthlabel.setText(str(int(newval + 0.5))) newval = self.__inchwidth * newscale / self.__scale self.__inchwidthlabel.setText(self.FLTSTR_FORMAT % newval) newval = self.__pixheight * newscale / self.__scale self.__pixheightlabel.setText(str(int(newval + 0.5))) newval = self.__inchheight * newscale / self.__scale self.__inchheightlabel.setText(self.FLTSTR_FORMAT % newval) except Exception: pass def checkValues(self): okay = self.getValues()[2] if okay: self.accept() else: QMessageBox.warning(self, self.tr("Invalid value"), self.tr("Scale value is not valid")) def getValues(self): if self.__autoyesbtn.isChecked(): return (0.0, True, True) try: newscale = float(self.__scaleedit.text()) if (newscale < 0.0001) or (newscale > 10000.0): raise OverflowError() newwidth = self.__pixwidth * newscale / self.__scale newwidth = int(newwidth + 0.5) newheight = self.__pixheight * newscale / self.__scale newheight = int(newheight + 0.5) if (newwidth < self.__minpixwidth) or (newheight < self.__minpixheight): raise OverflowError() except Exception: return (0.0, False, False) return (newscale, False, True) def resetValues(self): self.__scaleedit.setText(self.FLTSTR_FORMAT % self.__scale) self.__pixwidthlabel.setText(str(int(self.__pixwidth + 0.5))) self.__inchwidthlabel.setText(self.FLTSTR_FORMAT % self.__inchwidth) self.__pixheightlabel.setText(str(int(self.__pixheight + 0.5))) self.__inchheightlabel.setText(self.FLTSTR_FORMAT % self.__inchheight) if self.__autoscale: self.__autoyesbtn.setChecked(True) self.setAutoScale(True) else: self.__autonobtn.setChecked(True) self.unsetAutoScale(True)
class PersonEditDialog(QDialog): GROUP_NAME = '' ORGANIZATION_NAME = '' def __init__(self, person, is_new=False): super().__init__(GlobalAccess().get_main_window()) self.is_ok = {} assert (isinstance(person, Person)) self.current_object = person self.is_new = is_new self.time_format = 'hh:mm:ss' time_accuracy = race().get_setting('time_accuracy', 0) if time_accuracy: self.time_format = 'hh:mm:ss.zzz' def exec_(self): self.init_ui() self.set_values_from_model() return super().exec_() def init_ui(self): self.setWindowTitle(_('Entry properties')) self.setWindowIcon(QIcon(config.ICON)) self.setSizeGripEnabled(False) self.setModal(True) self.layout = QFormLayout(self) self.label_surname = QLabel(_('Last name')) self.item_surname = QLineEdit() self.layout.addRow(self.label_surname, self.item_surname) self.label_name = QLabel(_('First name')) self.item_name = AdvComboBox() self.item_name.addItems(get_names()) self.layout.addRow(self.label_name, self.item_name) self.label_group = QLabel(_('Group')) self.item_group = AdvComboBox() self.item_group.addItems(get_race_groups()) self.layout.addRow(self.label_group, self.item_group) self.label_team = QLabel(_('Team')) self.item_team = AdvComboBox() self.item_team.addItems(get_race_teams()) self.layout.addRow(self.label_team, self.item_team) use_birthday = Config().configuration.get('use_birthday', False) if use_birthday: self.label_birthday = QLabel(_('Birthday')) self.item_birthday = QDateEdit() self.item_birthday.setDate(date.today()) self.item_birthday.setMaximumDate(date.today()) self.layout.addRow(self.label_birthday, self.item_birthday) else: self.label_year = QLabel(_('Year of birth')) self.item_year = QSpinBox() self.item_year.setMinimum(0) self.item_year.setMaximum(date.today().year) self.item_year.editingFinished.connect(self.year_change) self.layout.addRow(self.label_year, self.item_year) self.label_qual = QLabel(_('Qualification')) self.item_qual = AdvComboBox() for i in list(Qualification): self.item_qual.addItem(i.get_title()) self.layout.addRow(self.label_qual, self.item_qual) self.is_ok['bib'] = True self.label_bib = QLabel(_('Bib')) self.item_bib = QSpinBox() self.item_bib.setMinimum(0) self.item_bib.setMaximum(Limit.BIB) self.item_bib.valueChanged.connect(self.check_bib) self.layout.addRow(self.label_bib, self.item_bib) self.label_bib_info = QLabel('') self.layout.addRow(QLabel(''), self.label_bib_info) self.label_start = QLabel(_('Start time')) self.item_start = QTimeEdit() self.item_start.setDisplayFormat(self.time_format) self.layout.addRow(self.label_start, self.item_start) self.label_start_group = QLabel(_('Start group')) self.item_start_group = QSpinBox() self.item_start_group.setMinimum(0) self.item_start_group.setMaximum(99) self.layout.addRow(self.label_start_group, self.item_start_group) self.is_ok['card'] = True self.label_card = QLabel(_('Punch card #')) self.item_card = QSpinBox() self.item_card.setMinimum(0) self.item_card.setMaximum(9999999) self.item_card.valueChanged.connect(self.check_card) self.layout.addRow(self.label_card, self.item_card) self.label_card_info = QLabel('') self.layout.addRow(QLabel(''), self.label_card_info) self.item_rented = QCheckBox(_('rented card')) self.item_paid = QCheckBox(_('is paid')) self.item_out_of_competition = QCheckBox(_('out of competition')) self.item_personal = QCheckBox(_('personal participation')) self.layout.addRow(self.item_rented, self.item_out_of_competition) self.layout.addRow(self.item_paid, self.item_personal) self.label_comment = QLabel(_('Comment')) self.item_comment = QTextEdit() self.item_comment.setTabChangesFocus(True) self.layout.addRow(self.label_comment, self.item_comment) def cancel_changes(): self.close() def apply_changes(): try: self.apply_changes_impl() except Exception as e: logging.error(str(e)) self.close() button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.button_ok = button_box.button(QDialogButtonBox.Ok) self.button_ok.setText(_('OK')) self.button_ok.clicked.connect(apply_changes) self.button_cancel = button_box.button(QDialogButtonBox.Cancel) self.button_cancel.setText(_('Cancel')) self.button_cancel.clicked.connect(cancel_changes) self.layout.addRow(button_box) self.show() def year_change(self): """ Convert 2 digits of year to 4 2 -> 2002 11 - > 2011 33 -> 1933 56 -> 1956 98 - > 1998 0 -> 0 exception! """ widget = self.sender() assert isinstance(widget, QSpinBox) year = widget.value() if 0 < year < 100: cur_year = date.today().year new_year = cur_year - cur_year % 100 + year if new_year > cur_year: new_year -= 100 widget.setValue(new_year) def items_ok(self): ret = True for item_name in self.is_ok.keys(): if self.is_ok[item_name] is not True: ret = False break return ret def check_bib(self): bib = self.item_bib.value() self.label_bib_info.setText('') if bib: person = find(race().persons, bib=bib) if person: if person.bib == self.current_object.bib: self.button_ok.setEnabled(True) return self.button_ok.setDisabled(True) self.is_ok['bib'] = False info = '{}\n{}'.format(_('Number already exists'), person.full_name) if person.group: info = '{}\n{}: {}'.format(info, _('Group'), person.group.name) self.label_bib_info.setText(info) else: self.label_bib_info.setText(_('Number is unique')) self.is_ok['bib'] = True if self.items_ok(): self.button_ok.setEnabled(True) else: self.button_ok.setEnabled(True) def check_card(self): number = self.item_card.value() self.label_card_info.setText('') if number: person = None for _p in race().persons: if _p.card_number and _p.card_number == number: person = _p break if person: if person.card_number == self.current_object.card_number: self.button_ok.setEnabled(True) return self.button_ok.setDisabled(True) self.is_ok['card'] = False info = '{}\n{}'.format(_('Card number already exists'), person.full_name) if person.group: info = '{}\n{}: {}'.format(info, _('Group'), person.group.name) if person.bib: info = '{}\n{}: {}'.format(info, _('Bib'), person.bib) self.label_card_info.setText(info) else: self.label_card_info.setText(_('Card number is unique')) self.is_ok['card'] = True if self.items_ok(): self.button_ok.setEnabled(True) else: self.button_ok.setEnabled(True) def set_values_from_model(self): self.item_surname.setText(self.current_object.surname) self.item_surname.selectAll() self.item_name.setCurrentText(self.current_object.name) if self.current_object.group is not None: self.item_group.setCurrentText(self.current_object.group.name) else: self.item_group.setCurrentText(self.GROUP_NAME) if self.current_object.organization is not None: self.item_team.setCurrentText( self.current_object.organization.name) else: self.item_team.setCurrentText(self.ORGANIZATION_NAME) if self.current_object.qual: self.item_qual.setCurrentText(self.current_object.qual.get_title()) if self.current_object.bib: self.item_bib.setValue(int(self.current_object.bib)) if self.current_object.start_time is not None: time = time_to_qtime(self.current_object.start_time) self.item_start.setTime(time) if self.current_object.start_group is not None: self.item_start_group.setValue(int( self.current_object.start_group)) if self.current_object.card_number: self.item_card.setValue(self.current_object.card_number) self.item_out_of_competition.setChecked( self.current_object.is_out_of_competition) self.item_paid.setChecked(self.current_object.is_paid) self.item_paid.setChecked(self.current_object.is_paid) self.item_personal.setChecked(self.current_object.is_personal) self.item_rented.setChecked(self.current_object.is_rented_card) self.item_comment.setText(self.current_object.comment) use_birthday = Config().configuration.get('use_birthday', False) if use_birthday: if self.current_object.birth_date: self.item_birthday.setDate(self.current_object.birth_date) else: if self.current_object.get_year(): self.item_year.setValue(self.current_object.get_year()) def apply_changes_impl(self): person = self.current_object assert (isinstance(person, Person)) if self.is_new: race().persons.insert(0, person) if person.name != self.item_name.currentText(): person.name = self.item_name.currentText() if person.surname != self.item_surname.text(): person.surname = self.item_surname.text() if (person.group is not None and person.group.name != self.item_group.currentText()) or\ (person.group is None and len(self.item_group.currentText()) > 0): person.group = find(race().groups, name=self.item_group.currentText()) if (person.organization is not None and person.organization.name != self.item_team.currentText()) or \ (person.organization is None and len(self.item_team.currentText()) > 0): organization = find(race().organizations, name=self.item_team.currentText()) if organization is None: organization = Organization() organization.name = self.item_team.currentText() race().organizations.append(organization) Teamwork().send(organization.to_dict()) person.organization = organization if person.qual.get_title() != self.item_qual.currentText(): person.qual = Qualification.get_qual_by_name( self.item_qual.currentText()) if person.bib != self.item_bib.value(): person.bib = self.item_bib.value() new_time = time_to_otime(self.item_start.time()) if person.start_time != new_time: person.start_time = new_time if person.start_group != self.item_start_group.value( ) and self.item_start_group.value(): person.start_group = self.item_start_group.value() if (not person.card_number or int(person.card_number) != self.item_card.value()) \ and self.item_card.value: race().person_card_number(person, self.item_card.value()) if person.is_out_of_competition != self.item_out_of_competition.isChecked( ): person.is_out_of_competition = self.item_out_of_competition.isChecked( ) if person.is_paid != self.item_paid.isChecked(): person.is_paid = self.item_paid.isChecked() if person.is_rented_card != self.item_rented.isChecked(): person.is_rented_card = self.item_rented.isChecked() if person.is_personal != self.item_personal.isChecked(): person.is_personal = self.item_personal.isChecked() if person.comment != self.item_comment.toPlainText(): person.comment = self.item_comment.toPlainText() use_birthday = Config().configuration.get('use_birthday', False) if use_birthday: new_birthday = qdate_to_date(self.item_birthday.date()) if person.birth_date != new_birthday and new_birthday: if person.birth_date or new_birthday != date.today(): person.birth_date = new_birthday else: if person.get_year() != self.item_year.value(): person.set_year(self.item_year.value()) ResultCalculation(race()).process_results() Teamwork().send(person.to_dict())