Mod/Sandbox/Gui/Workbench.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2009 Werner Mayer <wmayer@users.sourceforge.net>        *
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 <QGLWidget>
00028 # include <Inventor/actions/SoAction.h>
00029 # include <Inventor/elements/SoModelMatrixElement.h>
00030 # include <Inventor/elements/SoViewVolumeElement.h>
00031 # include <Inventor/elements/SoViewportRegionElement.h>
00032 # include <Inventor/SoPrimitiveVertex.h>
00033 # include <Inventor/SbLinear.h>
00034 #endif
00035 
00036 #include "Workbench.h"
00037 #include <Gui/MenuManager.h>
00038 #include <Gui/ToolBarManager.h>
00039 
00040 using namespace SandboxGui;
00041 
00043 TYPESYSTEM_SOURCE(SandboxGui::Workbench, Gui::StdWorkbench)
00044 
00045 Workbench::Workbench()
00046 {
00047 }
00048 
00049 Workbench::~Workbench()
00050 {
00051 }
00052 
00053 Gui::MenuItem* Workbench::setupMenuBar() const
00054 {
00055     Gui::MenuItem* root = StdWorkbench::setupMenuBar();
00056     Gui::MenuItem* item = root->findItem("&Windows");
00057     Gui::MenuItem* test = new Gui::MenuItem;
00058     root->insertItem(item, test);
00059     Gui::MenuItem* threads = new Gui::MenuItem;
00060     threads->setCommand("Python Threads");
00061     *threads << "Sandbox_PythonLockThread" << "Sandbox_NolockPython"
00062              << "Sandbox_PyQtThread" << "Sandbox_PythonThread" << "Sandbox_PythonMainThread";
00063     test->setCommand("Threads");
00064     *test << "Sandbox_Thread" << "Sandbox_TestThread" << "Sandbox_WorkerThread" << "Sandbox_SeqThread"
00065           << "Sandbox_BlockThread" << "Sandbox_NoThread" << threads << "Separator"
00066           << "Sandbox_Dialog" << "Sandbox_FileDialog";
00067     Gui::MenuItem* misc = new Gui::MenuItem;
00068     root->insertItem(item, misc);
00069     misc->setCommand("Misc");
00070     *misc << "Sandbox_EventLoop" << "Sandbox_MeshLoad"
00071           << "Sandbox_MeshLoaderBoost"
00072           << "Sandbox_MeshLoaderFuture"
00073           << "Sandbox_MeshTestJob"
00074           << "Sandbox_MeshTestRef"
00075           << "Sandbox_CryptographicHash";
00076 
00077     Gui::MenuItem* widg = new Gui::MenuItem;
00078     root->insertItem(item, widg);
00079     widg->setCommand("Widgets");
00080     *widg << "Std_GrabWidget"
00081           << "Std_ImageNode"
00082           << "Sandbox_WidgetShape"
00083           << "Sandbox_GDIWidget"
00084           << "Sandbox_RedirectPaint";
00085 
00086     Gui::MenuItem* viewer = new Gui::MenuItem;
00087     root->insertItem(item, viewer);
00088     viewer->setCommand("Viewer");
00089     *viewer << "Sandbox_PlaneViewer"
00090             << "Sandbox_ExaminerViewer"
00091             << "Sandbox_FlyViewer";
00092     return root;
00093 }
00094 
00095 Gui::ToolBarItem* Workbench::setupToolBars() const
00096 {
00097     Gui::ToolBarItem* root = StdWorkbench::setupToolBars();
00098     Gui::ToolBarItem* test = new Gui::ToolBarItem(root);
00099     test->setCommand( "Sandbox Tools" );
00100     *test << "Sandbox_Thread" << "Sandbox_WorkerThread" << "Sandbox_SeqThread"
00101           << "Sandbox_BlockThread" << "Sandbox_NoThread"
00102           << "Sandbox_Dialog" << "Sandbox_FileDialog"; 
00103     return root;
00104 }
00105 
00106 Gui::ToolBarItem* Workbench::setupCommandBars() const
00107 {
00108     return 0;
00109 }
00110 
00111 // ----------------------------------------------------
00112 
00113 
00114 SO_NODE_SOURCE(SoWidgetShape);
00115 
00116 void SoWidgetShape::initClass()
00117 {
00118     SO_NODE_INIT_CLASS(SoWidgetShape, SoShape, "Shape");
00119 }
00120 
00121 SoWidgetShape::SoWidgetShape()
00122 {
00123     SO_NODE_CONSTRUCTOR(SoWidgetShape);
00124 }
00125 
00126 void SoWidgetShape::GLRender(SoGLRenderAction *action)
00127 {
00128 #if 1
00129     this->image = QPixmap::grabWidget(w, w->rect()).toImage();
00130     this->image = QGLWidget::convertToGLFormat(this->image);
00131 #endif
00132     glRasterPos2d(10,10);
00133     glDrawPixels(this->image.width(),this->image.height(),GL_RGBA,GL_UNSIGNED_BYTE,this->image.bits());
00134 }
00135 
00136 void SoWidgetShape::computeBBox(SoAction *action, SbBox3f &box, SbVec3f &center)
00137 {
00138     // ignore if node is empty
00139     if (this->image.isNull()) return;
00140 
00141     SbVec3f v0, v1, v2, v3;
00142     // this will cause a cache dependency on the view volume,
00143     // model matrix and viewport.
00144     this->getQuad(action->getState(), v0, v1, v2, v3);
00145 
00146     box.makeEmpty();
00147     box.extendBy(v0);
00148     box.extendBy(v1);
00149     box.extendBy(v2);
00150     box.extendBy(v3);
00151     center = box.getCenter();
00152 }
00153 
00154 // Calculates the quad in 3D.
00155 void
00156 SoWidgetShape::getQuad(SoState * state, SbVec3f & v0, SbVec3f & v1,
00157                        SbVec3f & v2, SbVec3f & v3)
00158 {
00159     SbVec3f nilpoint(0.0f, 0.0f, 0.0f);
00160     const SbMatrix & mat = SoModelMatrixElement::get(state);
00161     mat.multVecMatrix(nilpoint, nilpoint);
00162 
00163     const SbViewVolume &vv = SoViewVolumeElement::get(state);
00164 
00165     SbVec3f screenpoint;
00166     vv.projectToScreen(nilpoint, screenpoint);
00167 
00168     const SbViewportRegion & vp = SoViewportRegionElement::get(state);
00169     SbVec2s vpsize = vp.getViewportSizePixels();
00170 
00171     // find normalized width and height of image
00172     float nw = (float)this->image.width();
00173     nw /= (float)vpsize[0];
00174     float nh = (float)this->image.height();
00175     nh /= (float)vpsize[1];
00176 
00177     // need only half the width
00178     nw *= 0.5f;
00179     nh *= 0.5f;
00180 
00181     SbVec2f n0, n1, n2, n3;
00182 
00183     n0 = SbVec2f(screenpoint[0]-nw, screenpoint[1]-nh);
00184     n1 = SbVec2f(screenpoint[0]+nw, screenpoint[1]-nh);
00185     n2 = SbVec2f(screenpoint[0]+nw, screenpoint[1]+nh);
00186     n3 = SbVec2f(screenpoint[0]-nw, screenpoint[1]+nh);
00187 
00188     // get distance from nilpoint to camera plane
00189     float dist = -vv.getPlane(0.0f).getDistance(nilpoint);
00190 
00191     // find the four image points in the plane
00192     v0 = vv.getPlanePoint(dist, n0);
00193     v1 = vv.getPlanePoint(dist, n1);
00194     v2 = vv.getPlanePoint(dist, n2);
00195     v3 = vv.getPlanePoint(dist, n3);
00196 
00197     // transform back to object space
00198     SbMatrix inv = mat.inverse();
00199     inv.multVecMatrix(v0, v0);
00200     inv.multVecMatrix(v1, v1);
00201     inv.multVecMatrix(v2, v2);
00202     inv.multVecMatrix(v3, v3);
00203 }
00204 
00205 void SoWidgetShape::generatePrimitives(SoAction *action)
00206 {
00207     if (this->image.isNull()) return;
00208 
00209     SoState *state = action->getState();
00210     state->push();
00211 
00212     SbVec2s size;
00213     SbVec3f v0, v1, v2, v3;
00214     this->getQuad(action->getState(), v0, v1, v2, v3);
00215 
00216     SbVec3f n = (v1-v0).cross(v2-v0);
00217     n.normalize();
00218 
00219     this->beginShape(action, SoShape::QUADS);
00220     SoPrimitiveVertex vertex;
00221     vertex.setNormal(n);
00222 
00223     vertex.setTextureCoords(SbVec2f(0,0));
00224     vertex.setPoint(v0);
00225     this->shapeVertex(&vertex);
00226 
00227     vertex.setTextureCoords(SbVec2f(1,0));
00228     vertex.setPoint(v1);
00229     this->shapeVertex(&vertex);
00230 
00231     vertex.setTextureCoords(SbVec2f(1,1));
00232     vertex.setPoint(v2);
00233     this->shapeVertex(&vertex);
00234 
00235     vertex.setTextureCoords(SbVec2f(0,1));
00236     vertex.setPoint(v3);
00237     this->shapeVertex(&vertex);
00238 
00239     this->endShape();
00240 
00241     state->pop();
00242 }
00243 
00244 void SoWidgetShape::setWidget(QWidget* w)
00245 {
00246     this->w = w;
00247     this->w->show();
00248     QPainter::setRedirected(this->w, &this->image);
00249     this->image = QPixmap::grabWidget(w, w->rect()).toImage();
00250     this->image = QGLWidget::convertToGLFormat(this->image);
00251 }

Generated on Wed Nov 23 19:01:11 2011 for FreeCAD by  doxygen 1.6.1