Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) RubiksClockWidget

 

RubiksClockWidget is a widget class for a RubiksClock.

 

 

 

 

 

rubiksclockdial.h

 

//---------------------------------------------------------------------------
/*
RubiksClockDial, class for displaying a Rubik's Clock Dial
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/CppRubiksClockWidget.htm
//---------------------------------------------------------------------------
#ifndef RUBIKSCLOCKDIAL_H
#define RUBIKSCLOCKDIAL_H
//---------------------------------------------------------------------------
#include <iosfwd>
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include "widget.h"
//---------------------------------------------------------------------------
struct Dial;
//---------------------------------------------------------------------------
///RubiksClockDial is a class to display a Rubik's Clock Dial
struct RubiksClockDial : public Widget
{
  RubiksClockDial(
    const int time,
    const int x,
    const int y,
    const int width,
    const int height,
    const unsigned char red,
    const unsigned char green,
    const unsigned char blue);

  ///Obtain a read-and-write pointert to the Dial
  //Dial * GetDial() { return m_dial.get(); }

  ///Obtain a read-only pointert to the Dial
  const Dial * GetDial() const { return m_dial.get(); }

  int GetTime() const { return m_time % 12; }

  ///Obtain this class its version
  static const std::string GetVersion();

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

  ///Turn the dials n_positions_clockwise clockwise,
  ///negative values are also allowed
  void Turn(const int n_positions_clockwise);

  private:
  //DialWidget can only be deleted by Boost smart pointers
  virtual ~RubiksClockDial() {}
  friend void boost::checked_delete<>(RubiksClockDial*);

  boost::scoped_ptr<Dial> m_dial;

  ///Denotes the time shown by the dial as in a clock
  int m_time;

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

  public:
  //From www.richelbilderbeek.nl/CppGetAngle.htm
  static double GetAngle(const double dX, const double dY);

  //From www.richelbilderbeek.nl/CppGetDistance.htm
  static double GetDistance(const double dX, const double dY);
};
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const RubiksClockDial& widget);
//---------------------------------------------------------------------------
#endif // RUBIKSCLOCKDIAL_H

 

 

 

 

 

rubiksclockdial.cpp

 

//---------------------------------------------------------------------------
/*
RubiksClockDial, class for displaying a Rubik's Clock Dial
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/CppRubiksClockWidget.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <cmath>
#include <iostream>
//---------------------------------------------------------------------------
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include "dial.h"
#include "rubiksclockdial.h"
#include "rectangle.h"
#include "trace.h"
//---------------------------------------------------------------------------
RubiksClockDial::RubiksClockDial(
  const int time,
  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_dial(new Dial(0.0,red,green,blue)),
    m_time(time + 1)
{
  this->SetGeometry(Rect(x,y,width,height));
  this->Turn(-1);
}
//---------------------------------------------------------------------------
const std::string RubiksClockDial::GetVersion()
{
  return "1.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> RubiksClockDial::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-09-08: Version 1.0: initial version");
  return v;
}
//---------------------------------------------------------------------------
void RubiksClockDial::Turn(const int n_positions_clockwise)
{
  if (n_positions_clockwise % 12 != 0)
  {
    m_time += n_positions_clockwise;
    m_time %= 12;
    m_time += 12;
    m_time %= 12;
    assert(m_time >= 0);
    assert(m_time < 12);
    m_dial->SetPosition(boost::numeric_cast<double>(m_time) / 12.0);
  }
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const RubiksClockDial& widget)
{
  os
    << "<RubiksClockDial>"
    << *widget.m_dial
    << widget.GetGeometry()
    << "</RubiksClockDial>";
  return os;
}
//---------------------------------------------------------------------------


 

 

 

 

 

rubiksclockdialwidget.h

 

//---------------------------------------------------------------------------
/*
RubiksClockDialWidget, class for displaying a RubiksClockDial
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/CppRubiksClockDialWidget.htm
//---------------------------------------------------------------------------
#ifndef RUBIKSCLOCKDIALWIDGET_H
#define RUBIKSCLOCKDIALWIDGET_H
//---------------------------------------------------------------------------
#include <iosfwd>
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include "widget.h"
//---------------------------------------------------------------------------
struct RubiksClockDial;
//---------------------------------------------------------------------------
///RubiksClockDialWidget is a class to display a RubiksClockDial
struct RubiksClockDialWidget : public Widget
{
  RubiksClockDialWidget(
    const double position,
    const int x,
    const int y,
    const int width,
    const int height,
    const unsigned char red,
    const unsigned char green,
    const unsigned char blue);

  ///Obtain a read-and-write pointert to the RubiksClockDial
  RubiksClockDial * GetRubiksClockDial() { return m_dial.get(); }

  ///Obtain a read-only pointert to the RubiksClockDial
  const RubiksClockDial * GetRubiksClockDial() const { return m_dial.get(); }

  ///Obtain this class its version
  static const std::string GetVersion();

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

  ///Is the dial clicked?
  bool IsClicked(const int x, const int y) const;

  private:
  //RubiksClockDialWidget can only be deleted by Boost smart pointers
  virtual ~RubiksClockDialWidget() {}
  friend void boost::checked_delete<>(RubiksClockDialWidget*);

  boost::scoped_ptr<RubiksClockDial> m_dial;

  static double GetDistance(const double dX, const double dY);


  friend std::ostream& operator<<(std::ostream& os, const RubiksClockDialWidget& widget);
};
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const RubiksClockDialWidget& widget);
//---------------------------------------------------------------------------
#endif // RUBIKSCLOCKDIALWIDGET_H

 

 

 

 

 

rubiksclockdialwidget.cpp

 

//---------------------------------------------------------------------------
/*
RubiksClockDialWidget, class for displaying a RubiksClockDial
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/CppRubiksClockDialWidget.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <cmath>
#include <iostream>
//---------------------------------------------------------------------------
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include "dial.h"
#include "rubiksclockdial.h"
#include "rubiksclockdialwidget.h"
#include "rectangle.h"
//#include "trace.h"
//---------------------------------------------------------------------------
RubiksClockDialWidget::RubiksClockDialWidget(
  const double position,
  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_dial(new RubiksClockDial(position,x,y,width,height,red,green,blue))
{
  this->SetGeometry(Rect(x,y,width,height));
}
//---------------------------------------------------------------------------
double RubiksClockDialWidget::GetDistance(const double dX, const double dY)
{
  return std::sqrt( (dX * dX) + (dY * dY) );
}
//---------------------------------------------------------------------------
const std::string RubiksClockDialWidget::GetVersion()
{
  return "1.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> RubiksClockDialWidget::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-09-08: Version 1.0: initial version");
  return v;
}
//---------------------------------------------------------------------------
bool RubiksClockDialWidget::IsClicked(const int x, const int y) const
{
  const double widget_midx
    = boost::numeric_cast<double>(GetGeometry().GetX())
    + (boost::numeric_cast<double>(this->GetGeometry().GetWidth()) / 2.0);
  const double widget_midy
    = boost::numeric_cast<double>(GetGeometry().GetY())
    + (boost::numeric_cast<double>(this->GetGeometry().GetHeight()) / 2.0);
  const double x_d = boost::numeric_cast<double>(x);
  const double y_d = boost::numeric_cast<double>(y);
  return GetDistance(x_d - widget_midx, y_d - widget_midy)
    < (boost::numeric_cast<double>(this->GetGeometry().GetWidth()) / 2.0);
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const RubiksClockDialWidget& widget)
{
  os
    << "<RubiksClockDialWidget>"
    << *widget.m_dial
    << widget.GetGeometry()
    << "</RubiksClockDialWidget>";
  return os;
}
//---------------------------------------------------------------------------


 

 

 

 

 

rubiksclockwidget.h

 

//---------------------------------------------------------------------------
/*
RubiksClockWidget, class for displaying a RubiksClock
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/CppRubiksClockWidget.htm
//---------------------------------------------------------------------------
#ifndef RUBIKSCLOCKWIDGET_H
#define RUBIKSCLOCKWIDGET_H
//---------------------------------------------------------------------------
#include <iosfwd>
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include "widget.h"
//---------------------------------------------------------------------------
struct RubiksClock;
//---------------------------------------------------------------------------
///RubiksClockWidget is a class to display a RubiksClock
struct RubiksClockWidget : public Widget
{
  RubiksClockWidget(
    const int x = 0,
    const int y = 0,
    const int width = 192,
    const int height = 192);

  ///Click on the RubiksClock by the left mouse button or another
  void Click(const int x, const int y,const bool button_left);

  ///Flip the Rubik's Clock and display the other side
  void Flip();

  ///Does the widget display the front side?
  bool GetDisplayFront() const { return m_display_front; }

  ///Obtain a read-and-write pointert to the RubiksClock
  RubiksClock * GetRubiksClock() { return m_clock.get(); }

  ///Obtain a read-only pointert to the RubiksClock
  const RubiksClock * GetRubiksClock() const { return m_clock.get(); }

  ///Obtain this class its version
  static const std::string GetVersion();

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

  ///Respond to a change in the clock
  mutable boost::signals2::signal<void ()> m_signal_widget_flipped;

  private:
  //RubiksClockWidget can only be deleted by Boost smart pointers
  virtual ~RubiksClockWidget() {}
  friend void boost::checked_delete<>(RubiksClockWidget*);

  ///The RubiksClock
  boost::scoped_ptr<RubiksClock> m_clock;

  ///Does this widget display the front or the back side?
  bool m_display_front;

  ///Respond to a change in geometry
  void OnResize();

  friend std::ostream& operator<<(std::ostream& os, const RubiksClockWidget& widget);
};
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const RubiksClockWidget& widget);
//---------------------------------------------------------------------------
#endif // RUBIKSCLOCKWIDGET_H

 

 

 

 

 

rubiksclockwidget.cpp

 

//---------------------------------------------------------------------------
/*
RubiksClockWidget, class for displaying a RubiksClock
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/CppRubiksClockWidget.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <cmath>
#include <iostream>
//---------------------------------------------------------------------------
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include "dial.h"
#include "rubiksclock.h"
#include "rubiksclockdial.h"
#include "rubiksclockdialwidget.h"
#include "rubiksclockwidget.h"
#include "rectangle.h"
#include "togglebutton.h"
#include "togglebuttonwidget.h"
//#include "trace.h"
//---------------------------------------------------------------------------
RubiksClockWidget::RubiksClockWidget(
  const int x,
  const int y,
  const int width,
  const int height)
  : m_clock(new RubiksClock()),
    m_display_front(true)
{
  m_signal_geometry_changed.connect(
    boost::bind(
      &RubiksClockWidget::OnResize,
      this));

  this->SetGeometry(Rect(x,y,width,height));
}
//---------------------------------------------------------------------------
void RubiksClockWidget::Click(const int x,const int y,const bool button_left)
{
  RubiksClock::Times& times = (m_display_front ? m_clock->GetFrontTimes() : m_clock->GetBackTimes());
  RubiksClock::Pegs& pegs = m_clock->GetFrontPegs();

  for (int i=0; i!=2; ++i)
  {
    for (int j=0; j!=2; ++j)
    {
      if (times.times[i*2][j*2]->IsClicked(x,y))
      {
        if(m_display_front)
        {
          this->m_clock->TurnWheel(
            i
            ? (j ? RubiksClock::bottomRight : RubiksClock::topRight)
            : (j ? RubiksClock::bottomLeft : RubiksClock::topLeft),
            button_left ? 1 : -1);
        }
        else
        {
          this->m_clock->TurnWheel(
            i
            ? (j ? RubiksClock::bottomLeft : RubiksClock::topLeft)
            : (j ? RubiksClock::bottomRight : RubiksClock::topRight),
            button_left ? 1 : -1);
        }
      }
      else if (pegs.pegs[i][j]->GetGeometry().IsIn(x,y))
      {
        m_clock->TogglePeg(
          i
          ? (j ? RubiksClock::bottomRight : RubiksClock::topRight)
          : (j ? RubiksClock::bottomLeft : RubiksClock::topLeft));
      }
    }
  }
}
//---------------------------------------------------------------------------
void RubiksClockWidget::Flip()
{
  m_display_front = !m_display_front;
  m_signal_widget_flipped();
}
//---------------------------------------------------------------------------
const std::string RubiksClockWidget::GetVersion()
{
  return "1.2";
}
//---------------------------------------------------------------------------
const std::vector<std::string> RubiksClockWidget::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-09-01: Version 1.0: initial version");
  v.push_back("2011-09-09: Version 1.1: use of geometries");
  v.push_back("2011-09-15: Version 1.2: allow flipping the clock");
  return v;
}
//---------------------------------------------------------------------------
void RubiksClockWidget::OnResize()
{
  m_clock->SetGeometry(this->GetGeometry());
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const RubiksClockWidget& widget)
{
  os
    << "<RubiksClockWidget>"
    << *widget.m_clock
    << widget.GetGeometry()
    << "</RubiksClockWidget>";
  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