2021-05-22 22:43:44 +02:00
|
|
|
#include "sqmtablemodel.h"
|
|
|
|
|
|
|
|
|
|
|
|
SQMTableModel::SQMTableModel(QObject *parent)
|
|
|
|
: QAbstractTableModel(parent) {
|
|
|
|
}
|
|
|
|
|
2021-05-24 23:25:17 +02:00
|
|
|
|
2021-05-28 17:18:56 +02:00
|
|
|
int SQMTableModel::rowCount(const QModelIndex & /*parent*/) const {
|
|
|
|
return 2;
|
2021-05-24 23:25:17 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 17:18:56 +02:00
|
|
|
int SQMTableModel::columnCount(const QModelIndex & /*parent*/) const {
|
2021-05-24 23:25:17 +02:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-22 22:43:44 +02:00
|
|
|
QVariant SQMTableModel::data(const QModelIndex &index, int role) const {
|
2021-05-28 18:20:00 +02:00
|
|
|
try {
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
int row = index.row();
|
|
|
|
int col = index.column();
|
2021-05-22 22:43:44 +02:00
|
|
|
|
2021-05-28 18:20:00 +02:00
|
|
|
return sqmMatrix.at(col).at(row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
return QVariant();
|
2021-05-22 22:43:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 17:18:56 +02:00
|
|
|
QVariant SQMTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
|
|
|
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
|
|
|
|
switch (section) {
|
|
|
|
case 0:
|
|
|
|
return QString("BIN");
|
|
|
|
case 1:
|
|
|
|
return QString("SQN");
|
|
|
|
case 2:
|
|
|
|
return QString("MUL");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2021-05-24 23:25:17 +02:00
|
|
|
|
2021-05-28 16:49:53 +02:00
|
|
|
bool SQMTableModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
|
|
|
if (role == Qt::EditRole) {
|
|
|
|
if (!checkIndex(index)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int row = index.row();
|
|
|
|
int col = index.column();
|
|
|
|
|
|
|
|
|
|
|
|
// call calculateSqmMatrix
|
|
|
|
sqmMatrix.at(col).at(row) = value.toInt();
|
|
|
|
CalculateSqmMatrix(row);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Qt::ItemFlags SQMTableModel::flags(const QModelIndex &index) const {
|
|
|
|
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-22 22:43:44 +02:00
|
|
|
void SQMTableModel::SetStartValues(int pBase, int pExp, int pMod) {
|
|
|
|
base = pBase;
|
|
|
|
exp = pExp;
|
|
|
|
mod = pMod;
|
2021-05-28 18:20:00 +02:00
|
|
|
|
|
|
|
CalculateSqmMatrix(0);
|
2021-05-22 22:43:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-24 12:12:42 +02:00
|
|
|
// Convert int to binary
|
2021-05-22 22:43:44 +02:00
|
|
|
std::string SQMTableModel::IntToBinary(int n) {
|
|
|
|
string bin;
|
|
|
|
int mask = 1;
|
|
|
|
while (n != 0) {
|
|
|
|
bin += (n & mask) == 0 ? "0" : "1";
|
|
|
|
n = n >> 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bin;
|
|
|
|
}
|
|
|
|
|
2021-05-28 19:32:05 +02:00
|
|
|
void SQMTableModel::CalculateSqmMatrix() {
|
|
|
|
// Clear sqmMatrix
|
|
|
|
sqmMatrix.clear()
|
|
|
|
|
|
|
|
// Calculate binary of exponent
|
2021-05-22 22:43:44 +02:00
|
|
|
string bin = IntToBinary(exp);
|
2021-05-24 23:25:17 +02:00
|
|
|
binLen = bin.length();
|
2021-05-22 22:43:44 +02:00
|
|
|
|
2021-05-28 19:32:05 +02:00
|
|
|
// Init BIN Column
|
2021-05-22 22:43:44 +02:00
|
|
|
vector<int> colBin;
|
2021-05-28 19:32:05 +02:00
|
|
|
for (int i = 0; i < binLen; i++) {
|
2021-05-22 22:43:44 +02:00
|
|
|
colBin.push_back(bin[i] - '0');
|
|
|
|
}
|
2021-05-28 18:20:00 +02:00
|
|
|
sqmMatrix.push_back(colBin);
|
2021-05-28 19:32:05 +02:00
|
|
|
|
|
|
|
// Init SQN & MUL Column
|
|
|
|
vector<int> colSqn, colMul;
|
|
|
|
colSqn.push_back(1);
|
|
|
|
colMul.push_back(base);
|
2021-05-28 18:20:00 +02:00
|
|
|
sqmMatrix.push_back(colSqn);
|
|
|
|
sqmMatrix.push_back(colMul);
|
|
|
|
|
2021-05-22 22:43:44 +02:00
|
|
|
|
2021-05-28 19:32:05 +02:00
|
|
|
// Calculate SQM
|
|
|
|
for (int i = 0; i < binLen; i++) {
|
|
|
|
sqmMatrix.at(1).push_back((sqmMatrix.at(2).at(i - 1) * sqmMatrix.at(2).at(i - 1)) % mod);
|
|
|
|
|
|
|
|
if (sqmMatrix.at(0).at(i) == 0) {
|
|
|
|
sqmMatrix.at(2).push_back(sqmMatrix.at(1).at(i));
|
2021-05-22 22:43:44 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-05-28 19:32:05 +02:00
|
|
|
sqmMatrix.at(2).push_back((sqmMatrix.at(1).at(i) * base) % mod);
|
2021-05-22 22:43:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-28 19:38:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
void SQMTableModel::UpdateSqmMatrix(QModelIndex startIndex) {
|
|
|
|
int start_row = startIndex.row();
|
|
|
|
int start_col = startIndex.column();
|
|
|
|
|
|
|
|
// If change appeared in col 2, start in next row
|
|
|
|
if (start_col == 2) {
|
|
|
|
start_row++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update sqmMatrix
|
|
|
|
for (int i = start_row; i < binLen; i++) {
|
|
|
|
sqmMatrix.at(1).at(i) = (sqmMatrix.at(2).at(i - 1) * sqmMatrix.at(2).at(i - 1)) % mod;
|
|
|
|
|
|
|
|
if (sqmMatrix.at(0).at(i) == 0) {
|
|
|
|
sqmMatrix.at(2).at(i) = sqmMatrix.at(1).at(i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sqmMatrix.at(2).at(i) = (sqmMatrix.at(1).at(i) * base) % mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|