1
{ Search options offline wiki
3
Copyright (C) 2012 Mattias Gaertner mattias@freepascal.org
5
This source is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free
7
Software Foundation; either version 2 of the License, or (at your option)
10
This code is distributed in the hope that it will be useful, but WITHOUT ANY
11
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15
A copy of the GNU General Public License is available on the World Wide Web
16
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
17
to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21
unit WikiSearchOptions;
28
Classes, SysUtils, FileUtil, LazLogger, BasicCodeTools,
29
CodeToolsStructs, WikiHelpManager, WikiStrConsts, WikiFormat, Forms, Controls,
30
Graphics, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Grids;
34
{ TWikiSearchOptsWnd }
36
TWikiSearchOptsWnd = class(TForm)
37
ImageList1: TImageList;
38
LanguagesGroupBox: TGroupBox;
39
LanguagesSplitter: TSplitter;
40
LanguagesTreeView: TTreeView;
41
ScoresGroupBox: TGroupBox;
42
ScoresStringGrid: TStringGrid;
43
procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
44
procedure FormCreate(Sender: TObject);
45
procedure LanguagesTreeViewMouseDown(Sender: TObject; {%H-}Button: TMouseButton;
46
{%H-}Shift: TShiftState; X, Y: Integer);
47
procedure ScoresStringGridEditingDone(Sender: TObject);
49
FLanguages: TStringToStringTree;
50
FOnOptionsChanged: TNotifyEvent;
52
function GetLangCodeEnabled(const ID: string): boolean;
53
function GetLanguages: string;
54
function LangNodeTextToCode(NodeText: string): string;
55
function LangToNodeText(LangID: string; Count: integer = -1): string;
56
procedure SetLangCodeEnabled(const ID: string; AValue: boolean);
57
procedure SetLanguages(AValue: string);
58
procedure FillScoresGrid;
59
function Score2String(s: single): string;
60
procedure DoOptionsChanged;
62
property Languages: string read GetLanguages write SetLanguages;
63
property LangCodeEnabled[const ID: string]: boolean read GetLangCodeEnabled
64
write SetLangCodeEnabled;
65
procedure UpdateAvailableLanguages;
66
procedure UpdateEnabledLanguages;
67
property OnOptionsChanged: TNotifyEvent read FOnOptionsChanged write FOnOptionsChanged;
68
property Scoring: TWHScoring read FScoring;
72
WikiSearchOptsWnd: TWikiSearchOptsWnd = nil;
78
{ TWikiSearchOptsWnd }
80
procedure TWikiSearchOptsWnd.FormCreate(Sender: TObject);
82
FScoring:=TWHScoring.Create;
83
FScoring.Assign(WikiHelp.DefaultScoring);
85
Caption:=wrsWikiSearchOptions;
86
LanguagesGroupBox.Caption:=wrsLanguages;
87
FLanguages:=TStringToStringTree.Create(false);
90
ScoresGroupBox.Caption:=wrsScores;
91
with ScoresStringGrid do begin
93
Cells[0, 1]:=wrsPageTitleWholeWord;
94
Cells[0, 2]:=wrsPageTitlePart;
95
Cells[0, 3]:=wrsHeaderWholeWord;
96
Cells[0, 4]:=wrsHeaderPart;
97
Cells[0, 5]:=wrsTextWholeWord;
98
Cells[0, 6]:=wrsTextPart;
99
Cells[0, 7]:=wrsLinkWholeWord;
100
Cells[0, 8]:=wrsLinkPart;
105
procedure TWikiSearchOptsWnd.FormClose(Sender: TObject;
106
var CloseAction: TCloseAction);
108
FreeAndNil(FLanguages);
109
FreeAndNil(FScoring);
112
procedure TWikiSearchOptsWnd.LanguagesTreeViewMouseDown(Sender: TObject;
113
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
118
TVNode:=LanguagesTreeView.GetNodeAt(X,Y);
119
if TVNode=nil then exit;
120
if x>=TVNode.DisplayStateIconLeft then begin
121
LangID:=LangNodeTextToCode(TVNode.Text);
122
LangCodeEnabled[LangID]:=not LangCodeEnabled[LangID];
126
procedure TWikiSearchOptsWnd.ScoresStringGridEditingDone(Sender: TObject);
128
Category: TWHFitsCategory;
129
FitsString: TWHFitsStringFlag;
135
Row:=ScoresStringGrid.Row;
136
Col:=ScoresStringGrid.Col;
139
1: begin Category:=whfcPageTitle; FitsString:=whfsWholeWord; end;
140
2: begin Category:=whfcPageTitle; FitsString:=whfsPart; end;
141
3: begin Category:=whfcHeader; FitsString:=whfsWholeWord; end;
142
4: begin Category:=whfcHeader; FitsString:=whfsPart; end;
143
5: begin Category:=whfcText; FitsString:=whfsWholeWord; end;
144
6: begin Category:=whfcText; FitsString:=whfsPart; end;
145
7: begin Category:=whfcLink; FitsString:=whfsWholeWord; end;
146
8: begin Category:=whfcLink; FitsString:=whfsPart; end;
149
OldScore:=Scoring.Phrases[Category,FitsString];
150
NewScore:=StrToFloatDef(ScoresStringGrid.Cells[Col,Row],OldScore);
151
if (NewScore<-10000) then NewScore:=-10000;
152
if (NewScore>10000) then NewScore:=10000;
153
ScoresStringGrid.Cells[Col,Row]:=Score2String(NewScore);
154
if OldScore<>NewScore then begin
155
Scoring.Phrases[Category,FitsString]:=NewScore;
161
procedure TWikiSearchOptsWnd.SetLanguages(AValue: string);
168
if AValue=GetLanguages then Exit;
170
if AValue='' then begin
171
// empty = only english,original
175
while p^<>#0 do begin
177
while not (p^ in [#0,',']) do inc(p);
178
if p>StartPos then begin
179
if StartPos^='-' then begin
180
// not original language
181
end else if StartPos^='*' then begin
183
for i:=0 to LanguagesTreeView.Items.TopLvlCount-1 do
184
FLanguages[LangNodeTextToCode(LanguagesTreeView.Items.TopLvlItems[i].Text)]:='1';
186
// a specific language
187
Lang:=SubString(StartPos,p-StartPos);
188
if WikiLangCodeToCaption(Lang)<>Lang then
189
FLanguages[Lang]:='1';
192
while p^=',' do inc(p);
195
UpdateEnabledLanguages;
198
procedure TWikiSearchOptsWnd.FillScoresGrid;
200
with ScoresStringGrid do begin
201
Cells[1,1]:=Score2String(Scoring.Phrases[whfcPageTitle,whfsWholeWord]);
202
Cells[1,2]:=Score2String(Scoring.Phrases[whfcPageTitle,whfsPart]);
203
Cells[1,3]:=Score2String(Scoring.Phrases[whfcHeader,whfsWholeWord]);
204
Cells[1,4]:=Score2String(Scoring.Phrases[whfcHeader,whfsPart]);
205
Cells[1,5]:=Score2String(Scoring.Phrases[whfcText,whfsWholeWord]);
206
Cells[1,6]:=Score2String(Scoring.Phrases[whfcText,whfsPart]);
207
Cells[1,7]:=Score2String(Scoring.Phrases[whfcLink,whfsWholeWord]);
208
Cells[1,8]:=Score2String(Scoring.Phrases[whfcLink,whfsPart]);
212
function TWikiSearchOptsWnd.Score2String(s: single): string;
214
Result:=FloatToStrF(s,ffGeneral,5,2);
217
procedure TWikiSearchOptsWnd.DoOptionsChanged;
219
if Assigned(OnOptionsChanged) then
220
OnOptionsChanged(Self);
223
procedure TWikiSearchOptsWnd.SetLangCodeEnabled(const ID: string;
226
if AValue=GetLangCodeEnabled(ID) then exit;
230
FLanguages.Remove(ID);
231
UpdateEnabledLanguages;
235
function TWikiSearchOptsWnd.LangNodeTextToCode(NodeText: string): string;
239
p:=Pos(' (',NodeText);
240
if p>0 then Delete(NodeText,p,length(NodeText));
241
Result:=WikiLangCaptionToCode(NodeText);
244
function TWikiSearchOptsWnd.LangToNodeText(LangID: string; Count: integer
247
Result:=WikiLangCodeToCaption(LangID);
251
Result+=' ('+IntToStr(Count)+')';
254
procedure TWikiSearchOptsWnd.UpdateAvailableLanguages;
259
LangToCount: TStringToPointerTree;
261
S2PItem: PStringToPointerTreeItem;
263
// collect all languages and count them
264
Langs:=TStringList.Create;
265
if WikiHelp.LoadComplete then begin
266
LangToCount:=TStringToPointerTree.Create(true);
268
for i:=0 to WikiHelp.Converter.Count-1 do begin
269
Lang:=WikiHelp.Converter[i].WikiLanguage;
270
LangToCount[Lang]:=LangToCount[Lang]+1;
272
for S2PItem in LangToCount do
273
if S2PItem^.Name<>'' then
274
Langs.Add(LangToNodeText(S2PItem^.Name,{%H-}PtrUInt(S2PItem^.Value)));
276
Langs.Insert(0,LangToNodeText('',{%H-}PtrUInt(LangToCount[''])));
277
Langs.Add(LangToNodeText('*',WikiHelp.Converter.Count));
282
Langs.Add(LangToNodeText(''));
283
Langs.Add(LangToNodeText('*',0));
286
LanguagesTreeView.BeginUpdate;
288
for i:=0 to Langs.Count-1 do begin
289
if i<LanguagesTreeView.Items.TopLvlCount then begin
290
TVNode:=LanguagesTreeView.Items.TopLvlItems[i];
291
TVNode.Text:=Langs[i];
293
TVNode:=LanguagesTreeView.Items.Add(nil,Langs[i]);
296
while LanguagesTreeView.Items.TopLvlCount>Langs.Count do
297
LanguagesTreeView.Items.TopLvlItems[LanguagesTreeView.Items.TopLvlCount-1].Delete;
299
LanguagesTreeView.EndUpdate;
302
UpdateEnabledLanguages;
305
procedure TWikiSearchOptsWnd.UpdateEnabledLanguages;
309
LangEnabled: Boolean;
311
for i:=0 to LanguagesTreeView.Items.TopLvlCount-1 do begin
312
TVNode:=LanguagesTreeView.Items.TopLvlItems[i];
313
LangEnabled:=LangCodeEnabled[LangNodeTextToCode(TVNode.Text)];
317
TVNode.StateIndex:=0;
321
function TWikiSearchOptsWnd.GetLangCodeEnabled(const ID: string): boolean;
323
Result:=FLanguages.Contains(ID);
326
function TWikiSearchOptsWnd.GetLanguages: string;
328
S2SItem: PStringToStringTreeItem;
331
if not FLanguages.Contains('') then
333
for S2SItem in FLanguages do begin
334
if S2SItem^.Name='' then continue;
335
if Result<>'' then Result+=',';
336
Result+=S2SItem^.Name;