changeset 883:30268a0cafca am-dev

basic scene Qt sample working
author Alain Mazy <alain@mazy.be>
date Tue, 09 Jul 2019 14:34:56 +0200
parents 31319fe867b9
children aad5ccf1be10
files Samples/Qt/BasicScene.cpp Samples/Qt/QStoneOpenGlWidget.cpp Samples/Qt/QStoneOpenGlWidget.h Samples/Qt/Scene2DInteractor.cpp Samples/Qt/Scene2DInteractor.h
diffstat 5 files changed, 136 insertions(+), 270 deletions(-) [+]
line wrap: on
line diff
--- a/Samples/Qt/BasicScene.cpp	Tue Jul 09 11:46:43 2019 +0200
+++ b/Samples/Qt/BasicScene.cpp	Tue Jul 09 14:34:56 2019 +0200
@@ -146,123 +146,6 @@
 }
 
 
-//void TakeScreenshot(const std::string& target,
-//                    const Scene2D& scene,
-//                    unsigned int canvasWidth,
-//                    unsigned int canvasHeight)
-//{
-//  // Take a screenshot, then save it as PNG file
-//  CairoCompositor compositor(scene, canvasWidth, canvasHeight);
-//  compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, FONT_SIZE, Orthanc::Encoding_Latin1);
-//  compositor.Refresh();
-
-//  Orthanc::ImageAccessor canvas;
-//  compositor.GetCanvas().GetReadOnlyAccessor(canvas);
-
-//  Orthanc::Image png(Orthanc::PixelFormat_RGB24, canvas.GetWidth(), canvas.GetHeight(), false);
-//  Orthanc::ImageProcessing::Convert(png, canvas);
-
-//  Orthanc::PngWriter writer;
-//  writer.WriteToFile(target, png);
-//}
-
-
-//void HandleApplicationEvent(ViewportControllerPtr controller,
-//                            const OpenGLCompositor& compositor,
-//                            const SDL_Event& event,
-//                            FlexiblePointerTrackerPtr& activeTracker)
-//{
-//  Scene2D& scene(*controller->GetScene());
-//  if (event.type == SDL_MOUSEMOTION)
-//  {
-//    int scancodeCount = 0;
-//    const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount);
-
-//    if (activeTracker.get() == NULL &&
-//        SDL_SCANCODE_LCTRL < scancodeCount &&
-//        keyboardState[SDL_SCANCODE_LCTRL])
-//    {
-//      // The "left-ctrl" key is down, while no tracker is present
-
-//      PointerEvent e;
-//      e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
-
-//      ScenePoint2D p = e.GetMainPosition().Apply(scene.GetCanvasToSceneTransform());
-
-//      char buf[64];
-//      sprintf(buf, "(%0.02f,%0.02f)", p.GetX(), p.GetY());
-
-//      if (scene.HasLayer(LAYER_POSITION))
-//      {
-//        TextSceneLayer& layer =
-//          dynamic_cast<TextSceneLayer&>(scene.GetLayer(LAYER_POSITION));
-//        layer.SetText(buf);
-//        layer.SetPosition(p.GetX(), p.GetY());
-//      }
-//      else
-//      {
-//        std::auto_ptr<TextSceneLayer>
-//          layer(new TextSceneLayer);
-//        layer->SetColor(0, 255, 0);
-//        layer->SetText(buf);
-//        layer->SetBorder(20);
-//        layer->SetAnchor(BitmapAnchor_BottomCenter);
-//        layer->SetPosition(p.GetX(), p.GetY());
-//        scene.SetLayer(LAYER_POSITION, layer.release());
-//      }
-//    }
-//    else
-//    {
-//      scene.DeleteLayer(LAYER_POSITION);
-//    }
-//  }
-//  else if (event.type == SDL_MOUSEBUTTONDOWN)
-//  {
-//    PointerEvent e;
-//    e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
-
-//    switch (event.button.button)
-//    {
-//      case SDL_BUTTON_MIDDLE:
-//        activeTracker = boost::make_shared<PanSceneTracker>(controller, e);
-//        break;
-
-//      case SDL_BUTTON_RIGHT:
-//        activeTracker = boost::make_shared<ZoomSceneTracker>(controller,
-//          e, compositor.GetCanvasHeight());
-//        break;
-
-//      case SDL_BUTTON_LEFT:
-//        activeTracker = boost::make_shared<RotateSceneTracker>(controller, e);
-//        break;
-
-//      default:
-//        break;
-//    }
-//  }
-//  else if (event.type == SDL_KEYDOWN &&
-//           event.key.repeat == 0 /* Ignore key bounce */)
-//  {
-//    switch (event.key.keysym.sym)
-//    {
-//      case SDLK_s:
-//        controller->FitContent(compositor.GetCanvasWidth(),
-//                         compositor.GetCanvasHeight());
-//        break;
-
-//      case SDLK_c:
-//        TakeScreenshot("screenshot.png", scene,
-//                       compositor.GetCanvasWidth(),
-//                       compositor.GetCanvasHeight());
-//        break;
-
-//      default:
-//        break;
-//    }
-//  }
-//}
-
-
 static void GLAPIENTRY OpenGLMessageCallback(GLenum source,
                                              GLenum type,
                                              GLuint id,
@@ -279,89 +162,6 @@
   }
 }
 
