Statistiques
| Révision:

root / UFObservProfilP.pas @ 3

Historique | Voir | Annoter | Télécharger (11,259 ko)

1
unit UFObservProfilP;
2

    
3
interface
4

    
5
uses
6
  Windows, Forms, Classes, Controls, Messages, Dialogs, StdCtrls, Buttons,
7
  DB, Grids, DBGrids, JvEnterTab, JvExControls, JvCsvData;
8

    
9
type
10
  TFObservProfilP = class(TForm)
11
    BBOk: TBitBtn;
12
    BBCancel: TBitBtn;
13
    DataSourceObservProfilP: TDataSource;
14
    DBGridObserv: TDBGrid;
15
    SBAddLine: TSpeedButton;
16
    SBDelLine: TSpeedButton;
17
    SBImport: TSpeedButton;
18
    JvEnterAsTab1: TJvEnterAsTab;
19
    MErrors: TMemo;
20
    JvCsvDataSetImport: TJvCsvDataSet;
21
    OpenDialogImport: TOpenDialog;
22
    procedure FormShow(Sender: TObject);
23
    procedure FormCreate(Sender: TObject);
24
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
25
    procedure SBAddLineClick(Sender: TObject);
26
    procedure SBDelLineClick(Sender: TObject);
27
    procedure SBImportClick(Sender: TObject);
28
    procedure DBGridObservKeyDown(Sender: TObject; var Key: Word;
29
      Shift: TShiftState);
30
    procedure FormDestroy(Sender: TObject);
31
  private
32
    { D?clarations priv?es }
33
    DBGridOldWndProc: TWndMethod;
34
    Procedure DBGridNewWndProc(var Mess: TMessage);
35
  public
36
    { D?clarations publiques }
37
  end;
38

    
39
var
40
  FObservProfilP: TFObservProfilP;
41

    
42
implementation
43

    
44
uses
45
  SysUtils, gnugettext, UVariables, UFProfilP;
46

    
47
{$R *.dfm}
48

    
49
{ TFObservProfilP }
50

    
51
procedure TFObservProfilP.DBGridNewWndProc(var Mess: TMessage);
52
begin
53
  if Mess.Msg = WM_MOUSEWHEEL
54
  then // Interception de l'?v?nement WM_MOUSEWHEEL
55
    with FProfilP.ASQLite3TableObservProfilP do
56
      if SmallInt(Mess.WParamHi) < 0
57
      then
58
        Next
59
      else
60
        Prior
61
  else // Traitement des autres messages
62
    DBGridOldWndProc(Mess);
63
end;
64

    
65
procedure TFObservProfilP.DBGridObservKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
66
begin
67
  if DBGridObserv.DataSource.DataSet.CanModify
68
  then
69
    case Key of
70
      VK_DOWN:
71
        with FProfilP.ASQLite3TableObservProfilP do
72
          if FieldByName('Line').AsInteger = RecordCount
73
          then
74
            Key := 0;
75
      VK_TAB:
76
        with FProfilP.ASQLite3TableObservProfilP do
77
          if (FieldByName('Line').AsInteger = RecordCount)
78
          and (DBGridObserv.SelectedField = FieldByName('Lean'))
79
          then
80
            Key := 0;
81
      VK_INSERT:
82
        Key := 0;
83
      VK_DELETE:
84
        if Shift = [ssCtrl]
85
        then
86
          Key := 0;
87
    end;
88
end;
89

    
90
procedure TFObservProfilP.FormClose(Sender: TObject; var Action: TCloseAction);
91
var
92
  AgePrec: Integer;
93
  FeedPrec: Double;
94
  FeedCount, WeightCount: Integer;
95
begin
96
  if ModalResult = mrOk
97
  then // V?rifier la coh?rence des donn?es
98
    with FProfilP.ASQLite3TableObservProfilP do
99
    begin
100
      MErrors.Lines.Clear;
101
      First;
102
      if (FieldByName('Age').AsInteger <= FProfilP.PBAgeInit.MinValue) or (FieldByName('Age').AsInteger >= FProfilP.PBAgeInit.MaxValue)
103
      then
104
        MErrors.Lines.Add(Format(_('Line %d: '), [FieldByName('Line').AsInteger])
105
          + Format(_('The initial age should be between %d and %d days.'), [FProfilP.PBAgeInit.MinValue, FProfilP.PBAgeInit.MaxValue]));
106
      if FieldByName('Feed').AsFloat <> 0
107
      then
108
        MErrors.Lines.Add(Format(_('Line %d: '), [FieldByName('Line').AsInteger])
109
          + _('The initial cumulative feed usage should be equal to zero.'));
110
      if (FieldByName('Weight').AsFloat <= FProfilP.PBPVInit.MinValue) or (FieldByName('Weight').AsFloat >= FProfilP.PBPVInit.MaxValue)
111
      then
