|
| 1 | +#include "displayprint.h" |
| 2 | +#include "displayprintexportform.h" |
| 3 | +#include <QScrollBar> |
| 4 | + |
| 5 | +DisplayPrint::DisplayPrint() |
| 6 | +{ |
| 7 | + QList<ParameterDelegate::ParameterInfo> exportInfos = { |
| 8 | + {"plugin_name", QJsonValue::String}, |
| 9 | + {"image_width", QJsonValue::Double}, |
| 10 | + {"image_height", QJsonValue::Double}, |
| 11 | + {"image_filename", QJsonValue::String} |
| 12 | + }; |
| 13 | + |
| 14 | + m_exportDelegate = ParameterDelegate::create( |
| 15 | + exportInfos, |
| 16 | + [this](const QJsonObject ¶meters) { |
| 17 | + QString pluginName = parameters.value("plugin_name").toString(); |
| 18 | + return QString("Export %1 Image").arg(pluginName); |
| 19 | + }, |
| 20 | + [](QSharedPointer<ParameterDelegate> delegate, QSize size) { |
| 21 | + Q_UNUSED(size) |
| 22 | + return new DisplayPrintExportForm(delegate); |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +ImporterExporterInterface* DisplayPrint::createDefaultImporterExporter() |
| 27 | +{ |
| 28 | + return new DisplayPrint(); |
| 29 | +} |
| 30 | + |
| 31 | +QString DisplayPrint::name() |
| 32 | +{ |
| 33 | + return "Display Print"; |
| 34 | +} |
| 35 | + |
| 36 | +QString DisplayPrint::description() |
| 37 | +{ |
| 38 | + return "Saves a Display Plugin's display to an image"; |
| 39 | +} |
| 40 | + |
| 41 | +QStringList DisplayPrint::tags() |
| 42 | +{ |
| 43 | + return {"Generic"}; |
| 44 | +} |
| 45 | + |
| 46 | +bool DisplayPrint::canExport() |
| 47 | +{ |
| 48 | + return true; |
| 49 | +} |
| 50 | + |
| 51 | +bool DisplayPrint::canImport() |
| 52 | +{ |
| 53 | + return false; |
| 54 | +} |
| 55 | + |
| 56 | +QSharedPointer<ParameterDelegate> DisplayPrint::importParameterDelegate() |
| 57 | +{ |
| 58 | + return nullptr; |
| 59 | +} |
| 60 | + |
| 61 | +QSharedPointer<ParameterDelegate> DisplayPrint::exportParameterDelegate() |
| 62 | +{ |
| 63 | + return m_exportDelegate; |
| 64 | +} |
| 65 | + |
| 66 | +QSharedPointer<ImportResult> DisplayPrint::importBits(QJsonObject parameters, |
| 67 | + QSharedPointer<PluginActionProgress> progress) |
| 68 | +{ |
| 69 | + Q_UNUSED(parameters) |
| 70 | + Q_UNUSED(progress) |
| 71 | + return ImportResult::error("Import not implemented"); |
| 72 | +} |
| 73 | + |
| 74 | +QSharedPointer<ExportResult> DisplayPrint::exportBits(QSharedPointer<const BitContainer> container, |
| 75 | + QJsonObject parameters, |
| 76 | + QSharedPointer<PluginActionProgress> progress) |
| 77 | +{ |
| 78 | + if (!m_exportDelegate->validate(parameters)) { |
| 79 | + return ExportResult::error(QString("Invalid parameters passed to %1").arg(name())); |
| 80 | + } |
| 81 | + |
| 82 | + auto pluginManager = DisplayPrintExportForm::loadUpPluginManager(); |
| 83 | + auto displayPlugin = pluginManager->getDisplay(parameters.value("plugin_name").toString()); |
| 84 | + if (displayPlugin.isNull()) { |
| 85 | + return ExportResult::error(QString("Failed to load display '%1'").arg(parameters.value("plugin_name").toString())); |
| 86 | + } |
| 87 | + |
| 88 | + QSharedPointer<BitContainerManager> containerManager(new BitContainerManager()); |
| 89 | + QScopedPointer<QScrollBar> vScrollDummy(new QScrollBar()); |
| 90 | + QScopedPointer<QScrollBar> hScrollDummy(new QScrollBar()); |
| 91 | + |
| 92 | + QSharedPointer<DisplayHandle> displayHandle(new DisplayHandle(containerManager, vScrollDummy.data(), hScrollDummy.data())); |
| 93 | + |
| 94 | + //TODO: make this less hacky |
| 95 | + auto noConstContainer = BitContainer::create(container->bits(), container->info()); |
| 96 | + |
| 97 | + auto display = displayPlugin->display(displayHandle); |
| 98 | + display->setGeometry(0, 0, parameters.value("image_width").toInt(), parameters.value("image_height").toInt()); |
| 99 | + containerManager->addContainer(noConstContainer); |
| 100 | + containerManager->selectContainer(noConstContainer); |
| 101 | + |
| 102 | + QPixmap pixmap(parameters.value("image_width").toInt(), parameters.value("image_height").toInt()); |
| 103 | + display->render(&pixmap); |
| 104 | + |
| 105 | + QFile file(parameters.value("image_filename").toString()); |
| 106 | + if (!file.open(QFile::WriteOnly | QFile::Truncate)) { |
| 107 | + return ExportResult::error(QString("Failed to open file for writing '%1'").arg(file.fileName())); |
| 108 | + } |
| 109 | + pixmap.save(&file, "PNG"); |
| 110 | + |
| 111 | + return ExportResult::result(parameters); |
| 112 | +} |
0 commit comments