2016-06-11 16:24:30 -07:00
/ *
2018-06-05 15:18:05 -07:00
Copyright ( c ) 2018 , Lars Brubaker , John Lewin
2016-06-11 16:24:30 -07:00
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 ;
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.SlicerConfiguration ;
namespace MatterHackers.MatterControl
{
2018-06-05 15:18:05 -07:00
public class ImportSettingsPage : DialogPage
2016-06-20 15:45:17 -07:00
{
2016-10-05 14:57:31 -07:00
private string settingsFilePath ;
private int selectedMaterial = - 1 ;
private int selectedQuality = - 1 ;
2016-06-20 15:45:17 -07:00
2018-06-05 15:18:05 -07:00
public ImportSettingsPage ( string settingsFilePath , PrinterConfig printer )
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
2018-06-05 14:17:51 -07:00
var settingsToImport = PrinterSettings . LoadFile ( settingsFilePath ) ;
2016-06-20 15:45:17 -07:00
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.
2019-06-28 22:22:58 -07:00
DisplayFailedToImportMessage ( settingsFilePath ) ;
this . Parents < SystemWindow > ( ) . First ( ) . Close ( ) ;
2018-03-16 15:14:23 -07:00
}
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 ;
2018-11-03 10:12:27 -07:00
contentRow . AddChild ( scrollWindow ) ;
2016-06-20 15:45:17 -07:00
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:" )
{
2018-11-03 09:13:07 -07:00
TextColor = theme . TextColor ,
2018-06-05 15:45:55 -07:00
Margin = new BorderDouble ( 0 , 3 ) ,
2016-06-20 15:45:17 -07:00
} ) ;
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-06-05 14:17:51 -07:00
var qualityButton = new RadioButton ( string . IsNullOrEmpty ( qualitySetting . Name ) ? "no name" : qualitySetting . Name )
2016-06-20 15:45:17 -07:00
{
2018-11-03 09:13:07 -07:00
TextColor = theme . TextColor ,
2016-06-20 15:45:17 -07:00
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:" )
{
2018-11-03 09:13:07 -07:00
TextColor = theme . TextColor ,
2016-06-20 15:45:17 -07:00
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-06-05 14:17:51 -07:00
var materialButton = new RadioButton ( string . IsNullOrEmpty ( materialSetting . Name ) ? "no name" : materialSetting . Name )
2016-06-20 15:45:17 -07:00
{
2018-11-03 09:13:07 -07:00
TextColor = theme . TextColor ,
2016-06-20 15:45:17 -07:00
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-06-05 15:18:05 -07:00
var mergeButton = theme . CreateDialogButton ( "Import" . Localize ( ) ) ;
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-06-05 14:36:41 -07:00
if ( File . Exists ( settingsFilePath ) )
2018-06-05 14:19:56 -07:00
{
2018-06-05 15:24:15 -07:00
if ( Path . GetExtension ( settingsFilePath ) . ToLower ( ) = = ProfileManager . ProfileExtension )
2018-06-05 14:19:56 -07:00
{
2018-06-05 15:24:15 -07:00
var printerSettingsLayer = new PrinterSettingsLayer ( ) ;
printer . Settings . Merge ( printerSettingsLayer , settingsToImport , sourceFilter , copyName ) ;
2019-04-02 10:00:08 -07:00
var layerName = printerSettingsLayer . ContainsKey ( SettingsKey . layer_name ) ? printerSettingsLayer [ SettingsKey . layer_name ] : "none" ;
2018-06-05 15:30:06 -07:00
string sectionName = destIsMaterial ? "Material" . Localize ( ) : "Quality" . Localize ( ) ;
2018-06-08 14:04:34 -07:00
string importSettingSuccessMessage = string . Format ( "You have successfully imported a new {0} setting. You can find '{1}' in your list of {0} settings." . Localize ( ) , sectionName , layerName ) ;
2018-06-05 15:30:06 -07:00
2018-06-19 15:02:25 -07:00
DialogWindow . ChangeToPage (
2019-04-02 09:56:43 -07:00
new ImportSucceededPage ( importSettingSuccessMessage )
2018-06-05 15:30:06 -07:00
{
2018-06-19 15:02:25 -07:00
DialogWindow = this . DialogWindow ,
2018-06-05 15:30:06 -07:00
} ) ;
2018-06-05 15:24:15 -07:00
if ( destIsMaterial )
{
printer . Settings . MaterialLayers . Add ( printerSettingsLayer ) ;
2020-09-01 09:22:18 -07:00
var newMaterial = printer . Settings . MaterialLayers [ printer . Settings . MaterialLayers . Count - 1 ] ;
printer . Settings . SetValue ( SettingsKey . active_material_key , newMaterial . LayerID ) ;
2018-06-05 15:24:15 -07:00
}
else
{
printer . Settings . QualityLayers . Add ( printerSettingsLayer ) ;
2020-09-01 09:22:18 -07:00
var newQuality = printer . Settings . QualityLayers [ printer . Settings . QualityLayers . Count - 1 ] ;
printer . Settings . SetValue ( SettingsKey . active_quality_key , newQuality . LayerID ) ;
2018-06-05 15:24:15 -07:00
}
}
else
{
// Inform of unexpected extension type
StyledMessageBox . ShowMessageBox (
"Oops! Unable to recognize settings file '{0}'." . Localize ( ) . FormatWith ( Path . GetFileName ( settingsFilePath ) ) ,
"Unable to Import" . Localize ( ) ) ;
2018-06-05 14:19:56 -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 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
}
2016-10-05 14:57:31 -07:00
}