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 .
* /
2016-10-05 14:57:31 -07:00
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using MatterHackers.Agg ;
2017-08-20 02:34:39 -07:00
using MatterHackers.Agg.Platform ;
2016-06-11 16:24:30 -07:00
using MatterHackers.Agg.UI ;
2016-10-05 14:57:31 -07:00
using MatterHackers.Localizations ;
2016-06-11 16:24:30 -07:00
using MatterHackers.MatterControl.CustomWidgets ;
using MatterHackers.MatterControl.SlicerConfiguration ;
namespace MatterHackers.MatterControl
{
2017-11-08 15:56:37 -08:00
public class SelectPartsOfPrinterToImport : DialogPage
2016-06-20 15:45:17 -07:00
{
2017-03-01 13:48:49 -08:00
private string importMessage = "Select what you would like to merge into your current profile." . Localize ( ) ;
2016-06-21 17:06:01 -07:00
2016-10-05 14:57:31 -07:00
private string settingsFilePath ;
private PrinterSettings settingsToImport ;
private int selectedMaterial = - 1 ;
private int selectedQuality = - 1 ;
2016-06-20 15:45:17 -07:00
2016-10-05 14:57:31 -07:00
private PrinterSettingsLayer destinationLayer ;
private string sectionName ;
2016-06-21 17:06:01 -07:00
private bool isMergeIntoUserLayer = false ;
2017-08-24 00:16:31 -07:00
public SelectPartsOfPrinterToImport ( string settingsFilePath , PrinterSettingsLayer destinationLayer , string sectionName = null )
2016-06-20 15:45:17 -07:00
{
2017-08-23 23:33:28 -07:00
this . WindowTitle = "Import Wizard" ;
2017-08-24 00:16:31 -07:00
this . HeaderText = "Select What to Import" . Localize ( ) ;
2017-08-23 23:33:28 -07:00
2016-06-21 17:06:01 -07:00
this . isMergeIntoUserLayer = destinationLayer = = ActiveSliceSettings . Instance . UserLayer ;
2016-06-21 16:36:11 -07:00
this . destinationLayer = destinationLayer ;
2016-06-21 17:06:01 -07:00
this . sectionName = sectionName ;
2016-08-10 09:49:32 -07:00
// TODO: Need to handle load failures for import attempts
2016-06-20 15:45:17 -07:00
settingsToImport = PrinterSettings . LoadFile ( settingsFilePath ) ;
this . settingsFilePath = settingsFilePath ;
var scrollWindow = new ScrollableWidget ( )
{
AutoScroll = true ,
2017-08-07 15:47:27 -07:00
HAnchor = HAnchor . Stretch ,
VAnchor = VAnchor . Stretch ,
2016-06-20 15:45:17 -07:00
} ;
2017-08-07 15:47:27 -07:00
scrollWindow . ScrollArea . HAnchor = HAnchor . Stretch ;
2016-06-20 15:45:17 -07:00
contentRow . AddChild ( scrollWindow ) ;
var container = new FlowLayoutWidget ( FlowDirection . TopToBottom )
{
2017-08-07 15:47:27 -07:00
HAnchor = HAnchor . Stretch ,
2016-06-20 15:45:17 -07:00
} ;
scrollWindow . AddChild ( container ) ;
2016-06-21 17:06:01 -07:00
if ( isMergeIntoUserLayer )
{
2016-07-27 17:17:33 -07:00
container . AddChild ( new WrappedTextWidget ( importMessage , textColor : ActiveTheme . Instance . PrimaryTextColor ) ) ;
2016-06-21 17:06:01 -07:00
}
2016-06-20 15:45:17 -07:00
// add in the check boxes to select what to import
container . AddChild ( new TextWidget ( "Main Settings:" )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
2016-06-21 17:06:01 -07:00
Margin = new BorderDouble ( 0 , 3 , 0 , isMergeIntoUserLayer ? 10 : 0 ) ,
2016-06-20 15:45:17 -07:00
} ) ;
2016-06-21 15:39:18 -07:00
var mainProfileRadioButton = new RadioButton ( "Printer Profile" )
2016-06-20 15:45:17 -07:00
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 5 , 0 ) ,
2017-08-07 15:47:27 -07:00
HAnchor = HAnchor . Left ,
2016-06-20 15:45:17 -07:00
Checked = true ,
} ;
2016-06-21 15:39:18 -07:00
container . AddChild ( mainProfileRadioButton ) ;
2016-06-20 15:45:17 -07:00
if ( settingsToImport . QualityLayers . Count > 0 )
{
container . AddChild ( new TextWidget ( "Quality Presets:" )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 0 , 3 , 0 , 15 ) ,
} ) ;
2016-06-21 15:39:18 -07:00
int buttonIndex = 0 ;
2016-06-20 15:45:17 -07:00
foreach ( var qualitySetting in settingsToImport . QualityLayers )
{
2016-06-21 15:39:18 -07:00
RadioButton qualityButton = new RadioButton ( qualitySetting . Name )
2016-06-20 15:45:17 -07:00
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 5 , 0 , 0 , 0 ) ,
2017-08-07 15:47:27 -07:00
HAnchor = HAnchor . Left ,
2016-06-21 15:39:18 -07:00
} ;
container . AddChild ( qualityButton ) ;
int localButtonIndex = buttonIndex ;
qualityButton . CheckedStateChanged + = ( s , e ) = >
{
if ( qualityButton . Checked )
{
selectedQuality = localButtonIndex ;
}
else
{
selectedQuality = - 1 ;
}
} ;
buttonIndex + + ;
2016-06-20 15:45:17 -07:00
}
}
if ( settingsToImport . MaterialLayers . Count > 0 )
{
container . AddChild ( new TextWidget ( "Material Presets:" )
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 0 , 3 , 0 , 15 ) ,
} ) ;
2016-06-21 15:39:18 -07:00
int buttonIndex = 0 ;
2016-06-20 15:45:17 -07:00
foreach ( var materialSetting in settingsToImport . MaterialLayers )
{
2016-06-21 15:39:18 -07:00
RadioButton materialButton = new RadioButton ( materialSetting . Name )
2016-06-20 15:45:17 -07:00
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
Margin = new BorderDouble ( 5 , 0 ) ,
2017-08-07 15:47:27 -07:00
HAnchor = HAnchor . Left ,
2016-06-21 15:39:18 -07:00
} ;
container . AddChild ( materialButton ) ;
int localButtonIndex = buttonIndex ;
materialButton . CheckedStateChanged + = ( s , e ) = >
{
if ( materialButton . Checked )
{
selectedMaterial = localButtonIndex ;
}
else
{
selectedMaterial = - 1 ;
}
} ;
buttonIndex + + ;
2016-06-20 15:45:17 -07:00
}
}
2016-06-21 17:06:01 -07:00
var mergeButtonTitle = this . isMergeIntoUserLayer ? "Merge" . Localize ( ) : "Import" . Localize ( ) ;
2016-10-05 14:57:31 -07:00
var mergeButton = textImageButtonFactory . Generate ( mergeButtonTitle ) ;
2016-06-20 15:45:17 -07:00
mergeButton . Name = "Merge Profile" ;
2016-10-05 14:57:31 -07:00
mergeButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( ( ) = >
2016-06-21 15:39:18 -07:00
{
2016-11-07 12:27:56 -08:00
bool copyName = false ;
2016-10-05 14:57:31 -07:00
PrinterSettingsLayer sourceLayer = null ;
if ( selectedMaterial > - 1 )
2016-06-20 15:45:17 -07:00
{
2016-10-05 14:57:31 -07:00
sourceLayer = settingsToImport . MaterialLayers [ selectedMaterial ] ;
2016-11-07 12:27:56 -08:00
copyName = true ;
2016-06-21 15:39:18 -07:00
}
2016-10-05 14:57:31 -07:00
else if ( selectedQuality > - 1 )
2016-06-21 16:36:11 -07:00
{
2016-10-05 14:57:31 -07:00
sourceLayer = settingsToImport . QualityLayers [ selectedQuality ] ;
2016-11-07 12:27:56 -08:00
copyName = true ;
2016-06-21 16:36:11 -07:00
}
2016-06-20 15:45:17 -07:00
2016-10-05 14:57:31 -07:00
List < PrinterSettingsLayer > sourceFilter ;
if ( selectedQuality = = - 1 & & selectedMaterial = = - 1 )
2016-06-21 15:39:18 -07:00
{
2016-10-05 14:57:31 -07:00
sourceFilter = new List < PrinterSettingsLayer > ( )
2016-06-21 15:39:18 -07:00
{
2016-10-05 14:57:31 -07:00
settingsToImport . OemLayer ,
settingsToImport . UserLayer
} ;
2016-06-21 15:39:18 -07:00
}
2016-10-05 14:57:31 -07:00
else
2016-06-21 16:36:11 -07:00
{
2016-10-05 14:57:31 -07:00
sourceFilter = new List < PrinterSettingsLayer > ( )
{
sourceLayer
} ;
2016-06-21 16:36:11 -07:00
}
2016-10-05 14:57:31 -07:00
2016-11-07 12:27:56 -08:00
ActiveSliceSettings . Instance . Merge ( destinationLayer , settingsToImport , sourceFilter , copyName ) ;
2016-10-05 14:57:31 -07:00
this . Parents < SystemWindow > ( ) . FirstOrDefault ( ) ? . CloseOnIdle ( ) ;
} ) ;
2017-08-23 17:27:30 -07:00
this . AddPageAction ( mergeButton ) ;
2016-10-05 14:57:31 -07:00
if ( settingsToImport . QualityLayers . Count = = 0 & & settingsToImport . MaterialLayers . Count = = 0 )
2016-06-21 15:39:18 -07:00
{
2016-10-05 14:57:31 -07:00
// Only main setting so don't ask what to merge just do it.
UiThread . RunOnIdle ( ( ) = >
2016-06-20 15:45:17 -07:00
{
2016-10-05 14:57:31 -07:00
var sourceFilter = new List < PrinterSettingsLayer > ( )
2016-06-20 15:45:17 -07:00
{
2016-10-05 14:57:31 -07:00
settingsToImport . OemLayer ? ? new PrinterSettingsLayer ( ) ,
settingsToImport . UserLayer ? ? new PrinterSettingsLayer ( )
} ;
2016-06-20 15:45:17 -07:00
2016-11-07 12:27:56 -08:00
ActiveSliceSettings . Instance . Merge ( destinationLayer , settingsToImport , sourceFilter , false ) ;
2016-06-15 11:49:57 -07:00
2016-10-05 14:57:31 -07:00
string successMessage = importPrinterSuccessMessage . FormatWith ( Path . GetFileNameWithoutExtension ( settingsFilePath ) ) ;
if ( ! isMergeIntoUserLayer )
{
string sourceName = isMergeIntoUserLayer ? Path . GetFileNameWithoutExtension ( settingsFilePath ) : destinationLayer [ SettingsKey . layer_name ] ;
2017-03-01 13:48:49 -08:00
string importSettingSuccessMessage = "You have successfully imported a new {1} setting. You can find '{0}' in your list of {1} settings." . Localize ( ) ;
successMessage = importSettingSuccessMessage . FormatWith ( sourceName , sectionName ) ;
2016-10-05 14:57:31 -07:00
}
2016-06-15 11:49:57 -07:00
2016-10-05 14:57:31 -07:00
WizardWindow . ChangeToPage ( new ImportSucceeded ( successMessage )
{
WizardWindow = this . WizardWindow ,
} ) ;
} ) ;
2016-06-21 17:06:01 -07:00
}
2016-06-15 11:49:57 -07:00
}
2016-10-05 14:57:31 -07:00
2017-03-01 13:48:49 -08:00
private string importPrinterSuccessMessage = "Settings have been merged into your current printer." . Localize ( ) ;
2016-06-15 11:49:57 -07:00
}
2017-11-08 15:56:37 -08:00
public class ImportSucceeded : DialogPage
2016-06-17 14:44:35 -07:00
{
2016-06-21 15:39:18 -07:00
public ImportSucceeded ( string successMessage ) :
2017-12-04 10:39:08 -08:00
base ( "Done" . Localize ( ) )
2016-06-17 14:44:35 -07:00
{
2017-08-23 23:33:28 -07:00
this . WindowTitle = "Import Wizard" . Localize ( ) ;
2017-08-24 00:16:31 -07:00
this . HeaderText = "Import Successful" . Localize ( ) ;
2017-08-23 17:27:30 -07:00
contentRow . AddChild ( new WrappedTextWidget ( successMessage , textColor : ActiveTheme . Instance . PrimaryTextColor ) ) ;
2016-06-17 14:44:35 -07:00
}
}
2017-11-08 15:56:37 -08:00
public class ImportSettingsPage : DialogPage
2016-06-15 11:49:57 -07:00
{
2016-10-05 14:57:31 -07:00
private RadioButton newPrinterButton ;
private RadioButton mergeButton ;
private RadioButton newQualityPresetButton ;
private RadioButton newMaterialPresetButton ;
2016-06-11 16:24:30 -07:00
2017-08-24 00:16:31 -07:00
public ImportSettingsPage ( )
2016-06-11 16:24:30 -07:00
{
2017-08-23 23:33:28 -07:00
this . WindowTitle = "Import Settings" . Localize ( ) ;
2017-08-24 00:16:31 -07:00
this . HeaderText = "Import Wizard" . Localize ( ) ;
2017-08-23 15:51:29 -07:00
2016-06-13 15:28:56 -07:00
var container = new FlowLayoutWidget ( FlowDirection . TopToBottom )
{
2017-08-07 15:47:27 -07:00
HAnchor = HAnchor . Stretch ,
2016-06-13 15:28:56 -07:00
} ;
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
{
2017-08-20 02:34:39 -07:00
AggContext . FileDialogs . 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 ;
2017-08-23 17:27:30 -07:00
this . AddPageAction ( importButton ) ;
2016-06-11 16:24:30 -07:00
}
2016-06-13 15:28:56 -07:00
private GuiWidget CreateDetailInfo ( string detailText )
{
2016-07-27 17:17:33 -07:00
var wrappedText = new WrappedTextWidget ( detailText )
2016-06-13 15:28:56 -07:00
{
TextColor = ActiveTheme . Instance . PrimaryTextColor ,
} ;
2017-02-01 16:39:24 -08:00
var container = new GuiWidget ( )
2016-06-13 15:28:56 -07:00
{
2017-08-07 15:47:27 -07:00
HAnchor = HAnchor . Stretch ,
VAnchor = VAnchor . Fit ,
2016-06-13 15:28:56 -07:00
Margin = new BorderDouble ( 25 , 15 , 5 , 5 ) ,
} ;
container . AddChild ( wrappedText ) ;
return container ;
}
2017-11-07 14:15:34 -08:00
public static void ImportFromExisting ( string settingsFilePath )
{
if ( ProfileManager . ImportFromExisting ( settingsFilePath ) )
{
2017-12-04 14:14:12 -08:00
string importPrinterSuccessMessage = "You have successfully imported a new printer profile. You can find '{0}' in your list of available printers." . Localize ( ) ;
2017-11-08 15:56:37 -08:00
DialogWindow . Show (
2017-11-07 14:15:34 -08:00
new ImportSucceeded ( importPrinterSuccessMessage . FormatWith ( Path . GetFileNameWithoutExtension ( settingsFilePath ) ) ) ) ;
}
else
{
displayFailedToImportMessage ( settingsFilePath ) ;
}
}
2016-06-21 15:39:18 -07:00
2016-06-11 16:24:30 -07:00
private void ImportSettingsFile ( string settingsFilePath )
{
2016-10-05 14:57:31 -07:00
if ( newPrinterButton . Checked )
2016-06-11 16:24:30 -07:00
{
2017-11-07 14:15:34 -08:00
ImportFromExisting ( settingsFilePath ) ;
2016-06-15 11:49:57 -07:00
}
2016-10-05 14:57:31 -07:00
else if ( mergeButton . Checked )
2016-06-15 11:49:57 -07:00
{
MergeSettings ( settingsFilePath ) ;
}
2016-10-05 14:57:31 -07:00
else if ( newQualityPresetButton . Checked )
2016-06-15 11:49:57 -07:00
{
ImportToPreset ( settingsFilePath ) ;
}
2016-10-05 14:57:31 -07:00
else if ( newMaterialPresetButton . Checked )
2016-06-15 11:49:57 -07:00
{
ImportToPreset ( settingsFilePath ) ;
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 ) )
{
2016-06-21 16:36:11 -07:00
PrinterSettingsLayer newLayer ;
2016-06-21 17:06:01 -07:00
string sectionName = ( newMaterialPresetButton . Checked ) ? "Material" . Localize ( ) : "Quality" . Localize ( ) ;
2016-06-14 12:55:19 -07:00
string importType = Path . GetExtension ( settingsFilePath ) . ToLower ( ) ;
switch ( importType )
{
2016-07-14 11:36:14 -07:00
case ProfileManager . ProfileExtension :
2016-06-21 16:36:11 -07:00
newLayer = new PrinterSettingsLayer ( ) ;
2016-08-10 14:04:33 -07:00
newLayer [ SettingsKey . layer_name ] = Path . GetFileNameWithoutExtension ( settingsFilePath ) ;
2016-06-21 17:06:01 -07:00
2016-06-21 16:36:11 -07:00
if ( newQualityPresetButton . Checked )
{
ActiveSliceSettings . Instance . QualityLayers . Add ( newLayer ) ;
}
else
{
// newMaterialPresetButton.Checked
ActiveSliceSettings . Instance . MaterialLayers . Add ( newLayer ) ;
}
2016-06-14 12:55:19 -07:00
// open a wizard to ask what to import to the preset
2016-06-21 17:06:01 -07:00
WizardWindow . ChangeToPage ( new SelectPartsOfPrinterToImport ( settingsFilePath , newLayer , sectionName ) ) ;
2016-06-21 16:36:11 -07:00
2016-06-14 12:55:19 -07:00
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
2016-08-10 16:38:58 -07:00
bool containsValidSetting = false ;
2016-06-14 12:55:19 -07:00
{
2016-06-21 16:36:11 -07:00
newLayer = new PrinterSettingsLayer ( ) ;
2016-06-20 12:47:10 -07:00
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
2016-10-05 14:57:31 -07:00
foreach ( var keyName in PrinterSettings . KnownSettings )
2016-06-14 12:55:19 -07:00
{
2016-10-05 14:57:31 -07:00
if ( ActiveSliceSettings . Instance . Contains ( keyName ) )
2016-06-14 12:55:19 -07:00
{
2016-08-10 16:38:58 -07:00
containsValidSetting = true ;
2016-10-05 14:57:31 -07:00
string currentValue = ActiveSliceSettings . Instance . GetValue ( keyName , baseAndOEMCascade ) . Trim ( ) ;
string newValue ;
2016-08-10 16:38:58 -07:00
// Compare the value to import to the layer cascade value and only set if different
2016-10-05 14:57:31 -07:00
if ( settingsToImport . TryGetValue ( keyName , out newValue )
& & currentValue ! = newValue )
2016-08-10 16:38:58 -07:00
{
2016-10-05 14:57:31 -07:00
newLayer [ keyName ] = newValue ;
2016-08-10 16:38:58 -07:00
}
2016-06-14 12:55:19 -07:00
}
}
2016-10-05 14:57:31 -07:00
if ( containsValidSetting )
2016-06-20 12:56:11 -07:00
{
2016-08-10 16:38:58 -07:00
if ( newMaterialPresetButton . Checked )
{
ActiveSliceSettings . Instance . MaterialLayers . Add ( newLayer ) ;
}
else
{
ActiveSliceSettings . Instance . QualityLayers . Add ( newLayer ) ;
}
ActiveSliceSettings . Instance . Save ( ) ;
2017-12-04 14:14:12 -08:00
string importSettingSuccessMessage = "You have successfully imported a new {1} setting. You can find '{0}' in your list of {1} settings." . Localize ( ) ;
2016-08-10 16:38:58 -07:00
WizardWindow . ChangeToPage ( new ImportSucceeded ( importSettingSuccessMessage . FormatWith ( Path . GetFileNameWithoutExtension ( settingsFilePath ) , sectionName ) )
{
WizardWindow = this . WizardWindow ,
} ) ;
2016-06-20 12:56:11 -07:00
}
else
{
2016-08-10 16:38:58 -07:00
displayFailedToImportMessage ( settingsFilePath ) ;
2016-06-20 12:56:11 -07:00
}
2016-06-14 12:55:19 -07:00
}
2016-08-11 14:59:23 -07:00
2016-06-14 12:55:19 -07:00
break ;
default :
// Did not figure out what this file is, let the user know we don't understand it
2017-10-18 19:54:06 -07:00
StyledMessageBox . ShowMessageBox ( "Oops! Unable to recognize settings file '{0}'." . Localize ( ) . FormatWith ( Path . GetFileName ( settingsFilePath ) ) , "Unable to Import" . Localize ( ) ) ;
2016-06-14 12:55:19 -07:00
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 )
{
2016-07-14 11:36:14 -07:00
case ProfileManager . ProfileExtension :
2016-06-21 16:36:11 -07:00
WizardWindow . ChangeToPage ( new SelectPartsOfPrinterToImport ( settingsFilePath , ActiveSliceSettings . Instance . UserLayer ) ) ;
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-08-11 14:59:23 -07:00
// create a scope for variables
2016-06-11 16:24:30 -07:00
{
2016-08-11 14:59:23 -07:00
var settingsToImport = PrinterSettingsLayer . LoadFromIni ( settingsFilePath ) ;
bool containsValidSetting = false ;
2016-06-11 16:24:30 -07:00
var activeSettings = ActiveSliceSettings . Instance ;
2016-10-05 14:57:31 -07:00
foreach ( var keyName in PrinterSettings . KnownSettings )
2016-06-11 16:24:30 -07:00
{
2016-10-05 14:57:31 -07:00
if ( activeSettings . Contains ( keyName ) )
2016-06-11 16:24:30 -07:00
{
2016-08-10 16:38:58 -07:00
containsValidSetting = true ;
2016-10-05 14:57:31 -07:00
string currentValue = activeSettings . GetValue ( keyName ) . Trim ( ) ;
string newValue ;
2016-08-10 16:38:58 -07:00
// Compare the value to import to the layer cascade value and only set if different
2016-10-05 14:57:31 -07:00
if ( settingsToImport . TryGetValue ( keyName , out newValue )
& & currentValue ! = newValue )
2016-08-10 16:38:58 -07:00
{
2016-10-05 14:57:31 -07:00
activeSettings . UserLayer [ keyName ] = newValue ;
2016-08-10 16:38:58 -07:00
}
2016-06-11 16:24:30 -07:00
}
}
2016-08-11 14:59:23 -07:00
if ( containsValidSetting )
2016-08-10 16:38:58 -07:00
{
activeSettings . Save ( ) ;
}
else
{
displayFailedToImportMessage ( settingsFilePath ) ;
}
2016-08-11 14:59:23 -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
2017-10-18 19:54:06 -07:00
StyledMessageBox . ShowMessageBox ( "Oops! Unable to recognize settings file '{0}'." . Localize ( ) . FormatWith ( Path . GetFileName ( settingsFilePath ) ) , "Unable to Import" . Localize ( ) ) ;
2016-06-11 16:24:30 -07:00
break ;
}
}
Invalidate ( ) ;
}
2016-08-10 16:38:58 -07:00
2017-11-07 14:15:34 -08:00
private static void displayFailedToImportMessage ( string settingsFilePath )
2016-08-10 16:38:58 -07:00
{
2017-10-18 19:54:06 -07:00
StyledMessageBox . ShowMessageBox ( "Oops! Settings file '{0}' did not contain any settings we could import." . Localize ( ) . FormatWith ( Path . GetFileName ( settingsFilePath ) ) , "Unable to Import" . Localize ( ) ) ;
2016-08-10 16:38:58 -07:00
}
2016-06-11 16:24:30 -07:00
}
2016-10-05 14:57:31 -07:00
}