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