Skip to content

Commit

Permalink
Model selection tree improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
n00mkrad committed Apr 6, 2021
1 parent 8a3000e commit 57b1b6a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
27 changes: 23 additions & 4 deletions Code/Forms/ModelSelectForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public partial class ModelSelectForm : Form
public ModelSelectForm(Button modelButton, int modelNumber)
{
InitializeComponent();
//Show(this);
//TopMost = true;
//CenterToScreen();
modelBtn = modelButton;
modelNo = modelNumber;
SelectLastUsed();
Expand All @@ -32,13 +29,25 @@ public ModelSelectForm(Button modelButton, int modelNumber)
private void ModelSelectForm_Load(object sender, EventArgs e)
{
string modelDir = Config.Get("modelPath");

if (!Directory.Exists(modelDir))
{
Program.ShowMessage("The saved model directory does not exist - Make sure you've set a models folder!");
Close();
return;
}

if (IOUtils.GetAmountOfFiles(modelDir, true, "*.pth") < 1)
{
Program.ShowMessage($"The saved model directory does not contain any model (.pth) files!\n\nPlease put some models into '{modelDir}'.");
Close();
return;
}

ForceLowercaseExtensions(modelDir);
DirectoryInfo modelsDir = new DirectoryInfo(modelDir);
BuildTree(modelsDir, modelTree.Nodes);

if (Config.GetBool("modelSelectAutoExpand"))
modelTree.ExpandAll();
else
Expand All @@ -51,7 +60,7 @@ private async void SelectLastUsed()
return;

while (modelTree.Nodes.Count < 1)
await Task.Delay(1);
await Task.Delay(100);

if (string.IsNullOrWhiteSpace(Program.currentModel1))
modelTree.SelectedNode = modelTree.Nodes[0];
Expand All @@ -70,6 +79,15 @@ private void CheckNodesRecursive(TreeNode parentNode)
}
}

private void ForceLowercaseExtensions(string path)
{
foreach (FileInfo file in IOUtils.GetFileInfosSorted(path, true, "*.*"))
{
if(file.Extension == ".PTH")
file.MoveTo(Path.ChangeExtension(file.FullName, "pth"));
}
}

private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe)
{
TreeNode curNode = addInMe.Add(directoryInfo.Name);
Expand All @@ -79,6 +97,7 @@ private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe)
if (file.Extension == ".pth") // Hide any other file extension
curNode.Nodes.Add(file.FullName, Path.ChangeExtension(file.Name, null));
}

foreach (DirectoryInfo subdir in directoryInfo.GetDirectories())
{
if (subdir.GetFiles("*.pth", SearchOption.AllDirectories).Length > 0) // Don't list folders that have no PTH files
Expand Down
23 changes: 23 additions & 0 deletions Code/IO/IOUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,5 +493,28 @@ public static bool HasEnoughDiskSpace(int mBytes, string drivePath, float multip
return true;
return false;
}

public static string[] GetFilesSorted(string path, bool recursive = false, string pattern = "*")
{
SearchOption opt = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
return Directory.GetFiles(path, pattern, opt).OrderBy(x => Path.GetFileName(x)).ToArray();
}

public static string[] GetFilesSorted(string path, string pattern = "*")
{
return GetFilesSorted(path, false, pattern);
}

public static string[] GetFilesSorted(string path)
{
return GetFilesSorted(path, false, "*");
}

public static FileInfo[] GetFileInfosSorted(string path, bool recursive = false, string pattern = "*")
{
SearchOption opt = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
DirectoryInfo dir = new DirectoryInfo(path);
return dir.GetFiles(pattern, opt).OrderBy(x => x.Name).ToArray();
}
}
}

0 comments on commit 57b1b6a

Please sign in to comment.