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 .
* /
using System ;
using System.IO ;
using System.Linq ;
using MatterHackers.Agg ;
using MatterHackers.Agg.Platform ;
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
using MatterHackers.MatterControl.CustomWidgets ;
using MatterHackers.MatterControl.PrinterControls.PrinterConnections ;
using MatterHackers.MatterControl.SettingsManagement ;
using MatterHackers.MatterControl.SlicerConfiguration ;
namespace MatterHackers.MatterControl.PrintLibrary
{
public class InventoryTreeView : TreeView
{
private TreeNode printersNode ;
private FlowLayoutWidget rootColumn ;
private EventHandler unregisterEvents ;
public InventoryTreeView ( ThemeConfig theme )
: base ( theme )
{
2018-10-23 11:52:10 -07:00
rootColumn = new FlowLayoutWidget ( FlowDirection . TopToBottom )
2018-10-07 11:36:52 -07:00
{
2018-10-23 11:52:10 -07:00
HAnchor = HAnchor . Stretch ,
VAnchor = VAnchor . Fit
} ;
this . AddChild ( rootColumn ) ;
2018-10-07 11:36:52 -07:00
2018-10-23 11:52:10 -07:00
// Printers
printersNode = new TreeNode ( theme )
{
Text = "Printers" . Localize ( ) ,
HAnchor = HAnchor . Stretch ,
AlwaysExpandable = true ,
Image = AggContext . StaticData . LoadIcon ( "printer.png" , 16 , 16 , theme . InvertIcons )
} ;
printersNode . TreeView = this ;
var forcedHeight = 20 ;
var mainRow = printersNode . Children . FirstOrDefault ( ) ;
mainRow . HAnchor = HAnchor . Stretch ;
mainRow . AddChild ( new HorizontalSpacer ( ) ) ;
// add in the create printer button
var createPrinter = new IconButton ( AggContext . StaticData . LoadIcon ( "md-add-circle_18.png" , 18 , 18 , theme . InvertIcons ) , theme )
{
Name = "Create Printer" ,
VAnchor = VAnchor . Center ,
Margin = theme . ButtonSpacing . Clone ( left : theme . ButtonSpacing . Right ) ,
ToolTipText = "Create Printer" . Localize ( ) ,
Height = forcedHeight ,
Width = forcedHeight
} ;
createPrinter . Click + = ( s , e ) = > UiThread . RunOnIdle ( ( ) = >
{
2018-11-11 13:45:48 -08:00
if ( ApplicationController . Instance . AnyPrintTaskRunning )
2018-10-07 11:36:52 -07:00
{
2018-10-23 11:52:10 -07:00
StyledMessageBox . ShowMessageBox ( "Please wait until the print has finished and try again." . Localize ( ) , "Can't add printers while printing" . Localize ( ) ) ;
}
else
2018-10-07 11:36:52 -07:00
{
2018-10-23 11:52:10 -07:00
DialogWindow . Show ( PrinterSetup . GetBestStartPage ( PrinterSetup . StartPageOptions . ShowMakeModel ) ) ;
}
} ) ;
mainRow . AddChild ( createPrinter ) ;
2018-10-07 11:36:52 -07:00
2018-10-23 11:52:10 -07:00
// add in the import printer button
var importPrinter = new IconButton ( AggContext . StaticData . LoadIcon ( "md-import_18.png" , 18 , 18 , theme . InvertIcons ) , theme )
{
VAnchor = VAnchor . Center ,
Margin = theme . ButtonSpacing ,
ToolTipText = "Import Printer" . Localize ( ) ,
Height = forcedHeight ,
Width = forcedHeight
} ;
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 ) )
2018-10-07 11:36:52 -07:00
{
2018-10-23 11:52:10 -07:00
//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
2018-10-07 11:36:52 -07:00
{
2018-10-23 11:52:10 -07:00
StyledMessageBox . ShowMessageBox ( "Oops! Settings file '{0}' did not contain any settings we could import." . Localize ( ) . FormatWith ( Path . GetFileName ( result . FileName ) ) , "Unable to Import" . Localize ( ) ) ;
2018-10-07 11:36:52 -07:00
}
2018-10-23 11:52:10 -07:00
}
} ) ;
} ) ;
mainRow . AddChild ( importPrinter ) ;
2018-10-07 11:36:52 -07:00
2018-10-23 11:52:10 -07:00
rootColumn . AddChild ( printersNode ) ;
2018-10-07 11:36:52 -07:00
2018-10-23 11:52:10 -07:00
InventoryTreeView . RebuildPrintersList ( printersNode , theme ) ;
this . Invalidate ( ) ;
2018-10-07 11:36:52 -07:00
2018-10-23 11:52:10 -07:00
// Filament
var materialsNode = new TreeNode ( theme )
{
Text = "Materials" . Localize ( ) ,
AlwaysExpandable = true ,
Image = AggContext . StaticData . LoadIcon ( "filament.png" , 16 , 16 , theme . InvertIcons )
} ;
materialsNode . TreeView = this ;
2018-10-07 11:36:52 -07:00
2018-10-23 11:52:10 -07:00
rootColumn . AddChild ( materialsNode ) ;
2018-10-07 11:36:52 -07:00
PrinterSettings . SettingChanged . RegisterEvent ( ( s , e ) = >
{
var activePrinter = ApplicationController . Instance . ActivePrinter ;
string settingsName = ( e as StringEventArgs ) ? . Data ;
if ( settingsName ! = null & & settingsName = = SettingsKey . printer_name )
{
if ( ProfileManager . Instance . ActiveProfile ! = null )
{
ProfileManager . Instance . ActiveProfile . Name = activePrinter . Settings . GetValue ( SettingsKey . printer_name ) ;
2018-10-16 15:54:44 -07:00
InventoryTreeView . RebuildPrintersList ( printersNode , theme ) ;
this . Invalidate ( ) ;
2018-10-07 11:36:52 -07:00
}
}
} , ref unregisterEvents ) ;
PrinterSettings . SettingChanged . RegisterEvent ( ( s , e ) = >
{
string settingsName = ( e as StringEventArgs ) ? . Data ;
if ( settingsName = = SettingsKey . printer_name )
{
2018-10-16 15:54:44 -07:00
InventoryTreeView . RebuildPrintersList ( printersNode , theme ) ;
this . Invalidate ( ) ;
2018-10-07 11:36:52 -07:00
}
} , ref unregisterEvents ) ;
//// Rebuild the droplist any time the ActivePrinter changes
//ApplicationController.Instance.ActivePrinterChanged.RegisterEvent((s, e) =>
//{
// this.Rebuild();
//}, ref unregisterEvents);
// Rebuild the droplist any time the Profiles list changes
ProfileManager . ProfilesListChanged . RegisterEvent ( ( s , e ) = >
{
2018-10-16 15:54:44 -07:00
InventoryTreeView . RebuildPrintersList ( printersNode , theme ) ;
this . Invalidate ( ) ;
2018-10-07 11:36:52 -07:00
} , ref unregisterEvents ) ;
}
2018-10-16 15:54:44 -07:00
public static void RebuildPrintersList ( TreeNode printersNode , ThemeConfig theme )
2018-10-07 11:36:52 -07:00
{
if ( printersNode = = null )
{
return ;
}
printersNode . Nodes . Clear ( ) ;
//Add the menu items to the menu itself
foreach ( var printer in ProfileManager . Instance . ActiveProfiles . OrderBy ( p = > p . Name ) )
{
var printerNode = new TreeNode ( theme )
{
Text = printer . Name ,
2018-10-11 17:24:42 -07:00
Name = $"{printer.Name} Node" ,
2018-10-07 11:36:52 -07:00
Tag = printer
} ;
printerNode . Load + = ( s , e ) = >
{
printerNode . Image = OemSettings . Instance . GetIcon ( printer . Make ) ;
} ;
printersNode . Nodes . Add ( printerNode ) ;
}
printersNode . Expanded = true ;
}
}
}