2018-10-07 11:36:52 -07:00
/ *
Copyright ( c ) 2018 , John Lewin
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 .
* /
2018-10-12 10:55:49 -07:00
using Markdig.Agg ;
2018-10-07 11:36:52 -07:00
using MatterHackers.Agg ;
using MatterHackers.Agg.Image ;
using MatterHackers.Agg.UI ;
2018-10-12 10:55:49 -07:00
using MatterHackers.Agg.VertexSource ;
2018-10-07 11:36:52 -07:00
using MatterHackers.Localizations ;
2018-10-12 12:12:15 -07:00
using MatterHackers.MatterControl.ConfigurationPage ;
2018-10-07 11:36:52 -07:00
using MatterHackers.MatterControl.CustomWidgets ;
using MatterHackers.MatterControl.SettingsManagement ;
using MatterHackers.MatterControl.SlicerConfiguration ;
2018-10-12 10:55:49 -07:00
using Newtonsoft.Json ;
using System ;
using System.Diagnostics ;
using System.IO ;
using System.Net.Http ;
using System.Threading.Tasks ;
namespace MatterHackers.MatterControl.Library.Widgets.HardwarePage
2018-10-07 11:36:52 -07:00
{
public class PrinterDetails : FlowLayoutWidget
{
private ThemeConfig theme ;
2018-10-12 10:55:49 -07:00
private PrinterInfo printerInfo ;
2018-10-07 11:36:52 -07:00
public PrinterDetails ( PrinterInfo printerInfo , ThemeConfig theme )
2018-10-12 10:55:49 -07:00
: base ( FlowDirection . TopToBottom )
2018-10-07 11:36:52 -07:00
{
2018-10-12 10:55:49 -07:00
this . printerInfo = printerInfo ;
2018-10-07 11:36:52 -07:00
this . theme = theme ;
var headingRow = this . AddHeading (
OemSettings . Instance . GetIcon ( printerInfo . Make ) ,
printerInfo . Name ) ;
headingRow . AddChild ( new HorizontalSpacer ( ) ) ;
headingRow . HAnchor = HAnchor . Stretch ;
var openButton = new TextButton ( "Open" . Localize ( ) , theme )
{
BackgroundColor = theme . AccentMimimalOverlay
} ;
openButton . Click + = ( s , e ) = >
{
PrinterDetails . SwitchPrinters ( printerInfo . ID ) ;
} ;
headingRow . AddChild ( openButton ) ;
this . AddChild ( headingRow ) ;
2018-10-12 10:55:49 -07:00
}
2018-10-07 11:36:52 -07:00
2018-10-12 10:55:49 -07:00
public override async void OnLoad ( EventArgs args )
{
if ( File . Exists ( printerInfo . ProfilePath ) )
2018-10-07 11:36:52 -07:00
{
2018-10-12 10:55:49 -07:00
// load up the printer profile so we can get the MatterHackers Skew-ID out of it
var profile = PrinterSettings . LoadFile ( printerInfo . ProfilePath ) ;
2018-10-07 11:36:52 -07:00
2018-10-12 10:55:49 -07:00
// if there is a SID than get the data from that url and display it
2018-10-12 12:44:59 -07:00
var storeID = profile . GetValue ( SettingsKey . matterhackers_sid ) ;
if ( ! string . IsNullOrWhiteSpace ( storeID ) )
2018-10-12 10:55:49 -07:00
{
2018-10-12 12:44:59 -07:00
var product = ( await LoadProductData ( storeID ) ) . ProductSku ;
2018-10-12 10:55:49 -07:00
// put in controls from the feed that show relevant printer information
2018-10-12 12:12:15 -07:00
var row = new FlowLayoutWidget ( )
{
HAnchor = HAnchor . Stretch ,
Margin = new BorderDouble ( top : theme . DefaultContainerPadding )
} ;
2018-10-12 10:55:49 -07:00
this . AddChild ( row ) ;
2018-10-12 12:12:15 -07:00
var image = new ImageBuffer ( 150 , 10 ) ;
row . AddChild ( new ImageWidget ( image )
{
Margin = new BorderDouble ( right : theme . DefaultContainerPadding ) ,
VAnchor = VAnchor . Top
} ) ;
ApplicationController . Instance . DownloadToImageAsync ( image , product . FeaturedImage . ImageUrl , scaleToImageX : true ) ;
2018-10-12 10:55:49 -07:00
var descriptionBackground = new GuiWidget ( )
{
HAnchor = HAnchor . Stretch ,
2018-10-12 12:12:15 -07:00
VAnchor = VAnchor . Fit | VAnchor . Top ,
Padding = theme . DefaultContainerPadding
2018-10-12 10:55:49 -07:00
} ;
var description = new MarkdownWidget ( theme )
{
MinimumSize = new VectorMath . Vector2 ( 350 , 0 ) ,
HAnchor = HAnchor . Stretch ,
VAnchor = VAnchor . Fit ,
Markdown = product . ProductDescription . Trim ( )
} ;
descriptionBackground . AddChild ( description ) ;
descriptionBackground . BeforeDraw + = ( s , e ) = >
{
var rect = new RoundedRect ( descriptionBackground . LocalBounds , 3 ) ;
e . Graphics2D . Render ( rect , theme . SlightShade ) ;
} ;
2018-10-12 12:12:15 -07:00
row . AddChild ( descriptionBackground ) ;
2018-10-12 12:22:48 -07:00
var padding = theme . DefaultContainerPadding ;
2018-10-12 12:12:15 -07:00
var addonsColumn = new FlowLayoutWidget ( FlowDirection . TopToBottom )
{
2018-10-12 12:22:48 -07:00
Padding = new BorderDouble ( padding , padding , padding , 0 ) ,
2018-10-12 12:12:15 -07:00
HAnchor = HAnchor . Stretch
} ;
2018-10-12 12:22:48 -07:00
var addonsSection = new SectionWidget ( "Upgrades and Accessories" , addonsColumn , theme ) ;
this . AddChild ( addonsSection ) ;
theme . ApplyBoxStyle ( addonsSection ) ;
addonsSection . Margin = addonsSection . Margin . Clone ( left : 0 ) ;
2018-10-12 12:12:15 -07:00
foreach ( var item in product . ProductListing . AddOns )
{
var icon = new ImageBuffer ( 80 , 0 ) ;
ApplicationController . Instance . DownloadToImageAsync ( icon , item . FeaturedImage . ImageUrl , scaleToImageX : true ) ;
var link = new LinkLabel ( "More Info" , theme , pointSize : theme . DefaultFontSize )
{
VAnchor = VAnchor . Center
} ;
link . Click + = ( s , e ) = >
{
ApplicationController . Instance . LaunchBrowser ( $"https://www.matterhackers.com/store/l/{item.AddOnListingReference}/sk/{item.AddOnSkuReference}" ) ;
} ;
addonsColumn . AddChild ( new AddOnRow ( item . AddOnTitle , theme , link , icon )
{
HAnchor = HAnchor . Stretch ,
} ) ;
}
2018-10-12 10:55:49 -07:00
}
2018-10-07 11:36:52 -07:00
}
2018-10-12 10:55:49 -07:00
base . OnLoad ( args ) ;
}
2018-10-07 11:36:52 -07:00
2018-10-12 10:55:49 -07:00
public async Task < ProductSidData > LoadProductData ( string sid )
{
try
2018-10-07 11:36:52 -07:00
{
2018-10-12 10:55:49 -07:00
var client = new HttpClient ( ) ;
string json = await client . GetStringAsync ( $"https://mh-pls-prod.appspot.com/p/1/product-sid/{sid}?IncludeListingData=True" ) ;
2018-10-07 11:36:52 -07:00
2018-10-12 10:55:49 -07:00
var result = JsonConvert . DeserializeObject < ProductSidData > ( json ) ;
return result ;
}
catch ( Exception ex )
{
Trace . WriteLine ( "Error collecting or loading printer details: " + ex . Message ) ;
2018-10-07 11:36:52 -07:00
}
2018-10-12 10:55:49 -07:00
return null ;
2018-10-07 11:36:52 -07:00
}
public static void SwitchPrinters ( string printerID )
{
var activePrinter = ApplicationController . Instance . ActivePrinter ;
if ( printerID = = "new"
| | string . IsNullOrEmpty ( printerID )
| | printerID = = activePrinter . Settings . ID )
{
// do nothing
}
else
{
// TODO: when this opens a new tab we will not need to check any printer
if ( activePrinter . Connection . PrinterIsPrinting
| | activePrinter . Connection . PrinterIsPaused )
{
// TODO: Rather than block here, the UI elements driving the change should be disabled while printing/paused
UiThread . RunOnIdle ( ( ) = >
StyledMessageBox . ShowMessageBox ( "Please wait until the print has finished and try again." . Localize ( ) , "Can't switch printers while printing" . Localize ( ) )
) ;
}
else
{
ProfileManager . Instance . LastProfileID = printerID ;
2018-10-12 13:27:35 -07:00
ProfileManager . Instance . LoadPrinter ( ) . ContinueWith ( task = >
{
var printer = task . Result ;
// TODO: Alternatively we could hold and restore the Scene from the prior printer
printer . Bed . LoadPlateFromHistory ( ) . ConfigureAwait ( false ) ;
} ) ;
2018-10-07 11:36:52 -07:00
}
}
}
private GuiWidget AddHeading ( ImageBuffer icon , string text )
{
var row = new FlowLayoutWidget ( )
{
Margin = new BorderDouble ( top : 5 )
} ;
row . AddChild ( new ImageWidget ( icon , false )
{
Margin = new BorderDouble ( right : 4 ) ,
VAnchor = VAnchor . Center
} ) ;
row . AddChild (
new TextWidget (
text ,
textColor : theme . Colors . PrimaryTextColor ,
pointSize : theme . DefaultFontSize )
{
VAnchor = VAnchor . Center
} ) ;
return row ;
}
private GuiWidget AddHeading ( string text )
{
var row = new FlowLayoutWidget ( )
{
Margin = new BorderDouble ( top : 5 )
} ;
row . AddChild (
new TextWidget (
text ,
textColor : theme . Colors . PrimaryTextColor ,
pointSize : theme . DefaultFontSize ) ) ;
return row ;
}
2018-10-12 12:12:15 -07:00
private class AddOnRow : SettingsItem
{
public AddOnRow ( string text , ThemeConfig theme , GuiWidget optionalControls , ImageBuffer icon )
: base ( text , theme , null , optionalControls , icon )
{
imageWidget . Cursor = Cursors . Hand ;
imageWidget . Margin = 4 ;
imageWidget . Click + = ( s , e ) = >
{
optionalControls . InvokeClick ( ) ;
} ;
}
}
2018-10-07 11:36:52 -07:00
}
}