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
{
2018-11-15 12:13:27 -08:00
public class HardwareTreeView : TreeView
2018-10-07 11:36:52 -07:00
{
private TreeNode printersNode ;
private FlowLayoutWidget rootColumn ;
private EventHandler unregisterEvents ;
2018-11-15 12:13:27 -08:00
public HardwareTreeView ( ThemeConfig theme )
2018-10-07 11:36:52 -07:00
: 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 ,
2018-11-29 09:53:48 -08:00
Width = forcedHeight ,
Name = "Import Printer Button"
2018-10-23 11:52:10 -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 ) )
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-11-15 12:13:27 -08:00
HardwareTreeView . CreatePrinterProfilesTree ( printersNode , theme ) ;
2018-10-23 11:52:10 -07:00
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
2018-11-16 08:44:56 -08:00
// Register listeners
PrinterSettings . AnyPrinterSettingChanged + = Printer_SettingChanged ;
2018-10-07 11:36:52 -07:00
2018-11-13 08:21:28 -08:00
// Rebuild the treeview anytime the Profiles list changes
2018-10-07 11:36:52 -07:00
ProfileManager . ProfilesListChanged . RegisterEvent ( ( s , e ) = >
{
2018-11-15 12:13:27 -08:00
HardwareTreeView . CreatePrinterProfilesTree ( printersNode , theme ) ;
2018-10-16 15:54:44 -07:00
this . Invalidate ( ) ;
2018-10-07 11:36:52 -07:00
} , ref unregisterEvents ) ;
}
2018-11-12 09:33:10 -08:00
public static void CreatePrinterProfilesTree ( 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 ;
}
2018-11-12 09:42:39 -08:00
public static void CreateOpenPrintersTree ( TreeNode printersNode , ThemeConfig theme )
{
if ( printersNode = = null )
{
return ;
}
printersNode . Nodes . Clear ( ) ;
//Add the menu items to the menu itself
foreach ( var printer in ApplicationController . Instance . ActivePrinters )
{
string printerName = printer . Settings . GetValue ( SettingsKey . printer_name ) ;
var printerNode = new TreeNode ( theme )
{
Text = printerName ,
Name = $"{printerName} Node" ,
Tag = printer
} ;
printerNode . Load + = ( s , e ) = >
{
printerNode . Image = OemSettings . Instance . GetIcon ( printer . Settings . GetValue ( SettingsKey . make ) ) ;
} ;
printersNode . Nodes . Add ( printerNode ) ;
}
printersNode . Expanded = true ;
}
2018-11-16 08:44:56 -08:00
public override void OnClosed ( EventArgs e )
{
// Unregister listeners
PrinterSettings . AnyPrinterSettingChanged - = Printer_SettingChanged ;
base . OnClosed ( e ) ;
}
private void Printer_SettingChanged ( object s , EventArgs e )
{
string settingsName = ( e as StringEventArgs ) ? . Data ;
if ( settingsName ! = null & & settingsName = = SettingsKey . printer_name )
{
2018-11-16 14:42:27 -08:00
// Allow enough time for ProfileManager to respond and refresh its data
UiThread . RunOnIdle ( ( ) = >
{
HardwareTreeView . CreatePrinterProfilesTree ( printersNode , theme ) ;
} , . 2 ) ;
2018-11-16 08:44:56 -08:00
this . Invalidate ( ) ;
}
}
2018-10-07 11:36:52 -07:00
}
}