#include "tablemodel.h" #include "../formats/jsonparser.h" #include "commands/insertrowscommand.h" #include "commands/removerowscommand.h" #include "modelitem.h" QHash TableModel::ROLE_NAMES = {{NameRole, "Name"}, {DescriptionRole, "Description"}, {InfoRole, "Info"}, {AmountRole, "Amount"}, {FactorRole, "Factor"}}; TableModel::TableModel(QUndoStack* undoStack, QObject* parent) : QAbstractTableModel{parent} , m_undoStack(undoStack) { for (int row = 0; row < 5; ++row) { QHash values; values[NameRole] = QString("Item %1").arg(row); values[DescriptionRole] = QString("This is item %1").arg(row); values[InfoRole] = QString("Info of item %1").arg(row); values[AmountRole] = row; values[FactorRole] = row * 1.1; shared_ptr item = make_unique(values); m_items.append(std::move(item)); } } Qt::ItemFlags TableModel::flags(const QModelIndex& index) const { return Qt::ItemIsEditable | QAbstractTableModel::flags(index); } QHash TableModel::roleNames() const { return ROLE_NAMES; } int TableModel::rowCount(const QModelIndex& parent) const { Q_UNUSED(parent); return m_items.size(); } int TableModel::columnCount(const QModelIndex& parent) const { Q_UNUSED(parent); return ROLE_NAMES.size(); } QVariant TableModel::data(const QModelIndex& index, int role) const { const int row = index.row(); const int column = index.column(); if (!index.isValid()) { return QVariant(); } if (row >= rowCount(QModelIndex()) || column >= columnCount(QModelIndex())) { return QVariant(); } int roleForColumn = getRoleForColumn(column); switch (role) { case Qt::DisplayRole: case Qt::EditRole: return m_items.at(row)->data(roleForColumn); case NameRole: case DescriptionRole: case InfoRole: case AmountRole: case FactorRole: return m_items.at(row)->data(role); } return QVariant(); } QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role == Qt::DisplayRole) { if (orientation == Qt::Horizontal) { const int columnRole = getRoleForColumn(section); const QString headerName = ROLE_NAMES.value(columnRole); return QString("%1").arg(headerName); } else { return QString("%1").arg(section); } } return QVariant(); } bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) { if (role == Qt::EditRole) { if (!checkIndex(index)) { return false; } int columnRole = getRoleForColumn(index.column()); shared_ptr item = m_items.at(index.row()); return item->setData(value, columnRole); } return false; } // bool TableModel::setItemData(const QModelIndex& index, const QMap& roles) {} bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) { if (parentIndex != QModelIndex()) { qWarning() << "Removing of child rows is not supported yet!"; return false; } const int lastRow = firstRow + nRows - 1; if (firstRow < 0 || lastRow >= m_items.size()) { qWarning() << "Trying to remove rows is out of bounds!"; return false; } RemoveRowsCommand* removeCommand = new RemoveRowsCommand(this, firstRow, nRows); m_undoStack->push(removeCommand); return true; } void TableModel::appendItems(const QByteArray& jsonDoc) { insertItems(-1, jsonDoc, QModelIndex()); } void TableModel::insertItems(int startPosition, const QByteArray& jsonDoc, const QModelIndex& parentIndex) { qInfo() << "Inserting item(s) into model..."; if (parentIndex != QModelIndex()) { qWarning() << "Using invalid parent index (no child support for now)"; } if (startPosition == -1 || startPosition > m_items.size()) { /// Appending item(s) startPosition = m_items.size(); } QList> valueList = JsonParser::toItemValuesList(jsonDoc); InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, valueList); m_undoStack->push(insertCommand); } int TableModel::getRoleForColumn(const int column) const { switch (column) { case 0: return NameRole; break; case 1: return DescriptionRole; break; case 2: return InfoRole; break; case 3: return AmountRole; break; case 4: return FactorRole; break; default: return NameRole; break; } } void TableModel::execInsertItems(const int firstRow, const QList> valueList) { const int nRows = valueList.size(); qDebug() << "Inserting" << nRows << "items..."; const int lastRow = firstRow + nRows - 1; beginInsertRows(QModelIndex(), firstRow, lastRow); for (int row = 0; row < nRows; ++row) { const int rowPosition = firstRow + row; shared_ptr item = make_unique(valueList.at(row)); m_items.insert(rowPosition, std::move(item)); } endInsertRows(); } void TableModel::execRemoveItems(const int firstRow, const int nRows) { const int lastRow = firstRow + nRows - 1; beginRemoveRows(QModelIndex(), firstRow, lastRow); m_items.remove(firstRow, nRows); endRemoveRows(); }