YATiSh
Yet Another TIme SHeet
yatishDlgTimeslot.cpp
Go to the documentation of this file.
1 #include "wx_pch.h"
2 #include "yatishDlgTimeslot.h"
3 
16 class DateValidator : public wxValidator {
17  public:
18  DateValidator () { pdt = nullptr; }
19  DateValidator (wxDateTime * p) { pdt = p; }
20  DateValidator ( const DateValidator& v ) : wxValidator() { pdt = v.pdt; }
21  wxObject * Clone () const { return new DateValidator (*this); }
22  bool TransferFromWindow ();
23  bool TransferToWindow ();
24  bool Validate (wxWindow *) { return true; }
25  private:
26  wxDateTime * pdt;
27  };
29  wxDatePickerCtrl * date = (wxDatePickerCtrl *) m_validatorWindow;
30  *pdt = date->GetValue();
31  return true;
32 }
34  wxDatePickerCtrl * date = (wxDatePickerCtrl *) m_validatorWindow;
35  date->SetValue (*pdt);
36  return true;
37 }
38 
42 class TimeValidator : public wxValidator {
43  public:
44  TimeValidator () { pdt = nullptr; }
45  TimeValidator (wxDateTime * p) { pdt = p; }
46  TimeValidator ( const TimeValidator& v ) : wxValidator() { pdt = v.pdt; }
47  wxObject * Clone () const { return new TimeValidator (*this); }
48  bool TransferFromWindow ();
49  bool TransferToWindow ();
50  bool Validate (wxWindow *) { return true; }
51  private:
52  wxDateTime * pdt;
53  };
55  wxTimePickerCtrl * date = (wxTimePickerCtrl *) m_validatorWindow;
56  *pdt = date->GetValue();
57  return true;
58 }
60  wxTimePickerCtrl * date = (wxTimePickerCtrl *) m_validatorWindow;
61  date->SetValue (*pdt);
62  return true;
63 }
64 
65 #ifndef WX_PRECOMP
66  //(*InternalHeadersPCH(yatishDlgTimeslot)
67  #include <wx/string.h>
68  #include <wx/intl.h>
69  //*)
70 #endif
71 //(*InternalHeaders(yatishDlgTimeslot)
72 //*)
73 
74 //(*IdInit(yatishDlgTimeslot)
75 const long yatishDlgTimeslot::idDatePicker1 = wxNewId();
76 const long yatishDlgTimeslot::idTimePicker1 = wxNewId();
77 const long yatishDlgTimeslot::idDatePicker2 = wxNewId();
78 const long yatishDlgTimeslot::idTimePicker2 = wxNewId();
79 const long yatishDlgTimeslot::idChoiceProject = wxNewId();
80 const long yatishDlgTimeslot::idChoiceTask = wxNewId();
81 const long yatishDlgTimeslot::idChoiceTool = wxNewId();
82 //*)
83 
84 BEGIN_EVENT_TABLE (yatishDlgTimeslot, wxDialog)
85  //(*EventTable(yatishDlgTimeslot)
86  //*)
87 END_EVENT_TABLE()
88 
89 
93  BuildContent();
94  pdb = p_db;
95  id = sql_id;
96  bool choiceOK;
97  choiceOK = pdb->FillChoice (choiceProject, project_tid);
98  choiceOK = pdb->FillChoice (choiceTask, task_tid);
99  choiceOK = pdb->FillChoice (choiceTool, tool_tid);
100  if (!choiceOK) wxLogError ("Initialization of choices failed (yatishDlgTimeslot)");
101  long activity_id = pdb->Activity (id);
102  project = pdb->ChoiceSelector (project_tid, activity_id);
103  task = pdb->ChoiceSelector (task_tid, activity_id);
104  tool = pdb->ChoiceSelector (tool_tid, activity_id);
105  choiceProject->SetValidator ( wxGenericValidator (&project) );
106  choiceTask ->SetValidator ( wxGenericValidator (&task) );
107  choiceTool ->SetValidator ( wxGenericValidator (&tool) );
108  if ( pdb->ReadDates (id, date1, date2) ) {
109  datePicker1->SetValidator ( DateValidator (&date1) );
110  datePicker2->SetValidator ( DateValidator (&date2) );
111  time1 = date1; time2 = date2; // date part not used
112  timePicker1->SetValidator ( TimeValidator (&time1) );
113  timePicker2->SetValidator ( TimeValidator (&time2) );
114  } else
115  wxLogError ("Initialization of dates failed (yatishDlgTimeslot)");
116  Fit(); SetMaxSize ( GetSize() ); // Fit() really needed here !
117 }
118 
121  BuildContent();
122  staticBoxSizer->GetStaticBox()->SetLabel (_("New row") );
123  pdb = p_db;
124  buttonSave->Disable();
125  bool choiceOK;
126  choiceOK = pdb->FillChoice (choiceProject, project_tid);
127  choiceOK = pdb->FillChoice (choiceTask, task_tid);
128  choiceOK = pdb->FillChoice (choiceTool, tool_tid);
129  if (!choiceOK) wxLogError ("Initialization of choices failed (yatishDlgTimeslot)");
130  project = 0;
131  task = 0;
132  tool = 0;
133  choiceProject->SetValidator ( wxGenericValidator (&project) );
134  choiceTask ->SetValidator ( wxGenericValidator (&task) );
135  choiceTool ->SetValidator ( wxGenericValidator (&tool) );
136  date1 = wxDateTime::Now(); date2 = date1;
137  datePicker1->SetValidator ( DateValidator (&date1) );
138  datePicker2->SetValidator ( DateValidator (&date2) );
139  time1 = date1; time2 = date2;
140  timePicker1->SetValidator ( TimeValidator (&time1) );
141  timePicker2->SetValidator ( TimeValidator (&time2) );
142  Fit(); SetMaxSize ( GetSize() );
143  if ( choiceProject->IsEmpty() || choiceTask->IsEmpty() || choiceTool->IsEmpty() )
144  buttonNew->Disable(); // in case we get here while some tables are still empty
145 }
146 
149  //(*Initialize(yatishDlgTimeslot)
150  wxBoxSizer* BoxSizer1;
151  wxBoxSizer* BoxSizer2;
152  wxButton* buttonCancel;
153  wxFlexGridSizer* FlexGridSizer1;
154  wxStaticText* StaticText1;
155  wxStaticText* StaticText2;
156 
157  Create(0, wxID_ANY, _("Table: yatish_timeslot"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
158  BoxSizer1 = new wxBoxSizer(wxVERTICAL);
159  staticBoxSizer = new wxStaticBoxSizer(wxHORIZONTAL, this, _("(previously selected row)"));
160  FlexGridSizer1 = new wxFlexGridSizer(3, 3, 0, 0);
161  StaticText1 = new wxStaticText(this, wxID_ANY, _("Start:"), wxDefaultPosition, wxDefaultSize, 0, _T("wxID_ANY"));
162  FlexGridSizer1->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
163  datePicker1 = new wxDatePickerCtrl(this, idDatePicker1, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize, wxDP_DEFAULT|wxDP_SHOWCENTURY, wxDefaultValidator, _T("idDatePicker1"));
164  FlexGridSizer1->Add(datePicker1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
165  timePicker1 = new wxTimePickerCtrl(this,idTimePicker1,wxDefaultDateTime,wxDefaultPosition,wxDefaultSize,wxDP_DEFAULT,wxDefaultValidator,_T("idTimePicker1"));
166  FlexGridSizer1->Add(timePicker1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
167  StaticText2 = new wxStaticText(this, wxID_ANY, _("Stop:"), wxDefaultPosition, wxDefaultSize, 0, _T("wxID_ANY"));
168  FlexGridSizer1->Add(StaticText2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
169  datePicker2 = new wxDatePickerCtrl(this, idDatePicker2, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize, wxDP_DEFAULT|wxDP_SHOWCENTURY, wxDefaultValidator, _T("idDatePicker2"));
170  FlexGridSizer1->Add(datePicker2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
171  timePicker2 = new wxTimePickerCtrl(this,idTimePicker2,wxDefaultDateTime,wxDefaultPosition,wxDefaultSize,wxDP_DEFAULT,wxDefaultValidator,_T("idTimePicker2"));
172  FlexGridSizer1->Add(timePicker2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
173  choiceProject = new wxChoice(this, idChoiceProject, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("idChoiceProject"));
174  FlexGridSizer1->Add(choiceProject, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
175  choiceTask = new wxChoice(this, idChoiceTask, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("idChoiceTask"));
176  FlexGridSizer1->Add(choiceTask, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
177  choiceTool = new wxChoice(this, idChoiceTool, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("idChoiceTool"));
178  FlexGridSizer1->Add(choiceTool, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
179  staticBoxSizer->Add(FlexGridSizer1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
180  BoxSizer1->Add(staticBoxSizer, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
181  BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
182  buttonCancel = new wxButton(this, wxID_CANCEL, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("wxID_CANCEL"));
183  BoxSizer2->Add(buttonCancel, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
184  buttonSave = new wxButton(this, wxID_SAVE, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("wxID_SAVE"));
185  BoxSizer2->Add(buttonSave, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
186  buttonNew = new wxButton(this, wxID_NEW, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("wxID_NEW"));
187  BoxSizer2->Add(buttonNew, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
188  BoxSizer1->Add(BoxSizer2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
189  SetSizer(BoxSizer1);
190  BoxSizer1->Fit(this);
191  BoxSizer1->SetSizeHints(this);
192 
193  Connect(wxID_SAVE,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&yatishDlgTimeslot::OnButtonSaveClick);
194  Connect(wxID_NEW,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&yatishDlgTimeslot::OnButtonNewClick);
195  //*)
196 }
197 
199  //(*Destroy(yatishDlgTimeslot)
200  //*)
201 }
202 
203 void yatishDlgTimeslot::OnButtonSaveClick (wxCommandEvent& event) {
204  TransferDataFromWindow();
205  wxDateTime dt1 = date1 + ( time1 - time1.GetDateOnly() ); // The date of the wxDateTime returned by a
206  wxDateTime dt2 = date2 + ( time2 - time2.GetDateOnly() ); // wxTimePickerCtrl is Jan 1st, current year!
207  if ( pdb->RecordTimeslot (id, dt1, dt2, project, task, tool) )
208  EndModal (wxID_SAVE);
209  else
210  EndModal (wxID_CANCEL);
211 }
212 
213 void yatishDlgTimeslot::OnButtonNewClick (wxCommandEvent& event) {
214  TransferDataFromWindow();
215  wxDateTime dt1 = date1 + ( time1 - time1.GetDateOnly() );
216  wxDateTime dt2 = date2 + ( time2 - time2.GetDateOnly() );
217  if ( pdb->RecordTimeslot (wxNOT_FOUND, dt1, dt2, project, task, tool) )
218  EndModal (wxID_NEW);
219  else
220  EndModal (wxID_CANCEL);
221 }
Simplified from the wxWidgets Wiki:
bool Validate(wxWindow *)
DateValidator(wxDateTime *p)
wxDateTime * pdt
DateValidator(const DateValidator &v)
wxObject * Clone() const
This class is necessary because wxGenericValidator doesn't support wxTimePickerCtrl.
TimeValidator(const TimeValidator &v)
wxObject * Clone() const
wxDateTime * pdt
TimeValidator(wxDateTime *p)
bool Validate(wxWindow *)
Interacts with yatish tables in a SQLite database.
bool FillChoice(wxChoice *, tableID)
Fills a wxChoice.
bool RecordTimeslot(long, const wxDateTime &, const wxDateTime &, int, int, int)
Modifies or creates a record in the yatish_timeslot table.
wxDatePickerCtrl * datePicker2
static const long idChoiceProject
static const long idChoiceTask
static const long idTimePicker1
yatishDlgTimeslot(yatishDBsqlite *, long)
This constructor initializes the dialog box with data from the SQL record identified by sql_id.
wxDatePickerCtrl * datePicker1
void OnButtonSaveClick(wxCommandEvent &event)
wxStaticBoxSizer * staticBoxSizer
wxTimePickerCtrl * timePicker1
static const long idDatePicker2
wxChoice * choiceProject
void OnButtonNewClick(wxCommandEvent &event)
yatishDBsqlite * pdb
wxTimePickerCtrl * timePicker2
static const long idDatePicker1
static const long idChoiceTool
static const long idTimePicker2
@ tool_tid
Definition: yatishDB.h:16
@ task_tid
Definition: yatishDB.h:15
@ project_tid
Definition: yatishDB.h:17