HatchKeeper  0.90
The Free Open-Source Egg Incubation Software
BatchDialog.cpp
Go to the documentation of this file.
1 /***********************************************************************
2  * @file src/Dialogs/BatchDialog.h
3  * @brief Declares BatchDialog and Related Functions
4  * @author mill-j
5  * @since (C) 2017 - 2023
6  * @copyright GNU GPL v3
7  * ********************************************************************/
8 
9 #include <wx/spinctrl.h>
10 #include <wx/choice.h>
11 #include <wx/wx.h>
12 
13 #include "DateDialog.cpp"
14 
15 using namespace std;
16 
22 class BatchDialog : public wxDialog
23 {
24  protected:
25  wxChoice* birdChoice;
26  wxChoice* breedChoice;
27  wxChoice* groupChoice;
28  wxChoice* IncubatorChoice;
29  wxTextCtrl* batchText;
30  wxStaticText* batchNameStat;
31  wxSpinCtrl* eggNumSpinner;
32  wxStaticText* NumberOfEggs;
33  wxChoice* candleChoice;
34  wxStaticText* showCandleText;
35  wxDatePickerCtrl* StartDatePicker;
36  wxStaticText* showStartText;
37  wxChoice* hatchChoice;
38  wxStaticText* showHatchText;
39  wxButton* cancelButton;
40  wxButton* saveButton;
41  bool isCancel = true; //lets other functs know if BatchDialog was saved or closed/canceled
42 
43  void OnBirdChoice(wxCommandEvent& );
44  void OnSaveButton(wxCommandEvent& );
45  void OnCancelButton(wxCommandEvent& );
46  void OnHatchChoice(wxCommandEvent& );
47  void OnBatchName(wxCommandEvent& );
48  int GetIncubationDays(int TypeSelect, string BreedName);
49  public:
50  bool WasCanceled();
51  BatchDialog( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxT("New Batch"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxCLOSE_BOX|wxCAPTION|wxSYSTEM_MENU );
52 };
53 
55 BatchDialog::BatchDialog( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxDialog( parent, id, title, pos, size, style )
56 {
57  this->SetSizeHints( wxDefaultSize, wxDefaultSize );
58 
59  //Colors
60  vector<int> ColorRed = Settings.GetIntArray("ColorRed");
61  vector<int> ColorGreen = Settings.GetIntArray("ColorGreen");
62  vector<int> ColorBlue = Settings.GetIntArray("ColorBlue");
63  this->SetForegroundColour( wxColor(ColorRed[8],ColorGreen[8],ColorBlue[8]) );
64  this->SetBackgroundColour( wxColor(ColorRed[7],ColorGreen[7],ColorBlue[7]) );
65  wxBoxSizer* bSizer1;
66  bSizer1 = new wxBoxSizer( wxVERTICAL );
67 
68  wxGridSizer* gSizer1;
69  gSizer1 = new wxGridSizer( 8, 2, 0, 0 );
70 
71  wxArrayString birdChoiceChoices;
72  birdChoice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, birdChoiceChoices, 0 );
73  birdChoice->SetSelection( 0 );
74  birdChoice->SetToolTip( l("Select Type") );
75  gSizer1->Add( birdChoice, 0, wxALL|wxEXPAND, 5 );
76 
77  wxArrayString breedChoiceChoices;
78  breedChoice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, breedChoiceChoices, 0 );
79  breedChoice->SetSelection( 0 );
80  breedChoice->SetToolTip( l("Select Breed") );
81  gSizer1->Add( breedChoice, 0, wxALL|wxEXPAND, 5 );
82 
83  wxArrayString groupChoiceChoices;
84  groupChoice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, groupChoiceChoices, 0 );
85  groupChoice->SetSelection( 0 );
86  groupChoice->SetToolTip( l("Select Group") );
87  gSizer1->Add( groupChoice, 0, wxALL|wxEXPAND, 5 );
88 
89  wxArrayString IncubatorChoiceChoices;
90  IncubatorChoice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, IncubatorChoiceChoices, 0 );
91  IncubatorChoice->SetSelection( 0 );
92  IncubatorChoice->SetToolTip( l("Select Incubator") );
93  gSizer1->Add( IncubatorChoice, 0, wxALL|wxEXPAND, 5 );
94 
95 
96  wxTextValidator textVal(wxFILTER_INCLUDE_CHAR_LIST,NULL);
97  textVal.SetCharIncludes(Info.GetValidChars().c_str());
98 
99  batchText = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0,textVal );
100  gSizer1->Add( batchText, 0, wxALL|wxEXPAND, 5 );
101 
102  batchNameStat = new wxStaticText( this, wxID_ANY, l("Batch Name"), wxDefaultPosition, wxDefaultSize, 0 );
103  batchNameStat->Wrap( -1 );
104  gSizer1->Add( batchNameStat, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL, 5 );
105 
106  eggNumSpinner = new wxSpinCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 1, 10000, 1 );
107  eggNumSpinner->SetForegroundColour( wxColor(0,0,0) );
108  gSizer1->Add( eggNumSpinner, 0, wxALL|wxEXPAND, 5 );
109 
110  NumberOfEggs = new wxStaticText( this, wxID_ANY, l("Eggs"), wxDefaultPosition, wxDefaultSize, 0 );
111  NumberOfEggs->Wrap( -1 );
112  gSizer1->Add( NumberOfEggs, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL, 5 );
113 
114  wxArrayString candleChoiceChoices;
115  candleChoice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, candleChoiceChoices, 0 );
116  candleChoice->SetSelection( 0 );
117  gSizer1->Add( candleChoice, 0, wxALL|wxEXPAND, 5 );
118 
119  showCandleText = new wxStaticText( this, wxID_ANY, l("Candle Date"), wxDefaultPosition, wxDefaultSize, 0 );
120  showCandleText->Wrap( -1 );
121  gSizer1->Add( showCandleText, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL, 5 );
122 
123  StartDatePicker = new wxDatePickerCtrl( this, wxID_ANY, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize, wxDP_DEFAULT );
124  gSizer1->Add( StartDatePicker, 0, wxALL|wxEXPAND, 5 );
125 
126  showStartText = new wxStaticText( this, wxID_ANY, l("Start Date"), wxDefaultPosition, wxDefaultSize, 0 );
127  showStartText->Wrap( -1 );
128  gSizer1->Add( showStartText, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL, 5 );
129 
130  wxString hatchChoiceChoices[] = { l("Automatic Calculation"), l("Set Hatch Date") };
131  int hatchChoiceNChoices = sizeof( hatchChoiceChoices ) / sizeof( wxString );
132  hatchChoice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,hatchChoiceNChoices, hatchChoiceChoices , 0 );
133  hatchChoice->SetSelection( 0 );
134  hatchChoice->Enable(false);
135  gSizer1->Add( hatchChoice, 0, wxALL|wxEXPAND, 5 );
136 
137  showHatchText = new wxStaticText( this, wxID_ANY, l("Hatch Date"), wxDefaultPosition, wxDefaultSize, 0 );
138  showHatchText->Wrap( -1 );
139  gSizer1->Add( showHatchText, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5 );
140 
141  cancelButton = new wxButton( this, wxID_ANY, l("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
142  gSizer1->Add( cancelButton, 0, wxALL|wxEXPAND, 5 );
143 
144  saveButton = new wxButton( this, wxID_ANY, l("Save"), wxDefaultPosition,wxDefaultSize, 0 );
145  saveButton->Enable(false);
146  gSizer1->Add( saveButton, 0, wxALL|wxEXPAND, 5 );
147 
148 
149  bSizer1->Add( gSizer1, 0, wxEXPAND, 5 );
150  bSizer1->SetSizeHints(this);
151  saveButton->Bind(wxEVT_BUTTON, &BatchDialog::OnSaveButton, this);
152  cancelButton->Bind(wxEVT_BUTTON, &BatchDialog::OnCancelButton, this);
153  birdChoice->Bind(wxEVT_CHOICE, &BatchDialog::OnBirdChoice, this);
154  hatchChoice->Bind(wxEVT_CHOICE, &BatchDialog::OnHatchChoice, this);
155  batchText->Bind(wxEVT_TEXT, &BatchDialog::OnBatchName, this);
156 
157  this->SetSizer( bSizer1 );
158  this->Layout();
159 
160  int counter;
161  //Insert Candle Days
162  for(counter = 0; counter < 50; counter++)
163  candleChoice->Append(l("Candle On Day ") + to_string(counter));
164 
165  candleChoice->SetSelection( Settings.GetInt("TillCandle") );
166 
167  //Insert Schedules
168  for (counter = 0; counter < Schedules.GetCount();counter++)
169  hatchChoice->Append( Schedules.GetName(counter).c_str());
170 
171  birdChoice->Append(l("Select Type"));
172  //Insert Types
173  for (counter = 0; counter < Types.GetCount();counter++)
174  birdChoice->Append(Types.GetName(counter).c_str());
175 
176  birdChoice->SetSelection(0);
177 
178  //Insert Groups
179  for (counter = 0; counter < Groups.GetCount();counter++)
180  groupChoice->Append(Groups.GetName(counter).c_str());
181 
182  groupChoice->SetSelection(0);
183 
184  //Show Incubators
185  vector<string> Inc = Settings.GetArray("Incubators");
186  for (counter = 0; counter < Inc.size();counter++)
187  IncubatorChoice->Append(Inc[counter].c_str());
188  IncubatorChoice->SetSelection(0);
189 }
190 
192 void BatchDialog::OnSaveButton(wxCommandEvent& )
193 {
194  if(birdChoice->GetSelection() == 0) {
195  wxMessageDialog dialog(this,
196  l("Please Select A Bird Type First!"));
197  dialog.ShowModal();
198  return;
199  }
200 
201  if(batchText->GetLineText(0) == "") {
202  wxMessageDialog dialog(this,
203  l("Please Enter Batch Name!"));
204  dialog.ShowModal();
205  return;
206  }
207 
208  if(!Batches.IsValidName(batchText->GetLineText(0).ToStdString())) {
209  wxMessageDialog dialog(this,
210  ("Name Exists! Please Enter A New Name!"));
211  dialog.ShowModal();
212  return;
213  }
214 
215  wxDateTime WXdate = StartDatePicker->GetValue();
216 
217  HK_Date HKdate(
218  WXdate.GetDay(wxDateTime::Local),
219  WXdate.GetMonth(wxDateTime::Local) + 1,
220  WXdate.GetYear(wxDateTime::Local)
221  );
222 
223  //Copy Start Date
224  HK_Date HKcandle = HKdate;
225  HKcandle.AddDays(candleChoice->GetSelection());
226 
227  //Get Hatch Date
228  HK_Date HKhatch = HKdate;
229  HKhatch.AddDays(GetIncubationDays(birdChoice->GetSelection() - 1,breedChoice->GetString(breedChoice->GetSelection()).ToStdString()));
230 
231  //Get Lockdown Date
232  HK_Date HKlock = HKhatch;
233  HKlock.SubtractDays(Settings.GetInt("BeforeHatch"));
234 
235  //Create A Batch With Our Data
236  HK_Batch Batch(
238  batchText->GetLineText(0).ToStdString(),
239  birdChoice->GetString(birdChoice->GetSelection()).ToStdString(),
240  breedChoice->GetString(breedChoice->GetSelection()).ToStdString(),
241  groupChoice->GetString(groupChoice->GetSelection()).ToStdString(),
242  l("Comments:"),
243  IncubatorChoice->GetString(IncubatorChoice->GetSelection()).ToStdString(),
244  eggNumSpinner->GetValue(),
245  0,
246  0,
247  HKdate,
248  HKcandle,
249  HKlock,
250  HKhatch,
251  0,
252  0
253  );
254 
255  if(!Database.Execute(Batch.GetSQL())) {
256  wxMessageDialog* dialog = new wxMessageDialog(this,
257  "Sqlite Error: " + Database.GetErrors());
258  dialog->ShowModal();
259  dialog->Destroy();
260  return;
261  }
262  isCancel = false;
263  EndModal(0);
264 }
265 
267 void BatchDialog::OnCancelButton(wxCommandEvent& )
268 {
269  isCancel = true;
270  EndModal(0);
271 }
272 
274 void BatchDialog::OnBatchName(wxCommandEvent& ) {
275  if(birdChoice->GetSelection() > 0 && batchText->GetLineText(0) != "")
276  saveButton->Enable(true);
277  else
278  saveButton->Enable(false);
279 }
280 
282 void BatchDialog::OnBirdChoice(wxCommandEvent& )
283 {
284  if(birdChoice->GetSelection() > 0) {
285  hatchChoice->Enable(true);
286 
287  if(batchText->GetLineText(0) != "")
288  saveButton->Enable(true);
289  }
290  else {
291  hatchChoice->Enable(false);
292  saveButton->Enable(false);
293  }
294 
295  string s = birdChoice->GetString(birdChoice->GetSelection()).ToStdString();
296 
297  breedChoice->Clear();
298 
299  for(int a = 0;a < Breeds.GetCount(); a++) {
300  if(s == Breeds.GetText(a))
301  breedChoice->Append(Breeds.GetName(a).c_str());
302  }
303 
304  breedChoice->Append(l("Unknown"));
305  breedChoice->SetSelection( 0 );
306 }
307 
309 void BatchDialog::OnHatchChoice(wxCommandEvent & )
310 {
311  int sel = hatchChoice->GetSelection();
312 
313  if (sel == 0)
314  return;
315 
316  if(birdChoice->GetSelection() == 0) {
317  wxMessageDialog dialog(this,
318  l("Please Select A Bird Type First!"));
319  dialog.ShowModal();
320  return;
321  }
322 
323  if (sel == 1) {
324  DateDialog dialog(this,2002,l("Select Hatch Date"));
325  dialog.ShowModal();
326  HK_Date Date = dialog.GetDate();
327 
328  Date.SubtractDays(GetIncubationDays(birdChoice->GetSelection() - 1,breedChoice->GetString(breedChoice->GetSelection()).ToStdString()));
329 
330  wxDateTime startD(
331  Date.GetDay(),
332  wxDateTime::Month(wxDateTime::Jan + Date.GetMonth() - 1),
333  Date.GetYear(),
334  0,0,0,0
335  );
336 
337  StartDatePicker->SetValue(startD);
338  }
339  if (sel > 1) {
340  HK_Date date = Schedules.GetDate(sel-2);
341 
342  date.SubtractDays(GetIncubationDays(birdChoice->GetSelection() - 1,breedChoice->GetString(breedChoice->GetSelection()).ToStdString()));
343 
344  wxDateTime startD(date.GetDay(),
345  wxDateTime::Month(wxDateTime::Jan + date.GetMonth() - 1),
346  date.GetYear(),0,0,0,0);
347 
348  StartDatePicker->SetValue(startD);
349  }
350 }
351 
353 int BatchDialog::GetIncubationDays(int TypeSelection, string BreedName) {
354  for(int a = 0; a < Breeds.GetCount(); a++) {
355  if(Breeds.GetName(a) == BreedName)
356  return Breeds.GetNumber(a);
357  }
358  return Types.GetNumber(TypeSelection);
359 }
360 
361 
HK_KeyValue Settings
Definition: Declare.h:36
HK_Info Info
Definition: Declare.h:34
HK_Batches Batches
Definition: Declare.h:44
HK_Storage Types
Definition: Declare.h:38
HK_Storage Breeds
Definition: Declare.h:37
HK_Database Database(HATCHKEEPER_DATA+"HatchKeeper.db")
std::string l(std::string Text)
A Helper Function For HK_Language.
Definition: Declare.h:52
HK_Storage Groups
Definition: Declare.h:41
HK_Storage Schedules
Definition: Declare.h:39
A Dialog For Adding A New Batch.
Definition: BatchDialog.cpp:23
wxChoice * groupChoice
Definition: BatchDialog.cpp:27
wxDatePickerCtrl * StartDatePicker
Definition: BatchDialog.cpp:35
wxTextCtrl * batchText
Definition: BatchDialog.cpp:29
wxStaticText * showCandleText
Definition: BatchDialog.cpp:34
wxChoice * candleChoice
Definition: BatchDialog.cpp:33
int GetIncubationDays(int TypeSelect, string BreedName)
Returns Incubation Days, Returns Default Days From The Selected Type If Breed Days Unavailible.
wxStaticText * NumberOfEggs
Definition: BatchDialog.cpp:32
wxChoice * breedChoice
Definition: BatchDialog.cpp:26
wxStaticText * batchNameStat
Definition: BatchDialog.cpp:30
void OnSaveButton(wxCommandEvent &)
Saves The New Batch To The Database.
wxChoice * hatchChoice
Definition: BatchDialog.cpp:37
wxButton * saveButton
Definition: BatchDialog.cpp:40
bool WasCanceled()
Returns True If The Cancel Button Was Clicked.
wxSpinCtrl * eggNumSpinner
Definition: BatchDialog.cpp:31
void OnCancelButton(wxCommandEvent &)
Cancels Add Batch And Closes Dialog.
void OnHatchChoice(wxCommandEvent &)
Fired When The Hatch Date Dropdown Changes.
void OnBatchName(wxCommandEvent &)
Fired When Batch Name Changes.
wxStaticText * showHatchText
Definition: BatchDialog.cpp:38
BatchDialog(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=wxT("New Batch"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxCLOSE_BOX|wxCAPTION|wxSYSTEM_MENU)
Constructor.
Definition: BatchDialog.cpp:55
wxStaticText * showStartText
Definition: BatchDialog.cpp:36
wxChoice * IncubatorChoice
Definition: BatchDialog.cpp:28
wxChoice * birdChoice
Definition: BatchDialog.cpp:25
void OnBirdChoice(wxCommandEvent &)
Fired When Type Changes.
wxButton * cancelButton
Definition: BatchDialog.cpp:39
This Dialog Allows The User To Select A Date.
Definition: DateDialog.cpp:24
HK_Date GetDate()
Returns Selected Date.
Definition: DateDialog.cpp:76
Holds Data For One Batch, Includes Basic Set/Get Methods.
Definition: Batch.h:22
vector< string > GetSQL()
Returns An SQL String To Set Batch In Database.
Definition: HK_Batch.cpp:190
int GetValidID()
Returns An ID That Isn't Used By Any HK_Batch In This HK_Batches Instance.
Definition: HK_Batches.cpp:194
bool IsValidName(string Name)
Returns True If Name Isn't Already In Use.
Definition: HK_Batches.cpp:216
bool Execute(string SQL)
Executes SQL Statments.
string GetErrors()
Returns sqlite3_errmsg(HK_DB))
A Class For Managing Dates.
Definition: Date.h:25
int GetMonth()
Returns Month Value.
Definition: HK_Date.cpp:60
bool SubtractDays(int Days)
Subtracts Days To This HK_Date.
Definition: HK_Date.cpp:79
bool AddDays(int Days)
Adds Days To This HK_Date.
Definition: HK_Date.cpp:70
int GetDay()
Returns Day Value.
Definition: HK_Date.cpp:57
int GetYear()
Returns Year Value.
Definition: HK_Date.cpp:63
std::string GetValidChars()
Returns Chars That Are Allowed In Text Controls.
Definition: HK_Info.cpp:12
vector< string > GetArray(int Selection)
Returns The Selected Value As An Array Of Strings.
vector< int > GetIntArray(int Selection)
Returns The Selected Value As An Array Of Integers.
int GetInt(int Selection)
Returns The Selected Setting Value As An Integer.
Definition: HK_KeyValue.cpp:49
HK_Date GetDate(int Select)
Returns The Selected Stored Date.
Definition: HK_Storage.cpp:71
int GetCount()
Returns The Number Of Elements Stored.
Definition: HK_Storage.cpp:101
int GetNumber(int Select)
Returns The Selected Stored Number.
Definition: HK_Storage.cpp:96
string GetName(int Select)
Returns The Selected Stored Name.
Definition: HK_Storage.cpp:61
string GetText(int Select)
Returns The Selected Stored Text.
Definition: HK_Storage.cpp:91