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