import getpass import inspect import os import time from qtpy.QtCore import Qt from qtpy.QtWidgets import (QComboBox, QDialog, QFileDialog, QHBoxLayout, QLabel, QLineEdit, QPushButton, QTextEdit, QVBoxLayout) import elog # https://github.com/paulscherrerinstitute/py_elog from pyqtacc.bdbase.enumkind import MsgSeverity from pyqtacc.bdbase.sendelogframe import QSendToELOGFrame _version = "1.0.0" _pymodule = os.path.basename(__file__) _appname, _appext = _pymodule.split(".") def _line(): """Macro to return the current line number. The current line number within the file is used when reporting messages to the message logging window. Returns: int: Current line number. """ return inspect.currentframe().f_back.f_lineno class QSendToELOG(QSendToELOGFrame): """ Graphical interface to elog """ def __init__(self, parent, logbook=None, categoryIdx=0, domainIdx=0, systemIdx=0, sectionIdx=0, eintragIdx=0, statusIdx=0, title=None, message=None, attachFile=None, destination_folder=None): super().__init__(parent, logbook=logbook, title=title, message=message) self.settings = self.parent.settings #First check what is the logbook being requested? #find layout items self.layout_items = self.get_logbook_specific_items(self.logbook) self.layout_to_widget_dict = {} self.category_idx = categoryIdx self.domain_idx = domainIdx self.system_idx = systemIdx self.section_idx = sectionIdx self.eintrag_idx = eintragIdx self.status_idx = statusIdx #print("indices", self.category_idx, self.domain_idx, self.system_idx, # self.section_idx, flush=True) self.category = None self.domain = None self.system = None self.section = None self.eintrag = None self.estatus = None self.effekt = None #self.elog_section = list(self.parent.settings.data["Elog"]['section']) try: self.elog_section = list(self.parent.settings.data['ElogBooks'][ self.logbook]['Optional']['Section']) except KeyError: self.elog_section = None #print("LAYOUT ITEMS: ", self.layout_items, flush=True) self.sim_list = ["Sand", "test"] self.initialize_layout(self.logbook) self.exec() def reset_layout(self): def remove_wgt(wgt): self.layout.removeItem(wgt) while wgt.count(): item = wgt.takeAt(0) widget = item.widget() widget.deleteLater() for layout in self.get_logbook_specific_items(self.logbook): remove_wgt(self.layout_to_widget_dict[layout]) def create_layout_widgets(self): if not self.eintrag: self.eintrag = QHBoxLayout() self.eintrag.addWidget(QLabel('Eintrag: ')) self.eintrag_items = QComboBox() self.eintrag_items.setObjectName("Elog") self.eintrag.addWidget(self.eintrag_items) self.layout_to_widget_dict['Eintrag'] = self.eintrag if not self.category: self.category = QHBoxLayout() self.category.addWidget(QLabel('Category: ')) self.category_items = QComboBox() self.category_items.setObjectName("Elog") self.category.addWidget(self.category_items) self.layout_to_widget_dict['Category'] = self.category if not self.domain: self.domain = QHBoxLayout() self.domain.addWidget(QLabel('Domain: ')) self.domain_items = QComboBox() self.domain_items.setObjectName("Elog") self.domain.addWidget(self.domain_items) self.domain_items.currentIndexChanged.connect(self.on_domain_change) self.layout_to_widget_dict['Domain'] = self.domain if not self.system: self.system = QHBoxLayout() self.system.addWidget(QLabel('System: ')) self.system_items = QComboBox() self.system_items.setObjectName("Elog") self.system.addWidget(self.system_items) self.layout_to_widget_dict['System'] = self.system if not self.section: self.section = QHBoxLayout() self.section.addWidget(QLabel('Section: ')) self.section_items = QComboBox() self.section_items.setObjectName("Elog") self.section.addWidget(self.section_items) self.layout_to_widget_dict['Section'] = self.section if not self.effekt: self.effekt = QHBoxLayout() self.effekt.addWidget(QLabel('Effect: ')) self.effekt_le = QLineEdit() self.effekt_le.setObjectName('Elog') self.effekt_le.setText(str("")) self.effekt_le.setFixedWidth(300) self.effekt_le.setAlignment(Qt.AlignCenter) self.effekt.addWidget(self.effekt_le) self.layout_to_widget_dict['Effect'] = self.effekt if not self.estatus: self.estatus = QHBoxLayout() self.estatus.addWidget(QLabel('Status: ')) self.estatus_items = QComboBox() self.estatus_items.setObjectName("Elog") self.estatus.addWidget(self.estatus_items) self.layout_to_widget_dict['Status'] = self.estatus def initialize_layout(self, logbook): #Decide on layout self.create_layout_widgets() item_no = 2 if 'Eintrag' in self.layout_items: self.eintrag_idx = self.settings.data['ElogInit'][logbook][ 'Eintrag'] self.eintrag_items.clear() self.eintrag_items.addItems(list(self.parent.settings.data[ 'ElogBooks'][logbook]['Required']['Eintrag'])) self.eintrag_items.setCurrentIndex(self.eintrag_idx) self.layout.insertLayout(item_no, self.eintrag) if 'Category' in self.layout_items: try: self.category_idx = self.settings.data['ElogInit'][logbook][ 'Category'] except KeyError as ex: print("Did not find Category Index" + str(ex)) self.category_items.clear() key = 'Required' if 'Required' in self.parent.settings.data[ "ElogBooks"][logbook] else 'Optional' self.category_items.addItems(list(self.parent.settings.data[ "ElogBooks"][logbook][key]['Category'])) self.category_items.setCurrentIndex(self.category_idx) self.layout.insertLayout(item_no, self.category) if 'Effect' in self.layout_items: item_no += 1 self.layout.insertLayout(item_no, self.effekt) if 'Domain' in self.layout_items: item_no += 1 self.domain_items.clear() self.domain_items.addItems(list(self.parent.settings.data[ 'ElogBooks'][logbook]['Optional']['Domain'])) self.domain_items.setCurrentIndex(self.domain_idx) self.layout.insertLayout(item_no, self.domain) if 'Section' in self.layout_items: item_no += 1 self.section_items.clear() self.section_items.addItems(list(self.parent.settings.data[ 'ElogBooks'][logbook]['Optional']['Section'][self.domain_idx])) self.section_items.setCurrentIndex(self.section_idx) self.layout.insertLayout(item_no, self.section) if 'System' in self.layout_items: item_no += 1 self.system_idx = self.settings.data['ElogInit'][logbook][ 'System'] self.system_items.clear() self.system_items.addItems(list(self.parent.settings.data[ 'ElogBooks'][logbook]['Optional']['System'])) self.system_items.setCurrentIndex(self.system_idx) self.layout.insertLayout(item_no, self.system) if 'Status' in self.layout_items: item_no += 1 self.estatus_items.clear() self.estatus_items.addItems(list(self.parent.settings.data[ 'ElogBooks'][logbook]['Optional']['Status'])) self.estatus_items.setCurrentIndex(self.status_idx) self.layout.insertLayout(item_no, self.estatus) #IS this required --attached files should be passed on self.attachFile = self.parent.attach_files self.filesE.clear() self.files_text = '' if self.attachFile is not None: _attachFile = [] if isinstance(self.attachFile, str): _attachFile.append(self.attachFile) elif isinstance(self.attachFile, list): _attachFile = self.attachFile for i, file in enumerate(_attachFile): _attach_base = os.path.basename(file) if i > 0: self.files_text += "\n" self.files_text += str(_attach_base) self.filesE.setText(self.files_text) self.fflag = True if any(substring.upper() in logbook.upper() \ for substring in self.sim_list): _bgcolor = "QComboBox {background: plum; color : black;}" else: _bgcolor = "QComboBox {background: lightblue; color : black;}" self.elog_items.setStyleSheet(_bgcolor) #have to remove widgets within layout too! #https://riverbankcomputing.com/pipermail/pyqt/2009-November/025214.html def on_elog_change(self, idx): #print(self.elog_items.currentText(), "new=", idx) #print(self.elog_items.itemText(idx)) #print(self.logbook) new_logbook = self.elog_items.itemText(idx) #print("NEW", new_logbook, flush=True) #print("OLD", self.logbook, flush=True) #Meet the new logbook. Same as the old logbook if new_logbook == self.logbook: return try: self.elog_section = list(self.parent.settings.data['ElogBooks'][ new_logbook]['Optional']['Section']) except KeyError: pass self.layout_items = self.get_logbook_specific_items(new_logbook) if any(substring.upper() in new_logbook.upper() \ for substring in self.sim_list): _bgcolor = "QComboBox {background: plum; color : black;}" else: _bgcolor = "QComboBox {background: lightblue; color : black;}" self.reset_layout() self.initialize_layout(new_logbook) self.elog_items.setStyleSheet(_bgcolor) self.logbook = new_logbook def on_domain_change(self, i): self.section_items.clear() self.section_items.addItems(self.elog_section[i]) #self.section_items.setCurrentIndex(0) def ok(self): #proj = self.category_items.currentText() #if not proj: # self.messagelbl.setText('Category attribute is required') # return #self.attributes['Category'] = proj el = self.elog_items.currentText() if 'Eintrag' in self.layout_items: self.attributes['Eintrag'] = self.eintrag_items.currentText() if 'Category' in self.layout_items: self.attributes['Category'] = self.category_items.currentText() if 'Effect' in self.layout_items: self.attributes['Effect'] = self.effekt_le.text() if 'Domain' in self.layout_items: self.attributes['Domain'] = self.domain_items.currentText() if 'System' in self.layout_items: self.attributes['System'] = self.system_items.currentText() if 'Section' in self.layout_items: self.attributes['Section'] = self.section_items.currentText() if 'Status' in self.layout_items: self.attributes['Status'] = self.estatus_items.currentText() QSendToELOGFrame.ok(self)