Code cleanup

This commit is contained in:
spx 2012-12-23 07:05:46 +09:00
parent e4e48e2239
commit eaf53aa2ae
3 changed files with 137 additions and 171 deletions

View File

@ -85,6 +85,26 @@ void CMeterHistogram::DisposeBuffer()
m_SecondaryValues = NULL;
}
/*
** Creates the buffers.
**
*/
void CMeterHistogram::CreateBuffer()
{
DisposeBuffer();
// Create buffers for values
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
if (maxSize > 0)
{
m_PrimaryValues = new double[maxSize]();
if (m_Measures.size() >= 2)
{
m_SecondaryValues = new double[maxSize]();
}
}
}
/*
** Load the images and calculate the dimensions of the meter from them.
** Or create the brushes if solid color histogram is used.
@ -114,15 +134,15 @@ void CMeterHistogram::Initialize()
if (m_PrimaryImage.IsLoaded())
{
int oldW = m_W;
int oldH = m_H;
int oldSize = m_GraphHorizontalOrientation ? m_H : m_W;
Bitmap* bitmap = m_PrimaryImage.GetImage();
m_W = bitmap->GetWidth();
m_H = bitmap->GetHeight();
if (oldW != m_W || oldH != m_H)
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
if (oldSize != maxSize)
{
m_SizeChanged = true;
}
@ -162,19 +182,7 @@ void CMeterHistogram::Initialize()
}
else if (m_SizeChanged)
{
DisposeBuffer();
// Create buffers for values
if (m_W >= 0 || m_H >= 0)
{
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
maxSize = (maxSize == 0) ? 1 : maxSize;
m_PrimaryValues = new double[maxSize]();
if (secondaryMeasure)
{
m_SecondaryValues = new double[maxSize]();
}
}
CreateBuffer();
m_SizeChanged = false;
}
@ -192,6 +200,7 @@ void CMeterHistogram::ReadOptions(CConfigParser& parser, const WCHAR* section)
std::wstring oldBothImageName = m_OverlapImageName;
int oldW = m_W;
int oldH = m_H;
bool oldGraphHorizontalOrientation = m_GraphHorizontalOrientation;
CMeter::ReadOptions(parser, section);
@ -241,38 +250,6 @@ void CMeterHistogram::ReadOptions(CConfigParser& parser, const WCHAR* section)
m_Autoscale = 0!=parser.ReadInt(section, L"AutoScale", 0);
m_Flip = 0!=parser.ReadInt(section, L"Flip", 0);
if (m_Initialized)
{
if (m_PrimaryImageName.empty())
{
if (oldW != m_W || oldH != m_H)
{
m_SizeChanged = true;
Initialize(); // Reload the image
}
}
else
{
// Reset to old dimensions
m_W = oldW;
m_H = oldH;
m_PrimaryNeedsReload = (wcscmp(oldPrimaryImageName.c_str(), m_PrimaryImageName.c_str()) != 0);
m_SecondaryNeedsReload = (wcscmp(oldSecondaryImageName.c_str(), m_SecondaryImageName.c_str()) != 0);
m_OverlapNeedsReload = (wcscmp(oldBothImageName.c_str(), m_OverlapImageName.c_str()) != 0);
if (m_PrimaryNeedsReload ||
m_SecondaryNeedsReload ||
m_OverlapNeedsReload ||
m_PrimaryImage.IsOptionsChanged() ||
m_SecondaryImage.IsOptionsChanged() ||
m_OverlapImage.IsOptionsChanged())
{
Initialize(); // Reload the image
}
}
}
const WCHAR* graph = parser.ReadString(section, L"GraphStart", L"RIGHT").c_str();
if (_wcsicmp(graph, L"RIGHT") == 0)
{
@ -289,55 +266,56 @@ void CMeterHistogram::ReadOptions(CConfigParser& parser, const WCHAR* section)
graph = parser.ReadString(section, L"GraphOrientation", L"VERTICAL").c_str();
if (_wcsicmp(graph, L"VERTICAL") == 0)
{
// Restart graph
if (m_GraphHorizontalOrientation)
{
m_GraphHorizontalOrientation = false;
DisposeBuffer();
// Create buffers for values
if (m_W > 0)
{
m_PrimaryValues = new double[m_W]();
if (m_Measures.size() >= 2)
{
m_SecondaryValues = new double[m_W]();
}
}
}
else
{
m_GraphHorizontalOrientation = false;
}
}
else if (_wcsicmp(graph, L"HORIZONTAL") == 0)
{
// Restart graph
if (!m_GraphHorizontalOrientation)
{
m_GraphHorizontalOrientation = true;
DisposeBuffer();
// Create buffers for values
if (m_H > 0)
{
m_PrimaryValues = new double[m_H]();
if (m_Measures.size() >= 2)
{
m_SecondaryValues = new double[m_H]();
}
}
}
else
{
m_GraphHorizontalOrientation = true;
}
}
else
{
LogWithArgs(LOG_ERROR, L"GraphOrientation=%s is not valid in [%s]", graph, m_Name.c_str());
}
if (m_Initialized)
{
if (m_PrimaryImageName.empty())
{
int oldSize = oldGraphHorizontalOrientation ? oldH : oldW;
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
if (oldSize != maxSize || oldGraphHorizontalOrientation != m_GraphHorizontalOrientation)
{
m_SizeChanged = true;
Initialize(); // Reload the image
}
}
else
{
// Reset to old dimensions
m_W = oldW;
m_H = oldH;
m_PrimaryNeedsReload = (wcscmp(oldPrimaryImageName.c_str(), m_PrimaryImageName.c_str()) != 0);
m_SecondaryNeedsReload = (wcscmp(oldSecondaryImageName.c_str(), m_SecondaryImageName.c_str()) != 0);
m_OverlapNeedsReload = (wcscmp(oldBothImageName.c_str(), m_OverlapImageName.c_str()) != 0);
m_SizeChanged = (oldGraphHorizontalOrientation != m_GraphHorizontalOrientation);
if (m_PrimaryNeedsReload ||
m_SecondaryNeedsReload ||
m_OverlapNeedsReload ||
m_PrimaryImage.IsOptionsChanged() ||
m_SecondaryImage.IsOptionsChanged() ||
m_OverlapImage.IsOptionsChanged())
{
Initialize(); // Reload the image
}
else if (m_SizeChanged)
{
CreateBuffer();
}
}
}
}
/*
@ -346,7 +324,11 @@ void CMeterHistogram::ReadOptions(CConfigParser& parser, const WCHAR* section)
*/
bool CMeterHistogram::Update()
{
if (CMeter::Update() && !m_Measures.empty() && m_PrimaryValues)
if (CMeter::Update() && !m_Measures.empty())
{
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
if (maxSize > 0) // m_PrimaryValues is not NULL
{
CMeasure* measure = m_Measures[0];
CMeasure* secondaryMeasure = (m_Measures.size() >= 2) ? m_Measures[1] : NULL;
@ -360,8 +342,6 @@ bool CMeterHistogram::Update()
}
++m_MeterPos;
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
maxSize = (maxSize == 0) ? 1 : maxSize;
m_MeterPos %= maxSize;
m_MaxPrimaryValue = measure->GetMaxValue();
@ -420,6 +400,7 @@ bool CMeterHistogram::Update()
}
}
}
}
return true;
}
return false;

