-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathLayoutPages.cs
More file actions
342 lines (297 loc) · 9.09 KB
/
Copy pathLayoutPages.cs
File metadata and controls
342 lines (297 loc) · 9.09 KB
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
// -------------------------------------------------------------------------------------------
// <copyright file="LayoutPages.cs" company="MapWindow OSS Team - www.mapwindow.org">
// MapWindow OSS Team - 2015
// </copyright>
// -------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;
using System.Linq;
using System.Text;
using MW5.Plugins.Printing.Helpers;
using MW5.Shared;
namespace MW5.Plugins.Printing.Model
{
public class LayoutPages : List<LayoutPage>
{
private PrinterSettings _settings;
private int _pageCountX;
private int _pageCountY;
private int _pageHeight;
private int _pageWidth;
public LayoutPages()
{
PageCountX = 1;
PageCountY = 1;
MarkPageSizeDirty();
}
public void Initialize(PrinterSettings settings)
{
if (settings == null) throw new NullReferenceException("settings");
_settings = settings;
}
public bool HasScheduled
{
get { return this.Count(p => p.Scheduled) != 0; }
}
/// <summary>
/// Do we have at least one selected page
/// </summary>
public bool HasSelection
{
get { return this.Count(p => p.Selected) != 0; }
}
/// <summary>
/// Do we have any page left unprinted
/// </summary>
public bool HasUnprintedPages
{
get
{
if (!HasScheduled)
{
return this.Count(p => !p.Printed) > 0;
}
return this.Count(p => !p.Printed && p.Scheduled) > 0;
}
}
/// <summary>
/// Returns total number of pages
/// </summary>
public int PageCount
{
get { return PageCountX * PageCountY; }
}
/// <summary>
/// Number of horizontal pages
/// </summary>
public int PageCountX
{
get { return _pageCountX; }
set
{
_pageCountX = value;
UpdatePages();
}
}
/// <summary>
/// Number of vertical pages
/// </summary>
public int PageCountY
{
get { return _pageCountY; }
set
{
_pageCountY = value;
UpdatePages();
}
}
/// <summary>
/// Changes the number of pages in the layout.
/// </summary>
public void Resize(int pageCountX, int pageCountY)
{
if (pageCountX <= 0 || PageCountY <= 0 || pageCountX > 20 || pageCountY > 20)
{
Logger.Current.Warn("Unable to resize layout. Invalid number of pages: {0} × {1}.");
return;
}
_pageCountX = pageCountX;
_pageCountY = pageCountY;
UpdatePages();
}
/// <summary>
/// Gets a height of a single page in 1/100 of an inch
/// </summary>
public int PageHeight
{
get
{
if (_pageHeight == -1)
{
_pageHeight = PageSettings.Landscape ? GetPaperWidth() : GetPaperHeight();
}
return _pageHeight;
}
}
/// <summary>
/// Gets a width of a single page in 1/100 of an inch
/// </summary>
public int PageWidth
{
get
{
if (_pageWidth == -1)
{
_pageWidth = PageSettings.Landscape ? GetPaperHeight() : GetPaperWidth();
}
return _pageWidth;
}
}
public int SelectedCount
{
get { return this.Count(p => p.Selected); }
}
/// <summary>
/// Gets the height of the paper (all pages) in 1/100 of an inch
/// </summary>
public int TotalHeight
{
get { return PageHeight * PageCountY; }
}
/// <summary>
/// Gets the width of the paper (all the pages) in 1/100 of an inch
/// </summary>
public int TotalWidth
{
get { return PageWidth * PageCountX; }
}
private PageSettings PageSettings
{
get { return _settings.DefaultPageSettings; }
}
public LayoutPage GetPage(int pageIndexX, int pageIndexY)
{
int index = pageIndexY * PageCountX + pageIndexX;
return index >= 0 && index < Count ? this[index] : null;
}
/// <summary>
/// Gets index of page which corresponds to the specified position in paper coordinates
/// </summary>
public int GetPageIndex(float positionX)
{
for (int i = 0; i < 10; i++)
{
if (GetPagePositionX(i + 1) > positionX) return i;
}
return -1;
}
/// <summary>
/// Returns X coordinate of page with specified index (in paper coordinates 1/100 of inch)
/// </summary>
public float GetPagePositionX(int pageIndex)
{
return pageIndex * PageWidth;
}
/// <summary>
/// Returns Y coordinate of page with specified index (in paper coordinates 1/100 of inch)
/// </summary>
public float GetPagePositionY(int pageIndex)
{
return pageIndex * PageHeight;
}
/// <summary>
/// Returns page rectangle in paper coordinates (1/100 of inch)
/// </summary>
public Rectangle GetPageRectange(int x, int y)
{
return new Rectangle(x * PageWidth, y * PageHeight, PageWidth, PageHeight);
}
public void MarkPageSizeDirty()
{
_pageHeight = -1;
_pageWidth = -1;
}
public void MarkUnprinted()
{
foreach (var page in this)
{
page.Printed = false;
page.Scheduled = false;
}
}
/// <summary>
/// Finds page located at specific screen coordinates
/// </summary>
public LayoutPage PageByClick(PointF pointPaper)
{
var x = (int)Math.Floor((pointPaper.X / PageWidth));
var y = (int)Math.Floor((pointPaper.Y / PageHeight));
return GetPage(x, y);
}
public int PageIndex(LayoutPage page)
{
return page.Y * PageCountX + page.X + 1;
}
/// <summary>
/// Returns user readable string with selected pages
/// </summary>
public override string ToString()
{
if (this.Count(p => p.Selected) == 0)
{
return Count > 1 ? string.Format("{0}-{1}", 1, Count) : "1";
}
var builder = new StringBuilder();
int groupStart = 0;
for (int i = 0; i < Count - 1; i++)
{
if (this[i].Selected)
{
if (this[i + 1].Selected && groupStart == -1)
{
groupStart = i + 1;
}
else
{
if (builder.Length > 0)
{
builder.Append(", ");
}
builder.Append((i + 1).ToString(CultureInfo.InvariantCulture));
}
}
else if (groupStart != 0)
{
if (builder.Length > 0)
{
builder.Append(", ");
}
builder.AppendFormat("{0}-{1}", groupStart, i + 1);
groupStart = 0;
}
}
return builder.ToString();
}
private int GetPaperHeight()
{
try
{
var sett = PageSettings;
return sett.PaperSize.Height - sett.Margins.Top - sett.Margins.Bottom;
}
catch (InvalidPrinterException)
{
return 1169;
}
}
private int GetPaperWidth()
{
try
{
var sett = PageSettings;
return sett.PaperSize.Width - sett.Margins.Left - sett.Margins.Right;
}
catch (InvalidPrinterException)
{
return 827;
}
}
/// <summary>
/// Updates a list of pages when number of pages changes
/// </summary>
private void UpdatePages()
{
Clear();
for (int y = 0; y < PageCountY; y++)
{
for (int x = 0; x < PageCountX; x++)
{
Add(new LayoutPage { Selected = false, X = x, Y = y });
}
}
}
}
}