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