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