2018-04-17 17:13:18 -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 .
* /
using System ;
using System.IO ;
using System.Linq ;
using MatterHackers.Agg ;
using MatterHackers.Agg.Platform ;
using MatterHackers.Agg.UI ;
2018-04-19 15:54:55 -07:00
using MatterHackers.DataConverters3D ;
2018-04-17 17:13:18 -07:00
using MatterHackers.Localizations ;
using MatterHackers.MatterControl.CustomWidgets ;
using MatterHackers.MatterControl.DataStorage ;
using MatterHackers.MatterControl.Library ;
using MatterHackers.MatterControl.PrinterControls.PrinterConnections ;
using MatterHackers.MatterControl.SlicerConfiguration ;
using MatterHackers.VectorMath ;
namespace MatterHackers.MatterControl.PartPreviewWindow.PlusTab
{
public class ExplorerBar : FlowLayoutWidget
{
protected Toolbar toolbar ;
protected FlowLayoutWidget headingBar ;
public ExplorerBar ( string text , ThemeConfig theme , GuiWidget rightAnchorItem = null )
: base ( FlowDirection . TopToBottom )
{
this . HAnchor = HAnchor . Stretch ;
this . VAnchor = VAnchor . Fit ;
this . Margin = new BorderDouble ( 30 , 0 , 30 , 15 ) ;
headingBar = new FlowLayoutWidget ( )
{
} ;
this . AddChild ( headingBar ) ;
headingBar . AddChild ( theme . CreateHeading ( text ) ) ;
toolbar = new Toolbar ( rightAnchorItem )
{
HAnchor = HAnchor . Stretch ,
VAnchor = VAnchor . Fit ,
Padding = 8 ,
BackgroundColor = theme . MinimalShade
} ;
this . AddChild ( toolbar ) ;
}
}
public class PrinterBar : ExplorerBar
{
private PrinterInfo printerInfo ;
2018-04-24 08:54:29 -07:00
private EventHandler unregisterEvents ;
private PrinterSelector printerSelector ;
2018-04-17 17:13:18 -07:00
public PrinterBar ( PartPreviewContent partPreviewContent , PrinterInfo printerInfo , ThemeConfig theme )
2018-04-18 17:04:37 -07:00
: base ( printerInfo ? . Name ? ? "" , theme )
2018-04-17 17:13:18 -07:00
{
headingBar . CloseAllChildren ( ) ;
2018-04-24 08:54:29 -07:00
headingBar . AddChild ( printerSelector = new PrinterSelector ( theme )
2018-04-17 17:13:18 -07:00
{
VAnchor = VAnchor . Fit ,
HAnchor = HAnchor . Absolute ,
Border = 0 ,
MinimumSize = Vector2 . Zero ,
2018-04-19 15:54:55 -07:00
Width = 200 ,
BackgroundColor = theme . MinimalShade
2018-04-17 17:13:18 -07:00
} ) ;
2018-04-24 08:54:29 -07:00
printerSelector . SelectionChanged + = ( s , e ) = >
{
this . RebuildPlateOptions ( partPreviewContent , theme ) ;
} ;
2018-04-20 13:55:31 -07:00
// add in the create printer button
var createPrinter = new IconButton ( AggContext . StaticData . LoadIcon ( "icon_circle_plus.png" , 16 , 16 , theme . InvertIcons ) , theme )
{
Name = "Create Printer" ,
VAnchor = VAnchor . Center ,
Margin = theme . ButtonSpacing ,
ToolTipText = "Create Printer" . Localize ( )
} ;
createPrinter . Click + = ( s , e ) = >
{
UiThread . RunOnIdle ( ( ) = >
{
//simpleTabs.RemoveTab(simpleTabs.ActiveTab);
if ( ApplicationController . Instance . ActivePrinter . Connection . PrinterIsPrinting
| | ApplicationController . Instance . ActivePrinter . Connection . PrinterIsPaused )
{
StyledMessageBox . ShowMessageBox ( "Please wait until the print has finished and try again." . Localize ( ) , "Can't add printers while printing" . Localize ( ) ) ;
}
else
{
DialogWindow . Show ( PrinterSetup . GetBestStartPage ( PrinterSetup . StartPageOptions . ShowMakeModel ) ) ;
}
} ) ;
} ;
headingBar . AddChild ( createPrinter ) ;
// add in the import printer button
var importPrinter = new IconButton ( AggContext . StaticData . LoadIcon ( "icon_import_white_full.png" , 16 , 16 , theme . InvertIcons ) , theme )
{
VAnchor = VAnchor . Center ,
Margin = theme . ButtonSpacing ,
ToolTipText = "Import Printer" . Localize ( )
} ;
importPrinter . Click + = ( s , e ) = >
{
UiThread . RunOnIdle ( ( ) = >
{
AggContext . FileDialogs . OpenFileDialog (
new OpenFileDialogParams (
"settings files|*.ini;*.printer;*.slice" ) ,
( result ) = >
{
if ( ! string . IsNullOrEmpty ( result . FileName )
& & File . Exists ( result . FileName ) )
{
//simpleTabs.RemoveTab(simpleTabs.ActiveTab);
if ( ProfileManager . ImportFromExisting ( result . FileName ) )
{
string importPrinterSuccessMessage = "You have successfully imported a new printer profile. You can find '{0}' in your list of available printers." . Localize ( ) ;
DialogWindow . Show (
new ImportSucceeded ( importPrinterSuccessMessage . FormatWith ( Path . GetFileNameWithoutExtension ( result . FileName ) ) ) ) ;
}
else
{
StyledMessageBox . ShowMessageBox ( "Oops! Settings file '{0}' did not contain any settings we could import." . Localize ( ) . FormatWith ( Path . GetFileName ( result . FileName ) ) , "Unable to Import" . Localize ( ) ) ;
}
}
} ) ;
} ) ;
} ;
headingBar . AddChild ( importPrinter ) ;
2018-04-17 17:13:18 -07:00
this . printerInfo = printerInfo ;
2018-04-24 08:54:29 -07:00
this . RebuildPlateOptions ( partPreviewContent , theme ) ;
// Rebuild on change
ProfileManager . ProfilesListChanged . RegisterEvent ( ( s , e ) = >
{
this . RebuildPlateOptions ( partPreviewContent , theme ) ;
} , ref unregisterEvents ) ;
}
private void RebuildPlateOptions ( PartPreviewContent partPreviewContent , ThemeConfig theme )
{
toolbar . ActionArea . CloseAllChildren ( ) ;
2018-04-17 17:13:18 -07:00
// Select the 25 most recent files and project onto FileSystemItems
var recentFiles = new DirectoryInfo ( ApplicationDataStorage . Instance . PlatingDirectory ) . GetFiles ( "*.mcx" ) . OrderByDescending ( f = > f . LastWriteTime ) ;
2018-04-24 08:11:24 -07:00
// HACK: Creating a listview just to generate part thumbnails is invalid. Rework thumbnail generation so we have a solution for this case
2018-04-17 17:13:18 -07:00
var listView = new ListView ( ApplicationController . Instance . Library , theme ) ;
2018-04-24 08:54:29 -07:00
var lastProfileID = ProfileManager . Instance . LastProfileID ;
var lastProfile = ProfileManager . Instance [ lastProfileID ] ;
if ( lastProfile = = null )
2018-04-19 15:54:55 -07:00
{
2018-04-24 08:54:29 -07:00
toolbar . AddChild ( new TextWidget ( "Select a printer to continue" . Localize ( ) + "..." , pointSize : theme . DefaultFontSize )
{
Margin = 15
} ) ;
}
else
2018-04-19 15:54:55 -07:00
{
2018-04-24 08:54:29 -07:00
var emptyPlateButton = new ImageWidget ( AggContext . StaticData . LoadIcon ( "empty-workspace.png" , 70 , 70 ) )
{
Margin = new BorderDouble ( right : 5 ) ,
Selectable = true ,
BackgroundColor = theme . MinimalShade ,
Name = "Open Empty Plate Button"
} ;
emptyPlateButton . Click + = async ( s , e ) = >
{
var printer = await ProfileManager . Instance . LoadPrinter ( ) ;
2018-04-20 14:05:58 -07:00
2018-04-24 08:54:29 -07:00
printer . ViewState . ViewMode = PartViewMode . Model ;
2018-04-19 15:54:55 -07:00
2018-04-24 08:54:29 -07:00
await printer . Bed . ClearPlate ( ) ;
2018-04-19 15:54:55 -07:00
2018-04-24 08:54:29 -07:00
string printerName = printer . Settings . GetValue ( SettingsKey . printer_name ) ;
2018-04-19 15:54:55 -07:00
2018-04-24 08:54:29 -07:00
partPreviewContent . CreatePrinterTab ( printer , theme , printerName ) ;
2018-04-17 17:13:18 -07:00
} ;
2018-04-24 08:54:29 -07:00
toolbar . AddChild ( emptyPlateButton ) ;
2018-04-17 17:13:18 -07:00
2018-04-24 08:54:29 -07:00
foreach ( var item in recentFiles . Take ( 10 ) . Select ( f = > new SceneReplacementFileItem ( f . FullName ) ) . ToList < ILibraryItem > ( ) )
2018-04-17 17:13:18 -07:00
{
2018-04-24 08:54:29 -07:00
var iconButton = new IconViewItem ( new ListViewItem ( item , listView ) , 70 , 70 , theme )
{
Margin = new BorderDouble ( right : 5 ) ,
Selectable = true
} ;
iconButton . Children . First ( ) . Click + = ( s , e ) = >
2018-04-17 17:13:18 -07:00
{
2018-04-24 08:54:29 -07:00
if ( this . PositionWithinLocalBounds ( e . X , e . Y ) )
2018-04-17 17:13:18 -07:00
{
2018-04-24 08:54:29 -07:00
UiThread . RunOnIdle ( async ( ) = >
{
await ProfileManager . Instance . LoadPrinterOpenItem ( item ) ;
2018-04-17 17:13:18 -07:00
2018-04-24 08:54:29 -07:00
var printer = ApplicationController . Instance . ActivePrinter ;
2018-04-17 17:13:18 -07:00
2018-04-24 08:54:29 -07:00
printer . ViewState . ViewMode = PartViewMode . Model ;
2018-04-20 14:05:58 -07:00
2018-04-24 08:54:29 -07:00
partPreviewContent . CreatePrinterTab (
printer ,
theme ,
printer . Settings . GetValue ( SettingsKey . printer_name ) ) ;
} ) ;
}
} ;
2018-04-17 17:13:18 -07:00
2018-04-24 08:54:29 -07:00
toolbar . AddChild ( iconButton ) ;
}
2018-04-17 17:13:18 -07:00
}
}
2018-04-24 08:54:29 -07:00
public override void OnClosed ( ClosedEventArgs e )
2018-04-17 17:13:18 -07:00
{
2018-04-24 08:54:29 -07:00
unregisterEvents ? . Invoke ( this , null ) ;
base . OnClosed ( e ) ;
2018-04-17 17:13:18 -07:00
}
}
public class PartsBar : ExplorerBar
{
public PartsBar ( PartPreviewContent partPreviewContent , ThemeConfig theme )
: base ( "Parts" . Localize ( ) , theme )
{
// ** Mock recent parts **
var recentParts = new DirectoryInfo ( ApplicationDataStorage . Instance . PlatingDirectory ) . GetFiles ( "*.mcx" ) . OrderBy ( f = > f . LastWriteTime ) ;
var listView = new ListView ( ApplicationController . Instance . Library , theme ) ;
2018-04-24 08:55:05 -07:00
var emptyPlateButton = new ImageWidget ( AggContext . StaticData . LoadIcon ( "new-part.png" , 70 , 70 ) )
2018-04-17 17:13:18 -07:00
{
2018-04-19 15:54:55 -07:00
Margin = new BorderDouble ( right : 5 ) ,
Selectable = true ,
BackgroundColor = theme . MinimalShade ,
2018-04-20 12:01:22 -07:00
Name = "Create Part Button"
2018-04-17 17:13:18 -07:00
} ;
2018-04-19 15:54:55 -07:00
emptyPlateButton . Click + = async ( s , e ) = >
2018-04-17 17:13:18 -07:00
{
UiThread . RunOnIdle ( ( ) = >
{
BedConfig bed ;
//simpleTabs.RemoveTab(simpleTabs.ActiveTab);
partPreviewContent . CreatePartTab (
"New Part" ,
bed = new BedConfig ( ) ,
theme ) ;
bed . LoadContent (
new EditContext ( )
{
ContentStore = ApplicationController . Instance . Library . PlatingHistory ,
SourceItem = BedConfig . NewPlatingItem ( )
} ) . ConfigureAwait ( false ) ;
} ) ;
} ;
2018-04-19 15:54:55 -07:00
toolbar . AddChild ( emptyPlateButton ) ;
foreach ( var item in recentParts . Take ( 10 ) . Select ( f = > new SceneReplacementFileItem ( f . FullName ) ) . ToList < ILibraryItem > ( ) )
{
toolbar . AddChild ( new IconViewItem ( new ListViewItem ( item , listView ) , 70 , 70 , theme )
{
Margin = new BorderDouble ( right : 5 )
} ) ;
}
2018-04-17 17:13:18 -07:00
}
}
}