-
Notifications
You must be signed in to change notification settings - Fork 5
PGNDATABASE - An idea at a chess pgn database double-click to load games #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ interface | |
ApplicationForms, Dialogs, SysUtils, NotationForms, ClockForms, | ||
ChessRules, AnalysisForms, TextureContainers, ChessGame, GameStartDialogs, | ||
ChessStrings, ActnList, Menus, ComCtrls, BoardForms, MoveVariantForms, | ||
PseudoDockedForms, Classes; | ||
PseudoDockedForms, Classes, RegExpr, unitFrmPgnDatabase; | ||
|
||
resourcestring | ||
SResignConfirmation = 'Do you really want to resign?'; | ||
|
@@ -42,6 +42,7 @@ interface | |
|
||
TMainForm = class(TApplicationForm) | ||
AboutAction: TAction; | ||
actLoadPGNFile: TAction; | ||
ExitAction: TAction; | ||
MainMenu: TMainMenu; | ||
MenuItem1: TMenuItem; | ||
|
@@ -87,6 +88,9 @@ TMainForm = class(TApplicationForm) | |
MenuItem46: TMenuItem; | ||
MenuItem47: TMenuItem; | ||
MenuItem48: TMenuItem; | ||
mniLoadPGN: TMenuItem; | ||
mniFile: TMenuItem; | ||
dlgOpenPGN: TOpenDialog; | ||
Panel4: TPanel; | ||
Splitter3: TSplitter; | ||
ToolButton28: TToolButton; | ||
|
@@ -143,6 +147,7 @@ TMainForm = class(TApplicationForm) | |
ToolButton8: TToolButton; | ||
ToolButton9: TToolButton; | ||
procedure AboutActionExecute(Sender: TObject); | ||
procedure actLoadPGNFileExecute(Sender: TObject); | ||
procedure ChangeLookActionExecute(Sender: TObject); | ||
procedure ExitActionExecute(Sender: TObject); | ||
procedure FontSelectActionExecute(Sender: TObject); | ||
|
@@ -163,11 +168,11 @@ TMainForm = class(TApplicationForm) | |
procedure ChildFormHide(Sender: TObject); | ||
private | ||
Texture: TTextureContainer; | ||
FGame: TChessGame; | ||
protected | ||
procedure AddWindow(AForm: TApplicationForm; APanel: TCustomPanel; | ||
ASplitter: TSplitter; CanHide: boolean); | ||
public | ||
FGame: TChessGame; | ||
procedure CreateNewGame; | ||
end; | ||
|
||
|
@@ -203,6 +208,77 @@ procedure TMainForm.AboutActionExecute(Sender: TObject); | |
AboutBox.ShowModal; | ||
end; | ||
|
||
procedure TMainForm.actLoadPGNFileExecute(Sender: TObject); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var | ||
SList : TStringList; | ||
i, R, j, row : Integer; | ||
s, s2, pgntags, white, black : AnsiString; | ||
RegexObj: TRegExpr; | ||
begin | ||
//load pgn file | ||
RegexObj := TRegExpr.Create(); | ||
RegexObj.Expression := '((((\[.*?\])\s*)+)(1\..+?(0-1|1-0|1/2-1/2)))'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The game can be also unfinished and end with |
||
if dlgOpenPGN.Execute() then | ||
begin | ||
frmPGNDatabase.Show(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
frmPGNDatabase.gridPGNDatabase.Clear(); | ||
frmPGNDatabase.gridPGNDatabase.RowCount := 1 + frmPGNDatabase.gridPGNDatabase.FixedRows; | ||
s := ''; | ||
s2 := ''; | ||
R := 1; | ||
row := frmPGNDatabase.gridPGNDatabase.FixedRows; | ||
SList := TStringList.Create(); | ||
SList.LoadFromFile(dlgOpenPGN.FileName); | ||
for i := 0 to SList.Count-1 do | ||
begin | ||
s2 := SList[i]; | ||
s += s2 + #13#10; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never use |
||
if (s2.Contains(' 1-0') or s2.Contains(' 1/2-1/2') or s2.Contains(' 0-1')) and RegexObj.Exec(s) then | ||
begin | ||
pgntags := RegexObj.Match[2]; | ||
j := pgntags.IndexOf('[White "'); | ||
white := ''; | ||
if (j > 0) then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if j > 0 then Braces here are not required. |
||
begin | ||
white := pgntags.Substring(j+8, pgntags.Substring(j+8).IndexOf('"]')); | ||
end; | ||
j := pgntags.IndexOf('[Black "'); | ||
black := ''; | ||
if (j > 0) then | ||
begin | ||
black := pgntags.Substring(j+8, pgntags.Substring(j+8).IndexOf('"]')); | ||
end; | ||
|
||
frmPGNDatabase.gridPGNDatabase.RowCount := frmPGNDatabase.gridPGNDatabase.RowCount + 1; | ||
|
||
frmPGNDatabase.gridPGNDatabase.Cells[0, row] := RegexObj.Match[2]; | ||
frmPGNDatabase.gridPGNDatabase.Cells[1, row] := white; | ||
frmPGNDatabase.gridPGNDatabase.Cells[2, row] := black; | ||
frmPGNDatabase.gridPGNDatabase.Cells[3, row] := RegexObj.Match[5]; | ||
Inc(row); | ||
s := ''; | ||
R := R + 1; | ||
end | ||
end; | ||
if (R-1 = 1) then | ||
begin | ||
frmPGNDatabase.Caption := FormatFloat('#,',R-1) + ' PGN Game'; | ||
end else | ||
begin | ||
frmPGNDatabase.Caption := FormatFloat('#,',R-1) + ' PGN Games'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For various UI messages, we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, it could be a better idea to place the number of game on the status bar, not into the window title |
||
end; | ||
|
||
try | ||
CreateNewGame(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIR
So, it's necessary to disallow using this in the analysis mode. |
||
except | ||
on E: Exception do | ||
MessageDlg(E.Message, mtError, [mbOK], 0); | ||
end; | ||
SList.Free(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // create SList
try
// do something
finally
FreeAndNil(SList);
end; |
||
end; | ||
RegexObj.Free(); | ||
end; | ||
|
||
procedure TMainForm.ChangeLookActionExecute(Sender: TObject); | ||
var | ||
WasTexture: TTextureContainer; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not a good idea to promote the field from private to public. Passing either a callback to change the current game or a reference to
TChessGame
into the new form is acceptable, though.