1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2009-2018 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 //#define DEBUG_SOLVE
22 
23 using GDModel;
24 using GKCore.Kinships;
25 using GKCore.MVP;
26 using GKCore.MVP.Views;
27 using GKCore.Options;
28 
29 namespace GKCore.Controllers
30 {
31     /// <summary>
32     ///
33     /// </summary>
34     public class RelationshipCalculatorDlgController : DialogController<IRelationshipCalculatorDlg>
35     {
36         private GDMIndividualRecord fRec1;
37         private GDMIndividualRecord fRec2;
38         private string fResult;
39 
RelationshipCalculatorDlgController(IRelationshipCalculatorDlg view)40         public RelationshipCalculatorDlgController(IRelationshipCalculatorDlg view) : base(view)
41         {
42         }
43 
SelectRec1()44         public void SelectRec1()
45         {
46             GDMIndividualRecord iRec = fBase.Context.SelectRecord(GDMRecordType.rtIndividual, null) as GDMIndividualRecord;
47             if (iRec != null) SetRec1(iRec);
48         }
49 
SelectRec2()50         public void SelectRec2()
51         {
52             GDMIndividualRecord iRec = fBase.Context.SelectRecord(GDMRecordType.rtIndividual, null) as GDMIndividualRecord;
53             if (iRec != null) SetRec2(iRec);
54         }
55 
SetRec1(GDMIndividualRecord value)56         public void SetRec1(GDMIndividualRecord value)
57         {
58             fRec1 = value;
59             Solve();
60         }
61 
SetRec2(GDMIndividualRecord value)62         public void SetRec2(GDMIndividualRecord value)
63         {
64             fRec2 = value;
65             Solve();
66         }
67 
Solve()68         private void Solve()
69         {
70             fResult = "???";
71 
72             if (fRec1 != null && fRec2 != null) {
73                 using (KinshipsGraph kinsGraph = KinshipsGraph.SearchGraph(fBase.Context, fRec1)) {
74                     if (kinsGraph.IsEmpty()) {
75                         fResult = "Empty graph.";
76                         return;
77                     }
78 
79                     if (kinsGraph.FindVertex(fRec2.XRef) == null) {
80                         fResult = "These individuals have no common relatives.";
81                         return;
82                     }
83 
84                     kinsGraph.SetTreeRoot(fRec1);
85                     fResult = kinsGraph.GetRelationship(fRec2, true, GlobalOptions.Instance.ShortKinshipForm);
86 
87                     #if DEBUG_SOLVE
88                     fResult += "\r\n" + kinsGraph.IndividualsPath;
89                     #endif
90                 }
91             }
92 
93             UpdateView();
94         }
95 
UpdateView()96         public override void UpdateView()
97         {
98             if (fRec1 == null) {
99                 fView.Label1.Text = @"XXX1";
100                 fView.Person1.Text = "";
101             } else {
102                 fView.Label1.Text = fRec1.XRef;
103                 fView.Person1.Text = GKUtils.GetNameString(fRec1, true, false);
104             }
105 
106             if (fRec2 == null) {
107                 fView.Label2.Text = @"XXX2";
108                 fView.Person2.Text = "";
109             } else {
110                 fView.Label2.Text = fRec2.XRef;
111                 fView.Person2.Text = GKUtils.GetNameString(fRec2, true, false);
112             }
113 
114             fView.Result.Text = fResult;
115         }
116     }
117 }
118