mirror of
				https://github.com/subsurface/subsurface.git
				synced 2025-02-19 22:16:15 +00:00 
			
		
		
		
	Make 'string_to_grams()' use proper type safe types
Make it use 'weight_t' and hide the "grams" part inside the type. That was the whole point of the weight_t type, after all. Returning a "double" was always bogus, since we internally always do integer grams (and the function actually used "rint()" to get all the rounding right anyway). As a result, it's now called "string_to_weight()". Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
		
							parent
							
								
									8626778918
								
							
						
					
					
						commit
						b9d7e440fd
					
				
					 1 changed files with 7 additions and 4 deletions
				
			
		|  | @ -464,13 +464,14 @@ void WeightModel::passInData(const QModelIndex& index, const QVariant& value) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| double string_to_grams(char *str) | weight_t string_to_weight(char *str) | ||||||
| { | { | ||||||
| 	char *end; | 	char *end; | ||||||
| 	double value = strtod_flags(str, &end, 0); | 	double value = strtod_flags(str, &end, 0); | ||||||
| 	QString rest = QString(end).trimmed(); | 	QString rest = QString(end).trimmed(); | ||||||
| 	QString local_kg = WeightModel::tr("kg"); | 	QString local_kg = WeightModel::tr("kg"); | ||||||
| 	QString local_lbs = WeightModel::tr("lbs"); | 	QString local_lbs = WeightModel::tr("lbs"); | ||||||
|  | 	weight_t weight; | ||||||
| 
 | 
 | ||||||
| 	if (rest.startsWith("kg") || rest.startsWith(local_kg)) | 	if (rest.startsWith("kg") || rest.startsWith(local_kg)) | ||||||
| 		goto kg; | 		goto kg; | ||||||
|  | @ -480,9 +481,11 @@ double string_to_grams(char *str) | ||||||
| 	if (prefs.units.weight == prefs.units.LBS) | 	if (prefs.units.weight == prefs.units.LBS) | ||||||
| 		goto lbs; | 		goto lbs; | ||||||
| kg: | kg: | ||||||
| 	return rint(value * 1000); | 	weight.grams = rint(value * 1000); | ||||||
|  | 	return weight; | ||||||
| lbs: | lbs: | ||||||
| 	return lbs_to_grams(value); | 	weight.grams = lbs_to_grams(value); | ||||||
|  | 	return weight; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role) | bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role) | ||||||
|  | @ -509,7 +512,7 @@ bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int r | ||||||
| 		break; | 		break; | ||||||
| 	case WEIGHT: | 	case WEIGHT: | ||||||
| 		if (CHANGED(data, "", "")) { | 		if (CHANGED(data, "", "")) { | ||||||
| 			ws->weight.grams = string_to_grams(vString.toUtf8().data()); | 			ws->weight = string_to_weight(vString.toUtf8().data()); | ||||||
| 			// now update the ws_info
 | 			// now update the ws_info
 | ||||||
| 			changed = true; | 			changed = true; | ||||||
| 			WSInfoModel *wsim = WSInfoModel::instance(); | 			WSInfoModel *wsim = WSInfoModel::instance(); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue