476 lines
16 KiB
C++
Executable File
476 lines
16 KiB
C++
Executable File
/**
|
|
* Model for logistical terms table
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by hjl, SLSE, 2017-01-13
|
|
*/
|
|
|
|
|
|
#include "cod_logistical_terms_model.h"
|
|
#include <LtsWidgets/lts_message_dialog.h>
|
|
#include <QGuiApplication>
|
|
#include <QPalette>
|
|
|
|
|
|
using namespace CodificationManagement;
|
|
|
|
static const int s_matrixCount = 5;
|
|
|
|
LogisticalTermsModel::LogisticalTermsModel(QWidget *parent)
|
|
: QAbstractTableModel(parent)
|
|
, m_dtc(nullptr)
|
|
, m_codificationDataId(0)
|
|
, m_isEditMode(false)
|
|
, m_logger(getLogger("LogisticalTermsModel"))
|
|
{
|
|
DEBUG_LOG(m_logger) << "ctor LogisticalTermsModel";
|
|
m_matrix.resize(s_matrixCount);
|
|
for (int i = 0; i < s_matrixCount; ++i)
|
|
m_matrix[i].resize(s_matrixCount);
|
|
|
|
} // end of ctor()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool LogisticalTermsModel::init(const std::shared_ptr<CodificationManagement::Interface::Dtc> &dtc,
|
|
int codificationDataId,
|
|
bool isEditMode)
|
|
{
|
|
DEBUG_LOG(m_logger) << "LogisticalTerms::init()";
|
|
if (!dtc)
|
|
return false;
|
|
|
|
DEBUG_LOG(m_logger) << "LogisticalTermsModel::init()...";
|
|
|
|
m_dtc = dtc;
|
|
m_codificationDataId = codificationDataId;
|
|
m_isEditMode = isEditMode;
|
|
|
|
resetHeader();
|
|
resetMatrix();
|
|
|
|
if (!isEditMode)
|
|
{
|
|
connect(m_dtc.get(), &CodificationManagement::Interface::Dtc::codificationDataChanged,
|
|
this, &LogisticalTermsModel::onCodificationDataChanged);
|
|
}
|
|
return true;
|
|
} // end of init()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void LogisticalTermsModel::resetHeader()
|
|
{
|
|
// non constant Header values available... werden spaeter mit den Werten aus csv-Datei oder DB gesetzt.
|
|
m_ColumnSelectionValues = QVector<QChar>(s_matrixCount, QLatin1Char('?'));
|
|
m_RowSelectionValues = QVector<QChar>(s_matrixCount, QLatin1Char('?'));
|
|
} // end of resetHeader()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void LogisticalTermsModel::resetMatrix()
|
|
{
|
|
beginResetModel();
|
|
for (auto &colData : m_matrix)
|
|
{
|
|
for (auto &cell : colData)
|
|
cell = QLatin1String("---");
|
|
}
|
|
endResetModel();
|
|
}
|
|
// end of resetMatrix()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool LogisticalTermsModel::setColumnSelectionValuesCSV(const QString &csvData)
|
|
{
|
|
m_ColumnSelectionValues.clear();
|
|
m_ColumnSelectionValues.resize(s_matrixCount);
|
|
|
|
const QVector<QStringRef> slColumns = csvData.splitRef(QLatin1Char(';'));
|
|
|
|
if (slColumns.size() != m_ColumnSelectionValues.size())
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid column data count." << " codificationDataId: " << m_codificationDataId
|
|
<< " Soll/Ist: " << m_ColumnSelectionValues.size() << "/" << slColumns.size();
|
|
m_ColumnSelectionValues.fill(QLatin1Char(' '));
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < m_ColumnSelectionValues.size(); ++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(index(0, 0), index(0, m_ColumnSelectionValues.size() - 1));
|
|
return true;
|
|
} // end of setColumnSelectionValuesCSV()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool LogisticalTermsModel::setRowSelectionValuesCSV(const QString &csvData)
|
|
{
|
|
m_RowSelectionValues.clear();
|
|
m_RowSelectionValues.resize(s_matrixCount);
|
|
|
|
const QVector<QStringRef> slRows = csvData.splitRef(QStringLiteral("\n"));
|
|
|
|
if (slRows.size() != m_RowSelectionValues.size())
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid row data count." << " codificationDataId: " << m_codificationDataId
|
|
<< " Soll/Ist: " << m_RowSelectionValues.size() << "/" << slRows.size()
|
|
<< " Default values will be used. ";
|
|
m_RowSelectionValues.fill(QLatin1Char(' '));
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < m_RowSelectionValues.size(); ++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(index(0, 0), index(0, m_RowSelectionValues.size() - 1));
|
|
return true;
|
|
} // end of setRowSelectionValuesCSV()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool LogisticalTermsModel::setMatrixValuesCSV(const QString &csvData)
|
|
{
|
|
DEBUG_LOG(m_logger) << "setMatrixValuesCSV: " << csvData;
|
|
|
|
const QStringList slRows = splitQuotedStringToRows(csvData);
|
|
|
|
if (slRows.isEmpty())
|
|
{
|
|
ERROR_LOG(m_logger) << "split quoted csv data failed! " << " codificationDataId: " << m_codificationDataId
|
|
<< " csvData: " << csvData;
|
|
return false;
|
|
}
|
|
|
|
if (slRows.size() != m_matrix.size())
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid row data count." << " codificationDataId: " << m_codificationDataId
|
|
<< " Soll/Ist: " << m_matrix.size() << "/" << slRows.size();
|
|
return false;
|
|
}
|
|
|
|
for (int row = 0; row < slRows.size(); ++row)
|
|
{
|
|
const QVector<QStringRef> slColumns = slRows[row].splitRef(QLatin1Char(';'));
|
|
if (slColumns.size() != m_matrix.size())
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid column data count." << " codificationDataId: " << m_codificationDataId
|
|
<< " Soll/Ist: " << m_matrix.size() << "/" << slColumns.size();
|
|
return false;
|
|
}
|
|
|
|
for (int column = 0; column < m_matrix.size(); ++column)
|
|
m_matrix[row][column] = slColumns.at(column).toString();
|
|
|
|
}
|
|
return true;
|
|
} // end of setMatrixValuesCSV()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QString LogisticalTermsModel::getHorizontalHeaderCSVString() const
|
|
{
|
|
QStringList slColumns;
|
|
slColumns.reserve(m_ColumnSelectionValues.size());
|
|
for (const QChar &c : m_ColumnSelectionValues)
|
|
slColumns += QString(c);
|
|
|
|
return slColumns.join(QLatin1Char(';'));
|
|
} // end of getHorizontalHeaderCSVString()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QString LogisticalTermsModel::getVerticalHeaderCSVString() const
|
|
{
|
|
QStringList slRows;
|
|
slRows.reserve(m_RowSelectionValues.size());
|
|
for (const QChar &c : m_RowSelectionValues)
|
|
slRows += QString(c);
|
|
|
|
return slRows.join(QLatin1Char('\n'));
|
|
} // end of getVerticalHeaderCSVString()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QString LogisticalTermsModel::getMatrixDataCSVString() const
|
|
{
|
|
QStringList slColumns;
|
|
slColumns.reserve(m_matrix.size());
|
|
QStringList slRows;
|
|
slRows.reserve(m_matrix.size());
|
|
|
|
for (const auto &colData : m_matrix)
|
|
{
|
|
slColumns.clear();
|
|
for (const auto &cell : colData)
|
|
{
|
|
if (cell.contains(QLatin1Char('\n')))
|
|
slColumns += QLatin1Char('\"') + cell + QLatin1Char('\"');
|
|
else
|
|
slColumns += cell;
|
|
}
|
|
slRows += slColumns.join(QLatin1Char(';'));
|
|
}
|
|
|
|
const QString qs(slRows.join(QLatin1Char('\n')));
|
|
DEBUG_LOG(m_logger) << "getMatrixDataCSVString() : " << qs;
|
|
return qs;
|
|
} // end of getMatrixDataCSVString()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void LogisticalTermsModel::setEditMode(bool isEditMode)
|
|
{
|
|
m_isEditMode = isEditMode;
|
|
// DEBUG_LOG(m_logger) << "emit dataChanged() 0/0 up to row: " <<rowCount() - 1 << " col: " << columnCount() - 1;
|
|
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
|
|
} // end of setEditMode()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool LogisticalTermsModel::isEditMode() const
|
|
{
|
|
return m_isEditMode;
|
|
} // end of isEditMode();
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int LogisticalTermsModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return s_matrixCount + (m_isEditMode ? 1 : 0); // Matrixgroesse + 1 Character fuer RowIndex
|
|
} // end of rowCount()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int LogisticalTermsModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return s_matrixCount + (m_isEditMode ? 1 : 0); // Matrixgroesse + 1 Character fuer ColumnIndex
|
|
} // end of columnCount()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QVariant LogisticalTermsModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
{
|
|
if (role != Qt::DisplayRole)
|
|
return QAbstractTableModel::headerData(section, orientation, role);
|
|
|
|
// Im header nichts anzeigen bzw. in der view die Vertical/horizontal header ausblenden
|
|
if (isEditMode())
|
|
return QVariant();
|
|
|
|
if (orientation == Qt::Horizontal)
|
|
{
|
|
if (section < m_ColumnSelectionValues.size())
|
|
return m_ColumnSelectionValues.at(section);
|
|
return QChar::fromLatin1('?');
|
|
}
|
|
|
|
if (orientation == Qt::Vertical)
|
|
{
|
|
if (section < m_RowSelectionValues.size())
|
|
return m_RowSelectionValues.at(section);
|
|
return QChar::fromLatin1('?');
|
|
}
|
|
|
|
return QAbstractTableModel::headerData(section, orientation, role);
|
|
} // end of headerData()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QVariant LogisticalTermsModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole)
|
|
{
|
|
if (!m_isEditMode)
|
|
{
|
|
// DEBUG_LOG(m_logger) << "data() showMode row: " <<index.row() << " col: " << index.column()
|
|
// << " data: " << m_matrix[index.row() - 1][index.column() - 1];
|
|
return m_matrix[index.row()][index.column()];
|
|
}
|
|
|
|
if (index.column() == 0 && index.row() == 0)
|
|
return QVariant();
|
|
|
|
// row index ==> column index values
|
|
if (index.row() == 0 && index.column() <= m_ColumnSelectionValues.size())
|
|
{
|
|
// DEBUG_LOG(m_logger) << "data() editMode row: " <<index.row() << " col: " << index.column()
|
|
// << " ColSelVal: " << m_ColumnSelectionValues[index.column() - 1];
|
|
return m_ColumnSelectionValues.at(index.column() - 1);
|
|
}
|
|
|
|
// column index ==> row index values
|
|
if (index.column() == 0 && index.row() <= m_RowSelectionValues.size())
|
|
{
|
|
// DEBUG_LOG(m_logger) << "data() editMode row: " <<index.row() << " col: " << index.column()
|
|
// << " RowSelVal: " << m_RowSelectionValues[index.row() - 1];
|
|
return m_RowSelectionValues.at(index.row() - 1);
|
|
}
|
|
|
|
// Code Matrix
|
|
if (index.column() > 0 && index.row() > 0)
|
|
{
|
|
// DEBUG_LOG(m_logger) << "data() editMode row: " <<index.row() << " col: " << index.column()
|
|
// << " data: " << m_matrix[index.row() - 1][index.column() - 1];
|
|
return m_matrix[index.row() - 1][index.column() - 1]; // row/column fuer header abziehen
|
|
}
|
|
}
|
|
|
|
if (role == Qt::TextAlignmentRole)
|
|
return Qt::AlignCenter;
|
|
|
|
if (role == Qt::BackgroundRole)
|
|
{
|
|
if (m_isEditMode)
|
|
{
|
|
if ((index.row() == 0) || (index.column() == 0))
|
|
return QGuiApplication::palette().color(QPalette::Active, QPalette::Base); // Qt:: white
|
|
return QGuiApplication::palette().color(QPalette::Active, QPalette::Base); // Qt:: white
|
|
}
|
|
return QGuiApplication::palette().color(QPalette::Disabled, QPalette::Base); // RDE_GRAY
|
|
// return QGuiApplication::palette().color(QPalette::Active, QPalette::Base); // Qt:: white
|
|
}
|
|
return QVariant();
|
|
} // end of data()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool LogisticalTermsModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
DEBUG_LOG(m_logger) << "setData() row/col: " << index.row() << "/" << index.column()
|
|
<< " value:" << value.toString();
|
|
|
|
const QString qs = value.toString();
|
|
if (qs.isEmpty())
|
|
return false;
|
|
|
|
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;
|
|
}
|
|
|
|
if (role == Qt::EditRole)
|
|
{
|
|
// Input for ColumnSelectionValues
|
|
if (index.row() == 0)
|
|
{
|
|
m_ColumnSelectionValues[index.column() - 1] = qs.at(0);
|
|
// DEBUG_LOG(m_logger) << "setData() row: " <<index.row() << " col: " << index.column()
|
|
// << " ColSelVal: " << m_ColumnSelectionValues[index.column() - 1];
|
|
}
|
|
|
|
// Input for RowSelectionValues
|
|
else if (index.column() == 0 && (index.row() > 0))
|
|
m_RowSelectionValues[index.row() - 1] = qs.at(0);
|
|
|
|
// Matrix
|
|
else if ((index.column() > 0) && (index.row() > 0))
|
|
m_matrix[index.row() - 1][index.column() - 1] = qs;
|
|
emit dataModified();
|
|
}
|
|
return true;
|
|
} // end of setData()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
Qt::ItemFlags LogisticalTermsModel::flags(const QModelIndex &index) const
|
|
{
|
|
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
|
|
|
|
if (((index.row() <= rowCount() - 1) && (index.column() <= columnCount() - 1)) &&
|
|
!((index.row() == 0) && (index.column() == 0)))
|
|
{
|
|
if (isEditMode())
|
|
flags |= Qt::ItemIsEditable;
|
|
else
|
|
flags &= ~Qt::ItemIsEditable;
|
|
}
|
|
return flags;
|
|
} // end of flags()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void LogisticalTermsModel::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: " << 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();
|
|
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.logisticalTermTableColumnIndex());
|
|
setColumnSelectionValuesCSV(qs);
|
|
|
|
qs = QString::fromStdString(data.logisticalTermTableRowIndex());
|
|
setRowSelectionValuesCSV(qs);
|
|
|
|
qs = QString::fromStdString(data.logisticalTermTableMatrix());
|
|
setMatrixValuesCSV(qs);
|
|
}
|
|
} // end of onCodificationDataChanged()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QStringList LogisticalTermsModel::splitQuotedStringToRows(const QString &str)
|
|
{
|
|
QStringList slRows;
|
|
QString row;
|
|
row.reserve(str.size());
|
|
|
|
for (int i = 0; i < str.size(); ++i)
|
|
{
|
|
if (str.at(i) != QLatin1Char('\"') && str.at(i) != QLatin1Char('\n'))
|
|
{
|
|
row.append(str.at(i));
|
|
continue;
|
|
}
|
|
|
|
if (str.at(i) == QLatin1Char('\"'))
|
|
{
|
|
++i; // oeffnedes " nicht ausgeben
|
|
while (str.at(i) != QLatin1Char('\"'))
|
|
{
|
|
row.append(str.at(i));
|
|
++i;
|
|
if (i > str.size())
|
|
return QStringList();
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (str.at(i) == QLatin1Char('\n'))
|
|
{
|
|
slRows += row;
|
|
row.clear();
|
|
}
|
|
}
|
|
// wenn die letzte row in String nicht mit \n endet muss sie hier noch angefuegt werden
|
|
if (!row.isEmpty())
|
|
slRows += row;
|
|
return slRows;
|
|
|
|
} // end of splitQuotedStringToRows()
|
|
//-----------------------------------------------------------------------------
|