~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/Data/ICSharpCode.Data.EDMDesigner.Core.UI/Converters/ComplexPropertyRelationToXYConverter.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
#region Usings
 
5
 
 
6
using System;
 
7
using System.Collections.Generic;
 
8
using System.Linq;
 
9
using System.Text;
 
10
using System.Globalization;
 
11
using System.Windows.Data;
 
12
using ICSharpCode.Data.EDMDesigner.Core.UI.UserControls.Relations;
 
13
using System.Windows.Media;
 
14
 
 
15
#endregion
 
16
 
 
17
namespace ICSharpCode.Data.EDMDesigner.Core.UI.Converters
 
18
{
 
19
    public class ComplexPropertyRelationToXYConverter : RelationToXYConverterBase
 
20
    {
 
21
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 
22
        {
 
23
            var complexPropertyRelation = (ComplexPropertyRelation)value;
 
24
            var fromX = -((MatrixTransform)complexPropertyRelation.Canvas.TransformToVisual(complexPropertyRelation.FromTypeDesigner)).Matrix.OffsetX;
 
25
            var toMiddleWidth = complexPropertyRelation.ToTypeDesigner.ActualWidth / 2.0;
 
26
            var toX = -((MatrixTransform)complexPropertyRelation.Canvas.TransformToVisual(complexPropertyRelation.ToTypeDesigner)).Matrix.OffsetX + toMiddleWidth;
 
27
            var xDiff = toX - fromX;
 
28
            double x1, x2, x3, x4, x1Arrow, x2Arrow, xCanvas;
 
29
            var fromByLeft = true;
 
30
            if (xDiff > 0)
 
31
            {
 
32
                x1 = 0;
 
33
                x2 = xDiff;
 
34
                xCanvas = fromX;
 
35
                if (xDiff > complexPropertyRelation.FromTypeDesigner.ActualWidth / 2.0)
 
36
                {
 
37
                    double moreX = complexPropertyRelation.FromTypeDesigner.ActualWidth;
 
38
                    xCanvas += moreX;
 
39
                    x2 -= moreX;
 
40
                    fromByLeft = false;
 
41
                }
 
42
            }
 
43
            else
 
44
            {
 
45
                x1 = -xDiff;
 
46
                x2 = 0;
 
47
                xCanvas = toX;
 
48
            }
 
49
 
 
50
            var fromItem = complexPropertyRelation.FromTypeDesigner.IsExpanded ? complexPropertyRelation.FromItem : complexPropertyRelation.FromTypeDesigner;
 
51
            var fromY = -((MatrixTransform)complexPropertyRelation.Canvas.TransformToVisual(fromItem)).Matrix.OffsetY;
 
52
            var toY = -((MatrixTransform)complexPropertyRelation.Canvas.TransformToVisual(complexPropertyRelation.ToTypeDesigner)).Matrix.OffsetY;
 
53
            var yDiff = toY - fromY;
 
54
            var itemMiddleHeight = fromItem.ActualHeight / 2.0;
 
55
            var y1 = itemMiddleHeight;
 
56
            double y2, y3, y4, y1Arrow, y2Arrow, yCanvas;
 
57
            bool toByTop = true;
 
58
            if (yDiff > 0)
 
59
            {
 
60
                y2 = yDiff - y1;
 
61
                yCanvas = fromY + y1;
 
62
                y1 = 0;
 
63
            }
 
64
            else
 
65
            {
 
66
                y1 -= yDiff;
 
67
                yCanvas = toY;
 
68
                y2 = 0;
 
69
                if (yDiff < -complexPropertyRelation.ToTypeDesigner.ActualHeight / 2.0 || toY < MIN_SIZE)
 
70
                {
 
71
                    double moreY = complexPropertyRelation.ToTypeDesigner.ActualHeight;
 
72
                    yCanvas += moreY;
 
73
                    y1 -= moreY;
 
74
                    toByTop = false;
 
75
                }
 
76
            }
 
77
 
 
78
            var angle = GetAngle(x1, x2, y1, y2);
 
79
            var angleAbs = Math.Abs(angle);
 
80
            yDiff = y2 - y1;
 
81
            xDiff = x2 - x1;
 
82
            bool twoPoints, threePoints, fivePoints, fivePointsV, fivePointsH;
 
83
            if (angleAbs >= ARROW_LINE_MIN_ANGLE && angleAbs <= ARROW_LINE_MAX_ANGLE && (toByTop ? angle > 0 : angle < 0))
 
84
            {
 
85
                twoPoints = true;
 
86
                threePoints = fivePoints = fivePointsH = fivePointsV = false;
 
87
            }
 
88
            else
 
89
            {
 
90
                threePoints = ((y2 > y1) ? toByTop && yDiff > MIN_SIZE : !toByTop && yDiff < -MIN_SIZE) && (xDiff > MIN_SIZE ? !fromByLeft && x2 - x1 > MIN_SIZE : fromByLeft && x2 - x1 < -MIN_SIZE);
 
91
                if (threePoints)
 
92
                {
 
93
                    fivePointsH = false;
 
94
                    fivePointsV = false;
 
95
                    fivePoints = false;
 
96
                }
 
97
                else
 
98
                {
 
99
                    fromY += fromItem.ActualHeight / 2.0;
 
100
                    fivePointsV = fromX - MIN_SIZE * 2.0 < toX && fromX + complexPropertyRelation.ToTypeDesigner.ActualWidth + MIN_SIZE * (double)2 > toX && (fromY > toY + complexPropertyRelation.ToTypeDesigner.ActualHeight + MIN_SIZE || fromY < toY + complexPropertyRelation.ToTypeDesigner.ActualHeight + MIN_SIZE);
 
101
                    fivePointsH = (toByTop ? toY - MIN_SIZE <= fromY : toY + complexPropertyRelation.ToTypeDesigner.ActualHeight + MIN_SIZE > fromY) && (fromX - MIN_SIZE * 2.0 > toX || fromX + complexPropertyRelation.FromTypeDesigner.ActualWidth + MIN_SIZE * (double)2 < toX);
 
102
                    fivePoints = fivePointsH || fivePointsV;
 
103
                }
 
104
                twoPoints = !(fivePoints || threePoints);
 
105
            }
 
106
 
 
107
            if (twoPoints)
 
108
            {
 
109
                x3 = y3 = x4 = y4 = 0;
 
110
                FillArrow(angle, x1, x2, y1, y2, out x1Arrow, out x2Arrow, out y1Arrow, out y2Arrow);
 
111
                var translateXCanvas = Math.Min(x1Arrow, x2Arrow);
 
112
                if (translateXCanvas < 0)
 
113
                {
 
114
                    x1 -= translateXCanvas;
 
115
                    x2 -= translateXCanvas;
 
116
                    x1Arrow -= translateXCanvas;
 
117
                    x2Arrow -= translateXCanvas;
 
118
                    xCanvas += translateXCanvas;
 
119
                }
 
120
                var translateYCanvas = Math.Min(y1Arrow, y2Arrow);
 
121
                if (translateYCanvas < 0)
 
122
                {
 
123
                    y1 -= translateYCanvas;
 
124
                    y2 -= translateYCanvas;
 
125
                    y1Arrow -= translateYCanvas;
 
126
                    y2Arrow -= translateYCanvas;
 
127
                    yCanvas += translateYCanvas;
 
128
                }
 
129
            }
 
130
            else
 
131
            {
 
132
                var xTranslate = x2 - ARROW_WIDTH;
 
133
                if (xTranslate < 0)
 
134
                {
 
135
                    xCanvas += xTranslate;
 
136
                    x1 -= xTranslate;
 
137
                    x2 -= xTranslate;
 
138
                }
 
139
                x1Arrow = x2 - ARROW_WIDTH;
 
140
                x2Arrow = x2 + ARROW_WIDTH;
 
141
                if (threePoints)
 
142
                {
 
143
                    x4 = y4 = 0;
 
144
                    y3 = y2;
 
145
                    y2 = y1;
 
146
                    x3 = x2;
 
147
                    if (y3 > y1)
 
148
                        y1Arrow = y3 - ARROW_HEIGHT;
 
149
                    else
 
150
                        y1Arrow = y3 + ARROW_HEIGHT;
 
151
                }
 
152
                else if (fivePoints)
 
153
                {
 
154
                    x4 = x2;
 
155
                    y4 = y2;
 
156
                    if (fivePointsH)
 
157
                    {
 
158
                        x2 = Math.Abs(fromByLeft ? x1 - x2 + toMiddleWidth : x2 - toMiddleWidth - x1 ) / 2.0;
 
159
                        if (fromByLeft)
 
160
                        {
 
161
                            x3 = x1 + MIN_SIZE + (double)complexPropertyRelation.FromItemIndex * MORE_SIZE_PER_ITEM;
 
162
                            if (x3 < x4 - MIN_SIZE)
 
163
                                x2 = Math.Max(x2, x3);
 
164
                        }
 
165
                        else
 
166
                        {
 
167
                            x3 = x1 - MIN_SIZE - (double)complexPropertyRelation.FromItemIndex * MORE_SIZE_PER_ITEM;
 
168
                            if (x3 > x4 + MIN_SIZE)
 
169
                                x2 = Math.Min(x2, x3);
 
170
                        }
 
171
                    }
 
172
                    else if (fivePointsV)
 
173
                    {
 
174
                        var fromTypeY = -((MatrixTransform)complexPropertyRelation.Canvas.TransformToVisual(complexPropertyRelation.FromTypeDesigner)).Matrix.OffsetY;
 
175
                        if (toByTop)
 
176
                            y2 = Math.Abs(y2 - fromTypeY + yCanvas) / 2.0;
 
177
                        else
 
178
                        {
 
179
                            fromTypeY += complexPropertyRelation.FromTypeDesigner.ActualHeight;
 
180
                            y2 = Math.Abs(y2 - fromTypeY + yCanvas) / 2.0;
 
181
                        }
 
182
                        if (fromByLeft)
 
183
                            x2 = x1 - MIN_SIZE - (double)complexPropertyRelation.FromItemIndex * MORE_SIZE_PER_ITEM;
 
184
                        else
 
185
                            x2 = x1 + MIN_SIZE + (double)complexPropertyRelation.FromItemIndex * MORE_SIZE_PER_ITEM;
 
186
                    }
 
187
                    else
 
188
                        throw new NotImplementedException();
 
189
                    x3 = x2;
 
190
                    y2 = y1;
 
191
                    if (toByTop)
 
192
                    {
 
193
                        y3 = y4 - MIN_SIZE;
 
194
                        y1Arrow = y4 - ARROW_HEIGHT;
 
195
                    }
 
196
                    else
 
197
                    {
 
198
                        y3 = y4 + MIN_SIZE;
 
199
                        y1Arrow = y4 + ARROW_HEIGHT;
 
200
                    }
 
201
                }
 
202
                else
 
203
                    throw new InvalidOperationException();
 
204
                y2Arrow = y1Arrow;
 
205
            }
 
206
 
 
207
            return new double[][] { new[] { xCanvas, yCanvas }, new[] { x1, y1 }, new[] { x2, y2 }, new[] { x3, y3 }, new[] { x4, y3 }, new[] { x4, y4 }, new[] { x1Arrow, y1Arrow }, new[] { x2Arrow, y2Arrow } };
 
208
        }
 
209
    }
 
210
}