Skip to content

Commit 585806e

Browse files
committed
feat: add DisplayPrint exporter plugin for saving display images
1 parent cde80f0 commit 585806e

File tree

7 files changed

+427
-0
lines changed

7 files changed

+427
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2020-10-21T22:25:30.336Z
4+
#
5+
#-------------------------------------------------
6+
7+
QT += widgets
8+
9+
QT -= gui
10+
11+
TARGET = DisplayPrint
12+
TEMPLATE = lib
13+
14+
DEFINES += DISPLAYPRINT_LIBRARY
15+
16+
CONFIG += c++11 plugin
17+
CONFIG -= debug_and_release_target
18+
19+
# The following define makes your compiler emit warnings if you use
20+
# any feature of Qt which has been marked as deprecated (the exact warnings
21+
# depend on your compiler). Please consult the documentation of the
22+
# deprecated API in order to know how to port your code away from it.
23+
DEFINES += QT_DEPRECATED_WARNINGS
24+
25+
# You can also make your code fail to compile if you use deprecated APIs.
26+
# In order to do so, uncomment the following line.
27+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
28+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
29+
30+
SOURCES += displayprint.cpp \
31+
displayprintexportform.cpp
32+
33+
HEADERS += displayprint.h \
34+
displayprintexportform.h
35+
36+
FORMS += \
37+
displayprintexportform.ui
38+
39+
LIBS += -L$$OUT_PWD/../../../hobbits-core/ -lhobbits-core
40+
LIBS += -L$$OUT_PWD/../../../hobbits-widgets/ -lhobbits-widgets
41+
INCLUDEPATH += $$PWD/../../../hobbits-core $$PWD/../../../hobbits-widgets
42+
DEPENDPATH += $$PWD/../../../hobbits-core $$PWD/../../../hobbits-widgets
43+
44+
unix:!mac {
45+
QMAKE_LFLAGS_RPATH=
46+
QMAKE_LFLAGS += "-Wl,-rpath,'$$ORIGIN/../../lib:$$ORIGIN'"
47+
}
48+
49+
mac {
50+
QMAKE_LFLAGS_RPATH=
51+
QMAKE_LFLAGS += "-Wl,-rpath,'@executable_path/../Frameworks'"
52+
}
53+
54+
unix {
55+
target.path = $$(HOME)/.local/share/hobbits/plugins/importerexporters
56+
INSTALLS += target
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 &parameters) {
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef DISPLAYPRINT_H
2+
#define DISPLAYPRINT_H
3+
4+
#include "importexportinterface.h"
5+
#include "parameterdelegate.h"
6+
7+
class DisplayPrint : public QObject, ImporterExporterInterface
8+
{
9+
Q_OBJECT
10+
Q_PLUGIN_METADATA(IID "hobbits.ImporterExporterInterface.DisplayPrint")
11+
Q_INTERFACES(ImporterExporterInterface)
12+
13+
public:
14+
DisplayPrint();
15+
16+
ImporterExporterInterface* createDefaultImporterExporter() override;
17+
18+
QString name() override;
19+
QString description() override;
20+
QStringList tags() override;
21+
22+
bool canExport() override;
23+
bool canImport() override;
24+
25+
virtual QSharedPointer<ParameterDelegate> importParameterDelegate() override;
26+
virtual QSharedPointer<ParameterDelegate> exportParameterDelegate() override;
27+
28+
QSharedPointer<ImportResult> importBits(QJsonObject parameters,
29+
QSharedPointer<PluginActionProgress> progress) override;
30+
QSharedPointer<ExportResult> exportBits(QSharedPointer<const BitContainer> container,
31+
QJsonObject parameters,
32+
QSharedPointer<PluginActionProgress> progress) override;
33+
34+
private:
35+
QSharedPointer<ParameterDelegate> m_exportDelegate;
36+
};
37+
38+
#endif // DISPLAYPRINT_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "displayprintexportform.h"
2+
#include "ui_displayprintexportform.h"
3+
#include <QFileDialog>
4+
#include "settingsmanager.h"
5+
6+
DisplayPrintExportForm::DisplayPrintExportForm(QSharedPointer<ParameterDelegate> delegate):
7+
ui(new Ui::DisplayPrintExportForm()),
8+
m_paramHelper(new ParameterHelper(delegate))
9+
{
10+
ui->setupUi(this);
11+
12+
m_paramHelper->addComboBoxParameter("plugin_name", ui->cb_pluginName);
13+
m_paramHelper->addSpinBoxIntParameter("image_width", ui->sb_width);
14+
m_paramHelper->addSpinBoxIntParameter("image_height", ui->sb_height);
15+
m_paramHelper->addLineEditStringParameter("image_filename", ui->le_file);
16+
17+
auto manager = loadUpPluginManager();
18+
for (auto display : manager->displays()) {
19+
ui->cb_pluginName->addItem(display->name(), display->name());
20+
}
21+
}
22+
23+
DisplayPrintExportForm::~DisplayPrintExportForm()
24+
{
25+
delete ui;
26+
}
27+
28+
QString DisplayPrintExportForm::title()
29+
{
30+
return "Configure Display Print";
31+
}
32+
33+
QJsonObject DisplayPrintExportForm::parameters()
34+
{
35+
return m_paramHelper->getParametersFromUi();
36+
}
37+
38+
QSharedPointer<HobbitsPluginManager> DisplayPrintExportForm::loadUpPluginManager()
39+
{
40+
// TODO: this should be some kind of hobbits core functionality
41+
QSharedPointer<HobbitsPluginManager> manager(new HobbitsPluginManager());
42+
43+
QStringList warnings;
44+
QStringList pluginPaths;
45+
pluginPaths.append(
46+
SettingsManager::getPluginLoaderSetting(
47+
SettingsManager::PLUGIN_PATH_KEY).toString().split(":"));
48+
49+
QStringList pathBuffer;
50+
for (QString pluginPath : pluginPaths) {
51+
52+
if (pluginPath.startsWith("~/")) {
53+
pluginPath.replace(0, 1, QDir::homePath());
54+
}
55+
else if (!pluginPath.startsWith("/")) {
56+
pluginPath = QApplication::applicationDirPath() + "/" + pluginPath;
57+
}
58+
pathBuffer.append(pluginPath);
59+
}
60+
pluginPaths = pathBuffer;
61+
62+
for (QString pluginPath : pluginPaths) {
63+
manager->loadPlugins(pluginPath);
64+
}
65+
66+
return manager;
67+
}
68+
69+
bool DisplayPrintExportForm::setParameters(QJsonObject parameters)
70+
{
71+
return m_paramHelper->applyParametersToUi(parameters);
72+
}
73+
74+
void DisplayPrintExportForm::on_tb_selectFile_clicked()
75+
{
76+
QString file = QFileDialog::getSaveFileName(this, "Select Output Image File", QDir::homePath(), tr("PNG Image (*.png)"));
77+
78+
if (file.isEmpty()) {
79+
return;
80+
}
81+
82+
ui->le_file->setText(file);
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef DISPLAYPRINTEXPORTFORM_H
2+
#define DISPLAYPRINTEXPORTFORM_H
3+
4+
#include "abstractparametereditor.h"
5+
#include "parameterhelper.h"
6+
#include "hobbitspluginmanager.h"
7+
8+
namespace Ui
9+
{
10+
class DisplayPrintExportForm;
11+
}
12+
13+
class DisplayPrintExportForm : public AbstractParameterEditor
14+
{
15+
Q_OBJECT
16+
17+
public:
18+
DisplayPrintExportForm(QSharedPointer<ParameterDelegate> delegate);
19+
~DisplayPrintExportForm() override;
20+
21+
QString title() override;
22+
23+
bool setParameters(QJsonObject parameters) override;
24+
QJsonObject parameters() override;
25+
26+
static QSharedPointer<HobbitsPluginManager> loadUpPluginManager();
27+
28+
private slots:
29+
void on_tb_selectFile_clicked();
30+
31+
private:
32+
Ui::DisplayPrintExportForm *ui;
33+
QSharedPointer<ParameterHelper> m_paramHelper;
34+
};
35+
36+
#endif // DISPLAYPRINTEXPORTFORM_H

0 commit comments

Comments
 (0)