-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
208 lines (187 loc) · 5.46 KB
/
mainwindow.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
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
#include "mainwindow.h"
#include "elliptictable.h"
#include "ellipticscene.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <math.h>
int g_mod_p = 59;
int g_par_a = 2;
int g_par_b = 4;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow), mDisableHandler(false), mFirstResize(true)
{
ui->setupUi(this);
mTable = new EllipticTable(this);
ui->tableView->setMaximumWidth(160);
ui->tableView->setModel(mTable);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
connect(ui->tableView->selectionModel(),&QItemSelectionModel::currentChanged,this,&MainWindow::handleCurrentChanged);
for (int i=0; i<3; i++)
ui->tableView->setColumnWidth(i,48);
mScene = new EllipticScene(this);
ui->graphicsView->setScene(mScene);
QHBoxLayout*ll = new QHBoxLayout(ui->legend);
ll->setMargin(3);
ui->legend->setAutoFillBackground(true);
// on_spinMod_valueChanged(g_mod_p);
connect(mScene,&EllipticScene::pointSelected,this,&MainWindow::handlePointClicked);
mTimer = new QTimer(this);
connect(mTimer,&QTimer::timeout,this,&MainWindow::handleTimer);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::handlePointClicked(const QPoint& p)
{
mTable->setEntries(p);
selectRow(0);
}
void MainWindow::selectRow(int irow)
{
QModelIndex inx = mTable->index(irow,0);
ui->tableView->selectionModel()->select(inx,QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
ui->tableView->setCurrentIndex(inx);
}
void MainWindow::handleCurrentChanged(const QModelIndex& current, const QModelIndex& previous = QModelIndex())
{
Q_UNUSED(previous);
QPoint selectedPoint = mTable->getPoint(current);
mScene->selectPoint(selectedPoint);
}
void MainWindow::handleTimer()
{
QModelIndex inx = ui->tableView->currentIndex();
if (!inx.isValid()) return;
int n = mTable->rowCount();
int irow = inx.row() >= n-1 ? 0 : inx.row() + 1;
selectRow(irow);
}
void MainWindow::on_checkLoop_stateChanged(int loopState)
{
if (loopState)
mTimer->start(500);
else
mTimer->stop();
}
void MainWindow::on_spinA_valueChanged(int val)
{
if (mDisableHandler) return;
mDisableHandler = true;
g_par_a = val;
resetTable();
mDisableHandler = false;
}
void MainWindow::on_spinB_valueChanged(int val)
{
if (mDisableHandler) return;
mDisableHandler = true;
g_par_b = val;
resetTable();
mDisableHandler = false;
}
bool MainWindow::isPrimeNumber(int x) const
{
for (int j = 2; j <= sqrt(x); j++)
if (x%j==0) return false;
return true;
}
void MainWindow::on_spinMod_valueChanged(int val)
{
if (mDisableHandler) return;
mDisableHandler = true;
if (!isPrimeNumber(val))
{
if (val < 7)
val = 7;
else
{
int step = val > g_mod_p ? 1 : -1;
do {
val += step;
} while(!isPrimeNumber(val));
}
ui->spinMod->setValue(val);
}
g_mod_p = val;
if (ui->spinA->value() >= val) ui->spinA->setValue(val-1);
ui->spinA->setMaximum(val-1);
if (ui->spinB->value() >= val) ui->spinB->setValue(val-1);
ui->spinB->setMaximum(val-1);
resetTable();
mDisableHandler = false;
}
void MainWindow::resizeEvent(QResizeEvent *evt)
{
if (mDisableHandler) return;
Q_UNUSED(evt);
if (mFirstResize)
{
mDisableHandler = true;
ui->spinA->setValue(g_par_a);
ui->spinB->setValue(g_par_b);
ui->spinMod->setValue(g_mod_p);
mFirstResize = false;
resetTable();
mDisableHandler = false;
}
else
{
QModelIndex inx = ui->tableView->currentIndex();
mDisableHandler = true;
updateCanvas();
mDisableHandler = false;
if (inx.isValid()) handleCurrentChanged(inx);
}
}
void MainWindow::resetTable()
{
// valid elliptic curve ?
int valid = (4*g_par_a*g_par_a*g_par_a + 27*g_par_b*g_par_b) % g_mod_p;
ui->labValid->setText(QString("4a³+27b² = %1 mod p").arg(valid));
QPalette pal = ui->labValid->palette();
pal.setColor(ui->labValid->foregroundRole(),valid ? Qt::black : Qt::red);
ui->labValid->setPalette(pal);
updateCanvas();
mTable->setEntries(mScene->firstPoint());
generateLegend();
selectRow(0);
}
void MainWindow::updateCanvas()
{
float ftrans = height() - 90;
ftrans /= (g_mod_p + 4);
mScene->reset();
ui->graphicsView->setTransform(QTransform(ftrans,0,0,ftrans,500,2));
ui->graphicsView->update();
}
void MainWindow::generateLegend()
{
// update legend
QHBoxLayout* ll = static_cast<QHBoxLayout*>(ui->legend->layout());
for (QLabel* l : mLegend)
{
ll->removeWidget(l);
l->deleteLater();
}
mLegend.clear();
QLabel* l= new QLabel("1",this);
l->setMaximumWidth(36);
l->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ll->addWidget(l);
mLegend.append(l);
for (auto it = mScene->colorTable().constBegin(); it != mScene->colorTable().constEnd(); ++it)
{
l = new QLabel(QString::number(it.key()),this);
QPalette pal = l->palette();
pal.setColor(QPalette::Background,it.value());
l->setPalette(pal);
l->setAutoFillBackground(true);
l->setMaximumWidth(36);
l->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ll->addWidget(l);
mLegend.append(l);
}
}