-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
490 lines (428 loc) · 18.4 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Filename: MainWindow.xaml.cs
// Description: MainWindow is the main application window for the
// TestShapeFile application.
// 2007-01-22 nschan Initial revision.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using Ionic.Zip;
namespace kazOilMap
{
/// <summary>
/// Interaction logic for MainWindow.xaml.
/// </summary>
public sealed partial class MainWindow : System.Windows.Window
{
#region Private fields
// Open file dialog for choosing a shapefile.
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
// Helper class for reading shapefile and displaying WPF shapes on canvas.
ShapeDisplay shapeDisplay;
private static readonly GradientBrush MENU_BACKGROUND = new LinearGradientBrush(Color.FromArgb(255, 158, 190, 245), Color.FromArgb(255, 196, 218, 250), 45);
private KomProject currentProject = null;
#endregion Private fields
#region Constructor
/// <summary>
/// Constructor for MainWindow class.
/// </summary>
public MainWindow()
{
InitializeComponent();
// Create the shape display instance.
this.shapeDisplay = new ShapeDisplay(this, this.canvas1);
// Colorize the menus.
this.menu1.Background = MENU_BACKGROUND;
this.menu2.Background = MENU_BACKGROUND;
// Colorize the canvas.
this.canvas1.Background = new LinearGradientBrush(Colors.WhiteSmoke, Colors.LightSteelBlue, 45);
}
#endregion Constructor
#region Window closing
private void mainWindow_Closing(object sender, CancelEventArgs e)
{
// Ask the user to confirm exit.
string msg = kazOilMap.Properties.Resources.MainWindow_ExitQuestion;
string appName = kazOilMap.Properties.Resources.MainWindow_Title;
MessageBoxResult result = MessageBox.Show(msg, appName, MessageBoxButton.YesNo, MessageBoxImage.Question);
if ( result == MessageBoxResult.Yes )
{
// Proceed with closing of the window.
this.shapeDisplay.CancelReadShapeFile();
e.Cancel = false;
return;
}
// Cancel the closing of the window.
e.Cancel = true;
}
#endregion Window closing
#region File Menu
/// <summary>
/// Handle when the File menu is opened, so that we can update
/// the enabled state of the menu items appropriately.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void fileMI_SubmenuOpened(object sender, EventArgs e)
{
// Disable menu items if we are already reading a shapefile.
this.openMI.IsEnabled = !this.shapeDisplay.IsReadingShapeFile;
this.resetMI.IsEnabled = this.openMI.IsEnabled;
}
/// <summary>
/// Handle File|Open menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void openMI_Click(object sender, EventArgs e)
{
// Show the Open File dialog.
this.openFileDialog.Filter = "Shapefiles (*.shp)|*.shp";
this.openFileDialog.Title = "Open Shapefile for Import";
Nullable<bool> result = this.openFileDialog.ShowDialog();
if ( !result.HasValue || !result.Value )
return;
// Read the shapefile.
this.shapeDisplay.ReadShapeFile(this.openFileDialog.FileName);
}
/// <summary>
/// Handle File|Reset menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void resetMI_Click(object sender, EventArgs e)
{
// Ask user to confirm reset of canvas.
string msg = kazOilMap.Properties.Resources.MainWindow_ResetQuestion;
string appName = kazOilMap.Properties.Resources.MainWindow_Title;
MessageBoxResult result = MessageBox.Show(msg, appName, MessageBoxButton.YesNo, MessageBoxImage.Question);
if ( result == MessageBoxResult.Yes )
{
// Reset the canvas.
this.shapeDisplay.ResetCanvas();
}
}
/// <summary>
/// Handle File|Exit menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void exitMI_Click(object sender, EventArgs e)
{
// When we call Close(), this will trigger the Window.Closing event.
this.Close();
}
#endregion File Menu
#region View Menu
/// <summary>
/// Handle when the View Menu is opened.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void viewMI_SubmenuOpened(object sender, EventArgs e)
{
// Update enabled and checked states of menu items.
this.displayLonLatMI.IsEnabled = (this.shapeDisplay.CanZoom && !this.shapeDisplay.IsReadingShapeFile);
this.displayLonLatMI.IsChecked = this.shapeDisplay.IsDisplayLonLatEnabled;
this.enablePanningMI.IsEnabled = this.displayLonLatMI.IsEnabled;
this.enablePanningMI.IsChecked = this.shapeDisplay.IsPanningEnabled;
this.zoomMI.IsEnabled = this.displayLonLatMI.IsEnabled;
}
/// <summary>
/// Handle View|Display Lon/Lat menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void displayLonLatMI_Click(object sender, EventArgs e)
{
// Toggle the display of the lon/lat coordinates on the canvas.
this.shapeDisplay.IsDisplayLonLatEnabled = !this.shapeDisplay.IsDisplayLonLatEnabled;
}
/// <summary>
/// Handle View|Enable Panning menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void enablePanningMI_Click(object sender, EventArgs e)
{
// Toggle the panning feature on or off.
this.shapeDisplay.IsPanningEnabled = !this.shapeDisplay.IsPanningEnabled;
}
#endregion View Menu
#region Zoom Menu
/// <summary>
/// Handle when the View|Zoom menu is opened, so that we can update
/// the enabled state of the menu items appropriately.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void zoomMI_SubmenuOpened(object sender, EventArgs e)
{
this.zoom50MI.IsEnabled = (this.shapeDisplay.CanZoom && !this.shapeDisplay.IsReadingShapeFile);
this.zoom100MI.IsEnabled = this.zoom50MI.IsEnabled;
this.zoom200MI.IsEnabled = this.zoom50MI.IsEnabled;
this.zoom400MI.IsEnabled = this.zoom50MI.IsEnabled;
this.zoom800MI.IsEnabled = this.zoom50MI.IsEnabled;
this.zoom1600MI.IsEnabled = this.zoom50MI.IsEnabled;
this.zoom3200MI.IsEnabled = this.zoom50MI.IsEnabled;
}
/// <summary>
/// Handle View|Zoom 50% menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void zoom50_Click(object sender, EventArgs e)
{
this.shapeDisplay.Zoom(0.5);
}
/// <summary>
/// Handle View|Zoom 100% menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void zoom100_Click(object sender, EventArgs e)
{
this.shapeDisplay.Zoom(1);
}
/// <summary>
/// Handle View|Zoom 200% menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void zoom200_Click(object sender, EventArgs e)
{
this.shapeDisplay.Zoom(2);
}
/// <summary>
/// Handle View|Zoom 400% menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void zoom400_Click(object sender, EventArgs e)
{
this.shapeDisplay.Zoom(4);
}
/// <summary>
/// Handle View|Zoom 800% menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void zoom800_Click(object sender, EventArgs e)
{
this.shapeDisplay.Zoom(8);
}
/// <summary>
/// Handle View|Zoom 1600% menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void zoom1600_Click(object sender, EventArgs e)
{
this.shapeDisplay.Zoom(16);
}
/// <summary>
/// Handle View|Zoom 3200% menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void zoom3200_Click(object sender, EventArgs e)
{
this.shapeDisplay.Zoom(32);
}
#endregion Zoom Menu
#region Geometry Type Menu
/// <summary>
/// Handle when the Options|Geometry Type menu is opened, so that we can
/// update the enabled and checked states of the menu items appropriately.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void geometryTypeMI_SubmenuOpened(object sender, EventArgs e)
{
// Update enabled states.
this.pathGeometryMI.IsEnabled = !this.shapeDisplay.IsReadingShapeFile;
this.streamGeometryMI.IsEnabled = this.pathGeometryMI.IsEnabled;
this.streamGeometryUnstrokedMI.IsEnabled = this.pathGeometryMI.IsEnabled;
// Update checked states.
switch( this.shapeDisplay.GeometryType )
{
case GeometryType.UsePathGeometry:
this.pathGeometryMI.IsChecked = true;
this.streamGeometryMI.IsChecked = false;
this.streamGeometryUnstrokedMI.IsChecked = false;
break;
case GeometryType.UseStreamGeometry:
this.pathGeometryMI.IsChecked = false;
this.streamGeometryMI.IsChecked = true;
this.streamGeometryUnstrokedMI.IsChecked = false;
break;
case GeometryType.UseStreamGeometryNotStroked:
this.pathGeometryMI.IsChecked = false;
this.streamGeometryMI.IsChecked = false;
this.streamGeometryUnstrokedMI.IsChecked = true;
break;
}
}
/// <summary>
/// Handle Geometry Type|Path Geometry menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void pathGeometryMI_Click(object sender, EventArgs e)
{
this.shapeDisplay.GeometryType = GeometryType.UsePathGeometry;
}
/// <summary>
/// Handle Geometry|Stream Geometry menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void streamGeometryMI_Click(object sender, EventArgs e)
{
this.shapeDisplay.GeometryType = GeometryType.UseStreamGeometry;
}
/// <summary>
/// Handle Geometry Type|Stream Geometry Unstroked menu item click.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void streamGeometryUnstrokedMI_Click(object sender, EventArgs e)
{
this.shapeDisplay.GeometryType = GeometryType.UseStreamGeometryNotStroked;
}
#endregion Geometry Type Menu
#region Keyboard handling
/// <summary>
/// Handle the KeyDown event for the main window.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void mainWindow_KeyDown(object sender, KeyboardEventArgs e)
{
// Pan 10% of the canvas width or height.
if ( e.KeyboardDevice.IsKeyDown(Key.Left) )
this.shapeDisplay.Pan(0.10, 0);
else if ( e.KeyboardDevice.IsKeyDown(Key.Right) )
this.shapeDisplay.Pan(-0.10, 0);
else if ( e.KeyboardDevice.IsKeyDown(Key.Up) )
this.shapeDisplay.Pan(0, 0.10);
else if ( e.KeyboardDevice.IsKeyDown(Key.Down) )
this.shapeDisplay.Pan(0, -0.10);
}
#endregion Keyboard handling
#region My menu option handlers
private void RunTests(object sender, EventArgs e)
{
// Params.ParamsTest();
Debug(Utils.GetTempDirectory("asd"));
}
private void NewProject(object sender, EventArgs e)
{
currentProject = MakeNewProject();
ShowMessage(Messages.projectCreated);
saveProjectAsMI.IsEnabled = true;
}
private void SaveProjectAs(object sender, EventArgs e)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = Messages.kazOilMapProject; // Default file name
dlg.DefaultExt = "";// Messages.kazOilMapExtension; // Default file extension
dlg.Filter = Messages.kazOilMapFilter; // Filter files by extension
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
string filename = dlg.FileName;
string actualProjectName = System.IO.Path.GetFileNameWithoutExtension(filename);
string filenameParentDir = System.IO.Path.GetDirectoryName(filename);
// actual zipping
using (ZipFile zip = new ZipFile())
{
string dir = Utils.GetTempDirectory();
currentProject.paramz.Serialize(dir + System.IO.Path.DirectorySeparatorChar + Messages.kazOilMapMainXml);
// Debug(dir + System.IO.Path.DirectorySeparatorChar + Messages.kazOilMapMainXml);
zip.AddDirectory(dir);
zip.AddDirectoryByName("output");
zip.Save(filename);
}
}
}
private void OpenProject(object sender, EventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = ""; // Default file name
dlg.DefaultExt = Messages.kazOilMapExtension; // Default file extension
dlg.Filter = Messages.kazOilMapFilter; // Filter files by extension
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
string filename = dlg.FileName;
string actualProjectName = System.IO.Path.GetFileNameWithoutExtension(filename);
string filenameParentDir = System.IO.Path.GetDirectoryName(filename);
// actual zipping
using (ZipFile zip = new ZipFile(filename))
{
string dir = Utils.GetTempDirectory() + System.IO.Path.DirectorySeparatorChar + "1";
Debug(dir);
zip.ExtractAll(dir + System.IO.Path.DirectorySeparatorChar + "1");
// Debug(dir + System.IO.Path.DirectorySeparatorChar + Messages.kazOilMapMainXml);
/*
zip.AddDirectory(dir);
zip.AddDirectoryByName("output");
zip.Save(filename);
*/
}
}
}
#endregion
#region projects managing, tests
private KomProject MakeNewProject()
{
return new KomProject(Params.MakeDefaultProject());
}
#endregion
#region Utils
/// <summary>
/// shows message and then fades out
/// </summary>
/// <param name="message"></param>
private void ShowMessage(string message)
{
Messager.Content = message;
Messager.Visibility = System.Windows.Visibility.Visible;
DoubleAnimation a = new DoubleAnimation
{
From = 1.0,
To = 0.0,
FillBehavior = FillBehavior.Stop,
BeginTime = TimeSpan.FromSeconds(2),
Duration = new Duration(TimeSpan.FromSeconds(0.5))
};
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(a);
Storyboard.SetTarget(a, Messager);
Storyboard.SetTargetProperty(a, new PropertyPath(OpacityProperty));
storyboard.Completed += delegate { Messager.Visibility = System.Windows.Visibility.Hidden; };
storyboard.Begin();
}
private void Debug(string message)
{
DebugMessager.Content = message;
}
#endregion
}
}
// END