Ignore:
Timestamp:
Apr 2, 2016, 1:06:34 PM (8 years ago)
Author:
chronos
Message:
  • Modified: All drive dimensions are now stored in project file.
  • Added: Confirmation dialog for saving project on application exit.
  • Modified: ComboBox for selection of current drive now behaves correctly to update project parameters.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Form/UFormMain.pas

    r12 r13  
    1414
    1515  TFormMain = class(TForm)
    16     ABrowseDisk: TAction;
     16    ADriveSelect: TAction;
    1717    AOperationOptions: TAction;
    1818    AFileClose: TAction;
     
    3434    ComboBoxDrive: TComboBox;
    3535    Image1: TImage;
     36    ImageList1: TImageList;
    3637    Label1: TLabel;
    3738    Label10: TLabel;
     
    4546    Label8: TLabel;
    4647    LabelElapsedTime: TLabel;
    47     LabelBlockSize: TLabel;
    48     LabelBlockCount: TLabel;
     48    LabelSectorSize: TLabel;
     49    LabelSectorCount: TLabel;
    4950    LabelBlockCurrent: TLabel;
    5051    LabelBlockDamaged: TLabel;
     
    7374    SaveDialog1: TSaveDialog;
    7475    Timer1: TTimer;
     76    ToolBar1: TToolBar;
     77    ToolButton1: TToolButton;
     78    ToolButton2: TToolButton;
     79    ToolButton3: TToolButton;
    7580    XMLConfig1: TXMLConfig;
    7681    procedure AExitExecute(Sender: TObject);
     
    8792    procedure ComboBoxDriveChange(Sender: TObject);
    8893    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
     94    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    8995    procedure FormCreate(Sender: TObject);
    9096    procedure FormDestroy(Sender: TObject);
     
    98104    RedrawPending: Boolean;
    99105    LastProjectFileName: string;
     106    LastDriveName: string;
    100107    procedure ReloadOperationList;
    101108    procedure ProjectOpen(FileName: string);
     
    107114  public
    108115    DriveList: TDriveList;
    109     DriveScan: TDriveScan;
    110116    Project: TProject;
    111117    procedure SaveConfig;
     
    126132
    127133resourcestring
    128   SProjectModified = '(modified)';
     134  SModifiedFlag = '(modified)';
    129135  SNewProject = 'New project';
    130136  SSelectDriveForScan = 'Select drive for scan';
     
    132138  SSaveProject = 'Save scan project';
    133139  SBytes = 'bytes';
     140  SProjectModified = 'Project modified';
     141  SProjectModifiedQuestion = 'Project was modified. Do you want to save it?';
    134142
    135143{ TFormMain }
     
    175183procedure TFormMain.AScanStopExecute(Sender: TObject);
    176184begin
    177   DriveScan.Stop;
    178   UpdateInterface;
     185  if Assigned(Project) and Assigned(Project.CurrentScan) then begin
     186    Project.CurrentScan.Stop;
     187    UpdateInterface;
     188  end;
    179189end;
    180190
    181191procedure TFormMain.ComboBoxDriveChange(Sender: TObject);
    182192begin
    183   DriveScan.SectorCount := TDriveInfo(DriveList[ComboBoxDrive.ItemIndex]).SectorCount;
    184   DriveScan.DriveName := TDriveInfo(DriveList[ComboBoxDrive.ItemIndex]).Path;
    185   DriveScan.SectorStart := 0;
    186   DriveScan.SectorEnd := DriveScan.SectorCount - 1;
     193  if Project.DriveInfo.Path <> TDriveInfo(ComboBoxDrive.Items.Objects[ComboBoxDrive.ItemIndex]).Path then begin
     194    Project.DriveInfo.Assign(TDriveInfo(ComboBoxDrive.Items.Objects[ComboBoxDrive.ItemIndex]));
     195    Project.Modified := True;
     196    Project.CurrentScan.SectorCount := Project.DriveInfo.SectorCount;
     197    Project.CurrentScan.SectorSize := Project.DriveInfo.SectorSize;
     198    Project.CurrentScan.SectorStart := 0;
     199    Project.CurrentScan.SectorEnd := Project.CurrentScan.SectorCount - 1;
     200    Project.CurrentScan.Reset;
     201    UpdateInterface;
     202    Redraw;
     203  end;
    187204end;
    188205
     
    190207begin
    191208  LastBlockPos := 0;
    192   DriveScan.DriveName := TDriveInfo(DriveList[ComboBoxDrive.ItemIndex]).Path;
    193   DriveScan.Reset;
    194   DriveScan.SectorSize := Project.SectorSize;
    195   DriveScan.OnChange := DriveScanChange;
    196   DriveScan.OnTerminate := DriveScanTerminate;
    197   DriveScan.Start;
     209  Project.CurrentScan.DriveName := Project.DriveInfo.Path;
     210  Project.CurrentScan.SectorSize := Project.DriveInfo.SectorSize;
     211  Project.CurrentScan.Reset;
     212  Project.CurrentScan.OnChange := DriveScanChange;
     213  Project.CurrentScan.OnTerminate := DriveScanTerminate;
     214  Project.CurrentScan.Start;
    198215  Project.Modified := True;
    199216  UpdateInterface;
     
    212229procedure TFormMain.AScanContinueExecute(Sender: TObject);
    213230begin
    214   DriveScan.Start;
     231  Project.CurrentScan.Start;
    215232  Project.Modified := True;
    216233  UpdateInterface;
     
    224241procedure TFormMain.AFileCloseExecute(Sender: TObject);
    225242begin
    226   DriveScan := nil;
    227243  FreeAndNil(Project);
    228244  UpdateInterface;
     
    236252  Project.Modified := False;
    237253  Project.Scans.Add(TDriveScan.Create);
    238   DriveScan := TDriveScan(Project.Scans[0]);
    239 
    240   DriveScan.SectorStart := 0;
    241   DriveScan.SectorEnd := DriveScan.SectorCount - 1;
    242   DriveScan.Reset;
     254
     255  ComboBoxDrive.ItemIndex := DriveList.IndexOf(DriveList.FindByModel(Project.DriveInfo.Model));
     256  if (ComboBoxDrive.ItemIndex = -1) and (ComboBoxDrive.Items.Count > 0) then ComboBoxDrive.ItemIndex := 0;
     257  ComboBoxDriveChange(Self);
     258
    243259  Redraw;
    244260  UpdateInterface;
     
    279295procedure TFormMain.AOperationOptionsExecute(Sender: TObject);
    280296begin
    281   FormOperation.Load(DriveScan);
     297  FormOperation.Load(Project.CurrentScan);
    282298  if FormOperation.ShowModal = mrOk then begin
    283     FormOperation.Save(DriveScan);
     299    FormOperation.Save(Project.CurrentScan);
    284300    Project.Modified := True;
    285301    UpdateInterface;
     
    291307  AFileClose.Execute;
    292308  SaveConfig;
     309end;
     310
     311procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
     312var
     313  R: TModalResult;
     314begin
     315  if Assigned(Project) and Project.Modified then begin
     316    R := MessageDlg(SProjectModified, SProjectModifiedQuestion, mtConfirmation, [mbYes, mbNo, mbCancel], 0);
     317    if R = mrCancel then begin
     318      CanClose := False;
     319      Exit;
     320    end else
     321    if R = mrYes then begin
     322      AFileSave.Execute;
     323    end;
     324  end;
    293325end;
    294326
     
    315347      Picture.Bitmap.SetSize(Width, Height);
    316348    try
    317       DriveScan.Lock.Acquire;
     349      Project.CurrentScan.Lock.Acquire;
    318350      try
    319351        Picture.Bitmap.BeginUpdate(True);
    320         DriveScan.BlockMap.Draw(Picture.Bitmap.Canvas);
     352        Project.CurrentScan.BlockMap.Draw(Picture.Bitmap.Canvas);
    321353      finally
    322354        Picture.Bitmap.EndUpdate;
    323355      end;
    324       LabelSize.Caption := PrefixMultiplier.Add(DriveScan.BlockMap.SectorCount * DriveScan.SectorSize,
    325         BasePrefixMultipliers, SBytes);
    326       LabelSectorPerBlock.Caption := IntToStr(DriveScan.BlockMap.SectorPerBlock);
    327       LabelBlockSize.Caption := IntToStr(DriveScan.SectorSize) + ' ' + SBytes;
    328       LabelBlockCount.Caption := IntToStr(DriveScan.BlockMap.SectorCount);
    329       LabelBlockCurrent.Caption := IntToStr(DriveScan.SectorCurrent);
    330       LabelBlockDamaged.Caption := IntToStr(DriveScan.DamagedBlockCount);
     356      LabelSectorPerBlock.Caption := IntToStr(Project.CurrentScan.BlockMap.SectorPerBlock);
     357      LabelBlockCurrent.Caption := IntToStr(Project.CurrentScan.SectorCurrent);
     358      LabelBlockDamaged.Caption := IntToStr(Project.CurrentScan.DamagedBlockCount);
    331359    finally
    332       DriveScan.Lock.Release;
     360      Project.CurrentScan.Lock.Release;
    333361    end;
    334362    RedrawPending := False;
    335363  end;
    336364  try
    337     DriveScan.Lock.Acquire;
    338     if not DriveScan.Terminated then begin
    339       LabelElapsedTime.Caption := TimeToStr(DriveScan.GetElapsedTime);
    340       LabelEstimatedTime.Caption := TimeToStr((Now - DriveScan.TimeStart) /
    341         DriveScan.SectorCurrent * (DriveScan.BlockMap.SectorCount - DriveScan.SectorCurrent));
    342       LabelIOSpeed.Caption := PrefixMultiplier.Add((DriveScan.SectorCurrent - LastBlockPos) *
    343         DriveScan.SectorSize / (Timer1.Interval / 1000), BasePrefixMultipliers, 'B/s');
    344       LastBlockPos := DriveScan.SectorCurrent;
     365    Project.CurrentScan.Lock.Acquire;
     366    if not Project.CurrentScan.Terminated then begin
     367      LabelElapsedTime.Caption := TimeToStr(Project.CurrentScan.GetElapsedTime);
     368      LabelEstimatedTime.Caption := TimeToStr((Now - Project.CurrentScan.TimeStart) /
     369        Project.CurrentScan.SectorCurrent * (Project.CurrentScan.BlockMap.SectorCount - Project.CurrentScan.SectorCurrent));
     370      LabelIOSpeed.Caption := PrefixMultiplier.Add((Project.CurrentScan.SectorCurrent - LastBlockPos) *
     371        Project.CurrentScan.SectorSize / (Timer1.Interval / 1000), BasePrefixMultipliers, 'B/s');
     372      LastBlockPos := Project.CurrentScan.SectorCurrent;
    345373    end else begin
    346374      LabelElapsedTime.Caption := '';
     
    349377    end;
    350378  finally
    351     DriveScan.Lock.Release;
     379    Project.CurrentScan.Lock.Release;
    352380  end;
    353381  end else
     
    367395  Title: string;
    368396begin
    369   if Assigned(DriveScan) then begin
    370     AScanStart.Enabled := DriveScan.Terminated = True;
    371     AScanStop.Enabled := DriveScan.Terminated = False;
    372     AScanContinue.Enabled := (DriveScan.Terminated = True) and (DriveScan.SectorCurrent > DriveScan.SectorStart) and
    373       (DriveScan.SectorCurrent < DriveScan.SectorEnd);
     397  if Assigned(Project) and Assigned(Project.CurrentScan) then begin
     398    AScanStart.Enabled := Project.CurrentScan.Terminated = True;
     399    AScanStop.Enabled := Project.CurrentScan.Terminated = False;
     400    AScanContinue.Enabled := (Project.CurrentScan.Terminated = True) and
     401      (Project.CurrentScan.SectorCurrent > Project.CurrentScan.SectorStart) and
     402      (Project.CurrentScan.SectorCurrent < Project.CurrentScan.SectorEnd);
     403  end else begin
     404    AScanStart.Enabled := False;
     405    AScanStop.Enabled := False;
     406    AScanContinue.Enabled := False;
    374407  end;
    375408  AFileSave.Enabled := Assigned(Project) and Project.Modified;
    376409  AFileSaveAs.Enabled := Assigned(Project);
    377410  AFileClose.Enabled := Assigned(Project);
     411  ComboBoxDrive.Enabled := Assigned(Project);
     412  AProjectOptions.Enabled := Assigned(Project);;
     413  AOperationOptions.Enabled := Assigned(Project);;
    378414  ReloadOperationList;
    379415
     
    383419  Title := Title + Application.Title;
    384420  if Assigned(Project) and Project.Modified then
    385     Title := Title + ' ' + SProjectModified;
     421    Title := Title + ' ' + SModifiedFlag;
    386422  Caption := Title;
     423
     424  if Assigned(Project) then begin
     425    LabelSize.Caption := PrefixMultiplier.Add(Project.DriveInfo.Size,
     426      BasePrefixMultipliers, SBytes);
     427    LabelSectorCount.Caption := IntToStr(Project.DriveInfo.SectorCount);
     428    LabelSectorSize.Caption := IntToStr(Project.DriveInfo.SectorSize) + ' ' + SBytes;
     429  end else begin
     430    LabelSize.Caption := '';
     431    LabelSectorCount.Caption := '';
     432    LabelSectorSize.Caption := '';
     433  end;
    387434end;
    388435
    389436procedure TFormMain.SaveConfig;
    390437begin
    391   XMLConfig1.SetValue('DriveName', TDriveInfo(DriveList[ComboBoxDrive.ItemIndex]).Model);
     438  XMLConfig1.SetValue('DriveName', LastDriveName);
    392439  XMLConfig1.SetValue('LastProjectFileName', LastProjectFileName);
    393440end;
     
    395442procedure TFormMain.LoadConfig;
    396443begin
    397   ComboBoxDrive.ItemIndex := DriveList.IndexOf(DriveList.FindByModel(XMLConfig1.GetValue('DriveName', '')));
     444  LastDriveName := XMLConfig1.GetValue('DriveName', '');
    398445  LastProjectFileName := XMLConfig1.GetValue('LastProjectFileName', '');
    399446end;
     
    418465  AFileNew.Execute;
    419466  Project.LoadFromFile(FileName);
     467  ComboBoxDrive.ItemIndex := DriveList.IndexOf(DriveList.FindByModel(Project.DriveInfo.Model));
     468  if (ComboBoxDrive.ItemIndex = -1) and (ComboBoxDrive.Items.Count > 0) then ComboBoxDrive.ItemIndex := 0;
    420469  LastProjectFileName := FileName;
    421470  UpdateInterface;
     
    438487begin
    439488  DriveList.Detect;
    440   while ComboBoxDrive.Items.Count > DriveList.Count do
    441     ComboBoxDrive.Items.Delete(ComboBoxDrive.Items.Count - 1);
    442   while ComboBoxDrive.Items.Count < DriveList.Count do
    443     ComboBoxDrive.Items.Add('');
    444   for I := 0 to DriveList.Count - 1 do
    445     ComboBoxDrive.Items[I] := TDriveInfo(DriveList[I]).Model + ' (' +
    446       PrefixMultiplier.Add(TDriveInfo(DriveList[I]).Size, BasePrefixMultipliers, 'B') + ')';
     489  DriveList.LoadToStrings(ComboBoxDrive.Items);
    447490end;
    448491
Note: See TracChangeset for help on using the changeset viewer.