2016-06-11 16:24:30 -07:00
/ *
Copyright ( c ) 2016 , 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 MatterHackers.Agg.UI ;
using MatterHackers.MatterControl ;
using MatterHackers.MatterControl.CustomWidgets ;
using MatterHackers.MatterControl.SlicerConfiguration ;
using MatterHackers.Localizations ;
using System.IO ;
using MatterHackers.Agg ;
2016-06-20 12:47:10 -07:00
using System.Collections.Generic ;
2016-06-11 16:24:30 -07:00
namespace MatterHackers.MatterControl
{
2016-06-20 15:45:17 -07:00
public class SelectPartsOfPrinterToImport : WizardPage
{
static string importMessage = "Select the parts of the printer file that you would like to merge into your current profile." . Localize ( ) ;
string settingsFilePath ;
List < CheckBox > qualityChecks = new List < CheckBox > ( ) ;
List < CheckBox > materialChecks = new List < CheckBox > ( ) ;
PrinterSettings settingsToImport ;
public SelectPartsOfPrinterToImport ( string settingsFilePath ) :
base ( unlocalizedTextForTitle : "Import Wizard" )
{
settingsToImport = PrinterSettings . LoadFile ( settingsFilePath ) ;
this . headerLabel . Text = "Select Parts to Import" . Localize ( ) ;
this . settingsFilePath = settingsFilePath ;
var scrollWindow = new ScrollableWidget ( )
{
AutoScroll = true ,
HAnchor = HAnchor . ParentLeftRight ,
VAnchor = VAnchor . ParentBottomTop ,
} ;
scrollWindow . ScrollArea . HAnchor = HAnchor . ParentLeftRight ;
contentRow . AddChild ( scrollWindow ) ;
var container = new FlowLayoutWidget ( FlowDirection . TopToBottom )
{
HAnchor = HAnchor . ParentLeftRight ,
} ;
scrollWindow . AddChild ( container ) ;
var selectPartsMessage = new WrappedTextWidget ( importMessage , 10 , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
container . AddChild ( selectPartsMessage ) ;
// add in the check boxes to select what to import
container . AddChild ( new TextWidget ( "Main Settings:" )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 0 , 3 , 0 , 10 ) ,
} ) ;
var mainProfileCheckBox = new CheckBox ( "Printer Profile" )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 5 , 0 ) ,
HAnchor = HAnchor . ParentLeft ,
Checked = true ,
} ;
container . AddChild ( mainProfileCheckBox ) ;
if ( settingsToImport . QualityLayers . Count > 0 )
{
container . AddChild ( new TextWidget ( "Quality Presets:" )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 0 , 3 , 0 , 15 ) ,
} ) ;
foreach ( var qualitySetting in settingsToImport . QualityLayers )
{
qualityChecks . Add ( new CheckBox ( qualitySetting . Name )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 5 , 0 , 0 , 0 ) ,
HAnchor = HAnchor . ParentLeft ,
} ) ;
container . AddChild ( qualityChecks [ qualityChecks . Count - 1 ] ) ;
}
}
if ( settingsToImport . MaterialLayers . Count > 0 )
{
container . AddChild ( new TextWidget ( "Material Presets:" )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 0 , 3 , 0 , 15 ) ,
} ) ;
foreach ( var materialSetting in settingsToImport . MaterialLayers )
{
materialChecks . Add ( new CheckBox ( materialSetting . Name )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 5 , 0 ) ,
HAnchor = HAnchor . ParentLeft ,
} ) ;
container . AddChild ( materialChecks [ materialChecks . Count - 1 ] ) ;
}
}
var mergeButton = textImageButtonFactory . Generate ( "Merge" . Localize ( ) ) ;
mergeButton . Name = "Merge Profile" ;
mergeButton . Click + = ( s , e ) = >
{
UiThread . RunOnIdle ( ( ) = > WizardWindow ? . Close ( ) ) ;
var activeSettings = ActiveSliceSettings . Instance ;
var layerCascade = new List < PrinterSettingsLayer >
{
ActiveSliceSettings . Instance . OemLayer ,
ActiveSliceSettings . Instance . BaseLayer ,
ActiveSliceSettings . Instance . UserLayer ,
} ;
foreach ( var item in settingsToImport . BaseLayer )
{
// Compare the value to import to the layer cascade value and only set if different
string currentValue = activeSettings . GetValue ( item . Key , layerCascade ) . Trim ( ) ;
string importValue = settingsToImport . GetValue ( item . Key , layerCascade ) . Trim ( ) ;
if ( currentValue ! = item . Value )
{
activeSettings . UserLayer [ item . Key ] = item . Value ;
}
}
activeSettings . SaveChanges ( ) ;
UiThread . RunOnIdle ( ApplicationController . Instance . ReloadAdvancedControlsPanel ) ;
} ;
footerRow . AddChild ( mergeButton ) ;
footerRow . AddChild ( new HorizontalSpacer ( ) ) ;
footerRow . AddChild ( cancelButton ) ;
}
}
2016-06-15 11:49:57 -07:00
public class ImportToPrinterSucceeded : WizardPage
2016-06-11 16:24:30 -07:00
{
2016-06-15 11:49:57 -07:00
static string successMessage = "You have successfully imported a new printer profile. You can find '{0}' in your list of available printers." . Localize ( ) ;
public ImportToPrinterSucceeded ( string newProfileName ) :
base ( "Done" , "Import Wizard" )
{
this . headerLabel . Text = "Import Successful" . Localize ( ) ;
successMessage = successMessage . FormatWith ( newProfileName ) ;
var container = new FlowLayoutWidget ( FlowDirection . TopToBottom )
{
HAnchor = HAnchor . ParentLeftRight ,
} ;
contentRow . AddChild ( container ) ;
var successMessageWidget = new WrappedTextWidget ( successMessage , 10 , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
container . AddChild ( successMessageWidget ) ;
2016-06-20 12:56:11 -07:00
footerRow . AddChild ( new HorizontalSpacer ( ) ) ;
footerRow . AddChild ( cancelButton ) ;
2016-06-15 11:49:57 -07:00
}
}
2016-06-17 14:44:35 -07:00
public class ImportToSettingSucceeded : WizardPage
{
static string successMessage = "You have successfully imported a new {0} setting. You can find '{1}' in your list of {2} settings." . Localize ( ) ;
string settingType ;
public ImportToSettingSucceeded ( string newProfileName , string settingType ) :
base ( "Done" , "Import Wizard" )
{
this . settingType = settingType ;
this . headerLabel . Text = "Import Successful" . Localize ( ) ;
successMessage = successMessage . FormatWith ( settingType , newProfileName , settingType ) ;
var container = new FlowLayoutWidget ( FlowDirection . TopToBottom )
{
HAnchor = HAnchor . ParentLeftRight ,
} ;
contentRow . AddChild ( container ) ;
var successMessageWidget = new WrappedTextWidget ( successMessage , 10 , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
container . AddChild ( successMessageWidget ) ;
2016-06-20 12:56:11 -07:00
footerRow . AddChild ( new HorizontalSpacer ( ) ) ;
footerRow . AddChild ( cancelButton ) ;
2016-06-17 14:44:35 -07:00
}
}
public class ImportSettingsPage : WizardPage
2016-06-15 11:49:57 -07:00
{
RadioButton newPrinterButton ;
RadioButton mergeButton ;
RadioButton newQualityPresetButton ;
RadioButton newMaterialPresetButton ;
2016-06-11 16:24:30 -07:00
public ImportSettingsPage ( ) :
2016-06-14 11:45:18 -07:00
base ( "Cancel" , "Import Wizard" )
2016-06-11 16:24:30 -07:00
{
2016-06-13 15:28:56 -07:00
var container = new FlowLayoutWidget ( FlowDirection . TopToBottom )
{
HAnchor = HAnchor . ParentLeftRight ,
} ;
2016-06-11 16:24:30 -07:00
contentRow . AddChild ( container ) ;
2016-06-16 17:56:12 -07:00
if ( true )
{
container . AddChild ( new TextWidget ( "Merge Into:" )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 0 , 0 , 0 , 5 ) ,
} ) ;
// merge into current settings
mergeButton = new RadioButton ( "Current" . Localize ( ) , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
mergeButton . Checked = true ;
container . AddChild ( mergeButton ) ;
container . AddChild ( new TextWidget ( "Create New:" )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 0 , 0 , 0 , 15 ) ,
} ) ;
// add new profile
newPrinterButton = new RadioButton ( "Printer" . Localize ( ) , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
container . AddChild ( newPrinterButton ) ;
// add as quality preset
newQualityPresetButton = new RadioButton ( "Quality preset" . Localize ( ) , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
container . AddChild ( newQualityPresetButton ) ;
// add as material preset
newMaterialPresetButton = new RadioButton ( "Material preset" . Localize ( ) , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
2016-06-20 12:56:11 -07:00
container . AddChild ( newMaterialPresetButton ) ;
2016-06-16 17:56:12 -07:00
}
else
{
// add new profile
newPrinterButton = new RadioButton ( "Import as new printer profile" . Localize ( ) , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
newPrinterButton . Checked = true ;
container . AddChild ( newPrinterButton ) ;
container . AddChild (
CreateDetailInfo ( "Add a new printer profile to your list of available printers.\nThis will not change your current settings." )
) ;
// merge into current settings
mergeButton = new RadioButton ( "Merge into current printer profile" . Localize ( ) , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
container . AddChild ( mergeButton ) ;
container . AddChild (
CreateDetailInfo ( "Merge settings and presets (if any) into your current profile. \nYou will still be able to revert to the factory settings at any time." )
) ;
// add as quality preset
newQualityPresetButton = new RadioButton ( "Import settings as new QUALITY preset" . Localize ( ) , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
container . AddChild ( newQualityPresetButton ) ;
container . AddChild (
CreateDetailInfo ( "Add new quality preset with the settings from this import." )
) ;
// add as material preset
newMaterialPresetButton = new RadioButton ( "Import settings as new MATERIAL preset" . Localize ( ) , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
container . AddChild ( newMaterialPresetButton ) ;
container . AddChild (
CreateDetailInfo ( "Add new material preset with the settings from this import." )
) ;
}
2016-06-13 15:28:56 -07:00
2016-06-14 11:45:18 -07:00
var importButton = textImageButtonFactory . Generate ( "Choose File" . Localize ( ) ) ;
2016-06-15 11:49:57 -07:00
importButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( ( ) = >
2016-06-11 16:24:30 -07:00
{
FileDialog . OpenFileDialog (
2016-06-13 15:28:56 -07:00
new OpenFileDialogParams ( "settings files|*.ini;*.printer;*.slice" ) ,
2016-06-15 11:49:57 -07:00
( dialogParams ) = >
{
if ( ! string . IsNullOrEmpty ( dialogParams . FileName ) )
{
ImportSettingsFile ( dialogParams . FileName ) ;
}
} ) ;
2016-06-11 16:24:30 -07:00
} ) ;
importButton . Visible = true ;
cancelButton . Visible = true ;
//Add buttons to buttonContainer
footerRow . AddChild ( importButton ) ;
footerRow . AddChild ( new HorizontalSpacer ( ) ) ;
footerRow . AddChild ( cancelButton ) ;
}
2016-06-13 15:28:56 -07:00
private GuiWidget CreateDetailInfo ( string detailText )
{
var wrappedText = new WrappedTextWidget ( detailText , 5 )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
} ;
var container = new GuiWidget ( HAnchor . ParentLeftRight , VAnchor . FitToChildren )
{
Margin = new BorderDouble ( 25 , 15 , 5 , 5 ) ,
} ;
container . AddChild ( wrappedText ) ;
return container ;
}
2016-06-11 16:24:30 -07:00
private void ImportSettingsFile ( string settingsFilePath )
{
2016-06-15 11:49:57 -07:00
if ( newPrinterButton . Checked )
2016-06-11 16:24:30 -07:00
{
2016-06-15 12:58:36 -07:00
ProfileManager . ImportFromExisting ( settingsFilePath ) ;
2016-06-15 11:49:57 -07:00
WizardWindow . ChangeToPage ( new ImportToPrinterSucceeded ( Path . GetFileNameWithoutExtension ( settingsFilePath ) ) ) ;
}
else if ( mergeButton . Checked )
{
MergeSettings ( settingsFilePath ) ;
}
else if ( newQualityPresetButton . Checked )
{
ImportToPreset ( settingsFilePath ) ;
2016-06-17 14:44:35 -07:00
WizardWindow . ChangeToPage ( new ImportToSettingSucceeded ( Path . GetFileNameWithoutExtension ( settingsFilePath ) , "Quality" . Localize ( ) )
2016-06-15 11:49:57 -07:00
{
WizardWindow = this . WizardWindow ,
} ) ;
}
else if ( newMaterialPresetButton . Checked )
{
ImportToPreset ( settingsFilePath ) ;
2016-06-17 14:44:35 -07:00
WizardWindow . ChangeToPage ( new ImportToSettingSucceeded ( Path . GetFileNameWithoutExtension ( settingsFilePath ) , "Material" . Localize ( ) )
2016-06-15 11:49:57 -07:00
{
WizardWindow = this . WizardWindow ,
} ) ;
2016-06-11 16:24:30 -07:00
}
}
2016-06-14 12:55:19 -07:00
private void ImportToPreset ( string settingsFilePath )
{
if ( ! string . IsNullOrEmpty ( settingsFilePath ) & & File . Exists ( settingsFilePath ) )
{
string importType = Path . GetExtension ( settingsFilePath ) . ToLower ( ) ;
switch ( importType )
{
case ".printer" :
// open a wizard to ask what to import to the preset
throw new NotImplementedException ( "need to import from 'MatterControl.printer' files" ) ;
break ;
2016-06-15 11:49:57 -07:00
case ".slice" : // legacy presets file extension
2016-06-14 12:55:19 -07:00
case ".ini" :
2016-06-15 14:50:12 -07:00
var settingsToImport = PrinterSettingsLayer . LoadFromIni ( settingsFilePath ) ;
2016-06-14 12:55:19 -07:00
string layerHeight ;
2016-06-21 09:38:37 -07:00
bool isSlic3r = importType = = ".slice" | | settingsToImport . TryGetValue ( SettingsKey . layer_height , out layerHeight ) ;
2016-06-14 12:55:19 -07:00
if ( isSlic3r )
{
2016-06-20 12:47:10 -07:00
var newLayer = new PrinterSettingsLayer ( ) ;
newLayer . Name = Path . GetFileNameWithoutExtension ( settingsFilePath ) ;
2016-06-17 14:44:35 -07:00
2016-06-20 12:47:10 -07:00
// Only be the base and oem layers (not the user, quality or material layer)
var baseAndOEMCascade = new List < PrinterSettingsLayer >
{
ActiveSliceSettings . Instance . OemLayer ,
ActiveSliceSettings . Instance . BaseLayer
} ;
2016-06-14 12:55:19 -07:00
foreach ( var item in settingsToImport )
{
2016-06-20 12:47:10 -07:00
string currentValue = ActiveSliceSettings . Instance . GetValue ( item . Key , baseAndOEMCascade ) . Trim ( ) ;
2016-06-14 12:55:19 -07:00
// Compare the value to import to the layer cascade value and only set if different
if ( currentValue ! = item . Value )
{
2016-06-20 12:47:10 -07:00
newLayer [ item . Key ] = item . Value ;
2016-06-14 12:55:19 -07:00
}
}
2016-06-20 12:56:11 -07:00
if ( newMaterialPresetButton . Checked )
{
ActiveSliceSettings . Instance . MaterialLayers . Add ( newLayer ) ;
}
else
{
ActiveSliceSettings . Instance . QualityLayers . Add ( newLayer ) ;
}
2016-06-14 12:55:19 -07:00
2016-06-20 12:47:10 -07:00
ActiveSliceSettings . Instance . SaveChanges ( ) ;
2016-06-14 12:55:19 -07:00
}
else
{
// looks like a cura file
throw new NotImplementedException ( "need to import from 'cure.ini' files" ) ;
}
break ;
default :
// Did not figure out what this file is, let the user know we don't understand it
StyledMessageBox . ShowMessageBox ( null , "Oops! Unable to recognize settings file '{0}'." . Localize ( ) . FormatWith ( Path . GetFileName ( settingsFilePath ) ) , "Unable to Import" . Localize ( ) ) ;
break ;
}
}
Invalidate ( ) ;
}
2016-06-14 11:45:18 -07:00
private void MergeSettings ( string settingsFilePath )
2016-06-11 16:24:30 -07:00
{
if ( ! string . IsNullOrEmpty ( settingsFilePath ) & & File . Exists ( settingsFilePath ) )
{
string importType = Path . GetExtension ( settingsFilePath ) . ToLower ( ) ;
switch ( importType )
{
case ".printer" :
2016-06-20 15:45:17 -07:00
WizardWindow . ChangeToPage ( new SelectPartsOfPrinterToImport ( settingsFilePath ) ) ;
2016-06-11 16:24:30 -07:00
break ;
2016-06-14 12:55:19 -07:00
case ".slice" : // old presets format
2016-06-11 16:24:30 -07:00
case ".ini" :
2016-06-15 14:50:12 -07:00
var settingsToImport = PrinterSettingsLayer . LoadFromIni ( settingsFilePath ) ;
2016-06-11 16:24:30 -07:00
string layerHeight ;
2016-06-21 09:38:37 -07:00
bool isSlic3r = settingsToImport . TryGetValue ( SettingsKey . layer_height , out layerHeight ) ;
2016-06-11 16:24:30 -07:00
if ( isSlic3r )
{
var activeSettings = ActiveSliceSettings . Instance ;
foreach ( var item in settingsToImport )
{
// Compare the value to import to the layer cascade value and only set if different
2016-06-15 17:18:39 -07:00
string currentValue = activeSettings . GetValue ( item . Key , null ) . Trim ( ) ;
2016-06-11 16:24:30 -07:00
if ( currentValue ! = item . Value )
{
activeSettings . UserLayer [ item . Key ] = item . Value ;
}
}
activeSettings . SaveChanges ( ) ;
UiThread . RunOnIdle ( ApplicationController . Instance . ReloadAdvancedControlsPanel ) ;
}
else
{
// looks like a cura file
throw new NotImplementedException ( "need to import from 'cure.ini' files" ) ;
}
2016-06-20 15:45:17 -07:00
WizardWindow . Close ( ) ;
2016-06-11 16:24:30 -07:00
break ;
default :
2016-06-20 15:45:17 -07:00
WizardWindow . Close ( ) ;
2016-06-11 16:24:30 -07:00
// Did not figure out what this file is, let the user know we don't understand it
StyledMessageBox . ShowMessageBox ( null , "Oops! Unable to recognize settings file '{0}'." . Localize ( ) . FormatWith ( Path . GetFileName ( settingsFilePath ) ) , "Unable to Import" . Localize ( ) ) ;
break ;
}
}
Invalidate ( ) ;
}
}
}