ViewProviderImagePlane.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2011 Jürgen Riegel (juergen.riegel@web.de)              *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <Inventor/SbVec3f.h>
00028 # include <Inventor/nodes/SoSeparator.h>
00029 # include <Inventor/nodes/SoCoordinate3.h>
00030 # include <Inventor/nodes/SoDrawStyle.h>
00031 # include <Inventor/nodes/SoFaceSet.h>
00032 # include <Inventor/nodes/SoShapeHints.h>
00033 # include <Inventor/nodes/SoImage.h>
00034 # include <Inventor/nodes/SoTextureCoordinate2.h>
00035 # include <Inventor/nodes/SoTexture2.h>
00036 # include <QFile>
00037 # include <QFileInfo>
00038 # include <QImage>
00039 # include <QString>
00040 #endif
00041 
00042 #include "ViewProviderImagePlane.h"
00043 
00044 #include <Mod/Image/App/ImagePlane.h>
00045 #include <App/Document.h>
00046 #include <Gui/BitmapFactory.h>
00047 #include <Base/FileInfo.h>
00048 #include <Base/Stream.h>
00049 #include <Base/Console.h>
00050 #include <sstream>
00051 
00052 using namespace Gui;
00053 using namespace ImageGui;
00054 using namespace Image;
00055 
00056 
00057 PROPERTY_SOURCE(ImageGui::ViewProviderImagePlane, Gui::ViewProviderGeometryObject)
00058 
00059 ViewProviderImagePlane::ViewProviderImagePlane()
00060 {
00061     texture = new SoTexture2;
00062     texture->ref();
00063 
00064     pcCoords = new SoCoordinate3();
00065     pcCoords->ref();
00066 }
00067 
00068 ViewProviderImagePlane::~ViewProviderImagePlane()
00069 {
00070     pcCoords->unref();
00071     texture->unref();
00072 }
00073 
00074 void ViewProviderImagePlane::attach(App::DocumentObject *pcObj)
00075 {
00076     ViewProviderDocumentObject::attach(pcObj);
00077 
00078     // NOTE: SoFCSelection node has beem removed because it led to
00079     // problems using the image as a construction plan with the
00080     // draft commands
00081     SoSeparator* planesep = new SoSeparator;
00082     planesep->addChild(pcCoords);
00083 
00084     SoTextureCoordinate2 *textCoord = new SoTextureCoordinate2;
00085     textCoord->point.set1Value(0,0,0);
00086     textCoord->point.set1Value(1,1,0);
00087     textCoord->point.set1Value(2,1,1);
00088     textCoord->point.set1Value(3,0,1);
00089     planesep->addChild(textCoord);
00090 
00091     // texture
00092     texture->model = SoTexture2::MODULATE;
00093     planesep->addChild(texture);
00094 
00095     // plane
00096     pcCoords->point.set1Value(0,0,0,0);
00097     pcCoords->point.set1Value(1,1,0,0);
00098     pcCoords->point.set1Value(2,1,1,0);
00099     pcCoords->point.set1Value(3,0,1,0);
00100     SoFaceSet *faceset = new SoFaceSet;
00101     faceset->numVertices.set1Value(0,4);
00102     planesep->addChild(faceset);
00103 
00104     addDisplayMaskMode(planesep, "ImagePlane");
00105 }
00106 
00107 void ViewProviderImagePlane::setDisplayMode(const char* ModeName)
00108 {
00109     if (strcmp("ImagePlane",ModeName) == 0)
00110         setDisplayMaskMode("ImagePlane");
00111     ViewProviderGeometryObject::setDisplayMode(ModeName);
00112 }
00113 
00114 std::vector<std::string> ViewProviderImagePlane::getDisplayModes(void) const
00115 {
00116     std::vector<std::string> StrList;
00117     StrList.push_back("ImagePlane");
00118     return StrList;
00119 }
00120 
00121 bool ViewProviderImagePlane::loadSvg(const char* filename, float x, float y, QImage& img)
00122 {
00123     QFileInfo fi(QString::fromUtf8(filename));
00124     if (fi.suffix().toLower() == QLatin1String("svg")) {
00125         QPixmap px = BitmapFactory().pixmapFromSvg(filename, QSize((int)x,(int)y));
00126         img = px.toImage();
00127         return true;
00128     }
00129 
00130     return false;
00131 }
00132 
00133 void ViewProviderImagePlane::updateData(const App::Property* prop)
00134 {
00135     Image::ImagePlane* pcPlaneObj = static_cast<Image::ImagePlane*>(pcObject);
00136     if (prop == &pcPlaneObj->XSize || prop == &pcPlaneObj->YSize) {
00137         float x = pcPlaneObj->XSize.getValue();
00138         float y = pcPlaneObj->YSize.getValue();
00139 
00140         //pcCoords->point.setNum(4);
00141         pcCoords->point.set1Value(0,-(x/2),-(y/2),0.0);
00142         pcCoords->point.set1Value(1,+(x/2),-(y/2),0.0);
00143         pcCoords->point.set1Value(2,+(x/2),+(y/2),0.0);
00144         pcCoords->point.set1Value(3,-(x/2),+(y/2),0.0);
00145 
00146         QImage impQ;
00147         loadSvg(pcPlaneObj->ImageFile.getValue(), x, y, impQ);
00148         if (!impQ.isNull()) {
00149             SoSFImage img;
00150             // convert to Coin bitmap
00151             BitmapFactory().convert(impQ,img);
00152             texture->image = img;
00153         }
00154     }
00155     else if (prop == &pcPlaneObj->ImageFile) {
00156         float x = pcPlaneObj->XSize.getValue();
00157         float y = pcPlaneObj->YSize.getValue();
00158         QImage impQ;
00159         if (!loadSvg(pcPlaneObj->ImageFile.getValue(),x,y, impQ))
00160             impQ.load(QString::fromUtf8(pcPlaneObj->ImageFile.getValue()));
00161         if (!impQ.isNull()) {
00162             SoSFImage img;
00163             // convert to Coin bitmap
00164             BitmapFactory().convert(impQ,img);
00165             texture->image = img;
00166         }
00167     }
00168 }

Generated on Wed Nov 23 19:00:59 2011 for FreeCAD by  doxygen 1.6.1