2014-04-06 19:26:41 -07:00
/ *
Copyright ( c ) 2014 , Lars Brubaker
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-04-06 19:26:41 -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-04-06 19:26:41 -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-04-06 19:26:41 -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-04-06 19:26:41 -07:00
either expressed or implied , of the FreeBSD Project .
* /
using MatterHackers.Agg ;
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
using MatterHackers.MatterControl.CustomWidgets ;
2015-04-08 15:20:10 -07:00
using MatterHackers.MatterControl.DataStorage ;
using MatterHackers.VectorMath ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
2014-04-06 19:26:41 -07:00
namespace MatterHackers.MatterControl.SlicerConfiguration
{
2015-04-08 15:20:10 -07:00
public class SlicePresetListWidget : GuiWidget
{
private SlicePresetsWindow windowController ;
private TextImageButtonFactory buttonFactory ;
private LinkButtonFactory linkButtonFactory ;
private PresetListControl presetListControl ;
private Button importPresetButton ;
public SlicePresetListWidget ( SlicePresetsWindow windowController )
{
this . windowController = windowController ;
this . AnchorAll ( ) ;
linkButtonFactory = new LinkButtonFactory ( ) ;
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 ;
AddElements ( ) ;
AddHandlers ( ) ;
}
private void AddElements ( )
{
FlowLayoutWidget mainContainer = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
mainContainer . Padding = new BorderDouble ( 3 ) ;
mainContainer . AnchorAll ( ) ;
mainContainer . AddChild ( GetTopRow ( ) ) ;
mainContainer . AddChild ( GetMiddleRow ( ) ) ;
mainContainer . AddChild ( GetBottomRow ( ) ) ;
this . AddChild ( mainContainer ) ;
}
private void AddHandlers ( )
{
importPresetButton . Click + = new EventHandler ( importPreset_Click ) ;
}
private FlowLayoutWidget GetTopRow ( )
{
FlowLayoutWidget container = new FlowLayoutWidget ( ) ;
container . HAnchor = HAnchor . ParentLeftRight ;
container . Padding = new BorderDouble ( 0 , 6 ) ;
TextWidget labelText = new TextWidget ( "{0} Presets:" . FormatWith ( windowController . filterLabel . Localize ( ) ) , pointSize : 14 ) ;
labelText . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
container . AddChild ( labelText ) ;
container . AddChild ( new HorizontalSpacer ( ) ) ;
return container ;
}
private FlowLayoutWidget GetMiddleRow ( )
{
FlowLayoutWidget container = new FlowLayoutWidget ( ) ;
container . HAnchor = HAnchor . ParentLeftRight ;
container . VAnchor = Agg . UI . VAnchor . ParentBottomTop ;
container . BackgroundColor = ActiveTheme . Instance . SecondaryBackgroundColor ;
container . Margin = new BorderDouble ( 0 , 3 , 0 , 0 ) ;
presetListControl = new PresetListControl ( ) ;
IEnumerable < DataStorage . SliceSettingsCollection > collections = GetCollections ( ) ;
foreach ( DataStorage . SliceSettingsCollection collection in collections )
{
presetListControl . AddChild ( new PresetListItem ( this . windowController , collection ) ) ;
}
container . AddChild ( presetListControl ) ;
return container ;
}
private FlowLayoutWidget GetBottomRow ( )
{
FlowLayoutWidget container = new FlowLayoutWidget ( ) ;
container . HAnchor = HAnchor . ParentLeftRight ;
Button addPresetButton = buttonFactory . Generate ( LocalizedString . Get ( "Add" ) , "icon_circle_plus.png" ) ;
2015-08-12 13:16:39 -07:00
addPresetButton . ToolTipText = "Add a new Material Preset" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
addPresetButton . Click + = ( sender , e ) = >
{
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-04-08 15:20:10 -07:00
{
windowController . ChangeToSlicePresetDetail ( ) ;
} ) ;
} ;
importPresetButton = buttonFactory . Generate ( LocalizedString . Get ( "Import" ) ) ;
2015-08-12 13:16:39 -07:00
importPresetButton . ToolTipText = "Import an existing Material Preset" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
Button closeButton = buttonFactory . Generate ( LocalizedString . Get ( "Close" ) ) ;
closeButton . Click + = ( sender , e ) = >
{
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-04-08 15:20:10 -07:00
{
windowController . Close ( ) ;
} ) ;
} ;
2014-04-06 19:26:41 -07:00
2015-04-08 15:20:10 -07:00
container . AddChild ( addPresetButton ) ;
container . AddChild ( importPresetButton ) ;
container . AddChild ( new HorizontalSpacer ( ) ) ;
container . AddChild ( closeButton ) ;
return container ;
}
private void importPreset_Click ( object sender , EventArgs mouseEvent )
{
UiThread . RunOnIdle ( importPresetDo ) ;
}
2015-06-11 12:06:40 -07:00
private void importPresetDo ( )
2015-04-08 15:20:10 -07:00
{
OpenFileDialogParams openParams = new OpenFileDialogParams ( "Load Slice Preset|*.slice;*.ini" ) ;
openParams . ActionButtonLabel = "Load Slice Preset" ;
openParams . Title = "MatterControl: Select A File" ;
2014-06-15 14:21:05 -07:00
2014-10-11 14:48:10 -07:00
FileDialog . OpenFileDialog ( openParams , onPresetLoad ) ;
2015-04-08 15:20:10 -07:00
}
2014-06-15 14:21:05 -07:00
2015-04-08 15:20:10 -07:00
private void onPresetLoad ( OpenFileDialogParams openParams )
2014-10-11 14:48:10 -07:00
{
if ( openParams . FileNames ! = null )
2015-04-08 15:20:10 -07:00
{
2014-10-11 14:48:10 -07:00
DataStorage . SliceSettingsCollection settingsCollection ;
try
{
if ( File . Exists ( openParams . FileName ) )
{
//Create collection to hold preset settings
settingsCollection = new SliceSettingsCollection ( ) ;
settingsCollection . Tag = windowController . filterTag ;
settingsCollection . PrinterId = ActivePrinterProfile . Instance . ActivePrinter . Id ;
settingsCollection . Name = System . IO . Path . GetFileNameWithoutExtension ( openParams . FileName ) ;
settingsCollection . Commit ( ) ;
string [ ] lines = System . IO . File . ReadAllLines ( openParams . FileName ) ;
foreach ( string line in lines )
{
//Ignore commented lines
if ( ! line . StartsWith ( "#" ) )
{
string [ ] settingLine = line . Split ( '=' ) ;
string keyName = settingLine [ 0 ] . Trim ( ) ;
string settingDefaultValue = settingLine [ 1 ] . Trim ( ) ;
//To do - validate imported settings as valid (KP)
DataStorage . SliceSetting sliceSetting = new DataStorage . SliceSetting ( ) ;
sliceSetting . Name = keyName ;
2015-04-08 15:20:10 -07:00
sliceSetting . Value = settingDefaultValue ;
2014-10-11 14:48:10 -07:00
sliceSetting . SettingsCollectionId = settingsCollection . Id ;
sliceSetting . Commit ( ) ;
}
}
windowController . ChangeToSlicePresetList ( ) ;
}
}
2014-11-25 12:19:37 -08:00
catch ( Exception )
2015-04-08 15:20:10 -07:00
{
2014-10-11 14:48:10 -07:00
// Error loading configuration
}
}
}
2015-04-08 15:20:10 -07:00
private IEnumerable < DataStorage . SliceSettingsCollection > GetCollections ( )
{
IEnumerable < DataStorage . SliceSettingsCollection > results = Enumerable . Empty < DataStorage . SliceSettingsCollection > ( ) ;
if ( ActivePrinterProfile . Instance . ActivePrinter ! = null )
{
//Retrieve a list of collections matching from the Datastore
string query = string . Format ( "SELECT * FROM SliceSettingsCollection WHERE Tag = '{0}' AND PrinterId = {1} ORDER BY Name;" , windowController . filterTag , ActivePrinterProfile . Instance . ActivePrinter . Id ) ;
results = ( IEnumerable < DataStorage . SliceSettingsCollection > ) DataStorage . Datastore . Instance . dbSQLite . Query < DataStorage . SliceSettingsCollection > ( query ) ;
}
return results ;
}
private class PresetListItem : FlowLayoutWidget
{
private DataStorage . SliceSettingsCollection preset ;
private DataStorage . SliceSettingsCollection Preset { get { return preset ; } }
public PresetListItem ( SlicePresetsWindow windowController , DataStorage . SliceSettingsCollection preset )
{
this . preset = preset ;
this . BackgroundColor = RGBA_Bytes . White ;
this . HAnchor = HAnchor . ParentLeftRight ;
this . Margin = new BorderDouble ( 6 , 0 , 6 , 3 ) ;
this . Padding = new BorderDouble ( 3 ) ;
LinkButtonFactory linkButtonFactory = new LinkButtonFactory ( ) ;
linkButtonFactory . fontSize = 10 ;
int maxLabelWidth = 300 ;
TextWidget materialLabel = new TextWidget ( preset . Name , pointSize : 14 ) ;
materialLabel . EllipsisIfClipped = true ;
materialLabel . VAnchor = Agg . UI . VAnchor . ParentCenter ;
materialLabel . MinimumSize = new Vector2 ( maxLabelWidth , materialLabel . Height ) ;
materialLabel . Width = maxLabelWidth ;
Button materialEditLink = linkButtonFactory . Generate ( "edit" ) ;
materialEditLink . VAnchor = Agg . UI . VAnchor . ParentCenter ;
materialEditLink . Click + = ( sender , e ) = >
{
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-04-08 15:20:10 -07:00
{
windowController . ChangeToSlicePresetDetail ( preset ) ;
} ) ;
} ;
Button materialRemoveLink = linkButtonFactory . Generate ( "remove" ) ;
materialRemoveLink . Margin = new BorderDouble ( left : 4 ) ;
materialRemoveLink . VAnchor = Agg . UI . VAnchor . ParentCenter ;
materialRemoveLink . Click + = ( sender , e ) = >
{
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-04-08 15:20:10 -07:00
{
//Unwind this setting if it is currently active
if ( ActivePrinterProfile . Instance . ActivePrinter ! = null )
{
if ( preset . Id = = ActivePrinterProfile . Instance . ActiveQualitySettingsID )
{
ActivePrinterProfile . Instance . ActiveQualitySettingsID = 0 ;
}
string [ ] activeMaterialPresets = ActivePrinterProfile . Instance . ActivePrinter . MaterialCollectionIds . Split ( ',' ) ;
for ( int i = 0 ; i < activeMaterialPresets . Count ( ) ; i + + )
{
int index = 0 ;
Int32 . TryParse ( activeMaterialPresets [ i ] , out index ) ;
if ( preset . Id = = index )
{
ActivePrinterProfile . Instance . SetMaterialSetting ( i + 1 , 0 ) ;
}
}
}
preset . Delete ( ) ;
windowController . ChangeToSlicePresetList ( ) ;
ActiveSliceSettings . Instance . LoadAllSettings ( ) ;
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
} ) ;
} ;
this . AddChild ( materialLabel ) ;
this . AddChild ( new HorizontalSpacer ( ) ) ;
this . AddChild ( materialEditLink ) ;
this . AddChild ( materialRemoveLink ) ;
this . Height = 35 ;
}
}
private class PresetListControl : ScrollableWidget
{
private FlowLayoutWidget topToBottomItemList ;
public PresetListControl ( )
{
this . AnchorAll ( ) ;
this . AutoScroll = true ;
this . ScrollArea . HAnchor | = Agg . UI . HAnchor . ParentLeftRight ;
topToBottomItemList = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
topToBottomItemList . HAnchor = Agg . UI . HAnchor . Max_FitToChildren_ParentWidth ;
topToBottomItemList . Margin = new BorderDouble ( top : 3 ) ;
base . AddChild ( topToBottomItemList ) ;
}
public override void AddChild ( GuiWidget child , int indexInChildrenList = - 1 )
{
FlowLayoutWidget itemHolder = new FlowLayoutWidget ( ) ;
itemHolder . Margin = new BorderDouble ( 0 , 0 , 0 , 0 ) ;
itemHolder . HAnchor = Agg . UI . HAnchor . Max_FitToChildren_ParentWidth ;
itemHolder . AddChild ( child ) ;
itemHolder . VAnchor = VAnchor . FitToChildren ;
topToBottomItemList . AddChild ( itemHolder , indexInChildrenList ) ;
}
}
}
2014-04-06 19:26:41 -07:00
}