-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlipMain.cpp
More file actions
1054 lines (912 loc) · 35.6 KB
/
FlipMain.cpp
File metadata and controls
1054 lines (912 loc) · 35.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "FlipMain.h"
#include "FlipDataViewer.h"
#include "FlipProgramLog.h"
#include "FlipTemplateEditor.h"
#include <poppler/cpp/poppler-document.h>
#include <poppler/cpp/poppler-page.h>
#include <wx/bitmap.h>
#include <wx/bmpbndl.h>
#include <wx/datetime.h>
#include <wx/dir.h>
#include <wx/filename.h>
#include <wx/msgdlg.h>
#include <wx/regex.h>
#include <wx/stdpaths.h>
#include <wx/string.h>
#include <wx/textfile.h>
#include <wx/utils.h>
#include <wx/vector.h>
#include <wx/wfstream.h>
// define custom data types & events
// type to hold regex and substitution string pairs
typedef wxVector<std::pair<wxString, wxString>> RegexSubstitutionList;
wxDEFINE_EVENT(EVT_TEMPLATE_LIST_UPDATED, wxCommandEvent);
wxDEFINE_EVENT(EVT_FLIPMAIN_LAUNCH_CLICKED, wxCommandEvent);
// dev-note: if you add resource const values, ensure they prepend with leading / char
const wxString FlipMain::RESOURCE_MENU_ICONS_PATH = wxT("/resources/images/menuIcons");
FlipMain::FlipMain(wxWindow *parent)
: Main(parent),
m_startupArguments()
{
}
FlipMain::FlipMain(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &pos, const wxSize &size, long style)
: Main(parent),
FLIP_USER_HOME_PATH(wxGetHomeDir()),
FLIP_DEFAULT_CONFIG_PATH(wxGetHomeDir() + "/.flip"),
FLIP_DEFAULT_TEMPLATE_PATH(wxGetHomeDir() + "/.flip/templates"),
FLIP_DEFAULT_OUTPUT_FILENAME("/Documents/flipOutput.txt"),
m_startupArguments()
{
// dev-note: const class members must be declared in the constructor's initialiation list
// not in the constructor body. (allegedly)
// create a FlipProgramLog <wxFrame> object which is a child of this (FlipMain <wxFrame>)
m_programLog = std::make_unique<FlipProgramLog>(this);
m_programLog->LogMessage("Program started.");
// create a FlipTemplateEditor <wxFrame> object which is a child of this (FlipMain <wxFrame>)
m_templateEditor = std::make_unique<FlipTemplateEditor>(this);
// create a FlipDataViewer <wxFrame> object which is a child of this (FlipMain <wxFrame>)
m_dataViewer = std::make_unique<FlipDataViewer>(this);
this->SetSizerAndFit(this->m_mainFrameSizer);
this->SetupMenuIcons(this->m_menuFile);
this->SetupMenuIcons(this->m_menuLog);
// event handler binds
Bind(EVT_FLIPDATAVIEWER_BTNSAVE_CLICKED, &FlipMain::OnFlipDataViewerBtnSave, this);
Bind(EVT_FLIPDATAVIEWER_FINISHPROCESSING_CLICKED, &FlipMain::OnFlipDataViewerBtnFinishProcessing, this);
Bind(EVT_FLIPDATAVIEWER_CONTINUEPROCESSING_CLICKED, &FlipMain::OnFlipDataViewerBtnContinueProcessing, this);
Bind(wxEVT_MENU, &FlipMain::OnAbout, this, ID_MENU_FILE_ABOUT);
Bind(wxEVT_MENU, &FlipMain::OnQuit, this, ID_MENU_FILE_QUIT);
Bind(wxEVT_MENU, &FlipMain::OnShowProgramLog, this, ID_MENU_LOG_PROGRAMLOG);
Bind(wxEVT_MENU, &FlipMain::OnShowTemplateEditor, this, ID_MENU_FILE_TEMPLATEEDITOR);
Bind(wxEVT_MENU, &FlipMain::OnShowDataViewer, this, ID_MENU_FILE_DATAVIEWER);
Bind(wxEVT_TIMER, &FlipMain::OnTemplateFilePoll, this);
// spcific widget binds
m_btnLaunch->Bind(wxEVT_BUTTON, &FlipMain::OnBtnLaunch, this);
m_useTemplate->Bind(wxEVT_CHOICE, &FlipMain::OnUseTemplateChoice, this);
m_switchDBP->Bind(wxEVT_CHECKBOX, &FlipMain::OnSwitchDBPChecked, this);
// dev-note: we don't use . (current dir, or same dir as the .exe file) as a valid place
// for templates because during development this will populate the template wxChoice's with \
// a load of development files.
m_configTemplateDirs.Add(FLIP_DEFAULT_TEMPLATE_PATH);
m_configTemplateDirs.Add("./templates");
// load available templates into wxChoice widgets
LogMessage("Looking for existing template files in user's home/.flip/templates, and ./templates");
m_tmap_userTemplates = ReadUserTemplates();
// dev-note: UpdateTemplateChoices *should* invoke an update in FlipTemplateEditor::wxChoice widget as well
UpdateTemplateChoices();
// start poll timer to monitor templates folders
m_filePollTimer.SetOwner(this); // Attach timer to FlipMain
m_filePollTimer.Start(5000); // Poll every 5 seconds
// do some wxWidget positioning stuff
this->Fit();
}
FlipMain::~FlipMain()
{
// destructor
m_filePollTimer.Stop();
Unbind(wxEVT_TIMER, &FlipMain::OnTemplateFilePoll, this);
}
bool FlipMain::DoAutoLAUNCH()
{
bool result = m_startupArguments.IsSwitchPresent("quiet");
wxString str = wxString::Format("%s", result ? "true" : "false");
if (result)
{
LogMessage("Quiet mode (no GUI) is on");
}
if (m_doAutoLAUNCH)
{
LogMessage("Automatic data processing was launched");
// OnBtnLaunch() should verify reqd data is ready for us
// trigger event for OnBtnLaunch()
wxCommandEvent buttonEvent(wxEVT_BUTTON, m_btnLaunch->GetId());
wxPostEvent(m_btnLaunch, buttonEvent);
return true;
}
return false;
}
StartupArgumentsParser &FlipMain::GetArgumentsParser()
{
// return the m_startupArguments _instance_
return m_startupArguments;
}
wxString FlipMain::GetPDFPageText(const int pageNum)
{
if (pageNum >= 0 && pageNum < m_vec_pdfData.size())
{
return wxString(m_vec_pdfData[pageNum]);
}
return wxEmptyString;
}
wxString FlipMain::GetProcessedPDFPageText(const int pageNum)
{
if (pageNum >= 0 && pageNum < m_vec_pdfData.size())
{
return m_vec_pdfDataProcessed[pageNum];
}
return wxEmptyString;
}
int FlipMain::GetPDFPageTotal()
{
return m_vec_pdfData.size();
}
int FlipMain::GetProcessedPDFPageTotal()
{
return m_vec_pdfDataProcessed.size();
}
int FlipMain::GetRegexCurrentIndex()
{
return m_currentRegex;
}
int FlipMain::GetRegexTotal()
{
return m_regexList.size();
}
bool FlipMain::GetSwitchValue(const wxString &switchName)
{
if (switchName.Lower() == "dbp")
{
return m_switchDBP->GetValue();
}
else if (switchName.Lower() == "sws")
{
return m_switchSWS->GetValue();
}
else if (switchName.Lower() == "quiet")
{
return m_doQuietMode;
}
else
{
return false;
}
}
bool FlipMain::GetIsPDFPageRekt(const int &pageNum)
{
return std::find(m_vec_pdfDataRektPages.begin(), m_vec_pdfDataRektPages.end(), pageNum) != m_vec_pdfDataRektPages.end();
}
void FlipMain::LoadRegexSubstitutionPairs(const wxString &templateFilePath, RegexSubstitutionList ®exList)
{
wxTextFile file(templateFilePath);
if (!file.Open())
{
LogMessage("Core: Unable to open template file: " + templateFilePath);
return;
}
wxString regexPattern = "^(.*?)\\s*=>\\s*(.*)$"; // regex to extract program regex/substitution pair
wxRegEx regexValidator(regexPattern);
for (wxString line = file.GetFirstLine(); !file.Eof(); line = file.GetNextLine())
{
if (!fnIgnoreLine(line.ToStdString()))
{
if (regexValidator.Matches(line))
{
wxString regexString = regexValidator.GetMatch(line, 1); // Get the regex part (with whitespace preserved)
wxString substitutionString = regexValidator.GetMatch(line, 2); // Get the substitution part (could be empty)
// Handle backslashes and convert them to actual escaped values (e.g., "\n" to newline)
substitutionString.Replace("\\n", "\n");
substitutionString.Replace("\\t", "\t");
// Section: Convert $1, $2... to \1, \2... but keep \$ as literal $
wxString result;
for (size_t i = 0; i < substitutionString.Length(); ++i)
{
if (substitutionString[i] == '\\' && i + 1 < substitutionString.Length() && substitutionString[i + 1] == '$')
{
// This is a literal $ (preceded by \), so keep it as \$.
result << "\\$";
++i; // Skip next character since it's part of this escape sequence
}
else if (substitutionString[i] == '$')
{
// Convert $ to \ for regex group substitution
result << '\\';
}
else
{
// Otherwise, just copy the character
result << substitutionString[i];
}
}
substitutionString = result;
// Add the pair to the list
regexList.push_back(std::make_pair(regexString, substitutionString));
LogMessage("Valid regex in template file: " + line);
}
else
{
LogMessage("Invalid regex in template file: " + line);
}
}
}
file.Close();
}
void FlipMain::LogAllChildWidgets()
{
// Get the main frame's top-level window
wxWindowList &children = GetChildren();
// Iterate over all top-level children
for (wxWindowList::iterator it = children.begin(); it != children.end(); ++it)
{
wxWindow *child = *it;
if (child)
{
// Log the class name of each child window
wxString childClass = child->GetClassInfo()->GetClassName();
m_programLog->LogMessage("Child widget class name: " + childClass);
// Additionally, you can recursively log children of this window if needed
LogChildWidgetsRecursive(child);
}
}
}
void FlipMain::LogChildWidgetsRecursive(wxWindow *parent)
{
// Iterate over all children of the given parent window
wxWindowList &children = parent->GetChildren();
for (wxWindowList::iterator it = children.begin(); it != children.end(); ++it)
{
wxWindow *child = *it;
if (child)
{
// Log the class name of each child window
wxString childClass = child->GetClassInfo()->GetClassName();
wxString parentClass = parent->GetClassInfo()->GetClassName();
m_programLog->LogMessage("Child widget of " + parentClass + ": " + childClass);
// Recursively log children of this window
LogChildWidgetsRecursive(child);
}
}
}
void FlipMain::LogMessage(wxString message)
{
// public function to pass messages to the Program Log
if (m_programLog)
{
m_programLog->LogMessage(message);
}
if (m_useConsoleOutput)
{
std::cout << message << std::endl;
}
}
bool FlipMain::NormalizeFilePathString(wxString &path)
{
bool success = false;
if (!path.IsEmpty())
{
wxFileName wxfnPath(path);
wxfnPath.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE);
path = wxfnPath.GetFullPath();
success = true;
}
return success;
}
void FlipMain::OnAbout(wxCommandEvent &event)
{
wxMessageBox("Flip v0.1\n\nProcess PDF files and get their data.\n\nDeveloped by cabji - 2024", "About Flip", wxOK | wxICON_INFORMATION, nullptr);
}
void FlipMain::OnBtnLaunch(wxCommandEvent &event)
{
// Initialize local data & verify user input values
bool success = true;
std::set<int> processPages;
wxString templateFileAbsolutePath;
int problemCount = 1;
// reset class member data involved in this process to prevent any data accumulation
m_vec_pdfData.clear();
m_vec_pdfDataProcessed.clear();
m_regexList.clear();
m_currentRegex = 0;
// set continue processing buttton to be enabled if it's disabled
if (!m_dataViewer->GetBtnContinueProcessingAbility()) {m_dataViewer->ToggleBtnContinueProcessingAbility();}
// set Finish processing buttton to be enabled if it's disabled
if (!m_dataViewer->GetBtnFinishProcessingAbility()) {m_dataViewer->ToggleBtnFinishProcessingAbility();}
// set data viewer::save button to disabled if it enabled
if (m_dataViewer->GetBtnSaveAbility()) {m_dataViewer->ToggleBtnSaveAbility();}
// 1. Validate m_inputFile (wxFilePickerCtrl)
wxString inputFilePath = m_inputFile->GetPath();
if (inputFilePath.IsEmpty())
{
m_tempOutput << problemCount++ << ". Input file is empty\n";
success = false;
}
else if (!wxFile::Exists(inputFilePath))
{
m_tempOutput << problemCount++ << ". Input file does not exist\n";
success = false;
}
// 2. Validate m_useTemplate (wxChoice)
int templateSelectionIndex = m_useTemplate->GetSelection();
if (templateSelectionIndex == wxNOT_FOUND)
{
m_tempOutput << problemCount++ << ". No template selected\n";
success = false;
}
else
{
// Get the template file path using the template selection
wxString selectedTemplateFilename = m_useTemplate->GetString(templateSelectionIndex);
templateFileAbsolutePath = m_tmap_userTemplates[selectedTemplateFilename];
if (!wxFile::Exists(templateFileAbsolutePath))
{
m_tempOutput << problemCount++ << ". Template file does not exist/is inaccessible\n";
success = false;
}
}
// 3. Validate m_outputFile (wxFilePickerCtrl) or use a default output path
wxString outputFilePath = m_outputFile->GetPath();
if (outputFilePath.IsEmpty())
{
// If no output file provided, use the default output path in the user's home directory
wxString defaultOutputFilePath = FLIP_USER_HOME_PATH + FLIP_DEFAULT_OUTPUT_FILENAME;
outputFilePath = defaultOutputFilePath;
NormalizeFilePathString(defaultOutputFilePath);
m_tempOutput << problemCount++ << ". No output filename given, using default: " + defaultOutputFilePath + "\n";
}
else
{
// Check if the file can be created by attempting to open it for writing
wxFileOutputStream outputStream(outputFilePath);
if (!outputStream.IsOk())
{
m_tempOutput << problemCount++ << ". Output file is unwriteable/inaccessible\n";
success = false;
}
}
// 4. process switches
// 4a. Per page processing input validation
std::string pageRangeString = m_ProcessPages->GetValue().ToStdString();
// only attempt per-page processing if the user input something into the widget
if (!pageRangeString.empty())
{
processPages = fnParsePageSelection(pageRangeString);
if (processPages.empty())
{
m_tempOutput << problemCount++ << ". Page processing string is invalid. Hover cursor over widget to get help tips\n";
success = false;
}
wxString outStr = "Number of pages to be processed: " + std::to_string(processPages.size()) + " (Pages: ";
for (int pageNum : processPages)
{
outStr << pageNum << ",";
}
outStr.RemoveLast() << ")";
LogMessage(outStr);
}
// If any validation failed, output details on dialog and return early
if (!success)
{
LogMessage("Required values missing. Please check the following and try again: \n\n" + m_tempOutput);
if (!m_doQuietMode)
{
wxMessageBox("Required values missing. Please check the following and try again: \n\n" + m_tempOutput, "Required information missing", wxOK | wxICON_WARNING);
}
m_tempOutput = wxEmptyString;
return;
}
// At this point, all validations passed, and we can proceed with core processing
// reset m_tempOutput as we dont need to show any messages to the user for this current undertaking
m_tempOutput = wxEmptyString;
// *************** Start Core Functionality - Read and Parse PDF Data ***************
// get regexes from template - store in private member m_regexList
LoadRegexSubstitutionPairs(templateFileAbsolutePath, m_regexList);
// open input PDF file
poppler::document *inPDF = poppler::document::load_from_file(inputFilePath.ToStdString());
if (!inPDF)
{
LogMessage("Error: could not open input file '" + inputFilePath + "'");
return;
}
// get text data from PDF file
// use private member std::vector<std::string> m_vec_pdfData to store the PDF page text data
auto numPages = inPDF->pages();
// LogMessage("Reading text data from PDF file...");
// iterate the pages and extract text data into vec_PDFPageStrings
m_tempOutput = "Processing page number: ";
for (auto i = 0; i < numPages; ++i)
{
// per-page processing: to be added
if ((processPages.find(i) != processPages.end()) || (pageRangeString.empty()))
{
// append page number to output string
m_tempOutput << i << ", ";
poppler::page *inPDFPage = inPDF->create_page(i);
if (!inPDFPage)
{
LogMessage("Problem: Could not create poppler::page object, index: " + wxString::Format(wxT("%i"), i));
continue;
}
// Extract text and ensure it's not null
auto textData = inPDFPage->text().to_utf8();
std::string pageText(textData.data(), textData.size());
fnStrNormalizeNewLineChars(pageText);
// switch: -sws (strip excesive whitespace)
if (m_switchSWS->GetValue())
{
pageText = fnStrStripExcessiveWhitespace(pageText);
}
// switch: -dbp (show data before processing)
if (m_switchDBP->GetValue())
{
LogMessage("Data Before Processing - Page " + wxString::Format(wxT("%i"), i) + "\n" + pageText + "\n");
}
m_vec_pdfData.push_back(pageText);
delete inPDFPage;
}
}
m_tempOutput.RemoveLast(2);
LogMessage(m_tempOutput);
m_tempOutput = wxEmptyString;
int pagesProcessed = m_vec_pdfData.size();
// temporary output message:
LogMessage("Processed " + wxString::Format(wxT("%i"), pagesProcessed) + " pages from the input file.");
// at this point we need to know if -dbp switch is true, or if m_doAutoLAUNCH is true.
// if m_switchDBP->GetValue() == false or m_doAutoLAUNCH == true, we can continue
// processing immediately.
// otherwise, process continuation is handed over to the user via the FlipDataViewer window
// and we wait for button click from there.
wxCommandEvent tripEvent(EVT_FLIPMAIN_LAUNCH_CLICKED);
if (!m_switchDBP->GetValue() || m_doAutoLAUNCH)
{
OnFlipDataViewerBtnFinishProcessing(tripEvent);
// trigger event so that FlipDataViewer object knows the LAUNCH button was clicked
wxPostEvent(m_dataViewer.get(), tripEvent);
}
else
{
wxPostEvent(m_dataViewer.get(), tripEvent);
}
// if FlipMain::LAUNCH btn was clicked and m_doAutoLAUNCH == false, make the dataViewer frame show()
if (!m_doAutoLAUNCH)
{
m_dataViewer->Show();
}
else
{
// enable Save button in data viewer after automatic (non-user-stepped) processing
m_dataViewer->ToggleBtnSaveAbility();
}
}
void FlipMain::OnFlipDataViewerBtnContinueProcessing(wxCommandEvent &event)
{
LogMessage("Stepped processing continuing. Current regex = " + wxString::Format("%i", m_currentRegex));
// boundary check
if (m_vec_pdfData.empty() || m_currentRegex >= m_regexList.size())
{
LogMessage("Nothing left to process or no regex remaining.");
return;
}
// if this is not the first processing run, copy the existing processed pdf data into the m_vec_pdfData vector
if (m_currentRegex > 0)
{
m_vec_pdfData = m_vec_pdfDataProcessed;
m_vec_pdfDataProcessed.clear();
}
auto i = 0; // page counter
// Loop through each entry in m_vec_pdfData
for (std::string pdfDataEntry : m_vec_pdfData)
{
wxString wxPdfDataEntry = wxString::FromUTF8(pdfDataEntry);
// read current regex => sub pair from m_regexList
const auto &pair_regex = m_regexList[m_currentRegex];
m_tempOutput = "Page " + wxString::Format("%i", i) + ": ";
// get the regex string as a wxRegEx object
wxRegEx regex(pair_regex.first);
// if the regex string is valid
if (regex.IsValid())
{
// replace data in the page as per regex and sub strings
if (regex.Replace(&wxPdfDataEntry, pair_regex.second))
{
m_tempOutput << " Applied regex: |" + pair_regex.first + "| => |" + pair_regex.second + "| on PDF data.";
}
else
{
m_tempOutput << "No match found for regex: |" + pair_regex.first + "| (quitting processing for this page)";
LogMessage(m_tempOutput);
break; // Exit if the regex match fails
}
}
else
{
m_tempOutput << "Invalid regex: |" + pair_regex.first + "|";
}
LogMessage(m_tempOutput);
// Convert wxString back to std::string after processing
pdfDataEntry = std::string(wxPdfDataEntry.ToUTF8());
// push the processed data into the m_vec_pdfDataProcessed vector
m_vec_pdfDataProcessed.push_back(pdfDataEntry);
i++;
}
// Increment the regex counter for the next press of the Continue processing button by the user
m_currentRegex++;
// Trigger Data Viewer to update data displayed in UI widgets
wxCommandEvent tripEvent(wxEVT_SPIN);
wxPostEvent(m_dataViewer.get(), tripEvent);
LogMessage("Stepped processing applied for one regex.");
// check if we have reached the end and disable/enable buttons as required
if (m_currentRegex >= m_regexList.size())
{
// Toggle button abilities in the data viewer
m_dataViewer->ToggleBtnContinueProcessingAbility();
m_dataViewer->ToggleBtnFinishProcessingAbility();
m_dataViewer->ToggleBtnSaveAbility();
}
}
void FlipMain::OnFlipDataViewerBtnFinishProcessing(wxCommandEvent &event)
{
// this method handles a button press from FlipDataViewer::m_btnContinueProcessing
// we also call this method from FlipMain::OnBtnLaunch to continue processing automatically
// this method will end the PDF data processing (it will carry out the regex substitutions on m_vec_pdfData[x])
if (m_vec_pdfData.size() <= 0)
{
// quit because nothing to do
return;
}
// Copy m_vec_pdfData to m_vec_pdfDataProcessed
m_vec_pdfDataProcessed = m_vec_pdfData;
auto i = 0; // page counter
// Loop through each entry in m_vec_pdfDataProcessed
for (std::string &pdfDataEntry : m_vec_pdfDataProcessed)
{
// Convert std::string to wxString for processing
wxString wxPdfDataEntry = wxString::FromUTF8(pdfDataEntry);
// Start processing from the current regex (m_currentRegex) until the end
for (size_t regexIdx = m_currentRegex; regexIdx < m_regexList.size(); ++regexIdx)
{
const auto &pair_regex = m_regexList[regexIdx];
m_tempOutput = "Page " + wxString::Format("%i", i) + ": ";
// Create the regex object for the current pair's first value (the regex pattern)
wxRegEx regex(pair_regex.first);
// Check if the regex is valid
if (regex.IsValid())
{
// Perform the substitution on the wxPdfDataEntry
if (regex.Replace(&wxPdfDataEntry, pair_regex.second))
{
m_tempOutput << " Applied regex: |" + pair_regex.first + "| => |" + pair_regex.second + "| on PDF data.";
}
else
{
// save failed page number in m_vec_pdfDataRektPages
m_vec_pdfDataRektPages.push_back(i);
m_tempOutput << "No match found for regex: |" + pair_regex.first + "| (quitting processing for this page)";
LogMessage(m_tempOutput);
break; // break out of the loop if a regex match fails (means the input data is not matching our template)
}
}
else
{
m_tempOutput << "Invalid regex: |" + pair_regex.first + "|";
}
LogMessage(m_tempOutput);
}
i++; // Move to the next page
// Convert wxString back to std::string after processing
pdfDataEntry = std::string(wxPdfDataEntry.ToUTF8());
}
// Reset the current regex index after processing is complete
m_currentRegex = 0;
// Trigger the wxEVT_SPIN event in m_dataViewer to make the widgets update
wxCommandEvent tripEvent(wxEVT_SPIN);
wxPostEvent(m_dataViewer.get(), tripEvent);
// Toggle button abilities in the data viewer
m_dataViewer->ToggleBtnContinueProcessingAbility();
m_dataViewer->ToggleBtnFinishProcessingAbility();
m_dataViewer->ToggleBtnSaveAbility();
LogMessage("Regex processing completed.");
if (m_doAutoLAUNCH)
{
wxCommandEvent tripEvent;
OnFlipDataViewerBtnSave(tripEvent);
}
}
void FlipMain::OnFlipDataViewerBtnSave(wxCommandEvent &event)
{
// the Save button was clicked in the Data Viewer frame, user wants to save the content from the dataviewer
// 1. Data Validation
// If no output file provided, use the default output path in the user's home directory
wxString defaultOutputFilePath = FLIP_USER_HOME_PATH + FLIP_DEFAULT_OUTPUT_FILENAME;
wxString outFile = m_outputFile->GetTextCtrlValue();
wxString outData = wxEmptyString;
wxRegEx blankLineRegex("\n[\\s\\n]*$");
if (outFile == wxEmptyString)
{
outFile = defaultOutputFilePath;
}
// m_vec_pdfDataProcessed contains the data we want
// 2. loop through m_vec_pdfDataProcessed and build all the entries into a single string for saving to disk
for (wxString pageData : m_vec_pdfDataProcessed)
{
// Check for and remove any blank lines (including lines with only whitespace)
if (blankLineRegex.Matches(pageData))
{
LogMessage("we found a blank line and are removing it");
blankLineRegex.Replace(&pageData, "\n", wxRE_ADVANCED); // Replace blank lines with a single newline
}
wxString appendChar = '\n';
if (!pageData.IsEmpty() && pageData.Last() == '\n')
{
appendChar = wxEmptyString;
}
outData << pageData << appendChar;
}
LogMessage("Data to save: |" + outData + "|");
// 3. Save the content to the file using wxTextFile
wxTextFile textFile;
// Try opening the file if it exists, or create a new one
if (wxFileExists(outFile))
{
if (!textFile.Open(outFile))
{
LogMessage("Failed to open file: " + outFile);
if (!m_doQuietMode)
{
wxMessageBox("Failed to open the output file: " + outFile, "Error", wxICON_ERROR);
}
return;
}
textFile.Clear(); // Clear the content if you're overwriting
}
else
{
if (!textFile.Create(outFile))
{
LogMessage("Failed to create file: " + outFile);
if (!m_doQuietMode)
{
wxMessageBox("Failed to create the output file: " + outFile, "Error", wxICON_ERROR);
}
return;
}
}
// 4. Write the data to the file
textFile.AddLine(outData);
if (textFile.Write())
{
LogMessage("Data successfully written to: " + outFile);
if (!m_doQuietMode)
{
wxMessageBox("File saved successfully!\n\nSaved to: " + outFile, "Success", wxICON_INFORMATION);
}
}
else
{
LogMessage("Failed to write data to file: " + outFile);
if (!m_doQuietMode)
{
wxMessageBox("Failed to write to the output file: " + outFile, "Error", wxICON_ERROR);
}
}
textFile.Close();
// end the program if we are in quiet mode (no GUI shown)
if (m_doQuietMode)
{
this->Close();
}
}
void FlipMain::OnQuit(wxCommandEvent &event)
{
Close(true);
}
void FlipMain::OnShowDataViewer(wxCommandEvent &event)
{
if (m_dataViewer)
{
LogMessage("Showing Data Viewer window");
m_dataViewer->Show(true);
}
else
{
LogMessage("Data Viewer object m_dataViewer does not exist.");
}
}
void FlipMain::OnShowProgramLog(wxCommandEvent &event)
{
if (m_programLog)
{
LogMessage("Showing Program Log window");
m_programLog->Show(true);
}
else
{
LogMessage("Program Log object m_programLog does not exist.");
}
}
void FlipMain::OnShowTemplateEditor(wxCommandEvent &event)
{
if (m_templateEditor)
{
LogMessage("Showing Template Editor window");
m_templateEditor->Show(true);
}
else
{
LogMessage("Template Editor object m_templateEditor does not exist.");
}
}
void FlipMain::OnSwitchDBPChecked(wxCommandEvent &event)
{
// Get the parent of the checkbox
wxWindow *parent = m_switchDBP->GetParent();
// Get the class name of the parent
wxString parentClassName = parent->GetClassInfo()->GetClassName();
// Log the class name to the Program Log
FlipProgramLog::LogMessage("Parent class name: " + parentClassName, *m_programLog);
// Continue with other logic if needed...
}
void FlipMain::OnTemplateFilePoll(wxTimerEvent &event)
{
// Get the current state of the files on disk using the refactored ReadUserTemplates
TemplateMap currentFileState = ReadUserTemplates();
// Compare currentFileState with m_tmap_userTemplates to detect changes
bool hasChanges = false;
// Check for deleted files (present in m_tmap_userTemplates but not in currentFileState)
for (const auto &pair : m_tmap_userTemplates)
{
// Normalize paths for comparison
wxFileName existingFile(pair.second);
wxString existingRelativePath = existingFile.GetFullPath();
if (currentFileState.find(pair.first) == currentFileState.end())
{
LogMessage("File deleted: " + pair.second);
hasChanges = true;
}
}
// Check for added files (present in currentFileState but not in m_tmap_userTemplates)
for (const auto &pair : currentFileState)
{
// Normalize paths for comparison
wxFileName newFile(pair.second);
wxString newRelativePath = newFile.GetFullPath();
if (m_tmap_userTemplates.find(pair.first) == m_tmap_userTemplates.end())
{
LogMessage("File added: " + pair.second);
hasChanges = true;
}
}
// If there were changes, update m_tmap_userTemplates and the wxChoice widgets
if (hasChanges)
{
LogMessage("File state of templates location has changed, updating template choices");
m_tmap_userTemplates = currentFileState;
UpdateTemplateChoices();
}
}
void FlipMain::OnUseTemplateChoice(wxCommandEvent &event)
{
// get a pointer to the object that triggered the OnChoice event
wxChoice *choice = static_cast<wxChoice *>(event.GetEventObject());
int selection = choice->GetSelection();
if (selection != wxNOT_FOUND)
{
// update the tooltip for the template wxChoice widget
choice->SetToolTip(m_tmap_userTemplates[choice->GetStringSelection()]);
LogMessage("User selected template: " + choice->GetStringSelection() + " (Full path: " + m_tmap_userTemplates[choice->GetStringSelection()] + ")");
}
event.Skip(); // Call this to allow other event handlers to process this event
}
TemplateMap FlipMain::ReadUserTemplates()
{
TemplateMap returnVals;
for (const wxString &path : m_configTemplateDirs)
{
wxArrayString files;
wxDir::GetAllFiles(path, &files, "*.*", wxDIR_FILES | wxDIR_DIRS);
for (const wxString &file : files)
{
wxFileName fullPath(file);
// Get the relative path from the base directory (m_configTemplateDirs item) to the file
wxString relativePath = fullPath.GetFullPath().Mid(path.length() + 1); // +1 to remove the extra slash
returnVals[relativePath] = fullPath.GetFullPath();
}
}
return returnVals;
}
void FlipMain::SetUseConsoleOutput(const bool v)
{
m_useConsoleOutput = v;
}
bool FlipMain::SetInputFilename(const wxString &filename)
{
m_inputFile->SetPath(filename);
return true;
}
bool FlipMain::SetOutputFilename(const wxString &filename)
{
m_outputFile->SetPath(filename);
return true;
}
bool FlipMain::SetSwitchAutoLAUNCH()
{
m_doAutoLAUNCH = true;
return true;
}
bool FlipMain::SetSwitchDBP()
{
m_switchDBP->SetValue(true);
return true;
}
bool FlipMain::SetSwitchSWS()
{
m_switchSWS->SetValue(true);
return true;
}
bool FlipMain::SetSwitchPages(const wxString &value)
{
m_ProcessPages->SetValue(value);
return true;
}
bool FlipMain::SetSwitchQuiet()
{
m_doQuietMode = true;
return true;
}
bool FlipMain::SetSwitchTemplateFile(const wxString &filename)
{
int index = m_useTemplate->Append(filename);
m_useTemplate->Select(index);
return true;
}
void FlipMain::SetupMenuIcons(wxMenu *menu)
{
// Define the folder containing your menu icons, set to the relative path from the executable location
wxString iconFolderPath = wxGetCwd() + RESOURCE_MENU_ICONS_PATH;
// change all becksleshes to sleshes
iconFolderPath.Replace("\\", "/");
// Ensure the directory exists and is accessible
wxDir iconDir(iconFolderPath);
if (!iconDir.IsOpened())
{
LogMessage("Failed to open directory: " + iconFolderPath);
return;
}
// Attempt to get the menu title; abandon process if unavailable
wxString parentMenuName = menu->GetTitle().Lower(); // Try to get the menu title
if (parentMenuName.IsEmpty())
{
m_programLog->LogMessage("Warning: Parent menu name could not be retrieved. Abandoning process.");