view libgui/graphics/Container.cc @ 27303:07b330708e3c

use Qt signals for interpreter callbacks in Qt graphics toolkit * Backend.h, Backend.cc (Backend::m_interpreter): New data member. (Backend::createObject): Pass pointer to invoking Backend object in signal argument list. (Backend::interpreter_event): New slots. Access interpreter event_manager to post interpreter callbacks events here. * ObjectFactory.h, ObjectFactory.cc (ObjectFactory::createObject): New arg, backend. Connect interpreter_event signal from newly created object to interpreter_event slot in backend object. * __init_qt__.cc (__init__): Pass interpreter to Backend constructor. * module.mk (OCTAVE_GUI_GRAPHICS_MOC): New files, moc-Canvas.cc and moc-Container.cc. * Object.h (Object::interpreter_event): New signals. * Canvas.h, Canvas.cc (Canvas::interprter_event): New signals. (Canvas::canvasMouseReleaseEvent): Emit interpreter_event signal instead of accessing interpreter event_manager object directly here. * GLCanvas.cc (GLCanvas::do_print): Emit interpreter_event signal instead of accessing interpreter event_manager object directly here. * Container.h, Container.cc (Container::interpreter_event): New signals. (Container::canvas): Forward Canvas interpreter_event signals to Container interpreter_event signals. * ButtonGroup.cc (ButtonGroup::ButtonGroup) Forward Container interpreter_event signals to the ButtonGroup interpreter_event signal. Since ButtonGroup is derived from Object, this connection and the one made by ObjectFactory::createObject has the effect of passing the interpreter_event signal up to the Backend interpreter_event slot. * Figure.cc (Figure::Figure): Likewise, for Figure. * Panel.cc (Panel::Panel): Likewise, for Panel.
author John W. Eaton <jwe@octave.org>
date Mon, 29 Jul 2019 10:43:20 -0400
parents 7455523fdf01
children 6b2d20317b26
line wrap: on
line source

/*

Copyright (C) 2011-2019 Michael Goffioul

This file is part of Octave.

Octave is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Octave 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, see
<https://www.gnu.org/licenses/>.

*/

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <QChildEvent>
#include <QVBoxLayout>

#include "graphics.h"

#include "Canvas.h"
#include "Container.h"
#include "Object.h"
#include "QtHandlesUtils.h"

namespace QtHandles
{

  Container::Container (QWidget *xparent)
    : ContainerBase (xparent), m_canvas (nullptr)
  {
    setFocusPolicy (Qt::ClickFocus);
  }

  Container::~Container (void)
  { }

  Canvas*
  Container::canvas (const graphics_handle& gh, bool xcreate)
  {
    if (! m_canvas && xcreate)
      {
        gh_manager::auto_lock lock;
        graphics_object go = gh_manager::get_object (gh);

        if (go)
          {
            graphics_object fig = go.get_ancestor ("figure");

            m_canvas = Canvas::create (fig.get ("renderer").string_value (),
                                       this, gh);

            connect (m_canvas, SIGNAL (interpeter_event (const fcn_callback&)),
                     this, SIGNAL (interpeter_event (const fcn_callback&)));

            connect (m_canvas, SIGNAL (interpeter_event (const meth_callback&)),
                     this, SIGNAL (interpeter_event (const meth_callback&)));

            QWidget *canvasWidget = m_canvas->qWidget ();

            canvasWidget->lower ();
            canvasWidget->show ();
            canvasWidget->setGeometry (0, 0, width (), height ());
          }
      }

    return m_canvas;
  }

  void
  Container::resizeEvent (QResizeEvent* /* event */)
  {
    if (m_canvas)
      m_canvas->qWidget ()->setGeometry (0, 0, width (), height ());

    gh_manager::auto_lock lock;

    foreach (QObject *qObj, children ())
      {
        if (qObj->isWidgetType ())
          {
            Object *obj = Object::fromQObject (qObj);

            if (obj)
              {
                graphics_object go = obj->object ();

                if (go.valid_object ())
                  {
                    Matrix bb = go.get_properties ().get_boundingbox (false);

                    obj->qWidget<QWidget> ()->setGeometry
                      (octave::math::round (bb(0)),
                       octave::math::round (bb(1)),
                       octave::math::round (bb(2)),
                       octave::math::round (bb(3)));
                  }
              }
          }
      }
  }

  void
  Container::childEvent (QChildEvent *xevent)
  {
    // Enable mouse tracking in child widgets as they are added if the
    // container also has mouse tracking enabled.  There is no need to
    // do this when child objects are removed.

    if (xevent->added ())
      {
        QObject *obj = xevent->child ();

        if (obj && obj->isWidgetType ())
          {
            QWidget *widget = qobject_cast<QWidget *> (obj);

            if (widget)
              widget->setMouseTracking (hasMouseTracking ());
          }
      }
  }
}