-
Notifications
You must be signed in to change notification settings - Fork 6
/
ToSendListBox.cs
108 lines (101 loc) · 4.31 KB
/
ToSendListBox.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DigiRite
{
class ToSendListBox : System.Windows.Forms.CheckedListBox
{ /* Custom sort the checkListBoxToSend */
public ToSendListBox() : base()
{ }
public new void Sort()
{
// https://stackoverflow.com/questions/3012647/custom-listbox-sorting
if (this.Items.Count > 1)
{
bool swapped;
do
{
// bubble sort
int counter = this.Items.Count - 1;
swapped = false;
while (counter > 0)
{
SortedQueuedToSendListItem qsi1 = this.Items[counter] as SortedQueuedToSendListItem;
SortedQueuedToSendListItem qsi0 = this.Items[counter-1] as SortedQueuedToSendListItem;
if ((null != qsi1) && (null != qsi0))
{
if (qsi1.qp.QsoPriority(qsi1.q) <
qsi0.qp.QsoPriority(qsi0.q))
{
var qsi1Check = GetItemChecked(counter);
var qsi0Check = GetItemChecked(counter-1);
object temp = Items[counter];
this.Items[counter] = this.Items[counter - 1];
this.Items[counter - 1] = temp;
swapped = true;
if (qsi1Check != qsi0Check)
{
// The CheckedListBox ItemChecked stayed
// at the old item index (surprise!)
// fix that
SetItemChecked(counter, qsi0Check);
SetItemChecked(counter - 1, qsi1Check);
}
}
}
counter -= 1;
}
}
while (swapped);
}
}
}
class QueuedToSendListItem
{
public QueuedToSendListItem(string s, QsoInProgress q, QsoSequencer.MessageSent ms = null)
{
this.s = s;
this.q = q;
this.messageSentCb = ms;
}
public override string ToString()
{ return s; }
public String MessageText { get { return s; } set { s = value; } }
public QsoSequencer.MessageSent MessageSent { get { return messageSentCb; } }
public QsoInProgress q;
private string s;
private QsoSequencer.MessageSent messageSentCb;
}
// priority goes to Qsos in progress with fewer CyclesSinceMessaged
class QueuedToSendListItemComparer : IComparer<KeyValuePair<int, QueuedToSendListItem>>
{
public int Compare(KeyValuePair<int, QueuedToSendListItem> x, KeyValuePair<int, QueuedToSendListItem> y)
{
var xCyclesSinceMessaged = x.Value.q.CyclesSinceMessaged;
var yCyclesSinceMessaged = y.Value.q.CyclesSinceMessaged;
// If we transmitted to a guy at his most recent TX slot, and if we didn't
// copy him this RX slot, then give him one RX slot grace period
if ((xCyclesSinceMessaged == 1) && x.Value.q.TransmitedLastOpportunity)
xCyclesSinceMessaged = 0;
if ((yCyclesSinceMessaged == 1) && y.Value.q.TransmitedLastOpportunity)
yCyclesSinceMessaged = 0;
int ret = -1;
// more recently heard from sort to smaller numbers
if (xCyclesSinceMessaged < yCyclesSinceMessaged)
ret = -1;
else if (xCyclesSinceMessaged > yCyclesSinceMessaged)
ret = 1;
else // same age. return sort of original keys
ret = x.Key < y.Key ? -1 : (x.Key == y.Key ? 0 : 1);
return ret;
}
}
class SortedQueuedToSendListItem : QueuedToSendListItem
{
public QsosPanel qp;
public SortedQueuedToSendListItem(string s, QsoInProgress q, QsosPanel qp, QsoSequencer.MessageSent ms) : base(s,q, ms)
{ this.qp = qp; }
}
}