269 lines
8.9 KiB
C++
Executable File
269 lines
8.9 KiB
C++
Executable File
/**
|
|
* Model for authentication code table
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by hjl, SLSE, 2017-01-05
|
|
*/
|
|
|
|
|
|
#include "cod_authentication_code_model.h"
|
|
#include <QGuiApplication>
|
|
#include <QPalette>
|
|
|
|
using namespace CodificationManagement;
|
|
|
|
AuthenticationCodeModel::AuthenticationCodeModel(QObject *parent)
|
|
: QAbstractTableModel(parent)
|
|
, m_dtc(nullptr)
|
|
, m_codificationDataId(0)
|
|
, m_isEditMode(false)
|
|
, m_logger(getLogger("AuthenticationCodeTableModel"))
|
|
{
|
|
memset(m_matrix, 0, sizeof m_matrix);
|
|
DEBUG_LOG(m_logger) << "ctor AuthenticationCodeModel";
|
|
|
|
} // end of ctor()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool AuthenticationCodeModel::init(const std::shared_ptr<CodificationManagement::Interface::Dtc> &dtc,
|
|
int codificationDataId,
|
|
bool isEditMode)
|
|
{
|
|
DEBUG_LOG(m_logger) << "AuthenticationCodeModel::init()";
|
|
if (!dtc)
|
|
return false;
|
|
|
|
DEBUG_LOG(m_logger) << "AuthenticationCodeModel::init()...";
|
|
|
|
m_dtc = dtc;
|
|
m_codificationDataId = codificationDataId;
|
|
m_isEditMode = isEditMode;
|
|
|
|
// constant Header values...
|
|
m_ColumnSelectionValues.resize(5);
|
|
for (char i = 0; i < 5; ++i)
|
|
m_ColumnSelectionValues[i] = QLatin1Char('V' + i);
|
|
|
|
// constant Header values...
|
|
m_RowSelectionValues << QLatin1Char('A') << QLatin1Char('D') << QLatin1Char('E')
|
|
<< QLatin1Char('G') << QLatin1Char('H') << QLatin1Char('I')
|
|
<< QLatin1Char('L') << QLatin1Char('N') << QLatin1Char('O')
|
|
<< QLatin1Char('R') << QLatin1Char('S') << QLatin1Char('T')
|
|
<< QLatin1Char('U') ;
|
|
|
|
resetMatrix();
|
|
|
|
if (!isEditMode)
|
|
{
|
|
connect(m_dtc.get(), &CodificationManagement::Interface::Dtc::codificationDataChanged,
|
|
this, &AuthenticationCodeModel::onCodificationDataChanged);
|
|
}
|
|
return true;
|
|
} // end of init()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void AuthenticationCodeModel::resetMatrix()
|
|
{
|
|
for (int row = 0; row < rowCount(); ++row)
|
|
{
|
|
for (int column = 0; column < columnCount(); ++column)
|
|
{
|
|
m_matrix[row][column] = 0;
|
|
}
|
|
}
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, columnCount() - 1));
|
|
return ;
|
|
} // end of resetMatrix()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool AuthenticationCodeModel::setMatrixValuesCSV(const QString &csvData)
|
|
{
|
|
DEBUG_LOG(m_logger) << "setMatrixValuesCSV: " << csvData;
|
|
|
|
const QVector<QStringRef> slRows = csvData.splitRef(QLatin1Char('\n'));
|
|
if (slRows.size() != rowCount())
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid row data count." << " codificationDataId: " << m_codificationDataId
|
|
<< " Soll/Ist: " << rowCount() - 1 << "/" << slRows.size();
|
|
return false;
|
|
}
|
|
|
|
for (int row = 0; row < slRows.size(); ++row)
|
|
{
|
|
const QVector<QStringRef> slColumns = slRows[row].split(QLatin1Char(';'));
|
|
if (slColumns.size() != columnCount())
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid column data count." << " codificationDataId: " << m_codificationDataId
|
|
<< " Soll/Ist: " << columnCount() - 1 << "/" << slColumns.size();
|
|
return false;
|
|
}
|
|
|
|
for (int column = 0; column < columnCount(); ++column)
|
|
{
|
|
m_matrix[row][column] = slColumns.at(column).toInt();
|
|
}
|
|
}
|
|
return true;
|
|
} // end of setMatrixValuesCSV()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QString AuthenticationCodeModel::getMatrixDataCSVString() const
|
|
{
|
|
QStringList slColumns;
|
|
slColumns.reserve(columnCount());
|
|
QStringList slRows;
|
|
slRows.reserve(rowCount() * columnCount());
|
|
for (int row = 0; row < rowCount(); ++row)
|
|
{
|
|
slColumns.clear();
|
|
for (int column = 0; column < columnCount(); ++column)
|
|
{
|
|
slColumns << QStringLiteral("%1").arg(m_matrix[row][column], 2, 10, QLatin1Char('0'));
|
|
}
|
|
slRows << slColumns.join(QLatin1Char(';'));
|
|
}
|
|
return slRows.join(QLatin1Char('\n'));
|
|
} // end of getMatrixDataCSVString()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void AuthenticationCodeModel::setEditMode(bool isEditMode)
|
|
{
|
|
m_isEditMode = isEditMode;
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, columnCount() - 1));
|
|
} // end of setEditMode()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool AuthenticationCodeModel::isEditMode() const
|
|
{
|
|
return m_isEditMode;
|
|
} // end of isEditMode();
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int AuthenticationCodeModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return 13; // Matrixgroesse
|
|
} // end of rowCount()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int AuthenticationCodeModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return 5; // Matrixgroesse
|
|
} // end of columnCount()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QVariant AuthenticationCodeModel::headerData(int column, Qt::Orientation orientation, int role) const
|
|
{
|
|
if (role != Qt::DisplayRole)
|
|
return QAbstractTableModel::headerData(column, orientation, role);
|
|
|
|
if (orientation == Qt::Horizontal)
|
|
{
|
|
if (!m_ColumnSelectionValues.isEmpty())
|
|
return m_ColumnSelectionValues.at(column);
|
|
return QChar::fromLatin1('?');
|
|
}
|
|
if (orientation == Qt::Vertical)
|
|
{
|
|
if (!m_RowSelectionValues.isEmpty())
|
|
return m_RowSelectionValues.at(column);
|
|
return QChar::fromLatin1('?');
|
|
}
|
|
return QAbstractTableModel::headerData(column, orientation, role);
|
|
} // end of headerData()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QVariant AuthenticationCodeModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if ((role == Qt::DisplayRole || role == Qt::EditRole))
|
|
{
|
|
// DEBUG_LOG(m_logger) << "data() display role row: " <<index.row() << " col: " << index.column();
|
|
return QStringLiteral("%1").arg(m_matrix[index.row()][index.column()], 2, 10, QLatin1Char('0'));
|
|
}
|
|
|
|
if (role == Qt::TextAlignmentRole)
|
|
return Qt::AlignCenter;
|
|
|
|
if (role == Qt::BackgroundRole)
|
|
{
|
|
if (m_isEditMode)
|
|
// group ist hier egal, liefert immer die selbe Farbe
|
|
return QGuiApplication::palette().color(QPalette::Disabled, QPalette::AlternateBase); // RDE_LIGHTER_GRAY
|
|
else
|
|
return QGuiApplication::palette().color(QPalette::Disabled, QPalette::Base); // RDE_GRAY
|
|
}
|
|
return QVariant();
|
|
} // end of data()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool AuthenticationCodeModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
DEBUG_LOG(m_logger) << "setData() row/col: " << index.row() << "/" << index.column()
|
|
<< " char:" << value.toString() << " int:" << value.toInt();
|
|
|
|
bool bOk;
|
|
int val = value.toInt(&bOk);
|
|
|
|
if ((!bOk) || (val < 0) || (val > 99))
|
|
{
|
|
ERROR_LOG(m_logger) << "setData() row/col: " << index.row() << "/" << index.column()
|
|
<< " Invalid Input! " << value.toString() << " Required input value in the range 0-99.";
|
|
return false;
|
|
}
|
|
|
|
if (role == Qt::EditRole)
|
|
{
|
|
m_matrix[index.row()][index.column()] = val;
|
|
emit dataModified();
|
|
}
|
|
return true;
|
|
} // end of setData()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
Qt::ItemFlags AuthenticationCodeModel::flags(const QModelIndex &index) const
|
|
{
|
|
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
|
|
|
|
if ((index.row() <= rowCount()-1) && (index.column() <= columnCount() - 1))
|
|
{
|
|
if (isEditMode())
|
|
flags |= Qt::ItemIsEditable;
|
|
else
|
|
flags &= ~Qt::ItemIsEditable;
|
|
}
|
|
return flags;
|
|
} // end of flags()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void AuthenticationCodeModel::onCodificationDataChanged(const CodificationManagement::CodificationData &data,
|
|
DataModification modType)
|
|
{
|
|
const int id = data.id();
|
|
|
|
DEBUG_LOG(m_logger) << "onCodificationDataChanged for CodificationData id: " << id
|
|
<< " - AuthenticationCodeMatrix data: " << data.authenticationTableMatirx().c_str()
|
|
<< " - modType: " << (int)modType;
|
|
|
|
if (m_codificationDataId != data.id())
|
|
return;
|
|
|
|
if (modType == DataModification::eRemoved)
|
|
{
|
|
WARNING_LOG(m_logger) << "CodificationData was removed. id: " << id << " ToDo: 'ShowCodificationData' Tab loeschen !?";
|
|
// set matrix to default values
|
|
resetMatrix();
|
|
return;
|
|
}
|
|
|
|
if (modType == DataModification::eModified)
|
|
{
|
|
DEBUG_LOG(m_logger) << "CodificationData was modifed. id: " << id << " - " << data.note();
|
|
|
|
QString qs = QString::fromStdString(data.authenticationTableMatirx());
|
|
setMatrixValuesCSV(qs);
|
|
}
|
|
} // end of onCodificationDataChanged()
|
|
//-----------------------------------------------------------------------------
|