/** * Model for numeral code table * $Id$ * $HeadURL$ * $Author$ * Created by hjl, SLSE, 2016-11-02 */ #include "cod_numeral_code_model.h" #include #include #include #include NumeralCodeModel::NumeralCodeModel(QWidget *parent) : QAbstractTableModel(parent) , m_isEditMode(false) , m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("COD_Test_NumeralCodeTableModel")) { init(); } // end of ctor() //----------------------------------------------------------------------------- NumeralCodeModel::~NumeralCodeModel() { } // end of dtor() //----------------------------------------------------------------------------- bool NumeralCodeModel::init() { m_MatrixNumeralSet << '0' << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9'; m_MatrixCharacterSet1 << 'A' << 'B' << 'C' << 'D' << 'E' << 'F' << 'G' << 'H' << 'I' << 'J' << 'K' << 'L' << 'M' << 'N' << 'O' << 'P' << 'Q' << 'R' << 'S' << 'T' << 'U' << 'V' << 'W' << 'X' << 'Y' << 'Z'; m_MatrixCharacterSet2 << 'D' << 'E' << 'G' << 'H' << 'I' << 'L' << 'N' << 'O' << 'R' << 'S' << 'T' << 'U'; // DefaultValues... werden spaeter mit den Werten aus csv-Datei oder DB gesetzt. m_ColumnSelectionValues << 'A' << 'B' << 'C' << 'D' << 'E' << 'F' << 'G' << 'H' << 'I' << 'J' << 'K' << 'L' << 'M' ; // DefaultValues... werden spaeter mit den Werten aus csv-Datei oder DB gesetzt. m_RowSelectionValues << 'a' << 'b' << 'c' << 'd' << 'e' << 'f' << 'g' << 'h' << 'i' << 'j' << 'k' << 'l' << 'm' ; return initMatrix(); } // end of init() //----------------------------------------------------------------------------- bool NumeralCodeModel::setColumnSelectionValues(const QVector &data) { m_ColumnSelectionValues = data; emit dataChanged(createIndex(0, 0), createIndex(0, columnCount() - 1)); return true; } // end of setColumnSelectionValues() //----------------------------------------------------------------------------- bool NumeralCodeModel::setColumnSelectionValuesCSV(const QVector &csvData) { QStringList qsList; for (int i = 0; i < csvData.size(); ++i) { if (QLatin1Char(csvData.at(i)) != QLatin1Char(';')) qsList.append(QString(QLatin1Char(csvData.at(i)))); } // DEBUG_LOG(m_logger) << "setColumnSelectionValuesCSV() " // << " csvData.size(): " << csvData.size() // << " qsList.size: " << qsList.size() // ; QVector data; for (int i = 0; i < qsList.size(); ++i) { const QString &qs = qsList.at(i); if (qs.isEmpty()) continue; char c = qs.at(0).toLatin1(); data.append(c); } return setColumnSelectionValues(data);; } // end of setColumnSelectionValuesCSV() //----------------------------------------------------------------------------- bool NumeralCodeModel::setRowSelectionValues(const QVector &data) { m_RowSelectionValues = data; emit dataChanged(createIndex(0, 0), createIndex(0, columnCount() - 1)); return true; } // end of setRowSelectionValues() //----------------------------------------------------------------------------- bool NumeralCodeModel::setRowSelectionValuesCSV(const QVector &csvData) { QStringList qsList; for (int i = 0; i < csvData.size(); ++i) { if (QLatin1Char(csvData.at(i)) != QLatin1Char(';')) qsList.append(QString(QLatin1Char(csvData.at(i)))); } QVector data; for (int i = 0; i < qsList.size(); ++i) { const QString &qs = qsList.at(i); if (qs.isEmpty()) continue; char c = qs.at(0).toLatin1(); data.append(c); } return setRowSelectionValues(data); } // end of setRowSelectionValuesCSV() //----------------------------------------------------------------------------- QVector NumeralCodeModel::getHorizontalHeaderCSV() const { QVector qVec; qVec.reserve(m_ColumnSelectionValues.size() * 2); for (int i = 0; i < m_ColumnSelectionValues.size(); ++i) { qVec.append(m_ColumnSelectionValues.at(i)); qVec.append(';'); } // remove last separator qVec.remove(m_ColumnSelectionValues.size() * 2 - 1); return qVec; } // end of getHorizontalHeaderCSV() //----------------------------------------------------------------------------- QVector NumeralCodeModel::getVerticalHeaderCSV() const { QVector qVec; qVec.reserve(m_RowSelectionValues.size() * 2); for (int i = 0; i < m_RowSelectionValues.size(); ++i) { qVec.append(m_RowSelectionValues.at(i)); qVec.append(';'); } // remove last separator qVec.remove(m_RowSelectionValues.size() * 2 - 1); return qVec; } // end of getVerticalHeaderCSV() //----------------------------------------------------------------------------- void NumeralCodeModel::setEditMode(bool isEditMode) { m_isEditMode = isEditMode; // DEBUG_LOG(m_logger) << "emit dataChanged() 0/0 up to row: " < column index values if (index.row() == 0) return QChar::fromLatin1(m_ColumnSelectionValues[index.column() - 1]); // column index ==> row index values if ((index.column() == 0)) return QChar::fromLatin1(m_RowSelectionValues[index.row() - 1]); // Code Matrix if ((index.column() > 0) && (index.row() > 0)) { return QChar::fromLatin1(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)) { if (m_isEditMode) return QColor(Qt::darkRed); else return QColor(Qt::darkGray); } else return QColor(Qt::darkYellow); } 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(); QString tmp = value.toString(); if (tmp.isEmpty()) return false; const char c = tmp.at(0).toLatin1(); 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; } 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))) flags |= Qt::ItemIsEditable; return flags; } // end of flags() //----------------------------------------------------------------------------- bool 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(QLatin1Char(m_matrix[row][col])); qs.append(QLatin1String("; ")); } qs.append(QLatin1String("\n")); } QString qs1(QLatin1String("\n")); qs1.append(qs); // DEBUG_LOG(m_logger) << qs1; return true; } // end of initMatrix() //-----------------------------------------------------------------------------