deck editor
This commit is contained in:
parent
9a277ccccf
commit
3502ec80e4
8 changed files with 304 additions and 86 deletions
|
@ -28,6 +28,35 @@ CardInfo::~CardInfo()
|
||||||
delete pixmap;
|
delete pixmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString CardInfo::getMainCardType() const
|
||||||
|
{
|
||||||
|
QString result = getCardType();
|
||||||
|
/*
|
||||||
|
Legendary Artifact Creature - Golem
|
||||||
|
Instant // Instant
|
||||||
|
*/
|
||||||
|
|
||||||
|
int pos;
|
||||||
|
if ((pos = result.indexOf('-')) != -1)
|
||||||
|
result.remove(pos, result.length());
|
||||||
|
if ((pos = result.indexOf("//")) != -1)
|
||||||
|
result.remove(pos, result.length());
|
||||||
|
result = result.simplified();
|
||||||
|
/*
|
||||||
|
Legendary Artifact Creature
|
||||||
|
Instant
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ((pos = result.lastIndexOf(' ')) != -1)
|
||||||
|
result = result.mid(pos + 1);
|
||||||
|
/*
|
||||||
|
Creature
|
||||||
|
Instant
|
||||||
|
*/
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
void CardInfo::addEdition(const QString &edition)
|
void CardInfo::addEdition(const QString &edition)
|
||||||
{
|
{
|
||||||
if (!editions.contains(edition))
|
if (!editions.contains(edition))
|
||||||
|
|
|
@ -28,6 +28,7 @@ public:
|
||||||
QString getCardType() const { return cardtype; }
|
QString getCardType() const { return cardtype; }
|
||||||
QString getPowTough() const { return powtough; }
|
QString getPowTough() const { return powtough; }
|
||||||
QStringList getText() const { return text; }
|
QStringList getText() const { return text; }
|
||||||
|
QString getMainCardType() const;
|
||||||
void addEdition(const QString &edition);
|
void addEdition(const QString &edition);
|
||||||
QPixmap *getPixmap();
|
QPixmap *getPixmap();
|
||||||
void saveToStream(QDataStream &stream);
|
void saveToStream(QDataStream &stream);
|
||||||
|
|
|
@ -7,7 +7,19 @@
|
||||||
#include "decklist.h"
|
#include "decklist.h"
|
||||||
#include "carddatabase.h"
|
#include "carddatabase.h"
|
||||||
|
|
||||||
QString DecklistZone::getVisibleName() const
|
AbstractDecklistNode::AbstractDecklistNode(InnerDecklistNode *_parent)
|
||||||
|
: parent(_parent)
|
||||||
|
{
|
||||||
|
if (parent)
|
||||||
|
parent->append(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
InnerDecklistNode::~InnerDecklistNode()
|
||||||
|
{
|
||||||
|
clearTree();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString InnerDecklistNode::getVisibleName() const
|
||||||
{
|
{
|
||||||
if (name == "main")
|
if (name == "main")
|
||||||
return QObject::tr("Maindeck");
|
return QObject::tr("Maindeck");
|
||||||
|
@ -17,15 +29,35 @@ QString DecklistZone::getVisibleName() const
|
||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InnerDecklistNode::clearTree()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < size(); i++)
|
||||||
|
delete at(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int InnerDecklistNode::recursiveCount() const
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i < size(); i++) {
|
||||||
|
InnerDecklistNode *node = dynamic_cast<InnerDecklistNode *>(at(i));
|
||||||
|
if (node)
|
||||||
|
result += node->recursiveCount();
|
||||||
|
else
|
||||||
|
result += 1;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
DeckList::DeckList(CardDatabase *_db, QObject *parent)
|
DeckList::DeckList(CardDatabase *_db, QObject *parent)
|
||||||
: QObject(parent), db(_db)
|
: QObject(parent), db(_db)
|
||||||
{
|
{
|
||||||
|
root = new InnerDecklistNode;
|
||||||
initZones();
|
initZones();
|
||||||
}
|
}
|
||||||
|
|
||||||
DeckList::~DeckList()
|
DeckList::~DeckList()
|
||||||
{
|
{
|
||||||
cleanList();
|
delete root;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DeckList::loadFromFile_Native(QIODevice *device)
|
bool DeckList::loadFromFile_Native(QIODevice *device)
|
||||||
|
@ -43,15 +75,14 @@ bool DeckList::loadFromFile_Native(QIODevice *device)
|
||||||
else if (xml.name() == "comments")
|
else if (xml.name() == "comments")
|
||||||
comments = xml.readElementText();
|
comments = xml.readElementText();
|
||||||
else if (xml.name() == "zone") {
|
else if (xml.name() == "zone") {
|
||||||
DecklistZone *zone = new DecklistZone(xml.attributes().value("name").toString());
|
InnerDecklistNode *node = new InnerDecklistNode(xml.attributes().value("name").toString(), root);
|
||||||
zones.append(zone);
|
|
||||||
while (!xml.atEnd()) {
|
while (!xml.atEnd()) {
|
||||||
if (xml.readNext() == QXmlStreamReader::EndElement)
|
if (xml.readNext() == QXmlStreamReader::EndElement)
|
||||||
break;
|
break;
|
||||||
if (xml.name() == "card") {
|
if (xml.name() == "card") {
|
||||||
const int number = xml.attributes().value("number").toString().toInt();
|
const int number = xml.attributes().value("number").toString().toInt();
|
||||||
const QString card = xml.attributes().value("name").toString();
|
const QString card = xml.attributes().value("name").toString();
|
||||||
zone->append(new DecklistRow(number, card));
|
new DecklistCardNode(card, number, node);
|
||||||
while (!xml.atEnd())
|
while (!xml.atEnd())
|
||||||
if (xml.readNext() == QXmlStreamReader::EndElement)
|
if (xml.readNext() == QXmlStreamReader::EndElement)
|
||||||
break;
|
break;
|
||||||
|
@ -66,7 +97,7 @@ bool DeckList::loadFromFile_Native(QIODevice *device)
|
||||||
|
|
||||||
bool DeckList::saveToFile_Native(QIODevice *device)
|
bool DeckList::saveToFile_Native(QIODevice *device)
|
||||||
{
|
{
|
||||||
QXmlStreamWriter xml(device);
|
/* QXmlStreamWriter xml(device);
|
||||||
xml.setAutoFormatting(true);
|
xml.setAutoFormatting(true);
|
||||||
xml.writeStartDocument();
|
xml.writeStartDocument();
|
||||||
|
|
||||||
|
@ -91,11 +122,11 @@ bool DeckList::saveToFile_Native(QIODevice *device)
|
||||||
|
|
||||||
xml.writeEndDocument();
|
xml.writeEndDocument();
|
||||||
return true;
|
return true;
|
||||||
}
|
*/}
|
||||||
|
|
||||||
bool DeckList::loadFromFile_Plain(QIODevice *device)
|
bool DeckList::loadFromFile_Plain(QIODevice *device)
|
||||||
{
|
{
|
||||||
initZones();
|
/* initZones();
|
||||||
|
|
||||||
QTextStream in(device);
|
QTextStream in(device);
|
||||||
while (!in.atEnd()) {
|
while (!in.atEnd()) {
|
||||||
|
@ -125,18 +156,18 @@ bool DeckList::loadFromFile_Plain(QIODevice *device)
|
||||||
zone->append(new DecklistRow(number, line.mid(i + 1)));
|
zone->append(new DecklistRow(number, line.mid(i + 1)));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
*/}
|
||||||
|
|
||||||
bool DeckList::saveToFile_Plain(QIODevice *device)
|
bool DeckList::saveToFile_Plain(QIODevice *device)
|
||||||
{
|
{
|
||||||
QTextStream out(device);
|
/* QTextStream out(device);
|
||||||
for (int i = 0; i < zones.size(); i++)
|
for (int i = 0; i < zones.size(); i++)
|
||||||
for (int j = 0; j < zones[i]->size(); j++) {
|
for (int j = 0; j < zones[i]->size(); j++) {
|
||||||
DecklistRow *r = zones[i]->at(j);
|
DecklistRow *r = zones[i]->at(j);
|
||||||
out << QString("%1%2 %3\n").arg(zones[i]->getName() == "side" ? "SB: " : "").arg(r->getNumber()).arg(r->getCard());
|
out << QString("%1%2 %3\n").arg(zones[i]->getName() == "side" ? "SB: " : "").arg(r->getNumber()).arg(r->getCard());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
*/}
|
||||||
|
|
||||||
bool DeckList::loadFromFile(const QString &fileName, FileFormat fmt, QWidget *parent)
|
bool DeckList::loadFromFile(const QString &fileName, FileFormat fmt, QWidget *parent)
|
||||||
{
|
{
|
||||||
|
@ -151,8 +182,8 @@ bool DeckList::loadFromFile(const QString &fileName, FileFormat fmt, QWidget *pa
|
||||||
case CockatriceFormat: result = loadFromFile_Native(&file); break;
|
case CockatriceFormat: result = loadFromFile_Native(&file); break;
|
||||||
}
|
}
|
||||||
if (result) {
|
if (result) {
|
||||||
cacheCardPictures(parent);
|
|
||||||
emit deckLoaded();
|
emit deckLoaded();
|
||||||
|
cacheCardPictures(parent);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -225,39 +256,39 @@ bool DeckList::saveDialog(QWidget *parent)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DeckList::cacheCardPicturesHelper(InnerDecklistNode *item, QProgressDialog *progress)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < item->size(); i++) {
|
||||||
|
DecklistCardNode *node = dynamic_cast<DecklistCardNode *>(item->at(i));
|
||||||
|
if (node) {
|
||||||
|
db->getCard(node->getName())->getPixmap();
|
||||||
|
progress->setValue(progress->value() + 1);
|
||||||
|
} else
|
||||||
|
cacheCardPicturesHelper(dynamic_cast<InnerDecklistNode *>(item->at(i)), progress);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void DeckList::cacheCardPictures(QWidget *parent)
|
void DeckList::cacheCardPictures(QWidget *parent)
|
||||||
{
|
{
|
||||||
int totalCards = 0;
|
int totalCards = root->recursiveCount();
|
||||||
for (int i = 0; i < zones.size(); i++)
|
|
||||||
totalCards += zones[i]->size();
|
|
||||||
|
|
||||||
QProgressDialog progress(tr("Caching card pictures..."), QString(), 0, totalCards, parent);
|
QProgressDialog progress(tr("Caching card pictures..."), QString(), 0, totalCards, parent);
|
||||||
progress.setMinimumDuration(1000);
|
progress.setMinimumDuration(1000);
|
||||||
progress.setWindowModality(Qt::WindowModal);
|
progress.setWindowModality(Qt::WindowModal);
|
||||||
|
|
||||||
for (int i = 0; i < zones.size(); i++)
|
cacheCardPicturesHelper(root, &progress);
|
||||||
for (int j = 0; j < zones[i]->size(); j++) {
|
|
||||||
db->getCard(zones[i]->at(j)->getCard())->getPixmap();
|
|
||||||
progress.setValue(progress.value() + 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckList::cleanList()
|
void DeckList::cleanList()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < zones.size(); i++) {
|
root->clearTree();
|
||||||
for (int j = 0; j < zones[i]->size(); j++)
|
|
||||||
delete zones[i]->at(j);
|
|
||||||
zones[i]->clear();
|
|
||||||
delete zones[i];
|
|
||||||
}
|
|
||||||
zones.clear();
|
|
||||||
setName();
|
setName();
|
||||||
setComments();
|
setComments();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckList::initZones()
|
void DeckList::initZones()
|
||||||
{
|
{
|
||||||
// possibly Magic specific
|
/* // possibly Magic specific
|
||||||
zones.append(new DecklistZone("main"));
|
zones.append(new DecklistZone("main"));
|
||||||
zones.append(new DecklistZone("side"));
|
zones.append(new DecklistZone("side"));
|
||||||
}
|
*/}
|
||||||
|
|
|
@ -6,26 +6,45 @@
|
||||||
|
|
||||||
class CardDatabase;
|
class CardDatabase;
|
||||||
class QIODevice;
|
class QIODevice;
|
||||||
|
class QProgressDialog;
|
||||||
|
|
||||||
class DecklistRow {
|
class InnerDecklistNode;
|
||||||
private:
|
|
||||||
int number;
|
class AbstractDecklistNode {
|
||||||
QString card;
|
protected:
|
||||||
|
InnerDecklistNode *parent;
|
||||||
public:
|
public:
|
||||||
DecklistRow(int _number = 1, const QString &_card = QString()) : number(_number), card(_card) { }
|
AbstractDecklistNode(InnerDecklistNode *_parent = 0);
|
||||||
int getNumber() const { return number; }
|
virtual bool hasChildren() const = 0;
|
||||||
void setNumber(int _number) { number = _number; }
|
virtual QString getName() const = 0;
|
||||||
QString getCard() const { return card; }
|
const InnerDecklistNode *getParent() const { return parent; }
|
||||||
void setCard(const QString &_card) { card = _card; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class DecklistZone : public QList<DecklistRow *> {
|
class InnerDecklistNode : public AbstractDecklistNode, public QList<AbstractDecklistNode *> {
|
||||||
private:
|
private:
|
||||||
QString name;
|
QString name;
|
||||||
public:
|
public:
|
||||||
DecklistZone(const QString &_name) : name(_name) { }
|
InnerDecklistNode(const QString &_name = QString(), InnerDecklistNode *_parent = 0) : AbstractDecklistNode(_parent), name(_name) { }
|
||||||
|
~InnerDecklistNode();
|
||||||
|
bool hasChildren() const { return true; }
|
||||||
QString getName() const { return name; }
|
QString getName() const { return name; }
|
||||||
QString getVisibleName() const;
|
void setName(const QString &_name) { name = _name; }
|
||||||
|
virtual QString getVisibleName() const;
|
||||||
|
void clearTree();
|
||||||
|
int recursiveCount() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
class DecklistCardNode : public AbstractDecklistNode {
|
||||||
|
private:
|
||||||
|
QString name;
|
||||||
|
int number;
|
||||||
|
public:
|
||||||
|
DecklistCardNode(const QString &_name = QString(), int _number = 1, InnerDecklistNode *_parent = 0) : AbstractDecklistNode(_parent), name(_name), number(_number) { }
|
||||||
|
bool hasChildren() const { return false; }
|
||||||
|
int getNumber() const { return number; }
|
||||||
|
void setNumber(int _number) { number = _number; }
|
||||||
|
QString getName() const { return name; }
|
||||||
|
void setName(const QString &_name) { name = _name; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class DeckList : public QObject {
|
class DeckList : public QObject {
|
||||||
|
@ -39,7 +58,8 @@ private:
|
||||||
QString name, comments;
|
QString name, comments;
|
||||||
QString lastFileName;
|
QString lastFileName;
|
||||||
FileFormat lastFileFormat;
|
FileFormat lastFileFormat;
|
||||||
QList<DecklistZone *> zones;
|
InnerDecklistNode *root;
|
||||||
|
void cacheCardPicturesHelper(InnerDecklistNode *item, QProgressDialog *progress);
|
||||||
signals:
|
signals:
|
||||||
void deckLoaded();
|
void deckLoaded();
|
||||||
public slots:
|
public slots:
|
||||||
|
@ -65,8 +85,7 @@ public:
|
||||||
void cleanList();
|
void cleanList();
|
||||||
void initZones();
|
void initZones();
|
||||||
|
|
||||||
int zoneCount() const { return zones.size(); }
|
InnerDecklistNode *getRoot() const { return root; }
|
||||||
DecklistZone *getZoneByIndex(int index) const { return zones[index]; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -9,31 +9,116 @@ DeckListModel::DeckListModel(CardDatabase *_db, QObject *parent)
|
||||||
: QAbstractItemModel(parent), db(_db)
|
: QAbstractItemModel(parent), db(_db)
|
||||||
{
|
{
|
||||||
deckList = new DeckList(db, this);
|
deckList = new DeckList(db, this);
|
||||||
connect(deckList, SIGNAL(deckLoaded()), this, SLOT(resetModel()));
|
connect(deckList, SIGNAL(deckLoaded()), this, SLOT(rebuildTree()));
|
||||||
|
root = new InnerDecklistNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
DeckListModel::~DeckListModel()
|
DeckListModel::~DeckListModel()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckListModel::resetModel()
|
void DeckListModel::debugIndexInfo(const QString &func, const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
|
quint32 id = index.internalId(); // 32 bit int, from MSB to LSB:
|
||||||
|
int zone = id >> 30; // 2 bits - zone
|
||||||
|
int cardtype = (id >> 25) & 0x1f; // 5 bits - card type
|
||||||
|
int card = (id >> 3) & 0x3fffff; // 22 bits - card
|
||||||
|
int column = id & 0x7; // 3 bits - column
|
||||||
|
|
||||||
|
if (index.isValid())
|
||||||
|
qDebug(QString("index: function = %1, zone = %2, cardtype = %3, card = %4, column = %5").arg(func).arg(zone).arg(cardtype).arg(card).arg(column).toLatin1());
|
||||||
|
else
|
||||||
|
qDebug(QString("index: function = %1, invalid").arg(func).toLatin1());
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeckListModel::debugShowTree(InnerDecklistNode *node, int depth) const
|
||||||
|
{
|
||||||
|
for (int i = 0; i < node->size(); i++) {
|
||||||
|
DecklistModelCardNode *foo = dynamic_cast<DecklistModelCardNode *>(node->at(i));
|
||||||
|
if (!foo) {
|
||||||
|
InnerDecklistNode *bar = dynamic_cast<InnerDecklistNode *>(node->at(i));
|
||||||
|
qDebug(QString("%1%2").arg(QString(depth * 4, ' ')).arg(bar->getName()).toLatin1());
|
||||||
|
debugShowTree(bar, depth + 1);
|
||||||
|
} else
|
||||||
|
qDebug(QString("%1%2 %3").arg(QString(depth * 4, ' ')).arg(foo->getNumber()).arg(foo->getName()).toLatin1());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeckListModel::rebuildTree()
|
||||||
|
{
|
||||||
|
root->clearTree();
|
||||||
|
InnerDecklistNode *listRoot = deckList->getRoot();
|
||||||
|
for (int i = 0; i < listRoot->size(); i++) {
|
||||||
|
InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
|
||||||
|
InnerDecklistNode *node = new InnerDecklistNode(currentZone->getName(), root);
|
||||||
|
for (int j = 0; j < currentZone->size(); j++) {
|
||||||
|
DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
|
||||||
|
|
||||||
|
QString cardType = db->getCard(currentCard->getName())->getMainCardType();
|
||||||
|
InnerDecklistNode *cardTypeNode = dynamic_cast<InnerDecklistNode *>(findNode(cardType, node));
|
||||||
|
if (!cardTypeNode)
|
||||||
|
cardTypeNode = new InnerDecklistNode(cardType, node);
|
||||||
|
|
||||||
|
DecklistModelCardNode *newCard = new DecklistModelCardNode(currentCard, cardTypeNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debugShowTree(root, 0);
|
||||||
|
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AbstractDecklistNode *DeckListModel::findNode(const QString &name, InnerDecklistNode *root) const
|
||||||
|
{
|
||||||
|
for (int i = 0; i < root->size(); i++)
|
||||||
|
if (root->at(i)->getName() == name)
|
||||||
|
return root->at(i);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractDecklistNode *DeckListModel::findNode(const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
// debugIndexInfo("findNode", index);
|
||||||
|
if (index.isValid()) {
|
||||||
|
InnerDecklistNode *parentNode = dynamic_cast<InnerDecklistNode *>(findNode(index.parent()));
|
||||||
|
return parentNode->at(index.row());
|
||||||
|
} else
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
int DeckListModel::rowCount(const QModelIndex &parent) const
|
int DeckListModel::rowCount(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
if (!parent.isValid()) // parent = root
|
// debugIndexInfo("rowCount", parent);
|
||||||
return deckList->zoneCount();
|
InnerDecklistNode *node = dynamic_cast<InnerDecklistNode *>(findNode(parent));
|
||||||
else if (!parent.parent().isValid()) // parent = zone root
|
if (node) {
|
||||||
return deckList->getZoneByIndex(parent.row())->size();
|
// qDebug(QString(" rowCount: return %1").arg(node->size()).toLatin1());
|
||||||
else // parent = card
|
return node->size();
|
||||||
|
} else {
|
||||||
|
// qDebug(" rowCount: return const 0");
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DeckListModel::hasChildren(const QModelIndex &parent) const
|
bool DeckListModel::hasChildren(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
return !parent.parent().isValid();
|
// debugIndexInfo("hasChildren", parent);
|
||||||
|
if (!parent.isValid() && root->size())
|
||||||
|
return true;
|
||||||
|
if (parent.column() != 0)
|
||||||
|
return false;
|
||||||
|
/*
|
||||||
|
InnerDecklistNode *node = dynamic_cast<InnerDecklistNode *>(findNode(parent));
|
||||||
|
if (!node)
|
||||||
|
qDebug("line 109: return false");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
qDebug(QString("line 112: return %1").arg(node->size()).toLatin1());
|
||||||
|
return node->size();
|
||||||
|
|
||||||
|
if (!parent.isValid())
|
||||||
|
return true;
|
||||||
|
qDebug(QString(" hasChildren: return %1").arg((!parent.column() && (((parent.internalId() >> 3) & 0x3fffff) == 0x3fffff))).toLatin1());
|
||||||
|
*/ // An item (possibly) has children if its column is zero and its card is -1.
|
||||||
|
return (!parent.column() && (((parent.internalId() >> 3) & 0x3fffff) == 0x3fffff));
|
||||||
}
|
}
|
||||||
|
|
||||||
int DeckListModel::columnCount(const QModelIndex &/*parent*/) const
|
int DeckListModel::columnCount(const QModelIndex &/*parent*/) const
|
||||||
|
@ -43,14 +128,17 @@ int DeckListModel::columnCount(const QModelIndex &/*parent*/) const
|
||||||
|
|
||||||
QVariant DeckListModel::data(const QModelIndex &index, int role) const
|
QVariant DeckListModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
|
// debugIndexInfo("data", index);
|
||||||
if (!index.isValid())
|
if (!index.isValid())
|
||||||
return QVariant();
|
return QVariant();
|
||||||
if (index.column() >= 2)
|
if (index.column() >= 2)
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
if (!index.parent().isValid()) {
|
AbstractDecklistNode *tempNode = findNode(index);
|
||||||
if (index.row() >= deckList->zoneCount())
|
if (tempNode == root)
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
|
if (tempNode->hasChildren()) {
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::FontRole: {
|
case Qt::FontRole: {
|
||||||
QFont f;
|
QFont f;
|
||||||
|
@ -59,10 +147,11 @@ QVariant DeckListModel::data(const QModelIndex &index, int role) const
|
||||||
}
|
}
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
case Qt::EditRole: {
|
case Qt::EditRole: {
|
||||||
DecklistZone *zone = deckList->getZoneByIndex(index.row());
|
|
||||||
switch (index.column()) {
|
switch (index.column()) {
|
||||||
case 0: return zone->getVisibleName();
|
case 0: {
|
||||||
case 1: return QVariant();
|
InnerDecklistNode *node = dynamic_cast<InnerDecklistNode *>(tempNode);
|
||||||
|
return node->getVisibleName();
|
||||||
|
}
|
||||||
default: return QVariant();
|
default: return QVariant();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,16 +160,13 @@ QVariant DeckListModel::data(const QModelIndex &index, int role) const
|
||||||
default: return QVariant();
|
default: return QVariant();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
DecklistZone *zone = deckList->getZoneByIndex(index.parent().row());
|
|
||||||
if (index.row() >= zone->size())
|
|
||||||
return QVariant();
|
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
case Qt::EditRole: {
|
case Qt::EditRole: {
|
||||||
DecklistRow *r = zone->at(index.row());
|
DecklistModelCardNode *node = dynamic_cast<DecklistModelCardNode *>(tempNode);
|
||||||
switch (index.column()) {
|
switch (index.column()) {
|
||||||
case 0: return r->getNumber();
|
case 0: return node->getNumber();
|
||||||
case 1: return r->getCard();
|
case 1: return node->getName();
|
||||||
default: return QVariant();
|
default: return QVariant();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,26 +194,53 @@ QVariant DeckListModel::headerData(int section, Qt::Orientation orientation, int
|
||||||
|
|
||||||
QModelIndex DeckListModel::index(int row, int column, const QModelIndex &parent) const
|
QModelIndex DeckListModel::index(int row, int column, const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
int id;
|
// debugIndexInfo("index", parent);
|
||||||
if (!parent.isValid())
|
// for explanation of the bit shifting, look at parent()
|
||||||
id = -((row + 1) * 1000 + column);
|
int indexZone, indexCardType, indexCard;
|
||||||
else
|
if (!parent.isValid()) {
|
||||||
id = parent.row() * 1000000 + row * 1000 + column;
|
indexZone = row;
|
||||||
return createIndex(row, column, id);
|
indexCardType = 0x1f;
|
||||||
|
indexCard = 0x3fffff;
|
||||||
|
} else {
|
||||||
|
quint32 pid = parent.internalId();
|
||||||
|
indexZone = pid >> 30;
|
||||||
|
int pcardtype = (pid >> 25) & 0x1f;
|
||||||
|
|
||||||
|
if (pcardtype == 0x1f) {
|
||||||
|
indexCardType = row;
|
||||||
|
indexCard = 0x3fffff;
|
||||||
|
} else {
|
||||||
|
indexCardType = pcardtype;
|
||||||
|
indexCard = row;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// qDebug(QString("index(): zone = %1, cardtype = %2, card = %3, column = %4").arg(indexZone).arg(indexCardType).arg(indexCard).arg(column).toLatin1());
|
||||||
|
return createIndex(row, column, (indexZone << 30) + (indexCardType << 25) + (indexCard << 3) + column);
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex DeckListModel::parent(const QModelIndex &ind) const
|
QModelIndex DeckListModel::parent(const QModelIndex &ind) const
|
||||||
{
|
{
|
||||||
if ((int) ind.internalId() < 0)
|
// debugIndexInfo("parent", ind);
|
||||||
|
|
||||||
|
quint32 id = ind.internalId(); // 32 bit int, from MSB to LSB:
|
||||||
|
int zone = id >> 30; // 2 bits - zone
|
||||||
|
int cardtype = (id >> 25) & 0x1f; // 5 bits - card type
|
||||||
|
int card = (id >> 3) & 0x3fffff; // 22 bits - card
|
||||||
|
// int column = id & 0x7; // 3 bits - column
|
||||||
|
|
||||||
|
if (cardtype == 0x1f)
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
|
else if (card == 0x3fffff)
|
||||||
|
return index(zone, 0);
|
||||||
else
|
else
|
||||||
return index(ind.internalId() / 1000000, 0);
|
return index(cardtype, 0, index(zone, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
Qt::ItemFlags DeckListModel::flags(const QModelIndex &index) const
|
Qt::ItemFlags DeckListModel::flags(const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
Qt::ItemFlags result = Qt::ItemIsEnabled;
|
Qt::ItemFlags result = Qt::ItemIsEnabled;
|
||||||
if (index.parent().isValid()) {
|
if (((index.internalId() >> 3) & 0x3fffff) != 0x3fffff) {
|
||||||
result |= Qt::ItemIsSelectable;
|
result |= Qt::ItemIsSelectable;
|
||||||
if (index.column() == 0)
|
if (index.column() == 0)
|
||||||
result |= Qt::ItemIsEditable;
|
result |= Qt::ItemIsEditable;
|
||||||
|
@ -137,12 +250,13 @@ Qt::ItemFlags DeckListModel::flags(const QModelIndex &index) const
|
||||||
|
|
||||||
bool DeckListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
bool DeckListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||||
{
|
{
|
||||||
if (!index.isValid() || !index.parent().isValid() || role != Qt::EditRole)
|
DecklistModelCardNode *node = dynamic_cast<DecklistModelCardNode *>(findNode(index));
|
||||||
|
if (!node || (role != Qt::EditRole))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch (index.column()) {
|
switch (index.column()) {
|
||||||
case 0: deckList->getZoneByIndex(index.parent().row())->at(index.row())->setNumber(value.toInt()); break;
|
case 0: node->setNumber(value.toInt()); break;
|
||||||
case 1: deckList->getZoneByIndex(index.parent().row())->at(index.row())->setCard(value.toString()); break;
|
case 1: node->setName(value.toString()); break;
|
||||||
default: return false;
|
default: return false;
|
||||||
}
|
}
|
||||||
emit dataChanged(index, index);
|
emit dataChanged(index, index);
|
||||||
|
@ -151,21 +265,30 @@ bool DeckListModel::setData(const QModelIndex &index, const QVariant &value, int
|
||||||
|
|
||||||
bool DeckListModel::removeRows(int row, int count, const QModelIndex &parent)
|
bool DeckListModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||||
{
|
{
|
||||||
// Inserting zones is not supported.
|
/* DecklistNode *node = findNode(parent);
|
||||||
if (!parent.isValid())
|
if (row + count > node->size())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
beginRemoveRows(parent, row, row + count - 1);
|
beginRemoveRows(parent, row, row + count - 1);
|
||||||
|
|
||||||
for (int i = 0; i < count; i++)
|
for (int i = 0; i < count; i++)
|
||||||
deckList->getZoneByIndex(parent.row())->removeAt(row);
|
delete node->takeAt(row);
|
||||||
|
|
||||||
endRemoveRows();
|
endRemoveRows();
|
||||||
return true;
|
return true;
|
||||||
|
*/}
|
||||||
|
/*
|
||||||
|
void DeckListModel::insertCard(...)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DeckListModel::removeCard(...)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
*/
|
||||||
bool DeckListModel::insertRows(int row, int count, const QModelIndex &parent)
|
bool DeckListModel::insertRows(int row, int count, const QModelIndex &parent)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
// Inserting zones is not supported.
|
// Inserting zones is not supported.
|
||||||
if (!parent.isValid())
|
if (!parent.isValid())
|
||||||
return false;
|
return false;
|
||||||
|
@ -177,7 +300,7 @@ bool DeckListModel::insertRows(int row, int count, const QModelIndex &parent)
|
||||||
|
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
return true;
|
return true;
|
||||||
}
|
*/}
|
||||||
|
|
||||||
void DeckListModel::cleanList()
|
void DeckListModel::cleanList()
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,10 +7,22 @@
|
||||||
|
|
||||||
class CardDatabase;
|
class CardDatabase;
|
||||||
|
|
||||||
|
class DecklistModelCardNode : public AbstractDecklistNode {
|
||||||
|
private:
|
||||||
|
DecklistCardNode *dataNode;
|
||||||
|
public:
|
||||||
|
DecklistModelCardNode(DecklistCardNode *_dataNode, InnerDecklistNode *_parent) : AbstractDecklistNode(_parent), dataNode(_dataNode) { }
|
||||||
|
bool hasChildren() const { return false; }
|
||||||
|
inline int getNumber() const { return dataNode->getNumber(); }
|
||||||
|
inline void setNumber(int _number) { dataNode->setNumber(_number); }
|
||||||
|
inline QString getName() const { return dataNode->getName(); }
|
||||||
|
inline void setName(const QString &_name) { dataNode->setName(_name); }
|
||||||
|
};
|
||||||
|
|
||||||
class DeckListModel : public QAbstractItemModel {
|
class DeckListModel : public QAbstractItemModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private slots:
|
private slots:
|
||||||
void resetModel();
|
void rebuildTree();
|
||||||
public:
|
public:
|
||||||
DeckListModel(CardDatabase *_db, QObject *parent = 0);
|
DeckListModel(CardDatabase *_db, QObject *parent = 0);
|
||||||
~DeckListModel();
|
~DeckListModel();
|
||||||
|
@ -27,11 +39,14 @@ public:
|
||||||
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
|
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
|
||||||
void cleanList();
|
void cleanList();
|
||||||
DeckList *getDeckList() const { return deckList; }
|
DeckList *getDeckList() const { return deckList; }
|
||||||
bool loadFromFile(const QString &fileName, DeckList::FileFormat fmt);
|
|
||||||
bool saveToFile(const QString &fileName, DeckList::FileFormat fmt);
|
|
||||||
private:
|
private:
|
||||||
CardDatabase *db;
|
CardDatabase *db;
|
||||||
DeckList *deckList;
|
DeckList *deckList;
|
||||||
|
InnerDecklistNode *root;
|
||||||
|
AbstractDecklistNode *findNode(const QString &name, InnerDecklistNode *root) const;
|
||||||
|
AbstractDecklistNode *findNode(const QModelIndex &index) const;
|
||||||
|
void debugIndexInfo(const QString &func, const QModelIndex &index) const;
|
||||||
|
void debugShowTree(InnerDecklistNode *node, int depth) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -45,7 +45,7 @@ void DlgStartGame::actLoad()
|
||||||
QStringList DlgStartGame::getDeckList() const
|
QStringList DlgStartGame::getDeckList() const
|
||||||
{
|
{
|
||||||
QStringList result;
|
QStringList result;
|
||||||
DeckList *deckList = tableModel->getDeckList();
|
/* DeckList *deckList = tableModel->getDeckList();
|
||||||
for (int i = 0; i < deckList->zoneCount(); i++) {
|
for (int i = 0; i < deckList->zoneCount(); i++) {
|
||||||
DecklistZone *zone = deckList->getZoneByIndex(i);
|
DecklistZone *zone = deckList->getZoneByIndex(i);
|
||||||
for (int j = 0; j < zone->size(); j++) {
|
for (int j = 0; j < zone->size(); j++) {
|
||||||
|
@ -54,5 +54,5 @@ QStringList DlgStartGame::getDeckList() const
|
||||||
result << QString("%1%2").arg(zone->getName() == "side" ? "SB:" : "").arg(r->getCard());
|
result << QString("%1%2").arg(zone->getName() == "side" ? "SB:" : "").arg(r->getCard());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
*/ return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,7 +91,7 @@ WndDeckEditor::WndDeckEditor(CardDatabase *_db, QWidget *parent)
|
||||||
aSaveDeck->setShortcuts(QKeySequence::Save);
|
aSaveDeck->setShortcuts(QKeySequence::Save);
|
||||||
connect(aSaveDeck, SIGNAL(triggered()), this, SLOT(actSaveDeck()));
|
connect(aSaveDeck, SIGNAL(triggered()), this, SLOT(actSaveDeck()));
|
||||||
aSaveDeckAs = new QAction(tr("&Save deck as..."), this);
|
aSaveDeckAs = new QAction(tr("&Save deck as..."), this);
|
||||||
aSaveDeckAs->setShortcuts(QKeySequence::SaveAs);
|
// aSaveDeckAs->setShortcuts(QKeySequence::SaveAs);
|
||||||
connect(aSaveDeckAs, SIGNAL(triggered()), this, SLOT(actSaveDeckAs()));
|
connect(aSaveDeckAs, SIGNAL(triggered()), this, SLOT(actSaveDeckAs()));
|
||||||
aClose = new QAction(tr("&Close"), this);
|
aClose = new QAction(tr("&Close"), this);
|
||||||
aClose->setShortcut(tr("Ctrl+Q"));
|
aClose->setShortcut(tr("Ctrl+Q"));
|
||||||
|
|
Loading…
Reference in a new issue