Moving more HTML code into its own classes.

This commit is contained in:
Lars Brubaker 2015-04-11 14:29:35 -07:00
parent fbce0036c4
commit 08abfe9930
7 changed files with 373 additions and 281 deletions

258
AboutPage/AboutWidget.cs Normal file
View file

@ -0,0 +1,258 @@
/*
Copyright (c) 2014, Lars Brubaker
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using MatterHackers.Agg;
using MatterHackers.Agg.Font;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.PlatformAbstract;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.ContactForm;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.HtmlParsing;
using MatterHackers.MatterControl.PrintLibrary;
using MatterHackers.MatterControl.PrintQueue;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
namespace MatterHackers.MatterControl
{
public class AboutWidget : GuiWidget
{
public AboutWidget()
{
this.HAnchor = HAnchor.ParentLeftRight;
this.VAnchor = VAnchor.ParentTop;
this.Padding = new BorderDouble(5);
this.BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
FlowLayoutWidget customInfoTopToBottom = new FlowLayoutWidget(FlowDirection.TopToBottom);
customInfoTopToBottom.Name = "AboutPageCustomInfo";
customInfoTopToBottom.HAnchor = HAnchor.ParentLeftRight;
customInfoTopToBottom.VAnchor = VAnchor.Max_FitToChildren_ParentHeight;
customInfoTopToBottom.Padding = new BorderDouble(5, 10, 5, 0);
customInfoTopToBottom.AddChild(new UpdateControlView());
//AddMatterHackersInfo(customInfoTopToBottom);
customInfoTopToBottom.AddChild(new GuiWidget(1, 10));
string aboutHtmlFile = Path.Combine("OEMSettings", "AboutPage.html");
string htmlContent = StaticData.Instance.ReadAllText(aboutHtmlFile);
string aboutHtmlFile2 = Path.Combine("OEMSettings", "AboutPage2.html");
//htmlContent = StaticData.Instance.ReadAllText(aboutHtmlFile2);
//htmlContent = File.ReadAllText("C:/Users/LarsBrubaker/Downloads/test.html");
HtmlWidget htmlWidget = new HtmlWidget(htmlContent, ActiveTheme.Instance.PrimaryTextColor);
customInfoTopToBottom.AddChild(htmlWidget);
this.AddChild(customInfoTopToBottom);
}
public static void DeleteCacheData()
{
// delete everything in the GCodeOutputPath
// AppData\Local\MatterControl\data\gcode
// delete everything in the temp data that is not in use
// AppData\Local\MatterControl\data\temp
// plateImages
// project-assembly
// project-extract
// stl
// delete all unreference models in Library
// AppData\Local\MatterControl\Library
// delete all old update downloads
// AppData\updates
// start cleaning out unused data
// MatterControl\data\gcode
RemoveDirectory(DataStorage.ApplicationDataStorage.Instance.GCodeOutputPath);
string userDataPath = DataStorage.ApplicationDataStorage.Instance.ApplicationUserDataPath;
RemoveDirectory(Path.Combine(userDataPath, "updates"));
HashSet<string> referencedPrintItemsFilePaths = new HashSet<string>();
HashSet<string> referencedThumbnailFiles = new HashSet<string>();
// Get a list of all the stl and amf files referenced in the queue or library.
foreach (PrintItemWrapper printItem in QueueData.Instance.PrintItems)
{
string fileLocation = printItem.FileLocation;
if (!referencedPrintItemsFilePaths.Contains(fileLocation))
{
referencedPrintItemsFilePaths.Add(fileLocation);
referencedThumbnailFiles.Add(PartThumbnailWidget.GetImageFilenameForItem(printItem));
}
}
foreach (PrintItemWrapper printItem in LibraryData.Instance.PrintItems)
{
string fileLocation = printItem.FileLocation;
if (!referencedPrintItemsFilePaths.Contains(fileLocation))
{
referencedPrintItemsFilePaths.Add(fileLocation);
referencedThumbnailFiles.Add(PartThumbnailWidget.GetImageFilenameForItem(printItem));
}
}
// If the count is less than 0 then we have never run and we need to populate the library and queue still. So don't delete anything yet.
if (referencedPrintItemsFilePaths.Count > 0)
{
CleanDirectory(userDataPath, referencedPrintItemsFilePaths, referencedThumbnailFiles);
}
}
public string CreateCenteredButton(string content)
{
throw new NotImplementedException();
}
public string CreateLinkButton(string content)
{
throw new NotImplementedException();
}
public string DoToUpper(string content)
{
throw new NotImplementedException();
}
public string DoTranslate(string content)
{
throw new NotImplementedException();
}
public string GetBuildString(string content)
{
return VersionInfo.Instance.BuildVersion;
}
public string GetVersionString(string content)
{
return VersionInfo.Instance.ReleaseVersion;
}
private static int CleanDirectory(string path, HashSet<string> referencedPrintItemsFilePaths, HashSet<string> referencedThumbnailFiles)
{
int contentCount = 0;
foreach (string directory in Directory.EnumerateDirectories(path))
{
int directoryContentCount = CleanDirectory(directory, referencedPrintItemsFilePaths, referencedThumbnailFiles);
if (directoryContentCount == 0)
{
try
{
Directory.Delete(directory);
}
catch (Exception)
{
}
}
else
{
// it has a directory that has content
contentCount++;
}
}
foreach (string file in Directory.EnumerateFiles(path, "*.*"))
{
switch (Path.GetExtension(file).ToUpper())
{
case ".STL":
case ".AMF":
case ".GCODE":
if (referencedPrintItemsFilePaths.Contains(file))
{
contentCount++;
}
else
{
try
{
File.Delete(file);
}
catch (Exception)
{
}
}
break;
case ".PNG":
case ".TGA":
if (referencedThumbnailFiles.Contains(file))
{
contentCount++;
}
else
{
try
{
File.Delete(file);
}
catch (Exception)
{
}
}
break;
case ".JSON":
// may want to clean these up eventually
contentCount++; // if we delete these we should not incement this
break;
default:
// we have something in the directory that we are not going to delete
contentCount++;
break;
}
}
return contentCount;
}
private static void RemoveDirectory(string directoryToRemove)
{
try
{
if (Directory.Exists(directoryToRemove))
{
Directory.Delete(directoryToRemove, true);
}
}
catch (Exception)
{
}
}
}
}

98
AboutPage/AboutWindow.cs Normal file
View file

@ -0,0 +1,98 @@
/*
Copyright (c) 2014, Lars Brubaker
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using MatterHackers.Agg;
using MatterHackers.Agg.Font;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.PlatformAbstract;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.ContactForm;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.HtmlParsing;
using MatterHackers.MatterControl.PrintLibrary;
using MatterHackers.MatterControl.PrintQueue;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
namespace MatterHackers.MatterControl
{
public class AboutWindow : SystemWindow
{
private static AboutWindow aboutWindow = null;
private TextImageButtonFactory textImageButtonFactory = new TextImageButtonFactory();
public AboutWindow()
: base(500, 640)
{
GuiWidget aboutPage = new AboutWidget();
aboutPage.AnchorAll();
this.AddChild(aboutPage);
FlowLayoutWidget buttonRowContainer = new FlowLayoutWidget();
buttonRowContainer.HAnchor = HAnchor.ParentLeftRight;
buttonRowContainer.Padding = new BorderDouble(0, 3);
AddChild(buttonRowContainer);
Button cancelButton = textImageButtonFactory.Generate("Close");
cancelButton.Click += (sender, e) => { CancelButton_Click(); };
buttonRowContainer.AddChild(new HorizontalSpacer());
buttonRowContainer.AddChild(cancelButton);
this.Title = LocalizedString.Get("About MatterControl");
this.ShowAsSystemWindow();
}
public static void Show()
{
if (aboutWindow == null)
{
aboutWindow = new AboutWindow();
aboutWindow.Closed += (parentSender, e) =>
{
aboutWindow = null;
};
}
else
{
aboutWindow.BringToFront();
}
}
private void CancelButton_Click()
{
UiThread.RunOnIdle((state) =>
{
aboutWindow.Close();
});
}
}
}

View file

@ -45,243 +45,28 @@ using System.Net;
namespace MatterHackers.MatterControl
{
public class HtmlWidget : GuiWidget
public class HtmlWidget : FlowLayoutWidget
{
}
public class AboutPage : HtmlWidget
{
private RGBA_Bytes aboutTextColor = ActiveTheme.Instance.PrimaryTextColor;
private Stack<GuiWidget> elementsUnderConstruction = new Stack<GuiWidget>();
private string htmlContent = null;
private LinkButtonFactory linkButtonFactory = new LinkButtonFactory();
private TextImageButtonFactory textImageButtonFactory = new TextImageButtonFactory();
public AboutPage()
private Stack<GuiWidget> elementsUnderConstruction = new Stack<GuiWidget>();
HtmlParser htmlParser = new HtmlParser();
public HtmlWidget(string htmlContent, RGBA_Bytes aboutTextColor)
: base(FlowDirection.TopToBottom)
{
this.HAnchor = HAnchor.ParentLeftRight;
this.VAnchor = VAnchor.ParentTop;
this.Padding = new BorderDouble(5);
this.BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
elementsUnderConstruction.Push(this);
linkButtonFactory.fontSize = 12;
linkButtonFactory.textColor = aboutTextColor;
textImageButtonFactory.normalFillColor = RGBA_Bytes.Gray;
textImageButtonFactory.normalTextColor = ActiveTheme.Instance.PrimaryTextColor;
FlowLayoutWidget customInfoTopToBottom = new FlowLayoutWidget(FlowDirection.TopToBottom);
customInfoTopToBottom.Name = "AboutPageCustomInfo";
customInfoTopToBottom.HAnchor = HAnchor.ParentLeftRight;
customInfoTopToBottom.VAnchor = VAnchor.Max_FitToChildren_ParentHeight;
customInfoTopToBottom.Padding = new BorderDouble(5, 10, 5, 0);
customInfoTopToBottom.AddChild(new UpdateControlView());
//AddMatterHackersInfo(customInfoTopToBottom);
customInfoTopToBottom.AddChild(new GuiWidget(1, 10));
HtmlParser htmlParser = new HtmlParser();
if (htmlContent == null)
{
string aboutHtmlFile = Path.Combine("OEMSettings", "AboutPage.html");
htmlContent = StaticData.Instance.ReadAllText(aboutHtmlFile);
string aboutHtmlFile2 = Path.Combine("OEMSettings", "AboutPage2.html");
//htmlContent = StaticData.Instance.ReadAllText(aboutHtmlFile2);
}
//htmlContent = File.ReadAllText("C:/Users/LarsBrubaker/Downloads/test.html");
elementsUnderConstruction.Push(new FlowLayoutWidget(FlowDirection.TopToBottom));
elementsUnderConstruction.Peek().Name = "container widget";
elementsUnderConstruction.Peek().VAnchor = VAnchor.Max_FitToChildren_ParentHeight;
elementsUnderConstruction.Peek().HAnchor |= HAnchor.ParentCenter;
htmlParser.ParseHtml(htmlContent, AddContent, CloseContent);
customInfoTopToBottom.AddChild(elementsUnderConstruction.Peek());
this.AddChild(customInfoTopToBottom);
}
public static void DeleteCacheData()
{
// delete everything in the GCodeOutputPath
// AppData\Local\MatterControl\data\gcode
// delete everything in the temp data that is not in use
// AppData\Local\MatterControl\data\temp
// plateImages
// project-assembly
// project-extract
// stl
// delete all unreference models in Library
// AppData\Local\MatterControl\Library
// delete all old update downloads
// AppData\updates
// start cleaning out unused data
// MatterControl\data\gcode
RemoveDirectory(DataStorage.ApplicationDataStorage.Instance.GCodeOutputPath);
string userDataPath = DataStorage.ApplicationDataStorage.Instance.ApplicationUserDataPath;
RemoveDirectory(Path.Combine(userDataPath, "updates"));
HashSet<string> referencedPrintItemsFilePaths = new HashSet<string>();
HashSet<string> referencedThumbnailFiles = new HashSet<string>();
// Get a list of all the stl and amf files referenced in the queue or library.
foreach (PrintItemWrapper printItem in QueueData.Instance.PrintItems)
{
string fileLocation = printItem.FileLocation;
if (!referencedPrintItemsFilePaths.Contains(fileLocation))
{
referencedPrintItemsFilePaths.Add(fileLocation);
referencedThumbnailFiles.Add(PartThumbnailWidget.GetImageFilenameForItem(printItem));
}
}
foreach (PrintItemWrapper printItem in LibraryData.Instance.PrintItems)
{
string fileLocation = printItem.FileLocation;
if (!referencedPrintItemsFilePaths.Contains(fileLocation))
{
referencedPrintItemsFilePaths.Add(fileLocation);
referencedThumbnailFiles.Add(PartThumbnailWidget.GetImageFilenameForItem(printItem));
}
}
// If the count is less than 0 then we have never run and we need to populate the library and queue still. So don't delete anything yet.
if (referencedPrintItemsFilePaths.Count > 0)
{
CleanDirectory(userDataPath, referencedPrintItemsFilePaths, referencedThumbnailFiles);
}
}
public string CreateCenteredButton(string content)
{
throw new NotImplementedException();
}
public string CreateLinkButton(string content)
{
throw new NotImplementedException();
}
public string DoToUpper(string content)
{
throw new NotImplementedException();
}
public string DoTranslate(string content)
{
throw new NotImplementedException();
}
public string GetBuildString(string content)
{
return VersionInfo.Instance.BuildVersion;
}
public string GetVersionString(string content)
{
return VersionInfo.Instance.ReleaseVersion;
}
private static int CleanDirectory(string path, HashSet<string> referencedPrintItemsFilePaths, HashSet<string> referencedThumbnailFiles)
{
int contentCount = 0;
foreach (string directory in Directory.EnumerateDirectories(path))
{
int directoryContentCount = CleanDirectory(directory, referencedPrintItemsFilePaths, referencedThumbnailFiles);
if (directoryContentCount == 0)
{
try
{
Directory.Delete(directory);
}
catch (Exception)
{
}
}
else
{
// it has a directory that has content
contentCount++;
}
}
foreach (string file in Directory.EnumerateFiles(path, "*.*"))
{
switch (Path.GetExtension(file).ToUpper())
{
case ".STL":
case ".AMF":
case ".GCODE":
if (referencedPrintItemsFilePaths.Contains(file))
{
contentCount++;
}
else
{
try
{
File.Delete(file);
}
catch (Exception)
{
}
}
break;
case ".PNG":
case ".TGA":
if (referencedThumbnailFiles.Contains(file))
{
contentCount++;
}
else
{
try
{
File.Delete(file);
}
catch (Exception)
{
}
}
break;
case ".JSON":
// may want to clean these up eventually
contentCount++; // if we delete these we should not incement this
break;
default:
// we have something in the directory that we are not going to delete
contentCount++;
break;
}
}
return contentCount;
}
private static void RemoveDirectory(string directoryToRemove)
{
try
{
if (Directory.Exists(directoryToRemove))
{
Directory.Delete(directoryToRemove, true);
}
}
catch (Exception)
{
}
VAnchor |= VAnchor.ParentBottomTop;
HAnchor |= HAnchor.ParentLeftRight;
}
private void AddContent(HtmlParser htmlParser, string htmlContent)
@ -307,7 +92,7 @@ namespace MatterHackers.MatterControl
ImageBuffer image = new ImageBuffer(elementState.SizeFixed.x, elementState.SizeFixed.y, 32, new BlenderBGRA());
ImageWidget_AsyncLoadOnDraw imageWidget = new ImageWidget_AsyncLoadOnDraw(image, elementState.src);
// put the image into the widget when it is done downloading.
if (elementsUnderConstruction.Peek().Name == "a")
{
Button linkButton = new Button(0, 0, imageWidget);
@ -400,7 +185,7 @@ namespace MatterHackers.MatterControl
}
else if (elementState.Id == "clearCache")
{
createdButton.Click += (sender, mouseEvent) => { DeleteCacheData(); };
createdButton.Click += (sender, mouseEvent) => { AboutWidget.DeleteCacheData(); };
}
}
@ -471,55 +256,4 @@ namespace MatterHackers.MatterControl
}
}
}
public class AboutWindow : SystemWindow
{
private static AboutWindow aboutWindow = null;
private TextImageButtonFactory textImageButtonFactory = new TextImageButtonFactory();
public AboutWindow()
: base(500, 640)
{
GuiWidget aboutPage = new AboutPage();
aboutPage.AnchorAll();
this.AddChild(aboutPage);
FlowLayoutWidget buttonRowContainer = new FlowLayoutWidget();
buttonRowContainer.HAnchor = HAnchor.ParentLeftRight;
buttonRowContainer.Padding = new BorderDouble(0, 3);
AddChild(buttonRowContainer);
Button cancelButton = textImageButtonFactory.Generate("Close");
cancelButton.Click += (sender, e) => { CancelButton_Click(); };
buttonRowContainer.AddChild(new HorizontalSpacer());
buttonRowContainer.AddChild(cancelButton);
this.Title = LocalizedString.Get("About MatterControl");
this.ShowAsSystemWindow();
}
public static void Show()
{
if (aboutWindow == null)
{
aboutWindow = new AboutWindow();
aboutWindow.Closed += (parentSender, e) =>
{
aboutWindow = null;
};
}
else
{
aboutWindow.BringToFront();
}
}
private void CancelButton_Click()
{
UiThread.RunOnIdle((state) =>
{
aboutWindow.Close();
});
}
}
}

View file

@ -132,7 +132,7 @@ namespace MatterHackers.MatterControl
this.AddTab(new SimpleTextTabWidget(configurationPage, "Configuration Tab", TabTextSize,
ActiveTheme.Instance.SecondaryAccentColor, new RGBA_Bytes(), unselectedTextColor, new RGBA_Bytes()));
AboutTabPage = new TabPage(new AboutPage(), LocalizedString.Get("About").ToUpper());
AboutTabPage = new TabPage(new AboutWidget(), LocalizedString.Get("About").ToUpper());
aboutTabWidget = new SimpleTextTabWidget(AboutTabPage, "About Tab", TabTextSize,
ActiveTheme.Instance.SecondaryAccentColor, new RGBA_Bytes(), unselectedTextColor, new RGBA_Bytes());
this.AddTab(aboutTabWidget);

View file

@ -75,7 +75,7 @@ namespace MatterHackers.MatterControl
this.AddTab(new SimpleTextTabWidget(HistoryTabPage, "History Tab", 15,
ActiveTheme.Instance.TabLabelSelected, new RGBA_Bytes(), unselectedTextColor, new RGBA_Bytes()));
AboutTabPage = new TabPage(new AboutPage(), LocalizedString.Get("About").ToUpper());
AboutTabPage = new TabPage(new AboutWidget(), LocalizedString.Get("About").ToUpper());
AboutTabView = new SimpleTextTabWidget(AboutTabPage, "About Tab", 15,
ActiveTheme.Instance.TabLabelSelected, new RGBA_Bytes(), unselectedTextColor, new RGBA_Bytes());
this.AddTab(AboutTabView);

View file

@ -120,6 +120,8 @@
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Compile Include="AboutPage\AboutWindow.cs" />
<Compile Include="AboutPage\HTMLParser\HtmlWidget.cs" />
<Compile Include="AboutPage\HTMLParser\ImageWidget_AsyncLoadOnDraw.cs" />
<Compile Include="AboutPage\HTMLParser\ElementState.cs" />
<Compile Include="AboutPage\UpdateControlData.cs" />
@ -321,7 +323,7 @@
<Compile Include="Utilities\TupleList.cs" />
<Compile Include="VersionManagement\WebRequestHandler.cs" />
<Compile Include="VersionManagement\VersionFileHandler.cs" />
<Compile Include="AboutPage\AboutPage.cs" />
<Compile Include="AboutPage\AboutWidget.cs" />
<Compile Include="CustomWidgets\ThemeColorSelectorWidget.cs" />
<Compile Include="ControlElements\TextImageButtonFactory.cs" />
<Compile Include="EeProm\EePromMarlinWindow.cs" />

View file

@ -146,7 +146,7 @@ namespace MatterHackers.MatterControl
return;
case "CLEAR_CACHE":
AboutPage.DeleteCacheData();
AboutWidget.DeleteCacheData();
break;
case "SHOW_MEMORY":