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 ability to load settings #484

Merged
merged 5 commits into from
Feb 27, 2024
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
6 changes: 3 additions & 3 deletions BHoM_Windows_UI/Settings/SearchSettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
<Setter Property="Padding" Value="2" />
</Style>
</StackPanel.Resources>
<Button Name="SelectAllBtn" Content="Unselect all" Width="70" Background="#0DCAF0" Click="SelectAll"/>
<Button Content="Reset" Width="50" Background="#F0AD4E" Click="ResetAll" />
<Button Content="Save" Width="50" Background="#5CB85C" Click="SaveAll "/>
<Button Name="SelectAllBtn" Content="Unselect all" Width="70" Background="#0DCAF0" Click="SelectAll" ToolTip="Clicking this will uncheck all toolkits currently selected."/>
<Button Content="Load" Width="50" Background="#F0AD4E" Click="LoadSettings" ToolTip="Select a settings file from your system to load in. You will have the chance to inspect those settings before they are used. You will need to hit the Save button to confirm those settings." />
<Button Content="Save" Width="50" Background="#5CB85C" Click="SaveAll" ToolTip="Save the settings currently selected. This will export settings to your harddrive so they can be loaded again next time you use BHoM, as well as loaded into memory to take effect from the point you hit this button. The settings window will close once settings are saved."/>
</StackPanel>

<ScrollViewer Grid.Row="3" Grid.ColumnSpan="2" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden" Padding="2">
Expand Down
52 changes: 48 additions & 4 deletions BHoM_Windows_UI/Settings/SearchSettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
Expand Down Expand Up @@ -151,11 +152,48 @@ private void ConvertToCheckbox(ToolkitSelectItemModel toolkitItem)

/*************************************/

private void ResetAll(object sender, EventArgs e)
private void LoadSettings(object sender, EventArgs e)
{
m_ToolkitItems.ForEach(x => x.Include = true);
m_SelectAll = true;
SelectAllBtn.Content = "Unselect all";
Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();
openFileDlg.DefaultExt = ".json";
openFileDlg.Filter = "JSON Files (*json)|*json";

bool? result = openFileDlg.ShowDialog();
if (result.HasValue && result.Value)
{
var filePath = openFileDlg.FileName;
try
{
BH.Engine.Base.Compute.ThrowErrorsAsExceptions(true);
BH.Engine.Settings.Compute.LoadSettingsFromFile(filePath);
BH.Engine.Base.Compute.ThrowErrorsAsExceptions(false);

var existingSettings = Query.GetSettings(typeof(BH.oM.UI.SearchSettings)) as SearchSettings;
if (existingSettings != null)
{
m_Settings = existingSettings;
existingSettings.Toolkits.ForEach(x =>
{
var item = m_ToolkitItems.Where(y => y.Toolkit == x.Toolkit).FirstOrDefault();
if (item == null)
{
var newModel = new ToolkitSelectItemModel(x);
ConvertToCheckbox(newModel);
m_ToolkitItems.Add(newModel);
}
else
item.Include = x.Include;
});

m_ToolkitItems = m_ToolkitItems.OrderBy(x => x.Toolkit).ToList();
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred in loading that settings file. Settings have not been loaded. The error recorded was {ex.Message}", "Error loading settings file.", MessageBoxButton.OK);
BH.Engine.Base.Compute.ThrowErrorsAsExceptions(false); //Just in case - belt and braces
}
}
}

/*************************************/
Expand All @@ -175,9 +213,15 @@ private void SelectAll(object sender, EventArgs e)
m_ToolkitItems.ForEach(x => x.Include = m_SelectAll);

if (m_SelectAll)
{
SelectAllBtn.Content = "Unselect all";
SelectAllBtn.ToolTip = "Clicking this will uncheck all toolkits currently selected.";
}
else
{
SelectAllBtn.Content = "Select all";
SelectAllBtn.ToolTip = "Clicking this will check all toolkits currently not selected.";
}
}

/*************************************/
Expand Down