create map image export window

This commit is contained in:
garak 2019-05-20 22:08:04 -04:00 committed by huderlem
parent ca438fdc50
commit f750c56e29
6 changed files with 602 additions and 23 deletions

322
forms/mapimageexporter.ui Normal file
View file

@ -0,0 +1,322 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MapImageExporter</class>
<widget class="QDialog" name="MapImageExporter">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>696</width>
<height>396</height>
</rect>
</property>
<property name="windowTitle">
<string>Export Map Image</string>
</property>
<property name="sizeGripEnabled">
<bool>true</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Preview</string>
</property>
<layout class="QGridLayout" name="gridLayout_6">
<item row="0" column="0">
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>337</width>
<height>334</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="4">
<widget class="QGraphicsView" name="graphicsView_Preview">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="mouseTracking">
<bool>false</bool>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustIgnored</enum>
</property>
<property name="dragMode">
<enum>QGraphicsView::NoDrag</enum>
</property>
</widget>
</item>
<item row="4" column="4">
<spacer name="verticalSpacer_South">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<height>100</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="4">
<spacer name="verticalSpacer_North">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<height>100</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="5">
<spacer name="horizontalSpacer_West">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>100</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="0">
<spacer name="horizontalSpacer_East">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>100</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_Options">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Map</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboBox_MapSelection">
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
</property>
</widget>
</item>
<item row="0" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBox_Events">
<property name="title">
<string>Events</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_Event_Options">
<item row="0" column="1">
<widget class="QCheckBox" name="checkBox_Warps">
<property name="text">
<string>Warps</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="checkBox_Objects">
<property name="text">
<string>Objects</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QCheckBox" name="checkBox_BGs">
<property name="text">
<string>BGs</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="checkBox_Triggers">
<property name="text">
<string>Triggers</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="checkBox_HealSpots">
<property name="text">
<string>Heal Spots</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_Connections">
<property name="title">
<string>Connections</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_Connection_Options">
<item row="0" column="0">
<widget class="QCheckBox" name="checkBox_ConnectionUp">
<property name="text">
<string>Up</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QCheckBox" name="checkBox_ConnectionDown">
<property name="text">
<string>Down</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QCheckBox" name="checkBox_ConnectionLeft">
<property name="text">
<string>Left</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QCheckBox" name="checkBox_ConnectionRight">
<property name="text">
<string>Right</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_Misc">
<property name="title">
<string>Miscellaneous</string>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_Misc_Options">
<item row="0" column="0">
<widget class="QCheckBox" name="checkBox_Grid">
<property name="text">
<string>Grid</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="checkBox_Elevation">
<property name="text">
<string>Collision</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QCheckBox" name="checkBox_Border">
<property name="text">
<string>Border</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="pushButton_Reset">
<property name="text">
<string>Reset</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_Cancel">
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_Save">
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -16,6 +16,7 @@
#include "editor.h"
#include "tileseteditor.h"
#include "regionmapeditor.h"
#include "mapimageexporter.h"
#include "filterchildrenproxymodel.h"
#include "newmappopup.h"
#include "newtilesetdialog.h"
@ -71,7 +72,6 @@ private slots:
void on_tabWidget_currentChanged(int index);
void on_actionUndo_triggered();
void on_actionRedo_triggered();
void on_actionZoom_In_triggered();
@ -94,15 +94,10 @@ private slots:
void updateObjects();
void on_toolButton_Paint_clicked();
void on_toolButton_Select_clicked();
void on_toolButton_Fill_clicked();
void on_toolButton_Dropper_clicked();
void on_toolButton_Move_clicked();
void on_toolButton_Shift_clicked();
void onOpenMapListContextMenu(const QPoint &point);
@ -115,29 +110,17 @@ private slots:
void on_action_Export_Map_Image_triggered();
void on_comboBox_ConnectionDirection_currentIndexChanged(const QString &arg1);
void on_spinBox_ConnectionOffset_valueChanged(int offset);
void on_comboBox_ConnectedMap_currentTextChanged(const QString &mapName);
void on_pushButton_AddConnection_clicked();
void on_pushButton_RemoveConnection_clicked();
void on_comboBox_DiveMap_currentTextChanged(const QString &mapName);
void on_comboBox_EmergeMap_currentTextChanged(const QString &mapName);
void on_comboBox_PrimaryTileset_activated(const QString &arg1);
void on_comboBox_SecondaryTileset_activated(const QString &arg1);
void on_pushButton_clicked();
void on_checkBox_smartPaths_stateChanged(int selected);
void on_checkBox_Visibility_clicked(bool checked);
void on_checkBox_ToggleBorder_stateChanged(int arg1);
void resetMapViewScale();
@ -169,6 +152,7 @@ private:
Ui::MainWindow *ui;
TilesetEditor *tilesetEditor = nullptr;
RegionMapEditor *regionMapEditor = nullptr;
MapImageExporter *mapImageExporter = nullptr;
FilterChildrenProxyModel *mapListProxyModel;
NewMapPopup *newmapprompt = nullptr;
QStandardItemModel *mapListModel;

