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
{
2016-06-21 15:39:18 -07:00
static string importMessage = "Select what you would like to merge into your current profile." . Localize ( ) ;
2016-06-21 17:06:01 -07:00
2016-06-20 15:45:17 -07:00
string settingsFilePath ;
PrinterSettings settingsToImport ;
2016-06-21 15:39:18 -07:00
int selectedMaterial = - 1 ;
int selectedQuality = - 1 ;
2016-06-20 15:45:17 -07:00
2016-06-21 16:36:11 -07:00
PrinterSettingsLayer destinationLayer ;
2016-06-21 17:06:01 -07:00
string sectionName ;
private bool isMergeIntoUserLayer = false ;
2016-06-21 16:36:11 -07:00
2016-06-21 17:06:01 -07:00
public SelectPartsOfPrinterToImport ( string settingsFilePath , PrinterSettingsLayer destinationLayer , string sectionName = null ) :
2016-06-20 15:45:17 -07:00
base ( unlocalizedTextForTitle : "Import Wizard" )
{
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-06-20 15:45:17 -07:00
settingsToImport = PrinterSettings . LoadFile ( settingsFilePath ) ;
2016-06-21 15:39:18 -07:00
this . headerLabel . Text = "Select What to Import" . Localize ( ) ;
2016-06-20 15:45:17 -07:00
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 ) ;
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 ) ,
HAnchor = HAnchor . ParentLeft ,
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 ) ,
HAnchor = HAnchor . ParentLeft ,
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 ) ,
HAnchor = HAnchor . ParentLeft ,
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 ( ) ;
var mergeButton = textImageButtonFactory . Generate ( mergeButtonTitle ) ;
2016-06-20 15:45:17 -07:00
mergeButton . Name = "Merge Profile" ;
2016-06-21 15:39:18 -07:00
mergeButton . Click + = ( s , e ) = > UiThread . RunOnIdle ( Merge ) ;
footerRow . AddChild ( mergeButton ) ;
footerRow . AddChild ( new HorizontalSpacer ( ) ) ;
footerRow . AddChild ( cancelButton ) ;
if ( settingsToImport . QualityLayers . Count = = 0 & & settingsToImport . MaterialLayers . Count = = 0 )
2016-06-20 15:45:17 -07:00
{
2016-06-21 15:39:18 -07:00
// Only main setting so don't ask what to merge just do it.
UiThread . RunOnIdle ( Merge ) ;
}
}
2016-06-20 15:45:17 -07:00
2016-06-21 15:39:18 -07:00
static string importPrinterSuccessMessage = "Settings have been merged into your current printer." . Localize ( ) ;
2016-06-20 15:45:17 -07:00
2016-06-21 15:39:18 -07:00
HashSet < string > skipKeys = new HashSet < string >
{
2016-08-10 14:04:33 -07:00
SettingsKey . layer_name ,
2016-06-21 15:39:18 -07:00
"layer_id" ,
} ;
void Merge ( )
{
var activeSettings = ActiveSliceSettings . Instance ;
var layerCascade = new List < PrinterSettingsLayer >
{
ActiveSliceSettings . Instance . OemLayer ,
ActiveSliceSettings . Instance . BaseLayer ,
2016-06-21 16:36:11 -07:00
destinationLayer ,
2016-06-21 15:39:18 -07:00
} ;
PrinterSettingsLayer layerToImport = settingsToImport . BaseLayer ;
if ( selectedMaterial > - 1 )
{
var material = settingsToImport . MaterialLayers [ selectedMaterial ] ;
foreach ( var item in material )
2016-06-20 15:45:17 -07:00
{
2016-06-21 15:39:18 -07:00
if ( ! skipKeys . Contains ( item . Key ) )
{
2016-06-21 16:36:11 -07:00
destinationLayer [ item . Key ] = item . Value ;
2016-06-21 15:39:18 -07:00
}
}
2016-06-21 16:36:11 -07:00
2016-08-10 14:04:33 -07:00
if ( ! isMergeIntoUserLayer & & material . ContainsKey ( SettingsKey . layer_name ) )
2016-06-21 16:36:11 -07:00
{
2016-08-10 14:04:33 -07:00
destinationLayer [ SettingsKey . layer_name ] = material [ SettingsKey . layer_name ] ;
2016-06-21 16:36:11 -07:00
}
2016-06-21 15:39:18 -07:00
}
else if ( selectedQuality > - 1 )
{
var quality = settingsToImport . QualityLayers [ selectedQuality ] ;
2016-06-20 15:45:17 -07:00
2016-06-21 15:39:18 -07:00
foreach ( var item in quality )
{
if ( ! skipKeys . Contains ( item . Key ) )
{
2016-06-21 16:36:11 -07:00
destinationLayer [ item . Key ] = item . Value ;
2016-06-21 15:39:18 -07:00
}
}
2016-06-21 16:36:11 -07:00
2016-08-10 14:04:33 -07:00
if ( ! isMergeIntoUserLayer & & quality . ContainsKey ( SettingsKey . layer_name ) )
2016-06-21 16:36:11 -07:00
{
2016-08-10 14:04:33 -07:00
destinationLayer [ SettingsKey . layer_name ] = quality [ SettingsKey . layer_name ] ;
2016-06-21 16:36:11 -07:00
}
2016-06-21 15:39:18 -07:00
}
else
{
foreach ( var item in layerToImport )
2016-06-20 15:45:17 -07:00
{
// 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 )
{
2016-06-21 16:36:11 -07:00
destinationLayer [ item . Key ] = item . Value ;
2016-06-20 15:45:17 -07:00
}
}
2016-06-21 15:39:18 -07:00
}
2016-06-20 15:45:17 -07:00
2016-07-18 15:21:15 -07:00
activeSettings . Save ( ) ;
2016-06-15 11:49:57 -07:00
2016-06-21 15:39:18 -07:00
UiThread . RunOnIdle ( ApplicationController . Instance . ReloadAdvancedControlsPanel ) ;
2016-06-15 11:49:57 -07:00
2016-06-21 17:06:01 -07:00
string successMessage = importPrinterSuccessMessage . FormatWith ( Path . GetFileNameWithoutExtension ( settingsFilePath ) ) ;
if ( ! isMergeIntoUserLayer )
{
2016-08-10 14:04:33 -07:00
string sourceName = isMergeIntoUserLayer ? Path . GetFileNameWithoutExtension ( settingsFilePath ) : destinationLayer [ SettingsKey . layer_name ] ;
2016-06-21 17:06:01 -07:00
successMessage = ImportSettingsPage . importSettingSuccessMessage . FormatWith ( sourceName , sectionName ) ;
}
WizardWindow . ChangeToPage ( new ImportSucceeded ( successMessage )
2016-06-15 11:49:57 -07:00
{
2016-06-21 15:39:18 -07:00
WizardWindow = this . WizardWindow ,
} ) ;
2016-06-15 11:49:57 -07:00
}
}
2016-06-21 15:39:18 -07:00
public class ImportSucceeded : WizardPage
2016-06-17 14:44:35 -07:00
{
2016-06-21 15:39:18 -07:00
public ImportSucceeded ( string successMessage ) :
2016-06-17 14:44:35 -07:00
base ( "Done" , "Import Wizard" )
{
this . headerLabel . Text = "Import Successful" . Localize ( ) ;
var container = new FlowLayoutWidget ( FlowDirection . TopToBottom )
{
HAnchor = HAnchor . ParentLeftRight ,
} ;
contentRow . AddChild ( container ) ;
2016-07-27 17:17:33 -07:00
var successMessageWidget = new WrappedTextWidget ( successMessage , textColor : ActiveTheme . Instance . PrimaryTextColor ) ;
2016-06-17 14:44:35 -07:00
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 )
{
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 ,
} ;
var container = new GuiWidget ( HAnchor . ParentLeftRight , VAnchor . FitToChildren )
{
Margin = new BorderDouble ( 25 , 15 , 5 , 5 ) ,
} ;
container . AddChild ( wrappedText ) ;
return container ;
}
2016-06-21 15:39:18 -07:00
static string importPrinterSuccessMessage = "You have successfully imported a new printer profile. You can find '{0}' in your list of available printers." . Localize ( ) ;
2016-06-21 17:06:01 -07:00
internal static string importSettingSuccessMessage = "You have successfully imported a new {1} setting. You can find '{0}' in your list of {1} settings." . Localize ( ) ;
2016-06-21 15:39:18 -07:00
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-08-10 16:38:58 -07:00
if ( ProfileManager . ImportFromExisting ( settingsFilePath ) )
2016-06-21 15:39:18 -07:00
{
2016-08-10 16:38:58 -07:00
WizardWindow . ChangeToPage ( new ImportSucceeded ( importPrinterSuccessMessage . FormatWith ( Path . GetFileNameWithoutExtension ( settingsFilePath ) ) )
{
WizardWindow = this . WizardWindow ,
} ) ;
}
else
{
displayFailedToImportMessage ( settingsFilePath ) ;
}
2016-06-15 11:49:57 -07:00
}
else if ( mergeButton . Checked )
{
MergeSettings ( settingsFilePath ) ;
}
else if ( newQualityPresetButton . Checked )
{
ImportToPreset ( settingsFilePath ) ;
}
else if ( newMaterialPresetButton . Checked )
{
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 ) ;
2016-06-21 17:06:01 -07:00
2016-06-21 16:36:11 -07:00
}
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
foreach ( var item in settingsToImport )
{
2016-08-10 16:38:58 -07:00
if ( ActiveSliceSettings . Instance . Contains ( item . Key ) )
2016-06-14 12:55:19 -07:00
{
2016-08-10 16:38:58 -07:00
containsValidSetting = true ;
string currentValue = ActiveSliceSettings . Instance . GetValue ( item . Key , baseAndOEMCascade ) . Trim ( ) ;
// Compare the value to import to the layer cascade value and only set if different
if ( currentValue ! = item . Value )
{
newLayer [ item . Key ] = item . Value ;
}
2016-06-14 12:55:19 -07:00
}
2016-08-10 16:38:58 -07:00
2016-06-14 12:55:19 -07:00
}
2016-08-10 16:38:58 -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 ( ) ;
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
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 )
{
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 ;
foreach ( var item in settingsToImport )
{
2016-08-11 14:59:23 -07:00
if ( activeSettings . Contains ( item . Key ) )
2016-06-11 16:24:30 -07:00
{
2016-08-10 16:38:58 -07:00
containsValidSetting = true ;
2016-08-11 13:01:34 -07:00
string currentValue = activeSettings . GetValue ( item . Key ) . Trim ( ) ;
2016-08-10 16:38:58 -07:00
// Compare the value to import to the layer cascade value and only set if different
if ( currentValue ! = item . Value )
{
activeSettings . UserLayer [ item . Key ] = item . Value ;
}
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 ( ) ;
2016-06-11 16:24:30 -07:00
2016-08-10 16:38:58 -07:00
UiThread . RunOnIdle ( ApplicationController . Instance . ReloadAdvancedControlsPanel ) ;
}
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
StyledMessageBox . ShowMessageBox ( null , "Oops! Unable to recognize settings file '{0}'." . Localize ( ) . FormatWith ( Path . GetFileName ( settingsFilePath ) ) , "Unable to Import" . Localize ( ) ) ;
break ;
}
}
Invalidate ( ) ;
}
2016-08-10 16:38:58 -07:00
private void displayFailedToImportMessage ( string settingsFilePath )
{
StyledMessageBox . ShowMessageBox ( null , "Oops! Settings file '{0}' did not contain any settings we could import." . Localize ( ) . FormatWith ( Path . GetFileName ( settingsFilePath ) ) , "Unable to Import" . Localize ( ) ) ;
}
2016-06-11 16:24:30 -07:00
}
}