Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) WtRubiksClockWidget

 

WtRubiksClockWidget is a Wt widget class to display an RubiksClockWidget.

 

 

 

 

 

wtrubiksclockwidget.h

 

//---------------------------------------------------------------------------
/*
WtRubiksClockWidget, Wt widget for displaying the RubiksClock 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/CppWtRubiksClockWidget.htm
//---------------------------------------------------------------------------
#ifndef WTRUBIKSCLOCKWIDGET_H
#define WTRUBIKSCLOCKWIDGET_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include <Wt/WPaintDevice>
#include <Wt/WPaintedWidget>
//---------------------------------------------------------------------------
namespace Wt { struct WMouseEventEvent; }
struct RubiksClock;
struct RubiksClockWidget;
//---------------------------------------------------------------------------
///WtRubiksClockWidget displays a RubiksClock
struct WtRubiksClockWidget : public Wt::WPaintedWidget
{
  ///A WtRubiksClockWidget is created by its toggle state and its color
  WtRubiksClockWidget(
    const bool toggled = false,
    const unsigned char red = 255,
    const unsigned char green = 255,
    const unsigned char blue = 255);

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

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

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

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

  ///\brief
  ///Draw the RubiksClock
  ///
  ///Because a Rubik's Clock has two sides, the parameter
  ///front_size determines whether to draw the front side or the back side
  static void DrawRubiksClock(
    Wt::WPainter& painter,
    const int x, const int y,
    const int width, const int height,
    const RubiksClock * const clock,
    const bool front_size);

  ///Draw the RubiksClock
  static void DrawRubiksClock(
    Wt::WPainter& painter,
    const RubiksClockWidget * const widget);

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

  private:
  ///The RubiksClockWidget
  boost::scoped_ptr<RubiksClockWidget> m_widget;

  ///Do not let this be called by the client
  void resize(const Wt::WLength& width, const Wt::WLength& height);

  ///Repaint the WtRubiksClockWidget
  void DoRepaint();

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

  ///Respond to the widget resizing
  void OnResize();
};
//---------------------------------------------------------------------------

/*
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
#include "UnitRubiksClock.h"
//---------------------------------------------------------------------------
class TFormRubiksClockMain : public TForm
{
__published: // IDE-managed Components
        TImage *ImageClockFront;
        TImage *ImageClockBack;
        TPanel *Panel1;
        TButton *ButtonAbout;
        TButton *ButtonShuffle;
        void __fastcall FormResize(TObject *Sender);
        void __fastcall ImageClockFrontMouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall ImageClockBackMouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall ButtonAboutClick(TObject *Sender);
        void __fastcall ButtonShuffleClick(TObject *Sender);
private: // User declarations
  RubiksClock mClock;
  void DrawScreen();
public: // User declarations
        __fastcall TFormRubiksClockMain(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormRubiksClockMain *FormRubiksClockMain;
//---------------------------------------------------------------------------
void DrawRubiksClock(
  const RubiksClockTimes& times,
  const RubiksClockPegs& pegs,
  const TColor colorCasing,
  const TColor colorClock,
  TImage * const image);

void PaintVcl(TImage * const image,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue  );

*/

#endif // WTRUBIKSCLOCKWIDGET_H

 

 

 

 

 

wtrubiksclockwidget.cpp

 

