//////////////////////////////////////////////////////////////////////////////// /** * \file tebleformat.cpp * \version v1.0 * \author Ing. Dominik Malcik */ //////////////////////////////////////////////////////////////////////////////// #include "tableformat.h" TableFormat::TableFormat(QObject *parent): QAbstractTableModel(parent) { } TableFormat::TableFormat(QList pairs, QObject *parent) : QAbstractTableModel(parent) { listOfPairs = pairs; } Qt::ItemFlags TableFormat::flags(const QModelIndex &index) const { Qt::ItemFlags result = QAbstractTableModel::flags(index); if (index.column() == 0) { result = Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled; return result; } if (!index.isValid()) { return Qt::ItemIsEnabled; } return QAbstractTableModel::flags(index); } int TableFormat::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return listOfPairs.size(); } int TableFormat::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent); return 3; } QVariant TableFormat::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } if (index.row() >= listOfPairs.size() || index.row() < 0) { return QVariant(); } LayerRecord * currentRow = listOfPairs.at(index.row()); if (role == Qt::CheckStateRole && index.column() == 0) { return currentRow->getCheckBox(); } if (role == Qt::DisplayRole) { if (index.column() == 0) { // checkBox } else if (index.column() == 1) { return currentRow->getName(); } else if (index.column() == 2) { return currentRow->getOpacity(); } } return QVariant(); } QVariant TableFormat::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) return QVariant(); if (orientation == Qt::Horizontal) { switch (section) { case 0: return tr(""); case 1: return tr("Layer"); case 2: return tr("%"); default: return QVariant(); } } return QVariant(); } bool TableFormat::insertRows(int position, int rows, const QModelIndex &index) { Q_UNUSED(index); beginInsertRows(QModelIndex(), position, position+rows-1); for (int row = 0; row < rows; row++) { QString s1 = " "; listOfPairs.insert(0, new LayerRecord(true, s1, s1)); } endInsertRows(); return true; } bool TableFormat::removeRows(int position, int rows, const QModelIndex &index) { Q_UNUSED(index); beginRemoveRows(QModelIndex(), position, position+rows-1); for (int row=0; row < rows; ++row) { listOfPairs.removeAt(position); } endRemoveRows(); return true; } bool TableFormat::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole) { int row = index.row(); LayerRecord * p = listOfPairs.value(row); if (index.column() == 0) { } else if (index.column() == 1) { p->setName(value.toString()); } else if (index.column() == 2) { p->setOpacity(value.toString()); } else { return false; } listOfPairs.replace(row, p); emit(dataChanged(index, index)); return true; } return false; } QList TableFormat::getList() { return listOfPairs; }