#include "AddModel.h" #include void AddModel::setHeaderList(const HeaderList& headerList) { m_headerList = headerList; beginResetModel(); endResetModel(); } const HeaderList& AddModel::getHeaderList() const { return m_headerList; } bool AddModel::removeRow(int row, const QModelIndex &parent) { //qWarning() << "removeRow"; (void)parent; if (row >= m_dataList.count()) { //qWarning() << "removeRows false"; return false; } beginRemoveRows(parent, row, row); m_dataList.removeAt(row); endRemoveRows(); return true; } void AddModel::clear() { beginResetModel(); m_dataList.clear(); endResetModel(); } QVariant AddModel::data(const QModelIndex &index, int role) const { switch (role) { case Qt::EditRole: case Qt::DisplayRole: { if (index.row() >= m_dataList.count()) { return QVariant(); // probably the new line was not added yet (use setData the first time to create it) } return m_dataList.at(index.row()).at(index.column()); } } return QVariant(); } bool AddModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (role != Qt::EditRole) { return false; } if (index.row() >= m_dataList.count()) // no row in dataList? { Data newData; // create new row int headerListCount = m_headerList.count(); newData.reserve(headerListCount); for (int i = 0; i < headerListCount; i++) // preadd empty columns { newData.append(QString()); } m_dataList.append(newData); // finally add new line to dataList } Data data = m_dataList.at(index.row()); // get old row data.replace(index.column(), value.toString()); // replace column in row m_dataList.replace(index.row(), data); // finally replace row in dataList beginResetModel(); endResetModel(); // tell the model to show the new empty line return true; } QVariant::Type AddModel::getColumnType(const int column) const { if (column < 0 || column >= m_headerList.size()) return QVariant::Invalid; return m_headerList.at(column).second; } QVariant AddModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) return QVariant(); switch (role) { case Qt::DisplayRole: { return m_headerList.at(section).first; } } return QVariant(); } int AddModel::rowCount(const QModelIndex &) const { return m_dataList.count() + 1 /* empty row for new data */; // yes, if the user wants to add more than 1 line! } int AddModel::columnCount(const QModelIndex &) const { return m_headerList.count(); } Qt::ItemFlags AddModel::flags(const QModelIndex &) const { return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable; } void AddModel::setTablename(const QString& tableName) { m_tableName = tableName; } const QString& AddModel::getTableName() const { return m_tableName; }