2015-01-28 16:07:40 -08:00
/ *
Copyright ( c ) 2015 , Kevin Pope
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 :
2015-01-28 16:07:40 -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 .
2015-01-28 16:07:40 -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 .
2015-01-28 16:07:40 -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 ,
2015-01-28 16:07:40 -08:00
either expressed or implied , of the FreeBSD Project .
* /
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg ;
2015-04-08 15:20:10 -07:00
using MatterHackers.Agg.PlatformAbstract ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI ;
2016-04-25 13:35:36 -07:00
using MatterHackers.MatterControl.CustomWidgets ;
2014-06-11 14:52:58 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2015-04-08 15:20:10 -07:00
using MatterHackers.VectorMath ;
using System ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl
2015-04-06 16:01:57 -07:00
{
public class PrintProgressBar : GuiWidget
{
2015-04-08 15:20:10 -07:00
private double currentPercent = 0 ;
private RGBA_Bytes completeColor = new RGBA_Bytes ( 255 , 255 , 255 ) ;
private TextWidget printTimeRemaining ;
private TextWidget printTimeElapsed ;
2016-12-29 06:55:12 -08:00
private EventHandler unregisterEvents ;
2015-04-08 15:20:10 -07:00
private bool widgetIsExtended ;
2016-05-03 18:22:59 -07:00
private Agg . Image . ImageBuffer upImageBuffer ;
private Agg . Image . ImageBuffer downImageBuffer ;
private ImageWidget indicatorWidget ;
2015-04-06 16:01:57 -07:00
public bool WidgetIsExtended
{
get { return widgetIsExtended ; }
set
{
widgetIsExtended = value ;
ToggleExtendedDisplayProperties ( ) ;
}
}
2015-04-08 15:20:10 -07:00
private void ToggleExtendedDisplayProperties ( )
2015-04-06 16:01:57 -07:00
{
if ( ! WidgetIsExtended )
{
indicatorWidget . Image = downImageBuffer ;
}
else
{
indicatorWidget . Image = upImageBuffer ;
}
}
public PrintProgressBar ( bool widgetIsExtended = true )
{
MinimumSize = new Vector2 ( 0 , 24 ) ;
HAnchor = HAnchor . ParentLeftRight ;
BackgroundColor = ActiveTheme . Instance . SecondaryAccentColor ;
Margin = new BorderDouble ( 0 ) ;
2014-02-10 19:17:25 -08:00
2015-04-06 16:01:57 -07:00
FlowLayoutWidget container = new FlowLayoutWidget ( FlowDirection . LeftToRight ) ;
container . AnchorAll ( ) ;
container . Padding = new BorderDouble ( 6 , 0 ) ;
2014-02-20 18:38:46 -08:00
2015-04-06 16:01:57 -07:00
printTimeElapsed = new TextWidget ( "" , pointSize : 11 ) ;
printTimeElapsed . Printer . DrawFromHintedCache = true ;
printTimeElapsed . AutoExpandBoundsToText = true ;
2016-04-25 13:35:36 -07:00
printTimeElapsed . VAnchor = VAnchor . ParentCenter ;
2015-04-06 16:01:57 -07:00
printTimeRemaining = new TextWidget ( "" , pointSize : 11 ) ;
2015-01-28 12:35:51 -08:00
printTimeRemaining . Printer . DrawFromHintedCache = true ;
printTimeRemaining . AutoExpandBoundsToText = true ;
2016-04-25 13:35:36 -07:00
printTimeRemaining . VAnchor = VAnchor . ParentCenter ;
2015-04-06 16:01:57 -07:00
container . AddChild ( printTimeElapsed ) ;
2016-04-25 13:35:36 -07:00
container . AddChild ( new HorizontalSpacer ( ) ) ;
2015-04-06 16:01:57 -07:00
container . AddChild ( printTimeRemaining ) ;
AddChild ( container ) ;
2014-02-10 19:17:25 -08:00
2016-04-29 07:45:15 -07:00
if ( UserSettings . Instance . IsTouchScreen )
2015-04-06 16:01:57 -07:00
{
upImageBuffer = StaticData . Instance . LoadIcon ( "TouchScreen/arrow_up_32x24.png" ) ;
downImageBuffer = StaticData . Instance . LoadIcon ( "TouchScreen/arrow_down_32x24.png" ) ;
2014-02-10 19:17:25 -08:00
2015-04-06 16:01:57 -07:00
indicatorWidget = new ImageWidget ( upImageBuffer ) ;
indicatorWidget . HAnchor = HAnchor . ParentCenter ;
indicatorWidget . VAnchor = VAnchor . ParentCenter ;
2014-02-10 19:17:25 -08:00
2015-04-06 16:01:57 -07:00
WidgetIsExtended = widgetIsExtended ;
GuiWidget indicatorOverlay = new GuiWidget ( ) ;
indicatorOverlay . AnchorAll ( ) ;
indicatorOverlay . AddChild ( indicatorWidget ) ;
AddChild ( indicatorOverlay ) ;
}
2014-10-17 14:11:56 -07:00
2016-05-03 18:22:59 -07:00
var clickOverlay = new GuiWidget ( ) ;
2014-10-17 14:11:56 -07:00
clickOverlay . AnchorAll ( ) ;
2016-05-03 18:22:59 -07:00
clickOverlay . Click + = ( s , e ) = >
{
// In touchscreen mode, expand or collapse the print status row when clicked
ApplicationView mainView = ApplicationController . Instance . MainView ;
if ( mainView is TouchscreenView )
{
( ( TouchscreenView ) mainView ) . ToggleTopContainer ( ) ;
}
} ;
2014-10-17 14:11:56 -07:00
AddChild ( clickOverlay ) ;
2015-04-06 16:01:57 -07:00
PrinterConnectionAndCommunication . Instance . ActivePrintItemChanged . RegisterEvent ( Instance_PrintItemChanged , ref unregisterEvents ) ;
PrinterConnectionAndCommunication . Instance . CommunicationStateChanged . RegisterEvent ( Instance_PrintItemChanged , ref unregisterEvents ) ;
2014-01-29 19:09:30 -08:00
2016-05-03 18:22:59 -07:00
SetThemedColors ( ) ;
2017-02-23 13:21:18 -08:00
// This is a bit of a hack. Now that we have the printing window we don't want this to show progress but it is still used on touch screen for expanding the display.
if ( ! UserSettings . Instance . IsTouchScreen )
{
UiThread . RunOnIdle ( OnIdle ) ;
UpdatePrintStatus ( ) ;
}
2014-10-17 14:11:56 -07:00
}
2017-02-03 13:06:08 -08:00
public override void OnClosed ( ClosedEventArgs e )
2015-04-06 16:01:57 -07:00
{
2016-05-03 18:22:59 -07:00
unregisterEvents ? . Invoke ( this , null ) ;
2015-04-06 16:01:57 -07:00
base . OnClosed ( e ) ;
}
private void SetThemedColors ( )
{
this . printTimeElapsed . TextColor = ActiveTheme . Instance . PrimaryAccentColor ;
this . printTimeRemaining . TextColor = ActiveTheme . Instance . PrimaryAccentColor ;
this . BackgroundColor = ActiveTheme . Instance . SecondaryAccentColor ;
}
public void ThemeChanged ( object sender , EventArgs e )
{
//Set background color to new theme
SetThemedColors ( ) ;
this . Invalidate ( ) ;
}
2015-04-08 15:20:10 -07:00
private void Instance_PrintItemChanged ( object sender , EventArgs e )
2015-04-06 16:01:57 -07:00
{
UpdatePrintStatus ( ) ;
}
2015-06-11 12:06:40 -07:00
private void OnIdle ( )
2015-04-06 16:01:57 -07:00
{
currentPercent = PrinterConnectionAndCommunication . Instance . PercentComplete ;
UpdatePrintStatus ( ) ;
2016-04-25 12:51:29 -07:00
if ( ! HasBeenClosed )
2015-04-06 16:01:57 -07:00
{
UiThread . RunOnIdle ( OnIdle , 1 ) ;
}
}
2015-04-08 15:20:10 -07:00
2015-04-06 16:01:57 -07:00
private void UpdatePrintStatus ( )
{
if ( PrinterConnectionAndCommunication . Instance . ActivePrintItem = = null )
{
printTimeElapsed . Text = string . Format ( "" ) ;
printTimeRemaining . Text = string . Format ( "" ) ;
}
else
{
int secondsPrinted = PrinterConnectionAndCommunication . Instance . SecondsPrinted ;
int hoursPrinted = ( int ) ( secondsPrinted / ( 60 * 60 ) ) ;
int minutesPrinted = ( int ) ( secondsPrinted / 60 - hoursPrinted * 60 ) ;
secondsPrinted = secondsPrinted % 60 ;
if ( secondsPrinted > 0 )
{
if ( hoursPrinted > 0 )
{
printTimeElapsed . Text = string . Format ( "{0}:{1:00}:{2:00}" ,
hoursPrinted ,
minutesPrinted ,
secondsPrinted ) ;
}
else
{
printTimeElapsed . Text = string . Format ( "{0}:{1:00}" ,
minutesPrinted ,
secondsPrinted ) ;
}
}
else
{
printTimeElapsed . Text = string . Format ( "" ) ;
}
string printPercentRemainingText = string . Format ( "{0:0.0}%" , currentPercent ) ;
if ( PrinterConnectionAndCommunication . Instance . PrinterIsPrinting | | PrinterConnectionAndCommunication . Instance . PrinterIsPaused )
{
printTimeRemaining . Text = printPercentRemainingText ;
}
else if ( PrinterConnectionAndCommunication . Instance . PrintIsFinished )
{
printTimeRemaining . Text = "Done!" ;
}
else
{
printTimeRemaining . Text = string . Format ( "" ) ;
}
}
this . Invalidate ( ) ;
}
public override void OnDraw ( Graphics2D graphics2D )
{
graphics2D . FillRectangle ( 0 , 0 , Width * currentPercent / 100 , Height , completeColor ) ;
2015-04-08 15:20:10 -07:00
2015-04-06 16:01:57 -07:00
base . OnDraw ( graphics2D ) ;
}
}
2015-04-08 15:20:10 -07:00
}