From 8b94eabd9d6a3bea7df9e857e4efb56567469b9d Mon Sep 17 00:00:00 2001 From: mgr Date: Sun, 30 Sep 2018 01:01:56 +0200 Subject: [PATCH] Printing support #1525: shortcuts for the hex and text editors Print shortcuts are added in the context of the text and hex editors of the Edit Database Cell dock. --- src/EditDialog.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/EditDialog.h | 1 + 2 files changed, 39 insertions(+) diff --git a/src/EditDialog.cpp b/src/EditDialog.cpp index 4fc8ab08..8514f960 100644 --- a/src/EditDialog.cpp +++ b/src/EditDialog.cpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include EditDialog::EditDialog(QWidget* parent) : QDialog(parent), @@ -48,6 +50,12 @@ EditDialog::EditDialog(QWidget* parent) connect(sciEdit, SIGNAL(textChanged()), this, SLOT(updateApplyButton())); connect(sciEdit, SIGNAL(textChanged()), this, SLOT(editTextChanged())); + // Create shortcuts for the widgets that doesn't have its own printing mechanism. + QShortcut* shortcutPrintHex = new QShortcut(QKeySequence::Print, hexEdit, nullptr, nullptr, Qt::WidgetShortcut); + connect(shortcutPrintHex, &QShortcut::activated, this, &EditDialog::openPrintDialog); + QShortcut* shortcutPrintText = new QShortcut(QKeySequence::Print, ui->editorText, nullptr, nullptr, Qt::WidgetShortcut); + connect(shortcutPrintText, &QShortcut::activated, this, &EditDialog::openPrintDialog); + mustIndentAndCompact = Settings::getValue("databrowser", "indent_compact").toBool(); ui->buttonIndent->setChecked(mustIndentAndCompact); @@ -1022,3 +1030,33 @@ void EditDialog::setStackCurrentIndex(int editMode) break; } } + +void EditDialog::openPrintDialog() +{ + QPrinter printer; + QPrintPreviewDialog *dialog = new QPrintPreviewDialog(&printer); + + QTextDocument *document = new QTextDocument(); + switch (dataSource) { + case TextBuffer: + document->setPlainText(ui->editorText->toPlainText()); + break; + case SciBuffer: + // This case isn't really expected because the Scintilla widget has it's own printing slot + document->setPlainText(sciEdit->text()); + break; + case HexBuffer: + document->setPlainText(hexEdit->toReadableString()); + document->setDefaultFont(hexEdit->font()); + break; + } + + connect(dialog, &QPrintPreviewDialog::paintRequested, [&](QPrinter *previewPrinter) { + document->print(previewPrinter); + }); + + dialog->exec(); + + delete dialog; + delete document; +} diff --git a/src/EditDialog.h b/src/EditDialog.h index 6a02067f..64b34d42 100644 --- a/src/EditDialog.h +++ b/src/EditDialog.h @@ -89,6 +89,7 @@ private: bool promptInvalidData(const QString& dataType, const QString& errorString); void setDataInBuffer(const QByteArray& data, DataSources source); void setStackCurrentIndex(int editMode); + void openPrintDialog(); }; #endif