-
-//void Run(ViewportControllerPtr controller)
-//{
-//  SdlOpenGLWindow window("Hello", 1024, 768);
-
-//  controller->FitContent(window.GetCanvasWidth(), window.GetCanvasHeight());
-
-//  glEnable(GL_DEBUG_OUTPUT);
-//  glDebugMessageCallback(OpenGLMessageCallback, 0);
-
-//  OpenGLCompositor compositor(window, *controller->GetScene());
-//  compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
-//                     FONT_SIZE, Orthanc::Encoding_Latin1);
-
-//  FlexiblePointerTrackerPtr tracker;
-
-//  bool stop = false;
-//  while (!stop)
-//  {
-//    compositor.Refresh();
-
-//    SDL_Event event;
-//    while (!stop &&
-//           SDL_PollEvent(&event))
-//    {
-//      if (event.type == SDL_QUIT)
-//      {
-//        stop = true;
-//        break;
-//      }
-//      else if (event.type == SDL_MOUSEMOTION)
-//      {
-//        if (tracker)
-//        {
-//          PointerEvent e;
-//          e.AddPosition(compositor.GetPixelCenterCoordinates(
-//            event.button.x, event.button.y));
-//          tracker->PointerMove(e);
-//        }
-//      }
-//      else if (event.type == SDL_MOUSEBUTTONUP)
-//      {
-//        if (tracker)
-//        {
-//          PointerEvent e;
-//          e.AddPosition(compositor.GetPixelCenterCoordinates(
-//            event.button.x, event.button.y));
-//          tracker->PointerUp(e);
-//          if(!tracker->IsAlive())
-//            tracker.reset();
-//        }
-//      }
-//      else if (event.type == SDL_WINDOWEVENT &&
-//               event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
-//      {
-//        tracker.reset();
-//        compositor.UpdateSize();
-//      }
-//      else if (event.type == SDL_KEYDOWN &&
-//               event.key.repeat == 0 /* Ignore key bounce */)
-//      {
-//        switch (event.key.keysym.sym)
-//        {
-//          case SDLK_f:
-//            window.GetWindow().ToggleMaximize();
-//            break;
-
-//          case SDLK_q:
-//            stop = true;
-//            break;
-
-//          default:
-//            break;
-//        }
-//      }
-
-//      HandleApplicationEvent(controller, compositor, event, tracker);
-//    }
-
-//    SDL_Delay(1);
-//  }
-//}
-
 extern void InitGL();
 
 #include <QApplication>