View file

@ -0,0 +1,67 @@
#ifndef MAPIMAGEEXPORTER_H
#define MAPIMAGEEXPORTER_H
#include "map.h"
#include "editor.h"
#include <QDialog>
namespace Ui {
class MapImageExporter;
}
class MapImageExporter : public QDialog
{
Q_OBJECT
public:
explicit MapImageExporter(QWidget *parent, Editor *editor);
~MapImageExporter();
private:
Ui::MapImageExporter *ui;
Map *map = nullptr;
Editor *editor = nullptr;
QGraphicsScene *scene = nullptr;
QPixmap preview;
bool showObjects = false;
bool showWarps = false;
bool showBGs = false;
bool showTriggers = false;
bool showHealSpots = false;
bool showUpConnections = false;
bool showDownConnections = false;
bool showLeftConnections = false;
bool showRightConnections = false;
bool showGrid = false;
bool showBorder = false;
bool showCollision = false;
void updatePreview();
void saveImage();
private slots:
void on_checkBox_Objects_stateChanged(int state);
void on_checkBox_Warps_stateChanged(int state);
void on_checkBox_BGs_stateChanged(int state);
void on_checkBox_Triggers_stateChanged(int state);
void on_checkBox_HealSpots_stateChanged(int state);
void on_checkBox_ConnectionUp_stateChanged(int state);
void on_checkBox_ConnectionDown_stateChanged(int state);
void on_checkBox_ConnectionLeft_stateChanged(int state);
void on_checkBox_ConnectionRight_stateChanged(int state);
void on_checkBox_Elevation_stateChanged(int state);
void on_checkBox_Grid_stateChanged(int state);
void on_checkBox_Border_stateChanged(int state);
void on_pushButton_Save_pressed();
void on_pushButton_Reset_pressed();
void on_pushButton_Cancel_pressed();
};
#endif // MAPIMAGEEXPORTER_H

View file

@ -61,6 +61,7 @@ SOURCES += src/core/block.cpp \
src/ui/tilemaptileselector.cpp \
src/ui/regionmapeditor.cpp \
src/ui/newmappopup.cpp \
src/ui/mapimageexporter.cpp \
src/config.cpp \
src/editor.cpp \
src/main.cpp \
@ -119,6 +120,7 @@ HEADERS += include/core/block.h \
include/ui/tilemaptileselector.h \
include/ui/regionmapeditor.h \
include/ui/newmappopup.h \
include/ui/mapimageexporter.h \
include/config.h \
include/editor.h \
include/mainwindow.h \
@ -134,7 +136,8 @@ FORMS += forms/mainwindow.ui \
forms/regionmapeditor.ui \
forms/newmappopup.ui \
forms/aboutporymap.ui \
forms/newtilesetdialog.ui
forms/newtilesetdialog.ui \
forms/mapimageexporter.ui
RESOURCES += \
resources/images.qrc

View file

