forked from w5xd/Digi-Rite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQsosPanel.cs
544 lines (494 loc) · 19.3 KB
/
QsosPanel.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DigiRite
{
/* DigiRite has multiple QSOs "in progress" concurrently.
* This is shown visually in a PanelOfCheckBoxes, specialized here
* with some mouse event handling and on-screen drawing.
*/
class QsosPanel : PanelOfCheckBoxes
{
public QsosPanel(Panel tp, Label modelLabel, CheckBox modelCheckbox)
: base(tp, modelLabel, modelCheckbox)
{
tp.Controls.Clear();
tp.MouseDown += PanelRightMouseDown;
}
#region callbacks
public delegate void FillAlternatives(QsoInProgress qp);
FillAlternatives fillAlternativesCb;
public FillAlternatives fillAlternatives { set { fillAlternativesCb = value; } get => fillAlternativesCb; }
public delegate void LogAsIs(QsoInProgress qp);
LogAsIs logAsIsCb;
public LogAsIs logAsIs { set { logAsIsCb = value; } get => logAsIsCb; }
/* The callback linkage in DigiRite is convoluted:
* QsoInProgress.OnChangedCb calls us, but only through the lambda in our Add() method.
* Our own qsoActiveChanged is invoked from this panel's GUI interactions
*/
public delegate void QsoActiveChanged(QsoInProgress qp);
QsoActiveChanged qsoActiveChangedCb;
public QsoActiveChanged qsoActiveChanged { set { qsoActiveChangedCb = value; } get => qsoActiveChangedCb; }
public delegate void OnRemovedQso(QsoInProgress qp);
OnRemovedQso onRemovedQsoCb;
public OnRemovedQso onRemovedQso { set { onRemovedQsoCb = value; } get => onRemovedQsoCb; }
public delegate bool IsCurrentCycle(QsoInProgress qp);
IsCurrentCycle isCurrentCycleCb;
public IsCurrentCycle isCurrentCycle { set { isCurrentCycleCb = value; } get => isCurrentCycleCb; }
public delegate void OrderChanged();
OrderChanged orderChangedCb;
public OrderChanged orderChanged { set { orderChangedCb = value; } get => orderChangedCb; }
#endregion
#region state
private Dictionary<object, QsoInProgress> loggedInactive = new Dictionary<object, QsoInProgress>();
#endregion
// the underlying data is in the Control array
public List<QsoInProgress> QsosInProgress {
get {
List<QsoInProgress> ret = new List<QsoInProgress>();
foreach (var v in tlp.Controls)
{
QsoInProgressLabel ql = v as QsoInProgressLabel;
if (null != ql)
ret.Add(ql.qso);
}
return ret;
}
}
public Dictionary<object, QsoInProgress> QsosInProgressDictionary {
get {
Dictionary<object, QsoInProgress> ret = // includes loggedInactive as well as active
new Dictionary<object, QsoInProgress>(loggedInactive);
foreach (var qp in QsosInProgress)
ret[qp.GetKey()] = qp;
return ret;
}
}
public void Add(QsoInProgress qp)
{
int addedIndex = tlp.Controls.Count / 2;
QsoInProgressLabel lb = new QsoInProgressLabel(qp, colors);
lb.BackColor = lblBackColor;
lb.ForeColor = lblForeColor;
lb.AutoSize = false;
lb.isCurrentCycleCb = isCurrentCycleCb;
lb.Font = tlp.Font;
QsoCb cb = new QsoCb();
cb.GotFocus += lb.OnGetFocus;
cb.LostFocus += lb.OnLostFocus;
cb.Font = tlp.Font;
// set active and checked on add
cb.Checked = true;
cb.TabStop = addedIndex == 0;
qp.Active = true;
qp.OnChangedCb = new QsoInProgress.OnChanged(() =>
{
if (qp.IsLogged && !qp.Active)
{ // switch modes. qso is out of the gui and into loggedInactive
Remove(qp);
loggedInactive[qp.GetKey()] = qp;
qp.OnChangedCb = new QsoInProgress.OnChanged(() => OnLoggedInactiveChanged(qp));
}
else
{ // update GUI
lb.Invalidate();
if (cb.Checked != qp.Active)
cb.Checked = qp.Active;
}
});
cb.onRightMouse = new MouseEventHandler((object o, MouseEventArgs target) =>
{
RightMouseDown(o, qp, target);
});
lb.onRightMouse = new MouseEventHandler((object o, MouseEventArgs target) =>
{
RightMouseDown(o, qp, target);
});
tlp.Controls.Add(lb);
tlp.Controls.Add(cb);
lb.onClick += new EventHandler((object o, EventArgs e) =>
{
cb.Focus();
});
lb.onGetFocusCb += new FocusDrawingHelper.OnGetFocusCb(() => {
if (null != fillAlternativesCb)fillAlternativesCb(qp);
});
cb.CheckedChanged += new EventHandler((object o, EventArgs e) =>
{
OnCheck(o as CheckBox, qp);
});
lb.Size = lblSize;
cb.Size = cbSize;
PositionEntry(cb, lb, addedIndex);
qsoActiveChangedCb(qp);
}
public QsoInProgress FirstActive {
get {
foreach (var v in tlp.Controls)
{
QsoInProgressLabel ql = v as QsoInProgressLabel;
if ((null != ql) && ql.qso.Active)
return ql.qso;
}
return null;
}
}
public void Remove(QsoInProgress qp)
{
foreach (var c in tlp.Controls)
{
QsoInProgressLabel ql = c as QsoInProgressLabel;
if (null == ql)
continue;
if (Object.ReferenceEquals(qp, ql.qso))
{
int idx = tlp.Controls.IndexOf(ql);
IDisposable d1 = tlp.Controls[idx];
tlp.Controls.RemoveAt(idx);
IDisposable d2 = tlp.Controls[idx];
tlp.Controls.RemoveAt(idx);
d1.Dispose();
d2.Dispose();
qp.OnChangedCb = null;
SizeChanged(null,null); // redraw
if (null != orderChangedCb)
orderChangedCb();
return;
}
}
}
public int QsoPriority(QsoInProgress qp)
{ // linear search is not the fastest...
// ...but we're just not going to have more
// than a few dozen
int ret = 0;
for (int i = 0; i < tlp.Controls.Count; i++)
{
QsoInProgressLabel ql = tlp.Controls[i] as QsoInProgressLabel;
if (null == ql)
continue;
if (Object.ReferenceEquals(qp, ql.qso))
return 1 + ret;
else
ret += 1;
}
return ret;
}
public void RefreshOnScreen()
{ tlp.Invalidate(true); }
public void PurgeOldLoggedQsos(DateTime olderThan)
{
foreach (var qi in loggedInactive.ToList())
{
if (qi.Value.TimeOfLastReceived < olderThan)
loggedInactive.Remove(qi.Key);
}
}
private void OnLoggedInactiveChanged(QsoInProgress qp)
{
if (qp.Active)
{ // back into the GUI when it becomes active.
qp.OnChangedCb = null;
loggedInactive.Remove(qp.GetKey());
Add(qp);
}
}
// our GUI check box handler:
private void SetQsoActive(QsoInProgress qp, bool active)
{
if (qp.Active != active)
{
qp.Active = active; // note--invokes our lamda in Add()
qsoActiveChangedCb(qp);
}
}
private void OnCheck(CheckBox cb, QsoInProgress qp)
{
SetQsoActive(qp, cb.Checked);
fillAlternativesCb(qp);
}
private void PanelRightMouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
return;
ContextMenuStrip inProgressRightMouse;
List<ToolStripItem> toolStripItems = new List<ToolStripItem>();
{
var contextItem = new ToolStripMenuItem
{
Text = "&Inactive: remove all",
Tag = new Action(() => removeAllInactive())
};
contextItem.Click += inProgressContext_Click;
toolStripItems.Add(contextItem);
}
inProgressRightMouse = new ContextMenuStrip();
inProgressRightMouse.Items.AddRange(toolStripItems.ToArray());
System.Drawing.Point pos = Cursor.Position;
inProgressRightMouse.Show(pos);
inProgressRightMouse.Visible = true;
}
private void RightMouseDown(object sender, QsoInProgress q, MouseEventArgs e)
{
if (null != e // keyboard event forwards with null
&& e.Button != MouseButtons.Right)
return;
Control control = sender as Control;
int index = tlp.Controls.IndexOf(control) / 2;
ContextMenuStrip inProgressRightMouse;
List<ToolStripItem> toolStripItems = new List<ToolStripItem>();
{
var contextItem = new ToolStripMenuItem
{
Text = "&TX on their frequency",
Checked = q.Message.Hz == q.TransmitFrequency,
Tag = new Action(() =>
{
q.TransmitFrequency = q.TransmitFrequency == q.Message.Hz ?
0 : q.Message.Hz;
})
};
contextItem.Click += inProgressContext_Click;
toolStripItems.Add(contextItem);
}
{
var contextItem = new ToolStripMenuItem
{
Text = "&Active",
Checked = q.Active,
Tag = new Action(() =>
{
SetQsoActive(q, !q.Active);
})
};
contextItem.Click += inProgressContext_Click;
toolStripItems.Add(contextItem);
}
if (index > 0)
{
var tsi = new ToolStripMenuItem
{
Text = "Move QSO &Up",
Tag = new Action(() =>
{
int i = index * 2;
Control o1 = tlp.Controls[i];
Control o2 = tlp.Controls[1 + i];
tlp.Controls.SetChildIndex(o1, i-2);
tlp.Controls.SetChildIndex(o2, i-1);
SizeChanged(null, null);
if (null != orderChangedCb)
orderChangedCb();
})
};
tsi.Click += inProgressContext_Click;
toolStripItems.Add(tsi);
}
if (index < (tlp.Controls.Count / 2) - 1)
{
var tsi = new ToolStripMenuItem
{
Text = "Move QSO &Down",
Tag = new Action(() =>
{
int i = index * 2;
Control o1 = tlp.Controls[i];
Control o2 = tlp.Controls[1 + i];
tlp.Controls.SetChildIndex(o2, 3 + i);
tlp.Controls.SetChildIndex(o1, 2 + i);
SizeChanged(null,null);
if (null != orderChangedCb)
orderChangedCb();
})
};
tsi.Click += inProgressContext_Click;
toolStripItems.Add(tsi);
}
{
var contextItem = new ToolStripMenuItem
{
Text = "Log &QSO as-is",
Tag = new Action(() =>
{
logAsIsCb(q);
SetQsoActive(q, false);
})
};
contextItem.Click += inProgressContext_Click;
toolStripItems.Add(contextItem);
}
{
var contextItem = new ToolStripMenuItem
{
Text = "Alternative &Messages",
Tag = new Action(() => fillAlternativesCb(q))
};
contextItem.Click += inProgressContext_Click;
toolStripItems.Add(contextItem);
}
{
var contextItem = new ToolStripMenuItem
{
Text = "&Remove from List",
Tag = new Action(() => onRemovedQsoCb(q))
};
contextItem.Click += inProgressContext_Click;
toolStripItems.Add(contextItem);
}
{
var contextItem = new ToolStripMenuItem
{
Text = "&Inactive: remove all",
Tag = new Action(() => removeAllInactive())
};
contextItem.Click += inProgressContext_Click;
toolStripItems.Add(contextItem);
}
inProgressRightMouse = new ContextMenuStrip();
inProgressRightMouse.Items.AddRange(toolStripItems.ToArray());
System.Drawing.Point pos = (e != null) ?
Cursor.Position : /* mouse */
control.PointToScreen(control.Location) + control.Size; /* keyboard */
inProgressRightMouse.Show(pos);
inProgressRightMouse.Visible = true;
}
private void inProgressContext_Click(object sender, EventArgs e)
{
ToolStripMenuItem tmi = sender as ToolStripMenuItem;
if (null != tmi)
{
var action = tmi.Tag as Action;
if (null != action)
action();
}
}
private delegate bool ChooseThisQso(QsoInProgress q);
private void removeByCondition(ChooseThisQso choose)
{
bool foundOne = false;
for (; ; )
{
foundOne = false;
foreach (var c in tlp.Controls)
{
QsoInProgressLabel ql = c as QsoInProgressLabel;
if (null == ql)
continue;
if (choose(ql.qso))
{
int idx = tlp.Controls.IndexOf(ql);
tlp.Controls.RemoveAt(idx);
tlp.Controls.RemoveAt(idx);
foundOne = true;
break;
}
}
if (!foundOne)
break;
}
if (foundOne && null != orderChangedCb)
orderChangedCb();
SizeChanged(null, null);
}
public void removeAllInactive()
{ removeByCondition((QsoInProgress q) => !q.Active); }
}
class QsoInProgressLabel : FocusDrawingHelper
{
RttyRiteColors colors;
QsoInProgress qp;
public QsoInProgressLabel(QsoInProgress qp, RttyRiteColors colors)
{
this.qp = qp;
this.colors = colors;
}
public System.EventHandler onClick;
public MouseEventHandler onRightMouse;
public QsosPanel.IsCurrentCycle isCurrentCycleCb;
public QsoInProgress qso { get { return qp; } }
protected override void OnPaint(PaintEventArgs e)
{
bool isCurrent = true;
if (null != isCurrentCycleCb)
isCurrent = isCurrentCycleCb(qp);
System.Drawing.Color fillColor = BackColor;
if (!isCurrent)
fillColor = System.Drawing.Color.Gray;
using (System.Drawing.Brush fb = new System.Drawing.SolidBrush(fillColor))
e.Graphics.FillRectangle(fb, ClientRectangle);
bool doBck = false;
if (isCurrent)
{
doBck = qp.Dupe || qp.Mult; ;
if (qp.Dupe)
fillColor = colors.DupeBackGround;
else if (qp.Mult)
fillColor = colors.MultBackGround;
if (doBck)
{
String toRender = qp.ToString();
int toSkip = 2; // skip first two characters unconditionally
while (toSkip < toRender.Length && Char.IsWhiteSpace(toRender[toSkip]))
toSkip += 1;
string first = toRender.Substring(0, toSkip - 1);
string second = toRender.Substring(toSkip);
second = second.Trim();
var sze1 = TextRenderer.MeasureText(first, Font);
var sze2 = TextRenderer.MeasureText(second, Font);
System.Drawing.Rectangle toFill = ClientRectangle;
toFill.Size = sze2;
toFill.X += sze1.Width;
using (System.Drawing.Brush fcb = new System.Drawing.SolidBrush(fillColor))
e.Graphics.FillRectangle(fcb, toFill);
}
}
TextRenderer.DrawText(e.Graphics,
qp.ToString(),
Font,
ClientRectangle,
ForeColor,
TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
DrawFocusRect(e);
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
onClick(this, mevent);
else
onRightMouse(this, mevent);
}
// label control doesn't do what we want on these events:
protected override void OnMouseUp(MouseEventArgs mevent)
{ } // disable
protected override void OnClick(EventArgs e)
{ } // disable
}
class QsoCb : CheckBox
{
public MouseEventHandler onRightMouse;
protected override void OnMouseDown(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
base.OnMouseDown(mevent);
else
onRightMouse(this, mevent);
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
if (mevent.Button == MouseButtons.Left)
base.OnMouseUp(mevent);
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.P)
{
e.Handled = true;
e.SuppressKeyPress = true;
onRightMouse(this, null);
return;
}
base.OnKeyDown(e);
}
}
}