From 164c40234bf71cd45ffcd71be489eb10f7dcf378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Sun, 9 Nov 2025 03:00:30 +0100 Subject: [PATCH] Deck Loader is responsible for printing. Took 8 minutes Took 2 seconds --- .../src/interface/deck_loader/deck_loader.cpp | 95 ++++++++++++++++ .../src/interface/deck_loader/deck_loader.h | 12 +++ .../widgets/tabs/abstract_tab_deck_editor.cpp | 2 +- .../models/deck_list/deck_list_model.cpp | 102 +----------------- .../models/deck_list/deck_list_model.h | 7 -- 5 files changed, 109 insertions(+), 109 deletions(-) diff --git a/cockatrice/src/interface/deck_loader/deck_loader.cpp b/cockatrice/src/interface/deck_loader/deck_loader.cpp index 95f4d9224..966f1c214 100644 --- a/cockatrice/src/interface/deck_loader/deck_loader.cpp +++ b/cockatrice/src/interface/deck_loader/deck_loader.cpp @@ -14,6 +14,7 @@ #include #include #include +#include const QStringList DeckLoader::ACCEPTED_FILE_EXTENSIONS = {"*.cod", "*.dec", "*.dek", "*.txt", "*.mwDeck"}; @@ -603,3 +604,97 @@ QString DeckLoader::getCompleteCardName(const QString &cardName) const return cardName; } + +void DeckLoader::printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node) +{ + const int totalColumns = 2; + + if (node->height() == 1) { + QTextBlockFormat blockFormat; + QTextCharFormat charFormat; + charFormat.setFontPointSize(11); + charFormat.setFontWeight(QFont::Bold); + cursor->insertBlock(blockFormat, charFormat); + + QTextTableFormat tableFormat; + tableFormat.setCellPadding(0); + tableFormat.setCellSpacing(0); + tableFormat.setBorder(0); + QTextTable *table = cursor->insertTable(node->size() + 1, totalColumns, tableFormat); + for (int i = 0; i < node->size(); i++) { + auto *card = dynamic_cast(node->at(i)); + + QTextCharFormat cellCharFormat; + cellCharFormat.setFontPointSize(9); + + QTextTableCell cell = table->cellAt(i, 0); + cell.setFormat(cellCharFormat); + QTextCursor cellCursor = cell.firstCursorPosition(); + cellCursor.insertText(QString("%1 ").arg(card->getNumber())); + + cell = table->cellAt(i, 1); + cell.setFormat(cellCharFormat); + cellCursor = cell.firstCursorPosition(); + cellCursor.insertText(card->getName()); + } + } else if (node->height() == 2) { + QTextBlockFormat blockFormat; + QTextCharFormat charFormat; + charFormat.setFontPointSize(14); + charFormat.setFontWeight(QFont::Bold); + + cursor->insertBlock(blockFormat, charFormat); + + QTextTableFormat tableFormat; + tableFormat.setCellPadding(10); + tableFormat.setCellSpacing(0); + tableFormat.setBorder(0); + QVector constraints; + for (int i = 0; i < totalColumns; i++) { + constraints << QTextLength(QTextLength::PercentageLength, 100.0 / totalColumns); + } + tableFormat.setColumnWidthConstraints(constraints); + + QTextTable *table = cursor->insertTable(1, totalColumns, tableFormat); + for (int i = 0; i < node->size(); i++) { + QTextCursor cellCursor = table->cellAt(0, (i * totalColumns) / node->size()).lastCursorPosition(); + printDeckListNode(&cellCursor, dynamic_cast(node->at(i))); + } + } + + cursor->movePosition(QTextCursor::End); +} + +void DeckLoader::printDeckList(QPrinter *printer) +{ + QTextDocument doc; + + QFont font("Serif"); + font.setStyleHint(QFont::Serif); + doc.setDefaultFont(font); + + QTextCursor cursor(&doc); + + QTextBlockFormat headerBlockFormat; + QTextCharFormat headerCharFormat; + headerCharFormat.setFontPointSize(16); + headerCharFormat.setFontWeight(QFont::Bold); + + cursor.insertBlock(headerBlockFormat, headerCharFormat); + cursor.insertText(getName()); + + headerCharFormat.setFontPointSize(12); + cursor.insertBlock(headerBlockFormat, headerCharFormat); + cursor.insertText(getComments()); + cursor.insertBlock(headerBlockFormat, headerCharFormat); + + for (int i = 0; i < getRoot()->size(); i++) { + cursor.insertHtml("
"); + // cursor.insertHtml("
"); + cursor.insertBlock(headerBlockFormat, headerCharFormat); + + printDeckListNode(&cursor, dynamic_cast(getRoot()->at(i))); + } + + doc.print(printer); +} diff --git a/cockatrice/src/interface/deck_loader/deck_loader.h b/cockatrice/src/interface/deck_loader/deck_loader.h index f11ded444..a7af3bad9 100644 --- a/cockatrice/src/interface/deck_loader/deck_loader.h +++ b/cockatrice/src/interface/deck_loader/deck_loader.h @@ -8,6 +8,8 @@ #define DECK_LOADER_H #include +#include +#include #include inline Q_LOGGING_CATEGORY(DeckLoaderLog, "deck_loader") @@ -19,6 +21,13 @@ signals: void deckLoaded(); void loadFinished(bool success); +public slots: + /** + * @brief Prints the decklist to the provided QPrinter. + * @param printer The printer to render the decklist to. + */ + void printDeckList(QPrinter *printer); + public: enum FileFormat { @@ -93,6 +102,9 @@ public: bool saveToStream_Plain(QTextStream &out, bool addComments = true, bool addSetNameAndNumber = true) const; bool convertToCockatriceFormat(QString fileName); +private: + void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node); + protected: void saveToStream_DeckHeader(QTextStream &out) const; void saveToStream_DeckZone(QTextStream &out, diff --git a/cockatrice/src/interface/widgets/tabs/abstract_tab_deck_editor.cpp b/cockatrice/src/interface/widgets/tabs/abstract_tab_deck_editor.cpp index 9e3d71f7c..820847a7b 100644 --- a/cockatrice/src/interface/widgets/tabs/abstract_tab_deck_editor.cpp +++ b/cockatrice/src/interface/widgets/tabs/abstract_tab_deck_editor.cpp @@ -534,7 +534,7 @@ void AbstractTabDeckEditor::actSaveDeckToClipboardRawNoSetInfo() void AbstractTabDeckEditor::actPrintDeck() { auto *dlg = new QPrintPreviewDialog(this); - connect(dlg, &QPrintPreviewDialog::paintRequested, deckDockWidget->deckModel, &DeckListModel::printDeckList); + connect(dlg, &QPrintPreviewDialog::paintRequested, deckDockWidget->getDeckLoader(), &DeckLoader::printDeckList); dlg->exec(); } diff --git a/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.cpp b/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.cpp index cb5ab23d8..42219e759 100644 --- a/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.cpp +++ b/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.cpp @@ -2,12 +2,6 @@ #include #include -#include -#include -#include -#include -#include -#include #include DeckListModel::DeckListModel(QObject *parent) @@ -622,98 +616,4 @@ QList *DeckListModel::getZones() const zones->append(currentZone->getName()); } return zones; -} - -void DeckListModel::printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node) -{ - const int totalColumns = 2; - - if (node->height() == 1) { - QTextBlockFormat blockFormat; - QTextCharFormat charFormat; - charFormat.setFontPointSize(11); - charFormat.setFontWeight(QFont::Bold); - cursor->insertBlock(blockFormat, charFormat); - - QTextTableFormat tableFormat; - tableFormat.setCellPadding(0); - tableFormat.setCellSpacing(0); - tableFormat.setBorder(0); - QTextTable *table = cursor->insertTable(node->size() + 1, totalColumns, tableFormat); - for (int i = 0; i < node->size(); i++) { - auto *card = dynamic_cast(node->at(i)); - - QTextCharFormat cellCharFormat; - cellCharFormat.setFontPointSize(9); - - QTextTableCell cell = table->cellAt(i, 0); - cell.setFormat(cellCharFormat); - QTextCursor cellCursor = cell.firstCursorPosition(); - cellCursor.insertText(QString("%1 ").arg(card->getNumber())); - - cell = table->cellAt(i, 1); - cell.setFormat(cellCharFormat); - cellCursor = cell.firstCursorPosition(); - cellCursor.insertText(card->getName()); - } - } else if (node->height() == 2) { - QTextBlockFormat blockFormat; - QTextCharFormat charFormat; - charFormat.setFontPointSize(14); - charFormat.setFontWeight(QFont::Bold); - - cursor->insertBlock(blockFormat, charFormat); - - QTextTableFormat tableFormat; - tableFormat.setCellPadding(10); - tableFormat.setCellSpacing(0); - tableFormat.setBorder(0); - QVector constraints; - for (int i = 0; i < totalColumns; i++) { - constraints << QTextLength(QTextLength::PercentageLength, 100.0 / totalColumns); - } - tableFormat.setColumnWidthConstraints(constraints); - - QTextTable *table = cursor->insertTable(1, totalColumns, tableFormat); - for (int i = 0; i < node->size(); i++) { - QTextCursor cellCursor = table->cellAt(0, (i * totalColumns) / node->size()).lastCursorPosition(); - printDeckListNode(&cellCursor, dynamic_cast(node->at(i))); - } - } - - cursor->movePosition(QTextCursor::End); -} - -void DeckListModel::printDeckList(QPrinter *printer) -{ - QTextDocument doc; - - QFont font("Serif"); - font.setStyleHint(QFont::Serif); - doc.setDefaultFont(font); - - QTextCursor cursor(&doc); - - QTextBlockFormat headerBlockFormat; - QTextCharFormat headerCharFormat; - headerCharFormat.setFontPointSize(16); - headerCharFormat.setFontWeight(QFont::Bold); - - cursor.insertBlock(headerBlockFormat, headerCharFormat); - cursor.insertText(deckList->getName()); - - headerCharFormat.setFontPointSize(12); - cursor.insertBlock(headerBlockFormat, headerCharFormat); - cursor.insertText(deckList->getComments()); - cursor.insertBlock(headerBlockFormat, headerCharFormat); - - for (int i = 0; i < root->size(); i++) { - cursor.insertHtml("
"); - // cursor.insertHtml("
"); - cursor.insertBlock(headerBlockFormat, headerCharFormat); - - printDeckListNode(&cursor, dynamic_cast(root->at(i))); - } - - doc.print(printer); -} +} \ No newline at end of file diff --git a/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.h b/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.h index 07d9db18f..d4c15d5b8 100644 --- a/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.h +++ b/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.h @@ -132,11 +132,6 @@ public slots: * state of the deck. */ void rebuildTree(); - /** - * @brief Prints the decklist to the provided QPrinter. - * @param printer The printer to render the decklist to. - */ - void printDeckList(QPrinter *printer); signals: /** @@ -252,8 +247,6 @@ private: void emitRecursiveUpdates(const QModelIndex &index); void sortHelper(InnerDecklistNode *node, Qt::SortOrder order); - void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node); - template T getNode(const QModelIndex &index) const { if (!index.isValid())