@@ -397,67 +197,9 @@
     compositor->SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
                        FONT_SIZE, Orthanc::Encoding_Latin1);
 
+    interactor->SetCompositor(compositor);
     window.GetOpenGlWidget().SetCompositor(compositor);
 
     return a.exec();
   }
-
-
-
-
-
-
-
-
-
-
-
-
-//  StoneInitialize();
-//  Orthanc::Logging::EnableInfoLevel(true);
-
-//  QApplication app(argc, argv);
-
-//  OrthancStone::Samples::BasicSceneWindow window;
-
-//  QSurfaceFormat requestedFormat;
-//  requestedFormat.setVersion( 3, 3 );
-
-//  window.show();
-
-//  QOpenGLContext * context = new QOpenGLContext;
-//  context->setFormat( requestedFormat );
-//  context->create();
-
-//  GLenum err = glewInit();
-//  if( GLEW_OK != err ){
-//    qDebug() << "[Error] GLEW failed to initialize. " << (const char*)glewGetErrorString(err);
-//  }
-
-//  try
-//  {
-//    MessageBroker broker;
-//    ViewportControllerPtr controller = boost::make_shared<ViewportController>(
-//          boost::ref(broker));
-//    PrepareScene(controller);
-
-//    boost::shared_ptr<OpenGLCompositor> compositor(new OpenGLCompositor(window.GetOpenGlWidget(), *controller->GetScene()));
-
-//    compositor->SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
-//                       FONT_SIZE, Orthanc::Encoding_Latin1);
-
-//    window.SetCompositor(compositor);
-
-//    app.exec();
-//  }
-//  catch (Orthanc::OrthancException& e)
-//  {
-//    LOG(ERROR) << "EXCEPTION: " << e.What();
-//  }
-
-
-
-//  StoneFinalize();
-
-//  return 0;
 }
--- a/Samples/Qt/QStoneOpenGlWidget.cpp	Tue Jul 09 11:46:43 2019 +0200
+++ b/Samples/Qt/QStoneOpenGlWidget.cpp	Tue Jul 09 14:34:56 2019 +0200
@@ -60,7 +60,6 @@
   {
     guiEvent.altKey = true;
   }
-
 }
 
 void QStoneOpenGlWidget::mouseEvent(QMouseEvent* qtEvent, OrthancStone::GuiAdapterHidEventType guiEventType)
@@ -86,10 +85,84 @@
 
 void QStoneOpenGlWidget::mouseMoveEvent(QMouseEvent* qtEvent)
 {
-  mouseEvent(qtEvent, GUIADAPTER_EVENT_MOUSEDOWN);
+  mouseEvent(qtEvent, GUIADAPTER_EVENT_MOUSEMOVE);
 }
 
 void QStoneOpenGlWidget::mouseReleaseEvent(QMouseEvent* qtEvent)
 {
   mouseEvent(qtEvent, GUIADAPTER_EVENT_MOUSEUP);
 }
