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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SQMTableModel::CalculateSqmMatrix(int startRow) {
|
|
|
|
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 18:20:00 +02:00
|
|
|
// Fill vector with 0
|
|
|
|
vector<int> colSqn, colMul;
|
|
|
|
for (int i = 0; i <= binLen; i++) {
|
|
|
|
colSqn.push_back(0);
|
|
|
|
colMul.push_back(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-22 22:43:44 +02:00
|
|
|
// Initialize bin column
|
|
|
|
vector<int> colBin;
|
2021-05-28 18:20:00 +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);
|
|
|
|
sqmMatrix.push_back(colSqn);
|
|
|
|
sqmMatrix.push_back(colMul);
|
|
|
|
|
2021-05-22 22:43:44 +02:00
|
|
|
|
|
|
|
for (int i = startRow; i < binLen; i++) {
|
|
|
|
if (i == 0) {
|
|
|
|
// Initialize first row
|
|
|
|
sqmMatrix.at(1).at(0) = 1;
|
|
|
|
sqmMatrix.at(2).at(0) = base;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Square Multiply
|
|
|
|
sqmMatrix.at(1).at(i) = sqmMatrix.at(2).at(i - 1) * sqmMatrix.at(2).at(i - 1);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|