QModelIndex DeckListModel::addCard(const QString &cardName, const QString &zoneName) { InnerDecklistNode *zoneNode = createNodeIfNeeded(zoneName, root); CardInfo *info = db->getCard(cardName); QString cardType = info->getMainCardType(); InnerDecklistNode *cardTypeNode = createNodeIfNeeded(cardType, zoneNode); DecklistModelCardNode *cardNode = dynamic_cast<DecklistModelCardNode *>(cardTypeNode->findChild(cardName)); if (!cardNode) { DecklistCardNode *decklistCard = deckList->addCard(cardName, zoneName); QModelIndex parentIndex = nodeToIndex(cardTypeNode); beginInsertRows(parentIndex, cardTypeNode->size(), cardTypeNode->size()); cardNode = new DecklistModelCardNode(decklistCard, cardTypeNode); endInsertRows(); sort(1); emitRecursiveUpdates(parentIndex); return nodeToIndex(cardNode); } else { cardNode->setNumber(cardNode->getNumber() + 1); QModelIndex ind = nodeToIndex(cardNode); emitRecursiveUpdates(ind); return ind; } }
QVariant DeckListModel::data(const QModelIndex &index, int role) const { // debugIndexInfo("data", index); if (!index.isValid()) return QVariant(); if (index.column() >= columnCount()) return QVariant(); AbstractDecklistNode *temp = static_cast<AbstractDecklistNode *>(index.internalPointer()); DecklistModelCardNode *card = dynamic_cast<DecklistModelCardNode *>(temp); if (!card) { InnerDecklistNode *node = dynamic_cast<InnerDecklistNode *>(temp); switch (role) { case Qt::FontRole: { QFont f; f.setBold(true); return f; } case Qt::DisplayRole: case Qt::EditRole: switch (index.column()) { case 0: return node->recursiveCount(true); case 1: return node->getVisibleName(); case 2: return QString().sprintf("$%.2f", node->recursivePrice(true)); default: return QVariant(); } case Qt::BackgroundRole: { int color = 90 + 60 * node->depth(); return QBrush(QColor(color, 255, color)); } default: return QVariant(); } } else { switch (role) { case Qt::DisplayRole: case Qt::EditRole: { switch (index.column()) { case 0: return card->getNumber(); case 1: return card->getName(); case 2: return QString().sprintf("$%.2f", card->getTotalPrice()); default: return QVariant(); } } case Qt::BackgroundRole: { int color = 255 - (index.row() % 2) * 30; return QBrush(QColor(color, color, color)); } default: return QVariant(); } } }
bool DeckListModel::setData(const QModelIndex &index, const QVariant &value, int role) { DecklistModelCardNode *node = getNode<DecklistModelCardNode *>(index); if (!node || (role != Qt::EditRole)) return false; switch (index.column()) { case 0: node->setNumber(value.toInt()); break; case 1: node->setName(value.toString()); break; default: return false; } emitRecursiveUpdates(index); return true; }