Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) ShapeWidget

 

ShapeWidget is a widget class for a Shape.

 

 

 

 

 

shapewidget.h

 

//---------------------------------------------------------------------------
/*
ShapeWidget, class for displaying a Shape
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/CppShapeWidget.htm
//---------------------------------------------------------------------------
#ifndef SHAPEWIDGET_H
#define SHAPEWIDGET_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include "widget.h"
//---------------------------------------------------------------------------
struct Shape;
//---------------------------------------------------------------------------
///ShapeWidget is a class to display a Shape
struct ShapeWidget : public Widget
{
  ShapeWidget(
    const int n_corners,
    const double rotation,
    const int x = 0,
    const int y = 0,
    const int width = 100,
    const int height = 100,
    const unsigned char red = 255,
    const unsigned char green = 255,
    const unsigned char blue = 255);

  ///Make a deep copy of ShapeWidget
  ShapeWidget * Clone() const;

  ///Obtain a read-only pointert to the Shape
  const Shape * GetShape() const { return m_shape.get(); }


  private:
  //ShapeWidget can only be deleted by Boost smart pointers
  virtual ~ShapeWidget() {}
  friend void boost::checked_delete<>(ShapeWidget*);
  friend bool operator==(const ShapeWidget& lhs,const ShapeWidget& rhs);

  ///The Shape
  boost::scoped_ptr<Shape> m_shape;

  public:
  static const std::string GetVersion();
  static const std::vector<std::string> GetVersionHistory();
};
//---------------------------------------------------------------------------
bool operator==(const ShapeWidget& lhs,const ShapeWidget& rhs);
//---------------------------------------------------------------------------
#endif // SHAPEWIDGET_H

 

 

 

 

 

shapewidget.cpp

 

//---------------------------------------------------------------------------
#include <cassert>
#include <cmath>
//---------------------------------------------------------------------------
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include "shape.h"
#include "shapewidget.h"
#include "rectangle.h"
//#include "trace.h"
//---------------------------------------------------------------------------
ShapeWidget::ShapeWidget(
  const int n_corners,
  const double rotation,
  const int x,
  const int y,
  const int width,
  const int height,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
  : m_shape(new Shape(n_corners,rotation,red,green,blue))
{
  this->SetGeometry(Rect(x,y,width,height));
}
//---------------------------------------------------------------------------
///Make a deep copy of ShapeWidget
ShapeWidget * ShapeWidget::Clone() const
{
  ShapeWidget * const w = new ShapeWidget(
    this->GetShape()->GetNumberOfCorners(),
    this->GetShape()->GetRotation(),
    this->GetGeometry().GetX(),
    this->GetGeometry().GetY(),
    this->GetGeometry().GetWidth(),
    this->GetGeometry().GetHeight(),
    this->GetShape()->GetRed(),
    this->GetShape()->GetGreen(),
    this->GetShape()->GetBlue());
  assert(*w == *this);
  return w;
}
//---------------------------------------------------------------------------
const std::string ShapeWidget::GetVersion()
{
  return "2.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> ShapeWidget::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-07-13: Version 1.0: initial version");
  v.push_back("2011-07-18: Version 1.1: removed useless methods");
  v.push_back("2011-08-08: Version 2.0: conformized architecture to MysteryMachineWidget");
  return v;
}
//---------------------------------------------------------------------------
bool operator==(const ShapeWidget& lhs,const ShapeWidget& rhs)
{
  return  lhs.GetGeometry() ==  rhs.GetGeometry()
    &&   *lhs.GetShape()    == *rhs.GetShape();
}
//---------------------------------------------------------------------------

 

 

 

 

 

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