From e35a90b4f45a94ab5ddf7bc9660cab451fe1a7b3 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sat, 11 May 2024 19:53:35 -0400 Subject: (mapman) Export images. --- mapman/src/groupview.cpp | 39 +++++++++++++++++++++++++++++++++++++++ mapman/src/groupview.hpp | 3 +++ mapman/src/mainwindow.cpp | 15 +++++++++++++-- mapman/src/utils.cpp | 20 +++++++++++++++++++- mapman/src/utils.hpp | 5 +++++ 5 files changed, 79 insertions(+), 3 deletions(-) (limited to 'mapman') diff --git a/mapman/src/groupview.cpp b/mapman/src/groupview.cpp index ea4225a..7881184 100644 --- a/mapman/src/groupview.cpp +++ b/mapman/src/groupview.cpp @@ -1,6 +1,7 @@ #include "groupview.hpp" #include "library.hpp" #include "painter.hpp" +#include "src/utils.hpp" #include @@ -19,6 +20,8 @@ #include #include #include +#include +#include group_view::group_view() : QMdiSubWindow() { @@ -141,6 +144,14 @@ void group_view::set_library(map_library *lib) refresh_list(); } +void group_view::export_group_image(QString fn, int64_t gid) +{ + if (!l || !l->has_group(gid)) + return; + QImage img = image_of_map_group(l, l->get_group(gid), 1.); + img.save(fn, "PNG"); +} + void group_view::add_group() { map_group_t g { @@ -260,3 +271,31 @@ void group_view::update_map_view() for (int i = 0; i < g.hc * g.vc; ++i) p->set_map_id(i, g.populated[i], g.ids[i]); } + +void group_view::export_current_group() +{ + int64_t curgid = -1; + if (tv->currentIndex().isValid() && tv->currentIndex().siblingAtColumn(3).isValid()) + { + auto idx = tv->currentIndex().siblingAtColumn(3); + curgid = mf->data(idx, Qt::ItemDataRole::DisplayRole).toLongLong(); + } + if (!~curgid) + return; + QString fn = QFileDialog::getSaveFileName(this, "Export Image", QString(), "Images (*.png)"); + if (fn.isEmpty()) return; + export_group_image(fn, curgid); +} + +void group_view::export_all_groups() +{ + QString fp = QFileDialog::getExistingDirectory(this, "Select Output Directory"); + if (fp.isEmpty()) return; + auto gids = l->groups(); + for (auto gid : gids) + { + auto g = l->get_group(gid); + auto fn = QString("%1/%2_%3.png").arg(fp).arg(gid).arg(QString::fromStdString(g.title)); + export_group_image(fn, gid); + } +} diff --git a/mapman/src/groupview.hpp b/mapman/src/groupview.hpp index 6db8838..1a2902c 100644 --- a/mapman/src/groupview.hpp +++ b/mapman/src/groupview.hpp @@ -23,6 +23,7 @@ public: group_view(); ~group_view(); void set_library(map_library *lib); + void export_group_image(QString fn, int64_t gid); public slots: void add_group(); void rem_group(); @@ -32,6 +33,8 @@ public slots: void refresh_list(); void reset_dim(); void update_map_view(); + void export_current_group(); + void export_all_groups(); private: QTableView *tv; QStandardItemModel *m; diff --git a/mapman/src/mainwindow.cpp b/mapman/src/mainwindow.cpp index 83d70c0..d5a5662 100644 --- a/mapman/src/mainwindow.cpp +++ b/mapman/src/mainwindow.cpp @@ -35,6 +35,9 @@ mapman_main_window::mapman_main_window() : QMainWindow() auto cra = fm->addAction("Create / L&oad MapDB..."); auto cla = fm->addAction("&Close MapDB"); fm->addSeparator(); + auto xca = fm->addAction("&Export Current Art..."); + auto xaa = fm->addAction("Export &All Arts..."); + fm->addSeparator(); auto lda = fm->addAction("&Load Map Dump..."); fm->addSeparator(); auto cta = fm->addAction("&Compare Map Tally..."); @@ -43,7 +46,9 @@ mapman_main_window::mapman_main_window() : QMainWindow() cla->setEnabled(false); lda->setEnabled(false); cta->setEnabled(false); - connect(cra, &QAction::triggered, [this, cla, lda, cta] { + xca->setEnabled(false); + xaa->setEnabled(false); + connect(cra, &QAction::triggered, [this, cla, lda, cta, xca, xaa] { QString fn = QFileDialog::getSaveFileName(this, "Create / Load MapDB", QString(), "*.mapdb", nullptr, QFileDialog::Option::DontConfirmOverwrite); if (fn.length()) { @@ -63,15 +68,21 @@ mapman_main_window::mapman_main_window() : QMainWindow() cla->setEnabled(true); lda->setEnabled(true); cta->setEnabled(true); + xca->setEnabled(true); + xaa->setEnabled(true); } }); - connect(cla, &QAction::triggered, [this, cla, lda, cta] { + connect(cla, &QAction::triggered, [this, cla, lda, cta, xca, xaa] { if (l) delete l; l = nullptr; cla->setEnabled(false); lda->setEnabled(false); cta->setEnabled(false); + xca->setEnabled(false); + xaa->setEnabled(false); }); + connect(xca, &QAction::triggered, gv, &group_view::export_current_group); + connect(xaa, &QAction::triggered, gv, &group_view::export_all_groups); connect(lda, &QAction::triggered, [this] { if (!l) return; QString fn = QFileDialog::getOpenFileName(this, "Load Map Dump", QString(), "*.gz"); diff --git a/mapman/src/utils.cpp b/mapman/src/utils.cpp index e6f658c..06b0eb4 100644 --- a/mapman/src/utils.cpp +++ b/mapman/src/utils.cpp @@ -1,7 +1,10 @@ #include "utils.hpp" +#include "library.hpp" +#include "mapdump.hpp" #include #include +#include rgb_t modify_color(rgb_t c, uint8_t variant) { @@ -21,7 +24,7 @@ rgb_t modify_color(rgb_t c, uint8_t variant) QColor rgb2qcolor(rgb_t c) {return QColor(c.r, c.g, c.b);} -QPixmap pixmap_of_map_data(const std::array &map_data, double scaling) +QPixmap pixmap_of_map_data(const map_data_t &map_data, double scaling) { QImage ret(128, 128, QImage::Format_ARGB32); ret.setDevicePixelRatio(scaling); @@ -39,3 +42,18 @@ QPixmap pixmap_of_map_data(const std::array &map_data, doubl return QPixmap::fromImage(ret); } +QImage image_of_map_group(const map_library *library, const map_group_t &group, double scaling) +{ + QImage ret(group.hc * 128, group.vc * 128, QImage::Format_ARGB32); + ret.fill(0); + ret.setDevicePixelRatio(1.); + { + QPainter p(&ret); + for (int i = 0 ; i < group.vc; ++i) + for (int j = 0 ; j < group.hc; ++j) + if (group.populated[i * group.hc + j]) + p.drawPixmap(128 * j, 128 * i, pixmap_of_map_data(library->get_map(group.ids[i * group.hc + j]).map_data, 1.)); + } + ret.setDevicePixelRatio(scaling); + return ret; +} diff --git a/mapman/src/utils.hpp b/mapman/src/utils.hpp index 76978f2..286e0e2 100644 --- a/mapman/src/utils.hpp +++ b/mapman/src/utils.hpp @@ -5,6 +5,7 @@ #include #include +#include struct rgb_t { @@ -78,5 +79,9 @@ const rgb_t MAP_COLORS[62] = { {127, 167, 150} }; +class map_library; +struct map_group_t; + QPixmap pixmap_of_map_data(const map_data_t &map_data, double scaling = 1.); +QImage image_of_map_group(const map_library *library, const map_group_t &group, double scaling = 1.); #endif -- cgit v1.2.3