View File

@ -42,6 +42,7 @@ protected:
private:
void DisposeBuffer();
void CreateBuffer();
Gdiplus::Color m_PrimaryColor;
Gdiplus::Color m_SecondaryColor;

View File

@ -105,7 +105,8 @@ void CMeterLine::ReadOptions(CConfigParser& parser, const WCHAR* section)
// Store the current number of lines so we know if the buffer needs to be updated
int oldLineCount = (int)m_Colors.size();
int oldW = m_W;
int oldSize = m_GraphHorizontalOrientation ? m_H : m_W;
bool oldGraphHorizontalOrientation = m_GraphHorizontalOrientation;
CMeter::ReadOptions(parser, section);
@ -146,12 +147,6 @@ void CMeterLine::ReadOptions(CConfigParser& parser, const WCHAR* section)
ARGB color = parser.ReadColor(section, L"HorizontalColor", Color::Black); // This is left here for backwards compatibility
m_HorizontalColor = parser.ReadColor(section, L"HorizontalLineColor", color); // This is what it should be
if (m_Initialized &&
(oldLineCount != lineCount || oldW != m_W))
{
Initialize();
}
const WCHAR* graph = parser.ReadString(section, L"GraphStart", L"RIGHT").c_str();
if (_wcsicmp(graph, L"RIGHT") == 0)
{
@ -168,39 +163,28 @@ void CMeterLine::ReadOptions(CConfigParser& parser, const WCHAR* section)
graph = parser.ReadString(section, L"GraphOrientation", L"VERTICAL").c_str();
if (_wcsicmp(graph, L"VERTICAL") == 0)
{
// Restart graph
if (m_GraphHorizontalOrientation)
{
m_GraphHorizontalOrientation = false;
m_AllValues.clear();
Initialize();
m_CurrentPos = 0;
}
else
{
m_GraphHorizontalOrientation = false;
}
}
else if (_wcsicmp(graph, L"HORIZONTAL") == 0)
{
// Restart graph
if (!m_GraphHorizontalOrientation)
{
m_GraphHorizontalOrientation = true;
m_AllValues.clear();
Initialize();
m_CurrentPos = 0;
}
else
{
m_GraphHorizontalOrientation = true;
}
}
else
{
LogWithArgs(LOG_ERROR, L"GraphOrientation=%s is not valid in [%s]", graph, m_Name.c_str());
}
if (m_Initialized)
{
int maxSize = m_GraphHorizontalOrientation ? m_H : m_W;
if (oldLineCount != lineCount || oldSize != maxSize || oldGraphHorizontalOrientation != m_GraphHorizontalOrientation)
{
m_AllValues.clear();
m_CurrentPos = 0;
Initialize();
}
}
}
/*