all repos — mgba @ 3a6756b3991dab1de69cafde92f196c0adddac44

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 <QPainter>
12
13using namespace QGBA;
14
15PrinterView::PrinterView(std::shared_ptr<CoreController> controller, QWidget* parent)
16	: QDialog(parent)
17	, m_controller(controller)
18{
19	m_ui.setupUi(this);
20
21	connect(controller.get(), &CoreController::imagePrinted, this, &PrinterView::printImage);
22	connect(&m_timer, &QTimer::timeout, this, &PrinterView::printLine);
23	connect(m_ui.hurry, &QAbstractButton::clicked, this, &PrinterView::printAll);
24	connect(m_ui.tear, &QAbstractButton::clicked, this, &PrinterView::clear);
25	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &PrinterView::save);
26	m_timer.setInterval(80);
27	clear();
28}
29
30PrinterView::~PrinterView() {
31	m_controller->detachPrinter();
32}
33
34void PrinterView::save() {
35	QString filename = GBAApp::app()->getSaveFileName(this, tr("Save Printout"), tr("Portable Network Graphics (*.png)"));
36	if (filename.isNull()) {
37		return;
38	}
39	m_image.save(filename);
40}
41
42void PrinterView::clear() {
43	m_ui.image->setFixedHeight(0);
44	m_image = QPixmap();
45	m_ui.image->clear();
46	m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
47}
48
49void PrinterView::printImage(const QImage& image) {
50	QPixmap pixmap(image.width(), image.height() + m_image.height());
51	QPainter painter(&pixmap);
52	painter.drawPixmap(0, 0, m_image);
53	painter.drawImage(0, m_image.height(), image);
54	m_image = pixmap;
55	m_ui.image->setPixmap(m_image);
56	m_timer.start();
57	m_ui.hurry->setEnabled(true);
58}
59
60void PrinterView::printLine() {
61	m_ui.image->setFixedHeight(m_ui.image->height() + 1);
62	m_ui.scrollArea->ensureVisible(0, m_ui.image->height(), 0, 0);
63	if (m_ui.image->height() >= m_image.height()) {
64		printAll();
65	}
66}
67
68void PrinterView::printAll() {
69	m_timer.stop();
70	m_ui.image->setFixedHeight(m_image.height());
71	m_controller->endPrint();
72	m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(true);
73	m_ui.hurry->setEnabled(false);
74}