2014-03-30 17:54:05 -07:00
/ *
Copyright ( c ) 2014 , Kevin Pope
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.Collections.Generic ;
using System.Linq ;
using System.Text ;
2014-08-19 18:08:40 -07:00
using System.IO ;
2014-03-30 17:54:05 -07:00
using Newtonsoft.Json ;
using Newtonsoft.Json.Converters ;
using Newtonsoft.Json.Serialization ;
using Newtonsoft.Json.Utilities ;
using MatterHackers.Agg ;
using MatterHackers.Agg.UI ;
using MatterHackers.VectorMath ;
using MatterHackers.Localizations ;
using MatterHackers.MatterControl.CustomWidgets ;
using MatterHackers.MatterControl.DataStorage ;
namespace MatterHackers.MatterControl.SlicerConfiguration
{
public class SliceSelectorWidget : FlowLayoutWidget
{
Button editButton ;
2014-04-09 18:45:29 -07:00
ImageButtonFactory imageButtonFactory = new ImageButtonFactory ( ) ;
2014-03-30 17:54:05 -07:00
string filterTag ;
2014-04-05 15:51:13 -07:00
string filterLabel ;
2014-03-30 17:54:05 -07:00
public AnchoredDropDownList DropDownList ;
private TupleList < string , Func < bool > > DropDownMenuItems = new TupleList < string , Func < bool > > ( ) ;
2014-10-14 17:10:22 -07:00
int presetIndex ; //For multiple materials
2014-03-30 17:54:05 -07:00
2014-10-14 17:10:22 -07:00
public SliceSelectorWidget ( string label , RGBA_Bytes accentColor , string tag = null , int presetIndex = 1 )
2014-03-30 17:54:05 -07:00
: base ( FlowDirection . TopToBottom )
{
2014-10-14 17:10:22 -07:00
this . presetIndex = presetIndex ;
2014-04-05 15:51:13 -07:00
this . filterLabel = label ;
2014-03-30 17:54:05 -07:00
if ( tag = = null )
{
this . filterTag = label . ToLower ( ) ;
}
else
{
this . filterTag = tag ;
}
this . HAnchor = HAnchor . ParentLeftRight ;
this . VAnchor = Agg . UI . VAnchor . ParentBottomTop ;
this . BackgroundColor = ActiveTheme . Instance . PrimaryBackgroundColor ;
GuiWidget accentBar = new GuiWidget ( 1 , 5 ) ;
accentBar . BackgroundColor = accentColor ;
accentBar . HAnchor = HAnchor . ParentLeftRight ;
TextWidget labelText = new TextWidget ( LocalizedString . Get ( label ) . ToUpper ( ) ) ;
labelText . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
labelText . HAnchor = Agg . UI . HAnchor . ParentCenter ;
labelText . Margin = new BorderDouble ( 0 , 3 , 0 , 6 ) ;
this . AddChild ( labelText ) ;
this . AddChild ( GetPulldownContainer ( ) ) ;
this . AddChild ( new VerticalSpacer ( ) ) ;
this . AddChild ( accentBar ) ;
}
public virtual FlowLayoutWidget GetPulldownContainer ( )
{
DropDownList = CreateDropdown ( ) ;
FlowLayoutWidget container = new FlowLayoutWidget ( ) ;
container . HAnchor = HAnchor . ParentLeftRight ;
container . Padding = new BorderDouble ( 6 , 0 ) ;
editButton = imageButtonFactory . Generate ( "icon_edit_white.png" , "icon_edit_gray.png" ) ;
editButton . VAnchor = VAnchor . ParentCenter ;
editButton . Margin = new BorderDouble ( right : 6 ) ;
editButton . Click + = ( sender , e ) = >
{
2014-07-17 10:52:46 -07:00
if ( filterTag = = "material" )
{
2014-09-19 19:17:12 -07:00
if ( ApplicationController . Instance . EditMaterialPresetsWindow = = null )
2014-07-17 10:52:46 -07:00
{
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . EditMaterialPresetsWindow = new SlicePresetsWindow ( ReloadOptions , filterLabel , filterTag ) ;
ApplicationController . Instance . EditMaterialPresetsWindow . Closed + = ( popupWindowSender , popupWindowSenderE ) = > { ApplicationController . Instance . EditMaterialPresetsWindow = null ; } ;
2014-07-17 10:52:46 -07:00
}
else
{
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . EditMaterialPresetsWindow . BringToFront ( ) ;
2014-07-17 10:52:46 -07:00
}
}
if ( filterTag = = "quality" )
{
2014-09-19 19:17:12 -07:00
if ( ApplicationController . Instance . EditQualityPresetsWindow = = null )
2014-07-17 10:52:46 -07:00
{
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . EditQualityPresetsWindow = new SlicePresetsWindow ( ReloadOptions , filterLabel , filterTag ) ;
ApplicationController . Instance . EditQualityPresetsWindow . Closed + = ( popupWindowSender , popupWindowSenderE ) = > { ApplicationController . Instance . EditQualityPresetsWindow = null ; } ;
2014-07-17 10:52:46 -07:00
}
else
{
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . EditQualityPresetsWindow . BringToFront ( ) ;
2014-07-17 10:52:46 -07:00
}
}
2014-03-30 17:54:05 -07:00
} ;
container . AddChild ( editButton ) ;
container . AddChild ( DropDownList ) ;
return container ;
2014-07-17 10:52:46 -07:00
}
2014-03-30 17:54:05 -07:00
protected void ReloadOptions ( object sender , EventArgs e )
{
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
2014-03-30 17:54:05 -07:00
}
2014-04-06 19:25:58 -07:00
IEnumerable < DataStorage . SliceSettingsCollection > GetCollections ( )
{
IEnumerable < DataStorage . SliceSettingsCollection > results = Enumerable . Empty < DataStorage . SliceSettingsCollection > ( ) ;
//Retrieve a list of collections matching from the Datastore
2014-04-10 16:37:39 -07:00
if ( ActivePrinterProfile . Instance . ActivePrinter ! = null )
{
2014-04-10 18:32:43 -07:00
string query = string . Format ( "SELECT * FROM SliceSettingsCollection WHERE Tag = '{0}' AND PrinterId = {1} ORDER BY Name;" , filterTag , ActivePrinterProfile . Instance . ActivePrinter . Id ) ;
2014-04-10 16:37:39 -07:00
results = ( IEnumerable < DataStorage . SliceSettingsCollection > ) DataStorage . Datastore . Instance . dbSQLite . Query < DataStorage . SliceSettingsCollection > ( query ) ;
}
2014-04-06 19:25:58 -07:00
return results ;
}
2014-04-09 18:45:29 -07:00
void onItemSelect ( object sender , EventArgs e )
2014-03-30 17:54:05 -07:00
{
2014-04-09 18:45:29 -07:00
MenuItem item = ( MenuItem ) sender ;
if ( filterTag = = "material" )
2014-04-06 19:25:58 -07:00
{
2014-10-14 17:10:22 -07:00
if ( ActivePrinterProfile . Instance . GetMaterialSetting ( presetIndex ) ! = Int32 . Parse ( item . Value ) )
2014-04-06 19:25:58 -07:00
{
2014-10-14 17:10:22 -07:00
ActivePrinterProfile . Instance . SetMaterialSetting ( presetIndex , Int32 . Parse ( item . Value ) ) ;
2014-04-06 19:25:58 -07:00
}
2014-04-09 18:45:29 -07:00
}
else if ( filterTag = = "quality" )
{
if ( ActivePrinterProfile . Instance . ActiveQualitySettingsID ! = Int32 . Parse ( item . Value ) )
2014-04-06 19:25:58 -07:00
{
2014-04-09 18:45:29 -07:00
ActivePrinterProfile . Instance . ActiveQualitySettingsID = Int32 . Parse ( item . Value ) ;
2014-04-06 19:25:58 -07:00
}
2014-04-09 18:45:29 -07:00
}
UiThread . RunOnIdle ( ( state ) = >
{
2014-04-08 19:33:09 -07:00
ActiveSliceSettings . Instance . LoadAllSettings ( ) ;
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
2014-04-09 18:45:29 -07:00
} ) ;
}
void onNewItemSelect ( object sender , EventArgs e )
{
UiThread . RunOnIdle ( ( state ) = >
{
ActiveSliceSettings . Instance . LoadAllSettings ( ) ;
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
2014-07-17 10:52:46 -07:00
if ( filterTag = = "material" )
{
2014-09-19 19:17:12 -07:00
if ( ApplicationController . Instance . EditMaterialPresetsWindow = = null )
2014-07-17 10:52:46 -07:00
{
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . EditMaterialPresetsWindow = new SlicePresetsWindow ( ReloadOptions , filterLabel , filterTag , false , 0 ) ;
ApplicationController . Instance . EditMaterialPresetsWindow . Closed + = ( popupWindowSender , popupWindowSenderE ) = > { ApplicationController . Instance . EditMaterialPresetsWindow = null ; } ;
2014-07-17 10:52:46 -07:00
}
else
{
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . EditMaterialPresetsWindow . ChangeToSlicePresetFromID ( 0 ) ;
ApplicationController . Instance . EditMaterialPresetsWindow . BringToFront ( ) ;
2014-07-17 10:52:46 -07:00
}
}
if ( filterTag = = "quality" )
{
2014-09-19 19:17:12 -07:00
if ( ApplicationController . Instance . EditQualityPresetsWindow = = null )
2014-07-17 10:52:46 -07:00
{
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . EditQualityPresetsWindow = new SlicePresetsWindow ( ReloadOptions , filterLabel , filterTag , false , 0 ) ;
ApplicationController . Instance . EditQualityPresetsWindow . Closed + = ( popupWindowSender , popupWindowSenderE ) = > { ApplicationController . Instance . EditQualityPresetsWindow = null ; } ;
2014-07-17 10:52:46 -07:00
}
else
{
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . EditQualityPresetsWindow . ChangeToSlicePresetFromID ( 0 ) ;
ApplicationController . Instance . EditQualityPresetsWindow . BringToFront ( ) ;
2014-07-17 10:52:46 -07:00
}
}
2014-04-09 18:45:29 -07:00
} ) ;
}
AnchoredDropDownList CreateDropdown ( )
{
2014-06-25 21:16:44 -07:00
AnchoredDropDownList dropDownList = new AnchoredDropDownList ( "- default -" , maxHeight : 300 ) ;
2014-04-09 18:45:29 -07:00
dropDownList . Margin = new BorderDouble ( 0 , 3 ) ;
dropDownList . MinimumSize = new Vector2 ( dropDownList . LocalBounds . Width , dropDownList . LocalBounds . Height ) ;
MenuItem defaultMenuItem = dropDownList . AddItem ( "- default -" , "0" ) ;
defaultMenuItem . Selected + = new EventHandler ( onItemSelect ) ;
2014-04-06 19:25:58 -07:00
IEnumerable < DataStorage . SliceSettingsCollection > collections = GetCollections ( ) ;
foreach ( DataStorage . SliceSettingsCollection collection in collections )
{
MenuItem menuItem = dropDownList . AddItem ( collection . Name , collection . Id . ToString ( ) ) ;
2014-04-09 18:45:29 -07:00
menuItem . Selected + = new EventHandler ( onItemSelect ) ;
2014-04-06 19:25:58 -07:00
}
2014-04-09 18:45:29 -07:00
2014-04-10 16:37:39 -07:00
MenuItem addNewPreset = dropDownList . AddItem ( "<< Add >>" , "new" ) ;
2014-04-09 18:45:29 -07:00
addNewPreset . Selected + = new EventHandler ( onNewItemSelect ) ;
2014-04-06 19:25:58 -07:00
if ( filterTag = = "material" )
{
2014-04-10 16:37:39 -07:00
try
{
2014-10-14 17:10:22 -07:00
dropDownList . SelectedValue = ActivePrinterProfile . Instance . GetMaterialSetting ( presetIndex ) . ToString ( ) ;
2014-04-10 16:37:39 -07:00
}
catch
{
//Unable to set selected value
}
2014-04-06 19:25:58 -07:00
}
else if ( filterTag = = "quality" )
{
2014-04-10 16:37:39 -07:00
try
{
dropDownList . SelectedValue = ActivePrinterProfile . Instance . ActiveQualitySettingsID . ToString ( ) ;
}
catch
{
//Unable to set selected value
}
2014-04-06 19:25:58 -07:00
}
2014-03-30 17:54:05 -07:00
return dropDownList ;
}
}
public class SliceEngineSelector : SliceSelectorWidget
{
public AnchoredDropDownList EngineMenuDropList ;
private TupleList < string , Func < bool > > engineOptionsMenuItems = new TupleList < string , Func < bool > > ( ) ;
public SliceEngineSelector ( string label , RGBA_Bytes accentColor )
: base ( label , accentColor )
{
}
public override FlowLayoutWidget GetPulldownContainer ( )
{
EngineMenuDropList = CreateSliceEngineDropdown ( ) ;
FlowLayoutWidget container = new FlowLayoutWidget ( ) ;
container . HAnchor = HAnchor . ParentLeftRight ;
container . Padding = new BorderDouble ( 6 , 0 ) ;
container . AddChild ( EngineMenuDropList ) ;
return container ;
}
AnchoredDropDownList CreateSliceEngineDropdown ( )
{
AnchoredDropDownList engineMenuDropList = new AnchoredDropDownList ( "Engine " ) ;
engineMenuDropList . Margin = new BorderDouble ( 0 , 3 ) ;
2014-08-19 18:08:40 -07:00
{
//Add Each SliceEngineInfo Objects to DropMenu
2014-08-20 12:14:45 -07:00
foreach ( SliceEngineInfo engineMenuItem in SlicingQueue . AvailableSliceEngines )
{
MenuItem item = engineMenuDropList . AddItem ( engineMenuItem . Name ) ;
ActivePrinterProfile . SlicingEngineTypes itemEngineType = engineMenuItem . GetSliceEngineType ( ) ;
2014-08-20 10:52:47 -07:00
item . Selected + = ( sender , e ) = >
{
2014-08-20 12:14:45 -07:00
ActivePrinterProfile . Instance . ActiveSliceEngineType = itemEngineType ;
2014-09-19 19:17:12 -07:00
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
2014-08-20 10:52:47 -07:00
} ;
2014-08-19 18:08:40 -07:00
2014-08-20 12:14:45 -07:00
//Set item as selected if it matches the active slice engine
if ( engineMenuItem . GetSliceEngineType ( ) = = ActivePrinterProfile . Instance . ActiveSliceEngineType )
{
engineMenuDropList . SelectedLabel = engineMenuItem . Name ;
}
}
2014-08-19 18:08:40 -07:00
2014-08-20 12:14:45 -07:00
//If nothing is selected (ie selected engine is not available) set to
if ( engineMenuDropList . SelectedLabel = = "" )
{
try
{
engineMenuDropList . SelectedLabel = MatterSliceInfo . DisplayName ;
}
catch
{
throw new Exception ( "MatterSlice is not available, for some strange reason" ) ;
}
2014-08-19 18:08:40 -07:00
2014-08-20 12:14:45 -07:00
}
}
2014-03-30 17:54:05 -07:00
engineMenuDropList . MinimumSize = new Vector2 ( engineMenuDropList . LocalBounds . Width , engineMenuDropList . LocalBounds . Height ) ;
return engineMenuDropList ;
}
}
}