@ -1913,10 +1913,18 @@ void MainWindow::onTilesetsSaved(QString primaryTilesetLabel, QString secondaryT
void MainWindow::on_action_Export_Map_Image_triggered()
{
QString defaultFilepath = QString("%1/%2.png").arg(editor->project->root).arg(editor->map->name);
QString filepath = QFileDialog::getSaveFileName(this, "Export Map Image", defaultFilepath, "Image Files (*.png *.jpg *.bmp)");
if (!filepath.isEmpty()) {
editor->map_item->pixmap().save(filepath);
if (!this->mapImageExporter) {
this->mapImageExporter = new MapImageExporter(this, this->editor);
connect(this->mapImageExporter, &QObject::destroyed, [=](QObject *) { this->mapImageExporter = nullptr; });
this->mapImageExporter->setAttribute(Qt::WA_DeleteOnClose);
}
if (!this->mapImageExporter->isVisible()) {
this->mapImageExporter->show();
} else if (this->mapImageExporter->isMinimized()) {
this->mapImageExporter->showNormal();
} else {
this->mapImageExporter->activateWindow();
}
}

195
src/ui/mapimageexporter.cpp Normal file
View file

@ -0,0 +1,195 @@
#include "mapimageexporter.h"
#include "ui_mapimageexporter.h"
#include <QFileDialog>
#include <QMatrix>
#include <QImage>
#include <QPainter>
#include <QPoint>
MapImageExporter::MapImageExporter(QWidget *parent_, Editor *editor_) :
QDialog(parent_),
ui(new Ui::MapImageExporter)
{
ui->setupUi(this);
this->map = editor_->map;
this->editor = editor_;
this->ui->comboBox_MapSelection->addItems(*editor->project->mapNames);
this->ui->comboBox_MapSelection->setCurrentText(map->name);
this->ui->comboBox_MapSelection->setEnabled(false);// TODO: allow selecting map from drop-down
updatePreview();
}
MapImageExporter::~MapImageExporter() {
delete ui;
}
void MapImageExporter::saveImage() {
QString defaultFilepath = QString("%1/%2.png").arg(editor->project->root).arg(map->name);
QString filepath = QFileDialog::getSaveFileName(this, "Export Map Image", defaultFilepath,
"Image Files (*.png *.jpg *.bmp)");
if (!filepath.isEmpty()) {
this->ui->graphicsView_Preview->grab(this->ui->graphicsView_Preview->sceneRect().toRect()).save(filepath);
this->close();
}
}
void MapImageExporter::updatePreview() {
if (scene) {
delete scene;
scene = nullptr;
}
scene = new QGraphicsScene;
// draw background layer / base image
if (showCollision) {
preview = map->collision_pixmap;
} else {
preview = map->pixmap;
}
// draw events
QPainter eventPainter(&preview);
QList<Event*> events = map->getAllEvents();
for (Event *event : events) {
QString group = event->get("event_group_type");
if ((showObjects && group == "object_event_group")
|| (showWarps && group == "warp_event_group")
|| (showBGs && group == "bg_event_group")
|| (showTriggers && group == "coord_event_group")
|| (showHealSpots && group == "heal_event_group"))
eventPainter.drawImage(QPoint(event->getPixelX(), event->getPixelY()), event->pixmap.toImage());
}
eventPainter.end();
// draw map border
// note: this will break when allowing map to be selected from drop down maybe
int borderHeight = 0, borderWidth = 0;
if (showUpConnections || showDownConnections || showLeftConnections || showRightConnections) showBorder = true;
if (showBorder) {
borderHeight = 32 * 3, borderWidth = 32 * 3;
QPixmap newPreview = QPixmap(map->pixmap.width() + borderWidth * 2, map->pixmap.height() + borderHeight * 2);
QPainter borderPainter(&newPreview);
for (auto borderItem : editor->borderItems) {
borderPainter.drawImage(QPoint(borderItem->x() + borderWidth, borderItem->y() + borderHeight),
borderItem->pixmap().toImage());
}
borderPainter.drawImage(QPoint(borderWidth, borderHeight), preview.toImage());
borderPainter.end();
preview = newPreview;
}
// if showing connections, draw on outside of image
QPainter connectionPainter(&preview);
for (auto connectionItem : editor->connection_edit_items) {
QString direction = connectionItem->connection->direction;
if ((showUpConnections && direction == "up")
|| (showDownConnections && direction == "down")
|| (showLeftConnections && direction == "left")
|| (showRightConnections && direction == "right"))
connectionPainter.drawImage(connectionItem->initialX + borderWidth, connectionItem->initialY + borderHeight,
connectionItem->basePixmap.toImage());
}
connectionPainter.end();
// draw grid directly onto the pixmap
// since the last grid lines are outside of the pixmap, add a pixel to the bottom and right
if (showGrid) {
int addX = 1, addY = 1;
if (borderHeight) addY = 0;
if (borderWidth) addX = 0;
QPixmap newPreview = QPixmap(preview.width() + addX, preview.height() + addY);
QPainter gridPainter(&newPreview);
gridPainter.drawImage(QPoint(0, 0), preview.toImage());
for (auto lineItem : editor->gridLines) {
QPointF addPos(borderWidth, borderHeight);
gridPainter.drawLine(lineItem->line().p1() + addPos, lineItem->line().p2() + addPos);
}
gridPainter.end();
preview = newPreview;
}
scene->addPixmap(preview);
this->scene->setSceneRect(this->scene->itemsBoundingRect());
this->ui->graphicsView_Preview->setScene(scene);
this->ui->graphicsView_Preview->setFixedSize(scene->itemsBoundingRect().width() + 2,
scene->itemsBoundingRect().height() + 2);
}
void MapImageExporter::on_checkBox_Elevation_stateChanged(int state) {
showCollision = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_Grid_stateChanged(int state) {
showGrid = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_Border_stateChanged(int state) {
showBorder = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_Objects_stateChanged(int state) {
showObjects = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_Warps_stateChanged(int state) {
showWarps = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_BGs_stateChanged(int state) {
showBGs = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_Triggers_stateChanged(int state) {
showTriggers = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_HealSpots_stateChanged(int state) {
showHealSpots = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_ConnectionUp_stateChanged(int state) {
showUpConnections = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_ConnectionDown_stateChanged(int state) {
showDownConnections = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_ConnectionLeft_stateChanged(int state) {
showLeftConnections = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_checkBox_ConnectionRight_stateChanged(int state) {
showRightConnections = (state == Qt::Checked);
updatePreview();
}
void MapImageExporter::on_pushButton_Save_pressed() {
saveImage();
}
void MapImageExporter::on_pushButton_Reset_pressed() {
for (auto widget : this->findChildren<QCheckBox *>())
widget->setChecked(false);
}
void MapImageExporter::on_pushButton_Cancel_pressed() {
this->close();
}