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
2018-03-16 15:14:23 -07:00
public SelectPartsOfPrinterToImport ( string settingsFilePath )
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-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 ) ;
2018-03-16 15:14:23 -07:00
// if there are no settings to import
if ( settingsToImport . QualityLayers . Count = = 0 & & settingsToImport . MaterialLayers . Count = = 0 )
{
// Only main setting so don't ask what to merge just do it.
UiThread . RunOnIdle ( ( ) = >
{
DisplayFailedToImportMessage ( settingsFilePath ) ;
this . Parents < SystemWindow > ( ) . First ( ) . Close ( ) ;
} ) ;
}
2016-06-20 15:45:17 -07:00
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 ) ;
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 )
{
2018-03-16 15:14:23 -07:00
RadioButton qualityButton = new RadioButton ( string . IsNullOrEmpty ( qualitySetting . Name ) ? "no name" : 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 )
{
2018-03-16 15:14:23 -07:00
RadioButton materialButton = new RadioButton ( string . IsNullOrEmpty ( materialSetting . Name ) ? "no name" : 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
}
}
2018-03-16 15:14:23 -07:00
var importButtonTitle = "Import" . Localize ( ) ;
2018-04-14 20:51:01 -07:00
var mergeButton = theme . CreateDialogButton ( importButtonTitle ) ;
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 ;
2018-03-16 15:14:23 -07:00
bool destIsMaterial = true ;
2016-10-05 14:57:31 -07:00
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
{
2018-03-16 15:14:23 -07:00
destIsMaterial = false ;
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
2018-03-16 15:14:23 -07:00
PrinterSettingsLayer printerSettingsLayer = ImportSettings ( destIsMaterial , settingsFilePath ) ;
if ( printerSettingsLayer ! = null )
2016-06-20 15:45:17 -07:00
{
2018-03-16 15:14:23 -07:00
ActiveSliceSettings . Instance . Merge ( printerSettingsLayer , settingsToImport , sourceFilter , copyName ) ;
2016-06-15 11:49:57 -07:00
2018-03-16 15:14:23 -07:00
var layerName = ( printerSettingsLayer . ContainsKey ( SettingsKey . layer_name ) ) ? printerSettingsLayer [ SettingsKey . layer_name ] : "none" ;
Success ( settingsFilePath , layerName , destIsMaterial ? "Material" . Localize ( ) : "Quality" . Localize ( ) ) ;
}
else
{
UiThread . RunOnIdle ( ( ) = >
2016-10-05 14:57:31 -07:00
{
2018-03-16 15:14:23 -07:00
DisplayFailedToImportMessage ( settingsFilePath ) ;
this . Parents < SystemWindow > ( ) . First ( ) . Close ( ) ;
2016-10-05 14:57:31 -07:00
} ) ;
2018-03-16 15:14:23 -07:00
}
2016-06-11 16:24:30 -07:00
} ) ;
2018-03-16 15:14:23 -07:00
this . AddPageAction ( mergeButton ) ;
2016-06-11 16:24:30 -07:00
}
2018-03-16 15:14:23 -07:00
private void Success ( string settingsFilePath , string sourceName , string sectionName )
2016-06-13 15:28:56 -07:00
{
2018-03-16 15:14:23 -07:00
string importSettingSuccessMessage = $"You have successfully imported a new {sectionName} setting. You can find '{sourceName}' in your list of {sectionName} settings." . Localize ( ) ;
2016-06-13 15:28:56 -07:00
2018-03-16 15:14:23 -07:00
WizardWindow . ChangeToPage ( new ImportSucceeded ( importSettingSuccessMessage )
2017-11-07 14:15:34 -08:00
{
2018-03-16 15:14:23 -07:00
WizardWindow = this . WizardWindow ,
} ) ;
2017-11-07 14:15:34 -08:00
}
2016-06-21 15:39:18 -07:00
2018-03-16 15:14:23 -07:00
private static void DisplayFailedToImportMessage ( string settingsFilePath )
2016-06-11 16:24:30 -07:00
{
2018-03-16 15:14:23 -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-06-11 16:24:30 -07:00
}
2018-03-16 15:14:23 -07:00
public PrinterSettingsLayer ImportSettings ( bool destIsMaterial , string settingsFilePath )
2016-06-14 12:55:19 -07:00
{
2018-03-16 15:14:23 -07:00
PrinterSettingsLayer printerSettingsLayer = null ;
2016-06-14 12:55:19 -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 :
2018-03-16 15:14:23 -07:00
printerSettingsLayer = new PrinterSettingsLayer ( ) ;
printerSettingsLayer [ SettingsKey . layer_name ] = Path . GetFileNameWithoutExtension ( settingsFilePath ) ;
2016-06-21 17:06:01 -07:00
2018-03-16 15:14:23 -07:00
if ( destIsMaterial )
2016-06-21 16:36:11 -07:00
{
2018-03-16 15:14:23 -07:00
ActiveSliceSettings . Instance . MaterialLayers . Add ( printerSettingsLayer ) ;
2016-06-21 16:36:11 -07:00
}
else
{
2018-03-16 15:14:23 -07:00
ActiveSliceSettings . Instance . QualityLayers . Add ( printerSettingsLayer ) ;
2016-06-21 16:36:11 -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 ;
}
}
2016-06-11 16:24:30 -07:00
2018-03-16 15:14:23 -07:00
return printerSettingsLayer ;
2016-06-11 16:24:30 -07:00
}
2018-03-16 15:14:23 -07:00
}
2016-08-10 16:38:58 -07:00
2018-03-16 15:14:23 -07:00
public class ImportSucceeded : DialogPage
{
public ImportSucceeded ( string successMessage ) :
base ( "Done" . Localize ( ) )
2016-08-10 16:38:58 -07:00
{
2018-03-16 15:14:23 -07:00
this . WindowTitle = "Import Wizard" . Localize ( ) ;
this . HeaderText = "Import Successful" . Localize ( ) ;
contentRow . AddChild ( new WrappedTextWidget ( successMessage , textColor : ActiveTheme . Instance . PrimaryTextColor ) ) ;
2016-08-10 16:38:58 -07:00
}
2016-06-11 16:24:30 -07:00
}
2016-10-05 14:57:31 -07:00
}