2014-01-29 19:09:30 -08:00
/ *
2016-07-21 13:49:22 -07:00
Copyright ( c ) 2016 , Lars Brubaker , Kevin Pope
2014-01-29 19:09:30 -08: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-01-29 19:09:30 -08: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-01-29 19:09:30 -08: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-01-29 19:09:30 -08: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-01-29 19:09:30 -08:00
either expressed or implied , of the FreeBSD Project .
* /
2016-09-23 16:34:52 -07:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.Globalization ;
using System.IO ;
2017-03-15 16:17:06 -07:00
using System.Linq ;
2016-09-23 16:34:52 -07:00
using System.Net ;
using System.Threading ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg ;
2016-10-07 14:58:43 -07:00
using MatterHackers.Agg.Image ;
2017-08-20 02:34:39 -07:00
using MatterHackers.Agg.Platform ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI ;
2017-03-15 16:17:06 -07:00
using MatterHackers.DataConverters3D ;
Move to new library model and view
- Add new listview control for library content
- Migrate library providers to containers
- Cloud, Sqlite, Directories, Queue, History
- Migrate SideBar components to containers
- Primatives, Text, Braille, ImageConverter
- Create new library container types
- Zip files, Calibration parts, Printer SDCards
- Reduce leftnav to Library, Settings, Controls, Options
- Add DragDrop support for image content
2017-05-19 22:33:55 -07:00
using MatterHackers.ImageProcessing ;
2014-06-11 14:52:58 -07:00
using MatterHackers.Localizations ;
2014-01-29 19:09:30 -08:00
using MatterHackers.MatterControl.DataStorage ;
2017-03-15 16:17:06 -07:00
using MatterHackers.MatterControl.PartPreviewWindow ;
2014-03-16 10:23:56 -07:00
using MatterHackers.MatterControl.PluginSystem ;
2014-06-11 14:52:58 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2014-03-16 10:23:56 -07:00
using MatterHackers.MatterControl.PrintQueue ;
2014-05-06 18:25:16 -07:00
using MatterHackers.MatterControl.SettingsManagement ;
2014-06-11 14:52:58 -07:00
using MatterHackers.MatterControl.SlicerConfiguration ;
2014-10-12 08:18:24 -07:00
using MatterHackers.PolygonMesh.Processors ;
2015-04-08 17:47:17 -07:00
using MatterHackers.RenderOpenGl.OpenGl ;
2015-04-08 15:20:10 -07:00
using MatterHackers.VectorMath ;
2015-05-20 13:37:08 -07:00
using Mindscape.Raygun4Net ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl
{
2016-09-23 16:34:52 -07:00
public class MatterControlApplication : SystemWindow
2015-04-08 15:20:10 -07:00
{
2016-08-26 18:30:21 -07:00
#if DEBUG
2017-01-23 16:56:41 -08:00
2016-08-26 18:30:21 -07:00
//public static string MCWSBaseUri { get; } = "http://192.168.2.129:9206";
public static string MCWSBaseUri { get ; } = "https://mattercontrol-test.appspot.com" ;
2017-01-23 16:56:41 -08:00
2016-08-26 18:30:21 -07:00
#else
public static string MCWSBaseUri { get ; } = "https://mattercontrol.appspot.com" ;
#endif
2016-11-11 17:06:12 -08:00
public static bool CameraInUseByExternalProcess { get ; set ; } = false ;
2015-10-20 08:57:01 -07:00
public bool RestartOnClose = false ;
2015-04-08 15:20:10 -07:00
private static MatterControlApplication instance ;
2016-12-02 10:52:33 -08:00
private string [ ] commandLineArgs = null ;
2015-04-08 15:20:10 -07:00
private bool DoCGCollectEveryDraw = false ;
private int drawCount = 0 ;
private AverageMillisecondTimer millisecondTimer = new AverageMillisecondTimer ( ) ;
private bool ShowMemoryUsed = false ;
2016-06-01 18:17:11 -07:00
public void ConfigureWifi ( )
{
}
2015-04-08 15:20:10 -07:00
private Stopwatch totalDrawTime = new Stopwatch ( ) ;
2016-05-03 07:44:19 -07:00
public static bool IsLoading { get ; private set ; } = true ;
2017-03-03 16:31:25 -08:00
public static void RequestPowerShutDown ( )
{
// does nothing on windows
}
2015-04-08 15:20:10 -07:00
static MatterControlApplication ( )
2015-01-13 10:56:20 -08:00
{
2017-08-20 02:34:39 -07:00
if ( AggContext . OperatingSystem = = OSType . Mac & & AggContext . StaticData = = null )
2015-09-14 10:26:12 -07:00
{
// Set working directory - this duplicates functionality in Main but is necessary on OSX as Main fires much later (after the constructor in this case)
2015-12-23 16:23:19 -08:00
// resulting in invalid paths due to path tests running before the working directory has been overridden. Setting the value before initializing StaticData
2015-09-14 10:26:12 -07:00
// works around this architectural difference.
Directory . SetCurrentDirectory ( Path . GetDirectoryName ( System . Reflection . Assembly . GetEntryAssembly ( ) . Location ) ) ;
}
2015-04-08 15:20:10 -07:00
// Because fields on this class call localization methods and because those methods depend on the StaticData provider and because the field
// initializers run before the class constructor, we need to init the platform specific provider in the static constructor (or write a custom initializer method)
//
// Initialize a standard file system backed StaticData provider
2017-08-20 02:34:39 -07:00
if ( AggContext . StaticData = = null ) // it may already be initialized by tests
2015-08-23 17:21:47 -07:00
{
2017-08-20 02:34:39 -07:00
AggContext . StaticData = new MatterHackers . Agg . FileSystemStaticData ( ) ;
2015-08-23 17:21:47 -07:00
}
2017-10-01 11:17:28 -07:00
2017-10-01 19:00:12 -07:00
#if DEBUG
2016-12-01 22:03:15 -08:00
WinformsSystemWindow . InspectorCreator = ( systemWindow ) = >
2017-10-01 11:17:28 -07:00
{
if ( systemWindow = = Instance )
{
// If systemWindow is MatterControlApplication, include Scene
2017-10-21 08:56:08 -07:00
var partContext = ApplicationController . Instance . DragDropData ;
return new InspectForm ( systemWindow , partContext . SceneContext . Scene , partContext . View3DWidget ) ;
2017-10-01 11:17:28 -07:00
}
else
{
// Otherwise, exclude Scene
return new InspectForm ( systemWindow ) ;
}
} ;
2017-10-01 19:00:12 -07:00
#endif
2015-01-13 10:56:20 -08:00
}
2016-12-02 10:52:33 -08:00
private MatterControlApplication ( double width , double height )
: base ( width , height )
2016-12-01 18:03:40 -08:00
{
2016-12-02 10:52:33 -08:00
ApplicationSettings . Instance . set ( "HardwareHasCamera" , "false" ) ;
Name = "MatterControl" ;
// set this at startup so that we can tell next time if it got set to true in close
UserSettings . Instance . Fields . StartCount = UserSettings . Instance . Fields . StartCount + 1 ;
this . commandLineArgs = Environment . GetCommandLineArgs ( ) ;
Thread . CurrentThread . CurrentCulture = CultureInfo . InvariantCulture ;
2015-04-08 15:20:10 -07:00
for ( int currentCommandIndex = 0 ; currentCommandIndex < commandLineArgs . Length ; currentCommandIndex + + )
{
string command = commandLineArgs [ currentCommandIndex ] ;
2015-01-23 10:22:02 -08:00
string commandUpper = command . ToUpper ( ) ;
2015-04-08 15:20:10 -07:00
switch ( commandUpper )
{
2015-04-08 17:47:17 -07:00
case "FORCE_SOFTWARE_RENDERING" :
2016-12-02 16:49:18 -08:00
GL . HardwareAvailable = false ;
2015-04-08 17:47:17 -07:00
break ;
2015-04-08 15:20:10 -07:00
case "CLEAR_CACHE" :
2016-04-25 13:55:40 -07:00
AboutWidget . DeleteCacheData ( 0 ) ;
2015-04-08 15:20:10 -07:00
break ;
case "SHOW_MEMORY" :
ShowMemoryUsed = true ;
break ;
case "DO_GC_COLLECT_EVERY_DRAW" :
ShowMemoryUsed = true ;
DoCGCollectEveryDraw = true ;
break ;
2016-04-18 11:31:31 -07:00
//case "CREATE_AND_SELECT_PRINTER":
// if (currentCommandIndex + 1 <= commandLineArgs.Length)
// {
// currentCommandIndex++;
// string argument = commandLineArgs[currentCommandIndex];
// string[] printerData = argument.Split(',');
// if (printerData.Length >= 2)
// {
// Printer ActivePrinter = new Printer();
// ActivePrinter.Name = "Auto: {0} {1}".FormatWith(printerData[0], printerData[1]);
// ActivePrinter.Make = printerData[0];
// ActivePrinter.Model = printerData[1];
// if (printerData.Length == 3)
// {
// ActivePrinter.ComPort = printerData[2];
// }
// PrinterSetupStatus test = new PrinterSetupStatus(ActivePrinter);
// test.LoadSettingsFromConfigFile(ActivePrinter.Make, ActivePrinter.Model);
// ActiveSliceSettings.Instance = ActivePrinter;
// }
// }
// break;
2015-04-08 15:20:10 -07:00
}
if ( MeshFileIo . ValidFileExtensions ( ) . Contains ( Path . GetExtension ( command ) . ToUpper ( ) ) )
{
// If we are the only instance running then do nothing.
// Else send these to the running instance so it can load them.
}
}
//WriteTestGCodeFile();
2014-03-02 15:28:47 -08:00
#if ! DEBUG
2015-04-08 15:20:10 -07:00
if ( File . Exists ( "RunUnitTests.txt" ) )
2014-03-02 15:28:47 -08:00
#endif
2015-04-08 15:20:10 -07:00
{
2014-07-28 13:48:28 -07:00
#if IS_WINDOWS_FORMS
2015-04-08 15:20:10 -07:00
if ( ! Clipboard . IsInitialized )
{
Clipboard . SetSystemClipboard ( new WindowsFormsClipboard ( ) ) ;
}
2014-07-28 13:48:28 -07:00
#endif
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
// you can turn this on to debug some bounds issues
//GuiWidget.DebugBoundsUnderMouse = true;
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
GuiWidget . DefaultEnforceIntegerBounds = true ;
2014-01-29 19:09:30 -08:00
2017-01-23 16:56:41 -08:00
if ( UserSettings . Instance . IsTouchScreen )
2015-04-08 15:20:10 -07:00
{
2016-05-06 17:56:27 -07:00
GuiWidget . DeviceScale = 1.3 ;
2017-01-19 10:35:42 -08:00
SystemWindow . ShareSingleOsWindow = true ;
2015-04-08 15:20:10 -07:00
}
2017-06-06 15:04:07 -07:00
string textSizeMode = UserSettings . Instance . get ( UserSettingsKey . ApplicationTextSize ) ;
if ( ! string . IsNullOrEmpty ( textSizeMode ) )
{
double textSize = 1.0 ;
if ( double . TryParse ( textSizeMode , out textSize ) )
{
GuiWidget . DeviceScale = textSize ;
}
}
2016-05-06 17:56:27 -07:00
//GuiWidget.DeviceScale = 2;
2014-08-30 16:03:28 -07:00
2015-10-29 11:31:09 -07:00
using ( new PerformanceTimer ( "Startup" , "MainView" ) )
{
this . AddChild ( ApplicationController . Instance . MainView ) ;
}
2015-04-08 15:20:10 -07:00
this . MinimumSize = minSize ;
this . Padding = new BorderDouble ( 0 ) ; //To be re-enabled once native borders are turned off
2014-01-29 19:09:30 -08:00
#if false // this is to test freeing gcodefile memory
2015-04-08 15:20:10 -07:00
Button test = new Button ( "test" ) ;
test . Click + = ( sender , e ) = >
{
//MatterHackers.GCodeVisualizer.GCodeFile gcode = new GCodeVisualizer.GCodeFile();
//gcode.Load(@"C:\Users\lbrubaker\Downloads\drive assy.gcode");
SystemWindow window = new SystemWindow ( 100 , 100 ) ;
window . ShowAsSystemWindow ( ) ;
} ;
allControls . AddChild ( test ) ;
2014-01-29 19:09:30 -08:00
#endif
2015-04-08 15:20:10 -07:00
this . AnchorAll ( ) ;
2014-01-29 19:09:30 -08:00
2016-12-02 16:49:18 -08:00
if ( GL . HardwareAvailable )
2015-04-08 17:47:17 -07:00
{
UseOpenGL = true ;
}
2017-10-12 12:37:25 -07:00
string version = "2.0" ;
2014-05-22 16:55:28 -07:00
2016-09-09 09:55:53 -07:00
Title = "MatterHackers: MatterControl {0}" . FormatWith ( version ) ;
2015-04-08 15:20:10 -07:00
if ( OemSettings . Instance . WindowTitleExtra ! = null & & OemSettings . Instance . WindowTitleExtra . Trim ( ) . Length > 0 )
{
Title = Title + " - {1}" . FormatWith ( version , OemSettings . Instance . WindowTitleExtra ) ;
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
UiThread . RunOnIdle ( CheckOnPrinter ) ;
2014-01-29 19:09:30 -08:00
2016-08-30 10:30:55 -07:00
string desktopPosition = ApplicationSettings . Instance . get ( ApplicationSettingsKey . DesktopPosition ) ;
2017-01-25 11:06:22 -08:00
if ( ! string . IsNullOrEmpty ( desktopPosition ) )
2015-04-08 15:20:10 -07:00
{
string [ ] sizes = desktopPosition . Split ( ',' ) ;
2014-04-12 11:54:05 -07:00
2015-04-08 15:20:10 -07:00
//If the desktop position is less than -10,-10, override
int xpos = Math . Max ( int . Parse ( sizes [ 0 ] ) , - 10 ) ;
int ypos = Math . Max ( int . Parse ( sizes [ 1 ] ) , - 10 ) ;
2017-10-10 14:34:58 -07:00
this . InitialDesktopPosition = new Point2D ( xpos , ypos ) ;
2015-04-08 15:20:10 -07:00
}
2017-01-25 11:06:22 -08:00
else
{
2017-10-10 14:34:58 -07:00
this . InitialDesktopPosition = new Point2D ( - 1 , - 1 ) ;
2017-01-25 11:06:22 -08:00
}
this . Maximized = ApplicationSettings . Instance . get ( ApplicationSettingsKey . MainWindowMaximized ) = = "true" ;
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2016-10-07 14:58:43 -07:00
public void TakePhoto ( string imageFileName )
{
ImageBuffer noCameraImage = new ImageBuffer ( 640 , 480 ) ;
Graphics2D graphics = noCameraImage . NewGraphics2D ( ) ;
graphics . Clear ( RGBA_Bytes . White ) ;
graphics . DrawString ( "No Camera Detected" , 320 , 240 , pointSize : 24 , justification : Agg . Font . Justification . Center ) ;
graphics . DrawString ( DateTime . Now . ToString ( ) , 320 , 200 , pointSize : 12 , justification : Agg . Font . Justification . Center ) ;
2017-08-20 02:34:39 -07:00
AggContext . ImageIO . SaveImageData ( imageFileName , noCameraImage ) ;
2016-10-07 14:58:43 -07:00
PictureTaken ? . Invoke ( null , null ) ;
}
2017-01-23 16:56:41 -08:00
private bool dropWasOnChild = true ;
2016-12-29 06:55:12 -08:00
private EventHandler unregisterEvent ;
2015-04-08 15:20:10 -07:00
public static MatterControlApplication Instance
{
get
{
if ( instance = = null )
{
2016-12-02 10:52:33 -08:00
instance = CreateInstance ( ) ;
2015-12-22 11:34:04 -08:00
instance . ShowAsSystemWindow ( ) ;
2015-04-08 15:20:10 -07:00
}
return instance ;
}
}
2014-02-27 14:56:20 -08:00
2016-10-07 14:58:43 -07:00
public event EventHandler PictureTaken ;
2017-01-23 16:56:41 -08:00
private static Vector2 minSize { get ; set ; } = new Vector2 ( 600 , 600 ) ;
2016-12-02 10:52:33 -08:00
public static MatterControlApplication CreateInstance ( int overrideWidth = - 1 , int overrideHeight = - 1 )
2015-08-23 17:21:47 -07:00
{
2016-09-01 16:55:23 -07:00
int width = 0 ;
int height = 0 ;
2017-01-23 16:56:41 -08:00
if ( UserSettings . Instance . IsTouchScreen )
{
minSize = new Vector2 ( 800 , 480 ) ;
}
// check if the app has a size already set
2016-08-30 10:30:55 -07:00
string windowSize = ApplicationSettings . Instance . get ( ApplicationSettingsKey . WindowSize ) ;
2015-08-23 17:21:47 -07:00
if ( windowSize ! = null & & windowSize ! = "" )
{
2016-09-01 16:55:23 -07:00
// try and open our window matching the last size that we had for it.
2015-08-23 17:21:47 -07:00
string [ ] sizes = windowSize . Split ( ',' ) ;
2017-01-25 11:06:22 -08:00
2015-08-23 17:21:47 -07:00
width = Math . Max ( int . Parse ( sizes [ 0 ] ) , ( int ) minSize . x + 1 ) ;
height = Math . Max ( int . Parse ( sizes [ 1 ] ) , ( int ) minSize . y + 1 ) ;
}
2016-09-01 16:55:23 -07:00
else // try to set it to a big size or the min size
{
2017-08-20 02:34:39 -07:00
Point2D desktopSize = AggContext . DesktopSize ;
2016-09-01 16:55:23 -07:00
2016-10-07 14:58:43 -07:00
if ( overrideWidth ! = - 1 )
2016-09-01 16:55:23 -07:00
{
width = overrideWidth ;
}
else // try to set it to a good size
{
2016-10-07 14:58:43 -07:00
if ( width < desktopSize . x )
2016-09-01 16:55:23 -07:00
{
width = 1280 ;
}
}
2015-08-23 17:21:47 -07:00
2016-10-07 14:58:43 -07:00
if ( overrideHeight ! = - 1 )
2016-09-01 16:55:23 -07:00
{
2017-05-21 10:11:48 -07:00
// Height should be constrained to actual
height = Math . Min ( overrideHeight , desktopSize . y ) ;
2016-09-01 16:55:23 -07:00
}
else
{
if ( height < desktopSize . y )
{
height = 720 ;
}
}
}
using ( new PerformanceTimer ( "Startup" , "Total" ) )
2016-10-07 14:58:43 -07:00
{
2016-12-02 10:52:33 -08:00
instance = new MatterControlApplication ( width , height ) ;
2016-09-01 16:55:23 -07:00
}
2015-08-23 17:21:47 -07:00
return instance ;
}
2015-04-08 15:20:10 -07:00
public void LaunchBrowser ( string targetUri )
{
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-04-08 15:20:10 -07:00
{
System . Diagnostics . Process . Start ( targetUri ) ;
} ) ;
}
2017-02-03 13:06:08 -08:00
public override void OnClosed ( ClosedEventArgs e )
2015-04-08 15:20:10 -07:00
{
UserSettings . Instance . Fields . StartCountDurringExit = UserSettings . Instance . Fields . StartCount ;
2017-09-17 21:08:16 -07:00
if ( ApplicationController . Instance . ActivePrinter . Connection . CommunicationState ! = CommunicationStates . PrintingFromSd )
2016-07-25 15:06:05 -07:00
{
2017-09-17 21:08:16 -07:00
ApplicationController . Instance . ActivePrinter . Connection . Disable ( ) ;
2016-07-25 15:06:05 -07:00
}
2015-04-08 15:20:10 -07:00
//Close connection to the local datastore
2017-09-17 21:08:16 -07:00
ApplicationController . Instance . ActivePrinter . Connection . HaltConnectionThread ( ) ;
2015-04-08 15:20:10 -07:00
ApplicationController . Instance . OnApplicationClosed ( ) ;
2016-09-29 10:48:19 -07:00
Datastore . Instance . Exit ( ) ;
2015-04-08 15:20:10 -07:00
if ( RestartOnClose )
{
string appPathAndFile = System . Reflection . Assembly . GetExecutingAssembly ( ) . Location ;
string pathToAppFolder = Path . GetDirectoryName ( appPathAndFile ) ;
ProcessStartInfo runAppLauncherStartInfo = new ProcessStartInfo ( ) ;
runAppLauncherStartInfo . Arguments = "\"{0}\" \"{1}\"" . FormatWith ( appPathAndFile , 1000 ) ;
runAppLauncherStartInfo . FileName = Path . Combine ( pathToAppFolder , "Launcher.exe" ) ;
runAppLauncherStartInfo . WindowStyle = ProcessWindowStyle . Hidden ;
runAppLauncherStartInfo . CreateNoWindow = true ;
Process . Start ( runAppLauncherStartInfo ) ;
}
base . OnClosed ( e ) ;
}
2017-10-12 17:38:36 -07:00
public override void OnClosing ( ClosingEventArgs eventArgs )
2015-04-08 15:20:10 -07:00
{
// save the last size of the window so we can restore it next time.
2017-01-25 11:06:22 -08:00
ApplicationSettings . Instance . set ( ApplicationSettingsKey . MainWindowMaximized , this . Maximized . ToString ( ) . ToLower ( ) ) ;
if ( ! this . Maximized )
{
ApplicationSettings . Instance . set ( ApplicationSettingsKey . WindowSize , string . Format ( "{0},{1}" , Width , Height ) ) ;
ApplicationSettings . Instance . set ( ApplicationSettingsKey . DesktopPosition , string . Format ( "{0},{1}" , DesktopPosition . x , DesktopPosition . y ) ) ;
}
2015-04-08 15:20:10 -07:00
//Save a snapshot of the prints in queue
QueueData . Instance . SaveDefaultQueue ( ) ;
2017-03-08 10:25:36 -08:00
// If we are waiting for a response and get another request, just cancel the close until we get a response.
2017-10-07 11:16:36 -07:00
if ( exitDialogOpen )
2015-04-08 15:20:10 -07:00
{
2017-10-12 17:38:36 -07:00
eventArgs . Cancel = true ;
2017-03-08 10:25:36 -08:00
}
2017-10-12 17:38:36 -07:00
string caption = null ;
string message = null ;
2017-10-07 11:16:36 -07:00
if ( ! ApplicationExiting
& & ! exitDialogOpen
2017-09-17 21:08:16 -07:00
& & ApplicationController . Instance . ActivePrinter . Connection . PrinterIsPrinting )
2017-03-08 10:25:36 -08:00
{
2017-09-17 21:08:16 -07:00
if ( ApplicationController . Instance . ActivePrinter . Connection . CommunicationState ! = CommunicationStates . PrintingFromSd )
2016-10-20 09:55:12 -07:00
{
2017-10-12 17:38:36 -07:00
caption = "Abort Print" . Localize ( ) ;
message = "Are you sure you want to abort the current print and close MatterControl?" . Localize ( ) ;
2016-01-12 11:20:27 -08:00
}
2016-10-20 09:55:12 -07:00
else
{
2017-10-12 17:38:36 -07:00
caption = "Exit while printing" . Localize ( ) ;
message = "Are you sure you want exit while a print is running from SD Card?\n\nNote: If you exit, it is recommended you wait until the print is completed before running MatterControl again." . Localize ( ) ;
2016-10-20 09:55:12 -07:00
}
2015-04-08 15:20:10 -07:00
}
else if ( PartsSheet . IsSaving ( ) )
{
2017-10-12 17:38:36 -07:00
caption = "Confirm Exit" . Localize ( ) ;
message = "You are currently saving a parts sheet, are you sure you want to exit?" . Localize ( ) ;
2015-04-08 15:20:10 -07:00
}
2017-10-12 17:38:36 -07:00
if ( caption ! = null )
2015-04-08 15:20:10 -07:00
{
2017-10-12 17:38:36 -07:00
// Record that we are waiting for a response to the request to close
exitDialogOpen = true ;
2015-04-08 15:20:10 -07:00
2017-10-12 17:38:36 -07:00
StyledMessageBox . ShowMessageBox (
( exitConfirmed ) = >
{
// Record that the exitDialog has closed
exitDialogOpen = false ;
2017-10-07 11:02:22 -07:00
2017-10-12 17:38:36 -07:00
// Continue with shutdown if exit confirmed by user
if ( exitConfirmed )
{
this . ApplicationExiting = true ;
2017-10-07 11:16:36 -07:00
2017-10-12 17:38:36 -07:00
ApplicationController . Instance . Shutdown ( ) ;
2017-10-07 10:24:50 -07:00
2017-10-12 17:38:36 -07:00
// Always call PrinterConnection.Disable on exit unless PrintingFromSd
PrinterConnection printerConnection = ApplicationController . Instance . ActivePrinter . Connection ;
switch ( printerConnection . CommunicationState )
{
case CommunicationStates . PrintingFromSd :
case CommunicationStates . Paused when printerConnection . PrePauseCommunicationState = = CommunicationStates . PrintingFromSd :
break ;
2017-10-07 10:24:50 -07:00
2017-10-12 17:38:36 -07:00
default :
printerConnection . Disable ( ) ;
break ;
}
2017-10-09 07:58:32 -07:00
2017-10-12 17:38:36 -07:00
this . RestartOnClose = false ;
}
2017-10-07 10:24:50 -07:00
2017-10-12 17:38:36 -07:00
// If the user allowed the exit, don't cancel the OnClosing event
eventArgs . Cancel = ! exitConfirmed ;
} ,
message ,
caption ,
StyledMessageBox . MessageType . YES_NO ) ;
2017-02-17 14:36:09 -08:00
2017-10-12 17:38:36 -07:00
exitDialogOpen = false ;
2017-02-17 14:36:09 -08:00
}
2017-10-20 06:09:14 -07:00
else
{
this . ApplicationExiting = true ;
}
2017-02-17 14:36:09 -08:00
}
2017-10-12 17:38:36 -07:00
public bool ApplicationExiting { get ; private set ; } = false ;
private bool exitDialogOpen = false ;
2016-10-07 14:58:43 -07:00
public override void OnDraw ( Graphics2D graphics2D )
2015-04-08 15:20:10 -07:00
{
totalDrawTime . Restart ( ) ;
GuiWidget . DrawCount = 0 ;
2015-10-29 11:31:09 -07:00
using ( new PerformanceTimer ( "Draw Timer" , "MC Draw" ) )
2015-10-22 13:41:12 -07:00
{
base . OnDraw ( graphics2D ) ;
}
2015-04-08 15:20:10 -07:00
totalDrawTime . Stop ( ) ;
millisecondTimer . Update ( ( int ) totalDrawTime . ElapsedMilliseconds ) ;
if ( ShowMemoryUsed )
{
long memory = GC . GetTotalMemory ( false ) ;
2015-09-14 09:57:48 -07:00
this . Title = "Allocated = {0:n0} : {1:000}ms, d{2} Size = {3}x{4}, onIdle = {5:00}:{6:00}, widgetsDrawn = {7}" . FormatWith ( memory , millisecondTimer . GetAverage ( ) , drawCount + + , this . Width , this . Height , UiThread . CountExpired , UiThread . Count , GuiWidget . DrawCount ) ;
2015-04-08 15:20:10 -07:00
if ( DoCGCollectEveryDraw )
{
GC . Collect ( ) ;
}
}
2016-07-24 17:26:24 -07:00
//msGraph.AddData("ms", totalDrawTime.ElapsedMilliseconds);
//msGraph.Draw(MatterHackers.Agg.Transform.Affine.NewIdentity(), graphics2D);
}
public override void OnLoad ( EventArgs args )
{
2016-12-01 22:03:15 -08:00
// Moved from OnParentChanged
if ( File . Exists ( "RunUnitTests.txt" ) )
{
//DiagnosticWidget diagnosticView = new DiagnosticWidget(this);
}
// now that we are all set up lets load our plugins and allow them their chance to set things up
FindAndInstantiatePlugins ( ) ;
if ( ApplicationController . Instance . PluginsLoaded ! = null )
{
ApplicationController . Instance . PluginsLoaded . CallEvents ( null , null ) ;
}
2016-07-24 17:26:24 -07:00
foreach ( string arg in commandLineArgs )
2015-04-08 15:20:10 -07:00
{
2016-07-24 17:26:24 -07:00
string argExtension = Path . GetExtension ( arg ) . ToUpper ( ) ;
if ( argExtension . Length > 1
& & MeshFileIo . ValidFileExtensions ( ) . Contains ( argExtension ) )
2015-04-08 15:20:10 -07:00
{
2016-07-24 17:26:24 -07:00
QueueData . Instance . AddItem ( new PrintItemWrapper ( new PrintItem ( Path . GetFileName ( arg ) , Path . GetFullPath ( arg ) ) ) ) ;
2015-04-08 15:20:10 -07:00
}
2016-07-24 17:26:24 -07:00
}
2015-04-08 15:20:10 -07:00
2016-07-24 17:26:24 -07:00
ApplicationController . Instance . OnLoadActions ( ) ;
2016-07-13 17:20:31 -07:00
2016-09-12 21:55:40 -07:00
//HtmlWindowTest();
IsLoading = false ;
2017-03-15 16:17:06 -07:00
base . OnLoad ( args ) ;
2016-09-12 21:55:40 -07:00
}
2015-12-22 11:34:04 -08:00
public override void OnMouseMove ( MouseEventArgs mouseEvent )
2015-04-08 15:20:10 -07:00
{
2017-04-05 16:25:43 -07:00
// run this first to make sure a child has the chance to take the drag drop event
base . OnMouseMove ( mouseEvent ) ;
if ( ! mouseEvent . AcceptDrop & & mouseEvent . DragFiles ! = null )
{
// no child has accepted the drop
foreach ( string file in mouseEvent . DragFiles )
{
string extension = Path . GetExtension ( file ) . ToUpper ( ) ;
if ( ( extension ! = "" & & MeshFileIo . ValidFileExtensions ( ) . Contains ( extension ) )
| | extension = = ".GCODE"
| | extension = = ".ZIP" )
{
//mouseEvent.AcceptDrop = true;
}
}
dropWasOnChild = false ;
}
else
{
dropWasOnChild = true ;
}
2015-04-08 15:20:10 -07:00
if ( GuiWidget . DebugBoundsUnderMouse )
{
Invalidate ( ) ;
}
}
public void OpenCameraPreview ( )
{
//Camera launcher placeholder (KP)
2016-08-30 10:30:55 -07:00
if ( ApplicationSettings . Instance . get ( ApplicationSettingsKey . HardwareHasCamera ) = = "true" )
2015-04-08 15:20:10 -07:00
{
//Do something
}
else
{
//Do something else (like show warning message)
}
}
public void PlaySound ( string fileName )
{
2017-08-20 02:34:39 -07:00
if ( AggContext . OperatingSystem = = OSType . Windows )
2015-04-08 15:20:10 -07:00
{
2017-08-20 02:34:39 -07:00
using ( var mediaStream = AggContext . StaticData . OpenSteam ( Path . Combine ( "Sounds" , fileName ) ) )
2015-04-08 15:20:10 -07:00
{
( new System . Media . SoundPlayer ( mediaStream ) ) . Play ( ) ;
}
}
}
2015-06-11 12:06:40 -07:00
private void CheckOnPrinter ( )
2015-04-08 15:20:10 -07:00
{
2015-03-25 18:01:57 -07:00
try
{
2017-09-17 21:08:16 -07:00
ApplicationController . Instance . ActivePrinter . Connection . OnIdle ( ) ;
2015-03-25 18:01:57 -07:00
}
2015-04-08 15:20:10 -07:00
catch ( Exception e )
2015-03-25 18:01:57 -07:00
{
Debug . Print ( e . Message ) ;
2015-09-17 13:45:26 -07:00
GuiWidget . BreakInDebugger ( ) ;
2015-03-25 18:01:57 -07:00
#if DEBUG
throw e ;
#endif
}
2015-04-08 15:20:10 -07:00
UiThread . RunOnIdle ( CheckOnPrinter ) ;
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
private void FindAndInstantiatePlugins ( )
{
2014-01-29 19:09:30 -08:00
#if false
2015-04-08 15:20:10 -07:00
string pluginDirectory = Path . Combine ( ".." , ".." , ".." , "MatterControlPlugins" , "bin" ) ;
2014-01-29 19:09:30 -08:00
#if DEBUG
2015-04-08 15:20:10 -07:00
pluginDirectory = Path . Combine ( pluginDirectory , "Debug" ) ;
2014-01-29 19:09:30 -08:00
#else
2015-04-08 15:20:10 -07:00
pluginDirectory = Path . Combine ( pluginDirectory , "Release" ) ;
2014-01-29 19:09:30 -08:00
#endif
2015-04-08 15:20:10 -07:00
if ( ! Directory . Exists ( pluginDirectory ) )
{
2016-02-24 10:52:28 -08:00
string dataPath = ApplicationDataStorage . Instance . ApplicationUserDataPath ;
2015-04-08 15:20:10 -07:00
pluginDirectory = Path . Combine ( dataPath , "Plugins" ) ;
}
// TODO: this should look in a plugin folder rather than just the application directory (we probably want it in the user folder).
PluginFinder < MatterControlPlugin > pluginFinder = new PluginFinder < MatterControlPlugin > ( pluginDirectory ) ;
2014-01-29 19:09:30 -08:00
#endif
2014-11-25 07:47:03 -08:00
2015-04-08 15:20:10 -07:00
string oemName = ApplicationSettings . Instance . GetOEMName ( ) ;
2017-07-20 00:50:50 -07:00
foreach ( MatterControlPlugin plugin in PluginFinder . CreateInstancesOf < MatterControlPlugin > ( ) )
2015-04-08 15:20:10 -07:00
{
string pluginInfo = plugin . GetPluginInfoJSon ( ) ;
Dictionary < string , string > nameValuePairs = Newtonsoft . Json . JsonConvert . DeserializeObject < Dictionary < string , string > > ( pluginInfo ) ;
2014-07-15 14:02:03 -07:00
2015-04-08 15:20:10 -07:00
if ( nameValuePairs ! = null & & nameValuePairs . ContainsKey ( "OEM" ) )
{
if ( nameValuePairs [ "OEM" ] = = oemName )
{
plugin . Initialize ( this ) ;
}
}
else
{
plugin . Initialize ( this ) ;
}
}
}
2014-01-29 19:09:30 -08:00
2015-05-05 07:25:47 -07:00
private static void AssertDebugNotDefined ( )
{
#if DEBUG
throw new Exception ( "DEBUG is defined and should not be!" ) ;
#endif
}
public static void CheckKnownAssemblyConditionalCompSymbols ( )
{
MatterControlApplication . AssertDebugNotDefined ( ) ;
MatterHackers . GCodeVisualizer . GCodeFile . AssertDebugNotDefined ( ) ;
MatterHackers . Agg . Graphics2D . AssertDebugNotDefined ( ) ;
MatterHackers . Agg . UI . SystemWindow . AssertDebugNotDefined ( ) ;
MatterHackers . Agg . ImageProcessing . InvertLightness . AssertDebugNotDefined ( ) ;
MatterHackers . Localizations . TranslationMap . AssertDebugNotDefined ( ) ;
MatterHackers . MarchingSquares . MarchingSquaresByte . AssertDebugNotDefined ( ) ;
MatterHackers . MatterControl . PluginSystem . MatterControlPlugin . AssertDebugNotDefined ( ) ;
MatterHackers . MatterSlice . MatterSlice . AssertDebugNotDefined ( ) ;
MatterHackers . MeshVisualizer . MeshViewerWidget . AssertDebugNotDefined ( ) ;
MatterHackers . RenderOpenGl . GLMeshTrianglePlugin . AssertDebugNotDefined ( ) ;
}
2016-06-01 18:17:11 -07:00
public bool IsNetworkConnected ( )
{
return true ;
}
2015-02-19 10:56:54 -08:00
}
Move to new library model and view
- Add new listview control for library content
- Migrate library providers to containers
- Cloud, Sqlite, Directories, Queue, History
- Migrate SideBar components to containers
- Primatives, Text, Braille, ImageConverter
- Create new library container types
- Zip files, Calibration parts, Printer SDCards
- Reduce leftnav to Library, Settings, Controls, Options
- Add DragDrop support for image content
2017-05-19 22:33:55 -07:00
}