From 25abbc2444584f594d8b16965de18c8ca095ea67 Mon Sep 17 00:00:00 2001 From: spx Date: Thu, 4 Nov 2010 00:17:42 +0000 Subject: [PATCH] - Added multiple MeterStyle. - Code cleanup & cosmetics. --- Library/ConfigParser.cpp | 72 ++++++++++++++++++++++++++++++++-------- Library/ConfigParser.h | 16 ++++++--- Library/Group.cpp | 21 ++++++------ Library/Measure.cpp | 7 ++-- Library/Meter.cpp | 24 ++++++++------ Library/Meter.h | 4 +++ 6 files changed, 100 insertions(+), 44 deletions(-) diff --git a/Library/ConfigParser.cpp b/Library/ConfigParser.cpp index 571556e4..912136b2 100644 --- a/Library/ConfigParser.cpp +++ b/Library/ConfigParser.cpp @@ -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::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 CConfigParser::Tokenize(const std::wstring& str, const std::wstring delimiters) +std::vector CConfigParser::Tokenize(const std::wstring& str, const std::wstring& delimiters) { std::vector 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) diff --git a/Library/ConfigParser.h b/Library/ConfigParser.h index 849e3a24..8d6792d7 100644 --- a/Library/ConfigParser.h +++ b/Library/ConfigParser.h @@ -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 Tokenize(const std::wstring& str, const std::wstring delimiters); + static std::vector 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 m_Measures; - std::wstring m_StyleTemplate; + std::vector m_StyleTemplate; + + std::wstring m_LastUsedStyle; + bool m_LastReplaced; + bool m_LastDefaultUsed; std::vector m_Sections; // The sections must be an ordered array stdext::hash_map > m_Keys; diff --git a/Library/Group.cpp b/Library/Group.cpp index f1ad136b..a760f59f 100644 --- a/Library/Group.cpp +++ b/Library/Group.cpp @@ -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; } \ No newline at end of file diff --git a/Library/Measure.cpp b/Library/Measure.cpp index 15510f41..696a532a 100644 --- a/Library/Measure.cpp +++ b/Library/Measure.cpp @@ -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); } diff --git a/Library/Meter.cpp b/Library/Meter.cpp index 409ca025..70ab6f65 100644 --- a/Library/Meter.cpp +++ b/Library/Meter.cpp @@ -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); } diff --git a/Library/Meter.h b/Library/Meter.h index 0f16c7c8..b59b36d9 100644 --- a/Library/Meter.h +++ b/Library/Meter.h @@ -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;