Qt: Add map export

This commit is contained in:
Vicki Pfau 2017-09-23 12:33:56 -07:00
parent 697c1cfa9d
commit 229d138dac
3 changed files with 49 additions and 9 deletions

View File

@ -7,6 +7,9 @@
#include "CoreController.h"
#include "GBAApp.h"
#include "LogController.h"
#include <mgba-util/png-io.h>
#include <QButtonGroup>
#include <QFontDatabase>
@ -59,6 +62,11 @@ MapView::MapView(std::shared_ptr<CoreController> controller, QWidget* parent)
});
group->addButton(button);
}
#ifdef USE_PNG
connect(m_ui.exportButton, &QAbstractButton::clicked, this, &MapView::exportMap);
#else
m_ui.exportButton->setVisible(false);
#endif
}
void MapView::selectMap(int map) {
@ -73,14 +81,13 @@ void MapView::selectMap(int map) {
}
void MapView::updateTilesGBA(bool force) {
QImage bg;
{
CoreController::Interrupter interrupter(m_controller);
mMapCache* mapCache = mMapCacheSetGetPointer(&m_cacheSet->maps, m_map);
int tilesW = 1 << mMapCacheSystemInfoGetTilesWide(mapCache->sysConfig);
int tilesH = 1 << mMapCacheSystemInfoGetTilesHigh(mapCache->sysConfig);
bg = QImage(QSize(tilesW * 8, tilesH * 8), QImage::Format_ARGB32);
uchar* bgBits = bg.bits();
m_rawMap = QImage(QSize(tilesW * 8, tilesH * 8), QImage::Format_ARGB32);
uchar* bgBits = m_rawMap.bits();
for (int j = 0; j < tilesH; ++j) {
for (int i = 0; i < tilesW; ++i) {
mMapCacheCleanTile(mapCache, &m_mapStatus[i + j * tilesW], i, j);
@ -90,8 +97,8 @@ void MapView::updateTilesGBA(bool force) {
}
}
}
bg = bg.convertToFormat(QImage::Format_RGB32).rgbSwapped();
QPixmap map = QPixmap::fromImage(bg);
m_rawMap = m_rawMap.rgbSwapped();
QPixmap map = QPixmap::fromImage(m_rawMap.convertToFormat(QImage::Format_RGB32));
if (m_ui.magnification->value() > 1) {
map = map.scaled(map.size() * m_ui.magnification->value());
}
@ -104,3 +111,23 @@ void MapView::updateTilesGB(bool force) {
}
#endif
#ifdef USE_PNG
void MapView::exportMap() {
QString filename = GBAApp::app()->getSaveFileName(this, tr("Export map"),
tr("Portable Network Graphics (*.png)"));
VFile* vf = VFileDevice::open(filename, O_WRONLY | O_CREAT | O_TRUNC);
if (!vf) {
LOG(QT, ERROR) << tr("Failed to open output PNG file: %1").arg(filename);
return;
}
CoreController::Interrupter interrupter(m_controller);
png_structp png = PNGWriteOpen(vf);
png_infop info = PNGWriteHeaderA(png, m_rawMap.width(), m_rawMap.height());
mMapCache* mapCache = mMapCacheSetGetPointer(&m_cacheSet->maps, m_map);
QImage map = m_rawMap.rgbSwapped();
PNGWritePixelsA(png, map.width(), map.height(), map.bytesPerLine() / 4, static_cast<const void*>(map.constBits()));
PNGWriteClose(png, info);
}
#endif

View File

@ -21,6 +21,11 @@ Q_OBJECT
public:
MapView(std::shared_ptr<CoreController> controller, QWidget* parent = nullptr);
#ifdef USE_PNG
public slots:
void exportMap();
#endif
private slots:
void selectMap(int);
@ -37,6 +42,7 @@ private:
std::shared_ptr<CoreController> m_controller;
mMapCacheEntry m_mapStatus[1024 * 1024] = {}; // TODO: Correct size
int m_map = 0;
QImage m_rawMap;
};
}

View File

@ -14,7 +14,10 @@
<string>Maps</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1" rowspan="4">
<item row="0" column="0">
<layout class="QVBoxLayout" name="bgLayout"/>
</item>
<item row="0" column="1" rowspan="5">
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
@ -71,7 +74,7 @@
<item row="2" column="0">
<widget class="QGBA::AssetTile" name="tile"/>
</item>
<item row="3" column="0">
<item row="4" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
@ -114,8 +117,12 @@
</item>
</layout>
</item>
<item row="0" column="0">
<layout class="QVBoxLayout" name="bgLayout"/>
<item row="3" column="0">
<widget class="QPushButton" name="exportButton">
<property name="text">
<string>Export</string>
</property>
</widget>
</item>
</layout>
</widget>