-
Notifications
You must be signed in to change notification settings - Fork 0
/
elliptictable.cpp
138 lines (116 loc) · 3.36 KB
/
elliptictable.cpp
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
#include "elliptictable.h"
#include <QColor>
extern int g_mod_p;
extern int g_par_a;
extern int g_par_b;
//-------------------------------------------
EllipticTable::EllipticTable(QObject *parent) : QAbstractTableModel(parent)
{;}
void EllipticTable::setEntries(const QPoint &startP)
{
mEditIndex = QModelIndex();
beginResetModel();
mEntries.clear();
mEntries.append(EllipticPoint(startP.x(),startP.y()));
calculate();
endResetModel();
}
void EllipticTable::calculate()
{
if (mEntries.isEmpty()) return;
int inx = mEntries.size()-1;
int badcount = 0;
EllipticPoint startPoint = mEntries.at(inx);
EllipticPoint nextPoint = startPoint + mEntries.at(0);
while (nextPoint.isValid() && nextPoint != startPoint)
{
if (!nextPoint.isOnCurve()) ++badcount;
if (badcount >= 3) return;
mEntries.append(nextPoint);
inx++;
nextPoint = nextPoint + mEntries.at(0);
}
}
QPoint EllipticTable::getPoint(const QModelIndex& inx) const
{
if (!inx.isValid() || inx.row() >= mEntries.size()) return QPoint();
return mEntries.at(inx.row());
}
int EllipticTable::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return mEntries.size();
}
int EllipticTable::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 3;
}
QVariant EllipticTable::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) return QVariant();
if (orientation != Qt::Horizontal) return QVariant();
switch (section)
{
case 0: return "FAC";
case 1: return "X";
case 2: return "Y";
}
return QVariant();
}
bool EllipticTable::smallerIndex(const QModelIndex& inx) const
{
if (!mEditIndex.isValid()) return true;
if (inx.row() < mEditIndex.row()) return true;
if (inx.row() > mEditIndex.row()) return false;
return (inx.column() < mEditIndex.column());
}
QVariant EllipticTable::data(const QModelIndex &index, int role) const
{
if (role == Qt::TextAlignmentRole)
return Qt::AlignRight;
if (index.row() >= mEntries.size()) return QVariant();
const EllipticPoint& pdata = mEntries.at(index.row());
if (role == Qt::TextColorRole)
{
if (pdata.isOnCurve()) return QColor(Qt::black);
return QColor(Qt::darkRed);
}
if (role!= Qt::DisplayRole) return QVariant();
if (!index.column()) return index.row() + 1;
if (pdata.isInfinity()) return QString(QChar(8734));
return (index.column()==1 ? pdata.x() : pdata.y());
}
bool EllipticTable::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole) return false;
int irow = index.row();
if (irow >= mEntries.size()) return false;
bool ok;
int ival = value.toInt(&ok);
if (!ok) return false;
EllipticPoint lastP = mEntries.at(irow);
switch(index.column())
{
case 1:
lastP.setX(ival);
break;
case 2:
lastP.setY(ival);
break;
default:
return false;
}
beginResetModel();
mEntries[irow] = lastP;
for (int k = mEntries.size()-1; k>irow; k--)
mEntries.removeAt(k);
calculate();
endResetModel();
return true;
}
Qt::ItemFlags EllipticTable::flags(const QModelIndex &index) const
{
Q_UNUSED(index)
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
}