张洋 hai 1 mes
pai
achega
ca5fbe4a02
Modificáronse 1 ficheiros con 67 adicións e 8 borrados
  1. 67 8
      zhipuzi_pos_windows/control/CDiandanNatureItemUI.cpp

+ 67 - 8
zhipuzi_pos_windows/control/CDiandanNatureItemUI.cpp

@@ -32,6 +32,42 @@ namespace
 		ReleaseDC(NULL, hdc);
 		return width;
 	}
+
+	// 去掉属性值名称末尾的加价后缀(如 +3 / +27 / +0.5),避免与右侧红橙价重复
+	// 规则:尾部空格 + +号 + 可选小数数字;若去掉后为空则保留原名,防止无名称
+	std::wstring StripTrailingPrice(const std::wstring& name)
+	{
+		if (name.empty()) return name;
+		int n = (int)name.size();
+		int i = n - 1;
+		while (i >= 0 && name[i] == L' ') i--;
+		if (i < 0) return name;
+
+		// 必须以数字结尾
+		if (i >= 0 && name[i] >= L'0' && name[i] <= L'9')
+		{
+			// 整数部分
+			while (i >= 0 && name[i] >= L'0' && name[i] <= L'9') i--;
+			// 可选小数部分:.digits
+			if (i >= 0 && name[i] == L'.')
+			{
+				int dotPos = i;
+				int j = i - 1;
+				while (j >= 0 && name[j] >= L'0' && name[j] <= L'9') j--;
+				if (j < dotPos - 1) i = j; // 小数点后有数字,一起去掉
+			}
+			// 前面必须是 '+'
+			if (i >= 0 && name[i] == L'+')
+			{
+				int cut = i;
+				while (cut > 0 && name[cut - 1] == L' ') cut--;
+				if (cut > 0) return name.substr(0, cut);
+				// 整个字符串只剩下 +N 时保留原名,避免按钮名字为空
+				return name;
+			}
+		}
+		return name;
+	}
 }
 
 CDiandanNatureItemUI::CDiandanNatureItemUI()
@@ -67,22 +103,45 @@ void CDiandanNatureItemUI::addNatureValue(FoodNatureValue value)
 
 std::wstring CDiandanNatureItemUI::MakeBtnText(DuiLib::CPaintManagerUI* pManager, const std::wstring& name, int value, bool selected)
 {
-	std::wstring s = name;
+	// 去掉商品属性值名称末尾自带的加价后缀(如 "+3"),避免与右侧红橙价重复显示
+	std::wstring baseName = StripTrailingPrice(name);
+	if (baseName.empty()) baseName = name;
+
+	std::wstring s = baseName;
 	if (value > 0)
 	{
 		// 文本区宽度 = 按钮 185 - 左 padding 12 - 右 padding 12 - 右侧安全边距 2
 		const int TEXT_AREA_W = 185 - 12 - 12 - 2;
-		int nameFontId = selected ? 112 : 12;
-		int nameW = MeasureTextWidth(pManager, nameFontId, name);
+		int nameFontId = selected ? 112 : 12; // 选中 13 粗体 / 未选 13 常规
+
+		// 加价:暖红橙 #F5623D,字号/字重与属性名一致(沿用按钮基础字体,仅改色,不再用 14 粗体)
+		// 数字前加人民币符号 ¥(紧贴数字,+ 为加价标识)
+		std::wstring price = L"+¥" + std::to_wstring(value);
+		int priceW = MeasureTextWidth(pManager, nameFontId, price);
+
+		const int MIN_GAP = 4; // 名称与加价之间的最小间距
 
-		std::wstring price = L"+" + std::to_wstring(value);
-		int priceW = MeasureTextWidth(pManager, 11, price);
+		// 名称超长时截断并加省略号,保证加价始终完整可见(不会被右侧 padding 裁掉)
+		std::wstring showName = baseName;
+		int nameW = MeasureTextWidth(pManager, nameFontId, showName);
+		int availForName = TEXT_AREA_W - priceW - MIN_GAP;
+		if (nameW > availForName)
+		{
+			std::wstring trunc = baseName;
+			while (trunc.length() > 1)
+			{
+				trunc = trunc.substr(0, trunc.length() - 1);
+				if (MeasureTextWidth(pManager, nameFontId, trunc + L"…") <= availForName) break;
+			}
+			showName = trunc + L"…";
+			nameW = MeasureTextWidth(pManager, nameFontId, showName);
+		}
 
 		int offset = TEXT_AREA_W - nameW - priceW;
-		if (offset < 4) offset = 4; // 最小间距,防止名字过长时重叠
+		if (offset < MIN_GAP) offset = MIN_GAP; // 兜底间距,防止极端情况下重叠
 
-		// 加价橙色右对齐;选中态仍保持橙色,与绿色名称形成 C3 分色效果
-		s += L"{x " + std::to_wstring(offset) + L"}<c #C47410><f 11>" + price + L"</f></c>";
+		// 仅加红色,字体沿用按钮基础字体(与属性名一致),不加 <f> 标签
+		s = showName + L"{x " + std::to_wstring(offset) + L"}<c #F5623D>" + price + L"</c>";
 	}
 	return s;
 }