-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCanvasTab.cs
255 lines (206 loc) · 8.91 KB
/
CanvasTab.cs
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
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SCADA
{
public class CanvasTab : Canvas
{
public TabItemParent TabItemParent { get; set; }
public ItemScada IS { get; set; }
public int CountSelect { get; set; } // счетчик выделенных объектов на канвасе
protected Point Delta;
protected Point ComparePoint;
protected Rect rect = new Rect(0, 0, 0, 0);
protected RectangleGeometry rectangleGeometry = new RectangleGeometry();
public bool IsNegativeSelectX { get; set; }
public bool IsNegativeSelectY { get; set; }
protected System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
protected Rectangle selectedRectangle;
public Rectangle SelectedRectangle
{
get { return selectedRectangle; }
set { selectedRectangle = value; }
}
public List<ControlOnCanvas> SelectedControlOnCanvas = new List<ControlOnCanvas>();
protected List<ControlOnCanvas> hitResultsList = new List<ControlOnCanvas>();
static CanvasTab()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CanvasTab), new FrameworkPropertyMetadata(typeof(CanvasTab)));
}
protected CanvasTab()
{
SelectedRectangle = new Rectangle();
SelectedRectangle.Width = 0;
SelectedRectangle.Height = 0;
SelectedRectangle.Stroke = new SolidColorBrush(Colors.Black);
SelectedRectangle.StrokeThickness = 2;
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;
if (mainWindow != null) // Если не проверить на пустую ссылку, то при выделенном объекте на канвасе и выходе при alt+f4 выдает исключение
mainWindow.LabelCoordinateCursor.Content = null;
e.Handled = true;
}
protected override void OnDragLeave(DragEventArgs e)
{
base.OnDragLeave(e);
MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;
mainWindow.LabelCoordinateCursor.Content = null;
e.Handled = true;
}
protected override void OnDragOver(DragEventArgs e)
{
base.OnDragOver(e);
MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;
Point coordinate = e.GetPosition(this);
if (mainWindow.TabControlMain.SelectedIndex != -1)
{
mainWindow.LabelCoordinateCursor.Content = string.Format("{0}{1:F2}{2}{3}{4}", "X: ", coordinate.X, " ", "Y: ", coordinate.Y);
}
e.Effects = DragDropEffects.None;
if (this is CanvasPage)
{
DragAndDropCanvas data = (DragAndDropCanvas)e.Data.GetData(typeof(DragAndDropCanvas));
if (data != null)
{
if (!data.IsEthernet && !data.IsCom && !data.IsModbus)
{
e.Effects = DragDropEffects.Copy;
}
}
}
e.Handled = true;
}
public HitTestResultBehavior MyHitTestResultCallback(HitTestResult result)
{
// Retrieve the results of the hit test.
IntersectionDetail intersectionDetail = ((GeometryHitTestResult)result).IntersectionDetail;
ControlOnCanvas controlAdd = null;
if (result.VisualHit is Image)
{
if (((Image)result.VisualHit).TemplatedParent is ControlOnCanvas)
{
controlAdd = ((Image)result.VisualHit).TemplatedParent as ControlOnCanvas;
}
}
switch (intersectionDetail)
{
case IntersectionDetail.FullyContains:
// Add the hit test result to the list that will be processed after the enumeration.
if (controlAdd != null)
hitResultsList.Add(controlAdd);
return HitTestResultBehavior.Continue;
case IntersectionDetail.Intersects:
// Set the behavior to return visuals at all z-order levels.
if (controlAdd != null)
hitResultsList.Add(controlAdd);
return HitTestResultBehavior.Continue;
case IntersectionDetail.FullyInside:
// Set the behavior to return visuals at all z-order levels.
if (controlAdd != null)
hitResultsList.Add(controlAdd);
return HitTestResultBehavior.Continue;
default:
return HitTestResultBehavior.Stop;
}
}
public void RepositionAllObjects(Canvas canvas)
{
adjustNodesHorizontally(canvas);
adjustNodesVertically(canvas);
}
private void adjustNodesVertically(Canvas canvas)
{
double minLeft = Canvas.GetLeft(canvas.Children[0]);
foreach (UIElement child in canvas.Children)
{
double left = Canvas.GetLeft(child);
if (child is ControlOnCanvas)
{
Matrix m = ((ControlOnCanvas)child).RenderTransform.Value;
if (((int)m.M11 == -1) && ((int)m.M12 == 0))
{
double d = ((ControlOnCanvas)child).ActualWidth * -1;
left += d;
}
else if (((int)m.M11 == 0) && ((int)m.M12 == 1))
{
double d = ((ControlOnCanvas)child).ActualHeight * -1;
left += d;
}
}
if (left < minLeft)
minLeft = left;
}
if (minLeft < 0)
{
minLeft = -minLeft;
foreach (ControlOnCanvas controlOnCanvas in canvas.Children)
{
Canvas.SetLeft(controlOnCanvas, Canvas.GetLeft(controlOnCanvas) + minLeft);
controlOnCanvas.controlOnCanvasSer.Сoordinates = new Point(Canvas.GetLeft(controlOnCanvas), controlOnCanvas.controlOnCanvasSer.Сoordinates.Y);
}
}
}
private void adjustNodesHorizontally(Canvas canvas)
{
double minTop = Canvas.GetTop(canvas.Children[0]);
foreach (UIElement child in canvas.Children)
{
double top = Canvas.GetTop(child);
if (child is ControlOnCanvas)
{
Matrix m = ((ControlOnCanvas)child).RenderTransform.Value;
if (((int)m.M11 == 0) && ((int)m.M12 == -1))
{
double d = ((ControlOnCanvas)child).ActualWidth * -1;
top += d;
}
else if (((int)m.M11 == -1) && ((int)m.M12 == 0))
{
double d = ((ControlOnCanvas)child).ActualHeight * -1;
top += d;
}
}
if (top < minTop)
minTop = top;
}
if (minTop < 0)
{
minTop = -minTop;
foreach (ControlOnCanvas controlOnCanvas in canvas.Children)
{
Canvas.SetTop(controlOnCanvas, Canvas.GetTop(controlOnCanvas) + minTop);
controlOnCanvas.controlOnCanvasSer.Сoordinates = new Point(controlOnCanvas.controlOnCanvasSer.Сoordinates.X, Canvas.GetTop(controlOnCanvas));
}
}
}
protected override Size MeasureOverride(Size constraint)
{
base.MeasureOverride(constraint);
var desiredSize = new Size();
foreach (UIElement child in Children)
{
desiredSize = new Size(
Math.Max(desiredSize.Width, GetLeft(child) + child.DesiredSize.Width),
Math.Max(desiredSize.Height, GetTop(child) + child.DesiredSize.Height));
}
return desiredSize;
}
}
}