437 lines
14 KiB
C++
Executable File
437 lines
14 KiB
C++
Executable File
/**
|
|
* Model for numeral code table
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by hjl, SLSE, 2016-12-09
|
|
*/
|
|
|
|
|
|
#include "cod_numeral_code_model.h"
|
|
#include <LtsWidgets/lts_message_dialog.h>
|
|
#include <QGuiApplication>
|
|
#include <QPalette>
|
|
|
|
using namespace CodificationManagement;
|
|
|
|
NumeralCodeModel::NumeralCodeModel(QWidget *parent)
|
|
: QAbstractTableModel(parent)
|
|
, m_dtc(nullptr)
|
|
, m_codificationDataId(0)
|
|
, m_isEditMode(false)
|
|
, m_logger(getLogger("NumeralCodeTableModel"))
|
|
{
|
|
} // end of ctor()
|
|
|
|
//-----------------------------------------------------------------------------
|
|
bool NumeralCodeModel::init(const std::shared_ptr<CodificationManagement::Interface::Dtc> &dtc,
|
|
int codificationDataId,
|
|
bool isEditMode)
|
|
{
|
|
if (!dtc)
|
|
return false;
|
|
|
|
m_dtc = dtc;
|
|
m_codificationDataId = codificationDataId;
|
|
m_isEditMode = isEditMode;
|
|
|
|
|
|
m_MatrixNumeralSet.resize(10);
|
|
for (int i = 0; i < 10; ++i)
|
|
m_MatrixNumeralSet[i] = QLatin1Char('0' + i);
|
|
|
|
m_MatrixCharacterSet1.resize(26);
|
|
for (int i = 0; i < 26; ++i)
|
|
m_MatrixCharacterSet1[i] = QLatin1Char('A' + i);
|
|
|
|
m_MatrixCharacterSet2 << QLatin1Char('D') << QLatin1Char('E') << QLatin1Char('G') << QLatin1Char('H') << QLatin1Char('I') << QLatin1Char('L')
|
|
<< QLatin1Char('N') << QLatin1Char('O') << QLatin1Char('R') << QLatin1Char('S') << QLatin1Char('T') << QLatin1Char('U');
|
|
|
|
resetHeader();
|
|
initMatrix();
|
|
|
|
if (!isEditMode)
|
|
{
|
|
connect(m_dtc.get(), &CodificationManagement::Interface::Dtc::codificationDataChanged,
|
|
this, &NumeralCodeModel::onCodificationDataChanged);
|
|
}
|
|
return true;
|
|
} // end of init()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void NumeralCodeModel::resetHeader()
|
|
{
|
|
// DefaultValues... werden spaeter mit den Werten aus csv-Datei oder DB gesetzt.
|
|
m_ColumnSelectionValues = QVector<QChar>(columnCount() - 1, QLatin1Char('?'));
|
|
m_RowSelectionValues = QVector<QChar>(rowCount() - 1, QLatin1Char('?'));
|
|
} // end of resetHeader()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool NumeralCodeModel::setColumnSelectionValuesCSV(const QString &csvData)
|
|
{
|
|
m_ColumnSelectionValues = QVector<QChar>(columnCount() - 1, QLatin1Char(' '));
|
|
|
|
const QVector<QStringRef> slColumns = csvData.splitRef(QLatin1Char(';'));
|
|
|
|
if (slColumns.size() != columnCount() - 1)
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid column data count." << " codificationDataId: " << m_codificationDataId
|
|
<< " Soll/Ist: " << columnCount() - 1 << "/" << slColumns.size();
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < columnCount() - 1; ++i)
|
|
{
|
|
const QStringRef &qs = slColumns.at(i);
|
|
if (qs.size() != 1)
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid column data value at id: " << i
|
|
<< " codificationDataId: " << m_codificationDataId;
|
|
return false;
|
|
}
|
|
m_ColumnSelectionValues[i] = qs.at(0);
|
|
}
|
|
emit dataChanged(createIndex(0, 0), createIndex(0, columnCount() - 1));
|
|
return true;
|
|
} // end of setColumnSelectionValuesCSV()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool NumeralCodeModel::setRowSelectionValuesCSV(const QString &csvData)
|
|
{
|
|
m_RowSelectionValues = QVector<QChar>(rowCount() - 1, QLatin1Char(' '));
|
|
const QVector<QStringRef> slRows = csvData.splitRef(QStringLiteral("\n"));
|
|
|
|
if (slRows.size() != rowCount() - 1)
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid row data count." << " codificationDataId: " << m_codificationDataId
|
|
<< " Soll/Ist: " << rowCount() - 1 << "/" << slRows.size()
|
|
<< " Default values will be used. ";
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < rowCount() - 1; ++i)
|
|
{
|
|
const QStringRef &qs = slRows.at(i);
|
|
if (qs.size() != 1)
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid row data value at id: " << i
|
|
<< " codificationDataId: " << m_codificationDataId;
|
|
return false;
|
|
}
|
|
m_RowSelectionValues[i] = qs.at(0);
|
|
}
|
|
emit dataChanged(createIndex(0, 0), createIndex(0, columnCount() - 1));
|
|
return true;
|
|
} // end of setRowSelectionValuesCSV()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QString NumeralCodeModel::getHorizontalHeaderCSVString() const
|
|
{
|
|
QStringList slColumns;
|
|
slColumns.reserve(columnCount() - 1);
|
|
for (int i = 0; i < columnCount() -1; ++i)
|
|
{
|
|
slColumns << QStringLiteral("%1").arg(m_ColumnSelectionValues.at(i));
|
|
}
|
|
return slColumns.join(QLatin1Char(';'));
|
|
} // end of getHorizontalHeaderCSVString()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QString NumeralCodeModel::getVerticalHeaderCSVString() const
|
|
{
|
|
QStringList slRows;
|
|
slRows.reserve(rowCount() - 1);
|
|
for (int i = 0; i < rowCount() - 1; ++i)
|
|
{
|
|
slRows << QStringLiteral("%1").arg(m_RowSelectionValues.at(i));
|
|
}
|
|
return slRows.join(QStringLiteral("\n"));
|
|
} // end of getVerticalHeaderCSVString()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void NumeralCodeModel::setEditMode(bool isEditMode)
|
|
{
|
|
m_isEditMode = isEditMode;
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, columnCount() - 1));
|
|
} // end of setEditMode()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool NumeralCodeModel::isEditMode() const
|
|
{
|
|
return m_isEditMode;
|
|
} // end of isEditMode();
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int NumeralCodeModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return 14; // Matrixgroesse + 1 Character fuer RowIndex
|
|
} // end of rowCount()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int NumeralCodeModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return 14; // Matrixgroesse + 1 Character fuer ColumnIndex
|
|
} // end of columnCount()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QVariant NumeralCodeModel::headerData(int column, Qt::Orientation orientation, int role) const
|
|
{
|
|
if (role == Qt::DisplayRole )
|
|
{
|
|
if (isEditMode())
|
|
{
|
|
// Im header nichts anzeigen bzw. in der view die Vertical/horizontal header ausblenden
|
|
return QVariant();
|
|
}
|
|
else
|
|
{
|
|
if (column == 0)
|
|
return QVariant();
|
|
if (orientation == Qt::Horizontal)
|
|
{
|
|
if (!m_ColumnSelectionValues.isEmpty())
|
|
return m_ColumnSelectionValues.at(column - 1);
|
|
return QChar::fromLatin1('?');
|
|
}
|
|
if (orientation == Qt::Vertical)
|
|
{
|
|
if (!m_RowSelectionValues.isEmpty())
|
|
return m_RowSelectionValues.at(column - 1);
|
|
return QChar::fromLatin1('?');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (role == Qt::BackgroundRole)
|
|
{
|
|
if (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 QAbstractTableModel::headerData(column, orientation, role);
|
|
} // end of headerData()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QVariant NumeralCodeModel::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();
|
|
if ((index.column() == 0) && (index.row() == 0))
|
|
return QVariant();
|
|
|
|
// row index ==> column index values
|
|
if (index.row() == 0)
|
|
{
|
|
if (!m_ColumnSelectionValues.isEmpty())
|
|
return m_ColumnSelectionValues[index.column() - 1];
|
|
return QChar::fromLatin1('?');
|
|
}
|
|
|
|
// column index ==> row index values
|
|
if ((index.column() == 0))
|
|
{
|
|
if (!m_RowSelectionValues.isEmpty())
|
|
return m_RowSelectionValues[index.row() - 1];
|
|
return QChar::fromLatin1('?');
|
|
}
|
|
|
|
// Code Matrix
|
|
if ((index.column() > 0) && (index.row() > 0))
|
|
{
|
|
return m_matrix[index.row() - 1][index.column() - 1]; // row/column fuer header abziehen
|
|
}
|
|
|
|
// return QString(QLatin1String("R%1, C%2"))
|
|
// .arg(index.row() +1)
|
|
// .arg(index.column() +1 );
|
|
|
|
}
|
|
|
|
if (role == Qt::TextAlignmentRole)
|
|
return Qt::AlignCenter;
|
|
|
|
if (role == Qt::BackgroundRole)
|
|
{
|
|
if ((index.row() > 0) && (index.column() > 0))
|
|
// Matrix immer disabled
|
|
return QGuiApplication::palette().color(QPalette::Disabled, QPalette::Base); // RDE_GRAY
|
|
else
|
|
{
|
|
if (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
|
|
}
|
|
}
|
|
|
|
if (role == Qt::ForegroundRole)
|
|
{
|
|
if ((index.row() == 0) && (index.column() == 0))
|
|
return QColor(Qt::red);
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
} // end of data()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool NumeralCodeModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
DEBUG_LOG(m_logger) << "setData() row/col: " << index.row() << "/" << index.column()
|
|
<< " char:" << value.toString();
|
|
|
|
if (value.toString().contains(QLatin1Char('\"')) || value.toString().contains(QLatin1Char(';')))
|
|
{
|
|
LtsWidgets::LtsMessageDialog::showMessage(qobject_cast<QWidget *>(parent()), tr("Inputs with the characters ; \" are not possible."), tr("Input Error"));
|
|
return false;
|
|
}
|
|
|
|
QString tmp = value.toString();
|
|
if (tmp.isEmpty())
|
|
return false;
|
|
|
|
const QChar c = tmp.at(0);
|
|
|
|
if (role == Qt::EditRole)
|
|
{
|
|
// Input for ColumnSelectionValues
|
|
if (index.row() == 0)
|
|
m_ColumnSelectionValues[index.column() - 1] = c;
|
|
// Input for RowSelectionValues
|
|
if (index.column() == 0 && (index.row() > 0))
|
|
m_RowSelectionValues[index.row() - 1] = c;
|
|
emit dataModified();
|
|
}
|
|
|
|
return true;
|
|
} // end of setData()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
Qt::ItemFlags NumeralCodeModel::flags(const QModelIndex &index) const
|
|
{
|
|
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
|
|
if (((index.row() == 0) && (index.column() > 0)) ||
|
|
((index.row() > 0) && (index.column() == 0)))
|
|
{
|
|
if (isEditMode())
|
|
flags |= Qt::ItemIsEditable;
|
|
else
|
|
flags &= ~Qt::ItemIsEditable;
|
|
}
|
|
return flags;
|
|
} // end of flags()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void NumeralCodeModel::initMatrix()
|
|
{
|
|
int lastMatrixValueCnt = 0;
|
|
int lastMatrixCharacterCnt = 0;
|
|
int lastMatrixCharacterIndex = 0;
|
|
int numOfCharacter = 4;
|
|
|
|
for (int row = 0; row < 13; ++row)
|
|
{
|
|
// Neue Zeile mit Ziffer der letzten Zeile beginnen
|
|
if (row > 3)
|
|
{
|
|
numOfCharacter = 3;
|
|
lastMatrixValueCnt -= 1;
|
|
}
|
|
|
|
// Ab Zeile 9 CharacterSet2 verwenden
|
|
if (row == 9)
|
|
lastMatrixCharacterIndex = 0;
|
|
|
|
for (int col = 0; col < 13; ++col)
|
|
{
|
|
if (lastMatrixValueCnt < 10)
|
|
{
|
|
m_matrix[row][col] = m_MatrixNumeralSet.at(lastMatrixValueCnt);
|
|
lastMatrixValueCnt += 1;
|
|
|
|
if (lastMatrixValueCnt == 10)
|
|
lastMatrixCharacterCnt = 0;
|
|
}
|
|
else
|
|
{
|
|
if (row < 9)
|
|
m_matrix[row][col] = m_MatrixCharacterSet1.at(lastMatrixCharacterIndex);
|
|
else
|
|
m_matrix[row][col] = m_MatrixCharacterSet2.at(lastMatrixCharacterIndex);
|
|
lastMatrixCharacterCnt += 1;
|
|
lastMatrixCharacterIndex += 1;
|
|
|
|
if (lastMatrixCharacterCnt == numOfCharacter)
|
|
lastMatrixValueCnt = 0;
|
|
}
|
|
|
|
// Im ersten Teil den CharacterSet1 verwenden
|
|
if ((row < 9) && (lastMatrixCharacterIndex == 26))
|
|
lastMatrixCharacterIndex = 0;
|
|
|
|
// Im restlichen Teil den CharacterSet2 verwenden
|
|
if ((row >= 9) && (lastMatrixCharacterIndex == 12))
|
|
lastMatrixCharacterIndex = 0;
|
|
}
|
|
}
|
|
|
|
// ---- Check m_matrix---
|
|
// QString qs;
|
|
// for (int row = 0; row < 13; ++row)
|
|
// {
|
|
// for (int col = 0; col < 13; ++col)
|
|
// {
|
|
// qs.append(m_matrix[row][col]);
|
|
// qs.append(QLatin1String("; "));
|
|
// }
|
|
// qs.append(QLatin1String("\n"));
|
|
// }
|
|
// QString qs1(QLatin1String("\n"));
|
|
// qs1.append(qs);
|
|
// DEBUG_LOG(m_logger) << qs1;
|
|
|
|
return;
|
|
} // end of initMatrix()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void NumeralCodeModel::onCodificationDataChanged(const CodificationManagement::CodificationData &data,
|
|
DataModification modType)
|
|
{
|
|
const int id = data.id();
|
|
|
|
DEBUG_LOG(m_logger) << "onCodificationDataChanged for CodificationData id: " << id
|
|
<< " - NumCodeColumnIdx data: " << data.numeralCodeTableColumnIndex().c_str()
|
|
<< " - NumCodeRowIdx data: " << data.numeralCodeTableRowIndex().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 !?";
|
|
setColumnSelectionValuesCSV(QString());
|
|
setRowSelectionValuesCSV(QString());
|
|
return;
|
|
}
|
|
|
|
if (modType == DataModification::eModified)
|
|
{
|
|
DEBUG_LOG(m_logger) << "CodificationData was modifed. id: " << id << " - " << data.note();
|
|
|
|
QString qs = QString::fromStdString(data.numeralCodeTableColumnIndex());
|
|
setColumnSelectionValuesCSV(qs);
|
|
|
|
qs = QString::fromStdString(data.numeralCodeTableRowIndex());
|
|
setRowSelectionValuesCSV(qs);
|
|
}
|
|
} // end of onCodificationDataChanged()
|
|
//-----------------------------------------------------------------------------
|