all repos — mgba @ ca37c1ecbf7f6b11a4bc48a8e55154ca3c2ac785

mGBA Game Boy Advance Emulator

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 <QPainter>
13
14using namespace QGBA;
15
16PrinterView::PrinterView(std::shared_ptr<CoreController> controller, QWidget* parent)
17	: QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
18	, m_controller(controller)
19{
20	m_ui.setupUi(this);
21
22	connect(controller.get(), &CoreController::imagePrinted, this, &PrinterView::printImage);
23	connect(&m_timer, &QTimer::timeout, this, &PrinterView::printLine);
24	connect(m_ui.hurry, &QAbstractButton::clicked, this, &PrinterView::printAll);
25	connect(m_ui.tear, &QAbstractButton::clicked, this, &PrinterView::clear);
26	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &PrinterView::save);
27	m_timer.setInterval(80);
28
29	connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this](int mag) {
30		if (m_image.isNull()) {
31			return;
32		}
33		int oldMag = m_ui.image->size().width() / m_image.size().width();
34		m_ui.image->setPixmap(m_image.scaled(m_image.size() * mag));
35		m_ui.image->setFixedWidth(m_image.size().width() * mag);
36		m_ui.image->setFixedHeight(m_ui.image->size().height() / oldMag * mag);
37	});
38
39	QAction* save = new QAction(this);
40	save->setShortcut(QKeySequence::Save);
41	connect(save, &QAction::triggered, this, &PrinterView::save);
42	addAction(save);
43
44	clear();
45}
46
47PrinterView::~PrinterView() {
48	m_controller->detachPrinter();
49}
50
51void PrinterView::save() {
52	QString filename = GBAApp::app()->getSaveFileName(this, tr("Save Printout"), tr("Portable Network Graphics (*.png)"));
53	if (filename.isNull()) {
54		return;
55	}
56	m_image.save(filename);
57}
58
59void PrinterView::clear() {
60	m_ui.image->setFixedHeight(0);
61	m_image = QPixmap();
62	m_ui.image->clear();
63	m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
64}
65
66void PrinterView::printImage(const QImage& image) {
67	QPixmap pixmap(image.width(), image.height() + m_image.height());
68	QPainter painter(&pixmap);
69	painter.drawPixmap(0, 0, m_image);
70	painter.drawImage(0, m_image.height(), image);
71	m_image = pixmap;
72	m_ui.image->setPixmap(m_image.scaled(m_image.size() * m_ui.magnification->value()));
73	m_timer.start();
74	m_ui.hurry->setEnabled(true);
75}
76
77void PrinterView::printLine() {
78	m_ui.image->setFixedHeight(m_ui.image->height() + m_ui.magnification->value());
79	m_ui.scrollArea->ensureVisible(0, m_ui.image->height(), 0, 0);
80	if (m_ui.image->height() >= m_image.height() * m_ui.magnification->value()) {
81		printAll();
82	}
83}
84
85void PrinterView::printAll() {
86	m_timer.stop();
87	m_ui.image->setFixedHeight(m_image.height() * m_ui.magnification->value());
88	m_controller->endPrint();
89	m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(true);
90	m_ui.hurry->setEnabled(false);
91}