Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Graph Settings #879

Merged
merged 22 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CalcViewModel/ApplicationViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void ApplicationViewModel::OnCopyCommand(Object ^ parameter)
{
DateCalcViewModel->OnCopyCommand(parameter);
}
else
else if (NavCategory::IsCalculatorViewMode(m_mode))
{
CalculatorViewModel->OnCopyCommand(parameter);
}
Expand Down
4 changes: 3 additions & 1 deletion src/CalcViewModel/CalcViewModel.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
<ClInclude Include="GraphingCalculator\EquationViewModel.h" />
<ClInclude Include="GraphingCalculator\GraphingCalculatorViewModel.h" />
<ClInclude Include="GraphingCalculator\VariableViewModel.h" />
<ClInclude Include="GraphingCalculator\GraphingSettingsViewModel.h" />
<ClInclude Include="HistoryItemViewModel.h" />
<ClInclude Include="HistoryViewModel.h" />
<ClInclude Include="MemoryItemViewModel.h" />
Expand Down Expand Up @@ -366,6 +367,7 @@
<ClCompile Include="DateCalculatorViewModel.cpp" />
<ClCompile Include="GraphingCalculator\EquationViewModel.cpp" />
<ClCompile Include="GraphingCalculator\GraphingCalculatorViewModel.cpp" />
<ClCompile Include="GraphingCalculator\GraphingSettingsViewModel.cpp" />
<ClCompile Include="HistoryItemViewModel.cpp" />
<ClCompile Include="HistoryViewModel.cpp" />
<ClCompile Include="MemoryItemViewModel.cpp" />
Expand Down Expand Up @@ -421,4 +423,4 @@
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.UI.Xaml.2.2.190830001\build\native\Microsoft.UI.Xaml.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.UI.Xaml.2.2.190830001\build\native\Microsoft.UI.Xaml.targets'))" />
</Target>
</Project>
</Project>
6 changes: 6 additions & 0 deletions src/CalcViewModel/CalcViewModel.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
<ClCompile Include="Common\Automation\NarratorAnnouncement.cpp">
<Filter>Common\Automation</Filter>
</ClCompile>
<ClCompile Include="GraphingCalculator\GraphingSettingsViewModel.cpp">
<Filter>GraphingCalculator</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand Down Expand Up @@ -220,6 +223,9 @@
<ClInclude Include="GraphingCalculator\VariableViewModel.h">
<Filter>GraphingCalculator</Filter>
</ClInclude>
<ClInclude Include="GraphingCalculator\GraphingSettingsViewModel.h">
<Filter>GraphingCalculator</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="DataLoaders\DefaultFromToCurrency.json">
Expand Down
164 changes: 164 additions & 0 deletions src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include "pch.h"
#include "GraphingSettingsViewModel.h"
#include <CalcManager\NumberFormattingUtils.h>

using namespace CalculatorApp::ViewModel;
using namespace CalcManager::NumberFormattingUtils;
using namespace GraphControl;
using namespace std;
using namespace Platform;

GraphingSettingsViewModel::GraphingSettingsViewModel()
: m_XMinValue(0)
, m_XMaxValue(0)
, m_YMinValue(0)
, m_YMaxValue(0)
, m_XMinError(false)
, m_XMaxError(false)
, m_YMinError(false)
, m_YMaxError(false)
, m_dontUpdateDisplayRange(false)
, m_XIsMinLastChanged(true)
, m_YIsMinLastChanged(true)
{
}

void GraphingSettingsViewModel::SetGrapher(Grapher ^ grapher)
{
if (grapher != nullptr)
{
if (grapher->TrigUnitMode == (int)Graphing::EvalTrigUnitMode::Invalid)
{
grapher->TrigUnitMode = (int)Graphing::EvalTrigUnitMode::Radians;
}
}
Graph = grapher;
InitRanges();
RaisePropertyChanged(L"TrigUnit");
}

