Printing support: print dialog from QScintilla widgets

Add printing support for QScintilla widgets (SQL, JSON and XML). It can be
access through the contextual menu, shortcut (Ctrl+P) or (in the case of
the "Execute SQL" tab) from a button in the toolbar.

Ctrl+P was previously assigned to Plot Dock since
63c338c359 but, as it was foreseen in that
commit, it should be assign to print is ever supported. This change must
be mentioned in release notes.

First part of printing support. See issue #1525.
This commit is contained in:
mgr
2018-09-16 16:06:27 +02:00
parent 9daf2657fc
commit 60c229c1c4
7 changed files with 71 additions and 7 deletions

View File

@@ -1,7 +1,9 @@
#include "ExtendedScintilla.h"
#include "FindReplaceDialog.h"
#include "Settings.h"
#include "Qsci/qscilexer.h"
#include "Qsci/qsciprinter.h"
#include <QFile>
#include <QDropEvent>
@@ -11,6 +13,7 @@
#include <QAction>
#include <QMenu>
#include <QPalette>
#include <QPrintDialog>
#include <cmath>
@@ -51,10 +54,13 @@ ExtendedScintilla::ExtendedScintilla(QWidget* parent) :
// Connect signals
connect(this, SIGNAL(linesChanged()), this, SLOT(updateLineNumberAreaWidth()));
// The shortcut is constrained to the Widget context so it does not conflict with other SqlTextEdit widgets in the Main Window.
// The shortcuts are constrained to the Widget context so they do not conflict with other SqlTextEdit widgets in the Main Window.
QShortcut* shortcutFindReplace = new QShortcut(QKeySequence(tr("Ctrl+H")), this, nullptr, nullptr, Qt::WidgetShortcut);
connect(shortcutFindReplace, SIGNAL(activated()), this, SLOT(openFindReplaceDialog()));
QShortcut* shortcutPrint = new QShortcut(QKeySequence(tr("Ctrl+P")), this, nullptr, nullptr, Qt::WidgetShortcut);
connect(shortcutPrint, &QShortcut::activated, this, &ExtendedScintilla::openPrintDialog);
// Prepare for adding the find/replace option to the QScintilla context menu
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint &)));
@@ -223,14 +229,28 @@ void ExtendedScintilla::showContextMenu(const QPoint &pos)
QAction* findReplaceAction = new QAction(QIcon(":/icons/text_replace"), tr("Find and Replace..."), this);
findReplaceAction->setShortcut(QKeySequence(tr("Ctrl+H")));
connect(findReplaceAction, &QAction::triggered, [&]() {
openFindReplaceDialog();
});
connect(findReplaceAction, &QAction::triggered, this, &ExtendedScintilla::openFindReplaceDialog);
QAction* printAction = new QAction(QIcon(":/icons/print"), tr("Print..."), this);
printAction->setShortcut(QKeySequence(tr("Ctrl+P")));
connect(printAction, &QAction::triggered, this, &ExtendedScintilla::openPrintDialog);
// This has to be created here, otherwise the set of enabled options would not update accordingly.
QMenu* editContextMenu = createStandardContextMenu();
editContextMenu->addSeparator();
editContextMenu->addAction(findReplaceAction);
editContextMenu->addAction(printAction);
editContextMenu->exec(mapToGlobal(pos));
}
void ExtendedScintilla::openPrintDialog()
{
QsciPrinter* printer = new QsciPrinter;
QPrintDialog printDialog(printer, this);
if (printDialog.exec() == QDialog::Accepted)
printer->printRange(this);
delete printer;
}