- Added multiple MeterStyle.

- Code cleanup & cosmetics.
This commit is contained in:
spx 2010-11-04 00:17:42 +00:00
parent 46862a033e
commit 25abbc2444
6 changed files with 100 additions and 44 deletions

View File

@ -585,7 +585,7 @@ bool CConfigParser::ReplaceMeasures(std::wstring& result)
**
**
*/
const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCTSTR defValue, bool bReplaceMeasures, bool* bReplaced)
const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCTSTR defValue, bool bReplaceMeasures)
{
static std::wstring result;
@ -602,18 +602,66 @@ const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCT
defValue = L"";
}
// Clear last status
m_LastUsedStyle.clear();
m_LastReplaced = false;
m_LastDefaultUsed = false;
std::wstring strDefault = defValue;
// If the template is defined read the value first from there.
if (!m_StyleTemplate.empty())
{
strDefault = GetValue(m_StyleTemplate, key, strDefault);
std::vector<std::wstring>::const_reverse_iterator iter = m_StyleTemplate.rbegin();
for ( ; iter != m_StyleTemplate.rend(); ++iter)
{
if (!(*iter).empty())
{
std::wstring strSection = (*iter);
std::wstring::size_type pos = strSection.find_first_not_of(L" \t\r\n");
if (pos != std::wstring::npos)
{
std::wstring::size_type lastPos = strSection.find_last_not_of(L" \t\r\n");
if (pos != 0 || lastPos != (strSection.length() - 1))
{
// Trim white-space
strSection.swap(std::wstring(strSection, pos, lastPos - pos + 1));
}
const std::wstring& strStyle = GetValue(strSection, key, strDefault);
//DebugLog(L"[%s] %s (from [%s]) : strDefault=%s (0x%08X), strStyle=%s (0x%08X)",
// section, key, strSection.c_str(), strDefault.c_str(), &strDefault, strStyle.c_str(), &strStyle);
if (&strStyle != &strDefault)
{
strDefault = strStyle;
m_LastUsedStyle = strSection;
break;
}
}
}
}
}
result = GetValue(section, key, strDefault);
if (result == defValue)
const std::wstring& strValue = GetValue(section, key, strDefault);
result = strValue;
if (!m_LastUsedStyle.empty())
{
return result;
if (&strValue != &strDefault)
{
m_LastUsedStyle.clear();
}
}
else
{
if (&strValue == &strDefault)
{
m_LastDefaultUsed = true;
return result;
}
}
// Check Litestep vars
@ -629,16 +677,14 @@ const std::wstring& CConfigParser::ReadString(LPCTSTR section, LPCTSTR key, LPCT
}
}
bool replaced = ReplaceVariables(result);
if (bReplaceMeasures)
if (ReplaceVariables(result))
{
replaced |= ReplaceMeasures(result);
m_LastReplaced = true;
}
if (bReplaced)
if (bReplaceMeasures && ReplaceMeasures(result))
{
*bReplaced = replaced;
m_LastReplaced = true;
}
return result;
@ -766,11 +812,11 @@ Color CConfigParser::ReadColor(LPCTSTR section, LPCTSTR key, const Color& defVal
**
** http://www.digitalpeer.com/id/simple
*/
std::vector<std::wstring> CConfigParser::Tokenize(const std::wstring& str, const std::wstring delimiters)
std::vector<std::wstring> CConfigParser::Tokenize(const std::wstring& str, const std::wstring& delimiters)
{
std::vector<std::wstring> tokens;
std::wstring::size_type lastPos = str.find_first_not_of(L";", 0); // skip delimiters at beginning.
std::wstring::size_type lastPos = str.find_first_not_of(delimiters, 0); // skip delimiters at beginning.
std::wstring::size_type pos = str.find_first_of(delimiters, lastPos); // find first "non-delimiter".
while (std::wstring::npos != pos || std::wstring::npos != lastPos)

View File

@ -42,12 +42,16 @@ public:
void SetVariable(const std::wstring& strVariable, const std::wstring& strValue) { SetVariable(m_Variables, strVariable, strValue); }
void SetStyleTemplate(const std::wstring& strStyle) { m_StyleTemplate = strStyle; }
void SetStyleTemplate(const std::wstring& strStyle) { m_StyleTemplate = Tokenize(strStyle, L"|"); }
void ClearStyleTemplate() { m_StyleTemplate.clear(); }
const std::wstring& GetLastUsedStyle() { return m_LastUsedStyle; }
bool GetLastReplaced() { return m_LastReplaced; }
bool GetLastDefaultUsed() { return m_LastDefaultUsed; }
void ResetMonitorVariables(CMeterWindow* meterWindow = NULL);
const std::wstring& ReadString(LPCTSTR section, LPCTSTR key, LPCTSTR defValue, bool bReplaceMeasures = true, bool* bReplaced = NULL);
const std::wstring& ReadString(LPCTSTR section, LPCTSTR key, LPCTSTR defValue, bool bReplaceMeasures = true);
double ReadFloat(LPCTSTR section, LPCTSTR key, double defValue);
double ReadFormula(LPCTSTR section, LPCTSTR key, double defValue);
int ReadInt(LPCTSTR section, LPCTSTR key, int defValue);
@ -60,7 +64,7 @@ public:
// Returns an int if the formula was read successfully, -1 for failure.
int ReadFormula(const std::wstring& result, double* number);
static std::vector<std::wstring> Tokenize(const std::wstring& str, const std::wstring delimiters);
static std::vector<std::wstring> Tokenize(const std::wstring& str, const std::wstring& delimiters);
static double ParseDouble(const std::wstring& string, double defValue, bool rejectExp = false);
static Gdiplus::Color ParseColor(LPCTSTR string);
@ -94,7 +98,11 @@ private:
hqMathParser* m_Parser;
std::map<std::wstring, CMeasure*> m_Measures;
std::wstring m_StyleTemplate;
std::vector<std::wstring> m_StyleTemplate;
std::wstring m_LastUsedStyle;
bool m_LastReplaced;
bool m_LastDefaultUsed;
std::vector<std::wstring> m_Sections; // The sections must be an ordered array
stdext::hash_map<std::wstring, std::vector<std::wstring> > m_Keys;

View File

@ -78,26 +78,25 @@ bool CGroup::BelongsToGroup(const std::wstring& group)
std::wstring CGroup::CreateGroup(const std::wstring& str)
{
std::wstring strTmp = str;
std::wstring strTmp;
// Trim whitespace
std::wstring::size_type pos = strTmp.find_last_not_of(L' ');
std::wstring::size_type pos = str.find_first_not_of(L" \t\r\n");
if (pos != std::wstring::npos)
{
strTmp.erase(pos + 1);
pos = strTmp.find_first_not_of(' ');
if (pos != std::wstring::npos)
std::wstring::size_type lastPos = str.find_last_not_of(L" \t\r\n");
if (pos != 0 || lastPos != (str.length() - 1))
{
strTmp.erase(0, pos);
// Trim white-space
strTmp.swap(std::wstring(str, pos, lastPos - pos + 1));
}
else
{
strTmp = str;
}
// Convert to lower
std::transform(strTmp.begin(), strTmp.end(), strTmp.begin(), ::towlower);
}
else
{
strTmp.erase(strTmp.begin(), strTmp.end());
}
return strTmp;
}

View File

@ -102,8 +102,6 @@ void CMeasure::Initialize()
*/
void CMeasure::ReadConfig(CConfigParser& parser, const WCHAR* section)
{
bool replaced;
// Clear substitutes to prevent from being added more than once.
if (!m_Substitute.empty())
{
@ -118,9 +116,8 @@ void CMeasure::ReadConfig(CConfigParser& parser, const WCHAR* section)
}
else
{
replaced = false;
const std::wstring& result = parser.ReadString(section, L"Disabled", L"0", true, &replaced);
if (replaced)
const std::wstring& result = parser.ReadString(section, L"Disabled", L"0");
if (parser.GetLastReplaced())
{
m_Disabled = 0!=(int)parser.ParseDouble(result, 0.0, true);
}

View File

@ -269,8 +269,6 @@ void CMeter::Hide()
*/
void CMeter::ReadConfig(const WCHAR* section)
{
bool replaced;
CConfigParser& parser = m_MeterWindow->GetParser();
// The MeterStyle defines a template where the values are read if the meter doesn't have it itself
@ -280,9 +278,13 @@ void CMeter::ReadConfig(const WCHAR* section)
parser.SetStyleTemplate(style);
}
replaced = false;
std::wstring coord = parser.ReadString(section, L"X", L"0", true, &replaced);
if (!m_Initialized || replaced)
std::wstring oldStyleX = m_StyleX;
std::wstring oldStyleY = m_StyleY;
std::wstring oldStyleHidden = m_StyleHidden;
std::wstring coord = parser.ReadString(section, L"X", L"0");
m_StyleX = parser.GetLastUsedStyle();
if (!m_Initialized || parser.GetLastReplaced() || !parser.GetLastDefaultUsed() && m_StyleX != oldStyleX)
{
if (!coord.empty())
{
@ -319,9 +321,9 @@ void CMeter::ReadConfig(const WCHAR* section)
}
}
replaced = false;
coord = parser.ReadString(section, L"Y", L"0", true, &replaced);
if (!m_Initialized || replaced)
coord = parser.ReadString(section, L"Y", L"0");
m_StyleY = parser.GetLastUsedStyle();
if (!m_Initialized || parser.GetLastReplaced() || !parser.GetLastDefaultUsed() && m_StyleY != oldStyleY)
{
if (!coord.empty())
{
@ -367,9 +369,9 @@ void CMeter::ReadConfig(const WCHAR* section)
}
else
{
replaced = false;
const std::wstring& result = parser.ReadString(section, L"Hidden", L"0", true, &replaced);
if (replaced)
const std::wstring& result = parser.ReadString(section, L"Hidden", L"0");
m_StyleHidden = parser.GetLastUsedStyle();
if (parser.GetLastReplaced() || !parser.GetLastDefaultUsed() && m_StyleHidden != oldStyleHidden)
{
m_Hidden = 0!=(int)parser.ParseDouble(result, 0.0, true);
}

View File

@ -127,6 +127,10 @@ protected:
CMeter* m_RelativeMeter;
bool m_DynamicVariables; // If true, the measure contains dynamic variables
std::wstring m_StyleX;
std::wstring m_StyleY;
std::wstring m_StyleHidden;
std::wstring m_ToolTipText;
std::wstring m_ToolTipTitle;
std::wstring m_ToolTipIcon;