void GraphingSettingsViewModel::InitRanges()
{
double xMin = 0, xMax = 0, yMin = 0, yMax = 0;
if (m_Graph != nullptr)
{
m_Graph->GetDisplayRanges(&xMin, &xMax, &yMin, &yMax);
}
m_dontUpdateDisplayRange = true;
m_XMinValue = xMin;
m_XMaxValue = xMax;
m_YMinValue = yMin;
m_YMaxValue = yMax;
auto valueStr = to_wstring(m_XMinValue);
TrimTrailingZeros(valueStr);
m_XMin = ref new String(valueStr.c_str());

valueStr = to_wstring(m_XMaxValue);
TrimTrailingZeros(valueStr);
m_XMax = ref new String(valueStr.c_str());

valueStr = to_wstring(m_YMinValue);
TrimTrailingZeros(valueStr);
m_YMin = ref new String(valueStr.c_str());

valueStr = to_wstring(m_YMaxValue);
TrimTrailingZeros(valueStr);
m_YMax = ref new String(valueStr.c_str());

m_dontUpdateDisplayRange = false;
}

void GraphingSettingsViewModel::RefreshPosition()
{
if (HasError())
{
InitRanges();
}
else
{
if (m_Graph != nullptr)
{
m_Graph->SetDisplayRanges(m_XMinValue, m_XMaxValue, m_YMinValue, m_YMaxValue);
}
}
}

void GraphingSettingsViewModel::UpdateDisplayRange(bool XValuesModified)
{
if (m_Graph == nullptr || m_dontUpdateDisplayRange || HasError())
{
return;
}

if (m_Graph->ForceProportionalAxes)
{
// If ForceProportionalAxes is set, the graph will try to automatically adjust ranges to remain proportional.
// but without a logic to choose which values can be modified or not.
// To solve this problem, we calculate the new ranges here, taking care to not modify the current axis and
// modifying only the least recently updated value of the other axis.

if (XValuesModified)
{
if (m_YIsMinLastChanged)
{
auto yMaxValue = m_YMinValue + (m_XMaxValue - m_XMinValue) * m_Graph->ActualHeight / m_Graph->ActualWidth;
if (m_YMaxValue != yMaxValue)
{
m_YMaxValue = yMaxValue;
auto valueStr = to_wstring(m_YMaxValue);
TrimTrailingZeros(valueStr);
m_YMax = ref new String(valueStr.c_str());
RaisePropertyChanged("YMax");
}
}
else
{
auto yMinValue = m_YMaxValue - (m_XMaxValue - m_XMinValue) * m_Graph->ActualHeight / m_Graph->ActualWidth;
if (m_YMinValue != yMinValue)
{
m_YMinValue = yMinValue;
auto valueStr = to_wstring(m_YMinValue);
TrimTrailingZeros(valueStr);
m_YMin = ref new String(valueStr.c_str());
RaisePropertyChanged("YMin");
}
}
}
else
{
if (m_XIsMinLastChanged)
{
auto xMaxValue = m_XMinValue + (m_YMaxValue - m_YMinValue) * m_Graph->ActualWidth / m_Graph->ActualHeight;
if (m_XMaxValue != xMaxValue)
{
m_XMaxValue = xMaxValue;
auto valueStr = to_wstring(m_XMaxValue);
TrimTrailingZeros(valueStr);
m_XMax = ref new String(valueStr.c_str());
RaisePropertyChanged("XMax");
}
}
else
{
auto xMinValue = m_XMaxValue - (m_YMaxValue - m_YMinValue) * m_Graph->ActualWidth / m_Graph->ActualHeight;
if (m_XMinValue != xMinValue)
{
m_XMinValue = xMinValue;
auto valueStr = to_wstring(m_XMinValue);
TrimTrailingZeros(valueStr);
m_XMin = ref new String(valueStr.c_str());
RaisePropertyChanged("XMin");
}
}
}
}
m_Graph->SetDisplayRanges(m_XMinValue, m_XMaxValue, m_YMinValue, m_YMaxValue);
}

bool GraphingSettingsViewModel::HasError()
{
return m_XMinError || m_YMinError || m_XMaxError || m_YMaxError || XError || YError;
}
Loading