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
2015-04-08 15:20:10 -07:00
modification , are permitted provided that the following conditions are met :
2014-03-30 17:54:05 -07:00
1. Redistributions of source code must retain the above copyright notice , this
2015-04-08 15:20:10 -07:00
list of conditions and the following disclaimer .
2014-03-30 17:54:05 -07:00
2. Redistributions in binary form must reproduce the above copyright notice ,
this list of conditions and the following disclaimer in the documentation
2015-04-08 15:20:10 -07:00
and / or other materials provided with the distribution .
2014-03-30 17:54:05 -07:00
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
2015-04-08 15:20:10 -07:00
of the authors and should not be interpreted as representing official policies ,
2014-03-30 17:54:05 -07:00
either expressed or implied , of the FreeBSD Project .
* /
2014-12-16 08:26:35 -08:00
//#define DO_IN_PLACE_EDIT
2014-03-30 17:54:05 -07:00
using MatterHackers.Agg ;
2015-12-29 16:41:51 -08:00
using MatterHackers.Agg.Image ;
2016-04-27 17:34:33 -07:00
using MatterHackers.Agg.ImageProcessing ;
2015-12-29 16:41:51 -08:00
using MatterHackers.Agg.PlatformAbstract ;
2014-03-30 17:54:05 -07:00
using MatterHackers.Agg.UI ;
2015-12-29 16:41:51 -08:00
using MatterHackers.ImageProcessing ;
2014-03-30 17:54:05 -07:00
using MatterHackers.Localizations ;
using MatterHackers.MatterControl.CustomWidgets ;
2016-02-23 11:15:03 -08:00
using MatterHackers.MatterControl.DataStorage ;
2015-04-08 15:20:10 -07:00
using MatterHackers.VectorMath ;
using System ;
using System.Collections.Generic ;
2015-09-11 10:48:24 -07:00
using System.Diagnostics ;
2015-04-08 15:20:10 -07:00
using System.Linq ;
2014-03-30 17:54:05 -07:00
namespace MatterHackers.MatterControl.SlicerConfiguration
{
2016-04-18 11:31:31 -07:00
public class PresetSelectorWidget : FlowLayoutWidget
2015-04-08 15:20:10 -07:00
{
private Button editButton ;
private ImageButtonFactory imageButtonFactory = new ImageButtonFactory ( ) ;
private string filterTag ;
private string filterLabel ;
2016-04-27 17:34:33 -07:00
public StyledDropDownList DropDownList ;
2015-04-08 15:20:10 -07:00
private TupleList < string , Func < bool > > DropDownMenuItems = new TupleList < string , Func < bool > > ( ) ;
2016-04-18 11:31:31 -07:00
private int extruderIndex ; //For multiple materials
public PresetSelectorWidget ( string label , RGBA_Bytes accentColor , string tag , int extruderIndex )
2015-04-08 15:20:10 -07:00
: base ( FlowDirection . TopToBottom )
{
2016-04-18 11:31:31 -07:00
this . extruderIndex = extruderIndex ;
2015-04-08 15:20:10 -07:00
this . filterLabel = label ;
2016-04-18 11:31:31 -07:00
this . filterTag = ( tag = = null ) ? label . ToLower ( ) : tag ;
2015-04-08 15:20:10 -07:00
this . HAnchor = HAnchor . ParentLeftRight ;
2015-08-03 17:24:44 -07:00
this . VAnchor = Agg . UI . VAnchor . Max_FitToChildren_ParentHeight ;
2015-04-08 15:20:10 -07:00
this . BackgroundColor = ActiveTheme . Instance . PrimaryBackgroundColor ;
2014-03-30 17:54:05 -07:00
2016-04-18 11:31:31 -07:00
GuiWidget accentBar = new GuiWidget ( 7 , 5 )
{
BackgroundColor = accentColor ,
HAnchor = HAnchor . ParentLeftRight
} ;
2015-08-03 17:24:44 -07:00
2016-04-18 11:31:31 -07:00
TextWidget labelText = new TextWidget ( label . Localize ( ) . ToUpper ( ) )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
HAnchor = Agg . UI . HAnchor . ParentCenter ,
Margin = new BorderDouble ( 0 , 3 , 0 , 6 )
} ;
2014-03-30 17:54:05 -07:00
2015-04-08 15:20:10 -07:00
this . AddChild ( labelText ) ;
this . AddChild ( GetPulldownContainer ( ) ) ;
this . AddChild ( new VerticalSpacer ( ) ) ;
this . AddChild ( accentBar ) ;
}
2014-03-30 17:54:05 -07:00
2015-04-08 15:20:10 -07:00
public virtual FlowLayoutWidget GetPulldownContainer ( )
{
DropDownList = CreateDropdown ( ) ;
2014-03-30 17:54:05 -07:00
2015-04-08 15:20:10 -07:00
FlowLayoutWidget container = new FlowLayoutWidget ( ) ;
container . HAnchor = HAnchor . ParentLeftRight ;
container . Padding = new BorderDouble ( 6 , 0 ) ;
2014-03-30 17:54:05 -07:00
2015-12-29 16:41:51 -08:00
ImageBuffer normalImage = StaticData . Instance . LoadIcon ( "icon_edit_white_32x32.png" ) ;
int iconSize = ( int ) ( 16 * TextWidget . GlobalPointSizeScaleRatio ) ;
normalImage = ImageBuffer . CreateScaledImage ( normalImage , iconSize , iconSize ) ;
editButton = imageButtonFactory . Generate ( normalImage , WhiteToColor . CreateWhiteToColor ( normalImage , RGBA_Bytes . Gray ) ) ;
2014-11-11 14:51:36 -08:00
2015-04-08 15:20:10 -07:00
editButton . VAnchor = VAnchor . ParentCenter ;
editButton . Margin = new BorderDouble ( right : 6 ) ;
editButton . Click + = ( sender , e ) = >
{
2014-12-16 08:26:35 -08:00
#if DO_IN_PLACE_EDIT
if ( filterTag = = "quality" )
{
SliceSettingsWidget . SettingsIndexBeingEdited = 2 ;
}
else
{
SliceSettingsWidget . SettingsIndexBeingEdited = 3 ;
}
2016-04-26 17:32:55 -07:00
// If there is a setting selected then reload the slice setting widget with the presetIndex to edit.
2014-12-16 08:26:35 -08:00
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
// If no setting selected then call onNewItemSelect(object sender, EventArgs e)
#else
2015-04-08 15:20:10 -07:00
if ( filterTag = = "material" )
{
if ( ApplicationController . Instance . EditMaterialPresetsWindow = = null )
{
ApplicationController . Instance . EditMaterialPresetsWindow = new SlicePresetsWindow ( ReloadOptions , filterLabel , filterTag ) ;
ApplicationController . Instance . EditMaterialPresetsWindow . Closed + = ( popupWindowSender , popupWindowSenderE ) = > { ApplicationController . Instance . EditMaterialPresetsWindow = null ; } ;
}
else
{
ApplicationController . Instance . EditMaterialPresetsWindow . BringToFront ( ) ;
}
}
2014-11-11 14:51:36 -08:00
2015-04-08 15:20:10 -07:00
if ( filterTag = = "quality" )
{
if ( ApplicationController . Instance . EditQualityPresetsWindow = = null )
{
ApplicationController . Instance . EditQualityPresetsWindow = new SlicePresetsWindow ( ReloadOptions , filterLabel , filterTag ) ;
ApplicationController . Instance . EditQualityPresetsWindow . Closed + = ( popupWindowSender , popupWindowSenderE ) = > { ApplicationController . Instance . EditQualityPresetsWindow = null ; } ;
}
else
{
ApplicationController . Instance . EditQualityPresetsWindow . BringToFront ( ) ;
}
}
2014-12-16 08:26:35 -08:00
#endif
2015-04-08 15:20:10 -07:00
} ;
container . AddChild ( editButton ) ;
container . AddChild ( DropDownList ) ;
2016-03-30 18:33:29 -07:00
2015-04-08 15:20:10 -07:00
return container ;
}
protected void ReloadOptions ( object sender , EventArgs e )
{
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
}
private void onItemSelect ( object sender , EventArgs e )
{
2016-04-18 11:31:31 -07:00
var activeSettings = ActiveSliceSettings . Instance ;
2015-04-08 15:20:10 -07:00
MenuItem item = ( MenuItem ) sender ;
if ( filterTag = = "material" )
{
2016-04-27 18:57:51 -07:00
if ( activeSettings . MaterialPresetKey ( extruderIndex ) ! = item . Text )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
activeSettings . SetMaterialPreset ( extruderIndex , item . Text ) ;
2015-04-08 15:20:10 -07:00
}
}
else if ( filterTag = = "quality" )
{
2016-04-18 11:31:31 -07:00
if ( activeSettings . ActiveQualityKey ! = item . Text )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
activeSettings . ActiveQualityKey = item . Text ;
2015-04-08 15:20:10 -07:00
}
}
2016-04-18 11:31:31 -07:00
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-04-08 15:20:10 -07:00
{
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
} ) ;
}
private void onNewItemSelect ( object sender , EventArgs e )
{
2014-12-16 08:26:35 -08:00
#if DO_IN_PLACE_EDIT
// pop up a dialog to request a new setting name
2016-04-26 17:32:55 -07:00
// after getting the new name select it and reload the slice setting widget editing the new setting
2014-12-16 08:26:35 -08:00
throw new NotImplementedException ( ) ;
2015-04-08 15:20:10 -07:00
#else
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-04-08 15:20:10 -07:00
{
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
if ( filterTag = = "material" )
{
if ( ApplicationController . Instance . EditMaterialPresetsWindow = = null )
{
2016-04-18 11:31:31 -07:00
ApplicationController . Instance . EditMaterialPresetsWindow = new SlicePresetsWindow ( ReloadOptions , filterLabel , filterTag , false ) ;
2015-04-08 15:20:10 -07:00
ApplicationController . Instance . EditMaterialPresetsWindow . Closed + = ( popupWindowSender , popupWindowSenderE ) = > { ApplicationController . Instance . EditMaterialPresetsWindow = null ; } ;
}
else
{
2016-04-18 11:31:31 -07:00
ApplicationController . Instance . EditMaterialPresetsWindow . ChangeToSlicePresetFromID ( "" ) ;
2015-04-08 15:20:10 -07:00
ApplicationController . Instance . EditMaterialPresetsWindow . BringToFront ( ) ;
}
}
if ( filterTag = = "quality" )
{
if ( ApplicationController . Instance . EditQualityPresetsWindow = = null )
{
2016-04-18 11:31:31 -07:00
ApplicationController . Instance . EditQualityPresetsWindow = new SlicePresetsWindow ( ReloadOptions , filterLabel , filterTag , false ) ;
2015-04-08 15:20:10 -07:00
ApplicationController . Instance . EditQualityPresetsWindow . Closed + = ( popupWindowSender , popupWindowSenderE ) = > { ApplicationController . Instance . EditQualityPresetsWindow = null ; } ;
}
else
{
2016-04-18 11:31:31 -07:00
ApplicationController . Instance . EditQualityPresetsWindow . ChangeToSlicePresetFromID ( "" ) ;
2015-04-08 15:20:10 -07:00
ApplicationController . Instance . EditQualityPresetsWindow . BringToFront ( ) ;
}
}
} ) ;
2014-12-16 08:26:35 -08:00
#endif
2015-04-08 15:20:10 -07:00
}
2016-04-27 17:34:33 -07:00
private StyledDropDownList CreateDropdown ( )
2015-04-08 15:20:10 -07:00
{
2016-04-27 17:34:33 -07:00
var dropDownList = new StyledDropDownList ( "- default -" , maxHeight : 300 )
{
UseLeftIcons = true ,
HAnchor = HAnchor . ParentLeftRight ,
MenuItemsPadding = new BorderDouble ( 10 , 4 , 10 , 6 ) ,
} ;
2015-04-08 15:20:10 -07:00
dropDownList . Margin = new BorderDouble ( 0 , 3 ) ;
dropDownList . MinimumSize = new Vector2 ( dropDownList . LocalBounds . Width , dropDownList . LocalBounds . Height ) ;
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
MenuItem defaultMenuItem = dropDownList . AddItem ( "- default -" , "0" ) ;
defaultMenuItem . Selected + = new EventHandler ( onItemSelect ) ;
2016-04-18 11:31:31 -07:00
var listSource = ( filterTag = = "material" ) ? ActiveSliceSettings . Instance . AllMaterialKeys ( ) : ActiveSliceSettings . Instance . AllQualityKeys ( ) ;
foreach ( var presetName in listSource )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
MenuItem menuItem = dropDownList . AddItem ( presetName , presetName ) ;
menuItem . Selected + = onItemSelect ;
2015-04-08 15:20:10 -07:00
}
2016-04-30 15:30:11 -07:00
MenuItem addNewPreset = dropDownList . AddItem ( InvertLightness . DoInvertLightness ( StaticData . Instance . LoadIcon ( "icon_circle_plus.png" ) ) , "Add New Setting..." , "new" ) ;
2016-04-27 17:34:33 -07:00
addNewPreset . Selected + = onNewItemSelect ;
if ( false )
2016-04-26 17:32:55 -07:00
{
FlowLayoutWidget container = new FlowLayoutWidget ( ) ;
container . HAnchor = HAnchor . ParentLeftRight ;
TextImageButtonFactory buttonFactory = new TextImageButtonFactory ( ) ;
buttonFactory . normalTextColor = ActiveTheme . Instance . PrimaryTextColor ;
buttonFactory . hoverTextColor = ActiveTheme . Instance . PrimaryTextColor ;
buttonFactory . disabledTextColor = ActiveTheme . Instance . PrimaryTextColor ;
buttonFactory . pressedTextColor = ActiveTheme . Instance . PrimaryTextColor ;
buttonFactory . borderWidth = 0 ;
Button addPresetButton = buttonFactory . Generate ( LocalizedString . Get ( "Add" ) , "icon_circle_plus.png" ) ;
addPresetButton . ToolTipText = "Add a new Settings Preset" . Localize ( ) ;
addPresetButton . Click + = ( sender , e ) = >
{
onNewItemSelect ( sender , e ) ;
} ;
container . AddChild ( addPresetButton ) ;
Button importPresetButton = buttonFactory . Generate ( LocalizedString . Get ( "Import" ) ) ;
importPresetButton . ToolTipText = "Import an existing Settings Preset" . Localize ( ) ;
importPresetButton . Click + = ( sender , e ) = >
{
} ;
container . AddChild ( importPresetButton ) ;
dropDownList . MenuItems . Add ( new MenuItem ( container ) ) ;
}
2015-04-08 15:20:10 -07:00
2016-04-18 11:31:31 -07:00
try
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
string settingsKey ;
if ( filterTag = = "material" )
2015-04-08 15:20:10 -07:00
{
2016-04-27 18:57:51 -07:00
settingsKey = ActiveSliceSettings . Instance . MaterialPresetKey ( extruderIndex ) ;
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
else
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
settingsKey = ActiveSliceSettings . Instance . ActiveQualityKey ;
2015-04-08 15:20:10 -07:00
}
2016-04-18 11:31:31 -07:00
if ( ! string . IsNullOrEmpty ( settingsKey ) )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
dropDownList . SelectedValue = settingsKey ;
2015-04-08 15:20:10 -07:00
}
}
2016-04-18 11:31:31 -07:00
catch ( Exception ex )
{
GuiWidget . BreakInDebugger ( ex . Message ) ;
}
2015-04-08 15:20:10 -07:00
return dropDownList ;
}
}
2014-03-30 17:54:05 -07:00
2015-04-08 15:20:10 -07:00
public class SliceEngineSelector : StyledDropDownList
{
private TupleList < string , Func < bool > > engineOptionsMenuItems = new TupleList < string , Func < bool > > ( ) ;
2014-03-30 17:54:05 -07:00
2015-04-08 15:20:10 -07:00
public SliceEngineSelector ( string label )
: base ( label )
{
HAnchor = HAnchor . ParentLeftRight ;
2014-03-30 17:54:05 -07:00
2015-04-08 15:20:10 -07:00
//Add Each SliceEngineInfo Objects to DropMenu
foreach ( SliceEngineInfo engineMenuItem in SlicingQueue . AvailableSliceEngines )
{
2015-02-20 12:05:44 -08:00
bool engineAllowed = true ;
2016-04-27 18:57:51 -07:00
if ( ActiveSliceSettings . Instance . ExtruderCount ( ) > 1 & & engineMenuItem . Name ! = "MatterSlice" )
2015-02-20 12:05:44 -08:00
{
engineAllowed = false ;
}
2015-04-08 15:20:10 -07:00
if ( engineAllowed )
2015-02-20 12:05:44 -08:00
{
MenuItem item = AddItem ( engineMenuItem . Name ) ;
2016-04-18 11:31:31 -07:00
SlicingEngineTypes itemEngineType = engineMenuItem . GetSliceEngineType ( ) ;
2015-02-20 12:05:44 -08:00
item . Selected + = ( sender , e ) = >
{
2016-04-28 09:41:27 -07:00
if ( ActiveSliceSettings . Instance . ActiveSliceEngineType ( ) ! = itemEngineType )
2015-02-20 12:05:44 -08:00
{
2016-04-28 09:41:27 -07:00
ActiveSliceSettings . Instance . ActiveSliceEngineType ( itemEngineType ) ;
2015-02-20 12:05:44 -08:00
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
}
} ;
//Set item as selected if it matches the active slice engine
2016-04-28 09:41:27 -07:00
if ( engineMenuItem . GetSliceEngineType ( ) = = ActiveSliceSettings . Instance . ActiveSliceEngineType ( ) )
2015-02-20 12:05:44 -08:00
{
SelectedLabel = engineMenuItem . Name ;
}
}
2015-04-08 15:20:10 -07:00
}
2014-11-11 14:51:36 -08:00
2016-04-26 17:32:55 -07:00
//If nothing is selected (i.e. selected engine is not available) set to
2015-04-08 15:20:10 -07:00
if ( SelectedLabel = = "" )
{
try
{
SelectedLabel = MatterSliceInfo . DisplayName ;
}
2016-04-18 11:31:31 -07:00
catch ( Exception ex )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
GuiWidget . BreakInDebugger ( ex . Message ) ;
throw new Exception ( "Unable to find MatterSlice executable" ) ;
2015-04-08 15:20:10 -07:00
}
}
2014-08-19 18:08:40 -07:00
2015-04-08 15:20:10 -07:00
MinimumSize = new Vector2 ( LocalBounds . Width , LocalBounds . Height ) ;
}
}
}