Skip to content

Commit

Permalink
Printing support #1525: shortcuts for the hex and text editors
Browse files Browse the repository at this point in the history
Print shortcuts are added in the context of the text and hex editors of
the Edit Database Cell dock.
  • Loading branch information
mgrojo committed Sep 29, 2018
1 parent 79983e2 commit 8b94eab
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/EditDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <QJsonDocument>
#include <QtXml/QDomDocument>
#include <QMessageBox>
#include <QPrinter>
#include <QPrintPreviewDialog>

EditDialog::EditDialog(QWidget* parent)
: QDialog(parent),
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/EditDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private slots:
bool promptInvalidData(const QString& dataType, const QString& errorString);
void setDataInBuffer(const QByteArray& data, DataSources source);
void setStackCurrentIndex(int editMode);
void openPrintDialog();
};

#endif

0 comments on commit 8b94eab

Please sign in to comment.