Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Widget

 

STLQt CreatorLubuntu

 

A widget/control is a user interface element, like a button or checkbox.

 

Widget is a GUI independent widget class and servers as a base class for others (incomplete list):

 

Similar widget classes are QWidget and Wt::WWidget.

Technical facts

 

 

 

 

 

 

./CppWidget/CppWidget.pri

 

INCLUDEPATH += \
    ../../Classes/CppWidget

SOURCES += \
    ../../Classes/CppWidget/widget.cpp

HEADERS  += \
    ../../Classes/CppWidget/widget.h

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

 

 

 

 

 

./CppWidget/widget.h

 

//---------------------------------------------------------------------------
/*
Widget, GUI independent widget class
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/CppWidget.htm
//---------------------------------------------------------------------------
#ifndef WIDGET_H
#define WIDGET_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-parameter"
#include <boost/make_shared.hpp>
#include <boost/checked_delete.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/signals2.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#pragma GCC diagnostic pop

namespace ribi {

///GUI indepedent widget class, modeled after the Qt and Wt architure
struct Widget
{
  typedef boost::geometry::model::d2::point_xy<double> Point;
  typedef boost::geometry::model::box<Point> Rect;

  static Rect CreateRect(const double left, const double top, const double width, const double height) noexcept;

  double GetBottom() const noexcept;
  const Rect& GetGeometry() const noexcept { return m_geometry; }
  Rect& GetGeometry() noexcept { return m_geometry; }
  double GetHeight() const noexcept;
  double GetLeft() const noexcept;
  double GetRight() const noexcept;
  double GetTop() const noexcept;
  static std::string GetVersion() noexcept;
  static std::vector<std::string> GetVersionHistory() noexcept;
  double GetWidth() const noexcept;

  ///Is the coordinat within the widget its rectangle?
  bool IsIn(const double x, const double y) const noexcept;

  ///SetGeometry resizes the Widget and emits an OnResize signal
  void SetGeometry(const Rect& geometry) noexcept;
  void SetGeometry(const double left, const double top, const double width, const double height) noexcept;

  ///Respond to a change in size
  mutable boost::signals2::signal<void ()> m_signal_geometry_changed;

  protected:
  virtual ~Widget() noexcept {}
  friend void boost::checked_delete<>(      Widget*);
  friend void boost::checked_delete<>(const Widget*);
  friend class boost::detail::sp_ms_deleter<      Widget>;
  friend class boost::detail::sp_ms_deleter<const Widget>;

  private:
  Rect m_geometry;
};

} //~namespace ribi

#endif // WIDGET_H

 

 

 

 

 

./CppWidget/widget.cpp

 

//---------------------------------------------------------------------------
/*
Widget, GUI independent widget class
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/CppWidget.htm
//---------------------------------------------------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include "widget.h"

#include "geometry.h"
#include <boost/geometry/algorithms/equals.hpp>

#pragma GCC diagnostic pop

ribi::Widget::Rect ribi::Widget::CreateRect(
  const double left, const double top, const double width, const double height) noexcept
{
  return Geometry().CreateRect(left,top,width,height);
}

double ribi::Widget::GetBottom() const noexcept
{
  return Geometry().GetBottom(m_geometry);
}

double ribi::Widget::GetHeight() const noexcept
{
  return Geometry().GetHeight(m_geometry);
}

double ribi::Widget::GetLeft() const noexcept
{
  return Geometry().GetLeft(m_geometry);
}

double ribi::Widget::GetRight() const noexcept
{
  return Geometry().GetRight(m_geometry);
}

double ribi::Widget::GetTop() const noexcept
{
  return Geometry().GetTop(m_geometry);
}

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

std::vector<std::string> ribi::Widget::GetVersionHistory() noexcept
{
  return {
    "2011-07-03: version 1.0: initial version",
    "2011-08-07: version 1.1: added signal that is emitted when geometry changes",
    "2014-03-28: version 2.0: replace Rect by Boost.Geometry its box class"
  };
}

double ribi::Widget::GetWidth() const noexcept
{
  return Geometry().GetWidth(m_geometry);
}

bool ribi::Widget::IsIn(const double x, const double y) const noexcept
{
  return boost::geometry::within(Point(x,y),m_geometry);
}

void ribi::Widget::SetGeometry(const double left, const double top, const double width, const double height) noexcept
{
  SetGeometry(CreateRect(left,top,width,height));
}

void ribi::Widget::SetGeometry(const Rect& geometry) noexcept
{
  if (!boost::geometry::equals(m_geometry,geometry))
  {
    m_geometry = geometry;
    m_signal_geometry_changed();
  }
}

 

 

 

 

 

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