Skip to content

Commit

Permalink
Printing support #1525: plot printing using preview dialog
Browse files Browse the repository at this point in the history
Add shortcut and entry in context menu for printing a plot.
  • Loading branch information
mgrojo committed Sep 29, 2018
1 parent e771662 commit 79983e2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/PlotDock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "FileDialog.h"
#include "MainWindow.h" // Just for BrowseDataTableSettings, not for the actual main window class

#include <QPrinter>
#include <QPrintPreviewDialog>

PlotDock::PlotDock(QWidget* parent)
: QDialog(parent),
ui(new Ui::PlotDock),
Expand Down Expand Up @@ -40,18 +43,28 @@ PlotDock::PlotDock(QWidget* parent)
QShortcut* shortcutCopy = new QShortcut(QKeySequence::Copy, ui->plotWidget, nullptr, nullptr, Qt::WidgetShortcut);
connect(shortcutCopy, SIGNAL(activated()), this, SLOT(copy()));

QShortcut* shortcutPrint = new QShortcut(QKeySequence::Print, ui->plotWidget, nullptr, nullptr, Qt::WidgetShortcut);
connect(shortcutPrint, &QShortcut::activated, this, &PlotDock::openPrintDialog);

ui->plotWidget->setContextMenuPolicy(Qt::CustomContextMenu);

// Set up context menu
m_contextMenu = new QMenu(this);

QAction* copyAction = new QAction(QIcon(":/icons/copy"), tr("Copy"), m_contextMenu);
copyAction->setShortcut(shortcutCopy->key());
m_contextMenu->addAction(copyAction);

connect(copyAction, &QAction::triggered, [&]() {
copy();
});

QAction* printAction = new QAction(QIcon(":/icons/print"), tr("Print..."), m_contextMenu);
printAction->setShortcut(shortcutPrint->key());
m_contextMenu->addAction(printAction);
connect(printAction, &QAction::triggered, [&]() {
openPrintDialog();
});

QAction* showLegendAction = new QAction(tr("Show legend"), m_contextMenu);
showLegendAction->setCheckable(true);
m_contextMenu->addAction(showLegendAction);
Expand Down Expand Up @@ -851,3 +864,27 @@ void PlotDock::reject()
// dock go away
return;
}

void PlotDock::openPrintDialog()
{
QPrinter printer;
QPrintPreviewDialog previewDialog(&printer, this);
connect(&previewDialog, &QPrintPreviewDialog::paintRequested, this, &PlotDock::renderPlot);
previewDialog.exec();
}

void PlotDock::renderPlot(QPrinter* printer)
{
QCPPainter painter(printer);
QRectF pageRect = printer->pageRect(QPrinter::DevicePixel);

int plotWidth = ui->plotWidget->viewport().width();
int plotHeight = ui->plotWidget->viewport().height();
double scale = pageRect.width()/(double)plotWidth;

painter.setMode(QCPPainter::pmVectorized);
painter.setMode(QCPPainter::pmNoCaching);

painter.scale(scale, scale);
ui->plotWidget->toPainter(&painter, plotWidth, plotHeight);
}
3 changes: 3 additions & 0 deletions src/PlotDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class SqliteTableModel;
class QTreeWidgetItem;
class QPrinter;
struct BrowseDataTableSettings;

namespace Ui {
Expand Down Expand Up @@ -110,6 +111,8 @@ private slots:
void copy();
void toggleLegendVisible(bool visible);
void toggleStackedBars(bool stacked);
void openPrintDialog();
void renderPlot(QPrinter* printer);
};

#endif

0 comments on commit 79983e2

Please sign in to comment.