//---------------------------------------------------------------------------
/*
WtRubiksClockWidget, Wt widget for displaying the RubiksClock 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/CppWtRubiksClockWidget.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 "rubiksclock.h"
#include "rubiksclockdial.h"
#include "rubiksclockdialwidget.h"
#include "rubiksclockwidget.h"
#include "togglebutton.h"
#include "togglebuttonwidget.h"
//#include "trace.h"
#include "wtrubiksclockwidget.h"
#include "wtdialwidget.h"
#include "wttogglebuttonwidget.h"
//---------------------------------------------------------------------------
WtRubiksClockWidget::WtRubiksClockWidget(
  const bool toggled,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
  : m_widget(new RubiksClockWidget(toggled,red,green,blue))
{
  assert(m_widget);

  m_widget->m_signal_widget_flipped.connect(
    boost::bind(
      &WtRubiksClockWidget::DoRepaint,
      this));

  m_widget->GetRubiksClock()->m_signal_clock_changed.connect(
    boost::bind(
      &WtRubiksClockWidget::DoRepaint,
      this));

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

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

  m_widget->SetGeometry(Rect(0,0,200,200));
}
//---------------------------------------------------------------------------
void WtRubiksClockWidget::DoRepaint()
{
  this->update();
}
//---------------------------------------------------------------------------
void WtRubiksClockWidget::DrawRubiksClock(
  Wt::WPainter& painter,
  const int left, const int top,
  const int width, const int height,
  const RubiksClock * const clock,
  const bool front_side)
{
  //Draw main clock ring
  {
    Wt::WPen pen = painter.pen();
    pen.setColor(Wt::black);
    painter.setPen(pen);
  }
  {
    painter.setBrush(
      Wt::WBrush(
        Wt::WColor(
          front_side ? 127 : 96,
          front_side ? 127 : 96,
          196)));
  }
  painter.drawEllipse(
    static_cast<double>(left),
    static_cast<double>(top),
    static_cast<double>(width),
    static_cast<double>(height));



  //Draw the clocks and pegs
  {
    for (int y=0; y!=3; ++y)
    {
      for (int x=0; x!=3; ++x)
      {
        const boost::shared_ptr<RubiksClockDialWidget> w
          = (front_side ? clock->GetFrontTimes() : clock->GetBackTimes()).times[x][y];
        WtDialWidget::DrawDial(
          painter,
          w->GetGeometry().GetX(),
          w->GetGeometry().GetY(),
          w->GetGeometry().GetWidth(),
          w->GetGeometry().GetHeight(),
          w->GetRubiksClockDial()->GetDial());
      }
    }
    //Draw the pegs
    for (int y=0; y!=2; ++y)
    {
      for (int x=0; x!=2; ++x)
      {
        WtToggleButtonWidget::DrawToggleButton(
          painter,
          (front_side
            ? clock->GetFrontPegs()
            : clock->GetBackPegs()).pegs[x][y].get());
      }
    }
  }
}
//---------------------------------------------------------------------------
void WtRubiksClockWidget::DrawRubiksClock(
  Wt::WPainter& painter,
  const RubiksClockWidget * const widget)
{
  DrawRubiksClock(
    painter,
    widget->GetGeometry().GetX(),
    widget->GetGeometry().GetY(),
    widget->GetGeometry().GetWidth(),
    widget->GetGeometry().GetHeight(),
    widget->GetRubiksClock(),
    widget->GetDisplayFront());
}
//---------------------------------------------------------------------------
const std::string WtRubiksClockWidget::GetVersion()
{
  return "1.1";
}
//---------------------------------------------------------------------------
const std::vector<std::string> WtRubiksClockWidget::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("YYYY-MM-DD: version X.Y: [description]");
  v.push_back("2011-09-01: version 1.0: initial version");
  v.push_back("2011-09-15: version 1.1: allow displaying both sides");
  return v;
}
//---------------------------------------------------------------------------
void WtRubiksClockWidget::OnClicked(const Wt::WMouseEvent& e)
{
  const int x = e.widget().x;
  const int y = e.widget().y;

  const bool is_left_button = e.button() == Wt::WMouseEvent::LeftButton
    || e.wheelDelta() > 0;
  m_widget->Click(x,y,is_left_button);
}
//---------------------------------------------------------------------------
void WtRubiksClockWidget::OnResize()
{
  resize(m_widget->GetGeometry().GetWidth(),m_widget->GetGeometry().GetHeight());
}
//---------------------------------------------------------------------------
void WtRubiksClockWidget::paintEvent(Wt::WPaintDevice *paintDevice)
{
  Wt::WPainter painter(paintDevice);
  DrawRubiksClock(painter,m_widget.get());
}
//---------------------------------------------------------------------------
void WtRubiksClockWidget::resize(const Wt::WLength& width, const Wt::WLength& height)
{
  Wt::WPaintedWidget::resize(width,height);
}
//---------------------------------------------------------------------------

 

 

 

 

 

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