[46] | 1 | unit Template;
|
---|
| 2 |
|
---|
| 3 | interface
|
---|
| 4 |
|
---|
| 5 | uses
|
---|
| 6 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
---|
| 7 | Dialogs, ExtCtrls, StdCtrls, StrUtils,
|
---|
| 8 | OniDataClass, Functions, Data, Menus, Buttons;
|
---|
| 9 |
|
---|
| 10 | type
|
---|
| 11 | TNewFileSelectedEvent = procedure(fileinfo: TFileInfo) of object;
|
---|
| 12 |
|
---|
| 13 | TForm_ToolTemplate = class(TForm)
|
---|
| 14 | panel_files: TPanel;
|
---|
| 15 | filelist: TListBox;
|
---|
| 16 | panel_extension: TPanel;
|
---|
| 17 | label_ext: TLabel;
|
---|
| 18 | combo_extension: TComboBox;
|
---|
| 19 | check_zerobyte: TCheckBox;
|
---|
| 20 | edit_filtername: TEdit;
|
---|
| 21 | check_filtername: TCheckBox;
|
---|
| 22 | Splitter1: TSplitter;
|
---|
| 23 | content: TPanel;
|
---|
| 24 | filepopup: TPopupMenu;
|
---|
| 25 | popup_import: TMenuItem;
|
---|
| 26 | popup_export: TMenuItem;
|
---|
| 27 | popup_separator: TMenuItem;
|
---|
| 28 | importd: TOpenDialog;
|
---|
| 29 | exportd: TSaveDialog;
|
---|
| 30 | btn_sort_id_asc: TSpeedButton;
|
---|
| 31 | btn_sort_id_desc: TSpeedButton;
|
---|
| 32 | btn_sort_name_asc: TSpeedButton;
|
---|
| 33 | btn_sort_name_desc: TSpeedButton;
|
---|
| 34 | btn_sort_ext_asc: TSpeedButton;
|
---|
| 35 | btn_sort_ext_desc: TSpeedButton;
|
---|
| 36 | procedure RecreateList;
|
---|
| 37 | procedure LoadFileNames;
|
---|
| 38 | procedure SelectFileName(filename: String);
|
---|
| 39 | procedure SelectFileID(id: Integer);
|
---|
| 40 | procedure check_filternameClick(Sender: TObject);
|
---|
| 41 | procedure check_zerobyteClick(Sender: TObject);
|
---|
| 42 | procedure combo_extensionClick(Sender: TObject);
|
---|
| 43 | procedure listClick(Sender: TObject);
|
---|
| 44 | procedure listMouseDown(Sender: TObject; Button: TMouseButton;
|
---|
| 45 | Shift: TShiftState; X, Y: Integer);
|
---|
| 46 |
|
---|
| 47 | procedure FormResize(Sender: TObject);
|
---|
| 48 | procedure FormCreate(Sender: TObject);
|
---|
| 49 | procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
| 50 | procedure popup_importClick(Sender: TObject);
|
---|
| 51 | procedure popup_exportClick(Sender: TObject);
|
---|
| 52 | procedure popup_opentool(Sender: TObject);
|
---|
| 53 | procedure filepopupPopup(Sender: TObject);
|
---|
| 54 | procedure btn_sortClick(Sender: TObject);
|
---|
| 55 | private
|
---|
| 56 | FSortBy: TSortType;
|
---|
| 57 | FOnNewFileSelected: TNewFileSelectedEvent;
|
---|
| 58 | FAllowedExts: String;
|
---|
| 59 | FAllowMultiSelect: Boolean;
|
---|
| 60 | procedure SetAllowedExts(exts: String);
|
---|
| 61 | procedure SetMultiSelect(allow: Boolean);
|
---|
| 62 | public
|
---|
| 63 | constructor Create(AOwner: TComponent); override;
|
---|
| 64 | procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean);
|
---|
| 65 | published
|
---|
| 66 | property OnNewFileSelected: TNewFileSelectedEvent read FOnNewFileSelected write FOnNewFileSelected;
|
---|
| 67 | property AllowedExts: String read FAllowedExts write SetAllowedExts;
|
---|
| 68 | property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect;
|
---|
| 69 | end;
|
---|
| 70 |
|
---|
| 71 | var
|
---|
| 72 | ToolList: TToolList;
|
---|
| 73 | procedure AddToolListEntry(context, name, exts: String);
|
---|
| 74 |
|
---|
| 75 | implementation
|
---|
| 76 | {$R *.dfm}
|
---|
| 77 | uses Main, Exporters;
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | procedure TForm_ToolTemplate.RecreateList;
|
---|
| 81 | var
|
---|
| 82 | i: LongWord;
|
---|
| 83 | exts: TStringArray;
|
---|
| 84 | begin
|
---|
| 85 | combo_extension.Items.Clear;
|
---|
| 86 | combo_extension.Items.Add('_All files_ (' +
|
---|
| 87 | IntToStr(OniDataConnection.GetFilesCount) + ')');
|
---|
| 88 | exts := OniDataConnection.GetExtensionsList;
|
---|
| 89 | for i := 0 to High(exts) do
|
---|
| 90 | if Length(FAllowedExts) > 0 then
|
---|
| 91 | begin
|
---|
| 92 | if Pos(MidStr(exts[i],1,4), FAllowedExts) > 0 then
|
---|
| 93 | begin
|
---|
| 94 | combo_extension.Items.Add(exts[i]);
|
---|
| 95 | end;
|
---|
| 96 | end else
|
---|
| 97 | combo_extension.Items.Add(exts[i]);
|
---|
| 98 | combo_extension.ItemIndex := 0;
|
---|
| 99 | combo_extensionClick(Self);
|
---|
| 100 | end;
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 | procedure TForm_ToolTemplate.LoadFileNames;
|
---|
| 106 | var
|
---|
| 107 | Extension: String;
|
---|
| 108 | no_zero_bytes: Boolean;
|
---|
| 109 | pattern: String;
|
---|
| 110 | files: TStringArray;
|
---|
| 111 | i: LongWord;
|
---|
| 112 | begin
|
---|
| 113 | Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4);
|
---|
| 114 | no_zero_bytes := not check_zerobyte.Checked;
|
---|
| 115 | pattern := '';
|
---|
| 116 | if check_filtername.Checked then
|
---|
| 117 | pattern := edit_filtername.Text;
|
---|
| 118 | if Extension = '_All' then
|
---|
| 119 | if Length(FAllowedExts) > 0 then
|
---|
| 120 | Extension := FAllowedExts
|
---|
| 121 | else
|
---|
| 122 | Extension := '';
|
---|
| 123 |
|
---|
| 124 | files := OniDataConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
|
---|
| 125 |
|
---|
| 126 | filelist.Visible := False;
|
---|
| 127 | filelist.Items.Clear;
|
---|
| 128 | if Length(files) > 0 then
|
---|
| 129 | for i := 0 to High(files) do
|
---|
| 130 | filelist.Items.Add(files[i]);
|
---|
| 131 | filelist.Visible := True;
|
---|
| 132 | end;
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | procedure TForm_ToolTemplate.popup_exportClick(Sender: TObject);
|
---|
| 136 | var
|
---|
| 137 | id: Integer;
|
---|
| 138 | ext: String;
|
---|
| 139 | begin
|
---|
| 140 | id := OniDataConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
|
---|
| 141 | ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
|
---|
| 142 | exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*';
|
---|
| 143 | exportd.DefaultExt := ext;
|
---|
| 144 | if exportd.Execute then
|
---|
| 145 | ExportDatFile(id, exportd.FileName);
|
---|
| 146 | end;
|
---|
| 147 |
|
---|
| 148 | procedure TForm_ToolTemplate.popup_importClick(Sender: TObject);
|
---|
| 149 | var
|
---|
| 150 | id: Integer;
|
---|
| 151 | finfo: TFileInfo;
|
---|
| 152 | fs: TFileStream;
|
---|
| 153 | data: TData;
|
---|
| 154 | begin
|
---|
| 155 | id := OniDataConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
|
---|
| 156 | finfo := OniDataConnection.GetFileInfo(id);
|
---|
| 157 |
|
---|
| 158 | importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' +
|
---|
| 159 | finfo.Extension + '|All files|*.*';
|
---|
| 160 | if importd.Execute then
|
---|
| 161 | begin
|
---|
| 162 | fs := TFileStream.Create(importd.FileName, fmOpenRead);
|
---|
| 163 | if fs.Size <> finfo.Size then
|
---|
| 164 | ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) +
|
---|
| 165 | ', file has to have same size as file in .dat.' + CrLf +
|
---|
| 166 | 'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
|
---|
| 167 | 'Size of chosen file: ' + FormatFileSize(fs.Size))
|
---|
| 168 | else begin
|
---|
| 169 | SetLength(data, fs.Size);
|
---|
| 170 | fs.Read(data[0], fs.Size);
|
---|
| 171 | OniDataConnection.UpdateDatFile(id, data);
|
---|
| 172 | Self.listClick(Self);
|
---|
| 173 | end;
|
---|
| 174 | fs.Free;
|
---|
| 175 | end;
|
---|
| 176 | end;
|
---|
| 177 |
|
---|
| 178 | procedure TForm_ToolTemplate.popup_opentool(Sender: TObject);
|
---|
| 179 | var
|
---|
| 180 | sender_name, context: String;
|
---|
| 181 | id: Integer;
|
---|
| 182 | begin
|
---|
| 183 | sender_name := TComponent(Sender).Name;
|
---|
| 184 | id := OniDataConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
|
---|
| 185 | context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name));
|
---|
| 186 | Form_Main.open_child(context, id);
|
---|
| 187 | end;
|
---|
| 188 |
|
---|
| 189 | procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject);
|
---|
| 190 | begin
|
---|
| 191 | LoadFileNames;
|
---|
| 192 | end;
|
---|
| 193 |
|
---|
| 194 |
|
---|
| 195 | constructor TForm_ToolTemplate.Create(AOwner: TComponent);
|
---|
| 196 | var
|
---|
| 197 | i: Integer;
|
---|
| 198 | item: TMenuItem;
|
---|
| 199 | begin
|
---|
| 200 | inherited;
|
---|
| 201 | RecreateList;
|
---|
| 202 | if Length(ToolList) > 0 then
|
---|
| 203 | begin
|
---|
| 204 | for i := 0 to High(ToolList) do
|
---|
| 205 | begin
|
---|
| 206 | item := TMenuItem.Create(filepopup);
|
---|
| 207 | item.Name := 'popup_' + ToolList[i].context;
|
---|
| 208 | item.Caption := 'Open with ' + ToolList[i].name;
|
---|
| 209 | item.OnClick := Self.popup_opentool;
|
---|
| 210 | filepopup.Items.Insert(i, item);
|
---|
| 211 | end;
|
---|
| 212 | end;
|
---|
| 213 | end;
|
---|
| 214 |
|
---|
| 215 | procedure TForm_ToolTemplate.filepopupPopup(Sender: TObject);
|
---|
| 216 | var
|
---|
| 217 | ext: String;
|
---|
| 218 | i: Integer;
|
---|
| 219 | begin
|
---|
| 220 | ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
|
---|
| 221 | for i := 0 to High(ToolList) do
|
---|
| 222 | begin
|
---|
| 223 | filepopup.Items.Items[i].Enabled := True;
|
---|
| 224 | if Length(ToolList[i].exts) > 0 then
|
---|
| 225 | if Pos(ext, ToolList[i].exts) = 0 then
|
---|
| 226 | filepopup.Items.Items[i].Enabled := False;
|
---|
| 227 | end;
|
---|
| 228 | end;
|
---|
| 229 |
|
---|
| 230 | procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject);
|
---|
| 231 | begin
|
---|
| 232 | LoadFileNames;
|
---|
| 233 | end;
|
---|
| 234 |
|
---|
| 235 | procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
|
---|
| 236 | begin
|
---|
| 237 | if btn_sort_id_asc.Down then
|
---|
| 238 | FSortBy := stIDAsc
|
---|
| 239 | else if btn_sort_id_desc.Down then
|
---|
| 240 | FSortBy := stIDDesc
|
---|
| 241 | else if btn_sort_name_asc.Down then
|
---|
| 242 | FSortBy := stNameAsc
|
---|
| 243 | else if btn_sort_name_desc.Down then
|
---|
| 244 | FSortBy := stNameDesc
|
---|
| 245 | else if btn_sort_ext_asc.Down then
|
---|
| 246 | FSortBy := stExtAsc
|
---|
| 247 | else if btn_sort_ext_desc.Down then
|
---|
| 248 | FSortBy := stExtDesc;
|
---|
| 249 | LoadFileNames;
|
---|
| 250 | end;
|
---|
| 251 |
|
---|
| 252 | procedure TForm_ToolTemplate.check_filternameClick(Sender: TObject);
|
---|
| 253 | begin
|
---|
| 254 | edit_filtername.Enabled := not check_filtername.Checked;
|
---|
| 255 | LoadFileNames;
|
---|
| 256 | end;
|
---|
| 257 |
|
---|
| 258 | procedure TForm_ToolTemplate.listClick(Sender: TObject);
|
---|
| 259 | var
|
---|
| 260 | fileid: Integer;
|
---|
| 261 | begin
|
---|
| 262 | if filelist.ItemIndex > -1 then
|
---|
| 263 | begin
|
---|
| 264 | fileid := OniDataConnection.ExtractFileID(
|
---|
| 265 | filelist.Items.Strings[filelist.ItemIndex]);
|
---|
| 266 | if Assigned(FOnNewFileSelected) then
|
---|
| 267 | FOnNewFileSelected(OniDataConnection.GetFileInfo(fileid));
|
---|
| 268 | end;
|
---|
| 269 | end;
|
---|
| 270 |
|
---|
| 271 | procedure TForm_ToolTemplate.listMouseDown(Sender: TObject; Button: TMouseButton;
|
---|
| 272 | Shift: TShiftState; X, Y: Integer);
|
---|
| 273 | var
|
---|
| 274 | pt: TPoint;
|
---|
| 275 | begin
|
---|
| 276 | pt.X := x;
|
---|
| 277 | pt.Y := y;
|
---|
| 278 | filelist.ItemIndex := filelist.ItemAtPos(pt, true);
|
---|
| 279 | Self.listClick(Self);
|
---|
| 280 | end;
|
---|
| 281 |
|
---|
| 282 |
|
---|
| 283 |
|
---|
| 284 | procedure TForm_ToolTemplate.SelectFileID(id: Integer);
|
---|
| 285 | var
|
---|
| 286 | i: Integer;
|
---|
| 287 | begin
|
---|
| 288 | filelist.ItemIndex := -1;
|
---|
| 289 | if filelist.Items.Count > 0 then
|
---|
| 290 | for i := 0 to filelist.Items.Count - 1 do
|
---|
| 291 | if OniDataConnection.ExtractFileID(filelist.Items.Strings[i]) = id then
|
---|
| 292 | begin
|
---|
| 293 | filelist.ItemIndex := i;
|
---|
| 294 | Break;
|
---|
| 295 | end;
|
---|
| 296 | Self.listClick(Self);
|
---|
| 297 | end;
|
---|
| 298 |
|
---|
| 299 | procedure TForm_ToolTemplate.SelectFileName(filename: String);
|
---|
| 300 | var
|
---|
| 301 | i: Integer;
|
---|
| 302 | begin
|
---|
| 303 | filelist.ItemIndex := -1;
|
---|
| 304 | if filelist.Items.Count > 0 then
|
---|
| 305 | for i := 0 to filelist.Items.Count - 1 do
|
---|
| 306 | if filelist.Items.Strings[i] = filename then
|
---|
| 307 | filelist.ItemIndex := i;
|
---|
| 308 | Self.listClick(Self);
|
---|
| 309 | end;
|
---|
| 310 |
|
---|
| 311 | procedure TForm_ToolTemplate.SetAllowedExts(exts: String);
|
---|
| 312 | begin
|
---|
| 313 | FAllowedExts := exts;
|
---|
| 314 | RecreateList;
|
---|
| 315 | end;
|
---|
| 316 |
|
---|
| 317 | procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String;
|
---|
| 318 | zerobytes: Boolean);
|
---|
| 319 | var
|
---|
| 320 | i: Integer;
|
---|
| 321 | begin
|
---|
| 322 | if Length(pattern) > 0 then
|
---|
| 323 | Self.edit_filtername.Text := pattern;
|
---|
| 324 | Self.check_filtername.Checked := Length(pattern) > 0;
|
---|
| 325 | if Length(extension) > 0 then
|
---|
| 326 | begin
|
---|
| 327 | for i := 0 to Self.combo_extension.Items.Count - 1 do
|
---|
| 328 | if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then
|
---|
| 329 | Break;
|
---|
| 330 | if i < Self.combo_extension.Items.Count then
|
---|
| 331 | Self.combo_extension.ItemIndex := i
|
---|
| 332 | else
|
---|
| 333 | Self.combo_extension.ItemIndex := -1;
|
---|
| 334 | end;
|
---|
| 335 | Self.check_zerobyte.Checked := zerobytes;
|
---|
| 336 | Self.LoadFileNames;
|
---|
| 337 | end;
|
---|
| 338 |
|
---|
| 339 | procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean);
|
---|
| 340 | begin
|
---|
| 341 | FAllowMultiSelect := allow;
|
---|
| 342 | filelist.MultiSelect := allow;
|
---|
| 343 | end;
|
---|
| 344 |
|
---|
| 345 |
|
---|
| 346 | procedure TForm_ToolTemplate.FormResize(Sender: TObject);
|
---|
| 347 | begin
|
---|
| 348 | if Self.Width < 300 then
|
---|
| 349 | Self.Width := 300;
|
---|
| 350 | if Self.Height < 200 then
|
---|
| 351 | Self.Height := 200;
|
---|
| 352 | end;
|
---|
| 353 |
|
---|
| 354 |
|
---|
| 355 |
|
---|
| 356 | procedure TForm_ToolTemplate.FormCreate(Sender: TObject);
|
---|
| 357 | begin
|
---|
| 358 | Self.Width := 260;
|
---|
| 359 | Self.Height := 300;
|
---|
| 360 | FOnNewFileSelected := nil;
|
---|
| 361 | FAllowedExts := '';
|
---|
| 362 | FAllowMultiSelect := False;
|
---|
| 363 | end;
|
---|
| 364 |
|
---|
| 365 | procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
| 366 | begin
|
---|
| 367 | Action := caFree;
|
---|
| 368 | end;
|
---|
| 369 |
|
---|
| 370 |
|
---|
| 371 | procedure AddToolListEntryExt(context, ext: String);
|
---|
| 372 | var
|
---|
| 373 | i: Integer;
|
---|
| 374 | begin
|
---|
| 375 | for i := 0 to High(ToolList) do
|
---|
| 376 | if ToolList[i].context = context then
|
---|
| 377 | begin
|
---|
| 378 | if Pos(ext, ToolList[i].exts) = 0 then
|
---|
| 379 | begin
|
---|
| 380 | if Length(ToolList[i].exts) = 0 then
|
---|
| 381 | ToolList[i].exts := ext
|
---|
| 382 | else
|
---|
| 383 | ToolList[i].exts := ToolList[i].exts + ',' + ext;
|
---|
| 384 | end;
|
---|
| 385 | Exit;
|
---|
| 386 | end;
|
---|
| 387 | end;
|
---|
| 388 |
|
---|
| 389 | procedure AddToolListEntry(context, name, exts: String);
|
---|
| 390 | var
|
---|
| 391 | i: Integer;
|
---|
| 392 | begin
|
---|
| 393 | if Length(ToolList) > 0 then
|
---|
| 394 | begin
|
---|
| 395 | for i := 0 to High(ToolList) do
|
---|
| 396 | if ToolList[i].context = context then
|
---|
| 397 | begin
|
---|
| 398 | if (Length(ToolList[i].name) = 0) and (Length(name) > 0) then
|
---|
| 399 | ToolList[i].name := name;
|
---|
| 400 | if Length(exts) > 0 then
|
---|
| 401 | AddToolListEntryExt(context, exts);
|
---|
| 402 | Exit;
|
---|
| 403 | end;
|
---|
| 404 | end;
|
---|
| 405 | SetLength(ToolList, Length(ToolList) + 1);
|
---|
| 406 | for i := High(ToolList) downto 1 do
|
---|
| 407 | if ToolList[i - 1].name > name then
|
---|
| 408 | ToolList[i] := ToolList[i - 1]
|
---|
| 409 | else
|
---|
| 410 | Break;
|
---|
| 411 | ToolList[i].context := context;
|
---|
| 412 | ToolList[i].name := name;
|
---|
| 413 | ToolList[i].exts := exts;
|
---|
| 414 | end;
|
---|
| 415 |
|
---|
| 416 | end.
|
---|