112
        MErrors.Lines.Add(Format(_('Line %d: '), [FieldByName('Line').AsInteger])
113
          + Format(_('The initial body weight should be between %1.1f and %1.1f kg.'), [FProfilP.PBPVInit.MinValue, FProfilP.PBPVInit.MaxValue]));
114
      FeedCount := 1;
115
      WeightCount :=1;
116
      AgePrec := FieldByName('Age').AsInteger;
117
      FeedPrec := FieldByName('Feed').AsFloat;
118
      Next;
119
      while not Eof do
120
      begin
121
        if FieldByName('Age').IsNull
122
        then
123
          MErrors.Lines.Add(Format(_('Line %d: '), [FieldByName('Line').AsInteger])
124
            + ('The age should be given for each line.'))
125
        else
126
        begin
127
          if FieldByName('Age').AsInteger <= AgePrec
128
          then
129
            MErrors.Lines.Add(Format(_('Line %d: '), [FieldByName('Line').AsInteger])
130
              + _('The age should be given in ascending order.'));
131
          AgePrec := FieldByName('Age').AsInteger;
132
        end;
133
        if not FieldByName('Feed').IsNull
134
        then
135
        begin
136
          if FieldByName('Feed').AsFloat < FeedPrec
137
          then
138
            MErrors.Lines.Add(Format(_('Line %d: '), [FieldByName('Line').AsInteger])
139
              + _('The cumulative feed usage cannot decrease.'));
140
          Inc(FeedCount);
141
          FeedPrec := FieldByName('Feed').AsFloat;
142
        end;
143
        if not FieldByName('Weight').IsNull
144
        then
145
          Inc(WeightCount);
146
        Next;
147
      end;
148
      if FeedCount < 3
149
      then
150
        MErrors.Lines.Add(_('At least three measurements of cumulative feed usage should be given.'));
151
      if WeightCount < 3
152
      then
153
        MErrors.Lines.Add(_('At least three measurements of body weight should be given.'));
154
      if MErrors.Lines.Count > 0
155
      then
156
        Action := caNone;
157
    end;
158
end;
159

    
160
procedure TFObservProfilP.FormCreate(Sender: TObject);
161
begin
162
  if Screen.Fonts.IndexOf('Arial Unicode MS') <> -1
163
  then
164
    Font.Name := 'Arial Unicode MS';
165
  TranslateComponent(Self);
166
  DBGridOldWndProc := DBGridObserv.WindowProc;
167
  DBGridObserv.WindowProc := DBGridNewWndProc;
168
end;
169

    
170
procedure TFObservProfilP.FormDestroy(Sender: TObject);
171
begin
172
  DBGridObserv.WindowProc := DBGridOldWndProc;
173
end;
174

    
175
procedure TFObservProfilP.FormShow(Sender : TObject);
176
begin
177
  with FProfilP.ASQLite3TableObservProfilP do
178
    if IsEmpty
179
    then // Initialisation avec ?tat initial et ?tat final
180
    begin
181
      Append;
182
      FieldByName('Profile').AsInteger := PProfilP.Num;
183
      FieldByName('Line').AsInteger := 1;
184
      FieldByName('Age').AsInteger := PProfilP.AgeInit;
185
      FieldByName('Feed').AsFloat := 0;
186
      FieldByName('Weight').AsFloat := PProfilP.PVInit;
187
      Post;
188
      Append;
189
      FieldByName('Profile').AsInteger := PProfilP.Num;
190
      FieldByName('Line').AsInteger := 3;
191
      if PProfilP.ModeFin = 0
192
      then // Dur?e
193
        FieldByName('Age').AsInteger := PProfilP.AgeInit + PProfilP.Duree
194
      else // poids vif
195
        FieldByName('Weight').AsFloat := PProfilP.PVFin;
196
      Post;
197
      Insert;
198
      FieldByName('Profile').AsInteger := PProfilP.Num;
199
      FieldByName('Line').AsInteger := 2;
200
      Post;
201
    end;
202
end;
203

    
204
procedure TFObservProfilP.SBAddLineClick(Sender: TObject);
205
var
206
  i: integer;
207
begin
208
  with FProfilP.ASQLite3TableObservProfilP do
209
    if FieldByName('Line').AsInteger > 1
210
    then
211
    begin
212
      i := FieldByName('Line').AsInteger;
213
      DisableControls;
214
      try
215
        Last;
216
        repeat
217
          Edit;
218
          FieldByName('Line').AsInteger := FieldByName('Line').AsInteger + 1;
219
          Post;
220
          Prior;
221
        until FieldByName('Line').AsInteger < i;
222
        Next;
223
        Insert;
224
        FieldByName('Profile').AsInteger := PProfilP.Num;
225
        FieldByName('Line').AsInteger := i;
226
        Post;
227
      finally
228
        EnableControls;
229
        ActiveControl := DBGridObserv;
230
        DBGridObserv.SelectedField := FieldByName('Age');
231
      end;
232
    end
233
    else
234
      Beep;
235
end;
236

    
237
procedure TFObservProfilP.SBDelLineClick(Sender: TObject);
238
var
239
  i: integer;
