Commit 8a90ea18 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Make it possible to export 3D plots

parent 3314c8d4
......@@ -9,7 +9,7 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
#Checking whether we can compile calgebra
set(QT_REQUIRED_VERSION 5.2)
find_package(Qt5 ${QT_REQUIRED_VERSION} REQUIRED NO_MODULE COMPONENTS Widgets Xml Svg Test Qml Quick)
find_package(Qt5 ${QT_REQUIRED_VERSION} REQUIRED NO_MODULE COMPONENTS Widgets Xml Svg Test Qml Quick PrintSupport)
include(FeatureSummary)
include(ECMAddAppIcon)
......
......@@ -59,6 +59,7 @@ add_library( AnalitzaPlot ${analitzaplot_SRCS} )
target_link_libraries ( AnalitzaPlot
Qt5::Core
Qt5::Gui
Qt5::PrintSupport
Analitza
)
......
......@@ -18,7 +18,8 @@
#include <QGuiApplication>
#include <QCommandLineParser>
#include <qfileinfo.h>
#include <QFileInfo>
#include <QImage>
#include <analitzaplot/plotter3d_es.h>
#include <analitzaplot/plotsmodel.h>
#include <analitzaplot/plotsfactory.h>
......@@ -35,6 +36,7 @@ public:
virtual int currentPlot() const override { return -1; }
virtual void renderGL() override {}
virtual void modelChanged() override {}
QImage grabImage() override { return {}; }
};
int main(int argc, char** argv)
......
......@@ -32,6 +32,9 @@
#include <QFile>
#include <QOpenGLFunctions>
#include <QPixmap>
#include <QUrl>
#include <QPrinter>
#include <QPainter>
#if defined(HAVE_IEEEFP_H)
#include <ieeefp.h>
......@@ -480,3 +483,34 @@ void Plotter3DES::drawRefPlane()
glDrawArrays(GL_LINES, 0, vxs.size());
program.disableAttributeArray(vertexLocation);
}
bool Plotter3DES::save(const QUrl& url)
{
if (!url.isLocalFile())
return false;
const QString path = url.toLocalFile();
if(path.endsWith(QLatin1String(".x3d")) || path.endsWith(QLatin1String(".stl"))) {
exportSurfaces(path);
} else if(path.endsWith(QLatin1String(".pdf"))) {
auto px = grabImage();
QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(path);
printer.setPaperSize(px.size(), QPrinter::DevicePixel);
printer.setPageMargins(0,0,0,0, QPrinter::DevicePixel);
QPainter painter;
painter.begin(&printer);
painter.drawImage(QPoint(0,0), px);
painter.end();
} else {
auto px = grabImage();
return px.save(path);
}
return true;
}
QStringList Plotter3DES::filters() const
{
return {QObject::tr("PNG File (*.png)"), QObject::tr("PDF Document(*.pdf)"), QObject::tr("X3D Document (*.x3d)"), QObject::tr("STL Document (*.stl)")};
}
......@@ -117,6 +117,20 @@ class ANALITZAPLOT_EXPORT Plotter3DES : private QOpenGLFunctions
/** sets the view to the initial perspective */
void resetViewport();
/**
* saves the currently displayed plot in @p url
*
* @returns whether it was saved successfully
*/
bool save(const QUrl &url);
/**
* @returns the filters supported by save
*
* @see save()
*/
QStringList filters() const;
protected:
void addPlots(PlotItem* item);
......@@ -125,6 +139,8 @@ class ANALITZAPLOT_EXPORT Plotter3DES : private QOpenGLFunctions
static const quint8 YAxisArrowColor[];
static const quint8 ZAxisArrowColor[];
virtual QImage grabImage() = 0;
void resetViewPrivate(const QVector3D& rot);
enum SceneObjectType {Axes, RefPlaneXY, XArrowAxisHint, YArrowAxisHint, ZArrowAxisHint};
......
......@@ -171,3 +171,8 @@ void PlotsView3DES::keyPressEvent(QKeyEvent* ev)
break;
}
}
QImage PlotsView3DES::grabImage()
{
return grabFramebuffer();
}
......@@ -79,6 +79,7 @@ private:
virtual void paintGL() override;
virtual void initializeGL() override;
virtual void resizeGL(int width, int height) override;
QImage grabImage() override;
QItemSelectionModel* m_selection;
......
......@@ -21,6 +21,9 @@
#include <QOpenGLFramebufferObject>
#include <analitzaplot/plotsmodel.h>
#include <QTimer>
#include <QQuickItemGrabResult>
#include <QGuiApplication>
#include <QElapsedTimer>
using namespace Analitza;
......@@ -72,7 +75,8 @@ QAbstractItemModel* Graph3DItem::model() const
void Graph3DItem::setModel(QAbstractItemModel* model)
{
disconnect(m_plotter->model(), 0, this, 0);
if (m_plotter->model())
disconnect(m_plotter->model(), 0, this, 0);
m_plotter->setModel(model);
......@@ -102,6 +106,16 @@ void Graph3DItem::resetViewport()
m_plotter->resetViewport();
}
bool Graph3DItem::save(const QUrl& url)
{
return m_plotter->save(url);
}
QStringList Graph3DItem::filters() const
{
return m_plotter->filters();
}
class Plotter3DFboRenderer : public QQuickFramebufferObject::Renderer
{
public:
......@@ -132,3 +146,18 @@ QQuickFramebufferObject::Renderer* Graph3DItem::createRenderer() const
m_plotter->setViewport(QRectF({0,0}, QSizeF(width(), height())));
return new Plotter3DFboRenderer(m_plotter);
}
QImage Plotter3DRenderer::grabImage()
{
QSharedPointer<QQuickItemGrabResult> imgGrab = m_item->grabToImage();
QImage ret;
connect(imgGrab.data(), &QQuickItemGrabResult::ready, this, [imgGrab, &ret]() {
ret = imgGrab->image();
});
QElapsedTimer timer;
timer.start();
while(ret.size().isEmpty() && timer.elapsed()<2000) {
qGuiApp->processEvents();
}
return ret;
}
......@@ -34,6 +34,7 @@ public:
void renderGL() override;
QQuickWindow* window() const;
QImage grabImage() override;
private:
Graph3DItem * m_item;
......@@ -42,6 +43,7 @@ private:
class Graph3DItem : public QQuickFramebufferObject
{
Q_OBJECT
Q_PROPERTY(QStringList filters READ filters CONSTANT)
Q_PROPERTY(QAbstractItemModel* model READ model WRITE setModel NOTIFY modelChanged)
public:
Graph3DItem(QQuickItem* parent = Q_NULLPTR);
......@@ -57,6 +59,10 @@ class Graph3DItem : public QQuickFramebufferObject
Q_SCRIPTABLE void scale(qreal s);
Q_SCRIPTABLE void resetViewport();
Q_SCRIPTABLE bool save(const QUrl &url);
QStringList filters() const;
Q_SIGNALS:
void modelChanged(QAbstractItemModel* model);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment