308 lines
9.7 KiB
C++
Executable File
308 lines
9.7 KiB
C++
Executable File
/**
|
|
* Model for phrase table
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by hjl, SLSE, 2017-01-19
|
|
*/
|
|
|
|
|
|
#include "cod_phrase_table_model.h"
|
|
#include <LtsWidgets/lts_message_dialog.h>
|
|
#include <QGuiApplication>
|
|
#include <QPalette>
|
|
|
|
using namespace CodificationManagement;
|
|
|
|
PhraseTableModel::PhraseTableModel(QWidget *parent)
|
|
: QAbstractTableModel(parent)
|
|
, m_dtc(nullptr)
|
|
, m_codificationDataId(0)
|
|
, m_isEditMode(false)
|
|
, m_logger(getLogger("PhraseTableModel"))
|
|
{
|
|
DEBUG_LOG(m_logger) << "ctor PhraseTableModel";
|
|
|
|
} // end of ctor()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool PhraseTableModel::init(const std::shared_ptr<CodificationManagement::Interface::Dtc> &dtc,
|
|
int codificationDataId,
|
|
bool isEditMode)
|
|
{
|
|
DEBUG_LOG(m_logger) << "PhraseTableModel::init()";
|
|
if (!dtc)
|
|
return false;
|
|
|
|
m_dtc = dtc;
|
|
m_codificationDataId = codificationDataId;
|
|
m_isEditMode = isEditMode;
|
|
|
|
// non header values used, header not visible
|
|
resetMatrix();
|
|
|
|
if (!isEditMode)
|
|
{
|
|
connect(m_dtc.get(), &CodificationManagement::Interface::Dtc::codificationDataChanged,
|
|
this, &PhraseTableModel::onCodificationDataChanged);
|
|
}
|
|
return true;
|
|
} // end of init()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void PhraseTableModel::resetMatrix()
|
|
{
|
|
for (int row = 0; row < rowCount(); ++row)
|
|
{
|
|
for (int column = 0; column < columnCount(); ++column)
|
|
{
|
|
if ((row == 0) && (column == 0))
|
|
{
|
|
m_matrix[row][column] = QLatin1String("A-Warnung ");
|
|
}
|
|
if ((row == 1) && (column == 0))
|
|
{
|
|
m_matrix[row][column] = QLatin1String("B-Warnung ");
|
|
}
|
|
if ((row == 2) && (column == 0))
|
|
{
|
|
m_matrix[row][column] = QLatin1String("C-Warnung ");
|
|
}
|
|
|
|
if ((row <= 2 && column > 0) || (row > 2))
|
|
m_matrix[row][column] = QLatin1String("--- ");
|
|
}
|
|
}
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, columnCount() - 1));
|
|
return ;
|
|
} // end of resetMatrix()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool PhraseTableModel::setMatrixValuesCSV(const QString &csvData)
|
|
{
|
|
DEBUG_LOG (m_logger) << "setMatrixValuesCSV() data: " << csvData;
|
|
|
|
const QVector<QStringRef> slRows = csvData.splitRef(QLatin1Char('\n'));
|
|
if (slRows.count() != rowCount())
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid row data count." << " codificationDataId: " << m_codificationDataId
|
|
<< " Soll/Ist: " << rowCount() << "/" << slRows.size();
|
|
return false;
|
|
}
|
|
|
|
for (int row = 0; row < rowCount(); ++row)
|
|
{
|
|
const QStringList slColumns = splitQuotedString(QLatin1Char(';'), slRows[row].toString());
|
|
if (slColumns.count() != columnCount())
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid column data count." << " codificationDataId: " << m_codificationDataId
|
|
<< " Soll/Ist: " << columnCount() << "/" << slColumns.size();
|
|
return false;
|
|
}
|
|
|
|
for (int column = 0; column < columnCount(); ++column)
|
|
{
|
|
m_matrix[row][column] = slColumns[column];
|
|
}
|
|
}
|
|
return true;
|
|
} // end of setMatrixValuesCSV()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QString PhraseTableModel::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)
|
|
{
|
|
const QString &cell = m_matrix[row][column];
|
|
if (cell.contains(QLatin1Char(';')))
|
|
slColumns += QLatin1Char('\"') + cell + QLatin1Char('\"');
|
|
else
|
|
slColumns += cell;
|
|
}
|
|
slRows << slColumns.join(QLatin1Char(';'));
|
|
}
|
|
return slRows.join(QLatin1Char('\n'));
|
|
} // end of getMatrixDataCSVString()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void PhraseTableModel::setEditMode(bool isEditMode)
|
|
{
|
|
m_isEditMode = isEditMode;
|
|
// DEBUG_LOG(m_logger) << "emit dataChanged() 0/0 up to row: " <<rowCount() - 1 << " col: " << columnCount() - 1;
|
|
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, columnCount() - 1));
|
|
} // end of setEditMode()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool PhraseTableModel::isEditMode() const
|
|
{
|
|
return m_isEditMode;
|
|
} // end of isEditMode();
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int PhraseTableModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return 13;
|
|
} // end of rowCount()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int PhraseTableModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
Q_UNUSED(parent)
|
|
return 2; // Matrixgroesse
|
|
} // end of columnCount()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QVariant PhraseTableModel::headerData(int column, Qt::Orientation orientation, int role) const
|
|
{
|
|
if (role != Qt::DisplayRole)
|
|
return QAbstractTableModel::headerData(column, orientation, role);
|
|
|
|
// Im header nichts anzeigen bzw. in der view die Vertical/horizontal header ausblenden
|
|
return QVariant();
|
|
} // end of headerData()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QVariant PhraseTableModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if ((role == Qt::DisplayRole || role == Qt::EditRole))
|
|
{
|
|
// DEBUG_LOG(m_logger) << "data() showMode row: " <<index.row() << " col: " << index.column()
|
|
// << " data: " << m_matrix[index.row()][index.column()];
|
|
return m_matrix[index.row()][index.column()];
|
|
}
|
|
|
|
if (role == Qt::TextAlignmentRole)
|
|
return Qt::AlignCenter;
|
|
|
|
if (role == Qt::BackgroundRole)
|
|
{
|
|
if ((index.row() <= rowCount()-1) && (index.column() <= columnCount() - 1))
|
|
{
|
|
if (m_isEditMode)
|
|
{
|
|
// return QColor(Qt::darkYellow);
|
|
return QGuiApplication::palette().color(QPalette::Disabled, QPalette::AlternateBase); // RDE_LIGHTER_GRAY
|
|
}
|
|
else
|
|
// return QColor(Qt::darkGray);
|
|
return QGuiApplication::palette().color(QPalette::Disabled, QPalette::Base); // RDE_GRAY
|
|
}
|
|
}
|
|
return QVariant();
|
|
} // end of data()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool PhraseTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
DEBUG_LOG(m_logger) << "setData() row/col: " << index.row() << "/" << index.column()
|
|
<< " value:" << value.toString();
|
|
|
|
// Input of ';' character works, but is also prevented because of the equal treatment to other tables
|
|
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 qs = value.toString();
|
|
if (qs.isEmpty())
|
|
return false;
|
|
|
|
if (role == Qt::EditRole)
|
|
{
|
|
if ((index.row() <= rowCount()-1) && (index.column() <= columnCount() - 1))
|
|
{
|
|
m_matrix[index.row()][index.column()] = qs;
|
|
emit dataModified();
|
|
}
|
|
}
|
|
return true;
|
|
} // end of setData()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
Qt::ItemFlags PhraseTableModel::flags(const QModelIndex &index) const
|
|
{
|
|
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
|
|
|
|
if (isEditMode())
|
|
{
|
|
if ((index.row() <= rowCount()-1) && (index.column() <= columnCount() - 1))
|
|
flags |= Qt::ItemIsEditable;
|
|
|
|
// Die obersten 3 Felder der ersten Spalte sind nicht editierbar!
|
|
if ((index.column() == 0) && (index.row() <= 2))
|
|
flags &= ~Qt::ItemIsEditable;
|
|
}
|
|
else
|
|
{
|
|
if ((index.row() <= rowCount()-1) && (index.column() <= columnCount() - 1))
|
|
flags &= ~Qt::ItemIsEditable;
|
|
}
|
|
return flags;
|
|
} // end of flags()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void PhraseTableModel::onCodificationDataChanged(const CodificationManagement::CodificationData &data,
|
|
DataModification modType)
|
|
{
|
|
const int id = data.id();
|
|
|
|
DEBUG_LOG(m_logger) << "onCodificationDataChanged for CodificationData id: " << id
|
|
<< " - PhraseTableModel data: " << data.phraseTable().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.phraseTable());
|
|
setMatrixValuesCSV(qs);
|
|
}
|
|
} // end of onCodificationDataChanged()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
QStringList PhraseTableModel::splitQuotedString(const QChar &sep, const QString &inString)
|
|
{
|
|
QString str = inString;
|
|
QStringList result;
|
|
|
|
while (str.contains(QLatin1Char('\"')))
|
|
{
|
|
int i = str.indexOf(QLatin1Char('\"'));
|
|
QString part = str.left(i);
|
|
|
|
result += part.split(sep, QString::SkipEmptyParts);
|
|
|
|
str.remove(0, i + 1);
|
|
|
|
i = str.indexOf(QLatin1Char('\"'));
|
|
|
|
result += str.left(i);
|
|
str.remove(0,i + 1);
|
|
}
|
|
result += str.split(sep, QString::SkipEmptyParts);
|
|
|
|
return result;
|
|
} // end of splitQuotedString()
|
|
//-----------------------------------------------------------------------------
|
|
|