src/platform/qt/PrinterView.cpp (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "PrinterView.h"
7
8#include "CoreController.h"
9#include "GBAApp.h"
10
11#include <QAction>
12#include <QClipboard>
13#include <QPainter>
14
15using namespace QGBA;
16
17PrinterView::PrinterView(std::shared_ptr<CoreController> controller, QWidget* parent)
18 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
19 , m_controller(controller)
20{
21 m_ui.setupUi(this);
22
23 connect(controller.get(), &CoreController::imagePrinted, this, &PrinterView::printImage);
24 connect(&m_timer, &QTimer::timeout, this, &PrinterView::printLine);
25 connect(m_ui.hurry, &QAbstractButton::clicked, this, &PrinterView::printAll);
26 connect(m_ui.tear, &QAbstractButton::clicked, this, &PrinterView::clear);
27 connect(m_ui.copyButton, &QAbstractButton::clicked, this, &PrinterView::copy);
28 connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &PrinterView::save);
29 m_timer.setInterval(80);
30
31 connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this](int mag) {
32 if (m_image.isNull()) {
33 return;
34 }
35 int oldMag = m_ui.image->size().width() / m_image.size().width();
36 m_ui.image->setPixmap(m_image.scaled(m_image.size() * mag));
37 m_ui.image->setFixedWidth(m_image.size().width() * mag);
38 m_ui.image->setFixedHeight(m_ui.image->size().height() / oldMag * mag);
39 });
40
41 QAction* save = new QAction(this);
42 save->setShortcut(QKeySequence::Save);
43 connect(save, &QAction::triggered, this, &PrinterView::save);
44 addAction(save);
45
46 QAction* copyAction = new QAction(this);
47 copyAction->setShortcut(QKeySequence::Copy);
48 connect(copyAction, &QAction::triggered, this, &PrinterView::copy);
49 addAction(copyAction);
50
51 clear();
52}
53
54PrinterView::~PrinterView() {
55 m_controller->detachPrinter();
56}
57
58void PrinterView::save() {
59 QString filename = GBAApp::app()->getSaveFileName(this, tr("Save Printout"), tr("Portable Network Graphics (*.png)"));
60 if (filename.isNull()) {
61 return;
62 }
63 m_image.save(filename);
64}
65
66void PrinterView::copy() {
67 GBAApp::app()->clipboard()->setImage(m_image.toImage());
68}
69
70void PrinterView::clear() {
71 m_ui.image->setFixedHeight(0);
72 m_image = QPixmap();
73 m_ui.image->clear();
74 m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
75 m_ui.copyButton->setEnabled(false);
76}
77
78void PrinterView::printImage(const QImage& image) {
79 QPixmap pixmap(image.width(), image.height() + m_image.height());
80 QPainter painter(&pixmap);
81 painter.drawPixmap(0, 0, m_image);
82 painter.drawImage(0, m_image.height(), image);
83 m_image = pixmap;
84 m_ui.image->setPixmap(m_image.scaled(m_image.size() * m_ui.magnification->value()));
85 m_timer.start();
86 m_ui.hurry->setEnabled(true);
87}
88
89void PrinterView::printLine() {
90 m_ui.image->setFixedHeight(m_ui.image->height() + m_ui.magnification->value());
91 m_ui.scrollArea->ensureVisible(0, m_ui.image->height(), 0, 0);
92 if (m_ui.image->height() >= m_image.height() * m_ui.magnification->value()) {
93 printAll();
94 }
95}
96
97void PrinterView::printAll() {
98 m_timer.stop();
99 m_ui.image->setFixedHeight(m_image.height() * m_ui.magnification->value());
100 m_controller->endPrint();
101 m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(true);
102 m_ui.copyButton->setEnabled(true);
103 m_ui.hurry->setEnabled(false);
104}