Skip to content

Commit 60c229c

Browse files
committed
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 63c338c 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.
1 parent 9daf265 commit 60c229c

7 files changed

Lines changed: 71 additions & 7 deletions

File tree

src/ExtendedScintilla.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "ExtendedScintilla.h"
22
#include "FindReplaceDialog.h"
33
#include "Settings.h"
4+
45
#include "Qsci/qscilexer.h"
6+
#include "Qsci/qsciprinter.h"
57

68
#include <QFile>
79
#include <QDropEvent>
@@ -11,6 +13,7 @@
1113
#include <QAction>
1214
#include <QMenu>
1315
#include <QPalette>
16+
#include <QPrintDialog>
1417
#include <cmath>
1518

1619

@@ -51,10 +54,13 @@ ExtendedScintilla::ExtendedScintilla(QWidget* parent) :
5154
// Connect signals
5255
connect(this, SIGNAL(linesChanged()), this, SLOT(updateLineNumberAreaWidth()));
5356

54-
// The shortcut is constrained to the Widget context so it does not conflict with other SqlTextEdit widgets in the Main Window.
57+
// The shortcuts are constrained to the Widget context so they do not conflict with other SqlTextEdit widgets in the Main Window.
5558
QShortcut* shortcutFindReplace = new QShortcut(QKeySequence(tr("Ctrl+H")), this, nullptr, nullptr, Qt::WidgetShortcut);
5659
connect(shortcutFindReplace, SIGNAL(activated()), this, SLOT(openFindReplaceDialog()));
5760

61+
QShortcut* shortcutPrint = new QShortcut(QKeySequence(tr("Ctrl+P")), this, nullptr, nullptr, Qt::WidgetShortcut);
62+
connect(shortcutPrint, &QShortcut::activated, this, &ExtendedScintilla::openPrintDialog);
63+
5864
// Prepare for adding the find/replace option to the QScintilla context menu
5965
setContextMenuPolicy(Qt::CustomContextMenu);
6066
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint &)));
@@ -223,14 +229,28 @@ void ExtendedScintilla::showContextMenu(const QPoint &pos)
223229

224230
QAction* findReplaceAction = new QAction(QIcon(":/icons/text_replace"), tr("Find and Replace..."), this);
225231
findReplaceAction->setShortcut(QKeySequence(tr("Ctrl+H")));
226-
connect(findReplaceAction, &QAction::triggered, [&]() {
227-
openFindReplaceDialog();
228-
});
232+
connect(findReplaceAction, &QAction::triggered, this, &ExtendedScintilla::openFindReplaceDialog);
233+
234+
QAction* printAction = new QAction(QIcon(":/icons/print"), tr("Print..."), this);
235+
printAction->setShortcut(QKeySequence(tr("Ctrl+P")));
236+
connect(printAction, &QAction::triggered, this, &ExtendedScintilla::openPrintDialog);
229237

230238
// This has to be created here, otherwise the set of enabled options would not update accordingly.
231239
QMenu* editContextMenu = createStandardContextMenu();
232240
editContextMenu->addSeparator();
233241
editContextMenu->addAction(findReplaceAction);
242+
editContextMenu->addAction(printAction);
234243

235244
editContextMenu->exec(mapToGlobal(pos));
236245
}
246+
247+
void ExtendedScintilla::openPrintDialog()
248+
{
249+
QsciPrinter* printer = new QsciPrinter;
250+
251+
QPrintDialog printDialog(printer, this);
252+
if (printDialog.exec() == QDialog::Accepted)
253+
printer->printRange(this);
254+
255+
delete printer;
256+
}

src/ExtendedScintilla.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public slots:
3030
// Set error indicator from position to end of line
3131
void setErrorIndicator(int position);
3232
void openFindReplaceDialog();
33+
void openPrintDialog();
3334

3435
protected:
3536
void dropEvent(QDropEvent* e);

src/MainWindow.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,7 @@ void MainWindow::init()
253253

