Changeset 7 for trunk


Ignore:
Timestamp:
Mar 30, 2016, 11:32:54 PM (9 years ago)
Author:
chronos
Message:
  • Added: Main menu with available actions.
  • Added: Basic functionality to save project to file and open project from file.
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/UBlockMap.pas

    r6 r7  
    66
    77uses
    8   Classes, SysUtils, Graphics, Math, Contnrs, Dialogs;
     8  Classes, SysUtils, Graphics, Math, Contnrs, Dialogs, DOM, XMLRead, XMLWrite,
     9  UXMLUtils;
    910
    1011type
     
    4344    constructor Create;
    4445    destructor Destroy; override;
     46    procedure SaveToNode(Node: TDOMNode);
     47    procedure LoadFromNode(Node: TDOMNode);
    4548    property BlockSize: TPoint read FBlockSize write SetBlockSize;
    4649    property DrawSize: TPoint read FDrawSize write SetDrawSize;
     
    321324end;
    322325
     326procedure TBlockMap.SaveToNode(Node: TDOMNode);
     327var
     328  NewNode: TDOMNode;
     329  NewNode2: TDOMNode;
     330  I: Integer;
     331begin
     332  WriteInteger(Node, 'SectorCount', SectorCount);
     333
     334  NewNode := Node.OwnerDocument.CreateElement('Changes');
     335  Node.AppendChild(NewNode);
     336  for I := 0 to FChanges.Count - 1 do begin
     337    NewNode2 := NewNode.OwnerDocument.CreateElement('Change');
     338    NewNode.AppendChild(NewNode2);
     339    WriteInteger(NewNode2, 'Index', TChange(FChanges[I]).Index);
     340    WriteInteger(NewNode2, 'Value', Integer(TChange(FChanges[I]).Value));
     341  end;
     342end;
     343
     344procedure TBlockMap.LoadFromNode(Node: TDOMNode);
     345var
     346  NewNode: TDOMNode;
     347  NewNode2: TDOMNode;
     348  NewChange: TChange;
     349begin
     350  SectorCount := ReadInteger(Node, 'SectorCount', 0);
     351
     352  NewNode := Node.FindNode('Changes');
     353  if Assigned(NewNode) then begin
     354    FChanges.Count := 0;
     355    NewNode2 := NewNode.FirstChild;
     356    while Assigned(NewNode2) and (NewNode2.NodeName = 'Change') do begin
     357      NewChange := TChange.Create;
     358      NewChange.Index := ReadInteger(NewNode2, 'Index', 0);
     359      NewChange.Value := TSectorState(ReadInteger(NewNode2, 'Value', 0));
     360      FChanges.Add(NewChange);
     361      NewNode2 := NewNode2.NextSibling;
     362    end;
     363  end;
     364end;
    323365
    324366end.
  • trunk/UDriveScan.pas

    r6 r7  
    66
    77uses
    8   Classes, SysUtils, Syncobjs, UBlockMap, Forms, UConfig;
     8  Classes, SysUtils, Syncobjs, UBlockMap, Forms, UConfig, DOM, XMLRead, XMLWrite,
     9  UXMLUtils, Contnrs;
    910
    1011type
     
    4748    constructor Create;
    4849    destructor Destroy; override;
     50    procedure SaveToNode(Node: TDOMNode);
     51    procedure LoadFromNode(Node: TDOMNode);
    4952    property OnTerminate: TNotifyEvent read FOnTerminate write FOnTerminate;
    5053    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    5154  end;
    5255
     56  { TDriveScanList }
     57
     58  TDriveScanList = class(TObjectList)
     59    procedure SaveToNode(Node: TDOMNode);
     60    procedure LoadFromNode(Node: TDOMNode);
     61  end;
     62
    5363
    5464implementation
     
    5666resourcestring
    5767  SUnknownRunMode = 'Unknown run mode';
     68
     69{ TDriveScanList }
     70
     71procedure TDriveScanList.SaveToNode(Node: TDOMNode);
     72var
     73  I: Integer;
     74  NewNode2: TDOMNode;
     75begin
     76  for I := 0 to Count - 1 do
     77  with TDriveScan(Items[I]) do begin
     78    NewNode2 := Node.OwnerDocument.CreateElement('Scan');
     79    Node.AppendChild(NewNode2);
     80    SaveToNode(NewNode2);
     81  end;
     82end;
     83
     84procedure TDriveScanList.LoadFromNode(Node: TDOMNode);
     85var
     86  Node2: TDOMNode;
     87  NewScan: TDriveScan;
     88begin
     89  Count := 0;
     90  Node2 := Node.FirstChild;
     91  while Assigned(Node2) and (Node2.NodeName = 'Scan') do begin
     92    NewScan := TDriveScan.Create;
     93    NewScan.LoadFromNode(Node2);
     94    Add(NewScan);
     95    Node2 := Node2.NextSibling;
     96  end;
     97end;
    5898
    5999
     
    174214end;
    175215
     216procedure TDriveScan.SaveToNode(Node: TDOMNode);
     217var
     218  NewNode: TDOMNode;
     219begin
     220  with Node do begin
     221    WriteInteger(Node, 'DamagedBlockCount', DamagedBlockCount);
     222    WriteInteger(Node, 'WritePattern', WritePattern);
     223    WriteString(Node, 'DriveName', Drive);
     224
     225    NewNode := OwnerDocument.CreateElement('SectorMap');
     226    AppendChild(NewNode);
     227    BlockMap.SaveToNode(NewNode);
     228  end;
     229end;
     230
     231procedure TDriveScan.LoadFromNode(Node: TDOMNode);
     232var
     233  NewNode: TDOMNode;
     234begin
     235  with Node do begin
     236    DamagedBlockCount := ReadInteger(Node, 'DamagedBlockCount', 0);
     237    WritePattern := ReadInteger(Node, 'WritePattern', 0);
     238    Drive := ReadString(Node, 'DriveName', '');
     239
     240    NewNode := FindNode('SectorMap');
     241    if Assigned(NewNode) then
     242      BlockMap.LoadFromNode(NewNode);
     243  end;
     244end;
    176245
    177246end.
  • trunk/UFormMain.lfm

    r4 r7  
    11object FormMain: TFormMain
    2   Left = 301
     2  Left = 442
    33  Height = 941
    4   Top = 361
     4  Top = 366
    55  Width = 1703
    66  Caption = 'CoolDisk'
    7   ClientHeight = 939
     7  ClientHeight = 909
    88  ClientWidth = 1703
    99  Menu = MainMenu1
     
    3636  object Image1: TImage
    3737    Left = 592
    38     Height = 908
     38    Height = 878
    3939    Top = 10
    4040    Width = 1092
     
    248248    ParentColor = False
    249249  end
    250   object MainMenu1: TMainMenu
    251     left = 323
    252     top = 288
    253   end
    254250  object Timer1: TTimer
    255251    Interval = 500
     
    277273      OnExecute = ABrowseDiskExecute
    278274    end
     275    object AExit: TAction
     276      Caption = 'Exit'
     277      OnExecute = AExitExecute
     278    end
     279    object AFileSaveAs: TAction
     280      Caption = 'Save as...'
     281      OnExecute = AFileSaveAsExecute
     282    end
     283    object AFileOpen: TAction
     284      Caption = 'Open'
     285      OnExecute = AFileOpenExecute
     286    end
    279287  end
    280288  object OpenDialog1: TOpenDialog
     
    282290    top = 360
    283291  end
     292  object SaveDialog1: TSaveDialog
     293    left = 672
     294    top = 432
     295  end
     296  object MainMenu1: TMainMenu
     297    left = 672
     298    top = 504
     299    object MenuItem1: TMenuItem
     300      Caption = 'File'
     301      object MenuItem2: TMenuItem
     302        Action = AFileOpen
     303      end
     304      object MenuItem3: TMenuItem
     305        Action = AFileSaveAs
     306      end
     307      object MenuItem4: TMenuItem
     308        Action = AExit
     309      end
     310    end
     311    object MenuItem5: TMenuItem
     312      Caption = 'Scan'
     313      object MenuItem6: TMenuItem
     314        Action = AScanStart
     315      end
     316      object MenuItem7: TMenuItem
     317        Action = AScanStop
     318      end
     319      object MenuItem8: TMenuItem
     320        Action = AScanOptions
     321      end
     322    end
     323  end
    284324end
  • trunk/UFormMain.pas

    r6 r7  
    1515  TFormMain = class(TForm)
    1616    ABrowseDisk: TAction;
     17    AFileOpen: TAction;
     18    AFileSaveAs: TAction;
     19    AExit: TAction;
    1720    AScanOptions: TAction;
    1821    AScanStart: TAction;
     
    4851    ListView1: TListView;
    4952    MainMenu1: TMainMenu;
     53    MenuItem1: TMenuItem;
     54    MenuItem2: TMenuItem;
     55    MenuItem3: TMenuItem;
     56    MenuItem4: TMenuItem;
     57    MenuItem5: TMenuItem;
     58    MenuItem6: TMenuItem;
     59    MenuItem7: TMenuItem;
     60    MenuItem8: TMenuItem;
    5061    OpenDialog1: TOpenDialog;
     62    SaveDialog1: TSaveDialog;
    5163    Timer1: TTimer;
    5264    procedure ABrowseDiskExecute(Sender: TObject);
     65    procedure AExitExecute(Sender: TObject);
     66    procedure AFileOpenExecute(Sender: TObject);
     67    procedure AFileSaveAsExecute(Sender: TObject);
    5368    procedure AScanOptionsExecute(Sender: TObject);
    5469    procedure AScanStartExecute(Sender: TObject);
     
    102117begin
    103118  PrefixMultiplier := TPrefixMultiplier.Create;
    104   DriveScan := TDriveScan.Create;
     119  Project := TProject.Create;
     120  Project.Scans.Add(TDriveScan.Create);
     121  DriveScan := TDriveScan(Project.Scans[0]);
    105122  DriveScan.OnChange := DriveScanChange;
    106123  DriveScan.OnTerminate := DriveScanTerminate;
    107   Project := TProject.Create;
    108124  EditDrive.Text := '/dev/sdb';
    109125end;
     
    137153begin
    138154  OpenDialog1.Title := 'Select drive for scan';
     155  OpenDialog1.DefaultExt := '';
    139156  OpenDialog1.FileName := EditDrive.Text;
    140157  if OpenDialog1.Execute then
     
    142159end;
    143160
     161procedure TFormMain.AExitExecute(Sender: TObject);
     162begin
     163  Close;
     164end;
     165
     166procedure TFormMain.AFileOpenExecute(Sender: TObject);
     167begin
     168  OpenDialog1.Title := 'Open stored scan project';
     169  OpenDialog1.DefaultExt := '.cdp';
     170  if OpenDialog1.Execute then begin
     171    Project.LoadFromFile(OpenDialog1.FileName);
     172  end;
     173end;
     174
     175procedure TFormMain.AFileSaveAsExecute(Sender: TObject);
     176begin
     177  SaveDialog1.Title := 'Save scan project';
     178  SaveDialog1.DefaultExt := '.cdp';
     179  if SaveDialog1.Execute then begin
     180    Project.SaveToFile(SaveDialog1.FileName);
     181  end;
     182end;
     183
    144184procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
    145185begin
     
    150190  Timer1.Enabled := False;
    151191  FreeAndNil(Project);
    152   FreeAndNil(DriveScan);
    153192  FreeAndNil(PrefixMultiplier);
    154193end;
  • trunk/UProject.pas

    r2 r7  
    66
    77uses
    8   Classes, SysUtils;
     8  Classes, SysUtils, Contnrs, DOM, XMLRead, XMLWrite, UXMLUtils, UDriveScan;
    99
    1010type
     
    2020    SectorCount: Integer;
    2121    Modified: Boolean;
     22    Scans: TDriveScanList;
    2223    constructor Create;
     24    destructor Destroy; override;
     25    procedure LoadFromFile(FileName: string);
     26    procedure SaveToFile(FileName: string);
    2327  end;
    2428
     29
    2530implementation
     31
     32resourcestring
     33  SWrongFileFormat = 'Wrong file format';
     34
    2635
    2736{ TProject }
     
    3039begin
    3140  SectorSize := 4096;
     41  Scans := TDriveScanList.Create;
    3242end;
     43
     44destructor TProject.Destroy;
     45begin
     46  FreeAndNil(Scans);
     47  inherited Destroy;
     48end;
     49
     50procedure TProject.SaveToFile(FileName: string);
     51var
     52  NewNode: TDOMNode;
     53  Doc: TXMLDocument;
     54  RootNode: TDOMNode;
     55begin
     56  Self.FileName := FileName;
     57  Doc := TXMLDocument.Create;
     58  with Doc do try
     59    RootNode := CreateElement('CoolDiskProject');
     60    AppendChild(RootNode);
     61    with RootNode do begin
     62      WriteInteger(RootNode, 'SectorSize', SectorSize);
     63
     64      NewNode := OwnerDocument.CreateElement('Scans');
     65      AppendChild(NewNode);
     66      Scans.SaveToNode(NewNode);
     67    end;
     68    ForceDirectories(ExtractFileDir(FileName));
     69    WriteXMLFile(Doc, FileName);
     70  finally
     71    Doc.Free;
     72  end;
     73end;
     74
     75procedure TProject.LoadFromFile(FileName: string);
     76var
     77  Doc: TXMLDocument;
     78  RootNode: TDOMNode;
     79  NewNode: TDOMNode;
     80begin
     81  Self.FileName := FileName;
     82  ReadXMLFile(Doc, FileName);
     83  with Doc do try
     84    if Doc.DocumentElement.NodeName <> 'CoolDiskProject' then
     85      raise Exception.Create(SWrongFileFormat);
     86    RootNode := Doc.DocumentElement;
     87    with RootNode do begin
     88      SectorSize := ReadInteger(RootNode, 'SectorSize', 4096);
     89
     90      NewNode := FindNode('Scans');
     91      if Assigned(NewNode) then
     92        Scans.LoadFromNode(NewNode);
     93    end;
     94  finally
     95    Doc.Free;
     96  end;
     97end;
     98
    3399
    34100end.
Note: See TracChangeset for help on using the changeset viewer.