diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index 3876df24..d3a50e3f 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -62,11 +62,21 @@ void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, painter->drawRect(QRectF(0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1)); } if (counters) { - painter->setFont(QFont("Times", 32, QFont::Bold)); - painter->setPen(QPen(Qt::black)); - painter->setBackground(QBrush(QColor(255, 255, 255, 100))); - painter->setBackgroundMode(Qt::OpaqueMode); - painter->drawText(boundingRect(), Qt::AlignCenter, QString::number(counters)); + QString numStr = QString::number(counters); + QFont font("Times", 32, QFont::Bold); + QFontMetrics fm(font); + QRect br = fm.boundingRect(numStr); + double w = br.width() * 1.42; + double h = br.height() * 1.42; + if (w < h) + w = h; + + painter->setPen(Qt::black); + painter->setBrush(QColor(255, 255, 255, 150)); + painter->drawEllipse(QRectF((boundingRect().width() - w) / 2.0, (boundingRect().height() - h) / 2.0, w, h)); + + painter->setFont(font); + painter->drawText(boundingRect(), Qt::AlignCenter, numStr); } painter->restore(); } diff --git a/cockatrice/src/decklist.cpp b/cockatrice/src/decklist.cpp index b5ba557c..cfb1cf58 100644 --- a/cockatrice/src/decklist.cpp +++ b/cockatrice/src/decklist.cpp @@ -53,6 +53,11 @@ AbstractDecklistNode *InnerDecklistNode::findChild(const QString &name) return 0; } +int InnerDecklistNode::height() const +{ + return at(0)->height() + 1; +} + int InnerDecklistNode::recursiveCount(bool countTotalCards) const { int result = 0; diff --git a/cockatrice/src/decklist.h b/cockatrice/src/decklist.h index 53af3a1c..9c962b5a 100644 --- a/cockatrice/src/decklist.h +++ b/cockatrice/src/decklist.h @@ -19,6 +19,7 @@ public: virtual QString getName() const = 0; InnerDecklistNode *getParent() const { return parent; } int depth() const; + virtual int height() const = 0; virtual bool compare(AbstractDecklistNode *other) const = 0; }; @@ -34,6 +35,7 @@ public: virtual QString getVisibleName() const; void clearTree(); AbstractDecklistNode *findChild(const QString &name); + int height() const; int recursiveCount(bool countTotalCards = false) const; bool compare(AbstractDecklistNode *other) const; void sort(Qt::SortOrder order = Qt::AscendingOrder); @@ -46,6 +48,7 @@ public: virtual void setNumber(int _number) = 0; virtual QString getName() const = 0; virtual void setName(const QString &_name) = 0; + int height() const { return 0; } bool compare(AbstractDecklistNode *other) const; }; diff --git a/cockatrice/src/decklistmodel.cpp b/cockatrice/src/decklistmodel.cpp index ca2c4edf..f7fdc4b4 100644 --- a/cockatrice/src/decklistmodel.cpp +++ b/cockatrice/src/decklistmodel.cpp @@ -2,6 +2,10 @@ #include #include #include +#include +#include +#include +#include #include "decklistmodel.h" #include "carddatabase.h" @@ -282,3 +286,43 @@ void DeckListModel::cleanList() deckList->cleanList(); reset(); } + +void DeckListModel::printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node) +{ + cursor->insertBlock(); + cursor->insertText(node->getVisibleName()); + if (node->height() == 1) { + QTextTableFormat tableFormat; + // XXX + QTextTable *table = cursor->insertTable(node->size(), 2, tableFormat); + for (int i = 0; i < node->size(); i++) { + AbstractDecklistCardNode *card = dynamic_cast(node->at(i)); + + QTextCursor cellCursor = table->cellAt(i, 0).firstCursorPosition(); + cellCursor.insertText(QString::number(card->getNumber())); + cellCursor = table->cellAt(i, 1).firstCursorPosition(); + cellCursor.insertText(card->getName()); + } + } else { + for (int i = 0; i < node->size(); i++) + printDeckListNode(cursor, dynamic_cast(node->at(i))); + } + cursor->movePosition(QTextCursor::End); +} + +void DeckListModel::printDeckList(QPrinter *printer) +{ + QTextDocument doc; + QTextCursor cursor(&doc); + + cursor.insertBlock(); + cursor.insertText(deckList->getName()); + + cursor.insertBlock(); + cursor.insertText(deckList->getComments()); + + for (int i = 0; i < root->size(); i++) + printDeckListNode(&cursor, dynamic_cast(root->at(i))); + + doc.print(printer); +} diff --git a/cockatrice/src/decklistmodel.h b/cockatrice/src/decklistmodel.h index e5fb40ac..82003b43 100644 --- a/cockatrice/src/decklistmodel.h +++ b/cockatrice/src/decklistmodel.h @@ -6,6 +6,8 @@ #include "decklist.h" class CardDatabase; +class QPrinter; +class QTextCursor; class DecklistModelCardNode : public AbstractDecklistCardNode { private: @@ -23,6 +25,8 @@ class DeckListModel : public QAbstractItemModel { Q_OBJECT private slots: void rebuildTree(); +public slots: + void printDeckList(QPrinter *printer); public: DeckListModel(CardDatabase *_db, QObject *parent = 0); ~DeckListModel(); @@ -48,6 +52,8 @@ private: void emitRecursiveUpdates(const QModelIndex &index); void debugIndexInfo(const QString &func, const QModelIndex &index) const; void debugShowTree(InnerDecklistNode *node, int depth) const; + + void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node); template T getNode(const QModelIndex &index) const { diff --git a/cockatrice/src/window_deckeditor.cpp b/cockatrice/src/window_deckeditor.cpp index 7b075a98..60075733 100644 --- a/cockatrice/src/window_deckeditor.cpp +++ b/cockatrice/src/window_deckeditor.cpp @@ -94,6 +94,9 @@ WndDeckEditor::WndDeckEditor(CardDatabase *_db, QWidget *parent) aSaveDeckAs = new QAction(tr("&Save deck as..."), this); // aSaveDeckAs->setShortcuts(QKeySequence::SaveAs); connect(aSaveDeckAs, SIGNAL(triggered()), this, SLOT(actSaveDeckAs())); + aPrintDeck = new QAction(tr("&Print deck..."), this); + aPrintDeck->setShortcuts(QKeySequence::Print); + connect(aPrintDeck, SIGNAL(triggered()), this, SLOT(actPrintDeck())); aClose = new QAction(tr("&Close"), this); aClose->setShortcut(tr("Ctrl+Q")); connect(aClose, SIGNAL(triggered()), this, SLOT(close())); @@ -107,6 +110,8 @@ WndDeckEditor::WndDeckEditor(CardDatabase *_db, QWidget *parent) deckMenu->addAction(aSaveDeck); deckMenu->addAction(aSaveDeckAs); deckMenu->addSeparator(); + deckMenu->addAction(aPrintDeck); + deckMenu->addSeparator(); deckMenu->addAction(aClose); setsMenu = menuBar()->addMenu(tr("&Sets")); @@ -234,6 +239,13 @@ bool WndDeckEditor::actSaveDeckAs() return false; } +void WndDeckEditor::actPrintDeck() +{ + QPrintPreviewDialog *dlg = new QPrintPreviewDialog(this); + connect(dlg, SIGNAL(paintRequested(QPrinter *)), deckModel, SLOT(printDeckList(QPrinter *))); + dlg->exec(); +} + void WndDeckEditor::actEditSets() { WndSets *w = new WndSets(db, this); diff --git a/cockatrice/src/window_deckeditor.h b/cockatrice/src/window_deckeditor.h index 87ee6ba7..c5d67c76 100644 --- a/cockatrice/src/window_deckeditor.h +++ b/cockatrice/src/window_deckeditor.h @@ -24,6 +24,7 @@ private slots: void actLoadDeck(); bool actSaveDeck(); bool actSaveDeckAs(); + void actPrintDeck(); void actEditSets(); @@ -49,7 +50,7 @@ private: QLineEdit *searchEdit, *nameEdit, *commentsEdit; QMenu *deckMenu, *setsMenu; - QAction *aNewDeck, *aLoadDeck, *aSaveDeck, *aSaveDeckAs, *aClose; + QAction *aNewDeck, *aLoadDeck, *aSaveDeck, *aSaveDeckAs, *aPrintDeck, *aClose; QAction *aEditSets; QAction *aAddCard, *aAddCardToSideboard, *aRemoveCard, *aIncrement, *aDecrement; public: