Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) WtShapeWidget

 

WtShapeWidget is a Wt widget class to display an ShapeWidget.

 

 

 

 

 

wtshapewidget.h

 

//---------------------------------------------------------------------------
/*
WtShapeWidget, Wt class for displaying a ShapeWidget
Copyright (C) 2011 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>
//---------------------------------------------------------------------------
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include <Wt/WPaintedWidget>
#include <Wt/WPaintDevice>
//---------------------------------------------------------------------------
struct Shape;
struct ShapeWidget;
//---------------------------------------------------------------------------
///WtShapeWidget manages and displays a Shape
struct WtShapeWidget : public Wt::WPaintedWidget
{
  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 const std::string GetVersion();
  static const std::vector<std::string> GetVersionHistory();
};
//---------------------------------------------------------------------------
bool operator==(const WtShapeWidget& lhs, const WtShapeWidget& rhs);
//---------------------------------------------------------------------------
#endif // QTSHAPEWIDGET_H
//---------------------------------------------------------------------------

 

 

 

 

 

wtshapewidget.cpp

 

//---------------------------------------------------------------------------
/*
WtShapeWidget, Wt class for displaying a ShapeWidget
Copyright (C) 2011 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>
//---------------------------------------------------------------------------
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include <Wt/WColor>
#include <Wt/WPainter>
#include <Wt/WPolygonArea>
//---------------------------------------------------------------------------
#include "shape.h"
#include "shapewidget.h"
#include "wtshapewidget.h"
//#include "trace.h"
//---------------------------------------------------------------------------
WtShapeWidget::WtShapeWidget(
  boost::shared_ptr<ShapeWidget> widget)
  : m_widget(widget)
{
  assert(widget);
  m_widget->m_signal_geometry_changed.connect(
    boost::bind(
      &WtShapeWidget::OnResize,
      this));

  this->OnResize();
}
//---------------------------------------------------------------------------
///Create a deep copy of WtShapeWidget for use with Wt
WtShapeWidget * 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 WtShapeWidget::DrawShape(
  Wt::WPainter& painter,
  const ShapeWidget * const widget)
{
  assert(widget);
  DrawShape(
    painter,
    widget->GetGeometry().GetX(),
    widget->GetGeometry().GetY(),
    widget->GetGeometry().GetWidth(),
    widget->GetGeometry().GetHeight(),
    widget->GetShape());
}
//---------------------------------------------------------------------------
void 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 + M_PI) * midx));
    const int y2 = boost::numeric_cast<int>(
      midy + (std::sin(rotation + M_PI) * midy));
    painter.drawLine(left+x1,top+y1,left+x2,top+y2);
    return;
  }

  const double d_angle = 2.0 * M_PI / 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);

}
//---------------------------------------------------------------------------
const std::string WtShapeWidget::GetVersion()
{
  return "2.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> WtShapeWidget::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-07-15: Version 1.0: initial version");
  v.push_back("2011-08-08: Version 2.0: conformized architecture to MysteryMachineWidget");
  return v;
}
//---------------------------------------------------------------------------
void WtShapeWidget::OnResize()
{
  resize(m_widget->GetGeometry().GetWidth(),m_widget->GetGeometry().GetHeight());
}
//---------------------------------------------------------------------------
void WtShapeWidget::paintEvent(Wt::WPaintDevice *paintDevice)
{
  Wt::WPainter painter(paintDevice);
  DrawShape(
    painter,
    0,0,width().toPixels(),height().toPixels(),
    this->m_widget->GetShape());
}
//---------------------------------------------------------------------------
void WtShapeWidget::resize(const Wt::WLength& width, const Wt::WLength& height)
{
  Wt::WPaintedWidget::resize(width,height);
}
//---------------------------------------------------------------------------
bool 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