2014-03-17 14:41:26 -07:00
/ *
2015-12-10 15:56:25 -08:00
Copyright ( c ) 2015 , Lars Brubaker
2014-03-17 14:41:26 -07:00
All rights reserved .
Redistribution and use in source and binary forms , with or without
2015-04-08 15:20:10 -07:00
modification , are permitted provided that the following conditions are met :
2014-03-17 14:41:26 -07:00
1. Redistributions of source code must retain the above copyright notice , this
2015-04-08 15:20:10 -07:00
list of conditions and the following disclaimer .
2014-03-17 14:41:26 -07:00
2. Redistributions in binary form must reproduce the above copyright notice ,
this list of conditions and the following disclaimer in the documentation
2015-04-08 15:20:10 -07:00
and / or other materials provided with the distribution .
2014-03-17 14:41:26 -07:00
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
2015-04-08 15:20:10 -07:00
of the authors and should not be interpreted as representing official policies ,
2014-03-17 14:41:26 -07:00
either expressed or implied , of the FreeBSD Project .
* /
2015-12-07 16:33:29 -08:00
using MatterHackers.Agg ;
2014-03-17 14:41:26 -07:00
using MatterHackers.Agg.UI ;
2016-07-06 16:40:23 -07:00
using MatterHackers.Localizations ;
using MatterHackers.MatterControl.PrinterCommunication ;
2015-07-13 12:49:01 -07:00
using MatterHackers.MatterControl.PrintLibrary ;
using MatterHackers.MatterControl.PrintLibrary.Provider ;
2014-03-17 14:41:26 -07:00
using MatterHackers.MatterControl.PrintQueue ;
using MatterHackers.MatterControl.SlicerConfiguration ;
2015-04-08 15:20:10 -07:00
using MatterHackers.VectorMath ;
using System ;
2016-07-12 17:23:45 -07:00
using System.Collections.Generic ;
2015-02-20 12:05:44 -08:00
using System.Diagnostics ;
2016-04-18 11:31:31 -07:00
using System.Linq ;
2015-12-08 12:02:44 -08:00
using System.Threading.Tasks ;
2014-03-17 14:41:26 -07:00
namespace MatterHackers.MatterControl
{
2015-12-22 16:44:54 -08:00
public abstract class ApplicationView : GuiWidget
2015-04-08 15:20:10 -07:00
{
2014-10-17 14:11:56 -07:00
public abstract void AddElements ( ) ;
2015-04-08 15:20:10 -07:00
}
2016-05-03 18:22:59 -07:00
public class TouchscreenView : ApplicationView
2015-04-08 15:20:10 -07:00
{
2016-05-03 18:22:59 -07:00
private FlowLayoutWidget TopContainer ;
2016-05-07 20:49:46 -07:00
private TouchscreenTabView touchscreenTabView ;
2015-04-08 15:20:10 -07:00
private QueueDataView queueDataView ;
private GuiWidget menuSeparator ;
private PrintProgressBar progressBar ;
2016-04-18 11:31:31 -07:00
private bool topIsHidden = false ;
2015-04-08 15:20:10 -07:00
2016-05-03 18:22:59 -07:00
public TouchscreenView ( )
2015-04-08 15:20:10 -07:00
{
AddElements ( ) ;
2016-04-18 11:31:31 -07:00
this . AnchorAll ( ) ;
}
2015-04-08 15:20:10 -07:00
2016-05-03 18:22:59 -07:00
public void ToggleTopContainer ( )
2014-10-17 14:11:56 -07:00
{
2014-10-27 19:54:01 -07:00
topIsHidden = ! topIsHidden ;
progressBar . WidgetIsExtended = ! progressBar . WidgetIsExtended ;
//To do - Animate this (KP)
2014-10-17 14:11:56 -07:00
this . menuSeparator . Visible = this . TopContainer . Visible ;
this . TopContainer . Visible = ! this . TopContainer . Visible ;
}
2015-04-08 15:20:10 -07:00
public override void AddElements ( )
{
2014-11-03 10:46:09 -08:00
topIsHidden = false ;
this . BackgroundColor = ActiveTheme . Instance . PrimaryBackgroundColor ;
2014-04-09 18:45:29 -07:00
2015-04-08 15:20:10 -07:00
FlowLayoutWidget container = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
container . AnchorAll ( ) ;
2014-04-09 18:45:29 -07:00
2016-04-18 11:31:31 -07:00
TopContainer = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
2014-10-17 14:11:56 -07:00
TopContainer . HAnchor = HAnchor . ParentLeftRight ;
2015-04-08 15:20:10 -07:00
#if ! __ANDROID__
2016-05-03 18:22:59 -07:00
// The application menu bar, which is suppressed on Android
ApplicationMenuRow menuRow = new ApplicationMenuRow ( ) ;
2014-10-17 14:11:56 -07:00
TopContainer . AddChild ( menuRow ) ;
2015-04-08 15:20:10 -07:00
#endif
2014-03-20 18:10:23 -07:00
2015-04-08 15:20:10 -07:00
menuSeparator = new GuiWidget ( ) ;
menuSeparator . Height = 12 ;
menuSeparator . HAnchor = HAnchor . ParentLeftRight ;
2014-10-17 14:11:56 -07:00
menuSeparator . MinimumSize = new Vector2 ( 0 , 12 ) ;
2015-04-08 15:20:10 -07:00
menuSeparator . Visible = false ;
2014-09-19 19:17:12 -07:00
2015-04-08 15:20:10 -07:00
queueDataView = new QueueDataView ( ) ;
2014-10-17 14:11:56 -07:00
TopContainer . AddChild ( new ActionBarPlus ( queueDataView ) ) ;
container . AddChild ( TopContainer ) ;
2014-10-27 19:54:01 -07:00
progressBar = new PrintProgressBar ( ) ;
2015-04-08 15:20:10 -07:00
container . AddChild ( progressBar ) ;
2014-10-17 14:11:56 -07:00
container . AddChild ( menuSeparator ) ;
2016-05-07 20:49:46 -07:00
touchscreenTabView = new TouchscreenTabView ( queueDataView ) ;
2014-10-27 19:54:01 -07:00
2016-05-07 20:49:46 -07:00
container . AddChild ( touchscreenTabView ) ;
2016-05-03 18:22:59 -07:00
this . AddChild ( container ) ;
2014-10-27 19:54:01 -07:00
}
}
2016-05-03 18:22:59 -07:00
public class DesktopView : ApplicationView
2015-04-08 15:20:10 -07:00
{
private WidescreenPanel widescreenPanel ;
2014-10-17 14:11:56 -07:00
2016-05-03 18:22:59 -07:00
public DesktopView ( )
2014-10-17 14:11:56 -07:00
{
2015-04-08 15:20:10 -07:00
AddElements ( ) ;
2016-05-03 18:22:59 -07:00
this . AnchorAll ( ) ;
2014-10-27 19:54:01 -07:00
}
2015-04-08 15:20:10 -07:00
2015-12-22 16:44:54 -08:00
public override void AddElements ( )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
this . BackgroundColor = ActiveTheme . Instance . PrimaryBackgroundColor ;
2015-11-16 18:01:25 -08:00
2016-04-18 11:31:31 -07:00
var container = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
2015-04-08 15:20:10 -07:00
container . AnchorAll ( ) ;
2014-04-27 20:24:58 -07:00
2016-05-03 18:22:59 -07:00
#if ! __ANDROID__
// The application menu bar, which is suppressed on Android
2016-04-18 11:31:31 -07:00
var menuRow = new ApplicationMenuRow ( ) ;
2015-04-08 15:20:10 -07:00
container . AddChild ( menuRow ) ;
2016-05-03 18:22:59 -07:00
#endif
2014-04-27 20:24:58 -07:00
2016-04-18 11:31:31 -07:00
var menuSeparator = new GuiWidget ( )
{
BackgroundColor = new RGBA_Bytes ( 200 , 200 , 200 ) ,
Height = 2 ,
HAnchor = HAnchor . ParentLeftRight ,
Margin = new BorderDouble ( 3 , 6 , 3 , 3 )
} ;
2015-04-08 15:20:10 -07:00
container . AddChild ( menuSeparator ) ;
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
widescreenPanel = new WidescreenPanel ( ) ;
container . AddChild ( widescreenPanel ) ;
2015-11-16 18:01:25 -08:00
using ( new PerformanceTimer ( "ReloadAll" , "AddChild" ) )
{
this . AddChild ( container ) ;
}
2015-02-20 12:05:44 -08:00
}
2015-04-08 15:20:10 -07:00
}
public class ApplicationController
{
private static ApplicationController globalInstance ;
2016-07-14 11:19:13 -07:00
public RootedObjectEventHandler AdvancedControlsPanelReloading = new RootedObjectEventHandler ( ) ;
2015-04-08 15:20:10 -07:00
public RootedObjectEventHandler CloudSyncStatusChanged = new RootedObjectEventHandler ( ) ;
2015-01-06 19:04:45 -08:00
public RootedObjectEventHandler DoneReloadingAll = new RootedObjectEventHandler ( ) ;
2015-07-15 12:28:52 -07:00
public RootedObjectEventHandler PluginsLoaded = new RootedObjectEventHandler ( ) ;
2015-01-06 19:04:45 -08:00
2016-07-11 14:59:40 -07:00
public static Action LoginAction ;
public static Action LogoutAction ;
2016-07-07 17:05:50 -07:00
public static Func < string > GetSessionInfo ;
2015-07-16 17:16:28 -07:00
2016-07-12 17:23:45 -07:00
public static Func < string , Task < Dictionary < string , string > > > GetProfileHistory ;
2016-07-13 13:39:11 -07:00
public static Func < PrinterInfo , string , Task > GetPrinterProfile ;
2016-07-14 12:43:53 -07:00
public static Func < Task > SyncPrinterProfiles ;
2016-07-12 17:23:45 -07:00
2015-04-08 15:20:10 -07:00
public SlicePresetsWindow EditMaterialPresetsWindow { get ; set ; }
public SlicePresetsWindow EditQualityPresetsWindow { get ; set ; }
public ApplicationView MainView ;
public event EventHandler ApplicationClosed ;
2014-09-19 19:17:12 -07:00
2015-04-08 15:20:10 -07:00
private event EventHandler unregisterEvents ;
2014-09-19 19:17:12 -07:00
2015-04-08 15:20:10 -07:00
public bool WidescreenMode { get ; set ; }
2014-09-19 19:17:12 -07:00
2015-04-08 15:20:10 -07:00
public ApplicationController ( )
{
//Name = "MainSlidePanel";
2016-07-14 11:19:13 -07:00
ActiveTheme . ThemeChanged . RegisterEvent ( ReloadAll , ref unregisterEvents ) ;
2015-04-08 15:20:10 -07:00
}
2015-01-06 19:04:45 -08:00
2015-07-16 17:16:28 -07:00
public void StartLogin ( )
{
2016-07-06 16:40:23 -07:00
if ( PrinterConnectionAndCommunication . Instance . PrinterIsPrinting
| | PrinterConnectionAndCommunication . Instance . PrinterIsPaused )
{
// can't login while printing
UiThread . RunOnIdle ( ( ) = >
StyledMessageBox . ShowMessageBox ( null , "Please wait until the print has finished and try again." . Localize ( ) , "Can't login while printing" . Localize ( ) )
) ;
}
else // do the regular login
{
2016-07-11 14:59:40 -07:00
LoginAction ? . Invoke ( ) ;
2016-07-06 16:40:23 -07:00
}
2015-07-16 17:16:28 -07:00
}
public void StartLogout ( )
{
2016-07-06 16:40:23 -07:00
if ( PrinterConnectionAndCommunication . Instance . PrinterIsPrinting
| | PrinterConnectionAndCommunication . Instance . PrinterIsPaused )
{
// can't log out while printing
UiThread . RunOnIdle ( ( ) = >
StyledMessageBox . ShowMessageBox ( null , "Please wait until the print has finished and try again." . Localize ( ) , "Can't log out while printing" . Localize ( ) )
) ;
}
else // do the regular log out
{
bool allowShowingLogoutWarning = true ;
if ( allowShowingLogoutWarning )
{
// Warn on logout that no access to user printers and cloud library put a 'Don't ask me again' check box
StyledMessageBox . ShowMessageBox ( ( clickedLogout ) = >
{
if ( clickedLogout )
{
2016-07-11 14:59:40 -07:00
LogoutAction ? . Invoke ( ) ;
2016-07-06 16:40:23 -07:00
}
} , "Are you sure you want to logout? You will not have access to your printer profiles or cloud library." . Localize ( ) , "Logout?" . Localize ( ) , StyledMessageBox . MessageType . YES_NO , "Logout" . Localize ( ) , "Cancel" . Localize ( ) ) ;
}
else // just run the logout event
{
2016-07-11 14:59:40 -07:00
LogoutAction ? . Invoke ( ) ;
2016-07-06 16:40:23 -07:00
}
}
2015-07-16 17:16:28 -07:00
}
public string GetSessionUsername ( )
{
2016-07-07 17:05:50 -07:00
if ( GetSessionInfo ! = null )
2015-07-16 17:16:28 -07:00
{
2016-07-07 17:05:50 -07:00
return GetSessionInfo ( ) ;
2015-07-16 17:16:28 -07:00
}
else
{
return null ;
}
}
2016-07-06 17:45:53 -07:00
private static string MakeValidFileName ( string name )
{
if ( string . IsNullOrEmpty ( name ) )
{
return name ;
}
string invalidChars = System . Text . RegularExpressions . Regex . Escape ( new string ( System . IO . Path . GetInvalidFileNameChars ( ) ) ) ;
string invalidRegStr = string . Format ( @"([{0}]*\.+$)|([{0}]+)" , invalidChars ) ;
return System . Text . RegularExpressions . Regex . Replace ( name , invalidRegStr , "_" ) ;
}
public string GetSessionUsernameForFileSystem ( )
{
return MakeValidFileName ( GetSessionUsername ( ) ) ;
}
2015-04-08 15:20:10 -07:00
public void ReloadAll ( object sender , EventArgs e )
{
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-04-08 15:20:10 -07:00
{
2015-11-16 18:01:25 -08:00
using ( new PerformanceTimer ( "ReloadAll" , "Total" ) )
2015-01-06 19:04:45 -08:00
{
2016-01-15 12:28:52 -08:00
// give the widget a chance to hear about the close before they are actually closed.
2016-01-24 20:06:04 -08:00
PopOutManager . SaveIfClosed = false ;
2016-04-18 11:31:31 -07:00
WidescreenPanel . PreChangePanels . CallEvents ( this , null ) ;
2016-01-12 11:20:27 -08:00
MainView . CloseAllChildren ( ) ;
2015-11-16 18:01:25 -08:00
using ( new PerformanceTimer ( "ReloadAll" , "AddElements" ) )
{
MainView . AddElements ( ) ;
}
2016-01-24 20:06:04 -08:00
PopOutManager . SaveIfClosed = true ;
2016-07-06 14:04:23 -07:00
DoneReloadingAll ? . CallEvents ( null , null ) ;
2016-01-24 20:06:04 -08:00
}
2015-04-08 15:20:10 -07:00
} ) ;
}
public void OnApplicationClosed ( )
{
2016-04-18 11:31:31 -07:00
ApplicationClosed ? . Invoke ( null , null ) ;
2015-04-08 15:20:10 -07:00
}
public static ApplicationController Instance
{
get
{
if ( globalInstance = = null )
{
2015-10-30 12:35:54 -07:00
//using (new PerformanceTimer("Startup", "AppController Instance"))
2015-04-08 15:20:10 -07:00
{
2015-10-29 11:31:09 -07:00
globalInstance = new ApplicationController ( ) ;
2016-04-29 07:45:15 -07:00
if ( UserSettings . Instance . DisplayMode = = ApplicationDisplayType . Touchscreen )
2015-10-29 11:31:09 -07:00
{
2016-05-03 18:22:59 -07:00
globalInstance . MainView = new TouchscreenView ( ) ;
2015-10-29 11:31:09 -07:00
}
else
{
2016-05-03 18:22:59 -07:00
globalInstance . MainView = new DesktopView ( ) ;
2015-10-29 11:31:09 -07:00
}
2016-06-08 11:46:46 -07:00
ActiveSliceSettings . ActivePrinterChanged . RegisterEvent ( ( s , e ) = > ApplicationController . Instance . ReloadAll ( null , null ) , ref globalInstance . unregisterEvents ) ;
2015-04-08 15:20:10 -07:00
}
}
return globalInstance ;
}
}
public void ReloadAdvancedControlsPanel ( )
{
2016-07-14 11:19:13 -07:00
AdvancedControlsPanelReloading . CallEvents ( this , null ) ;
2015-04-08 15:20:10 -07:00
}
2015-08-27 11:56:01 -07:00
public LibraryDataView CurrentLibraryDataView = null ;
2015-07-13 12:49:01 -07:00
public void SwitchToPurchasedLibrary ( )
{
// Switch to the purchased library
2015-08-27 11:56:01 -07:00
LibraryProviderSelector libraryProviderSelector = CurrentLibraryDataView . CurrentLibraryProvider . GetRootProvider ( ) as LibraryProviderSelector ;
2015-12-22 16:44:54 -08:00
if ( libraryProviderSelector ! = null )
2015-07-29 12:03:51 -07:00
{
2015-09-08 18:50:35 -07:00
LibraryProvider purchaseProvider = libraryProviderSelector . GetPurchasedLibrary ( ) ;
2015-12-22 16:44:54 -08:00
UiThread . RunOnIdle ( ( ) = >
2015-09-08 18:50:35 -07:00
{
CurrentLibraryDataView . CurrentLibraryProvider = purchaseProvider ;
} ) ;
2015-07-29 12:03:51 -07:00
}
2015-07-13 12:49:01 -07:00
}
2015-12-22 16:44:54 -08:00
public void SwitchToSharedLibrary ( )
{
// Switch to the shared library
LibraryProviderSelector libraryProviderSelector = CurrentLibraryDataView . CurrentLibraryProvider . GetRootProvider ( ) as LibraryProviderSelector ;
if ( libraryProviderSelector ! = null )
{
LibraryProvider sharedProvider = libraryProviderSelector . GetSharedLibrary ( ) ;
UiThread . RunOnIdle ( ( ) = >
{
CurrentLibraryDataView . CurrentLibraryProvider = sharedProvider ;
} ) ;
}
}
2015-09-17 19:19:15 -07:00
2015-07-12 19:02:42 -07:00
public void ChangeCloudSyncStatus ( bool userAuthenticated )
2015-04-08 15:20:10 -07:00
{
2015-07-12 19:02:42 -07:00
CloudSyncStatusChanged . CallEvents ( this , new CloudSyncEventArgs ( ) { IsAuthenticated = userAuthenticated } ) ;
2016-07-11 15:29:02 -07:00
string activeUserName = ApplicationController . Instance . GetSessionUsernameForFileSystem ( ) ;
UserSettings . Instance . set ( "ActiveUserName" , activeUserName ) ;
2016-07-14 11:19:13 -07:00
UserChanged ( ) ;
}
// Called after every startup and at the completion of every authentication change
public void UserChanged ( )
{
2016-07-11 15:29:02 -07:00
ProfileManager . Reload ( ) ;
2016-07-14 11:19:13 -07:00
var profileManager = ProfileManager . Instance ;
// Ensure SQLite printers are imported
profileManager . EnsurePrintersImported ( ) ;
2016-07-14 13:46:33 -07:00
var guestDB = ProfileManager . LoadGuestDB ( ) ;
2016-07-14 11:19:13 -07:00
// If profiles.json was created, run the import wizard to pull in any SQLite printers
2016-07-14 13:46:33 -07:00
if ( guestDB ? . Profiles ! = null & & guestDB . Profiles . Any ( ) & & ! profileManager . IsGuestProfile & & ! profileManager . PrintersImported )
2016-07-14 11:19:13 -07:00
{
var wizardPage = new CopyGuestProfilesToUser ( ( ) = >
{
// On success, set state indicating import has been run and update ProfileManager state
profileManager . PrintersImported = true ;
profileManager . Save ( ) ;
} ) ;
// Show the import printers wizard
2016-07-14 11:39:19 -07:00
WizardWindow . Show ( "/CopyGuestProfiles" , "Upload Printers" , wizardPage ) ;
2016-07-14 11:19:13 -07:00
}
2015-07-12 19:02:42 -07:00
}
public class CloudSyncEventArgs : EventArgs
{
public bool IsAuthenticated { get ; set ; }
2015-04-08 15:20:10 -07:00
}
2016-07-21 13:49:22 -07:00
public void OnLoadActions ( )
{
ApplicationController . Instance . UserChanged ( ) ;
if ( false & & UserSettings . Instance . get ( "SoftwareLicenseAccepted" ) ! = "true" )
{
//UiThread.RunOnIdle(() => WizardWindow.Show<LicenseAgreementPage>("SoftwareLicense", "Software License Agreement"));
}
bool showAuthWindow = WizardWindow . ShouldShowAuthPanel ? . Invoke ( ) ? ? false ;
if ( showAuthWindow )
{
//Launch window to prompt user to log in
UiThread . RunOnIdle ( ( ) = > WizardWindow . ShowPrinterSetup ( ) ) ;
}
else
{ //If user in logged in sync before checking to prompt to create printer
ApplicationController . SyncPrinterProfiles ( ) . ContinueWith ( ( task ) = >
{
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
if ( ! ProfileManager . Instance . ActiveProfiles . Any ( ) )
{
// Start the setup wizard if no profiles exist
UiThread . RunOnIdle ( ( ) = > WizardWindow . ShowPrinterSetup ( ) ) ;
}
} ) ;
}
if ( ActiveSliceSettings . Instance . PrinterSelected
& & ActiveSliceSettings . Instance . GetValue < bool > ( SettingsKey . auto_connect ) )
{
UiThread . RunOnIdle ( ( ) = >
{
//PrinterConnectionAndCommunication.Instance.HaltConnectionThread();
PrinterConnectionAndCommunication . Instance . ConnectToActivePrinter ( ) ;
} , 2 ) ;
}
}
2015-04-08 15:20:10 -07:00
}
}