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 ) ) ;
2018-05-17 12:42:13 -07:00
toolbar = new Toolbar ( theme , rightAnchorItem )
2018-04-17 17:13:18 -07:00
{
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-05-17 12:42:13 -07:00
var forcedHeight = printerSelector . Height ;
2018-04-20 13:55:31 -07:00
// add in the create printer button
2018-05-17 20:00:27 -07:00
var createPrinter = new IconButton ( AggContext . StaticData . LoadIcon ( "md-add-circle_18.png" , 18 , 18 , theme . InvertIcons ) , theme )
2018-04-20 13:55:31 -07:00
{
Name = "Create Printer" ,
VAnchor = VAnchor . Center ,
2018-05-17 12:42:13 -07:00
Margin = theme . ButtonSpacing . Clone ( left : theme . ButtonSpacing . Right ) ,
ToolTipText = "Create Printer" . Localize ( ) ,
Height = forcedHeight ,
Width = forcedHeight
2018-04-20 13:55:31 -07:00
} ;
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
2018-05-17 20:00:27 -07:00
var importPrinter = new IconButton ( AggContext . StaticData . LoadIcon ( "md-import_18.png" , 18 , 18 , theme . InvertIcons ) , theme )
2018-04-20 13:55:31 -07:00
{
VAnchor = VAnchor . Center ,
Margin = theme . ButtonSpacing ,
2018-05-17 12:42:13 -07:00
ToolTipText = "Import Printer" . Localize ( ) ,
Height = forcedHeight ,
Width = forcedHeight
2018-04-20 13:55:31 -07:00
} ;
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 (
2018-06-05 15:30:06 -07:00
new ImportSucceeded (
importPrinterSuccessMessage . FormatWith ( Path . GetFileNameWithoutExtension ( result . FileName ) ) ) ) ;
2018-04-20 13:55:31 -07:00
}
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 ( ) ;
var lastProfileID = ProfileManager . Instance . LastProfileID ;
var lastProfile = ProfileManager . Instance [ lastProfileID ] ;
if ( lastProfile = = null )
2018-04-19 15:54:55 -07:00
{
2018-04-25 16:42:40 -07:00
if ( ProfileManager . Instance . Profiles . Count > 0 )
2018-04-24 08:54:29 -07:00
{
2018-05-14 12:21:57 -07:00
toolbar . AddChild ( new TextWidget ( "Select a printer to continue" . Localize ( ) + "..." , textColor : theme . Colors . PrimaryTextColor , pointSize : theme . DefaultFontSize )
2018-04-25 16:42:40 -07:00
{
Margin = 15
} ) ;
}
else
{
2018-05-14 12:21:57 -07:00
toolbar . AddChild ( new TextWidget ( "Create a printer to continue" . Localize ( ) + "..." , textColor : theme . Colors . PrimaryTextColor , pointSize : theme . DefaultFontSize )
2018-04-25 16:42:40 -07:00
{
Margin = 15
} ) ;
}
2018-04-24 08:54:29 -07:00
}
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 ,
2018-05-17 20:00:08 -07:00
Name = "Open Empty Plate Button" ,
Cursor = Cursors . Hand
2018-04-24 08:54:29 -07:00
} ;
2018-04-30 07:12:02 -07:00
emptyPlateButton . Click + = ( s , e ) = >
2018-04-24 08:54:29 -07:00
{
2018-05-17 19:31:50 -07:00
if ( e . Button = = MouseButtons . Left )
2018-04-30 07:12:02 -07:00
{
2018-05-17 19:31:50 -07:00
UiThread . RunOnIdle ( async ( ) = >
{
var printer = await ProfileManager . Instance . LoadPrinter ( ) ;
printer . ViewState . ViewMode = PartViewMode . Model ;
2018-04-19 15:54:55 -07:00
2018-06-13 07:07:58 -07:00
// Load empty plate
await printer . Bed . LoadContent (
2018-05-17 19:31:50 -07:00
new EditContext ( )
{
ContentStore = ApplicationController . Instance . Library . PlatingHistory ,
SourceItem = BedConfig . NewPlatingItem ( ApplicationController . Instance . Library . PlatingHistory )
} ) ;
2018-06-13 07:07:58 -07:00
// Always switch to printer tab after changing plate
partPreviewContent . TabControl . SelectedTabIndex = 1 ;
2018-05-17 19:31:50 -07:00
} ) ;
}
2018-04-17 17:13:18 -07:00
} ;
2018-04-30 07:12:02 -07:00
2018-04-24 08:54:29 -07:00
toolbar . AddChild ( emptyPlateButton ) ;
2018-04-17 17:13:18 -07:00
2018-06-07 07:57:36 -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 ) ;
foreach ( var item in recentFiles . Where ( f = > f . Length > 500 ) . Select ( f = > new SceneReplacementFileItem ( f . FullName ) ) . Take ( 10 ) . ToList < ILibraryItem > ( ) )
2018-04-17 17:13:18 -07:00
{
2018-05-11 18:41:34 -07:00
var iconButton = new IconViewItem ( new ListViewItem ( item , ApplicationController . Instance . Library . PlatingHistory ) , 70 , 70 , theme )
2018-04-24 08:54:29 -07:00
{
Margin = new BorderDouble ( right : 5 ) ,
2018-05-17 20:00:08 -07:00
Selectable = true ,
Cursor = Cursors . Hand
2018-04-24 08:54:29 -07:00
} ;
2018-05-17 20:00:08 -07:00
iconButton . Click + = ( s , e ) = >
2018-04-17 17:13:18 -07:00
{
2018-05-17 19:31:50 -07:00
if ( this . PositionWithinLocalBounds ( e . X , e . Y )
& & e . Button = = MouseButtons . Left )
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 ;
printer . ViewState . ViewMode = PartViewMode . Model ;
2018-06-08 12:23:47 -07:00
2018-06-13 07:07:58 -07:00
// Always switch to printer tab after changing plate
2018-06-08 12:23:47 -07:00
partPreviewContent . TabControl . SelectedTabIndex = 1 ;
2018-04-24 08:54:29 -07:00
} ) ;
}
} ;
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 )
{
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-05-17 20:00:08 -07:00
Cursor = Cursors . Hand ,
2018-04-20 12:01:22 -07:00
Name = "Create Part Button"
2018-04-17 17:13:18 -07:00
} ;
2018-04-30 07:12:02 -07:00
emptyPlateButton . Click + = ( s , e ) = >
2018-04-17 17:13:18 -07:00
{
2018-05-17 19:31:50 -07:00
if ( e . Button = = MouseButtons . Left )
2018-04-17 17:13:18 -07:00
{
2018-05-17 19:31:50 -07:00
UiThread . RunOnIdle ( async ( ) = >
{
var workspace = new BedConfig ( ) ;
await workspace . LoadContent (
new EditContext ( )
{
ContentStore = ApplicationController . Instance . Library . PartHistory ,
SourceItem = BedConfig . NewPlatingItem ( ApplicationController . Instance . Library . PartHistory )
} ) ;
2018-05-03 23:00:02 -07:00
2018-05-17 19:31:50 -07:00
ApplicationController . Instance . Workspaces . Add ( workspace ) ;
2018-05-03 23:00:02 -07:00
2018-05-17 19:31:50 -07:00
partPreviewContent . CreatePartTab ( "New Part" , workspace , theme ) ;
} ) ;
}
2018-04-17 17:13:18 -07:00
} ;
2018-04-19 15:54:55 -07:00
toolbar . AddChild ( emptyPlateButton ) ;
2018-06-07 07:57:36 -07:00
var recentParts = new DirectoryInfo ( ApplicationDataStorage . Instance . PartHistoryDirectory ) . GetFiles ( "*.mcx" ) . OrderByDescending ( f = > f . LastWriteTime ) ;
foreach ( var item in recentParts . Where ( f = > f . Length > 500 ) . Select ( f = > new SceneReplacementFileItem ( f . FullName ) ) . Take ( 10 ) . ToList < ILibraryItem > ( ) )
2018-04-19 15:54:55 -07:00
{
2018-05-11 18:41:34 -07:00
var iconButton = new IconViewItem ( new ListViewItem ( item , ApplicationController . Instance . Library . PlatingHistory ) , 70 , 70 , theme )
2018-04-19 15:54:55 -07:00
{
2018-05-11 18:41:34 -07:00
Margin = new BorderDouble ( right : 5 ) ,
2018-05-17 20:00:08 -07:00
Selectable = true ,
2018-05-11 18:41:34 -07:00
} ;
2018-04-17 17:13:18 -07:00
2018-05-17 20:00:08 -07:00
iconButton . Click + = async ( s , e ) = >
2018-05-11 18:41:34 -07:00
{
2018-05-24 23:09:38 -07:00
// Activate selected item tab
if ( partPreviewContent . TabControl . AllTabs . FirstOrDefault ( t = > t . Text = = item . Name ) is ChromeTab existingItemTab )
2018-05-11 18:41:34 -07:00
{
2018-05-24 23:09:38 -07:00
partPreviewContent . TabControl . ActiveTab = existingItemTab ;
}
else
{
// Create tab for selected item
if ( this . PositionWithinLocalBounds ( e . X , e . Y )
& & e . Button = = MouseButtons . Left )
{
var workspace = new BedConfig ( ) ;
await workspace . LoadContent (
new EditContext ( )
{
ContentStore = ApplicationController . Instance . Library . PartHistory ,
SourceItem = item
} ) ;
2018-05-11 18:41:34 -07:00
2018-05-24 23:09:38 -07:00
ApplicationController . Instance . Workspaces . Add ( workspace ) ;
2018-05-11 18:41:34 -07:00
2018-05-24 23:09:38 -07:00
partPreviewContent . CreatePartTab ( item . Name , workspace , theme ) ;
}
2018-05-11 18:41:34 -07:00
}
} ;
toolbar . AddChild ( iconButton ) ;
}
2018-04-17 17:13:18 -07:00
}
}
}