int HistoryTreeModel::rowCount(const QModelIndex &parent) const { if ( parent.internalId() != 0 || parent.column() > 0 || !sourceModel()) return 0; // row count OF dates if (!parent.isValid()) { if (!m_sourceRowCache.isEmpty()) return m_sourceRowCache.count(); QDate currentDate; int rows = 0; int totalRows = sourceModel()->rowCount(); for (int i = 0; i < totalRows; ++i) { QDate rowDate = sourceModel()->index(i, 0).data(HistoryModel::DateRole).toDate(); if (rowDate != currentDate) { m_sourceRowCache.append(i); currentDate = rowDate; ++rows; } } Q_ASSERT(m_sourceRowCache.count() == rows); return rows; } // row count FOR a date int start = sourceDateRow(parent.row()); int end = sourceDateRow(parent.row() + 1); return (end - start); }
QVariant HistoryTreeModel::data(const QModelIndex &index, int role) const { if ((role == Qt::EditRole || role == Qt::DisplayRole)) { int start = index.internalId(); if (start == 0) { int offset = sourceDateRow(index.row()); if (index.column() == 0) { QModelIndex idx = sourceModel()->index(offset, 0); QDate date = idx.data(HistoryModel::DateRole).toDate(); if (date == QDate::currentDate()) return tr("Earlier Today"); return date.toString(QLatin1String("dddd, MMMM d, yyyy")); } if (index.column() == 1) { return tr("%1 items").arg(rowCount(index.sibling(index.row(), 0))); } } } if (role == Qt::DecorationRole && index.column() == 0 && !index.parent().isValid()) return QIcon(QLatin1String(":history.png")); if (role == HistoryModel::DateRole && index.column() == 0 && index.internalId() == 0) { int offset = sourceDateRow(index.row()); QModelIndex idx = sourceModel()->index(offset, 0); return idx.data(HistoryModel::DateRole); } return QAbstractProxyModel::data(index, role); }
bool HistoryTreeModel::removeRows(int row, int count, const QModelIndex &parent) { if (row < 0 || count <= 0 || row + count > rowCount(parent)) return false; if (parent.isValid() && rowCount(parent) == count - row) beginRemoveRows(QModelIndex(), parent.row(), parent.row()); else beginRemoveRows(parent, row, row + count - 1); if (parent.isValid()) { // removing pages int offset = sourceDateRow(parent.row()); return sourceModel()->removeRows(offset + row, count); } else { // removing whole dates for (int i = row + count - 1; i >= row; --i) { QModelIndex dateParent = index(i, 0); int offset = sourceDateRow(dateParent.row()); if (!sourceModel()->removeRows(offset, rowCount(dateParent))) return false; } } removingDown = true; return true; }
int TreeProxyModel::rowCount(const QModelIndex &parent) const { if( parent.internalId() != 0 || parent.column() > 0 || !sourceModel()) return 0; // Liefert die Anzahl der rows für die Tage if (!parent.isValid()) { if (!sourceRowCache.isEmpty()) return sourceRowCache.count(); QDate currentDate; int rows = 0; int sourceRows = sourceModel()->rowCount(); for (int i = 0; i < sourceRows; ++i) { /* The new model change the type automatic. QString rowDateString = sourceModel()->index(i, 1).data().toString(); QDate rowDate = QDate::fromString(rowDateString); */ QDate rowDate = sourceModel()->index(i, 1).data().toDate(); if (rowDate != currentDate) { sourceRowCache.append(i); currentDate = rowDate; ++rows; } } return rows; } // Liefert die Anzahl der rows für einen Tag int start = sourceDateRow(parent.row()); int end = sourceDateRow(parent.row()+1); return (end-start); }
QVariant TreeProxyModel::data(const QModelIndex &proxyIndex, int role) const { if ((role == Qt::EditRole || role == Qt::DisplayRole)) { int start = proxyIndex.internalId(); if (start == 0) { int offset = sourceDateRow(proxyIndex.row()); if (proxyIndex.column() == 1) { QModelIndex idx = sourceModel()->index(offset, 1); QDate date = idx.data().toDate(); return date.toString(); } if (proxyIndex.column() == 2) { double value = 0.0; if (proxyIndex.isValid()) { int startIdx = sourceDateRow(proxyIndex.row()); int endIdx = sourceDateRow(proxyIndex.row()+1); for (int i=startIdx; i<endIdx; ++i) { value += sourceModel()->index(i, 2).data().toDouble(); } } return " Summe: " + QString::number(value); } } } return QAbstractProxyModel::data(proxyIndex, role); }
QModelIndex HistoryTreeModel::mapToSource(const QModelIndex &proxyIndex) const { int offset = proxyIndex.internalId(); if (offset == 0) return QModelIndex(); int startDateRow = sourceDateRow(offset - 1); return sourceModel()->index(startDateRow + proxyIndex.row(), proxyIndex.column()); }