+
+void ConvertFromPlatform(
+    OrthancStone::GuiAdapterKeyboardEvent& guiEvent,
+    const QKeyEvent& qtEvent)
+{
+  if (qtEvent.text().length() > 0)
+  {
+    guiEvent.sym[0] = qtEvent.text()[0].cell();
+  }
+  else
+  {
+    guiEvent.sym[0] = 0;
+  }
+  guiEvent.sym[1] = 0;
+
+  if (qtEvent.modifiers().testFlag(Qt::ShiftModifier))
+  {
+    guiEvent.shiftKey = true;
+  }
+  if (qtEvent.modifiers().testFlag(Qt::ControlModifier))
+  {
+    guiEvent.ctrlKey = true;
+  }
+  if (qtEvent.modifiers().testFlag(Qt::AltModifier))
+  {
+    guiEvent.altKey = true;
+  }
+
+}
+
+
+bool QStoneOpenGlWidget::keyEvent(QKeyEvent* qtEvent, OrthancStone::GuiAdapterHidEventType guiEventType)
+{
+  bool handled = false;
+  OrthancStone::GuiAdapterKeyboardEvent guiEvent;
+  ConvertFromPlatform(guiEvent, *qtEvent);
+  guiEvent.type = guiEventType;
+
+  if (sceneInteractor_.get() != NULL && compositor_.get() != NULL)
+  {
+    handled = sceneInteractor_->OnKeyboardEvent(guiEvent);
+
+    if (handled)
+    {
+      // force redraw of the OpenGL widget
+      update();
+    }
+  }
+  return handled;
+}
+
+void QStoneOpenGlWidget::keyPressEvent(QKeyEvent *qtEvent)
+{
+  bool handled = keyEvent(qtEvent, GUIADAPTER_EVENT_KEYDOWN);
+  if (!handled)
+  {
+    QOpenGLWidget::keyPressEvent(qtEvent);
+  }
+}
+
+void QStoneOpenGlWidget::keyReleaseEvent(QKeyEvent *qtEvent)
+{
+  bool handled = keyEvent(qtEvent, GUIADAPTER_EVENT_KEYUP);
+  if (!handled)
+  {
+    QOpenGLWidget::keyPressEvent(qtEvent);
+  }
+}
+
+void QStoneOpenGlWidget::wheelEvent(QWheelEvent *qtEvent)
+{
+  OrthancStone::GuiAdapterWheelEvent guiEvent;
+  throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
+}
--- a/Samples/Qt/QStoneOpenGlWidget.h	Tue Jul 09 11:46:43 2019 +0200
+++ b/Samples/Qt/QStoneOpenGlWidget.h	Tue Jul 09 14:34:56 2019 +0200
@@ -19,6 +19,7 @@
     QStoneOpenGlWidget(QWidget *parent) :
       QOpenGLWidget(parent)
     {
+      setFocusPolicy(Qt::StrongFocus);
     }
 
   protected:
@@ -31,6 +32,9 @@
     void mousePressEvent(QMouseEvent* event) override;
     void mouseMoveEvent(QMouseEvent* event) override;
     void mouseReleaseEvent(QMouseEvent* event) override;
+    void keyPressEvent(QKeyEvent* event) override;
+    void keyReleaseEvent(QKeyEvent *event) override;
+    void wheelEvent(QWheelEvent* event) override;
 
     //**** IOpenGLContext overrides
 
@@ -60,6 +64,7 @@
 
   protected:
     void mouseEvent(QMouseEvent* qtEvent, OrthancStone::GuiAdapterHidEventType guiEventType);
+    bool keyEvent(QKeyEvent* qtEvent, OrthancStone::GuiAdapterHidEventType guiEventType);
 
   };
 }
--- a/Samples/Qt/Scene2DInteractor.cpp	Tue Jul 09 11:46:43 2019 +0200
+++ b/Samples/Qt/Scene2DInteractor.cpp	Tue Jul 09 14:34:56 2019 +0200
@@ -12,11 +12,26 @@
 using namespace OrthancStone;
 
 
-void BasicScene2DInteractor::OnMouseEvent(const GuiAdapterMouseEvent& event, const PointerEvent& pointerEvent)
+bool BasicScene2DInteractor::OnMouseEvent(const GuiAdapterMouseEvent& event, const PointerEvent& pointerEvent)
 {
   if (currentTracker_.get() != NULL)
   {
-    currentTracker_->PointerMove(pointerEvent);
+    switch (event.type)
+    {
+    case GUIADAPTER_EVENT_MOUSEUP:
+    {
+      currentTracker_->PointerUp(pointerEvent);
+      if (!currentTracker_->IsAlive())
+      {
+        currentTracker_.reset();
+      }
+    };break;
+    case GUIADAPTER_EVENT_MOUSEMOVE:
+    {
+      currentTracker_->PointerMove(pointerEvent);
+    };break;
+    }
+    return true;
   }
   else
   {
@@ -27,12 +42,29 @@
     {
       currentTracker_.reset(new PanSceneTracker(viewportController_, pointerEvent));
     }
-    else if (event.button == GUIADAPTER_MOUSEBUTTON_RIGHT)
+    else if (event.button == GUIADAPTER_MOUSEBUTTON_RIGHT && compositor_.get() != NULL)
     {
-     // TODO: need a pointer to compositor currentTracker_.reset(new ZoomSceneTracker(viewportController_, pointerEvent, viewportController_->));
+      currentTracker_.reset(new ZoomSceneTracker(viewportController_, pointerEvent, compositor_->GetHeight()));
     }
+    return true;
   }
-
+  return false;
 }
 
