-
Notifications
You must be signed in to change notification settings - Fork 0
/
ualarm.pas
646 lines (560 loc) · 19.1 KB
/
ualarm.pas
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//-----------------------------------------------------------------------------
// USB / Serial, Multi Channel Timer Programmer Console.
// Timer configuration frame.
//
// Copyright (C) 2020 SriKIT contributors.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Last updated: Dilshan Jayakody [24th Nov 2020]
//
// Update log:
// [24/11/2020] - Initial version - Dilshan Jayakody.
//-----------------------------------------------------------------------------
unit ualarm;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, StdCtrls, ExtCtrls, ComCtrls, ucommon,
Dialogs, Menus, Clipbrd;
type
TAlarmComponent = (alNone, alStartYear, alEndYear, alStartMonth, alEndMonth, alStartDay, alEndDay,
alStartHour, alEndHour, alStartMinute, alEndMinute, alStartSecond, alEndSecond);
{ TfmTimer }
TfmTimer = class(TFrame)
chkSelect: TCheckBox;
cmbStartMonth: TComboBox;
cmbEndMonth: TComboBox;
imglMenu: TImageList;
lblEnd: TLabel;
lblChannel: TLabel;
lblStartDate1: TLabel;
lblStartHour1: TLabel;
lblStartMinute1: TLabel;
lblStartMonth1: TLabel;
lblStartSec1: TLabel;
lblStartYear1: TLabel;
mnuDuplicate: TMenuItem;
mnuPasteValues: TMenuItem;
mnuMuteDate: TMenuItem;
mnuDelete: TMenuItem;
mnuSep2: TMenuItem;
mnuSep1: TMenuItem;
mnuPaste: TMenuItem;
mnuCut: TMenuItem;
mnuCopy: TMenuItem;
mnuItem: TPopupMenu;
txtEndDate: TEdit;
txtChannel: TEdit;
txtStartHour: TEdit;
txtEndHour: TEdit;
txtStartMinute: TEdit;
txtEndMinute: TEdit;
txtStartSeconds: TEdit;
lblStartHour: TLabel;
lblStartMinute: TLabel;
lblStartSec: TLabel;
lblStartMonth: TLabel;
lblStart: TLabel;
lblStartDate: TLabel;
txtEndSeconds: TEdit;
txtStartYear: TEdit;
gpAlarm: TGroupBox;
imgWarning: TImage;
lblStartYear: TLabel;
txtStartDate: TEdit;
txtEndYear: TEdit;
updEndDate: TUpDown;
updEndHour: TUpDown;
updEndMinute: TUpDown;
updEndSeconds: TUpDown;
updStartYear: TUpDown;
updStartDate: TUpDown;
updStartHour: TUpDown;
updStartMinute: TUpDown;
updStartSeconds: TUpDown;
updEndYear: TUpDown;
updChannel: TUpDown;
procedure cmbStartMonthChange(Sender: TObject);
procedure mnuCopyClick(Sender: TObject);
procedure mnuCutClick(Sender: TObject);
procedure mnuDeleteClick(Sender: TObject);
procedure mnuDuplicateClick(Sender: TObject);
procedure mnuItemPopup(Sender: TObject);
procedure mnuMuteDateClick(Sender: TObject);
procedure mnuPasteClick(Sender: TObject);
procedure mnuPasteValuesClick(Sender: TObject);
procedure updChannelChangingEx(Sender: TObject; var AllowChange: Boolean;
NewValue: SmallInt; Direction: TUpDownDirection);
procedure updStartDateChangingEx(Sender: TObject; var AllowChange: Boolean;
NewValue: SmallInt; Direction: TUpDownDirection);
procedure updStartHourChangingEx(Sender: TObject; var AllowChange: Boolean;
NewValue: SmallInt; Direction: TUpDownDirection);
procedure updStartMinuteChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection
);
procedure updStartSecondsChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection
);
procedure updStartYearChanging(Sender: TObject; var AllowChange: Boolean);
procedure updStartYearChangingEx(Sender: TObject; var AllowChange: Boolean;
NewValue: SmallInt; Direction: TUpDownDirection);
private
channelReset: boolean;
mainWindow: TForm;
FOnValueChanged: TNotifyEvent;
public
Constructor Create(AOwner: TComponent; baseWindow: TForm); reintroduce;
procedure SetAlarmInfo(info: TAlarmInfo; maxChannelCount: Byte);
function GetAlarmInfo() : TAlarmInfo;
function IsSelected() : Boolean;
function IsChannelReset() : Boolean;
function IsValidConfiguration(component: TAlarmComponent; val: Integer) : Boolean;
property OnValueChanged: TNotifyEvent read FOnValueChanged write FOnValueChanged;
end;
const
// Minimum values for date / time fields.
MIN_YEAR: Integer = 2020;
MIN_DATE: Integer = 1;
MIN_MONTH: Integer = 1;
NOT_USED_LABEL: string = 'None';
implementation
uses
uMain;
{$R *.lfm}
{ TfmTimer }
Constructor TfmTimer.Create(AOwner: TComponent; baseWindow: TForm);
begin
inherited Create(AOwner);
channelReset := false;
mainWindow := baseWindow;
end;
{$hints off}
procedure TfmTimer.updStartYearChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection);
var
txtEdit: TEdit;
component: TAlarmComponent;
begin
// Select either start-year or end-year text box.
if(TControl(Sender).Tag = 1) then
begin
txtEdit := txtEndYear;
component := alEndYear;
end
else
begin
txtEdit := txtStartYear;
component := alStartYear;
end;
// Apply new value into selected text box.
if(NewValue < MIN_YEAR) then
begin
// Remove year component from the UI.
txtEdit.Text := NOT_USED_LABEL;
end
else
begin
txtEdit.Text := IntToStr(NewValue);
end;
// Perform validations for user inputs.
imgWarning.Visible := not IsValidConfiguration(component, NewValue);
end;
{$hints on}
{$hints off}
procedure TfmTimer.updStartDateChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection);
var
txtEdit: TEdit;
component: TAlarmComponent;
begin
// Select either start-date or end-date text box.
if(TControl(Sender).Tag = 1) then
begin
txtEdit := txtEndDate;
component := alEndDay;
end
else
begin
txtEdit := txtStartDate;
component := alStartDay;
end;
// Apply new value into selected text box.
if(NewValue < MIN_DATE) then
begin
// Remove date component from the UI.
txtEdit.Text := NOT_USED_LABEL;
end
else
begin
txtEdit.Text := IntToStr(NewValue);
end;
// Perform validations for user inputs.
imgWarning.Visible := not IsValidConfiguration(component, NewValue);
end;
{$hints on}
{$hints off}
procedure TfmTimer.updChannelChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection);
begin
txtChannel.Text := IntToStr(NewValue);
end;
{$hints on}
procedure TfmTimer.cmbStartMonthChange(Sender: TObject);
var
component: TAlarmComponent;
cmbPos : Integer;
begin
if(TControl(Sender).Tag = 1) then
begin
component := alEndMonth;
cmbPos := cmbEndMonth.ItemIndex;
end
else
begin
component := alStartMonth;
cmbPos := cmbStartMonth.ItemIndex;
end;
imgWarning.Visible := not IsValidConfiguration(component, cmbPos);
if(Assigned(FOnValueChanged))then
begin
FOnValueChanged(self);
end;
end;
procedure TfmTimer.mnuCopyClick(Sender: TObject);
var
tmpAlarmInfo : TAlarmInfo;
clipbrdStream : TMemoryStream;
begin
// Get current values from the UI and save it to memory stream.
tmpAlarmInfo := GetAlarmInfo();
clipbrdStream := TMemoryStream.Create;
SaveAlarmInfoToStream(tmpAlarmInfo, clipbrdStream);
clipbrdStream.Position := 0;
// Save memory stream into clipboard buffer.
Clipboard.AddFormat((TfrmMain(mainWindow)).ClipboardRegFormat, clipbrdStream);
FreeAndNil(clipbrdStream);
end;
procedure TfmTimer.mnuCutClick(Sender: TObject);
begin
mnuCopyClick(Sender);
Application.ProcessMessages;
mnuDeleteClick(Sender);
end;
procedure TfmTimer.mnuDeleteClick(Sender: TObject);
begin
// Perform async delete to avoid component communication / render errors.
Application.ProcessMessages;
TfrmMain(mainWindow).DeleteTimerAsync(self.Name);
end;
procedure TfmTimer.mnuDuplicateClick(Sender: TObject);
begin
TfrmMain(mainWindow).DuplicateTimer(GetAlarmInfo());
end;
procedure TfmTimer.mnuItemPopup(Sender: TObject);
begin
// Enable paste menu if clipboard format is available.
mnuPaste.Enabled := Clipboard.HasFormat((TfrmMain(mainWindow)).ClipboardRegFormat);
mnuPasteValues.Enabled := mnuPaste.Enabled;
Application.ProcessMessages;
end;
procedure TfmTimer.mnuMuteDateClick(Sender: TObject);
var
tempChangeFlag: Boolean;
begin
// Set all date components to NONE.
tempChangeFlag := true;
updStartYear.Position := updStartYear.Min;
updStartYearChangingEx(updStartYear, tempChangeFlag, updStartYear.Min, TUpDownDirection.updNone);
updEndYear.Position := updEndYear.Min;
updStartYearChangingEx(updEndYear, tempChangeFlag, updEndYear.Min, TUpDownDirection.updNone);
updStartDate.Position := updStartDate.Min;
updStartDateChangingEx(updStartDate, tempChangeFlag, updStartDate.Min, TUpDownDirection.updNone);
updEndDate.Position := updEndDate.Min;
updStartDateChangingEx(updEndDate, tempChangeFlag, updEndDate.Min, TUpDownDirection.updNone);
cmbStartMonth.ItemIndex := 0;
cmbStartMonthChange(cmbStartMonth);
cmbEndMonth.ItemIndex := 0;
cmbStartMonthChange(cmbEndMonth);
end;
procedure TfmTimer.mnuPasteClick(Sender: TObject);
begin
TfrmMain(mainWindow).PasteNewTimer();
Application.ProcessMessages;
end;
procedure TfmTimer.mnuPasteValuesClick(Sender: TObject);
var
clipboardBuffer: TMemoryStream;
alarmInfo: TAlarmInfo;
mainForm: TfrmMain;
begin
mainForm := TfrmMain(mainWindow);
if(Clipboard.HasFormat(mainForm.ClipboardRegFormat)) then
begin
try
clipboardBuffer := TMemoryStream.Create;
// Get alarm information from clipboard.
Clipboard.GetFormat(mainForm.ClipboardRegFormat, clipboardBuffer);
clipboardBuffer.Position := 0;
alarmInfo := LoadAlarmInfoFromStream(clipboardBuffer);
// Assign clipboard data into current timer-panel.
SetAlarmInfo(alarmInfo, mainForm.MaxChannel);
// Update main window's change flags.
mainForm.SetChangeFlag(true);
finally
FreeAndNil(clipboardBuffer);
self.Invalidate;
end;
end;
end;
{$hints off}
procedure TfmTimer.updStartHourChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection);
var
txtEdit: TEdit;
component: TAlarmComponent;
begin
// Select either start-hour or end-hour text box.
if(TControl(Sender).Tag = 1) then
begin
txtEdit := txtEndHour;
component := alEndHour;
end
else
begin
txtEdit := txtStartHour;
component := alStartHour;
end;
// Apply new value into selected text box.
txtEdit.Text := IntToStr(NewValue);
// Perform validations for user inputs.
imgWarning.Visible := not IsValidConfiguration(component, NewValue);
end;
{$hints on}
{$hints off}
procedure TfmTimer.updStartMinuteChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection);
var
txtEdit: TEdit;
component: TAlarmComponent;
begin
// Select either start-hour or end-hour text box.
if(TControl(Sender).Tag = 1) then
begin
txtEdit := txtEndMinute;
component := alEndMinute;
end
else
begin
txtEdit := txtStartMinute;
component := alStartMinute;
end;
// Apply new value into selected text box.
txtEdit.Text := IntToStr(NewValue);
// Perform validations for user inputs.
imgWarning.Visible := not IsValidConfiguration(component, NewValue);
end;
{$hints on}
{$hints off}
procedure TfmTimer.updStartSecondsChangingEx(Sender: TObject;
var AllowChange: Boolean; NewValue: SmallInt; Direction: TUpDownDirection);
var
txtEdit: TEdit;
component: TAlarmComponent;
begin
// Select either start-seconds or end-seconds text box.
if(TControl(Sender).Tag = 1) then
begin
txtEdit := txtEndSeconds;
component := alEndSecond;
end
else
begin
txtEdit := txtStartSeconds;
component := alStartSecond;
end;
// Apply new value into selected text box.
txtEdit.Text := IntToStr(NewValue);
// Perform validations for user inputs.
imgWarning.Visible := not IsValidConfiguration(component, NewValue);
end;
{$hints on}
{$hints off}
procedure TfmTimer.updStartYearChanging(Sender: TObject;
var AllowChange: Boolean);
begin
if(Assigned(FOnValueChanged))then
begin
FOnValueChanged(self);
end;
end;
{$hints on}
procedure TfmTimer.SetAlarmInfo(info: TAlarmInfo; maxChannelCount: Byte);
var
tempChange : Boolean;
begin
// Assign start values into up-down counters.
updStartYear.Position := info.StartTime.Year;
cmbStartMonth.ItemIndex := info.StartTime.Month;
updStartDate.Position := info.StartTime.Date;
updStartHour.Position := info.StartTime.Hour;
updStartMinute.Position := info.StartTime.Minute;
updStartSeconds.Position := info.StartTime.Second;
// Assign end values into up-down counters.
updEndYear.Position := info.EndTime.Year;
cmbEndMonth.ItemIndex := info.EndTime.Month;
updEndDate.Position := info.EndTime.Date;
updEndHour.Position := info.EndTime.Hour;
updEndMinute.Position := info.EndTime.Minute;
updEndSeconds.Position := info.EndTime.Second;
tempChange := false;
// Update text fields with up-down counter values.
updStartYearChangingEx(updStartYear, tempChange, updStartYear.Position, TUpDownDirection.updNone);
updStartDateChangingEx(updStartDate, tempChange, updStartDate.Position, TUpDownDirection.updNone);
updStartHourChangingEx(updStartHour, tempChange, updStartHour.Position, TUpDownDirection.updNone);
updStartMinuteChangingEx(updStartMinute, tempChange, updStartMinute.Position, TUpDownDirection.updNone);
updStartSecondsChangingEx(updStartSeconds, tempChange, updStartSeconds.Position, TUpDownDirection.updNone);
updStartYearChangingEx(updEndYear, tempChange, updEndYear.Position, TUpDownDirection.updNone);
updStartDateChangingEx(updEndDate, tempChange, updEndDate.Position, TUpDownDirection.updNone);
updStartHourChangingEx(updEndHour, tempChange, updEndHour.Position, TUpDownDirection.updNone);
updStartMinuteChangingEx(updEndMinute, tempChange, updEndMinute.Position, TUpDownDirection.updNone);
updStartSecondsChangingEx(updEndSeconds, tempChange, updEndSeconds.Position, TUpDownDirection.updNone);
// Update channel information.
updChannel.Max := maxChannelCount;
if(info.Channel > updChannel.Max) then
begin
// Specified channel number is out-of range!
updChannel.Position := updChannel.Max;
channelReset := true;
end
else
begin
updChannel.Position := info.Channel;
end;
updChannelChangingEx(updChannel, tempChange, updChannel.Position, TUpDownDirection.updNone);
end;
function TfmTimer.GetAlarmInfo() : TAlarmInfo;
var
tmpInfo: TAlarmInfo;
tmpStartInfo, tmpEndInfo: TTimeInfo;
begin
// Extract channel details.
tmpInfo.Channel := updChannel.Position;
// Extract start date / time values.
tmpStartInfo.Year := updStartYear.Position;
tmpStartInfo.Month := cmbStartMonth.ItemIndex;
tmpStartInfo.Date := updStartDate.Position;
tmpStartInfo.Hour := updStartHour.Position;
tmpStartInfo.Minute := updStartMinute.Position;
tmpStartInfo.Second := updStartSeconds.Position;
tmpInfo.StartTime := tmpStartInfo;
// Extract end date / time values.
tmpEndInfo.Year := updEndYear.Position;
tmpEndInfo.Month := cmbEndMonth.ItemIndex;
tmpEndInfo.Date := updEndDate.Position;
tmpEndInfo.Hour := updEndHour.Position;
tmpEndInfo.Minute := updEndMinute.Position;
tmpEndInfo.Second := updEndSeconds.Position;
tmpInfo.EndTime := tmpEndInfo;
result := tmpInfo;
end;
function TfmTimer.IsSelected() : Boolean;
begin
result := chkSelect.Checked;
end;
function TfmTimer.IsChannelReset() : Boolean;
begin
result := channelReset;
end;
function TfmTimer.IsValidConfiguration(component: TAlarmComponent; val: Integer) : Boolean;
var
startYear, endYear, startMonth, endMonth, startDay, endDay : Integer;
startHour, endHour, startMinute, endMinute, startSeconds, endSeconds : Integer;
startDateComp, startTimeComp, endDateComp, endTimeComp : Int64;
function extractValue(comp: TAlarmComponent; componentVal: Integer) : Integer;
begin
if (component = comp) then
begin
result := val;
end
else
begin
result := componentVal;
end;
end;
begin
result := false;
// Extract current value or component value.
startYear := extractValue(alStartYear, updStartYear.Position);
endYear := extractValue(alEndYear, updEndYear.Position);
startMonth := extractValue(alStartMonth, cmbStartMonth.ItemIndex);
endMonth := extractValue(alEndMonth, cmbEndMonth.ItemIndex);
startDay := extractValue(alStartDay, updStartDate.Position);
endDay := extractValue(alEndDay, updEndDate.Position);
startHour := extractValue(alStartHour, updStartHour.Position);
endHour := extractValue(alEndHour, updEndHour.Position);
startMinute := extractValue(alStartMinute, updStartMinute.Position);
endMinute := extractValue(alEndMinute, updEndMinute.Position);
startSeconds := extractValue(alStartSecond, updStartSeconds.Position);
endSeconds := extractValue(alEndSecond, updEndSeconds.Position);
// Perform identical value check for date + time.
if((startYear = endYear) and (startMonth = endMonth) and (startDay = endDay) and
(startHour = endHour) and (startMinute = endMinute) and (startSeconds = endSeconds)) then
begin
exit;
end;
// Check either date + time or only a time mode is selected.
if((startYear < MIN_YEAR) or (endYear < MIN_YEAR) or (startMonth < MIN_MONTH) or
(endMonth < MIN_MONTH) or (startDay < MIN_DATE) or (endDay < MIN_DATE)) then
begin
if(not ((startYear < MIN_YEAR) and (startYear = endYear) and (startMonth < MIN_MONTH) and
(startMonth = endMonth) and (startDay < MIN_DATE) and (startDay = endDay))) then
begin
exit;
end;
end;
// Perform identical value check for time.
if((startYear <= MIN_YEAR) and (startMonth <= MIN_MONTH) and (startDay <= MIN_DATE) and
(startHour = endHour) and (startMinute = endMinute) and (startSeconds = endSeconds)) then
begin
exit;
end;
// In date + time mode : check start date/time is less than the end date/time.
if(startYear >= MIN_YEAR) then
begin
// Start date/time should less then end date/time.
startDateComp := StrToInt64(Format('%.*d', [4, startYear]) + Format('%.*d', [2, startMonth]) + Format('%.*d', [2, startDay]));
endDateComp := StrToInt64(Format('%.*d', [4, endYear]) + Format('%.*d', [2, endMonth]) + Format('%.*d', [2, endDay]));
startTimeComp := StrToInt64(Format('%.*d', [2, startHour]) + Format('%.*d', [2, startMinute]) + Format('%.*d', [2, startSeconds]));
endTimeComp := StrToInt64(Format('%.*d', [2, endHour]) + Format('%.*d', [2, endMinute]) + Format('%.*d', [2, endSeconds]));
// Validate date components.
if(startDateComp <= endDateComp) then
begin
// Date component is OK, checking time component.
if(startTimeComp > endTimeComp) then
begin
// Time component is invalid.
exit;
end;
end
else
begin
// Date component is invalid.
exit;
end;
end;
// Date/time validations are sucessful!
result := true;
end;
end.