-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
1890 lines (1298 loc) · 47.5 KB
/
mainwindow.cpp
File metadata and controls
1890 lines (1298 loc) · 47.5 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 "mainwindow.h"
#include "ui_mainwindow.h"
#include "framelesswindow.h"
#include "generictablepage.h"
#include "coleditdlg.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <fstream>
#include "macros.hpp"
#include "mulcoldlg.h"
#include <QFileInfo>
#include <sstream>
#include <vector>
using namespace std;
using namespace ZDB;
#include "regioncontrol.h"
#define _Q(s) QString(s)
#define Status(sta) statusLabel->setText(sta)
QProgressBar* gpPgBar;
void pgMax(const int& in){
gpPgBar->setRange(0,in);
}
void PgPos(const int& pos){
if (gpPgBar->maximum() > pos )
gpPgBar->setValue(pos);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
loading = false;
/*
What you see in mainwindow.ui is a lie. The sizes are adjusted here.
The government is a lie. Life is a lie.
EVERYTHING IS A LIE
*/
ui->splitter->setSizes(QList<int>() << 350 << 200);
statusLabel = new QLabel(this);
statusProgressBar = new QProgressBar(this);
QLabel* versionLabel = new QLabel(this);
gpPgBar = statusProgressBar;
versionLabel->setText("V1.4B");
// set text for the label
statusLabel->setText("Loading to table..........");
statusLabel->setMinimumSize(QSize(125,12));
// make progress bar text invisible
statusProgressBar->setTextVisible(false);
// add the two controls to the status bar
ui->statusBar->addPermanentWidget(statusLabel);
ui->statusBar->addPermanentWidget(statusProgressBar,1);
ui->statusBar->addPermanentWidget(versionLabel);
Table = nullptr;
ui->statusBar->showMessage("Ready.");
statusLabel->setText("Loading to table..........");
statusLabel->setText("Ready.");
Database.SetPGMaxF(&pgMax);
Database.SetPGPosF(&PgPos);
treeTables = ui->treeTables;
m_StringTable.UpdateProg.Connect(this,&MainWindow::uprog);
m_StringTable.UpdateRangeMax.Connect(this,&MainWindow::umax);
pSearchWindow = make_pair((FramelessWindow*)NULL,(SearchWindow*)NULL);
twStrTb = nullptr;
CheckForAssoc();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::OpenFromArgs(const char *path)
{
if (!path)
return;
if (strlen(path) == 0)
return;
QString openfname(path);
statusLabel->setText("Loading file");
if (!Database.Init(openfname.toStdString())){
SError("Cannot open database file");
statusLabel->setText("Ready(error)");
return;
}
// Table->clear();
// OpenTables.clear();
// Qt's file dialogs and stuff give us paths delimited by forward slash '/'
// but command line arguments in Windows give us those delimited by backwards slash. That's why
// the split command is different than the one at the open file dialog.
QStringList lstPath = openfname.split("\\");
CurrentFileName = lstPath.at(lstPath.size() - 1);
CurrentFullFilePath = openfname;
BuildDatabaseTree();
statusLabel->setText("Ready");
PDelim = L"\\";
}
void MainWindow::on_actionOpen_triggered()
{
QString fnamex = QFileDialog::getOpenFileName(this, tr("Open Database"), QString(), tr("Database Files (*.gdb)"));
if (fnamex == "")
return;
statusLabel->setText("Loading file");
if (!Database.Init(fnamex.toStdString())){
SError("Cannot open database file");
statusLabel->setText("Ready(error)");
return;
}
ui->tabWidget->clear();
Table = nullptr;
OpenTables.clear();
QStringList lstPath = fnamex.split("/");
CurrentFileName = lstPath.at(lstPath.size() - 1);
CurrentFullFilePath = fnamex;
BuildDatabaseTree();
PDelim = L"/";
statusLabel->setText("Ready");
}
void MainWindow::BuildDatabaseTree()
{
treeTables->clear();
QTreeWidgetItem* dbfile = addTreeRoot(CurrentFileName,QIcon(":/ico/treeicons/database64.png"));
vector<GSString> tableNames = Database.TableNames();
vector<GSString>::iterator It = tableNames.begin();
while (It != tableNames.end())
{
addTreeChild(dbfile,QString::fromStdString(*It),QIcon(":/ico/treeicons/table64.png"));
++It;
}
dbfile->setExpanded(true);
ZStringDelimiter DelimFindPath(CurrentFullFilePath.toStdWString());
DelimFindPath.AddDelimiter(L"/");
if (!DelimFindPath.szTokens())
DelimFindPath.AddDelimiter(L"\\");
// If we use a string delimiter to reassemble the full file path excluding the last token we should get the directory the GDB
// is in so we can scan for gst files.
GString gdbfolder = DelimFindPath.Reassemble(L"\\", (int)(DelimFindPath.szTokens() - 1));
vector<SItemW> GstFiles = FileSys.StuffInDirectory(gdbfolder, L"*.gst");
if (!GstFiles.size())
return;
QTreeWidgetItem* tiStrs = addTreeRoot("String Tables",QIcon(":/ico/treeicons/strtabroot64.png"));
vector<SItemW>::iterator Its = GstFiles.begin();
while (Its != GstFiles.end())
{
addTreeChild(tiStrs,QString::fromStdWString(Its->Name),QIcon(":/ico/treeicons/strtabico64.png"));
++Its;
}
tiStrs->setExpanded(true);
}
void MainWindow::LoadTable(ZTable &tab)
{
Table->blockSignals(true);
ui->menuTable->setEnabled(true);
nullh.clear();
loading = true;
Table->clear();
statusLabel->setText("Preparing columns");
Table->setColumnCount((int)tab.Columns().size());
QStringList ColLabels;
for (size_t c = 0;c < tab.Columns().size();++c)
ColLabels << QString::fromStdWString(tab.Columns()[c].name);
Table->setHorizontalHeaderLabels(ColLabels);
Table->horizontalHeader()->setSectionsMovable(true);
statusLabel->setText("Building table");
const int rc = (int)tab.GetNItems() / tab.Columns().size();
Table->setRowCount(rc);
nullh.reserve(rc);
statusProgressBar->setRange(0,(int) tab.Columns()[0].vItems.size() * tab.Columns().size());
int prog = 0;
ColIt Itc = tab.Columns().begin();
int colid = 0;
bool appheader = (tab.GetName().find(L".gst") == GString::npos);
while (Itc != tab.Columns().end())
{
int itemid = 0;
ZItemIt Iti = Itc->vItems.begin();
while (Iti != Itc->vItems.end())
{
// statusProgressBar->setValue(prog);
Table->setItem(itemid,colid,new QTableWidgetItem(QString::fromStdWString(Iti->sData)));
if (colid == 0 && appheader)
nullh << " > ";
++prog;
++itemid;
++Iti;
}
++colid;
++Itc;
}
statusProgressBar->setValue(0);
if (tab.GetName().find(L".gst") == GString::npos)
Table->setVerticalHeaderLabels(nullh);
else
Table->setColumnWidth(1,(int)Table->size().width() * 0.9);
statusLabel->setText("Ready");
loading = false;
ctName = tab.GetName();
if (GetOpenTable(ctName) == -1)
OpenTables.push_back(tab);
Table->blockSignals(false);
}
void MainWindow::Save()
{
// Since we are saving to an already existing file, we only need to save the tables that we modified.
try {
SaveCTChanges();
vector<ZTable>::iterator ItA = OpenTables.begin();
int i = 0;
GString sta = L"Saving table " + GString(i) + L"/" + GString((int)OpenTables.size());
statusLabel->setText(QString::fromStdWString(sta));
while (ItA != OpenTables.end())
{
++i;
sta = L"Saving table " + GString(i) + L"/" + GString((int)OpenTables.size());
statusLabel->setText(QString::fromStdWString(sta));
DBError::DBError Err;
if (ItA->GetName().find(L".gst") == GString::npos) {
if (!ItA->Columns().size()){
++ItA;
continue;
}
try {
Err = Database.Save(*ItA);
}
catch(...) {
int r = QMessageBox::critical(this,"Error","Something went wrong while saving.\nWould you like to try again?",QMessageBox::StandardButton::Yes,QMessageBox::StandardButton::No);
if (r == QMessageBox::StandardButton::Yes)
Save();
else
break;
}
if (Err != DBError::err_no_error)
SError("Failed to save with error " + QString::number((int)Err));
}
else {
m_StringTable.Data() = StrEntryFromZT(*ItA);
ZStringDelimiter DelimFindPath(CurrentFullFilePath.toStdWString());
DelimFindPath.AddDelimiter(PDelim);
// If we use a string delimiter to reassemble the full file path excluding the last token we should get the directory the GDB
// is in so we can add the file name and get the table.
GString gstfile = DelimFindPath.Reassemble(L"\\", (int)(DelimFindPath.szTokens() - 1)) + L"\\" + ItA->GetName();
Status("Saving string table");
bool strs = m_StringTable.Save(gstfile);
if (!strs)
SError("Failed to save string table");
}
++ItA;
}
statusProgressBar->setValue(0);
statusLabel->setText("Ready");
}
catch(...) {
int r = QMessageBox::critical(this,"Error","Something went wrong while saving.\nWould you like to try again?",QMessageBox::StandardButton::Yes,QMessageBox::StandardButton::No);
if (r == QMessageBox::StandardButton::Yes)
Save();
else
return;
}
}
int MainWindow::GetOpenTable(const GString &tname)
{
if (!OpenTables.size())
return -1;
vector<ZTable>::iterator It = OpenTables.begin();
int r = 0;
while (It != OpenTables.end())
{
if (It->GetName() == tname)
return r;
++r;
++It;
}
// If we don't return up there, it means we got here, we haven't got anything.
return -1;
}
bool MainWindow::GetTable(const GString &TableName, ZTable &table_out)
{
// Check if this table is open, otherwise we just ask the database for it.
GString tbnam = TableName;
size_t startpos = TableName.find_first_of((wchar_t)0x20);
if (GString::npos != startpos && TableName.find(L".gst") == GString::npos)
{
tbnam = TableName.substr(0, startpos);
}
int oti = GetOpenTable(tbnam);
if (oti != -1) {
// set the table
const int fres = findTab(QString::fromStdWString(tbnam));
if (fres != -1){
GenericTablePage* ptp = (GenericTablePage*)ui->tabWidget->widget(findTab(QString::fromStdWString(tbnam) ));
ui->tabWidget->setCurrentIndex(findTab(QString::fromStdWString(tbnam)));
Table = ptp->pTable;
}else{
GenericTablePage* npage = new GenericTablePage(this);
const int dex = ui->tabWidget->addTab(npage,QIcon(":/ico/treeicons/table64.png"),QString::fromStdWString(tbnam));
GenericTablePage* ptp = (GenericTablePage*)ui->tabWidget->widget(dex);
ui->tabWidget->setCurrentIndex(dex);
Table = ptp->pTable;
npage->pDBTable = &OpenTables[oti];
npage->mDBTable = OpenTables[oti];
}
table_out = OpenTables[oti];
return true;
}
//table_out.SetName(tbnam);
if (tbnam.find(L".gst") != wstring::npos)
{
Status("Loading string table");
ZStringDelimiter DelimFindPath(CurrentFullFilePath.toStdWString());
DelimFindPath.AddDelimiter(L"/");
// Just in case
if (!DelimFindPath.szTokens())
DelimFindPath.AddDelimiter(L"\\");
// If we use a string delimiter to reassemble the full file path excluding the last token we should get the directory the GDB
// is in so we can add the file name and get the table.
GString gstfile = DelimFindPath.Reassemble(L"\\", (int)(DelimFindPath.szTokens() - 1) ) + L"\\" + tbnam;
m_StringTable.Load(gstfile);
table_out = ZTFromStrEntry(tbnam,m_StringTable.Data());
GenericTablePage* npage = new GenericTablePage(this);
const int dex = ui->tabWidget->addTab(npage,QIcon(":/ico/treeicons/strtabico64.png"),QString::fromStdWString(tbnam));
GenericTablePage* ptp = (GenericTablePage*)ui->tabWidget->widget(dex);
ui->tabWidget->setCurrentIndex(dex);
npage->pDBTable = &table_out;
npage->mDBTable = table_out;
Table = ptp->pTable;
return true;
}
// Add a new tab page if we haven't found it in the open tables.
table_out = Database.GetZTable(GSString(TableName));
GenericTablePage* npage = new GenericTablePage(this);
const int dex = ui->tabWidget->addTab(npage,QIcon(":/ico/treeicons/table64.png"),QString::fromStdWString(tbnam));
GenericTablePage* ptp = (GenericTablePage*)ui->tabWidget->widget(dex);
ui->tabWidget->setCurrentIndex(dex);
Table = ptp->pTable;
ptp->pDBTable = &table_out;
ptp->mDBTable = table_out;
return true;
}
ZTable &MainWindow::GetCurrentTable()
{
qDebug() << "getcurrenttable get table named " << QString::fromStdWString(OpenTables[GetOpenTable(ctName)].GetName());
return OpenTables[GetOpenTable(ctName)];
}
void MainWindow::uprog(int in)
{
statusProgressBar->setValue(in);
}
void MainWindow::umax(int in)
{
statusProgressBar->setRange(0,in);
}
void MainWindow::ResetEntry(QTableWidgetItem *pItem)
{
}
void MainWindow::SaveCTChanges()
{
if (OpenTables.empty())
return;
if (Table == nullptr)
return;
statusProgressBar->setRange(0,Table->columnCount() * Table->rowCount());
statusLabel->setText("Caching table");
// Assemble the whole table from the widget
qDebug() << QString::fromStdWString(ctName);
ZTable asmt;
asmt.SetName(ctName,false);
int pval = 0;
const int rows = Table->rowCount();
for (int col = 0;col < Table->columnCount();++col)
{
ZColumn ACol(Table->horizontalHeaderItem(col)->text().toStdWString(),(size_t)col,GetCurrentTable().Columns()[col].dataType);
ACol.vItems.reserve(rows);
for (int row = 0; row < rows;++row)
{
// statusProgressBar->setValue(pval);
QTableWidgetItem* pItem = Table->item(row,col);
std::wstring MStr = L"";
if (pItem){
MStr = pItem->text().toStdWString();
}
else{
SError("Empty item at row " + QString::number(row) + _Q(" col: ") + QString::number(col));
return;
}
if (ACol.dataType == EDataType::e_Double){
double num = ZTUtil::IStrStreamConv<double>(MStr);
wostringstream ss;
ss << num;
MStr = ss.str();
}
if (ACol.dataType == EDataType::e_Decimal || ACol.dataType == EDataType::e_Numeric){
INT64 num = ZTUtil::IStrStreamConv<INT64>(MStr);
wostringstream ss;
ss << num;
MStr = ss.str();
}
if (ACol.dataType == EDataType::e_Float){
float num = ZTUtil::IStrStreamConv<float>(MStr);
wostringstream ss;
ss << num;
MStr = ss.str();
}
ZItem addit(MStr,(size_t)row);
ACol.vItems.push_back(addit);
++pval;
}
asmt.Columns().push_back(ACol);
}
const int ftres = findTab(QString::fromStdWString(ctName));
if (ftres != -1){
GenericTablePage* gtp = (GenericTablePage*)ui->tabWidget->widget(ftres);
gtp->mDBTable = asmt;
}
GetCurrentTable() = asmt;
statusProgressBar->setValue(0);
statusLabel->setText("Ready");
}
QTreeWidgetItem *MainWindow::addTreeRoot(QString name, QIcon ico)
{
QTreeWidgetItem *treeItem = new QTreeWidgetItem(treeTables);
treeItem->setText(0,name);
treeItem->setIcon(0,ico);
return treeItem;
}
QTreeWidgetItem *MainWindow::addTreeChild(QTreeWidgetItem *parent, QString name, QIcon ico)
{
QTreeWidgetItem *treeItemc = new QTreeWidgetItem();
treeItemc->setText(0,name.trimmed());
treeItemc->setIcon(0,ico);
parent->addChild(treeItemc);
return treeItemc;
}
void MainWindow::SError(const QString &caption, const QString &title)
{
//FramelessWindow fw;
// QIcon icon(":/icons/res/zico.ico");
//framelessWindow.setWindowIcon(icon);
//fw.setWindowTitle(title);
QMessageBox msgBox;
// msgBox.setStyle(ui->centralWidget->style());
msgBox.setWindowTitle(title);
msgBox.setText(caption);
msgBox.setIcon(QMessageBox::Critical);
msgBox.exec();
}
void MainWindow::on_treeTables_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
// Prevent the event from being triggered while it's loading, causing crashs;
treeTables->blockSignals(true);
if (loading)
return;
if (item->childCount() == 0)
{
loading = true;
ZTable gTab;
GetTable(item->text(column).toStdString(),gTab);
LoadTable(gTab);
loading = false;
}
treeTables->blockSignals(false);
}
void MainWindow::on_actionSave_triggered()
{
Save();
}
void MainWindow::on_actionAdd_triggered()
{
Table->blockSignals(true);
Table->insertRow(Table->rowCount());
nullh << " > ";
Table->setVerticalHeaderLabels(nullh);
QTableWidgetItem* dpItem = Table->item(Table->rowCount() - 1,0);
Table->scrollToItem(dpItem);
Table->selectRow(Table->rowCount() - 1);
if (GetCurrentTable().GetName().find(L".gst") != GString::npos){
Table->setItem(Table->rowCount() - 1,0,new QTableWidgetItem(QString::number(maxstrid + 1)));
++maxstrid;
}
Table->blockSignals(false);
}
void MainWindow::on_actionDuplicate_triggered()
{
QList<QTableWidgetItem*> seli = Table->selectedItems();
if (!seli.size())
return;
Table->insertRow(Table->rowCount());
const int at = Table->rowCount() - 1;
for (int acol = 0;acol < Table->columnCount();++acol)
{
// Set across all columns our item.
Table->setItem(at,acol,new QTableWidgetItem(*Table->item(seli[0]->row(),acol)));
}
QTableWidgetItem* dpItem = Table->item(at,0);
Table->scrollToItem(dpItem);
Table->selectRow(at);
nullh << " > ";
Table->setVerticalHeaderLabels(nullh);
}
void MainWindow::on_actionRemove_triggered()
{
QList<QTableWidgetItem*> seli = Table->selectedItems();
QList<QTableWidgetItem*>::iterator It = seli.begin();
while (It != seli.end())
{
QTableWidgetItem* item = *It;
Table->removeRow(item->row());
++It;
}
}
int MainWindow::findTab(const QString &name)
{
for (int i = 0;i < ui->tabWidget->count();++i){
if (name == ui->tabWidget->tabText(i))
return i;
}
return -1;
}
void MainWindow::on_tabWidget_currentChanged(int index)
{
GenericTablePage* ptp = (GenericTablePage*)ui->tabWidget->widget(index);
if (!ptp)
return;
SaveCTChanges();
// Set the table we're editing.
ctName = ui->tabWidget->tabText(index).toStdWString();
Table = ptp->pTable;
if (ui->tabWidget->tabText(index).contains(".gst"))
ui->tabWidget->setTabIcon(index,QIcon(":/ico/treeicons/strtabico64.png"));
}
void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
GenericTablePage* gtpDestroy = (GenericTablePage*)ui->tabWidget->widget(index);
ui->tabWidget->removeTab(index);
delete gtpDestroy;
gtpDestroy = nullptr;
if (ui->tabWidget->count() == 0)
ui->menuTable->setEnabled(false);
Table = nullptr;
}
ZTable MainWindow::ZTFromStrEntry(const GString &strename, vector<StrEntry> &strev)
{
Status("Prepare strings");
std::vector<StrEntry>::iterator Set = strev.begin();
ZTable ZTStringTab;
ZTStringTab.SetName(strename,false);
ZColumn ColID(L"ID", 0, EDataType::e_Int);
ZColumn ColStr(L"String", 1, EDataType::e_Varchar);
ZTStringTab.Columns().push_back(ColID);
ZTStringTab.Columns().push_back(ColStr);
ZTStringTab.Columns()[0].Items().reserve(strev.size());
ZTStringTab.Columns()[1].Items().reserve(strev.size());
statusProgressBar->setRange(0, (int)strev.size());
size_t rid = 0;
int lmid = 0;
while (Set != strev.end())
{
ZItem ItID(GString(Set->ID), rid);
ZItem ItStr(Set->GetString(), rid);
ZTStringTab.Columns()[0].Items().push_back(ItID);
ZTStringTab.Columns()[1].Items().push_back(ItStr);
// statusProgressBar->setValue((int)rid);
if (Set->ID > lmid)
lmid = Set->ID;
++rid;
++Set;
}
maxstrid = lmid;
return ZTStringTab;
}
vector<StrEntry> MainWindow::StrEntryFromZT(ZTable &ztstr)
{
Status("Pre-string saving");
vector<StrEntry> Strer;
Strer.reserve(ztstr.Columns()[0].Items().size());
MI(ztstr.Columns()[0].Items(), CSit);
size_t x = 0;
statusProgressBar->setRange(0, (int)ztstr.Columns()[0].Items().size());
while (CSit != ztstr.Columns()[0].Items().end())
{
StrEntry EnA(CSit->sData.ToINT32(), ztstr.Columns()[1].Items()[x].sData);
Strer.push_back(EnA);
// statusProgressBar->setValue((int)x);
++x;
++CSit;
}
return Strer;
}
void MainWindow::SetControls(bool ctrls)
{
QList<QWidget *> buttonsList = this->findChildren<QWidget *>();
for (int i = 0; i < buttonsList.count(); i++){
buttonsList.at(i)->setEnabled(ctrls);
}
}
#pragma comment(lib,"Advapi32.lib")
BOOL IsElevated() {
BOOL fRet = FALSE;
HANDLE hToken = NULL;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
TOKEN_ELEVATION Elevation;
DWORD cbSize = sizeof(TOKEN_ELEVATION);
if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize)) {
fRet = Elevation.TokenIsElevated;
}
}
if (hToken) {
CloseHandle(hToken);
}
return fRet;
}
bool MainWindow::Associate()
{
if (!IsElevated()){
SError("Cannot associate: Need administrator privileges.\nPlease restart as administrator and try again.");
return false;
}
QString exepath = QCoreApplication::applicationDirPath();
exepath.replace("/","\\");
QString exef = QCoreApplication::applicationFilePath();
exef.replace("/","\\");
// Make the assoc command
string asocmd = "assoc .gdb=zrsgdbf";
// Make the ftype command which makes it send it to this program with the first argument
string ftypecmd = string("ftype zrsgdbf=") + SQUOTE + exef.toStdString() + SQUOTE + " " + SQUOTE + "%1" + SQUOTE;
// Make a registry add command to set that file's default icon to be the one we've got here
string fticocmd = "reg add hkcr\\zrsgdbf\\DefaultIcon /f /ve /d " + std::string(SQUOTE) + exepath.toStdString() + "\\dbf.ico" + SQUOTE;
system(asocmd.c_str());
system(ftypecmd.c_str());
system(fticocmd.c_str());
std::ofstream msglog;
msglog.open("reglog.txt");
msglog << asocmd << '\n';
msglog << ftypecmd << '\n';
msglog << fticocmd << '\n';
msglog.close();
std::ofstream ofsAs;
ofsAs.open(QString(exepath + "\\assoc.bin").toStdWString());
ofsAs << "1";
ofsAs.close();
return true;
}
bool fileExists(QString path) {
QFileInfo check_file(path);
// check if file exists and if yes: Is it really a file and no directory?
if (check_file.exists() && check_file.isFile()) {
return true;
} else {
return false;
}
}
void MainWindow::CheckForAssoc()
{
QString exepath = QCoreApplication::applicationDirPath();
exepath.replace("/","\\");
QString fpath = QString(exepath + "\\assoc.bin");
if (fileExists(fpath))
return;
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Associate with files", "ZDEditorRS does not appear to be currently associated with the .gdb file format.\n Would you like to do that now?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
Associate();
} else {
std::ofstream ofsAs;
ofsAs.open(fpath.toStdWString());
ofsAs << "1";
ofsAs.close();
}
}
void MainWindow::on_actionEdit_Columns_triggered()
{
if (!OpenTables.size())
return;
IF_ERROR(GetCurrentTable().GetName().find(L".gst") != GString::npos,"Cannot edit the columns of a string table.");
SetControls(false);
// Set our editing
FramelessWindow FDlg(this);
FDlg.setWindowIcon(QIcon(":/acticons/coledit64.png"));
FDlg.setWindowTitle("Edit Columns");
ColEditDlg dlg(this);
dlg.m_Table = GetCurrentTable();
dlg.pFwParent = &FDlg;
FDlg.setContent(&dlg);
FDlg.ContentDlg(&dlg);
FDlg.show();
// block
dlg.setModal(true);
if (dlg.exec() != QDialog::Accepted){
SetControls(true);
return;
}
SetControls(true);
GetCurrentTable() = dlg.m_Table;
LoadTable(dlg.m_Table);
const int ftres = findTab(QString::fromStdWString(dlg.m_Table.GetName()));
if (ftres != -1){
GenericTablePage* gtp = (GenericTablePage*)ui->tabWidget->widget(ftres);
gtp->mDBTable = dlg.m_Table;
}
return;
}
void MainWindow::on_actionSearch_triggered()
{
AIF_RETURN(!OpenTables.size());
QString Tname = QString::fromStdWString(GetCurrentTable().GetName());
// FTR: Find Table Result
const int ftr = findTab(Tname);
AIF_RETURN(ftr == -1);
/*
if (get<0>(pSearchWindow) != NULL){
FramelessWindow* pFw = get<0>(pSearchWindow);
if (pFw->isVisible())
pFw->close();
pSearchWindow = make_pair((FramelessWindow*)NULL,(SearchWindow*)NULL);
};
*/
// We create these windows with new, as this function doesn't block
// and exits immediately, destroying them if they're allocated on the stack.