markus / MarkupToPDF / Controls / Etc / SignControl.cs @ ab7fe8c0
이력 | 보기 | 이력해설 | 다운로드 (20.8 KB)
1 |
using System; |
---|---|
2 |
using System.Net; |
3 |
using System.Windows; |
4 |
using System.Windows.Controls; |
5 |
using System.Windows.Documents; |
6 |
using System.Windows.Ink; |
7 |
using System.Windows.Input; |
8 |
using System.Windows.Media; |
9 |
using System.Windows.Media.Animation; |
10 |
using System.Windows.Shapes; |
11 |
using System.Collections.Generic; |
12 |
using System.ComponentModel; |
13 |
using System.Linq; |
14 |
using MarkupToPDF.Controls.Common; |
15 |
using MarkupToPDF.Common; |
16 |
using MarkupToPDF.Serialize.Core; |
17 |
using MarkupToPDF.Serialize.S_Control; |
18 |
using System.Windows.Media.Imaging; |
19 |
using System.Threading.Tasks; |
20 |
|
21 |
//강인구 추가 |
22 |
namespace MarkupToPDF.Controls.Etc |
23 |
{ |
24 |
[TemplatePart(Name = "PART_Image", Type = typeof(Image))] |
25 |
public class SignControl : CommentUserInfo, IDisposable, INormal, INotifyPropertyChanged, IPath, IViewBox, IMarkupCommonData |
26 |
{ |
27 |
#region 초기선언 |
28 |
public string ProjectNO { get; set; } |
29 |
|
30 |
private const string PART_Image = "PART_Image"; |
31 |
public Image Base_Image = null; |
32 |
#endregion |
33 |
|
34 |
static SignControl() |
35 |
{ |
36 |
DefaultStyleKeyProperty.OverrideMetadata(typeof(SignControl), new FrameworkPropertyMetadata(typeof(SignControl))); |
37 |
//Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("/MarkupToPDF;Component/Themes/generic.xaml")) as ResourceDictionary); |
38 |
//ResourceDictionary dictionary = new ResourceDictionary(); |
39 |
//dictionary.Source = new Uri("/MarkupToPDF;component/themes/generic.xaml", UriKind.RelativeOrAbsolute); |
40 |
//Application.Current.Resources.MergedDictionaries.Add(dictionary); |
41 |
//System.Diagnostics.Debug.WriteLine("resource Count :" + Application.Current.Resources.MergedDictionaries.Count); |
42 |
} |
43 |
|
44 |
public void Dispose() |
45 |
{ |
46 |
//GC.Collect(); |
47 |
////GC.SuppressFinalize(this); |
48 |
this.Base_Image = null; |
49 |
} |
50 |
#region Dependency Properties |
51 |
public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register( |
52 |
"UserID", typeof(string), typeof(SignControl), new PropertyMetadata(null)); |
53 |
public static readonly DependencyProperty SignImageProperty = DependencyProperty.Register( |
54 |
"SignImage", typeof(ImageSource), typeof(SignControl), new PropertyMetadata(null)); |
55 |
public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register( |
56 |
"StartPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged)); |
57 |
public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register( |
58 |
"EndPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged)); |
59 |
public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register( |
60 |
"PointSet", typeof(List<Point>), typeof(SignControl), new PropertyMetadata(new List<Point>())); |
61 |
public static readonly DependencyProperty UserNubmerProperty = DependencyProperty.Register( |
62 |
"UserNumber", typeof(string), typeof(SignControl), new PropertyMetadata(null)); |
63 |
public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register( |
64 |
"TopRightPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged)); |
65 |
public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register( |
66 |
"LeftBottomPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged)); |
67 |
public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(SignControl), |
68 |
new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged))); |
69 |
public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(SignControl), |
70 |
new PropertyMetadata((double)0, OnCenterXYChanged)); |
71 |
public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(SignControl), |
72 |
new PropertyMetadata((double)0, OnCenterXYChanged)); |
73 |
public static readonly DependencyProperty IsSelectedProperty = |
74 |
DependencyProperty.Register("IsSelected", typeof(bool), typeof(SignControl), new FrameworkPropertyMetadata(false, IsSelectedChanged)); |
75 |
|
76 |
public static readonly DependencyProperty ControlTypeProperty = |
77 |
DependencyProperty.Register("ControlType", typeof(ControlType), typeof(SignControl), new FrameworkPropertyMetadata(ControlType.Sign)); |
78 |
#endregion |
79 |
#region PropertyChanged Method |
80 |
public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
81 |
{ |
82 |
var instance = (SignControl)sender; |
83 |
if (e.OldValue != e.NewValue && instance.Base_Image != null) |
84 |
{ |
85 |
instance.SetValue(e.Property, e.NewValue); |
86 |
instance.SetImage(); |
87 |
} |
88 |
} |
89 |
public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
90 |
{ |
91 |
|
92 |
} |
93 |
public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
94 |
{ |
95 |
var instance = (SignControl)sender; |
96 |
if (e.OldValue != e.NewValue && instance.Base_Image != null) |
97 |
{ |
98 |
instance.SetValue(e.Property, e.NewValue); |
99 |
instance.SetImage(); |
100 |
} |
101 |
} |
102 |
public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
103 |
{ |
104 |
var instance = (SignControl)sender; |
105 |
if (e.OldValue != e.NewValue && instance.Base_Image != null) |
106 |
{ |
107 |
instance.SetValue(e.Property, e.NewValue); |
108 |
instance.SetImage(); |
109 |
} |
110 |
} |
111 |
#endregion |
112 |
#region Properties |
113 |
public string UserID |
114 |
{ |
115 |
get { return (string)GetValue(UserIDProperty); } |
116 |
set |
117 |
{ |
118 |
if (this.UserID != value) |
119 |
{ |
120 |
SetValue(UserIDProperty, value); |
121 |
RaisePropertyChanged("UserID"); |
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
public double CenterX |
127 |
{ |
128 |
get { return (double)GetValue(CenterXProperty); } |
129 |
set { SetValue(CenterXProperty, value); } |
130 |
} |
131 |
public double CenterY |
132 |
{ |
133 |
get { return (double)GetValue(CenterYProperty); } |
134 |
set { SetValue(CenterYProperty, value); } |
135 |
} |
136 |
public Point EndPoint |
137 |
{ |
138 |
get { return (Point)GetValue(EndPointProperty); } |
139 |
set |
140 |
{ |
141 |
if (this.EndPoint != value) |
142 |
{ |
143 |
SetValue(EndPointProperty, value); |
144 |
RaisePropertyChanged("EndPoint"); |
145 |
} |
146 |
} |
147 |
} |
148 |
public List<Point> PointSet |
149 |
{ |
150 |
get { return (List<Point>)GetValue(PointSetProperty); } |
151 |
set { SetValue(PointSetProperty, value); } |
152 |
} |
153 |
public Point StartPoint |
154 |
{ |
155 |
get { return (Point)GetValue(StartPointProperty); } |
156 |
set |
157 |
{ |
158 |
if (this.StartPoint != value) |
159 |
{ |
160 |
SetValue(StartPointProperty, value); |
161 |
RaisePropertyChanged("StartPoint"); |
162 |
} |
163 |
} |
164 |
} |
165 |
public string UserNumber |
166 |
{ |
167 |
get { return (string)GetValue(UserNubmerProperty); } |
168 |
set |
169 |
{ |
170 |
if (this.UserNumber != value) |
171 |
{ |
172 |
SetValue(UserNubmerProperty, value); |
173 |
RaisePropertyChanged("UserNumber"); |
174 |
} |
175 |
} |
176 |
} |
177 |
public Point TopRightPoint |
178 |
{ |
179 |
get { return (Point)GetValue(TopRightPointProperty); } |
180 |
set |
181 |
{ |
182 |
SetValue(TopRightPointProperty, value); |
183 |
RaisePropertyChanged("TopRightPoint"); |
184 |
} |
185 |
} |
186 |
public Point LeftBottomPoint |
187 |
{ |
188 |
get { return (Point)GetValue(LeftBottomPointProperty); } |
189 |
set |
190 |
{ |
191 |
SetValue(LeftBottomPointProperty, value); |
192 |
RaisePropertyChanged("LeftBottomPoint"); |
193 |
} |
194 |
} |
195 |
public override double CommentAngle |
196 |
{ |
197 |
get { return (double)GetValue(AngleProperty); } |
198 |
set |
199 |
{ |
200 |
if (this.CommentAngle != value) |
201 |
{ |
202 |
SetValue(AngleProperty, value); |
203 |
} |
204 |
} |
205 |
} |
206 |
public ImageSource SignImage |
207 |
{ |
208 |
get { return (ImageSource)this.GetValue(SignImageProperty); } |
209 |
set { this.SetValue(SignImageProperty, value); } |
210 |
} |
211 |
|
212 |
public override bool IsSelected |
213 |
{ |
214 |
get |
215 |
{ |
216 |
return (bool)GetValue(IsSelectedProperty); |
217 |
} |
218 |
set |
219 |
{ |
220 |
SetValue(IsSelectedProperty, value); |
221 |
} |
222 |
} |
223 |
|
224 |
public override ControlType ControlType |
225 |
{ |
226 |
set |
227 |
{ |
228 |
SetValue(ControlTypeProperty, value); |
229 |
} |
230 |
get |
231 |
{ |
232 |
return (ControlType)GetValue(ControlTypeProperty); |
233 |
} |
234 |
} |
235 |
|
236 |
#endregion |
237 |
public override void OnApplyTemplate() |
238 |
{ |
239 |
base.OnApplyTemplate(); |
240 |
Base_Image = GetTemplateChild(PART_Image) as Image; |
241 |
SetImage(); |
242 |
} |
243 |
|
244 |
private void SetImage() |
245 |
{ |
246 |
this.ApplyTemplate(); |
247 |
|
248 |
Point mid = MathSet.FindCentroid(new List<Point>() |
249 |
{ |
250 |
this.StartPoint, |
251 |
this.LeftBottomPoint, |
252 |
this.EndPoint, |
253 |
this.TopRightPoint, |
254 |
}); |
255 |
|
256 |
double AngleData = this.CommentAngle * -1; |
257 |
|
258 |
PathFigure pathFigure = new PathFigure(); |
259 |
pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData); |
260 |
|
261 |
LineSegment lineSegment0 = new LineSegment(); |
262 |
lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData); |
263 |
pathFigure.Segments.Add(lineSegment0); |
264 |
|
265 |
LineSegment lineSegment1 = new LineSegment(); |
266 |
lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData); |
267 |
pathFigure.Segments.Add(lineSegment1); |
268 |
|
269 |
LineSegment lineSegment2 = new LineSegment(); |
270 |
lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData); |
271 |
pathFigure.Segments.Add(lineSegment2); |
272 |
|
273 |
LineSegment lineSegment3 = new LineSegment(); |
274 |
lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData); |
275 |
pathFigure.Segments.Add(lineSegment3); |
276 |
|
277 |
PathGeometry pathGeometry = new PathGeometry(); |
278 |
pathGeometry.Figures = new PathFigureCollection(); |
279 |
pathFigure.IsClosed = true; |
280 |
pathGeometry.Figures.Add(pathFigure); |
281 |
this.Base_Image.Width = pathGeometry.Bounds.Width; |
282 |
this.Base_Image.Height = pathGeometry.Bounds.Height; |
283 |
this.Tag = pathGeometry; |
284 |
|
285 |
Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_Image.Width / 2); |
286 |
Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_Image.Height / 2); |
287 |
} |
288 |
public event PropertyChangedEventHandler PropertyChanged; |
289 |
protected void RaisePropertyChanged(string propName) |
290 |
{ |
291 |
if (PropertyChanged != null) |
292 |
PropertyChanged(this, new PropertyChangedEventArgs(propName)); |
293 |
} |
294 |
|
295 |
public override void UpdateControl() |
296 |
{ |
297 |
this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y); |
298 |
this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y); |
299 |
this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y); |
300 |
this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y); |
301 |
} |
302 |
|
303 |
/// <summary> |
304 |
/// call when mouse is moving while drawing control |
305 |
/// </summary> |
306 |
/// <author>humkyung</author> |
307 |
/// <param name="pt"></param> |
308 |
/// <param name="bAxisLocked"></param> |
309 |
public override void OnCreatingMouseMove(Point pt, bool bAxisLocked) |
310 |
{ |
311 |
this.EndPoint = pt; |
312 |
this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y); |
313 |
this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y); |
314 |
|
315 |
var _sign = Application.Current.FindResource("UserSign"); |
316 |
|
317 |
if (this.StartPoint != this.EndPoint && this.SignImage == null && _sign != null) |
318 |
{ |
319 |
//var _sign = GetUserSign.GetSign(this.UserNumber, this.ProjectNO); |
320 |
|
321 |
byte[] imageBytes = System.Convert.FromBase64String(_sign.ToString()); |
322 |
|
323 |
System.IO.MemoryStream stream = new System.IO.MemoryStream(); |
324 |
stream.Write(imageBytes, 0, imageBytes.Length); |
325 |
stream.Position = 0; |
326 |
System.Drawing.Image img = System.Drawing.Image.FromStream(stream); |
327 |
BitmapImage returnImage = new BitmapImage(); |
328 |
returnImage.BeginInit(); |
329 |
System.IO.MemoryStream ms = new System.IO.MemoryStream(); |
330 |
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); |
331 |
ms.Seek(0, System.IO.SeekOrigin.Begin); |
332 |
returnImage.StreamSource = ms; |
333 |
returnImage.EndInit(); |
334 |
stream.Close(); |
335 |
|
336 |
this.SignImage = returnImage; |
337 |
} |
338 |
|
339 |
this.PointSet = new List<Point> |
340 |
{ |
341 |
this.StartPoint, |
342 |
this.LeftBottomPoint, |
343 |
this.EndPoint, |
344 |
this.TopRightPoint, |
345 |
}; |
346 |
} |
347 |
|
348 |
/// <summary> |
349 |
/// move control point has same location of given pt along given delta |
350 |
/// </summary> |
351 |
/// <author>humkyung</author> |
352 |
/// <date>2019.06.20</date> |
353 |
/// <param name="pt"></param> |
354 |
/// <param name="dx"></param> |
355 |
/// <param name="dy"></param> |
356 |
public override void OnMoveCtrlPoint(Point pt, double dx, double dy, bool bAxisLocked = false) |
357 |
{ |
358 |
IPath path = (this as IPath); |
359 |
|
360 |
Point selected = MathSet.getNearPoint(path.PointSet, pt); |
361 |
selected.X += dx; |
362 |
selected.Y += dy; |
363 |
int i = 0; |
364 |
for (i = 0; i < (this as IPath).PointSet.Count; i++) |
365 |
{ |
366 |
if (pt.Equals((this as IPath).PointSet[i])) |
367 |
{ |
368 |
path.PointSet[i] = selected; |
369 |
break; |
370 |
} |
371 |
} |
372 |
|
373 |
var OppositeP = (i + path.PointSet.Count / 2) % path.PointSet.Count; |
374 |
var PreviousP = (i + (path.PointSet.Count - 1)) % path.PointSet.Count; |
375 |
var NextP = (i + 1) % path.PointSet.Count; |
376 |
|
377 |
if (bAxisLocked) |
378 |
{ |
379 |
double _dx = path.PointSet[i].X - path.PointSet[OppositeP].X; |
380 |
double _dy = path.PointSet[i].Y - path.PointSet[OppositeP].Y; |
381 |
double distance = Math.Max(Math.Abs(_dx), Math.Abs(_dy)); |
382 |
|
383 |
var PreviousV = path.PointSet[PreviousP] - path.PointSet[OppositeP]; |
384 |
PreviousV.Normalize(); |
385 |
path.PointSet[PreviousP] = path.PointSet[OppositeP] + PreviousV * distance; |
386 |
|
387 |
var NextV = path.PointSet[NextP] - path.PointSet[OppositeP]; |
388 |
NextV.Normalize(); |
389 |
path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * distance; |
390 |
|
391 |
path.PointSet[i] = path.PointSet[OppositeP] + PreviousV * distance + NextV * distance; |
392 |
} |
393 |
else |
394 |
{ |
395 |
var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[PreviousP]); |
396 |
var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[i].X - path.PointSet[OppositeP].X, |
397 |
path.PointSet[i].Y - path.PointSet[OppositeP].Y); |
398 |
path.PointSet[PreviousP] = new Point(path.PointSet[OppositeP].X + PreviousV.X * l, path.PointSet[OppositeP].Y + PreviousV.Y * l); |
399 |
|
400 |
var NextV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[NextP]); |
401 |
l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[i].X - path.PointSet[OppositeP].X, path.PointSet |
402 |
[i].Y - path.PointSet[OppositeP].Y); |
403 |
path.PointSet[NextP] = new Point(path.PointSet[OppositeP].X + NextV.X * l, path.PointSet[OppositeP].Y + NextV.Y * l); |
404 |
} |
405 |
|
406 |
this.UpdateControl(); |
407 |
} |
408 |
|
409 |
/// <summary> |
410 |
/// return SignControl's area |
411 |
/// </summary> |
412 |
/// <author>humkyung</author> |
413 |
/// <date>2019.06.13</date> |
414 |
public override Rect ItemRect |
415 |
{ |
416 |
get |
417 |
{ |
418 |
double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X); |
419 |
double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y); |
420 |
double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X); |
421 |
double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y); |
422 |
|
423 |
return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY)); |
424 |
} |
425 |
} |
426 |
|
427 |
/// <summary> |
428 |
/// Serialize this |
429 |
/// </summary> |
430 |
/// <param name="sUserId"></param> |
431 |
/// <returns></returns> |
432 |
public override string Serialize() |
433 |
{ |
434 |
using (S_SignControl STemp = new S_SignControl()) |
435 |
{ |
436 |
STemp.Angle = this.CommentAngle; |
437 |
STemp.EndPoint = this.EndPoint; |
438 |
STemp.UserID = this.UserID; |
439 |
STemp.LB = this.LeftBottomPoint; |
440 |
STemp.Name = this.GetType().Name; |
441 |
STemp.PointSet = this.PointSet; |
442 |
STemp.StartPoint = this.StartPoint; |
443 |
STemp.Opac = this.Opacity; |
444 |
STemp.TR = this.TopRightPoint; |
445 |
STemp.UserNumber = this.UserNumber == null ? this.UserID: this.UserNumber; |
446 |
|
447 |
///강인구 추가(2017.11.02) |
448 |
///Memo 추가 |
449 |
STemp.Memo = this.Memo; |
450 |
|
451 |
return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize())); |
452 |
} |
453 |
} |
454 |
|
455 |
/// <summary> |
456 |
/// create a signcontrol from given string |
457 |
/// </summary> |
458 |
/// <param name="str"></param> |
459 |
/// <returns></returns> |
460 |
public static SignControl FromString(string str, SolidColorBrush brush, string sProjectNo) |
461 |
{ |
462 |
SignControl instance = null; |
463 |
using (S_SignControl s = JsonSerializerHelper.JsonDeserialize<S_SignControl>(str)) |
464 |
{ |
465 |
instance = new SignControl |
466 |
{ |
467 |
CommentAngle = s.Angle, |
468 |
StartPoint = s.StartPoint, |
469 |
TopRightPoint = s.TR, |
470 |
EndPoint = s.EndPoint, |
471 |
LeftBottomPoint = s.LB, |
472 |
PointSet = s.PointSet, |
473 |
Opacity = s.Opac, |
474 |
SignImage = null, |
475 |
UserID = s.UserID, |
476 |
UserNumber = s.UserNumber, |
477 |
Memo = s.Memo |
478 |
}; |
479 |
|
480 |
if (s.UserNumber != null) |
481 |
{ |
482 |
var _sign = GetUserSign.GetSign(s.UserNumber, sProjectNo); |
483 |
if (_sign != null) |
484 |
{ |
485 |
byte[] imageBytes = System.Convert.FromBase64String(_sign); |
486 |
|
487 |
BitmapImage returnImage = new BitmapImage(); |
488 |
|
489 |
using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) |
490 |
{ |
491 |
stream.WriteAsync(imageBytes, 0, imageBytes.Length); |
492 |
|
493 |
stream.Position = 0; |
494 |
System.Drawing.Image img = System.Drawing.Image.FromStream(stream); |
495 |
returnImage.BeginInit(); |
496 |
System.IO.MemoryStream ms = new System.IO.MemoryStream(); |
497 |
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); |
498 |
ms.Seek(0, System.IO.SeekOrigin.Begin); |
499 |
returnImage.StreamSource = ms; |
500 |
returnImage.EndInit(); |
501 |
stream.Close(); |
502 |
} |
503 |
|
504 |
instance.SignImage = returnImage; |
505 |
} |
506 |
} |
507 |
} |
508 |
|
509 |
return instance; |
510 |
} |
511 |
|
512 |
public double LineSize |
513 |
{ |
514 |
get; |
515 |
set; |
516 |
} |
517 |
public Geometry PathData |
518 |
{ |
519 |
get; |
520 |
set; |
521 |
} |
522 |
} |
523 |
} |