forked from glauberlima/TabelasProtheus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnit1.pas
618 lines (487 loc) · 14.5 KB
/
Unit1.pas
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
unit Unit1;
interface
uses
Windows, Forms, ShellAPI, ImgList, Controls, XPMan, Menus, ApoEnv, DB, ApoDSet, SysUtils,
StdCtrls, Buttons, Graphics, ExtCtrls, Grids, DBGrids, ComCtrls, Classes, Messages, IniFiles;
const
WM_ICONTRAY = WM_USER + 1;
type
TfrmMain = class(TForm)
sbar: TStatusBar;
btFechar: TButton;
PageControl: TPageControl;
TabSheet1: TTabSheet;
DBGridX2: TDBGrid;
TabSheet2: TTabSheet;
DBGridX3: TDBGrid;
TabSheet3: TTabSheet;
DBGrid3: TDBGrid;
btSobre: TButton;
Panel1: TPanel;
btIR: TBitBtn;
edValor: TEdit;
cbxOp: TComboBox;
cbxCampo: TComboBox;
Image1: TImage;
btConfig: TButton;
dsSX2: TDataSource;
tbSX2: TApolloTable;
tbSX3: TApolloTable;
dsSX3: TDataSource;
dsSIX: TDataSource;
tbSIX: TApolloTable;
ApolloEnv1: TApolloEnv;
PopupMenu1: TPopupMenu;
ExibirOcultar1: TMenuItem;
Sobre1: TMenuItem;
N1: TMenuItem;
Sair1: TMenuItem;
XPManifest1: TXPManifest;
ImageList1: TImageList;
tbSX2X2_CHAVE: TStringField;
tbSX2X2_PATH: TStringField;
tbSX2X2_ARQUIVO: TStringField;
tbSX2X2_NOME: TStringField;
tbSX2X2_MODO: TStringField;
tbSX2_X2_MODO: TStringField;
tbSX3X3_ARQUIVO: TStringField;
tbSX3X3_TITULO: TStringField;
tbSX3X3_ORDEM: TStringField;
tbSX3X3_CAMPO: TStringField;
tbSX3X3_TIPO: TStringField;
tbSX3X3_TAMANHO: TSmallintField;
tbSX3X3_DECIMAL: TSmallintField;
tbSX3X3_DESCRIC: TStringField;
tbSX3X3_PICTURE: TStringField;
tbSX3X3_VALID: TStringField;
tbSX3X3_F3: TStringField;
tbSX3X3_VLDUSER: TStringField;
tbSX3X3_CONTEXT: TStringField;
tbSX3_X3_CONTEXTO: TStringField;
tbSX3X3_CBOX: TStringField;
tbSX3X3_RELACAO: TStringField;
tbSX3X3_INIBRW: TStringField;
tbSX3X3_WHEN: TStringField;
tbSIXORDEM: TStringField;
tbSIXCHAVE: TStringField;
tbSIXDESCRICAO: TStringField;
procedure cbxCampoChange(Sender: TObject);
procedure cbxOpChange(Sender: TObject);
procedure btIRClick(Sender: TObject);
procedure btFecharClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure edValorKeyPress(Sender: TObject; var Key: Char);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Sobre1Click(Sender: TObject);
procedure Sair1Click(Sender: TObject);
procedure tbSX2AfterOpen(DataSet: TDataSet);
procedure tbSX2AfterScroll(DataSet: TDataSet);
procedure PopupMenu1Popup(Sender: TObject);
procedure ExibirOcultar1Click(Sender: TObject);
procedure btConfigClick(Sender: TObject);
procedure btSobreClick(Sender: TObject);
procedure tbSX3CalcFields(DataSet: TDataSet);
procedure tbSX2CalcFields(DataSet: TDataSet);
procedure DBGridX3DblClick(Sender: TObject);
procedure DBGrid3DblClick(Sender: TObject);
procedure tbSX3AfterScroll(DataSet: TDataSet);
private
{ Private declarations }
TrayIconData: TNotifyIconData;
procedure ShowTrayIcon(mostrar : boolean);
procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;
procedure ConfigINI(flag : byte);
function TabelasOK : boolean;
procedure InicializaTabelas;
procedure FinalizaTabelas;
procedure ShowConfig;
procedure ShowSobre;
procedure ShowDetSX3(tabela : string);
procedure ShowDetSIX(tabela : string);
public
procedure ShowIndexar;
procedure Indexar(tabela : TApolloTable ; expr : string ; ForceReindex : boolean);
{ Public declarations }
end;
var
frmMain : TfrmMain;
campo : string;
op : string;
valor : string;
// Variáveis globais que guardam as configurações do INI
X2_NAME : string;
X3_NAME : string;
IX_NAME : string;
PATH : string;
USESINDEX : byte;
implementation
uses Unit2, Unit3, Unit4, Unit6, Unit5;
{$R *.dfm}
// Reindexa as tabelas, para melhorar a velocidade do acesso
procedure TFrmMain.Indexar(tabela : TApolloTable ; expr : string ; ForceReindex : boolean);
var
idx : integer;
nome_idx : string;
begin
with tabela do begin
CloseIndexes;
// nome do índice
nome_idx := ExtractFilePath(Application.ExeName) + ChangeFileExt(TableName, '.NTX');
// se o parâmetro ForceReindex for True, então apaga o índice atual, para forçar
// uma reindexação
if ForceReindex then DeleteFile(nome_idx);
try
idx := IndexOpen(nome_idx); // tenta abrir o índice...
except
on EApolloError do begin // se não conseguir, cria um novo
frmIndexar := TfrmIndexar.Create(self);
with frmIndexar do begin
lbTabela.Caption := TableName;
Show;
Update;
end;
// cria o novo índice
idx := Index(nome_idx, expr, IDX_UNIQUE, False, '');
FreeAndNil(frmIndexar);
end;
end;
SetOrder(idx); // ativa o índice
end;
end;
// Exibe/oculta o ícone da bandeja
procedure TfrmMain.ShowTrayIcon(mostrar : boolean);
begin
if mostrar then begin // mostra o ícone
with TrayIconData do
begin
cbSize := SizeOf(TrayIconData);
Wnd := Handle;
uID := 0;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_ICONTRAY;
hIcon := Application.Icon.Handle;
StrPCopy(szTip, Application.Title);
end;
Shell_NotifyIcon(NIM_ADD, @TrayIconData);
end
else // esconde o ícone
Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
end;
// Finaliza corretamente as tabelas
procedure TfrmMain.FinalizaTabelas;
begin
tbSX2.Close;
tbSIX.Close;
tbSX3.Close;
end;
// Inicializa corretamente as tabelas
procedure TfrmMain.InicializaTabelas;
begin
// Seta o diretórios das tabelas
tbSX2.DatabaseName := PATH;
tbSX3.DatabaseName := PATH;
tbSIX.DatabaseName := PATH;
// Seta os nomes das tabelas
tbSX2.TableName := X2_NAME;
tbSX3.TableName := X3_NAME;
tbSIX.TableName := IX_NAME;
// Abre as tabelas
tbSX2.Open;
tbSX3.Open;
tbSIX.Open;
// Reindexa as tabelas, se necessário
Indexar(tbSX2, 'X2_CHAVE', False);
Indexar(tbSX3, 'X3_ARQUIVO+X3_ORDEM', False);
Indexar(tbSIX, 'INDICE+ORDEM', False);
end;
// Checa se as tabelas necessárias existem
function TfrmMain.TabelasOK : boolean;
var X2_PATH, X3_PATH, IX_PATH : string;
begin
X2_PATH := PATH + '\' + X2_NAME; // Caminho completo do SX2
X3_PATH := PATH + '\' + X3_NAME; // Caminho completo do SX3
IX_PATH := PATH + '\' + IX_NAME; // Caminho completo do SIX
if (not FileExists(X2_PATH)) or (not FileExists(X3_PATH)) or (not FileExists(IX_PATH)) then begin
messagebox(0,PChar('Configurações incorretas. Clique em OK para entrar no modo de configuração e corrigir o erro.'), 'Aviso', MB_ICONERROR+MB_TASKMODAL);
Result := False;
end
else
Result := True;
end;
// Exibe a tela Reindexar
procedure TfrmMain.ShowIndexar;
begin
try
frmIndexar := TfrmIndexar.Create(self);
frmIndexar.ShowModal;
finally
FreeAndNil(frmIndexar);
end;
end;
// Exibe a tela de sobre
procedure TfrmMain.ShowSobre;
begin
try
frmSobre := TfrmSobre.Create(self);
frmSobre.ShowModal;
finally
FreeAndNil(frmSobre);
end;
end;
// Exibe a tela de detalhes dos índices (SIX)
procedure TfrmMain.ShowDetSIX(tabela : string);
begin
try
frmDetSIX := TfrmDetSIX.Create(self);
frmDetSIX.sbar.SimpleText := 'Tabela: ' + tabela;
frmDetSIX.ShowModal;
finally
FreeAndNil(frmDetSIX);
end;
end;
// Exibe a tela de detalhes dos campos (SX3)
procedure TfrmMain.ShowDetSX3(tabela : string);
begin
try
frmDetSX3 := TfrmDetSX3.Create(self);
frmDetSX3.sbar.SimpleText := 'Tabela: ' + tabela;
frmDetSX3.ShowModal;
finally
FreeAndNil(frmDetSX3);
end;
end;
// Exibe a tela de configurações
procedure TfrmMain.ShowConfig;
begin
try
frmConfig := TfrmConfig.Create(self);
frmConfig.ShowModal;
finally
FreeAndNil(frmConfig);
end;
end;
// Captura os cliques dados no trayicon do programa
procedure TfrmMain.TrayMessage(var Msg: TMessage);
var
p : TPoint;
begin
case Msg.lParam of
WM_LBUTTONDBLCLK:
begin
ExibirOcultar1Click(nil);
end;
WM_RBUTTONDOWN:
begin
SetForegroundWindow(Handle);
GetCursorPos(p);
PopUpMenu1.Popup(p.x-85, p.y);
PostMessage(Handle, WM_NULL, 0, 0);
end;
end;
end;
// Lê/Grava o arquivo INI
procedure TfrmMain.ConfigINI(flag : byte);
var
arq_ini : string;
ini : TIniFile;
begin
// flag = 1 -> Ler
// flag != 1 -> Gravar
// Nome do arquivo INI, obtido automaticamente do .EXE
arq_ini := ExtractFilePath(Application.ExeName) + ChangeFileExt(ExtractFileName(Application.ExeName),'.ini');
ini := TIniFile.Create(arq_ini);
if(flag=1) then begin
// Pega os nomes da SX2, SX3 e SIX, PATH e UseSindex do arquivo INI
X2_NAME := ini.ReadString('Config','X2Name','SX2010.DBF');
X3_NAME := ini.ReadString('Config','X3Name','SX3010.DBF');
IX_NAME := ini.ReadString('Config','IXName','SIX010.DBF');
PATH := ini.ReadString('Config','Path','.');
USESINDEX := ini.ReadInteger('Config','UseSindex',0);
end
else begin
ini.WriteString('Config','X2Name',X2_NAME);
ini.WriteString('Config','X3Name',X3_NAME);
ini.WriteString('Config','IXName',IX_NAME);
ini.WriteString('Config','Path',PATH);
ini.WriteInteger('Config','UseSindex',USESINDEX);
end;
FreeAndNil(ini);
end;
procedure TfrmMain.cbxCampoChange(Sender: TObject);
begin
campo := cbxCampo.Text;
end;
procedure TfrmMain.cbxOpChange(Sender: TObject);
begin
op := cbxOp.Text;
end;
// Ao clicar no botão Localizar
procedure TfrmMain.btIRClick(Sender: TObject);
var
vrT1 : string;
vrT2 : string;
begin
// Ativa a primeira página: Tabelas
PageControl.ActivePageIndex := 0;
vrT1 := UpperCase(QuotedStr(edValor.Text + '%'));
vrT2 := UpperCase(QuotedStr('%' + edValor.Text + '%'));
if (campo = 'Sigla') and (op = 'começa com') then
//tbSX2.Filter := 'UPPER(X2_CHAVE) LIKE ' + vrT1
tbSX2.Query('UPPER(X2_CHAVE) LIKE ' + vrT1)
else if (campo = 'Sigla') and (op = 'contém') then
//tbSX2.Filter := 'UPPER(X2_CHAVE) LIKE ' + vrT2
tbSX2.Query('UPPER(X2_CHAVE) LIKE ' + vrT2)
else if (campo = 'Nome') and (op = 'começa com') then
//tbSX2.Filter := 'UPPER(X2_NOME) LIKE ' + vrT1
tbSX2.Query('UPPER(X2_NOME) LIKE ' + vrT1)
else if (campo = 'Nome') and (op = 'contém') then
//tbSX2.Filter := 'UPPER(X2_NOME) LIKE ' + vrT2
tbSX2.Query('UPPER(X2_NOME) LIKE ' + vrT2)
end;
// Ao clicar no botão Fechar
procedure TfrmMain.btFecharClick(Sender: TObject);
begin
Application.Terminate;
end;
// Ao ativar o formulário principal
procedure TfrmMain.FormActivate(Sender: TObject);
begin
// Configura os hints para 5 segundos
Application.HintHidePause := 5000;
frmMain.Caption := Application.Title;
sbar.Panels[1].Text := Format('Caminho SIGAADV: %s',[PATH]);
campo := 'Sigla';
op := 'começa com';
edValor.SetFocus;
end;
// Ao pressionar alguma tecla dentro do campo Valor
procedure TfrmMain.edValorKeyPress(Sender: TObject; var Key: Char);
begin
if key = #13 then btIRClick(Sender);
end;
// Ao criar o formulário (ocorre antes do Ativar)
procedure TfrmMain.FormCreate(Sender: TObject);
begin
// Lê os dados do INI
ConfigINI(1);
// Verifica se as tabelas estão OK
if TabelasOK then begin
InicializaTabelas;
ShowTrayIcon(true);
end
else begin // opa! Não estão Ok... abrindo configurações
frmMain.WindowState := WsMinimized;
frmMain.Hide;
ShowConfig;
Application.Terminate;
end;
end;
// Ao destruir o formulário
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
// Grava o INI
ConfigINI(2);
// Fecha as tabelas
FinalizaTabelas;
// Remove o ícone da bandeja
ShowTrayIcon(false);
end;
procedure TfrmMain.Sobre1Click(Sender: TObject);
begin
btSobreClick(Sender);
end;
procedure TfrmMain.Sair1Click(Sender: TObject);
begin
btFecharClick(Sender);
end;
procedure TfrmMain.tbSX2AfterOpen(DataSet: TDataSet);
begin
sbar.Panels[0].Text := Format('%d tabela(s) encontrada(s)',[DataSet.RecordCount]);
end;
procedure TfrmMain.tbSX2AfterScroll(DataSet: TDataSet);
var x2_chave, n1, n2, n3 : string;
begin
x2_chave := QuotedStr(tbSX2.FieldByName('X2_CHAVE').AsString);
//tbSX3.Filter := 'X3_ARQUIVO = ' + x2_chave;
tbSX3.Query('X3_ARQUIVO = ' + x2_chave);
//tbSIX.Filter := 'INDICE = ' + x2_chave;
tbSIX.Query('INDICE = ' + x2_chave);
n1 := Application.Title;
n2 := Trim(DataSet.FieldByName('x2_chave').AsString);
n3 := Trim(DataSet.FieldByName('x2_nome').AsString);
// Define o título do formulário: mostra ou não o nome da tabela
// atual junto com o título do form
if DataSet.RecordCount = 0 then begin
frmMain.Caption := Format('%s - [Nenhuma tabela encontrada]', [Application.Title]);
PageControl.ActivePageIndex := 0;
PageControl.Pages[1].TabVisible := False;
PageControl.Pages[2].TabVisible := False;
end
else begin
frmMain.Caption := Format('%s - [%s - %s]',[n1, n2, n3]);
PageControl.Pages[1].TabVisible := True;
PageControl.Pages[2].TabVisible := True;
end;
tbSX2AfterOpen(DataSet);
end;
procedure TfrmMain.PopupMenu1Popup(Sender: TObject);
begin
if frmMain.Visible then
ExibirOcultar1.Caption := '&Ocultar'
else
ExibirOcultar1.Caption := '&Exibir';
end;
procedure TfrmMain.ExibirOcultar1Click(Sender: TObject);
begin
frmMain.Visible := not frmMain.Visible;
end;
procedure TfrmMain.btConfigClick(Sender: TObject);
begin
ShowConfig;
end;
procedure TfrmMain.btSobreClick(Sender: TObject);
begin
ShowSobre;
end;
procedure TfrmMain.tbSX3CalcFields(DataSet: TDataSet);
var x3_contexto_valor : string;
begin
// Calcula o valor do campo virtual _X3_CONTEXTO
if tbSX3.FieldByName('X3_CONTEXT').AsString = 'V' then
x3_contexto_valor := 'Virtual'
else
x3_contexto_valor := 'Real';
// Seta os valores dos campos virtuais
tbSX3.FieldByName('_X3_CONTEXTO').AsString := x3_contexto_valor;
end;
procedure TfrmMain.tbSX2CalcFields(DataSet: TDataSet);
begin
// Cálculo do campo fórmula _X2_MODO
if tbSX2.FieldByName('X2_MODO').AsString = 'C' then
tbSX2.FieldByName('_X2_MODO').AsString := 'Compartilhado'
else
tbSX2.FieldByName('_X2_MODO').AsString := 'Exclusivo';
end;
procedure TfrmMain.DBGridX3DblClick(Sender: TObject);
var tabela : string;
begin
tabela := tbSX2.FieldByName('X2_CHAVE').AsString + ' - ' +
tbSX2.FieldByName('X2_NOME').AsString;
ShowDetSX3(tabela);
end;
procedure TfrmMain.DBGrid3DblClick(Sender: TObject);
var tabela : string;
begin
tabela := tbSX2.FieldByName('X2_CHAVE').AsString + ' - ' +
tbSX2.FieldByName('X2_NOME').AsString;
ShowDetSIX(tabela);
end;
procedure TfrmMain.tbSX3AfterScroll(DataSet: TDataSet);
begin
// Só executa se o frmDetSX3 existir na memória
if Assigned(frmDetSX3) then begin // frmDetSX3 <> nil
frmDetSX3.Caption := 'Detalhes do campo: ' + DataSet.FieldByName('X3_CAMPO').AsString;
end;
end;
end.