def test_variant_to_float(): for value, variant in [(5, "5"), (5, 5), (5.5, 5.5), (-5.5, -5.5)]: conv_value = mqt.variant_to_float(pqt.QtCore.QVariant(variant)) assert value == conv_value german_locale = pqt.QtCore.QLocale(pqt.QtCore.QLocale.German) for value, string in [(5, "5"), (5.5, "5,5"), (1000, "1.000")]: conv_value = mqt.variant_to_float(pqt.QtCore.QVariant(string), locale=german_locale) assert conv_value == value french_locale = pqt.QtCore.QLocale(pqt.QtCore.QLocale.French) for value, string in [(5, "5"), (5.5, "5,5"), (1000, "1 000")]: conv_value = mqt.variant_to_float(pqt.QtCore.QVariant(string), locale=french_locale) assert conv_value == value english_locale = pqt.QtCore.QLocale(pqt.QtCore.QLocale.English) for value, string in [(5, "5"), (5.5, "5.5"), (1000, "1,000")]: conv_value = mqt.variant_to_float(pqt.QtCore.QVariant(string), locale=english_locale) assert conv_value == value
def setData(self, index, value, role=QtCore.Qt.EditRole, update=True): """Change a data element of the flight track; overrides the corresponding QAbstractTableModel method. NOTE: Performance computations loose their validity if a change is made. """ if index.isValid() and 0 <= index.row() < len(self.waypoints): waypoint = self.waypoints[index.row()] column = index.column() index2 = index # in most cases only one field is being changed if column == LOCATION: waypoint.location = variant_to_string(value) elif column == LAT: try: # The table fields accept basically any input. # If the string cannot be converted to "float" (raises ValueError), the user input is discarded. value = variant_to_float(value) except TypeError as ex: logging.error("unexpected error: %s %s %s %s", type(ex), ex, type(value), value) except ValueError as ex: logging.error("%s", ex) else: waypoint.lat = value waypoint.location = "" loc = find_location(waypoint.lat, waypoint.lon, 1e-3) if loc is not None: waypoint.lat, waypoint.lon = loc[0] waypoint.location = loc[1] # A change of position requires an update of the distances. if update: self.update_distances(index.row()) # Notify the views that items between the edited item and # the distance item of the corresponding waypoint have been # changed. # Delete the location name -- it won't be valid anymore # after its coordinates have been changed. index2 = self.createIndex(index.row(), LOCATION) elif column == LON: try: # The table fields accept basically any input. # If the string cannot be converted to "float" (raises ValueError), the user input is discarded. value = variant_to_float(value) except TypeError as ex: logging.error("unexpected error: %s %s %s %s", type(ex), ex, type(value), value) except ValueError as ex: logging.error("%s", ex) else: waypoint.lon = value waypoint.location = "" loc = find_location(waypoint.lat, waypoint.lon, 1e-3) if loc is not None: waypoint.lat, waypoint.lon = loc[0] waypoint.location = loc[1] if update: self.update_distances(index.row()) index2 = self.createIndex(index.row(), LOCATION) elif column == FLIGHTLEVEL: try: # The table fields accept basically any input. # If the string cannot be converted to "float" (raises ValueError), the user input is discarded. flightlevel = variant_to_float(value) pressure = float( thermolib.flightlevel2pressure(flightlevel)) except TypeError as ex: logging.error("unexpected error: %s %s %s %s", type(ex), ex, type(value), value) except ValueError as ex: logging.error("%s", ex) else: waypoint.flightlevel = flightlevel waypoint.pressure = pressure if update: self.update_distances(index.row()) # need to notify view of the second item that has been # changed as well. index2 = self.createIndex(index.row(), PRESSURE) elif column == PRESSURE: try: # The table fields accept basically any input. # If the string cannot be converted to "float" (raises ValueError), the user input is discarded. pressure = variant_to_float( value) * 100 # convert hPa to Pa if pressure > 200000: raise ValueError flightlevel = float( round(thermolib.pressure2flightlevel(pressure))) pressure = float( thermolib.flightlevel2pressure(flightlevel)) except TypeError as ex: logging.error("unexpected error: %s %s %s %s", type(ex), ex, type(value), value) except ValueError as ex: logging.error("%s", ex) else: waypoint.pressure = pressure waypoint.flightlevel = flightlevel if update: self.update_distances(index.row()) index2 = self.createIndex(index.row(), FLIGHTLEVEL) else: waypoint.comments = variant_to_string(value) self.modified = True # Performance computations loose their validity if a change is made. if update: self.dataChanged.emit(index, index2) return True return False