def server_address_changed(self): """Validate and store a changed 'server address' value""" proposed_new_address: str = self.ui.serverAddress.text() valid = RmNetUtils.valid_server_address(proposed_new_address) if valid: self._preferences.set_server_address(proposed_new_address) SharedUtils.background_validity_color(self.ui.serverAddress, valid)
def dither_max_radius_changed(self): proposed_new_number: str = self.ui.ditherMaxRadius.text() new_number = Validators.valid_float_in_range(proposed_new_number, 0, 12*60*60) valid = new_number is not None if valid: self._preferences.set_dither_max_radius(new_number) SharedUtils.background_validity_color(self.ui.ditherMaxRadius, valid)
def port_number_changed(self): """Validate and store a changed 'server port number' value""" proposed_new_number: str = self.ui.portNumber.text() new_number = Validators.valid_int_in_range(proposed_new_number, 1, 65536) valid = new_number is not None if valid: self._preferences.set_port_number(new_number) SharedUtils.background_validity_color(self.ui.portNumber, valid)
def target_adus_changed(self): """Validate and store a changed 'target ADUs' value""" proposed_new_number: str = self.ui.targetADUs.text() new_number = Validators.valid_float_in_range(proposed_new_number, 1, 500000) valid = new_number is not None if valid: self._preferences.set_target_adus(new_number) SharedUtils.background_validity_color(self.ui.targetADUs, valid)
def number_of_flats_changed(self): """Validate and store a changed 'number of flat frames' value""" proposed_new_number: str = self.ui.numFlats.text() new_number = Validators.valid_int_in_range(proposed_new_number, 0, 256) valid = new_number is not None if valid: self._preferences.set_default_frame_count(new_number) SharedUtils.background_validity_color(self.ui.numFlats, valid)
def adu_tolerance_changed(self): """Validate and store a changed 'ADU tolerance' value""" proposed_new_number: str = self.ui.aduTolerance.text() new_number = Validators.valid_float_in_range(proposed_new_number, 0, 100) valid = new_number is not None if valid: self._preferences.set_adu_tolerance(new_number / 100.0) SharedUtils.background_validity_color(self.ui.aduTolerance, valid)
def source_az_changed(self): """Validate and store light source azimuth""" proposed_new_number: str = self.ui.sourceAz.text() new_number: Optional[float] = Validators.valid_float_in_range(proposed_new_number, -360, +360) valid = new_number is not None if valid: self._preferences.set_source_az(new_number) SharedUtils.background_validity_color(self.ui.sourceAz, valid)
def set_up_ui(self, preferences: Preferences): """Set UI fields in the dialog from the given preferences settings""" self._preferences = preferences self.connect_responders() self.load_ui_from_prefs(preferences) # If a window size is saved, set the window size window_size = self._preferences.get_prefs_window_size() if window_size is not None: self.ui.resize(window_size) # Watch events go by so we can save resize information self.ui.installEventFilter(self) # Set font sizes of all elements using fonts to the saved font size standard_font_size = self._preferences.get_standard_font_size() SharedUtils.set_font_sizes(parent=self.ui, standard_size=standard_font_size, title_prefix=Constants.MAIN_TITLE_LABEL_PREFIX, title_increment=Constants.MAIN_TITLE_FONT_SIZE_INCREMENT, subtitle_prefix=Constants.SUBTITLE_LABEL_PREFIX, subtitle_increment=Constants.SUBTITLE_FONT_SIZE_INCREMENT )
def validate_filter_name_field(self, fs: FilterSpec, can_be_blank=False) -> bool: # print(f"validate_filter_name_field({fs},{can_be_blank})") # First we validate the format of the name, and whether it can be blank name_field_name: str = f"filterName_{fs.get_slot_number()}" this_field: QLineEdit = self.ui.findChild(QLineEdit, name_field_name) assert this_field is not None proposed_name = this_field.text().strip() if len(proposed_name) == 0: name_valid = can_be_blank else: name_valid = FilterSpec.valid_filter_name(proposed_name) # If the name is otherwise valid, we check that it would be unique in the list if name_valid and len(proposed_name) > 0: other_fs: FilterSpec for other_fs in self._preferences.get_filter_spec_list(): if other_fs.get_slot_number() != fs.get_slot_number(): # Only compare to other filters if other_fs.get_name().strip() == proposed_name: # Collision with existing name other than the one we're setting name_valid = False SharedUtils.background_validity_color(this_field, name_valid) # print(f"validate_filter_name_field({fs},{can_be_blank}) returns {name_valid}") return name_valid
def getCardSwipe(self): # Read the card data as a password so it doesn't show on the screen cardID = SharedUtils.sanitizeInput(getpass.getpass("\nWaiting for card swipe...")) try: # Return the card ID return self.regex.search(cardID).group(1) except AttributeError: # If exit or back, just return to go back if "exit" in cardID or "back" in cardID: return c.BACK # Else, a match wasn't found which probably means there was # and error reading the card or the card isn't a PSU ID card # but assume the former else: return c.ERROR_READING_CARD
def showPointsWidget(self): self.pointsTextArea.clear() self.centralWidget.setCurrentWidget(self.pointsWidget) # Get the access ID to show points for or an empty string for all access ID's accessID, ok = QInputDialog.getText(self, "Access ID", "Access ID (blank for all access ID\'s):") if not ok: # The show points thread was not declared yet so just skip the closeShowPointsScreen function self.showMainMenuWidget() # Init the show points thread # accessID will be used in SQL queries. Sanitize it. self.showPointsThread = ShowPointsThread(self.db, SharedUtils.sanitizeInput(str(accessID)), self.setPoints) self.showPointsThread.start()
def postCardSwipe(self, checkinStatus, accessID, cardID, sqlError, pointValue): if checkinStatus == c.SQL_ERROR: QMessageBox.critical(self, "Database Error", "WARNING! Database error: " + sqlError.args[1], QMessageBox.Ok, QMessageBox.Ok) # Don't bother to change UI elements or start the sleep thread, just wait for the next card return elif checkinStatus == c.ERROR_READING_CARD: self.checkinImg.setPixmap(self.redPix) self.checkinLabel.setText("Error reading card. Swipe again.") elif checkinStatus == c.BAD_CHECKIN_TIME: self.checkinImg.setPixmap(self.redPix) self.checkinLabel.setText("You may only check-in once per hour.") elif checkinStatus == c.FUTURE_CHECKIN_TIME: self.checkinImg.setPixmap(self.redPix) self.checkinLabel.setText("Previous check-in time was in the future. Check your local system time.") elif checkinStatus == c.SUCCESS: self.checkinImg.setPixmap(self.greenPix) self.checkinLabel.setText(str(accessID) + " +" + str(pointValue) + " points") elif checkinStatus == c.CARD_NOT_IN_DB: # If the card is not in the DB ask to add it reply = QMessageBox.question(self, "Card Not in Database", "This card was not found in the database. Add it now?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: # If adding new card, get the accessID associated with the card accessID, ok = QInputDialog.getText(self, "Add New Card", "Access ID:") # Sanitize the accessID input and call the add card thread if ok and accessID != "": self.addCardThread = AddCardThread(self.db, cardID, SharedUtils.sanitizeInput(str(accessID)), pointValue, self.postCardSwipe) self.addCardThread.start() # Don't bother to change UI elements or start the sleep thread, just wait for the next card return else: self.checkinImg.setPixmap(self.redPix) self.checkinLabel.setText("An unknown error occurred.") QMessageBox.critical(self, "Unknown Error", "An unknown error occurred", QMessageBox.Ok, QMessageBox.Ok) # Force a repaint of the UI self.checkinImg.update() self.checkinLabel.update() # Sleep for a few seconds before resetting the UI for the next card # The number of seconds is defined in the constants file # This must be on a separate thread since blocking the UI thread is a big no-no self.sleepThread.start()
def showCheckinWidget(self): self.centralWidget.setCurrentWidget(self.checkinWidget) # Get the point value while 1: pointValue, ok = QInputDialog.getText(self, "Point Value", "Point Value:", text=str(c.DEFAULT_POINTS)) if ok: if str(pointValue).isdigit(): break else: QMessageBox.critical(self, "Input Error", "Invalid input", QMessageBox.Ok, QMessageBox.Ok) else: self.closeCheckinScreen() return # Init the checkin thread # pointValue will be used in SQL queries. Sanitize it. self.checkinThread = CheckinThread(self.db, SharedUtils.sanitizeInput(str(pointValue)), self.postCardSwipe)
def keyPressEvent(self, event): # Only look for card swipes if the checkin widget is currently shown if self.centralWidget.currentWidget() == self.checkinWidget: try: # Try to match the input to the card ID regex cardID = self.regex.search(self.cardInput).group(1) # A match was made so reset cardInput for the next card self.cardInput = "" # Set the card ID and start the checkin thread # cardID is going into an SQL query; don't forget to sanitize the input if not self.checkinThread.isRunning(): self.checkinThread.setCardID(SharedUtils.sanitizeInput(str(cardID))) self.checkinThread.start() except AttributeError: # If a match was not made append the current text to card input self.cardInput += event.text()
def showPoints(self): accessID = SharedUtils.sanitizeInput(raw_input("\nAccess ID (blank for all): ")) showPointsResult = self.db.showPoints(accessID) if showPointsResult["showPointsStatus"] == c.SQL_ERROR: self.showDatabaseError(showPointsResult["sqlError"]) elif showPointsResult["showPointsStatus"] == c.NO_RESULTS: print "\nThere were no results to that query." elif showPointsResult["showPointsStatus"] == c.SUCCESS: # If showing all users, display a pretty table if accessID == "": print "\n+--------------------+\n| Access ID | Points |\n+--------------------+" for i in range(len(showPointsResult["pointsTuple"])): print "|%10s | %6s |" % (showPointsResult["pointsTuple"][i][0], showPointsResult["pointsTuple"][i][1]) print "+--------------------+" # Show a single user's points else: print "\n%s has %s points." % (accessID, str(showPointsResult["pointsTuple"][0][0]))
def __init__(self): QDialog.__init__(self, flags=Qt.Dialog) self.ui = uic.loadUi(SharedUtils.path_for_file_in_program_directory("PrefsWindow.ui")) self._preferences: Optional[Preferences] = None self._data_model: Optional[DataModel] = None
def checkin(self): # Get and validate the point value for this check-in # Limited to 500 points to prevent bad typos while 1: pointValue = SharedUtils.sanitizeInput(raw_input("\nPoint Value (" + str(c.DEFAULT_POINTS) + "): ")) # Validate point input if pointValue == "": pointValue = str(c.DEFAULT_POINTS) break elif (pointValue.isdigit() and int(pointValue) <= 500) or pointValue == "back": break else: print "Invalid input. Try again." while 1: cardID = self.getCardSwipe() # If the user requested to exit the loop, break if cardID == c.BACK: break elif cardID == c.ERROR_READING_CARD: print "Error reading card. Swipe card again." continue # Sanitize cardID cardID = SharedUtils.sanitizeInput(cardID) # cardID will be empty if it failed sanitization. Skip checkin if that is the case if cardID == "": continue # Do the checkin checkinResult = self.db.checkin(cardID, pointValue) if checkinResult["checkinStatus"] == c.SQL_ERROR: self.showDatabaseError(checkinResult["sqlError"]) elif checkinResult["checkinStatus"] == c.BAD_CHECKIN_TIME: print "Error: You may only check-in once per hour." elif checkinResult["checkinStatus"] == c.FUTURE_CHECKIN_TIME: print "Error: Previous check-in time was in the future. Check your local system time." elif checkinResult["checkinStatus"] == c.CARD_NOT_IN_DB: # Ask if user wants to add the card addCard = raw_input("Error: Card not found in database. Add it now? (Y,n) ") if addCard == "n": continue # Get the accessID for the new card accessID = SharedUtils.sanitizeInput(raw_input("Access ID: ")) # Add the card addCardResult = self.db.addCard(cardID, accessID, pointValue) if addCardResult["addCardStatus"] == c.SUCCESS: self.showCheckinConfirmation(accessID, pointValue) elif addCardResult["addCardStatus"] == c.SQL_ERROR: self.showDatabaseError(addCardResult["sqlError"]) elif checkinResult["checkinStatus"] == c.SUCCESS: self.showCheckinConfirmation(checkinResult["accessID"], pointValue) else: print "Unknown error checking in."