markus / MarkupToPDF / Controls / Etc / SignControl.cs @ fa48eb85
이력 | 보기 | 이력해설 | 다운로드 (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 |
public double CenterX |
125 |
{ |
126 |
get { return (double)GetValue(CenterXProperty); } |
127 |
set { SetValue(CenterXProperty, value); } |
128 |
} |
129 |
public double CenterY |
130 |
{ |
131 |
get { return (double)GetValue(CenterYProperty); } |
132 |
set { SetValue(CenterYProperty, value); } |
133 |
} |
134 |
public Point EndPoint |
135 |
{ |
136 |
get { return (Point)GetValue(EndPointProperty); } |
137 |
set |
138 |
{ |
139 |
if (this.EndPoint != value) |
140 |
{ |
141 |
SetValue(EndPointProperty, value); |
142 |
RaisePropertyChanged("EndPoint"); |
143 |
} |
144 |
} |
145 |
} |
146 |
public List<Point> PointSet |
147 |
{ |
148 |
get { return (List<Point>)GetValue(PointSetProperty); } |
149 |
set { SetValue(PointSetProperty, value); } |
150 |
} |
151 |
public Point StartPoint |
152 |
{ |
153 |
get { return (Point)GetValue(StartPointProperty); } |
154 |
set |
155 |
{ |
156 |
if (this.StartPoint != value) |
157 |
{ |
158 |
SetValue(StartPointProperty, value); |
159 |
RaisePropertyChanged("StartPoint"); |
160 |
} |
161 |
} |
162 |
} |
163 |
public string UserNumber |
164 |
{ |
165 |
get { return (string)GetValue(UserNubmerProperty); } |
166 |
set |
167 |
{ |
168 |
if (this.UserNumber != value) |
169 |
{ |
170 |
SetValue(UserNubmerProperty, value); |
171 |
RaisePropertyChanged("UserNumber"); |
172 |
} |
173 |
} |
174 |
} |
175 |
public Point TopRightPoint |
176 |
{ |
177 |
get { return (Point)GetValue(TopRightPointProperty); } |
178 |
set |
179 |
{ |
180 |
SetValue(TopRightPointProperty, value); |
181 |
RaisePropertyChanged("TopRightPoint"); |
182 |
} |
183 |
} |
184 |
public Point LeftBottomPoint |
185 |
{ |
186 |
get { return (Point)GetValue(LeftBottomPointProperty); } |
187 |
set |
188 |
{ |
189 |
SetValue(LeftBottomPointProperty, value); |
190 |
RaisePropertyChanged("LeftBottomPoint"); |
191 |
} |
192 |
} |
193 |
public override double CommentAngle |
194 |
{ |
195 |
get { return (double)GetValue(AngleProperty); } |
196 |
set |
197 |
{ |
198 |
if (this.CommentAngle != value) |
199 |
{ |
200 |
SetValue(AngleProperty, value); |
201 |
} |
202 |
} |
203 |
} |
204 |
public ImageSource SignImage |
205 |
{ |
206 |
get { return (ImageSource)this.GetValue(SignImageProperty); } |
207 |
set { this.SetValue(SignImageProperty, value); } |
208 |
} |
209 |
|
210 |
public override bool IsSelected |
211 |
{ |
212 |
get |
213 |
{ |
214 |
return (bool)GetValue(IsSelectedProperty); |
215 |
} |
216 |
set |
217 |
{ |
218 |
SetValue(IsSelectedProperty, value); |
219 |
} |
220 |
} |
221 |
|
222 |
public override ControlType ControlType |
223 |
{ |
224 |
set |
225 |
{ |
226 |
SetValue(ControlTypeProperty, value); |
227 |
} |
228 |
get |
229 |
{ |
230 |
return (ControlType)GetValue(ControlTypeProperty); |
231 |
} |
232 |
} |
233 |
|
234 |
#endregion |
235 |
public override void OnApplyTemplate() |
236 |
{ |
237 |
base.OnApplyTemplate(); |
238 |
Base_Image = GetTemplateChild(PART_Image) as Image; |
239 |
SetImage(); |
240 |
} |
241 |
|
242 |
private void SetImage() |
243 |
{ |
244 |
this.ApplyTemplate(); |
245 |
|
246 |
Point mid = MathSet.FindCentroid(new List<Point>() |
247 |
{ |
248 |
this.StartPoint, |
249 |
this.LeftBottomPoint, |
250 |
this.EndPoint, |
251 |
this.TopRightPoint, |
252 |
}); |
253 |
|
254 |
double AngleData = this.CommentAngle * -1; |
255 |
|
256 |
PathFigure pathFigure = new PathFigure(); |
257 |
pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData); |
258 |
|
259 |
LineSegment lineSegment0 = new LineSegment(); |
260 |
lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData); |
261 |
pathFigure.Segments.Add(lineSegment0); |
262 |
|
263 |
LineSegment lineSegment1 = new LineSegment(); |
264 |
lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData); |
265 |
pathFigure.Segments.Add(lineSegment1); |
266 |
|
267 |
LineSegment lineSegment2 = new LineSegment(); |
268 |
lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData); |
269 |
pathFigure.Segments.Add(lineSegment2); |
270 |
|
271 |
LineSegment lineSegment3 = new LineSegment(); |
272 |
lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData); |
273 |
pathFigure.Segments.Add(lineSegment3); |
274 |
|
275 |
PathGeometry pathGeometry = new PathGeometry(); |
276 |
pathGeometry.Figures = new PathFigureCollection(); |
277 |
pathFigure.IsClosed = true; |
278 |
pathGeometry.Figures.Add(pathFigure); |
279 |
this.Base_Image.Width = pathGeometry.Bounds.Width; |
280 |
this.Base_Image.Height = pathGeometry.Bounds.Height; |
281 |
this.Tag = pathGeometry; |
282 |
|
283 |
Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_Image.Width / 2); |
284 |
Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_Image.Height / 2); |
285 |
} |
286 |
public event PropertyChangedEventHandler PropertyChanged; |
287 |
protected void RaisePropertyChanged(string propName) |
288 |
{ |
289 |
if (PropertyChanged != null) |
290 |
PropertyChanged(this, new PropertyChangedEventArgs(propName)); |
291 |
} |
292 |
|
293 |
public override void UpdateControl() |
294 |
{ |
295 |
this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y); |
296 |
this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y); |
297 |
this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y); |
298 |
this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y); |
299 |
} |
300 |
|
301 |
/// <summary> |
302 |
/// call when mouse is moving while drawing control |
303 |
/// </summary> |
304 |
/// <author>humkyung</author> |
305 |
/// <param name="pt"></param> |
306 |
/// <param name="bAxisLocked"></param> |
307 |
public override void OnCreatingMouseMove(Point pt, bool bAxisLocked, bool bShiftKeyPressed) |
308 |
{ |
309 |
this.EndPoint = pt; |
310 |
this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y); |
311 |
this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y); |
312 |
|
313 |
if (this.StartPoint != this.EndPoint && this.SignImage == null) |
314 |
{ |
315 |
var _sign = GetUserSign.GetSign(this.UserNumber, this.ProjectNO); |
316 |
byte[] imageBytes = System.Convert.FromBase64String(_sign); |
317 |
System.IO.MemoryStream stream = new System.IO.MemoryStream(); |
318 |
stream.Write(imageBytes, 0, imageBytes.Length); |
319 |
stream.Position = 0; |
320 |
System.Drawing.Image img = System.Drawing.Image.FromStream(stream); |
321 |
BitmapImage returnImage = new BitmapImage(); |
322 |
returnImage.BeginInit(); |
323 |
System.IO.MemoryStream ms = new System.IO.MemoryStream(); |
324 |
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); |
325 |
ms.Seek(0, System.IO.SeekOrigin.Begin); |
326 |
returnImage.StreamSource = ms; |
327 |
returnImage.EndInit(); |
328 |
stream.Close(); |
329 |
|
330 |
this.SignImage = returnImage; |
331 |
} |
332 |
this.PointSet = new List<Point> |
333 |
{ |
334 |
this.StartPoint, |
335 |
this.LeftBottomPoint, |
336 |
this.EndPoint, |
337 |
this.TopRightPoint, |
338 |
}; |
339 |
} |
340 |
|
341 |
/// <summary> |
342 |
/// move control point has same location of given pt along given delta |
343 |
/// </summary> |
344 |
/// <author>humkyung</author> |
345 |
/// <date>2019.06.20</date> |
346 |
/// <param name="pt"></param> |
347 |
/// <param name="dx"></param> |
348 |
/// <param name="dy"></param> |
349 |
public override void OnMoveCtrlPoint(Point pt, double dx, double dy) |
350 |
{ |
351 |
IPath path = (this as IPath); |
352 |
|
353 |
Point selected = MathSet.getNearPoint(path.PointSet, pt); |
354 |
selected.X += dx; |
355 |
selected.Y += dy; |
356 |
int i = 0; |
357 |
for (i = 0; i < (this as IPath).PointSet.Count; i++) |
358 |
{ |
359 |
if (pt.Equals((this as IPath).PointSet[i])) |
360 |
{ |
361 |
path.PointSet[i] = selected; |
362 |
break; |
363 |
} |
364 |
} |
365 |
|
366 |
var ReverseP = (i + path.PointSet.Count() / 2) % path.PointSet.Count(); |
367 |
var PreviousP = (i + (path.PointSet.Count() - 1)) % path.PointSet.Count(); |
368 |
var NextP = (i + 1) % path.PointSet.Count(); |
369 |
|
370 |
var distance = MathSet.DistanceTo(path.PointSet[ReverseP], path.PointSet[i]); |
371 |
|
372 |
var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[ReverseP], path.PointSet[PreviousP]); |
373 |
var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[i].X - path.PointSet[ReverseP].X, |
374 |
path.PointSet[i].Y - path.PointSet[ReverseP].Y); |
375 |
path.PointSet[PreviousP] = new Point(path.PointSet[ReverseP].X + PreviousV.X * l, path.PointSet[ReverseP].Y + PreviousV.Y * l); |
376 |
|
377 |
var NextV = MathSet.GetNormVectorBetween(path.PointSet[ReverseP], path.PointSet[NextP]); |
378 |
l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[i].X - path.PointSet[ReverseP].X, path.PointSet |
379 |
[i].Y - path.PointSet[ReverseP].Y); |
380 |
path.PointSet[NextP] = new Point(path.PointSet[ReverseP].X + NextV.X * l, path.PointSet[ReverseP].Y + NextV.Y * l); |
381 |
|
382 |
this.UpdateControl(); |
383 |
} |
384 |
|
385 |
/// <summary> |
386 |
/// return SignControl's area |
387 |
/// </summary> |
388 |
/// <author>humkyung</author> |
389 |
/// <date>2019.06.13</date> |
390 |
public override Rect ItemRect |
391 |
{ |
392 |
get |
393 |
{ |
394 |
double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X); |
395 |
double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y); |
396 |
double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X); |
397 |
double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y); |
398 |
|
399 |
return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY)); |
400 |
} |
401 |
} |
402 |
|
403 |
/// <summary> |
404 |
/// Serialize this |
405 |
/// </summary> |
406 |
/// <param name="sUserId"></param> |
407 |
/// <returns></returns> |
408 |
public override string Serialize() |
409 |
{ |
410 |
using (S_SignControl STemp = new S_SignControl()) |
411 |
{ |
412 |
STemp.Angle = this.CommentAngle; |
413 |
STemp.EndPoint = this.EndPoint; |
414 |
STemp.UserID = this.UserID; |
415 |
STemp.LB = this.LeftBottomPoint; |
416 |
STemp.Name = this.GetType().Name; |
417 |
STemp.PointSet = this.PointSet; |
418 |
STemp.StartPoint = this.StartPoint; |
419 |
STemp.Opac = this.Opacity; |
420 |
STemp.TR = this.TopRightPoint; |
421 |
STemp.UserNumber = this.UserNumber == null ? this.UserID: this.UserNumber; |
422 |
|
423 |
///강인구 추가(2017.11.02) |
424 |
///Memo 추가 |
425 |
STemp.Memo = this.Memo; |
426 |
|
427 |
return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize())); |
428 |
} |
429 |
} |
430 |
|
431 |
/// <summary> |
432 |
/// create a signcontrol from given string |
433 |
/// </summary> |
434 |
/// <param name="str"></param> |
435 |
/// <returns></returns> |
436 |
public static SignControl FromString(string str, SolidColorBrush brush, string sProjectNo) |
437 |
{ |
438 |
SignControl instance = null; |
439 |
using (S_SignControl s = JsonSerializerHelper.JsonDeserialize<S_SignControl>(str)) |
440 |
{ |
441 |
instance = new SignControl |
442 |
{ |
443 |
CommentAngle = s.Angle, |
444 |
StartPoint = s.StartPoint, |
445 |
TopRightPoint = s.TR, |
446 |
EndPoint = s.EndPoint, |
447 |
LeftBottomPoint = s.LB, |
448 |
PointSet = s.PointSet, |
449 |
Opacity = s.Opac, |
450 |
SignImage = null, |
451 |
UserID = s.UserID, |
452 |
UserNumber = s.UserNumber, |
453 |
Memo = s.Memo |
454 |
}; |
455 |
|
456 |
if (s.UserNumber != null) |
457 |
{ |
458 |
var _sign = GetUserSign.GetSign(s.UserNumber, sProjectNo); |
459 |
if (_sign != null) |
460 |
{ |
461 |
byte[] imageBytes = System.Convert.FromBase64String(_sign); |
462 |
|
463 |
System.IO.MemoryStream stream = new System.IO.MemoryStream(); |
464 |
stream.Write(imageBytes, 0, imageBytes.Length); |
465 |
stream.Position = 0; |
466 |
System.Drawing.Image img = System.Drawing.Image.FromStream(stream); |
467 |
BitmapImage returnImage = new BitmapImage(); |
468 |
returnImage.BeginInit(); |
469 |
System.IO.MemoryStream ms = new System.IO.MemoryStream(); |
470 |
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); |
471 |
ms.Seek(0, System.IO.SeekOrigin.Begin); |
472 |
returnImage.StreamSource = ms; |
473 |
returnImage.EndInit(); |
474 |
stream.Close(); |
475 |
instance.SignImage = returnImage; |
476 |
} |
477 |
} |
478 |
} |
479 |
|
480 |
return instance; |
481 |
} |
482 |
|
483 |
public double LineSize |
484 |
{ |
485 |
get; |
486 |
set; |
487 |
} |
488 |
public Geometry PathData |
489 |
{ |
490 |
get; |
491 |
set; |
492 |
} |
493 |
} |
494 |
} |