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