240
begin
241
  with FProfilP.ASQLite3TableObservProfilP do
242
    if (FieldByName('Line').AsInteger > 1) and (FieldByName('Line').AsInteger < RecordCount)
243
    then
244
    begin
245
      i := FieldByName('Line').AsInteger;
246
      DisableControls;
247
      try
248
        Last;
249
        while FieldByName('Line').AsInteger > i do
250
        begin
251
          Edit;
252
          FieldByName('Line').AsInteger := FieldByName('Line').AsInteger - 1;
253
          Post;
254
          Prior;
255
        end;
256
        Delete;
257
      finally
258
        EnableControls;
259
        ActiveControl := DBGridObserv;
260
        DBGridObserv.SelectedField := FieldByName('Age');
261
      end;
262
    end
263
    else
264
      Beep;
265
end;
266

    
267
procedure TFObservProfilP.SBImportClick(Sender: TObject);
268
var
269
  CurrentFormatSettings: TFormatSettings;
270
begin
271
  OpenDialogImport.InitialDir := GetCurrentDir;
272
  if not OpenDialogImport.Execute
273
  then // Annulation
274
    Exit;
275
  GetLocaleFormatSettings(GetUserDefaultLCID, CurrentFormatSettings);
276
  JvCsvDataSetImport.Separator := CurrentFormatSettings.ListSeparator;
277
  JvCsvDataSetImport.FileName := OpenDialogImport.FileName;
278
  try
279
    JvCsvDataSetImport.Active := True;
280
    try
281
      if JvCsvDataSetImport.FieldCount < 3
282
      then
283
      begin
284
        MessageDlg(_('File must contain at least 3 columns'), mtError, [mbOK], 0);
285
        Exit;
286
      end;
287
      // Suppression des lignes vides
288
      JvCsvDataSetImport.First;
289
      while not JvCsvDataSetImport.Eof do
290
      begin
291
        if JvCsvDataSetImport.Fields[0].IsNull
292
        and JvCsvDataSetImport.Fields[1].IsNull
293
        and JvCsvDataSetImport.Fields[2].IsNull
294
        and ((JvCsvDataSetImport.FieldCount < 4) or JvCsvDataSetImport.Fields[3].IsNull)
295
        and ((JvCsvDataSetImport.FieldCount < 4) or JvCsvDataSetImport.Fields[4].IsNull)
296
        then
297
          JvCsvDataSetImport.Delete
298
        else
299
          JvCsvDataSetImport.Next;
300
      end;
301
      if JvCsvDataSetImport.RecordCount < 2
302
      then
303
      begin
304
        MessageDlg(_('File must contain at least 2 lines'), mtError, [mbOK], 0);
305
        Exit;
306
      end;
307
      with FProfilP.ASQLite3TableObservProfilP do
308
      begin
309
        DisableControls;
310
        try
311
          First;
312
          while not Eof do
313
            Delete;
314
          JvCsvDataSetImport.First;
315
          while not JvCsvDataSetImport.Eof do
316
          begin
317
            Append;
318
            FieldByName('Profile').AsInteger := PProfilP.Num;
319
            FieldByName('Line').AsInteger := JvCsvDataSetImport.RecNo + 1;
320
            if not JvCsvDataSetImport.Fields[0].IsNull
321
            then
322
              FieldByName('Age').AsInteger := JvCsvDataSetImport.Fields[0].AsInteger;
323
            if not JvCsvDataSetImport.Fields[1].IsNull
324
            then
325
              FieldByName('Feed').AsFloat := JvCsvDataSetImport.Fields[1].AsFloat;
326
            if not JvCsvDataSetImport.Fields[2].IsNull
327
            then
328
              FieldByName('Weight').AsFloat := JvCsvDataSetImport.Fields[2].AsFloat;
329
            if (JvCsvDataSetImport.FieldCount > 3) and not JvCsvDataSetImport.Fields[3].IsNull
330
            then
331
              FieldByName('Backfat').AsFloat := JvCsvDataSetImport.Fields[3].AsFloat;
332
            if (JvCsvDataSetImport.FieldCount > 4) and not JvCsvDataSetImport.Fields[4].IsNull
333
            then
334
              FieldByName('Lean').AsFloat := JvCsvDataSetImport.Fields[4].AsFloat;
335
            Post;
336
            JvCsvDataSetImport.Next;
337
          end;
338
        finally
339
          EnableControls;
340
          ActiveControl := DBGridObserv;
341
          DBGridObserv.SelectedField := FieldByName('Age');
342
        end;
343
      end;
344
    finally
345
      JvCsvDataSetImport.Active := False;
346
    end;
347
  except
348
    MessageDlg(_('Invalid file format'), mtError, [mbOK], 0);
349
//    HtmlHelp(Handle, PChar(Application.CurrentHelpFile), HH_HELP_CONTEXT, 5350);
350
    Application.HelpContext(5350);
351
  end;
352
end;
353

    
354
end.