forked from MatterHackers/MatterControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintHistoryWidget.cs
211 lines (179 loc) · 7.67 KB
/
PrintHistoryWidget.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Copyright (c) 2014, Kevin Pope
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.
*/
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.DataStorage;
using System;
namespace MatterHackers.MatterControl.PrintHistory
{
public class PrintHistoryWidget : GuiWidget
{
private TextImageButtonFactory textImageButtonFactory = new TextImageButtonFactory();
private CheckBox showOnlyCompletedCheckbox;
private CheckBox showTimestampCheckbox;
private PrintHistoryDataView historyView;
private TextWidget completedPrintsCount;
private TextWidget totalPrintTime;
public PrintHistoryWidget()
{
SetDisplayAttributes();
textImageButtonFactory.borderWidth = 0;
RGBA_Bytes historyPanelTextColor = ActiveTheme.Instance.PrimaryTextColor;
FlowLayoutWidget allControls = new FlowLayoutWidget(FlowDirection.TopToBottom);
{
FlowLayoutWidget completedStatsContainer = new FlowLayoutWidget();
completedStatsContainer.HAnchor = Agg.UI.HAnchor.ParentLeftRight;
completedStatsContainer.Padding = new BorderDouble(6, 2);
showOnlyCompletedCheckbox = new CheckBox("Only Show Completed".Localize(), historyPanelTextColor, textSize: 10);
showOnlyCompletedCheckbox.Margin = new BorderDouble(top: 8);
bool showOnlyCompleted = (UserSettings.Instance.get("PrintHistoryFilterShowCompleted") == "true");
showOnlyCompletedCheckbox.Checked = showOnlyCompleted;
showOnlyCompletedCheckbox.Width = 200;
completedStatsContainer.AddChild(new TextWidget("Completed Prints:".Localize() + " ", pointSize: 10, textColor: historyPanelTextColor));
completedPrintsCount = new TextWidget(GetCompletedPrints().ToString(), pointSize: 14, textColor: historyPanelTextColor);
completedPrintsCount.AutoExpandBoundsToText = true;
completedStatsContainer.AddChild(completedPrintsCount);
completedStatsContainer.AddChild(new HorizontalSpacer());
completedStatsContainer.AddChild(showOnlyCompletedCheckbox);
FlowLayoutWidget historyStatsContainer = new FlowLayoutWidget();
historyStatsContainer.HAnchor = Agg.UI.HAnchor.ParentLeftRight;
historyStatsContainer.Padding = new BorderDouble(6, 2);
showTimestampCheckbox = new CheckBox("Show Timestamp".Localize(), historyPanelTextColor, textSize: 10);
//showTimestampCheckbox.Margin = new BorderDouble(top: 8);
bool showTimestamp = (UserSettings.Instance.get("PrintHistoryFilterShowTimestamp") == "true");
showTimestampCheckbox.Checked = showTimestamp;
showTimestampCheckbox.Width = 200;
historyStatsContainer.AddChild(new TextWidget("Total Print Time:".Localize() + " ", pointSize: 10, textColor: historyPanelTextColor));
totalPrintTime = new TextWidget(GetPrintTimeString(), pointSize: 14, textColor: historyPanelTextColor);
totalPrintTime.AutoExpandBoundsToText = true;
historyStatsContainer.AddChild(totalPrintTime);
historyStatsContainer.AddChild(new HorizontalSpacer());
historyStatsContainer.AddChild(showTimestampCheckbox);
FlowLayoutWidget searchPanel = new FlowLayoutWidget(FlowDirection.TopToBottom);
searchPanel.BackgroundColor = ActiveTheme.Instance.TransparentDarkOverlay;
searchPanel.HAnchor = HAnchor.ParentLeftRight;
searchPanel.Padding = new BorderDouble(0, 6, 0, 2);
searchPanel.AddChild(completedStatsContainer);
searchPanel.AddChild(historyStatsContainer);
FlowLayoutWidget buttonPanel = new FlowLayoutWidget();
buttonPanel.HAnchor = HAnchor.ParentLeftRight;
buttonPanel.Padding = new BorderDouble(0, 3);
{
GuiWidget spacer = new GuiWidget();
spacer.HAnchor = HAnchor.ParentLeftRight;
buttonPanel.AddChild(spacer);
}
allControls.AddChild(searchPanel);
historyView = new PrintHistoryDataView();
historyView.DoneLoading += historyView_DoneLoading;
allControls.AddChild(historyView);
allControls.AddChild(buttonPanel);
}
allControls.AnchorAll();
this.AddChild(allControls);
AddHandlers();
}
private void historyView_DoneLoading(object sender, EventArgs e)
{
UpdateCompletedCount();
}
private void UpdateCompletedCount()
{
completedPrintsCount.Text = GetCompletedPrints().ToString();
totalPrintTime.Text = GetPrintTimeString();
}
private void AddHandlers()
{
showOnlyCompletedCheckbox.CheckedStateChanged += UpdateHistoryFilterShowCompleted;
showTimestampCheckbox.CheckedStateChanged += UpdateHistoryFilterShowTimestamp;
}
private void UpdateHistoryFilterShowCompleted(object sender, EventArgs e)
{
if (showOnlyCompletedCheckbox.Checked)
{
UserSettings.Instance.set("PrintHistoryFilterShowCompleted", "true");
}
else
{
UserSettings.Instance.set("PrintHistoryFilterShowCompleted", "false");
}
historyView.LoadHistoryItems();
}
private void UpdateHistoryFilterShowTimestamp(object sender, EventArgs e)
{
if (showTimestampCheckbox.Checked)
{
UserSettings.Instance.set("PrintHistoryFilterShowTimestamp", "true");
}
else
{
UserSettings.Instance.set("PrintHistoryFilterShowTimestamp", "false");
}
historyView.ShowTimestamp = showTimestampCheckbox.Checked;
historyView.LoadHistoryItems();
}
private int GetCompletedPrints()
{
//string query = "SELECT COUNT(*) FROM PrintTask WHERE PrintComplete = '{0}';".FormatWith(true);
var results = Datastore.Instance.dbSQLite.Table<PrintTask>().Where(o => o.PrintComplete == true);
return results.Count();
}
private int GetTotalPrintSeconds()
{
return Datastore.Instance.dbSQLite.ExecuteScalar<int>("SELECT SUM(PrintTimeSeconds) FROM PrintTask");
}
private string GetPrintTimeString()
{
int seconds = GetTotalPrintSeconds();
TimeSpan span = new TimeSpan(0, 0, seconds);
string timeString;
if (seconds <= 0)
{
timeString = "0min";
}
else if (seconds > 86400)
{
timeString = "{0}d {1}hrs {2}min".FormatWith(span.Days, span.Hours, span.Minutes);
}
else if (seconds > 3600)
{
timeString = "{0}hrs {1}min".FormatWith(span.Hours, span.Minutes);
}
else
{
timeString = "{0}min".FormatWith(span.Minutes);
}
return timeString;
}
private void SetDisplayAttributes()
{
this.Padding = new BorderDouble(3);
this.BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor;
this.AnchorAll();
}
}
}