Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) WtShapeWidget

 

WtQt CreatorLubuntu

 

WtShapeWidget is a Wt widget class to display an ShapeWidget.

Technical facts

 

 

 

 

 

 

./CppWtShapeWidget/CppWtShapeWidget.pri

 

INCLUDEPATH += \
    ../../Classes/CppWtShapeWidget

SOURCES += \
    ../../Classes/CppWtShapeWidget/wtshapewidget.cpp

HEADERS  += \
    ../../Classes/CppWtShapeWidget/wtshapewidget.h

OTHER_FILES += \
    ../../Classes/CppWtShapeWidget/Licence.txt

 

 

 

 

 

./CppWtShapeWidget/wtshapewidget.h

 

//---------------------------------------------------------------------------
/*
WtShapeWidget, Wt class for displaying a ShapeWidget
Copyright (C) 2011-2015 Richel Bilderbeek

This program 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.

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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppWtShapeWidget.htm
//---------------------------------------------------------------------------
#ifndef WTSHAPEWIDGET_H
#define WTSHAPEWIDGET_H

#include <string>
#include <vector>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>

#include <Wt/WPaintedWidget>
#include <Wt/WPaintDevice>
#pragma GCC diagnostic pop

namespace ribi {

struct Shape;
struct ShapeWidget;

///WtShapeWidget manages and displays a Shape
struct WtShapeWidget : public Wt::WPaintedWidget
{
  explicit WtShapeWidget(boost::shared_ptr<ShapeWidget> widget);

  ///Create a deep copy of WtShapeWidget for use with Wt
  WtShapeWidget * Clone() const;

  //mutable boost::signals2::signal<void ()> m_signal_position_changed;

  ///Draw a shape from a Shape
  static void DrawShape(
    Wt::WPainter& painter,
    const int left, const int top,
    const int width, const int height,
    const Shape * const shape);

  ///Draw a shape from a ShapeWidget
  static void DrawShape(
    Wt::WPainter& painter,
    const ShapeWidget * const widget);

  ShapeWidget * GetWidget() const { return m_widget.get(); }
  const ShapeWidget * GetWidget() { return m_widget.get(); }

private:
  ///The ShapeWidget
  boost::shared_ptr<ShapeWidget> m_widget;

  friend bool operator==(const WtShapeWidget& lhs, const WtShapeWidget& rhs);

  ///OnResize is called when the geometry of the widget is changed
  void OnResize();

  ///Wt paint event
  void paintEvent(Wt::WPaintDevice *paintDevice);

  ///Wt resize: hide it from sight
  void resize(const Wt::WLength& width, const Wt::WLength& height);

public:
  static std::string GetVersion();
  static std::vector<std::string> GetVersionHistory();
};

bool operator==(const WtShapeWidget& lhs, const WtShapeWidget& rhs);


} //~namespace ribi

#endif // QTSHAPEWIDGET_H

 

 

 

 

 

./CppWtShapeWidget/wtshapewidget.cpp

 

//---------------------------------------------------------------------------
/*
WtShapeWidget, Wt class for displaying a ShapeWidget
Copyright (C) 2011-2015 Richel Bilderbeek

This program 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.

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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppWtShapeWidget.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <cmath>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#include <boost/math/constants/constants.hpp>
#include <boost/numeric/conversion/cast.hpp>

#include <Wt/WColor>
#include <Wt/WPainter>
#include <Wt/WPolygonArea>

#include "geometry.h"
#include "shape.h"
#include "shapewidget.h"
#include "wtshapewidget.h"
//#include "trace.h"
#pragma GCC diagnostic pop

ribi::WtShapeWidget::WtShapeWidget(
  boost::shared_ptr<ShapeWidget> widget)
  : m_widget(widget)
{
  assert(widget);
  m_widget->m_signal_geometry_changed.connect(
    boost::bind(
      &ribi::WtShapeWidget::OnResize,
      this));

  this->OnResize();
}

///Create a deep copy of WtShapeWidget for use with Wt
ribi::WtShapeWidget * ribi::WtShapeWidget::Clone() const
{
  assert(m_widget);
  boost::shared_ptr<ShapeWidget> widget(m_widget->Clone());
  WtShapeWidget * wtwidget(new WtShapeWidget(widget));
  assert(*wtwidget == *this);
  return wtwidget;
}

void ribi::WtShapeWidget::DrawShape(
  Wt::WPainter& painter,
  const ShapeWidget * const widget)
{
  assert(widget);
  DrawShape(
    painter,
    Geometry().GetLeft(widget->GetGeometry()),
    Geometry().GetTop(widget->GetGeometry()),
    Geometry().GetWidth(widget->GetGeometry()),
    Geometry().GetHeight(widget->GetGeometry()),
    widget->GetShape()
  );
}

void ribi::WtShapeWidget::DrawShape(
  Wt::WPainter& painter,
  const int left, const int top,
  const int width, const int height,
  const Shape * const shape)
{
  const unsigned char red = shape->GetRed();
  const unsigned char green = shape->GetGreen();
  const unsigned char blue = shape->GetBlue();
  painter.setBrush(Wt::WColor(red,green,blue));

  const int n_corners = shape->GetNumberOfCorners();

  if (n_corners == 0)
  {
    //Draw a circle
    painter.drawEllipse(left,top,width-1,height-1);
    return;
  }

  const double rotation = shape->GetRotation();
  const double midx = boost::numeric_cast<double>(width) / 2.0;
  const double midy = boost::numeric_cast<double>(height) / 2.0;

  if (n_corners == 1)
  {
    //Draw a line
    const int x1 = boost::numeric_cast<int>(
      midx - (std::cos(rotation) * midx));
    const int y1 = boost::numeric_cast<int>(
      midy + (std::sin(rotation) * midy));
    const int x2 = boost::numeric_cast<int>(
      midx - (std::cos(rotation + boost::math::constants::pi<double>()) * midx));
    const int y2 = boost::numeric_cast<int>(
      midy + (std::sin(rotation + boost::math::constants::pi<double>()) * midy));
    painter.drawLine(left+x1,top+y1,left+x2,top+y2);
    return;
  }

  const double d_angle = 2.0 * boost::math::constants::pi<double>() / boost::numeric_cast<double>(n_corners);

  std::vector<Wt::WPointF> polygon;

  for (int i=0; i!=n_corners; ++i)
  {
    const double angle
      = rotation + (boost::numeric_cast<double>(i) * d_angle);
    const int x = left + boost::numeric_cast<int>(
      midx - (std::cos(angle) * midx));
    const int y = top + boost::numeric_cast<int>(
      midy + (std::sin(angle) * midy));
    polygon.push_back(Wt::WPointF(x,y));
  }
  painter.drawPolygon(&(polygon[0]),n_corners);

}

std::string ribi::WtShapeWidget::GetVersion()
{
  return "2.0";
}

std::vector<std::string> ribi::WtShapeWidget::GetVersionHistory()
{
  return {
    "2011-07-15: Version 1.0: initial version",
    "2011-08-08: Version 2.0: conformized architecture to MysteryMachineWidget"
  };
}

void ribi::WtShapeWidget::OnResize()
{
  resize(
    Geometry().GetWidth(m_widget->GetGeometry()),
    Geometry().GetHeight(m_widget->GetGeometry())
  );
}

void ribi::WtShapeWidget::paintEvent(Wt::WPaintDevice *paintDevice)
{
  Wt::WPainter painter(paintDevice);
  DrawShape(
    painter,
    0,0,width().toPixels(),height().toPixels(),
    this->m_widget->GetShape()
  );
}

void ribi::WtShapeWidget::resize(const Wt::WLength& width, const Wt::WLength& height)
{
  Wt::WPaintedWidget::resize(width,height);
}

bool ribi::operator==(const WtShapeWidget& lhs, const WtShapeWidget& rhs)
{
  return *lhs.m_widget == *rhs.m_widget;
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml