glc_3dwidgetmanagerhandle.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  Version 2.0.0 Beta 1, packaged on April 2010.
00006 
00007  http://glc-lib.sourceforge.net
00008 
00009  GLC-lib is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU General Public License as published by
00011  the Free Software Foundation; either version 2 of the License, or
00012  (at your option) any later version.
00013 
00014  GLC-lib is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU General Public License for more details.
00018 
00019  You should have received a copy of the GNU General Public License
00020  along with GLC-lib; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023  *****************************************************************************/
00025 
00026 #include "glc_3dwidgetmanagerhandle.h"
00027 
00028 #include "../viewport/glc_viewport.h"
00029 #include "../sceneGraph/glc_3dviewinstance.h"
00030 #include "glc_3dwidget.h"
00031 #include <QMouseEvent>
00032 
00033 GLC_3DWidgetManagerHandle::GLC_3DWidgetManagerHandle(GLC_Viewport* pViewport)
00034 : m_Collection()
00035 , m_Count(1)
00036 , m_3DWidgetHash()
00037 , m_MapBetweenInstanceWidget()
00038 , m_pViewport(pViewport)
00039 , m_Active3DWidgetId(0)
00040 , m_Preselected3DWidgetId(0)
00041 , m_PreviousViewMatrix(pViewport->compositionMatrix())
00042 {
00043 
00044 }
00045 
00046 GLC_3DWidgetManagerHandle::~GLC_3DWidgetManagerHandle()
00047 {
00048         QHash<GLC_uint, GLC_3DWidget*>::iterator iWidget= m_3DWidgetHash.begin();
00049         while (m_3DWidgetHash.constEnd() != iWidget)
00050         {
00051                 delete iWidget.value();
00052                 ++iWidget;
00053         }
00054 }
00055 
00056 void GLC_3DWidgetManagerHandle::add3DWidget(GLC_3DWidget* p3DWidget)
00057 {
00058         Q_ASSERT(!m_MapBetweenInstanceWidget.contains(p3DWidget->id()));
00059         m_3DWidgetHash.insert(p3DWidget->id(), p3DWidget);
00060         p3DWidget->setWidgetManager(this);
00061 }
00062 
00063 void GLC_3DWidgetManagerHandle::remove3DWidget(GLC_uint id)
00064 {
00065         Q_ASSERT(m_3DWidgetHash.contains(id));
00066         delete m_3DWidgetHash.take(id);
00067 }
00068 
00069 GLC_3DWidget* GLC_3DWidgetManagerHandle::take(GLC_uint id)
00070 {
00071         Q_ASSERT(m_3DWidgetHash.contains(id));
00072         return m_3DWidgetHash.take(id);
00073 }
00074 
00075 void GLC_3DWidgetManagerHandle::add3DViewInstance(const GLC_3DViewInstance& instance, GLC_uint widgetId)
00076 {
00077         const GLC_uint instanceId= instance.id();
00078         Q_ASSERT(!m_MapBetweenInstanceWidget.contains(instanceId));
00079         Q_ASSERT(!m_Collection.contains(instanceId));
00080 
00081         m_MapBetweenInstanceWidget.insert(instanceId, widgetId);
00082         m_Collection.add(instance, 0);
00083 }
00084 
00085 void GLC_3DWidgetManagerHandle::remove3DViewInstance(GLC_uint id)
00086 {
00087         Q_ASSERT(m_MapBetweenInstanceWidget.contains(id));
00088         Q_ASSERT(m_Collection.contains(id));
00089         m_Collection.remove(id);
00090         m_MapBetweenInstanceWidget.remove(id);
00091         if (m_Active3DWidgetId == id) m_Active3DWidgetId= 0;
00092 }
00093 
00094 void GLC_3DWidgetManagerHandle::clear()
00095 {
00096         m_Active3DWidgetId= 0;
00097         QHash<GLC_uint, GLC_3DWidget*>::iterator iWidget= m_3DWidgetHash.begin();
00098         while (m_3DWidgetHash.constEnd() != iWidget)
00099         {
00100                 delete iWidget.value();
00101                 ++iWidget;
00102         }
00103         m_3DWidgetHash.clear();
00104         m_Collection.clear();
00105         m_MapBetweenInstanceWidget.clear();
00106 }
00107 
00108 glc::WidgetEventFlag GLC_3DWidgetManagerHandle::mouseDoubleClickEvent(QMouseEvent * pEvent)
00109 {
00110 
00111         if (hasAnActiveWidget())
00112         {
00113 
00114         }
00115         return glc::IgnoreEvent;
00116 }
00117 
00118 glc::WidgetEventFlag GLC_3DWidgetManagerHandle::mouseMoveEvent(QMouseEvent * pEvent)
00119 {
00120         glc::WidgetEventFlag eventFlag= glc::IgnoreEvent;
00121         // Get the 3D cursor position and the id under
00122         QPair<GLC_uint, GLC_Point3d> cursorInfo= select(pEvent);
00123         const GLC_uint selectedId= cursorInfo.first;
00124         const GLC_Point3d pos(cursorInfo.second);
00125 
00126         if (hasAnActiveWidget())
00127         {
00128                 GLC_3DWidget* pActiveWidget= m_3DWidgetHash.value(m_Active3DWidgetId);
00129                 eventFlag= pActiveWidget->mouseMove(pos, pEvent->buttons(), selectedId);
00130         }
00131         else
00132         {
00133                 if (m_MapBetweenInstanceWidget.contains(selectedId))
00134                 {
00135                         const GLC_uint select3DWidgetId= m_MapBetweenInstanceWidget.value(selectedId);
00136 
00137                         if (m_Preselected3DWidgetId != select3DWidgetId)
00138                         {
00139                                 m_Preselected3DWidgetId= m_MapBetweenInstanceWidget.value(selectedId);
00140                                 GLC_3DWidget* pActiveWidget= m_3DWidgetHash.value(m_Preselected3DWidgetId);
00141                                 eventFlag= pActiveWidget->mouseOver(pos, selectedId);
00142                         }
00143                         else if (0 != m_Preselected3DWidgetId && (m_Preselected3DWidgetId != select3DWidgetId))
00144                         {
00145                                 eventFlag= m_3DWidgetHash.value(m_Preselected3DWidgetId)->unselect(pos, selectedId);
00146                         }
00147 
00148                 }
00149                 else if (0 != m_Preselected3DWidgetId)
00150                 {
00151                         eventFlag= m_3DWidgetHash.value(m_Preselected3DWidgetId)->unselect(pos, selectedId);
00152                         m_Preselected3DWidgetId= 0;
00153                 }
00154         }
00155         return eventFlag;
00156 }
00157 
00158 glc::WidgetEventFlag GLC_3DWidgetManagerHandle::mousePressEvent(QMouseEvent * pEvent)
00159 {
00160         glc::WidgetEventFlag eventFlag= glc::IgnoreEvent;
00161 
00162         if (pEvent->button() == Qt::LeftButton)
00163         {
00164                 // Get the 3D cursor position and the id under
00165                 QPair<GLC_uint, GLC_Point3d> cursorInfo= select(pEvent);
00166                 const GLC_uint selectedId= cursorInfo.first;
00167                 const GLC_Point3d pos(cursorInfo.second);
00168 
00169                 if (hasAnActiveWidget())
00170                 {
00171                         GLC_3DWidget* pActiveWidget= m_3DWidgetHash.value(m_Active3DWidgetId);
00172                         const bool activeWidgetUnderMouse= pActiveWidget->instanceBelongTo(selectedId);
00173                         if (activeWidgetUnderMouse)
00174                         {
00175                                 eventFlag= pActiveWidget->mousePressed(pos, pEvent->button(), selectedId);
00176                         }
00177                         else
00178                         {
00179                                 eventFlag= pActiveWidget->unselect(pos, selectedId);
00180                                 if (m_MapBetweenInstanceWidget.contains(selectedId))
00181                                 {
00182                                         m_Active3DWidgetId= m_MapBetweenInstanceWidget.value(selectedId);
00183                                         pActiveWidget= m_3DWidgetHash.value(m_Active3DWidgetId);
00184                                         eventFlag= pActiveWidget->select(pos, selectedId);
00185                                 }
00186                                 else
00187                                 {
00188                                         m_Active3DWidgetId= 0;
00189                                 }
00190                         }
00191 
00192                 }
00193                 else
00194                 {
00195                         if (m_MapBetweenInstanceWidget.contains(selectedId))
00196                         {
00197                                 m_Active3DWidgetId= m_MapBetweenInstanceWidget.value(selectedId);
00198                                 GLC_3DWidget* pActiveWidget= m_3DWidgetHash.value(m_Active3DWidgetId);
00199                                 eventFlag= pActiveWidget->select(pos, selectedId);
00200                         }
00201                 }
00202         }
00203 
00204         return eventFlag;
00205 }
00206 
00207 glc::WidgetEventFlag GLC_3DWidgetManagerHandle::mouseReleaseEvent(QMouseEvent * pEvent)
00208 {
00209         glc::WidgetEventFlag eventFlag= glc::IgnoreEvent;
00210         if (hasAnActiveWidget() && (pEvent->button() == Qt::LeftButton))
00211         {
00212 
00213                 GLC_3DWidget* pActiveWidget= m_3DWidgetHash.value(m_Active3DWidgetId);
00214 
00215                 eventFlag= pActiveWidget->mouseReleased(pEvent->button());
00216         }
00217         return eventFlag;
00218 }
00219 
00220 void GLC_3DWidgetManagerHandle::render()
00221 {
00223         if (m_pViewport->compositionMatrix() != m_PreviousViewMatrix)
00224         {
00225                 // Signal 3DWidget that the view as changed
00226                 QHash<GLC_uint, GLC_3DWidget*>::iterator iWidget= m_3DWidgetHash.begin();
00227                 while (m_3DWidgetHash.constEnd() != iWidget)
00228                 {
00229                         iWidget.value()->updateWidgetRep();
00230                         ++iWidget;
00231                 }
00232                 m_PreviousViewMatrix= m_pViewport->compositionMatrix();
00233         }
00234 
00235         // Render the 3D widget
00236         m_Collection.render(0, glc::WireRenderFlag);
00237         m_Collection.render(0, glc::TransparentRenderFlag);
00238         m_Collection.render(1, glc::WireRenderFlag);
00239         if (GLC_State::glslUsed())
00240         {
00241                 m_Collection.renderShaderGroup(glc::WireRenderFlag);
00242                 m_Collection.renderShaderGroup(glc::TransparentRenderFlag);
00243         }
00244 }
00245 
00246 QPair<GLC_uint, GLC_Point3d> GLC_3DWidgetManagerHandle::select(QMouseEvent* event)
00247 {
00248 
00249         GLC_uint selectionId= m_pViewport->selectOnPreviousRender(event->x(), event->y());
00250         const GLC_Point3d selectedPoint(m_pViewport->unProject(event->x(), event->y()));
00251 
00252         QPair<GLC_uint, GLC_Point3d> selection;
00253         selection.first= selectionId;
00254         selection.second= selectedPoint;
00255 
00256         return selection;
00257 }

SourceForge.net Logo

©2005 Laurent Ribon