view OrthancStone/Resources/Graveyard/Deprecated/Samples/Qt/QStoneOpenGlWidget.cpp @ 1527:4c4b267e4004

RtViewerPlugin : similar to the StoneWebPlugin, but for the sole RtViewer sample
author Benjamin Golinvaux <bgo@osimis.io>
date Sun, 02 Aug 2020 15:13:58 +0200
parents 244ad1e4e76a
children
line wrap: on
line source

/**
 * Stone of Orthanc
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017-2020 Osimis S.A., Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "../../Framework/OpenGL/OpenGLIncludes.h"
#include "QStoneOpenGlWidget.h"

#include <QMouseEvent>

using namespace OrthancStone;

void QStoneOpenGlWidget::initializeGL()
{
  glewInit();
}

void QStoneOpenGlWidget::MakeCurrent()
{
  this->makeCurrent();
}

void QStoneOpenGlWidget::resizeGL(int w, int h)
{

}

void QStoneOpenGlWidget::paintGL()
{
  if (compositor_)
  {
    compositor_->Refresh();
  }
  doneCurrent();
}

void ConvertFromPlatform(
    OrthancStone::GuiAdapterMouseEvent& guiEvent,
    PointerEvent& pointerEvent,
    const QMouseEvent& qtEvent,
    const IViewport& viewport)
{
  guiEvent.targetX = qtEvent.x();
  guiEvent.targetY = qtEvent.y();
  pointerEvent.AddPosition(viewport.GetPixelCenterCoordinates(guiEvent.targetX, guiEvent.targetY));

  switch (qtEvent.button())
  {
  case Qt::LeftButton: guiEvent.button = OrthancStone::GUIADAPTER_MOUSEBUTTON_LEFT; break;
  case Qt::MiddleButton: guiEvent.button = OrthancStone::GUIADAPTER_MOUSEBUTTON_MIDDLE; break;
  case Qt::RightButton: guiEvent.button = OrthancStone::GUIADAPTER_MOUSEBUTTON_RIGHT; break;
  default:
    guiEvent.button = OrthancStone::GUIADAPTER_MOUSEBUTTON_LEFT;
  }

  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;
  }
}

void QStoneOpenGlWidget::mouseEvent(QMouseEvent* qtEvent, OrthancStone::GuiAdapterHidEventType guiEventType)
{
  OrthancStone::GuiAdapterMouseEvent guiEvent;
  PointerEvent pointerEvent;
  ConvertFromPlatform(guiEvent, pointerEvent, *qtEvent, *this);
  guiEvent.type = guiEventType;

  if (sceneInteractor_.get() != NULL && compositor_.get() != NULL)
  {
    sceneInteractor_->OnMouseEvent(guiEvent, pointerEvent);
  }

  // force redraw of the OpenGL widget
  update();
}

void QStoneOpenGlWidget::mousePressEvent(QMouseEvent* qtEvent)
{
  mouseEvent(qtEvent, GUIADAPTER_EVENT_MOUSEDOWN);
}

void QStoneOpenGlWidget::mouseMoveEvent(QMouseEvent* qtEvent)
{
  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);

  // force redraw of the OpenGL widget
  update();
}