Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) WtMysteryMachineWidget

 

WtMysteryMachineWidget is a Wt widget class to display an MysteryMachineWidget.

 

 

 

 

 

wtmysterymachinewidget.h

 

//---------------------------------------------------------------------------
/*
WtMysteryMachineWidget, Wt widget for displaying the MysteryMachine class
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/CppWtMysteryMachineWidget.htm
//---------------------------------------------------------------------------
#ifndef WTMYSTERYMACHINEWIDGET_H
#define WTMYSTERYMACHINEWIDGET_H
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include <Wt/WPaintDevice>
#include <Wt/WPaintedWidget>
//---------------------------------------------------------------------------
namespace Wt { struct WMouseEventEvent; }
struct MysteryMachineWidget;
struct WtToggleButtonWidget;
//---------------------------------------------------------------------------
///WtMysteryMachineWidget displays a MysteryMachine
struct WtMysteryMachineWidget : public Wt::WPaintedWidget
{
  ///A WtMysteryMachineWidget is created by its width and height
  WtMysteryMachineWidget(const int width, const int height);

  ///Obtain a read-only pointer to the MysteryMachine
  const MysteryMachineWidget * GetWidget() const { return m_widget.get(); }

  ///Obtain a read-and-write pointer to the MysteryMachine
  MysteryMachineWidget * GetWidget() { return m_widget.get(); }

  ///Signal that is emitted when a WtMysteryMachineWidget is toggled
  mutable boost::signals2::signal<void ()> m_signal_changed;

  ///Obtain the WtMysteryMachineWidget its version
  static const std::string GetVersion();

  ///Obtain the WtMysteryMachineWidget its version history
  static const std::vector<std::string> GetVersionHistory();

  protected:
  ///Paint the WtMysteryMachineWidget
  void paintEvent(Wt::WPaintDevice *paintDevice);

  private:

  ///The MysteryMachine
  boost::scoped_ptr<MysteryMachineWidget> m_widget;

  ///Repaint the WtMysteryMachineWidget
  void DoRepaint();

  ///Respond to mouse click
  void OnClicked(const Wt::WMouseEvent& e);

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

  friend std::ostream& operator<<(std::ostream& os, const WtMysteryMachineWidget& widget);

  ///Wt resize: hide it from sight
  void resize(const Wt::WLength& width, const Wt::WLength& height);
};
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const WtMysteryMachineWidget& widget);
//---------------------------------------------------------------------------
#endif // WTMYSTERYMACHINEWIDGET_H

 

 

 

 

 

wtmysterymachinewidget.cpp

 

//---------------------------------------------------------------------------
/*
WtMysteryMachineWidget, Wt widget for displaying the MysteryMachine class
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/CppWtMysteryMachineWidget.htm
//---------------------------------------------------------------------------
#include <iostream>
#include <boost/bind.hpp>
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include <Wt/WBrush>
#include <Wt/WEvent>
#include <Wt/WPainter>
#include <Wt/WPen>
//---------------------------------------------------------------------------
#include "dial.h"
#include "dialwidget.h"
#include "led.h"
#include "ledwidget.h"
#include "togglebutton.h"
#include "togglebuttonwidget.h"
//#include "trace.h"
#include "mysterymachine.h"
#include "mysterymachinewidget.h"
#include "wtdialwidget.h"
#include "wtledwidget.h"
#include "wtmysterymachinewidget.h"
#include "wttogglebuttonwidget.h"
//---------------------------------------------------------------------------
WtMysteryMachineWidget::WtMysteryMachineWidget(
  const int width, const int height)
  : m_widget(new MysteryMachineWidget(
    Rect(0,0,width-1,height-1)))
{
  assert(m_widget);

  m_widget->GetMachine()->GetDialBack()->GetDial()->m_signal_position_changed.connect(boost::bind(
    &WtMysteryMachineWidget::DoRepaint,this));
  m_widget->GetMachine()->GetDialFront()->GetDial()->m_signal_position_changed.connect(boost::bind(
    &WtMysteryMachineWidget::DoRepaint,this));
  m_widget->GetMachine()->GetToggleButton()->GetToggleButton()->m_signal_toggled.connect(boost::bind(
    &WtMysteryMachineWidget::DoRepaint,this));

  m_widget->m_signal_geometry_changed.connect(
    boost::bind(
      &WtMysteryMachineWidget::OnResize,
      this));

  this->clicked().connect(this,&WtMysteryMachineWidget::OnClicked);

  this->m_widget->SetGeometry(Rect(0,0,width,height));
}
//---------------------------------------------------------------------------
void WtMysteryMachineWidget::DoRepaint()
{
  this->update();
}
//---------------------------------------------------------------------------
void WtMysteryMachineWidget::OnResize()
{
  WtMysteryMachineWidget::resize(m_widget->GetGeometry().GetWidth(),m_widget->GetGeometry().GetHeight());
}
//---------------------------------------------------------------------------
const std::string WtMysteryMachineWidget::GetVersion()
{
  return "1.1";
}
//---------------------------------------------------------------------------
const std::vector<std::string> WtMysteryMachineWidget::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("YYYY-MM-DD: version X.Y: [description]");
  v.push_back("2011-06-16: version 1.0: initial version");
  v.push_back("2011-08-20: Version 1.1: added operator<<");
  return v;
}
//---------------------------------------------------------------------------
void WtMysteryMachineWidget::OnClicked(const Wt::WMouseEvent& e)
{
  const int x = e.widget().x;
  const int y = e.widget().y;
  m_widget->Click(x,y);
}
//---------------------------------------------------------------------------
void WtMysteryMachineWidget::paintEvent(Wt::WPaintDevice *paintDevice)
{
  Wt::WPainter painter(paintDevice);
  painter.drawRect(0,0,width().toPixels(),height().toPixels());
  WtDialWidget::DrawDial(painter,GetWidget()->GetMachine()->GetDialBack());
  WtDialWidget::DrawDial(painter,GetWidget()->GetMachine()->GetDialFront());
  WtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedBack1());
  WtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedBack2());
  WtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedBack3());
  WtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedFront1());
  WtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedFront2());
  WtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedFront3());
  WtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedTopBack());
  WtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedTopFront());
  WtLedWidget::DrawLed(painter,GetWidget()->GetMachine()->GetLedTopMiddle());
  WtToggleButtonWidget::DrawToggleButton(painter,GetWidget()->GetMachine()->GetToggleButton());
}
//---------------------------------------------------------------------------
void WtMysteryMachineWidget::resize(const Wt::WLength& width, const Wt::WLength& height)
{
  Wt::WPaintedWidget::resize(width,height);
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const WtMysteryMachineWidget& widget)
{
  os
    << "<WtMysteryMachineWidget>"
    << *widget.m_widget
    << "</WtMysteryMachineWidget>";
  return os;
}
//---------------------------------------------------------------------------

 

 

 

 

 

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