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
|
{ Search options offline wiki
Copyright (C) 2012 Mattias Gaertner mattias@freepascal.org
This source is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1335, USA.
}
unit WikiSearchOptions;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LazLogger, AvgLvlTree, BasicCodeTools,
CodeToolsStructs, WikiHelpManager, WikiStrConsts, WikiFormat, Forms, Controls,
Graphics, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Grids;
type
{ TWikiSearchOptsWnd }
TWikiSearchOptsWnd = class(TForm)
ImageList1: TImageList;
LanguagesGroupBox: TGroupBox;
LanguagesSplitter: TSplitter;
LanguagesTreeView: TTreeView;
ScoresGroupBox: TGroupBox;
ScoresStringGrid: TStringGrid;
procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure LanguagesTreeViewMouseDown(Sender: TObject; {%H-}Button: TMouseButton;
{%H-}Shift: TShiftState; X, Y: Integer);
procedure ScoresStringGridEditingDone(Sender: TObject);
private
FLanguages: TStringToStringTree;
FOnOptionsChanged: TNotifyEvent;
FScoring: TWHScoring;
function GetLangCodeEnabled(const ID: string): boolean;
function GetLanguages: string;
function LangNodeTextToCode(NodeText: string): string;
function LangToNodeText(LangID: string; Count: integer = -1): string;
procedure SetLangCodeEnabled(const ID: string; AValue: boolean);
procedure SetLanguages(AValue: string);
procedure FillScoresGrid;
function Score2String(s: single): string;
procedure DoOptionsChanged;
public
property Languages: string read GetLanguages write SetLanguages;
property LangCodeEnabled[const ID: string]: boolean read GetLangCodeEnabled
write SetLangCodeEnabled;
procedure UpdateAvailableLanguages;
procedure UpdateEnabledLanguages;
property OnOptionsChanged: TNotifyEvent read FOnOptionsChanged write FOnOptionsChanged;
property Scoring: TWHScoring read FScoring;
end;
var
WikiSearchOptsWnd: TWikiSearchOptsWnd = nil;
implementation
{$R *.lfm}
{ TWikiSearchOptsWnd }
procedure TWikiSearchOptsWnd.FormCreate(Sender: TObject);
begin
FScoring:=TWHScoring.Create;
FScoring.Assign(WikiHelp.DefaultScoring);
Caption:=wrsWikiSearchOptions;
LanguagesGroupBox.Caption:=wrsLanguages;
FLanguages:=TStringToStringTree.Create(false);
FLanguages['']:='1';
ScoresGroupBox.Caption:=wrsScores;
with ScoresStringGrid do begin
RowCount:=9;
Cells[0, 1]:=wrsPageTitleWholeWord;
Cells[0, 2]:=wrsPageTitlePart;
Cells[0, 3]:=wrsHeaderWholeWord;
Cells[0, 4]:=wrsHeaderPart;
Cells[0, 5]:=wrsTextWholeWord;
Cells[0, 6]:=wrsTextPart;
Cells[0, 7]:=wrsLinkWholeWord;
Cells[0, 8]:=wrsLinkPart;
end;
FillScoresGrid;
end;
procedure TWikiSearchOptsWnd.FormClose(Sender: TObject;
var CloseAction: TCloseAction);
begin
FreeAndNil(FLanguages);
FreeAndNil(FScoring);
end;
procedure TWikiSearchOptsWnd.LanguagesTreeViewMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
TVNode: TTreeNode;
LangID: String;
begin
TVNode:=LanguagesTreeView.GetNodeAt(X,Y);
if TVNode=nil then exit;
if x>=TVNode.DisplayStateIconLeft then begin
LangID:=LangNodeTextToCode(TVNode.Text);
LangCodeEnabled[LangID]:=not LangCodeEnabled[LangID];
end;
end;
procedure TWikiSearchOptsWnd.ScoresStringGridEditingDone(Sender: TObject);
var
Category: TWHFitsCategory;
FitsString: TWHFitsStringFlag;
OldScore: Single;
NewScore: Extended;
Row: Integer;
Col: Integer;
begin
Row:=ScoresStringGrid.Row;
Col:=ScoresStringGrid.Col;
if Col=1 then begin
case Row of
1: begin Category:=whfcPageTitle; FitsString:=whfsWholeWord; end;
2: begin Category:=whfcPageTitle; FitsString:=whfsPart; end;
3: begin Category:=whfcHeader; FitsString:=whfsWholeWord; end;
4: begin Category:=whfcHeader; FitsString:=whfsPart; end;
5: begin Category:=whfcText; FitsString:=whfsWholeWord; end;
6: begin Category:=whfcText; FitsString:=whfsPart; end;
7: begin Category:=whfcLink; FitsString:=whfsWholeWord; end;
8: begin Category:=whfcLink; FitsString:=whfsPart; end;
else exit;
end;
OldScore:=Scoring.Phrases[Category,FitsString];
NewScore:=StrToFloatDef(ScoresStringGrid.Cells[Col,Row],OldScore);
if (NewScore<-10000) then NewScore:=-10000;
if (NewScore>10000) then NewScore:=10000;
ScoresStringGrid.Cells[Col,Row]:=Score2String(NewScore);
if OldScore<>NewScore then begin
Scoring.Phrases[Category,FitsString]:=NewScore;
DoOptionsChanged;
end;
end;
end;
procedure TWikiSearchOptsWnd.SetLanguages(AValue: string);
var
p: PChar;
StartPos: PChar;
i: Integer;
Lang: String;
begin
if AValue=GetLanguages then Exit;
FLanguages.Clear;
if AValue='' then begin
// empty = only english,original
FLanguages['']:='1';
end else begin
p:=PChar(AValue);
while p^<>#0 do begin
StartPos:=p;
while not (p^ in [#0,',']) do inc(p);
if p>StartPos then begin
if StartPos^='-' then begin
// not original language
end else if StartPos^='*' then begin
// fit any
for i:=0 to LanguagesTreeView.Items.TopLvlCount-1 do
FLanguages[LangNodeTextToCode(LanguagesTreeView.Items.TopLvlItems[i].Text)]:='1';
end else begin
// a specific language
Lang:=SubString(StartPos,p-StartPos);
if WikiLangCodeToCaption(Lang)<>Lang then
FLanguages[Lang]:='1';
end;
end;
while p^=',' do inc(p);
end;
end;
UpdateEnabledLanguages;
end;
procedure TWikiSearchOptsWnd.FillScoresGrid;
begin
with ScoresStringGrid do begin
Cells[1,1]:=Score2String(Scoring.Phrases[whfcPageTitle,whfsWholeWord]);
Cells[1,2]:=Score2String(Scoring.Phrases[whfcPageTitle,whfsPart]);
Cells[1,3]:=Score2String(Scoring.Phrases[whfcHeader,whfsWholeWord]);
Cells[1,4]:=Score2String(Scoring.Phrases[whfcHeader,whfsPart]);
Cells[1,5]:=Score2String(Scoring.Phrases[whfcText,whfsWholeWord]);
Cells[1,6]:=Score2String(Scoring.Phrases[whfcText,whfsPart]);
Cells[1,7]:=Score2String(Scoring.Phrases[whfcLink,whfsWholeWord]);
Cells[1,8]:=Score2String(Scoring.Phrases[whfcLink,whfsPart]);
end;
end;
function TWikiSearchOptsWnd.Score2String(s: single): string;
begin
Result:=FloatToStrF(s,ffGeneral,5,2);
end;
procedure TWikiSearchOptsWnd.DoOptionsChanged;
begin
if Assigned(OnOptionsChanged) then
OnOptionsChanged(Self);
end;
procedure TWikiSearchOptsWnd.SetLangCodeEnabled(const ID: string;
AValue: boolean);
begin
if AValue=GetLangCodeEnabled(ID) then exit;
if AValue then
FLanguages[ID]:='1'
else
FLanguages.Remove(ID);
UpdateEnabledLanguages;
DoOptionsChanged;
end;
function TWikiSearchOptsWnd.LangNodeTextToCode(NodeText: string): string;
var
p: SizeInt;
begin
p:=Pos(' (',NodeText);
if p>0 then Delete(NodeText,p,length(NodeText));
Result:=WikiLangCaptionToCode(NodeText);
end;
function TWikiSearchOptsWnd.LangToNodeText(LangID: string; Count: integer
): string;
begin
Result:=WikiLangCodeToCaption(LangID);
if Count<0 then
Result+=' (?)'
else
Result+=' ('+IntToStr(Count)+')';
end;
procedure TWikiSearchOptsWnd.UpdateAvailableLanguages;
var
Langs: TStringList;
i: Integer;
TVNode: TTreeNode;
LangToCount: TStringToPointerTree;
Lang: String;
S2PItem: PStringToPointerTreeItem;
begin
// collect all languages and count them
Langs:=TStringList.Create;
if WikiHelp.LoadComplete then begin
LangToCount:=TStringToPointerTree.Create(true);
try
for i:=0 to WikiHelp.Converter.Count-1 do begin
Lang:=WikiHelp.Converter[i].WikiLanguage;
LangToCount[Lang]:=LangToCount[Lang]+1;
end;
for S2PItem in LangToCount do
if S2PItem^.Name<>'' then
Langs.Add(LangToNodeText(S2PItem^.Name,{%H-}PtrUInt(S2PItem^.Value)));
Langs.Sort;
Langs.Insert(0,LangToNodeText('',{%H-}PtrUInt(LangToCount[''])));
Langs.Add(LangToNodeText('*',WikiHelp.Converter.Count));
finally
LangToCount.Free;
end;
end else begin
Langs.Add(LangToNodeText(''));
Langs.Add(LangToNodeText('*',0));
end;
LanguagesTreeView.BeginUpdate;
try
for i:=0 to Langs.Count-1 do begin
if i<LanguagesTreeView.Items.TopLvlCount then begin
TVNode:=LanguagesTreeView.Items.TopLvlItems[i];
TVNode.Text:=Langs[i];
end else begin
TVNode:=LanguagesTreeView.Items.Add(nil,Langs[i]);
end;
end;
while LanguagesTreeView.Items.TopLvlCount>Langs.Count do
LanguagesTreeView.Items.TopLvlItems[LanguagesTreeView.Items.TopLvlCount-1].Delete;
finally
LanguagesTreeView.EndUpdate;
Langs.Free;
end;
UpdateEnabledLanguages;
end;
procedure TWikiSearchOptsWnd.UpdateEnabledLanguages;
var
i: Integer;
TVNode: TTreeNode;
LangEnabled: Boolean;
begin
for i:=0 to LanguagesTreeView.Items.TopLvlCount-1 do begin
TVNode:=LanguagesTreeView.Items.TopLvlItems[i];
LangEnabled:=LangCodeEnabled[LangNodeTextToCode(TVNode.Text)];
if LangEnabled then
TVNode.StateIndex:=1
else
TVNode.StateIndex:=0;
end;
end;
function TWikiSearchOptsWnd.GetLangCodeEnabled(const ID: string): boolean;
begin
Result:=FLanguages.Contains(ID);
end;
function TWikiSearchOptsWnd.GetLanguages: string;
var
S2SItem: PStringToStringItem;
begin
Result:='';
if not FLanguages.Contains('') then
Result:='-';
for S2SItem in FLanguages do begin
if S2SItem^.Name='' then continue;
if Result<>'' then Result+=',';
Result+=S2SItem^.Name;
end;
end;
end.
|