+ support of arbitrary background colors in snapshot function
This commit is contained in:
@@ -61,11 +61,6 @@
|
||||
<string>Black</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text" >
|
||||
<string>Transparent</string>
|
||||
</property>
|
||||
</item>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
|
||||
@@ -92,7 +92,7 @@ void Thumbnail::SaveDocFile (Base::Writer &writer) const
|
||||
}
|
||||
else {
|
||||
try {
|
||||
this->viewer->savePicture(this->size, this->size, View3DInventorViewer::Current, img);
|
||||
this->viewer->savePicture(this->size, this->size, QColor(), img);
|
||||
}
|
||||
catch (...) {
|
||||
this->createThumbnailFromFramebuffer(img);
|
||||
|
||||
@@ -518,7 +518,8 @@ void View3DInventor::print(QPrinter* printer)
|
||||
ps = SoVectorizeAction::A4;
|
||||
break;
|
||||
}
|
||||
_viewer->saveGraphic(ps,View3DInventorViewer::White,&action);
|
||||
QColor c = Qt::white;
|
||||
_viewer->saveGraphic(ps,c,&action);
|
||||
out->closeFile();
|
||||
QSvgRenderer svg;
|
||||
if (svg.load(QString::fromUtf8(tmp.c_str()))) {
|
||||
@@ -537,7 +538,7 @@ void View3DInventor::print(QPrinter* printer)
|
||||
}
|
||||
else {
|
||||
try {
|
||||
_viewer->savePicture(rect.width(), rect.height(), View3DInventorViewer::White, img);
|
||||
_viewer->savePicture(rect.width(), rect.height(), QColor(Qt::white), img);
|
||||
}
|
||||
catch (...) {
|
||||
previewFromFramebuffer(rect, img);
|
||||
|
||||
@@ -796,7 +796,7 @@ void View3DInventorViewer::setSceneGraph(SoNode* root)
|
||||
}
|
||||
}
|
||||
|
||||
void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage& img) const
|
||||
void View3DInventorViewer::savePicture(int w, int h, const QColor& bg, QImage& img) const
|
||||
{
|
||||
// if no valid color use the current background
|
||||
bool useBackground = false;
|
||||
@@ -813,9 +813,8 @@ void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage
|
||||
renderer.setViewportRegion(vp);
|
||||
SoCallback* cb = 0;
|
||||
|
||||
// if we use transparency then we must not set a background color
|
||||
switch (eBackgroundType) {
|
||||
case Current:
|
||||
// for an invalid color use the viewer's current background color
|
||||
if (!bg.isValid()) {
|
||||
if (backgroundroot->findChild(pcBackGround) == -1) {
|
||||
const QColor col = this->backgroundColor();
|
||||
renderer.setBackgroundColor(SbColor(col.redF(), col.greenF(), col.blueF()));
|
||||
@@ -825,22 +824,9 @@ void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage
|
||||
cb = new SoCallback;
|
||||
cb->setCallback(clearBufferCB);
|
||||
}
|
||||
break;
|
||||
|
||||
case White:
|
||||
renderer.setBackgroundColor(SbColor(1.0, 1.0, 1.0));
|
||||
break;
|
||||
|
||||
case Black:
|
||||
renderer.setBackgroundColor(SbColor(0.0, 0.0, 0.0));
|
||||
break;
|
||||
|
||||
case Transparent:
|
||||
renderer.setComponents(SoFCOffscreenRenderer::RGB_TRANSPARENCY);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
else {
|
||||
renderer.setBackgroundColor(SbColor(bg.redF(), bg.greenF(), bg.blueF()));
|
||||
}
|
||||
|
||||
SoSeparator* root = new SoSeparator;
|
||||
@@ -888,29 +874,10 @@ void View3DInventorViewer::savePicture(int w, int h, int eBackgroundType, QImage
|
||||
}
|
||||
}
|
||||
|
||||
void View3DInventorViewer::saveGraphic(int pagesize, int eBackgroundType, SoVectorizeAction* va) const
|
||||
void View3DInventorViewer::saveGraphic(int pagesize, const QColor& bgcolor, SoVectorizeAction* va) const
|
||||
{
|
||||
const QColor col = this->backgroundColor();
|
||||
|
||||
switch(eBackgroundType) {
|
||||
case Current:
|
||||
va->setBackgroundColor(true, SbColor(col.redF(), col.greenF(), col.blueF()));
|
||||
break;
|
||||
|
||||
case White:
|
||||
va->setBackgroundColor(true, SbColor(1.0, 1.0, 1.0));
|
||||
break;
|
||||
|
||||
case Black:
|
||||
va->setBackgroundColor(true, SbColor(0.0, 0.0, 0.0));
|
||||
break;
|
||||
|
||||
case Transparent:
|
||||
break; // not supported
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (bgcolor.isValid())
|
||||
va->setBackgroundColor(true, SbColor(bgcolor.redF(), bgcolor.greenF(), bgcolor.blueF()));
|
||||
|
||||
float border = 10.0f;
|
||||
SbVec2s vpsize = this->getSoRenderManager()->getViewportRegion().getViewportSizePixels();
|
||||
@@ -1069,9 +1036,9 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const
|
||||
Base::FileInfo fi(filename);
|
||||
|
||||
// Write VRML V2.0
|
||||
if(fi.hasExtension("wrl") || fi.hasExtension("vrml") || fi.hasExtension("wrz")) {
|
||||
if (fi.hasExtension("wrl") || fi.hasExtension("vrml") || fi.hasExtension("wrz")) {
|
||||
// If 'wrz' is set then force compression
|
||||
if(fi.hasExtension("wrz"))
|
||||
if (fi.hasExtension("wrz"))
|
||||
binary = true;
|
||||
|
||||
SoToVRML2Action tovrml2;
|
||||
@@ -1081,7 +1048,7 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const
|
||||
std::string buffer = SoFCDB::writeNodesToString(vrmlRoot);
|
||||
vrmlRoot->unref(); // release the memory as soon as possible
|
||||
|
||||
if(binary) {
|
||||
if (binary) {
|
||||
// We want to write compressed VRML but Coin 2.4.3 doesn't do it even though
|
||||
// SoOutput::getAvailableCompressionMethods() delivers a string list that
|
||||
// contains 'GZIP'. setCompression() was called directly after opening the file,
|
||||
@@ -1091,7 +1058,7 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const
|
||||
Base::ofstream str(fi, std::ios::out | std::ios::binary);
|
||||
zipios::GZIPOutputStream gzip(str);
|
||||
|
||||
if(gzip) {
|
||||
if (gzip) {
|
||||
gzip << buffer;
|
||||
gzip.close();
|
||||
ret = true;
|
||||
@@ -1107,11 +1074,12 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(fi.hasExtension("idtf") || fi.hasExtension("svg")) {
|
||||
int ps=4, t=2;
|
||||
else if (fi.hasExtension("idtf") || fi.hasExtension("svg")) {
|
||||
int ps=4;
|
||||
QColor c = Qt::white;
|
||||
std::auto_ptr<SoVectorizeAction> vo;
|
||||
|
||||
if(fi.hasExtension("svg")) {
|
||||
if (fi.hasExtension("svg")) {
|
||||
vo = std::auto_ptr<SoVectorizeAction>(new SoFCVectorizeSVGAction());
|
||||
}
|
||||
else if(fi.hasExtension("idtf")) {
|
||||
@@ -1123,13 +1091,13 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const
|
||||
|
||||
SoVectorOutput* out = vo->getOutput();
|
||||
|
||||
if(!out || !out->openFile(filename)) {
|
||||
if (!out || !out->openFile(filename)) {
|
||||
std::ostringstream a_out;
|
||||
a_out << "Cannot open file '" << filename << "'";
|
||||
throw Base::Exception(a_out.str());
|
||||
}
|
||||
|
||||
saveGraphic(ps,t,vo.get());
|
||||
saveGraphic(ps,c,vo.get());
|
||||
out->closeFile();
|
||||
}
|
||||
else {
|
||||
@@ -1137,7 +1105,7 @@ bool View3DInventorViewer::dumpToFile(const char* filename, bool binary) const
|
||||
std::string buffer = SoFCDB::writeNodesToString(pcViewProviderRoot);
|
||||
Base::ofstream str(Base::FileInfo(filename), std::ios::out);
|
||||
|
||||
if(str) {
|
||||
if (str) {
|
||||
str << buffer;
|
||||
str.close();
|
||||
ret = true;
|
||||
|
||||
@@ -76,13 +76,6 @@ class GuiExport View3DInventorViewer : public Quarter::SoQTQuarterAdaptor, publi
|
||||
typedef Quarter::SoQTQuarterAdaptor inherited;
|
||||
|
||||
public:
|
||||
/// Background modes for the savePicture() method
|
||||
enum eBackgroundType {
|
||||
Current = 0, /**< Use the current viewer Background */
|
||||
Black = 1, /**< Black background */
|
||||
White = 2, /**< White background */
|
||||
Transparent = 3, /**< Transparent background */
|
||||
};
|
||||
/// Pick modes for picking points in the scene
|
||||
enum SelectionMode {
|
||||
Lasso = 0, /**< Select objects using a lasso. */
|
||||
@@ -204,8 +197,8 @@ public:
|
||||
* Creates an image with width \a w and height \a h of the current scene graph
|
||||
* and exports the rendered scenegraph to an image.
|
||||
*/
|
||||
void savePicture(int w, int h, int eBackgroundType, QImage&) const;
|
||||
void saveGraphic(int pagesize, int eBackgroundType, SoVectorizeAction* va) const;
|
||||
void savePicture(int w, int h, const QColor&, QImage&) const;
|
||||
void saveGraphic(int pagesize, const QColor&, SoVectorizeAction* va) const;
|
||||
//@}
|
||||
/**
|
||||
* Writes the current scenegraph to an Inventor file, either in ascii or binary.
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#ifndef __InventorAll__
|
||||
# include "InventorAll.h"
|
||||
# include <sstream>
|
||||
# include <QColor>
|
||||
# include <QImage>
|
||||
# include <QGLFramebufferObject>
|
||||
# include <Inventor/SbViewVolume.h>
|
||||
@@ -655,29 +656,15 @@ Py::Object View3DInventorPy::isAnimationEnabled(const Py::Tuple& args)
|
||||
return Py::Boolean(ok ? true : false);
|
||||
}
|
||||
|
||||
void View3DInventorPy::createImageFromFramebuffer(int backgroundType, int width, int height, QImage& img)
|
||||
void View3DInventorPy::createImageFromFramebuffer(int width, int height, const QColor& bgcolor, QImage& img)
|
||||
{
|
||||
QGLFramebufferObject fbo(width, height, QGLFramebufferObject::Depth);
|
||||
const QColor col = _view->getViewer()->backgroundColor();
|
||||
bool on = _view->getViewer()->hasGradientBackground();
|
||||
|
||||
switch(backgroundType){
|
||||
case 0: // Current
|
||||
break;
|
||||
case 1: // Black
|
||||
_view->getViewer()->setBackgroundColor(QColor(0,0,0));
|
||||
_view->getViewer()->setGradientBackground(false);
|
||||
break;
|
||||
case 2: // White
|
||||
_view->getViewer()->setBackgroundColor(QColor(255,255,255));
|
||||
_view->getViewer()->setGradientBackground(false);
|
||||
break;
|
||||
case 3: // Transparent
|
||||
_view->getViewer()->setBackgroundColor(QColor(255,255,255));
|
||||
_view->getViewer()->setGradientBackground(false);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
if (bgcolor.isValid()) {
|
||||
_view->getViewer()->setBackgroundColor(bgcolor);
|
||||
_view->getViewer()->setGradientBackground(false);
|
||||
}
|
||||
|
||||
_view->getViewer()->renderToFramebuffer(&fbo);
|
||||
@@ -688,46 +675,30 @@ void View3DInventorPy::createImageFromFramebuffer(int backgroundType, int width,
|
||||
|
||||
Py::Object View3DInventorPy::saveImage(const Py::Tuple& args)
|
||||
{
|
||||
char *cFileName,*cImageType="Current",*cComment="$MIBA";
|
||||
int w=-1,h=-1,t;
|
||||
char *cFileName,*cColor="Current",*cComment="$MIBA";
|
||||
int w=-1,h=-1;
|
||||
|
||||
if (!PyArg_ParseTuple(args.ptr(), "s|iiss",&cFileName,&w,&h,&cImageType,&cComment))
|
||||
if (!PyArg_ParseTuple(args.ptr(), "s|iiss",&cFileName,&w,&h,&cColor,&cComment))
|
||||
throw Py::Exception();
|
||||
|
||||
#ifdef __GNUC__
|
||||
if (strcasecmp(cImageType,"Current")==0)
|
||||
t=0;
|
||||
else if(strcasecmp(cImageType,"Black")==0)
|
||||
t=1;
|
||||
else if(strcasecmp(cImageType,"White")==0)
|
||||
t=2;
|
||||
else if(strcasecmp(cImageType,"Transparent")==0)
|
||||
t=3;
|
||||
else
|
||||
throw Py::Exception("Parameter 4 have to be (Current|Black|White|Transparent)");
|
||||
#else
|
||||
if (_stricmp(cImageType,"Current")==0)
|
||||
t=0;
|
||||
else if(_stricmp(cImageType,"Black")==0)
|
||||
t=1;
|
||||
else if(_stricmp(cImageType,"White")==0)
|
||||
t=2;
|
||||
else if(_stricmp(cImageType,"Transparent")==0)
|
||||
t=3;
|
||||
else
|
||||
throw Py::Exception("Parameter 4 have to be (Current|Black|White|Transparent)");
|
||||
#endif
|
||||
QColor bg;
|
||||
QString colname = QString::fromLatin1(cColor);
|
||||
if (colname.compare(QLatin1String("Current"), Qt::CaseInsensitive))
|
||||
bg = QColor(); // assign an invalid color here
|
||||
else
|
||||
bg.setNamedColor(colname);
|
||||
|
||||
QImage img;
|
||||
if (App::GetApplication().GetParameterGroupByPath
|
||||
("User parameter:BaseApp/Preferences/Document")->GetBool("DisablePBuffers",false)) {
|
||||
createImageFromFramebuffer(t, w, h, img);
|
||||
createImageFromFramebuffer(w, h, bg, img);
|
||||
}
|
||||
else {
|
||||
try {
|
||||
_view->getViewer()->savePicture(w, h, t, img);
|
||||
_view->getViewer()->savePicture(w, h, bg, img);
|
||||
}
|
||||
catch (const Base::Exception&) {
|
||||
createImageFromFramebuffer(t, w, h, img);
|
||||
createImageFromFramebuffer(w, h, bg, img);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -741,9 +712,10 @@ Py::Object View3DInventorPy::saveImage(const Py::Tuple& args)
|
||||
Py::Object View3DInventorPy::saveVectorGraphic(const Py::Tuple& args)
|
||||
{
|
||||
char* filename;
|
||||
int ps=4, t=2;
|
||||
int ps=4;
|
||||
char* name="white";
|
||||
|
||||
if (!PyArg_ParseTuple(args.ptr(), "s|ii",&filename,&ps,&t))
|
||||
if (!PyArg_ParseTuple(args.ptr(), "s|is",&filename,&ps,&name))
|
||||
throw Py::Exception();
|
||||
|
||||
std::auto_ptr<SoVectorizeAction> vo;
|
||||
@@ -769,7 +741,14 @@ Py::Object View3DInventorPy::saveVectorGraphic(const Py::Tuple& args)
|
||||
throw Py::Exception(a_out.str());
|
||||
}
|
||||
|
||||
_view->getViewer()->saveGraphic(ps,t,vo.get());
|
||||
QColor bg;
|
||||
QString colname = QString::fromLatin1(name);
|
||||
if (colname.compare(QLatin1String("Current"), Qt::CaseInsensitive))
|
||||
bg = _view->getViewer()->backgroundColor();
|
||||
else
|
||||
bg.setNamedColor(colname);
|
||||
|
||||
_view->getViewer()->saveGraphic(ps,bg,vo.get());
|
||||
out->closeFile();
|
||||
return Py::None();
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ private:
|
||||
typedef PyObject* (*method_varargs_handler)(PyObject *_self, PyObject *_args);
|
||||
static method_varargs_handler pycxx_handler;
|
||||
static PyObject *method_varargs_ext_handler(PyObject *_self, PyObject *_args);
|
||||
void createImageFromFramebuffer(int backgroundType, int width, int height, QImage&);
|
||||
void createImageFromFramebuffer(int width, int height, const QColor&, QImage&);
|
||||
|
||||
private:
|
||||
std::list<PyObject*> callbacks;
|
||||
|
||||
Reference in New Issue
Block a user