+bool BasicScene2DInteractor::OnKeyboardEvent(const GuiAdapterKeyboardEvent& guiEvent)
+{
+  switch (guiEvent.sym[0])
+  {
+  case 's':
+  {
+    viewportController_->FitContent(compositor_->GetWidth(), compositor_->GetHeight());
+    return true;
+  };
+  }
+  return false;
+}
 
+bool BasicScene2DInteractor::OnWheelEvent(const GuiAdapterWheelEvent& guiEvent)
+{
+  return false;
+}
--- a/Samples/Qt/Scene2DInteractor.h	Tue Jul 09 11:46:43 2019 +0200
+++ b/Samples/Qt/Scene2DInteractor.h	Tue Jul 09 14:34:56 2019 +0200
@@ -3,6 +3,7 @@
 #include "../../Framework/Scene2D/PointerEvent.h"
 #include "../../Framework/Scene2DViewport/ViewportController.h"
 #include "../../Framework/Scene2DViewport/IFlexiblePointerTracker.h"
+#include "../../Framework/Scene2D/Internals/CompositorHelper.h"
 #include "../../Applications/Generic/GuiAdapter.h"
 
 
@@ -12,19 +13,30 @@
   class Scene2DInteractor
   {
   protected:
-    boost::shared_ptr<ViewportController> viewportController_;
-    boost::shared_ptr<IFlexiblePointerTracker> currentTracker_;
+    boost::shared_ptr<ViewportController>       viewportController_;
+    boost::shared_ptr<IFlexiblePointerTracker>  currentTracker_;
+    boost::shared_ptr<ICompositor>              compositor_;
 
   public:
     Scene2DInteractor(boost::shared_ptr<ViewportController> viewportController) :
       viewportController_(viewportController)
     {}
 
-    virtual void OnMouseEvent(const GuiAdapterMouseEvent& guiEvent, const PointerEvent& pointerEvent) = 0;
+    void SetCompositor(boost::shared_ptr<ICompositor> compositor)
+    {
+      compositor_ = compositor;
+    }
+
+    virtual bool OnMouseEvent(const GuiAdapterMouseEvent& guiEvent, const PointerEvent& pointerEvent) = 0; // returns true if it has handled the event
+    virtual bool OnKeyboardEvent(const GuiAdapterKeyboardEvent& guiEvent) = 0; // returns true if it has handled the event
+    virtual bool OnWheelEvent(const GuiAdapterWheelEvent& guiEvent) = 0; // returns true if it has handled the event
 
   };
 }
 
+
+
+
 class BasicScene2DInteractor : public OrthancStone::Scene2DInteractor
 {
 public:
@@ -32,6 +44,8 @@
     Scene2DInteractor(viewportController)
   {}
 
-  virtual void OnMouseEvent(const OrthancStone::GuiAdapterMouseEvent& event, const OrthancStone::PointerEvent& pointerEvent) override;
+  virtual bool OnMouseEvent(const OrthancStone::GuiAdapterMouseEvent& event, const OrthancStone::PointerEvent& pointerEvent) override;
+  virtual bool OnKeyboardEvent(const OrthancStone::GuiAdapterKeyboardEvent& guiEvent);
+  virtual bool OnWheelEvent(const OrthancStone::GuiAdapterWheelEvent& guiEvent);
 };