Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
7.9 KiB

3 years ago
  1. unit MainForm;
  2. interface
  3. uses
  4. Classes, SysUtils, FileUtil, Forms,
  5. Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  6. ComCtrls, ActnList,
  7. // App units
  8. Runner, Options;
  9. type
  10. { TFormMain }
  11. TFormMain = class(TForm)
  12. ActDeskew: TAction;
  13. ActFinish: TAction;
  14. ActAddFiles: TAction;
  15. ActClearFiles: TAction;
  16. ActBrowseOutputDir: TAction;
  17. ActShowAbout: TAction;
  18. ActShowAdvOptions: TAction;
  19. ActionList: TActionList;
  20. ApplicationProperties: TApplicationProperties;
  21. BtnAddFiles: TButton;
  22. BtnDeskew: TButton;
  23. BtnClear: TButton;
  24. BtnFinish: TButton;
  25. BtnBrowseOutputDir: TButton;
  26. BtnAdvOptions: TButton;
  27. BtnAbout: TButton;
  28. CheckDefaultOutputFileOptions: TCheckBox;
  29. ColorBtnBackground: TColorButton;
  30. ComboFileFormat: TComboBox;
  31. EdDirOutput: TEdit;
  32. Label1: TLabel;
  33. Label2: TLabel;
  34. Label3: TLabel;
  35. LabOptOutputFolder: TLabel;
  36. LabBackColor: TLabel;
  37. LabDeskewProgressTitle: TLabel;
  38. LabOptFileFormat: TLabel;
  39. LabProgressTitle: TLabel;
  40. LabCurrentFile: TLabel;
  41. MemoOutput: TMemo;
  42. MemoFiles: TMemo;
  43. Notebook: TNotebook;
  44. PageIn: TPage;
  45. PageOut: TPage;
  46. Panel1: TPanel;
  47. PanelProgress: TPanel;
  48. PanelOut: TPanel;
  49. PanelFiles: TPanel;
  50. PanelOptions: TPanel;
  51. ProgressBar: TProgressBar;
  52. procedure ActAddFilesExecute(Sender: TObject);
  53. procedure ActBrowseOutputDirExecute(Sender: TObject);
  54. procedure ActClearFilesExecute(Sender: TObject);
  55. procedure ActDeskewExecute(Sender: TObject);
  56. procedure ActDeskewUpdate(Sender: TObject);
  57. procedure ActFinishExecute(Sender: TObject);
  58. procedure ActShowAboutExecute(Sender: TObject);
  59. procedure ActShowAdvOptionsExecute(Sender: TObject);
  60. procedure ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
  61. procedure FormCreate(Sender: TObject);
  62. procedure FormDestroy(Sender: TObject);
  63. procedure FormDropFiles(Sender: TObject; const FileNames: array of String);
  64. private
  65. FRunner: TRunner;
  66. procedure RunnerFinished(Sender: TObject; Reason: TFinishReason);
  67. procedure RunnerProgress(Sender: TObject; Index: Integer);
  68. procedure GatherOptions(AOptions: TOptions);
  69. public
  70. procedure ApplyOptions(AOptions: TOptions);
  71. end;
  72. var
  73. FormMain: TFormMain;
  74. implementation
  75. {$R *.lfm}
  76. uses
  77. ImagingUtility, Imaging, DataModule, AdvOptionsForm, AboutForm, Config;
  78. { TFormMain }
  79. procedure TFormMain.FormCreate(Sender: TObject);
  80. begin
  81. FRunner := TRunner.Create(MemoOutput);
  82. FRunner.OnFinished := RunnerFinished;
  83. FRunner.OnProgress := RunnerProgress;
  84. Caption := Application.Title + ' v' + Module.VersionString;
  85. ComboFileFormat.Items.Clear;
  86. ComboFileFormat.Items.AddObject('Same as input', TObject(ffSameAsInput));
  87. ComboFileFormat.Items.AddObject('PNG', TObject(ffPng));
  88. ComboFileFormat.Items.AddObject('JPEG', TObject(ffJpeg));
  89. ComboFileFormat.Items.AddObject('TIFF (support depends on platform)', TObject(ffTiff));
  90. ComboFileFormat.Items.AddObject('BMP', TObject(ffBmp));
  91. ComboFileFormat.Items.AddObject('PSD', TObject(ffPsd));
  92. ComboFileFormat.Items.AddObject('TGA', TObject(ffTga));
  93. ComboFileFormat.Items.AddObject('JNG', TObject(ffJng));
  94. ComboFileFormat.Items.AddObject('PPM', TObject(ffPpm));
  95. ComboFileFormat.ItemIndex := 0;
  96. ApplyOptions(Module.Options);
  97. Config.AfterMainFormCreation(Self);
  98. if Screen.WorkAreaHeight < Height then
  99. Height := Round(Screen.WorkAreaHeight * 0.9);
  100. end;
  101. procedure TFormMain.FormDestroy(Sender: TObject);
  102. begin
  103. GatherOptions(Module.Options);
  104. FRunner.Free;
  105. end;
  106. procedure TFormMain.FormDropFiles(Sender: TObject; const FileNames: array of String);
  107. var
  108. I: Integer;
  109. begin
  110. for I := 0 to High(FileNames) do
  111. MemoFiles.Append(FileNames[I]);
  112. end;
  113. procedure TFormMain.ApplyOptions(AOptions: TOptions);
  114. begin
  115. CheckDefaultOutputFileOptions.Checked := AOptions.DefaultOutputFileOptions;
  116. EdDirOutput.Text := AOptions.OutputFolder;
  117. EdDirOutput.SelStart := Length(EdDirOutput.Text);
  118. ComboFileFormat.ItemIndex := Integer(AOptions.OutputFileFormat);
  119. ColorBtnBackground.ButtonColor := RGBToColor(GetRedValue(AOptions.BackgroundColor), GetGreenValue(AOptions.BackgroundColor), GetBlueValue(AOptions.BackgroundColor));
  120. end;
  121. procedure TFormMain.GatherOptions(AOptions: TOptions);
  122. var
  123. LazColor: TColor;
  124. I: Integer;
  125. S: string;
  126. begin
  127. AOptions.Files.Clear;
  128. for I := 0 to MemoFiles.Lines.Count - 1 do
  129. begin
  130. S := Trim(MemoFiles.Lines[I]);
  131. if S <> '' then
  132. AOptions.Files.Add(S);
  133. end;
  134. AOptions.DefaultOutputFileOptions := CheckDefaultOutputFileOptions.Checked;
  135. AOptions.OutputFolder := EdDirOutput.Text;
  136. AOptions.OutputFileFormat := TFileFormat(PtrUInt(ComboFileFormat.Items.Objects[ComboFileFormat.ItemIndex]));
  137. LazColor := ColorBtnBackground.ButtonColor;
  138. AOptions.BackgroundColor := Color32(255, Red(LazColor), Green(LazColor), Blue(LazColor)).Color;
  139. end;
  140. procedure TFormMain.RunnerFinished(Sender: TObject; Reason: TFinishReason);
  141. begin
  142. LabCurrentFile.Hide;
  143. ActFinish.Enabled := True;
  144. ActFinish.Caption := 'Back to Input';
  145. case Reason of
  146. frFinished: LabDeskewProgressTitle.Caption := 'Deskewing Finished';
  147. frFailure: LabDeskewProgressTitle.Caption := 'Deskewing Finished with Failures';
  148. frStopped: LabDeskewProgressTitle.Caption := 'Deskewing Stopped';
  149. else
  150. Assert(False);
  151. end;
  152. LabProgressTitle.Caption := Format('%d files processed', [FRunner.InputPos]);
  153. if FRunner.Failures > 0 then
  154. LabProgressTitle.Caption := LabProgressTitle.Caption + Format(', %d failed', [FRunner.Failures]);
  155. end;
  156. procedure TFormMain.RunnerProgress(Sender: TObject; Index: Integer);
  157. begin
  158. ProgressBar.Position := Index + 1;
  159. LabCurrentFile.Caption := Format('%s [%d/%d]', [
  160. ExtractFileName(Module.Options.Files[Index]), Index + 1, Module.Options.Files.Count]);
  161. LabCurrentFile.Visible := True;
  162. LabProgressTitle.Visible := True;
  163. end;
  164. procedure TFormMain.ActDeskewUpdate(Sender: TObject);
  165. begin
  166. TAction(Sender).Enabled := (MemoFiles.Lines.Count > 0) and (Trim(MemoFiles.Lines[0]) <> '');
  167. end;
  168. procedure TFormMain.ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
  169. var
  170. NoDefault: Boolean;
  171. begin
  172. NoDefault := not CheckDefaultOutputFileOptions.Checked;
  173. ActBrowseOutputDir.Enabled := NoDefault;
  174. EdDirOutput.Enabled := ActBrowseOutputDir.Enabled;
  175. ComboFileFormat.Enabled := NoDefault;
  176. LabOptOutputFolder.Enabled := NoDefault;
  177. LabOptFileFormat.Enabled := NoDefault;
  178. FormAdvOptions.DoIdle;
  179. end;
  180. procedure TFormMain.ActDeskewExecute(Sender: TObject);
  181. begin
  182. GatherOptions(Module.Options);
  183. FormAdvOptions.GatherOptions(Module.Options);
  184. ActFinish.Caption := 'Stop';
  185. MemoOutput.Clear;
  186. ProgressBar.Position := 0;
  187. ProgressBar.Max := Module.Options.Files.Count;
  188. LabCurrentFile.Hide;
  189. LabProgressTitle.Hide;
  190. LabProgressTitle.Caption := 'Current file:';
  191. Notebook.PageIndex := 1;
  192. Application.ProcessMessages;
  193. FRunner.Run(Module.Options);
  194. end;
  195. procedure TFormMain.ActAddFilesExecute(Sender: TObject);
  196. var
  197. I: Integer;
  198. begin
  199. Module.OpenDialogMulti.Title := 'Select Picture Files';
  200. if Module.OpenDialogMulti.Execute then
  201. begin
  202. for I := 0 to Module.OpenDialogMulti.Files.Count - 1 do
  203. MemoFiles.Append(Module.OpenDialogMulti.Files[I]);
  204. end;
  205. end;
  206. procedure TFormMain.ActBrowseOutputDirExecute(Sender: TObject);
  207. begin
  208. if Module.SelectDirectoryDialog.Execute then
  209. begin
  210. EdDirOutput.Text := Module.SelectDirectoryDialog.FileName;
  211. EdDirOutput.SelStart := Length(EdDirOutput.Text);
  212. end;
  213. end;
  214. procedure TFormMain.ActClearFilesExecute(Sender: TObject);
  215. begin
  216. MemoFiles.Clear;
  217. end;
  218. procedure TFormMain.ActFinishExecute(Sender: TObject);
  219. begin
  220. if FRunner.IsRunning then
  221. begin
  222. ActFinish.Enabled := False;
  223. ActFinish.Caption := 'Stopping';
  224. FRunner.Stop;
  225. end
  226. else
  227. begin
  228. Notebook.PageIndex := 0;
  229. end;
  230. end;
  231. procedure TFormMain.ActShowAboutExecute(Sender: TObject);
  232. begin
  233. if not FormAbout.Visible then
  234. FormAbout.ShowModal;
  235. end;
  236. procedure TFormMain.ActShowAdvOptionsExecute(Sender: TObject);
  237. begin
  238. FormAdvOptions.ShowModal;
  239. end;
  240. end.