254254
// Add menu item for plot dock
255255
ui->viewMenu->insertAction(ui->viewDBToolbarAction, ui->dockPlot->toggleViewAction());
256-
QList<QKeySequence> plotkeyseqlist;
257-
plotkeyseqlist << QKeySequence(tr("Ctrl+P")) << QKeySequence(tr("Ctrl+D"));
258-
ui->viewMenu->actions().at(1)->setShortcuts(plotkeyseqlist);
256+
ui->viewMenu->actions().at(1)->setShortcut(QKeySequence(tr("Ctrl+D")));
259257
ui->viewMenu->actions().at(1)->setIcon(QIcon(":/icons/log_dock"));
260258

261259
// Add menu item for schema dock
@@ -3276,6 +3274,15 @@ void MainWindow::openFindReplaceDialog()
32763274
sqlWidget->getEditor()->openFindReplaceDialog();
32773275
}
32783276

3277+
void MainWindow::openSqlPrintDialog()
3278+
{
3279+
// The slot for the shortcut must discover which sqltexedit widget has the focus and then open its dialog.
3280+
SqlExecutionArea* sqlWidget = qobject_cast<SqlExecutionArea*>(ui->tabSqlAreas->currentWidget());
3281+
3282+
if (sqlWidget)
3283+
sqlWidget->getEditor()->openPrintDialog();
3284+
}
3285+
32793286
void MainWindow::saveAsView(QString query)
32803287
{
32813288
// Let the user select a name for the new view and make sure it doesn't already exist

src/MainWindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ private slots:
295295
void renameSqlTab(int index);
296296
void setFindFrameVisibility(bool show);
297297
void openFindReplaceDialog();
298+
void openSqlPrintDialog();
298299
void saveFilterAsView();
299300
void exportFilteredTable();
300301
void updateInsertDeleteRecordButton();

src/MainWindow.ui

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ You can drag SQL statements from an object row and drop them into other applicat
915915
<addaction name="actionSqlOpenTab"/>
916916
<addaction name="actionSqlOpenFile"/>
917917
<addaction name="actionSqlSaveFilePopup"/>
918+
<addaction name="actionSqlPrint"/>
918919
<addaction name="separator"/>
919920
<addaction name="actionExecuteSql"/>
920921
<addaction name="actionSqlExecuteLine"/>
@@ -2250,6 +2251,27 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
22502251
<string>Use escaped identifiers (e.g. &quot;Table1&quot;) when dragging the objects and dropping them into the editor </string>
22512252
</property>
22522253
</action>
2254+
<action name="actionSqlPrint">
2255+
<property name="icon">
2256+
<iconset resource="icons/icons.qrc">
2257+
<normaloff>:/icons/print</normaloff>:/icons/print</iconset>
2258+
</property>
2259+
<property name="text">
2260+
<string>Print</string>
2261+
</property>
2262+
<property name="toolTip">
2263+
<string>Print text from current SQL editor tab [Ctrl+P]</string>
2264+
</property>
2265+
<property name="statusTip">
2266+
<string/>
2267+
</property>
2268+
<property name="whatsThis">
2269+
<string>Open a dialog for printing the text in the current SQL editor tab</string>
2270+
</property>
2271+
<property name="shortcutContext">
2272+
<enum>Qt::WidgetShortcut</enum>
2273+
</property>
2274+
</action>
22532275
</widget>
22542276
<customwidgets>
22552277
<customwidget>
@@ -3613,6 +3635,18 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
36133635
<x>518</x>
36143636
<y>314</y>
36153637
</hint>
3638+
</hints>
3639+
</connection>
3640+
<connection>
3641+
<sender>actionSqlPrint</sender>
3642+
<signal>triggered()</signal>
3643+
<receiver>MainWindow</receiver>
3644+
<slot>openSqlPrintDialog()</slot>
3645+
<hints>
3646+
<hint type="sourcelabel">
3647+
<x>-1</x>
3648+
<y>-1</y>
3649+
</hint>
36163650
<hint type="destinationlabel">
36173651
<x>518</x>
36183652
<y>314</y>

src/icons/icons.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@
6666
<file alias="log_dock">application_side_list.png</file>
6767
<file alias="db_attach">database_link.png</file>
6868
<file alias="text_indent">text_indent.png</file>
69+
<file alias="print">printer.png</file>
6970
</qresource>
7071
</RCC>

src/icons/printer.png

731 Bytes
Loading

0 commit comments

Comments
 (0)