Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/WpfTestSvgSample/DrawingPage.xaml.cs @ 12762

Last change on this file since 12762 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 13.9 KB
Line 
1using System;
2using System.IO;
3using System.Collections.Generic;
4
5using System.Windows;
6using System.Windows.Input;
7using System.Windows.Media;
8using System.Windows.Controls;
9
10using SharpVectors.Runtime;
11using SharpVectors.Renderers;
12using SharpVectors.Renderers.Wpf;
13using SharpVectors.Converters;
14
15namespace WpfTestSvgSample
16{
17    /// <summary>
18    /// Interaction logic for DrawingPage.xaml
19    /// </summary>
20    public partial class DrawingPage : Page
21    {
22        #region Private Fields
23
24        private bool _saveXaml;
25
26        private string _drawingDir;
27        private DirectoryInfo _directoryInfo;
28
29        private FileSvgReader _fileReader;
30        private WpfDrawingSettings _wpfSettings;
31
32        private DirectoryInfo _workingDir;
33
34        /// <summary>
35        /// Specifies the current state of the mouse handling logic.
36        /// </summary>
37        private MouseHandlingMode mouseHandlingMode;
38
39        /// <summary>
40        /// The point that was clicked relative to the ZoomAndPanControl.
41        /// </summary>
42        private Point origZoomAndPanControlMouseDownPoint;
43
44        /// <summary>
45        /// The point that was clicked relative to the content that is contained within the ZoomAndPanControl.
46        /// </summary>
47        private Point origContentMouseDownPoint;
48
49        /// <summary>
50        /// Records which mouse button clicked during mouse dragging.
51        /// </summary>
52        private MouseButton mouseButtonDown;
53
54        #endregion
55
56        #region Constructors and Destructor
57
58        public DrawingPage()
59        {
60            InitializeComponent();
61
62            _saveXaml            = true;
63            _wpfSettings         = new WpfDrawingSettings();
64            _wpfSettings.CultureInfo = _wpfSettings.NeutralCultureInfo;
65
66            _fileReader          = new FileSvgReader(_wpfSettings);
67            _fileReader.SaveXaml = _saveXaml;
68            _fileReader.SaveZaml = false;
69
70            mouseHandlingMode = MouseHandlingMode.None;
71
72            string workDir = Path.Combine(Path.GetDirectoryName(
73                System.Reflection.Assembly.GetExecutingAssembly().Location), "XamlDrawings");
74
75            _workingDir = new DirectoryInfo(workDir);
76
77            this.Loaded += new RoutedEventHandler(OnPageLoaded);
78        }
79
80        #endregion     
81
82        #region Public Properties
83
84        public string XamlDrawingDir
85        {
86            get
87            {
88                return _drawingDir;
89            }
90            set
91            {
92                _drawingDir = value;
93
94                if (!String.IsNullOrEmpty(_drawingDir))
95                {
96                    _directoryInfo = new DirectoryInfo(_drawingDir);
97
98                    if (_fileReader != null)
99                    {
100                        _fileReader.SaveXaml = Directory.Exists(_drawingDir);
101                    }
102                }
103            }
104        }
105
106        public bool SaveXaml
107        {
108            get
109            {
110                return _saveXaml;
111            }
112            set
113            {
114                _saveXaml = value;
115            }
116        }
117
118        #endregion
119
120        #region Public Methods
121
122        public bool LoadDocument(string svgFilePath)
123        {
124            if (String.IsNullOrEmpty(svgFilePath) || !File.Exists(svgFilePath))
125            {
126                return false;
127            }
128
129            DirectoryInfo workingDir = _workingDir;
130            if (_directoryInfo != null)
131            {
132                workingDir = _directoryInfo;
133            }
134
135           //double currentZoom = zoomSlider.Value;
136
137            svgViewer.UnloadDiagrams();
138
139            //zoomSlider.Value = 1.0;
140
141            string fileExt = Path.GetExtension(svgFilePath);
142
143            if (String.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase) ||
144                String.Equals(fileExt, ".svg", StringComparison.OrdinalIgnoreCase))
145            {
146                if (_fileReader != null)
147                {
148                    _fileReader.SaveXaml = _saveXaml;
149                    _fileReader.SaveZaml = false;
150
151                    DrawingGroup drawing = _fileReader.Read(svgFilePath, workingDir);
152                    if (drawing != null)
153                    {
154                        svgViewer.RenderDiagrams(drawing);
155
156                        //zoomSlider.Value = currentZoom;
157
158                        Rect bounds = svgViewer.Bounds;
159
160                        //Rect rect = new Rect(0, 0,
161                        //    mainFrame.RenderSize.Width, mainFrame.RenderSize.Height);
162                        //Rect rect = new Rect(0, 0,
163                        //    bounds.Width, bounds.Height);
164                        if (bounds.IsEmpty)
165                        {
166                            bounds = new Rect(0, 0,
167                                canvasScroller.ActualWidth, canvasScroller.ActualHeight);
168                        }
169                        zoomPanControl.AnimatedZoomTo(bounds);
170
171                        return true;
172                    }
173                }
174            }
175            else if (String.Equals(fileExt, ".xaml", StringComparison.OrdinalIgnoreCase) ||
176                String.Equals(fileExt, ".zaml", StringComparison.OrdinalIgnoreCase))
177            {
178                svgViewer.LoadDiagrams(svgFilePath);
179
180                //zoomSlider.Value = currentZoom;
181
182                svgViewer.InvalidateMeasure();
183
184                return true;
185            }
186
187            return false;
188        }
189
190        public void UnloadDocument()
191        {
192            if (svgViewer != null)
193            {
194                svgViewer.UnloadDiagrams();
195            }
196        }
197
198        public bool SaveDocument(string fileName)
199        {
200            if (String.IsNullOrEmpty(fileName))
201            {
202                return false;
203            }
204
205            if (_fileReader == null || _fileReader.Drawing == null)
206            {
207                return false;
208            }
209
210            return _fileReader.Save(fileName, true, false);
211        }
212
213        public void PageSelected(bool isSelected)
214        {   
215        }
216
217        #endregion
218
219        #region Protected Methods
220
221        protected override void OnInitialized(EventArgs e)
222        {
223            base.OnInitialized(e);
224        }
225
226        #endregion
227
228        #region Private Event Handlers
229
230        private void OnPageLoaded(object sender, RoutedEventArgs e)
231        {
232            zoomSlider.Value = 100;
233
234            if (zoomPanControl != null)
235            {
236                zoomPanControl.IsMouseWheelScrollingEnabled = true;
237            }
238        }
239
240        private void OnZoomSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
241        {
242            if (zoomPanControl != null)
243            {
244                zoomPanControl.AnimatedZoomTo(zoomSlider.Value / 100.0);
245            }
246        }
247
248        private void OnZoomInClick(object sender, RoutedEventArgs e)
249        {
250            this.ZoomIn();
251        }
252
253        private void OnZoomOutClick(object sender, RoutedEventArgs e)
254        {
255            this.ZoomOut();
256        }
257
258        private void OnResetZoom(object sender, RoutedEventArgs e)
259        {
260            if (zoomPanControl == null)
261            {
262                return;
263            }
264
265            zoomPanControl.ContentScale = 1.0;
266        }
267
268        /// <summary>
269        /// The 'ZoomIn' command (bound to the plus key) was executed.
270        /// </summary>
271        private void OnZoomFitClick(object sender, RoutedEventArgs e)
272        {
273            if (svgViewer == null || zoomPanControl == null)
274            {
275                return;
276            }
277
278            Rect bounds = svgViewer.Bounds;
279
280            //Rect rect = new Rect(0, 0,
281            //    mainFrame.RenderSize.Width, mainFrame.RenderSize.Height);
282            //Rect rect = new Rect(0, 0,
283            //    bounds.Width, bounds.Height);
284            if (bounds.IsEmpty)
285            {
286                bounds = new Rect(0, 0,
287                    canvasScroller.ActualWidth, canvasScroller.ActualHeight);
288            }
289            zoomPanControl.AnimatedZoomTo(bounds);
290        }
291
292        private void OnPanClick(object sender, RoutedEventArgs e)
293        {
294            //if (drawScrollView == null)
295            //{
296            //    return;
297            //}
298
299            //drawScrollView.ZoomableCanvas.IsPanning =
300            //    (tbbPanning.IsChecked != null && tbbPanning.IsChecked.Value);
301        }
302
303        #region Private Zoom Panel Handlers
304
305        /// <summary>
306        /// Event raised on mouse down in the ZoomAndPanControl.
307        /// </summary>
308        private void OnZoomPanMouseDown(object sender, MouseButtonEventArgs e)
309        {
310            svgViewer.Focus();
311            Keyboard.Focus(svgViewer);
312
313            mouseButtonDown = e.ChangedButton;
314            origZoomAndPanControlMouseDownPoint = e.GetPosition(zoomPanControl);
315            origContentMouseDownPoint = e.GetPosition(svgViewer);
316
317            if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0 &&
318                (e.ChangedButton == MouseButton.Left ||
319                 e.ChangedButton == MouseButton.Right))
320            {
321                // Shift + left- or right-down initiates zooming mode.
322                mouseHandlingMode = MouseHandlingMode.Zooming;
323            }
324            else if (mouseButtonDown == MouseButton.Left)
325            {
326                // Just a plain old left-down initiates panning mode.
327                mouseHandlingMode = MouseHandlingMode.Panning;
328            }
329
330            if (mouseHandlingMode != MouseHandlingMode.None)
331            {
332                // Capture the mouse so that we eventually receive the mouse up event.
333                zoomPanControl.CaptureMouse();
334                e.Handled = true;
335            }
336        }
337
338        /// <summary>
339        /// Event raised on mouse up in the ZoomAndPanControl.
340        /// </summary>
341        private void OnZoomPanMouseUp(object sender, MouseButtonEventArgs e)
342        {
343            if (mouseHandlingMode != MouseHandlingMode.None)
344            {
345                if (mouseHandlingMode == MouseHandlingMode.Zooming)
346                {
347                    if (mouseButtonDown == MouseButton.Left)
348                    {
349                        // Shift + left-click zooms in on the content.
350                        ZoomIn();
351                    }
352                    else if (mouseButtonDown == MouseButton.Right)
353                    {
354                        // Shift + left-click zooms out from the content.
355                        ZoomOut();
356                    }
357                }
358
359                zoomPanControl.ReleaseMouseCapture();
360                mouseHandlingMode = MouseHandlingMode.None;
361                e.Handled = true;
362            }
363        }
364
365        /// <summary>
366        /// Event raised on mouse move in the ZoomAndPanControl.
367        /// </summary>
368        private void OnZoomPanMouseMove(object sender, MouseEventArgs e)
369        {
370            if (mouseHandlingMode == MouseHandlingMode.Panning)
371            {
372                //
373                // The user is left-dragging the mouse.
374                // Pan the viewport by the appropriate amount.
375                //
376                Point curContentMousePoint = e.GetPosition(svgViewer);
377                Vector dragOffset = curContentMousePoint - origContentMouseDownPoint;
378
379                zoomPanControl.ContentOffsetX -= dragOffset.X;
380                zoomPanControl.ContentOffsetY -= dragOffset.Y;
381
382                e.Handled = true;
383            }
384        }
385
386        /// <summary>
387        /// Event raised by rotating the mouse wheel
388        /// </summary>
389        private void OnZoomPanMouseWheel(object sender, MouseWheelEventArgs e)
390        {
391            e.Handled = true;
392
393            if (e.Delta > 0)
394            {
395                ZoomIn();
396            }
397            else if (e.Delta < 0)
398            {
399                ZoomOut();
400            }
401        }
402
403        /// <summary>
404        /// The 'ZoomIn' command (bound to the plus key) was executed.
405        /// </summary>
406        private void OnZoomFit(object sender, RoutedEventArgs e)
407        {
408            if (svgViewer == null || zoomPanControl == null)
409            {
410                return;
411            }
412
413            Rect bounds = svgViewer.Bounds;
414
415            //Rect rect = new Rect(0, 0,
416            //    mainFrame.RenderSize.Width, mainFrame.RenderSize.Height);
417            //Rect rect = new Rect(0, 0,
418            //    bounds.Width, bounds.Height);
419            if (bounds.IsEmpty)
420            {
421                bounds = new Rect(0, 0,
422                    canvasScroller.ActualWidth, canvasScroller.ActualHeight);
423            }
424            zoomPanControl.AnimatedZoomTo(bounds);
425        }
426
427        /// <summary>
428        /// The 'ZoomIn' command (bound to the plus key) was executed.
429        /// </summary>
430        private void OnZoomIn(object sender, RoutedEventArgs e)
431        {
432            ZoomIn();
433        }
434
435        /// <summary>
436        /// The 'ZoomOut' command (bound to the minus key) was executed.
437        /// </summary>
438        private void OnZoomOut(object sender, RoutedEventArgs e)
439        {
440            ZoomOut();
441        }
442
443        /// <summary>
444        /// Zoom the viewport out by a small increment.
445        /// </summary>
446        private void ZoomOut()
447        {
448            if (zoomPanControl == null)
449            {
450                return;
451            }
452
453            zoomPanControl.ContentScale -= 0.1;
454        }
455
456        /// <summary>
457        /// Zoom the viewport in by a small increment.
458        /// </summary>
459        private void ZoomIn()
460        {
461            if (zoomPanControl == null)
462            {
463                return;
464            }
465
466            zoomPanControl.ContentScale += 0.1;
467        }
468
469        #endregion
470
471        #endregion
472    }
473}
Note: See TracBrowser for help on using the repository browser.