Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Dial

 

Dial is a class for a dial.

 

Dial is displayed by QtDialWidget and WtDialWidget.

 

Dial is used in (among others) the tool TestDial.

 

 

 

 

 

dial.h

 

//---------------------------------------------------------------------------
/*
Dial, dial 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/CppDial.htm
//---------------------------------------------------------------------------
#ifndef DIAL_H
#define DIAL_H
//---------------------------------------------------------------------------
#include <iosfwd>
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
///Dial is a class for a dial
struct Dial
{
  Dial(const double position = 0.0,
    const unsigned char red   = 255,
    const unsigned char green = 255,
    const unsigned char blue  = 255);

  ///Get the blueness of the Dial its color
  unsigned char GetBlue() const { return m_blue; }

  ///Get the greenness of the Dial its color
  unsigned char GetGreen() const { return m_green; }

  ///\brief
  ///Get the position of the Dial
  ///
  ///0.00: Up   , 12:00 'o clock
  ///0.25: Right,  3:00 'o clock
  ///0.05: Down ,  6:00 'o clock
  ///0.75: Left ,  9:00 'o clock
  ///1.00: Up   , 12:00 'o clock
  double GetPosition() const { return m_position; }

  ///Get the redness of the Dial its color
  unsigned char GetRed() const { return m_red; }

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

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

  ///Set the blue the Dial
  void SetBlue(const int b);

  ///Set the color the Dial
  void SetColor(const int r, const int g, const int b);

  ///Set the greenness the Dial
  void SetGreen(const int g);

  ///Set the position of the Dial
  void SetPosition(const double position);

  ///Set the redness the Dial
  void SetRed(const int r);

  ///The signal emitted when the Dial its color is changed
  mutable boost::signals2::signal<void ()> m_signal_color_changed;

  ///The signal emitted when the Dial position is changed
  mutable boost::signals2::signal<void ()> m_signal_position_changed;

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

  unsigned char m_red;
  unsigned char m_green;
  unsigned char m_blue;

  double m_position;

  friend std::ostream& operator<<(std::ostream& os, const Dial& dial);

  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 Dial& dial);
//---------------------------------------------------------------------------
#endif // DIAL_H
//---------------------------------------------------------------------------

 

 

 

 

 

dial.cpp

 

//---------------------------------------------------------------------------
/*
Dial, dial 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/CppDial.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <cmath>
//---------------------------------------------------------------------------
#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include "dial.h"
//#include "trace.h"
//---------------------------------------------------------------------------
Dial::Dial(
  const double position,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
  : m_red(red), m_green(green), m_blue(blue)
{
  assert(position >= 0.0);
  assert(position <= 1.0);
  SetPosition(position);
}
//---------------------------------------------------------------------------
const std::string Dial::GetVersion()
{
  return "3.2";
}
//---------------------------------------------------------------------------
const std::vector<std::string> Dial::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-04-11: Version 1.0: initial version");
  v.push_back("2011-06-03: Version 2.0: moved widget logic to DialWidget class");
  v.push_back("2011-08-07: Version 3.0: conformized architure for MysteryMachine");
  v.push_back("2011-08-20: Version 3.1: added operator<<");
  v.push_back("2011-08-31: Version 3.2: allow changing the dial its color");
  return v;
}
//---------------------------------------------------------------------------
void Dial::SetBlue(const int b)
{
  assert(b >=   0);
  assert(b  < 256);

  if (m_blue != boost::numeric_cast<unsigned char>(b))
  {
    m_blue = boost::numeric_cast<unsigned char>(b);
    m_signal_color_changed();
  }
}
//---------------------------------------------------------------------------
void Dial::SetColor(const int r,const int g,const int b)
{
  SetRed(r);
  SetGreen(g);
  SetBlue(b);
}
//---------------------------------------------------------------------------
void Dial::SetGreen(const int g)
{
  assert(g >=   0);
  assert(g  < 256);

  if (m_green != boost::numeric_cast<unsigned char>(g))
  {
    m_green = boost::numeric_cast<unsigned char>(g);
    m_signal_color_changed();
  }
}
//---------------------------------------------------------------------------
void Dial::SetRed(const int r)
{
  assert(r >=   0);
  assert(r  < 256);

  if (m_red != boost::numeric_cast<unsigned char>(r))
  {
    m_red = boost::numeric_cast<unsigned char>(r);
    m_signal_color_changed();
  }
}
//---------------------------------------------------------------------------
void Dial::SetPosition(const double position)
{
  assert(position >= 0.0);
  assert(position <= 1.0);
  if (m_position != position)
  {
    m_position = position;
    m_signal_position_changed();
  }
}
//---------------------------------------------------------------------------
double Dial::GetAngle(const double dX, const double dY)
{
  //In which quadrant are we?
  if (dX > 0.0)
  {
    if (dY > 0.0)
    {
      //dX > 0.0 && dY > 0.0
      //Quadrant IV
      assert(dX > 0.0 && dY > 0.0);
      const double angle = (1.0 * M_PI) - std::atan(dX / dY);
      assert(angle > 0.5 * M_PI && angle < 1.0 * M_PI);
      return angle;
    }
    else if (dY < 0.0)
    {
      //dX > 0.0 && dY <= 0.0
      //Quadrant I
      assert(dX > 0.0 && dY < 0.0);
      const double angle = (0.0 * M_PI) - std::atan(dX / dY);
      assert(angle > 0.0 * M_PI && angle < 0.5 * M_PI);
      return angle;
    }
    else
    {
      //dX > 0.0 && dY == 0.0
      //On Y-axis, right side
      assert(dX > 0.0 && dY == 0.0);
      const double angle = 0.5 * M_PI;
      return angle;
    }
  }
  else if (dX < 0.0)
  {
    if (dY > 0.0)
    {
      //dX < 0.0 && dY > 0.0
      //Quadrant III
      assert(dX < 0.0 && dY > 0.0);
      const double angle = (1.0 * M_PI) - std::atan(dX / dY);
      assert(angle > 1.0 * M_PI && angle < 1.5 * M_PI);
      return angle;
    }
    else if (dY < 0.0)
    {
      //dX < 0.0 && dY < 0.0
      //Quadrant II
      assert(dX < 0.0 && dY < 0.0);
      const double angle = (2.0 * M_PI) - std::atan(dX / dY);
      assert(angle > 1.5 * M_PI && angle < 2.0 * M_PI);
      return angle;
    }
    else
    {
      //dX < 0.0 && dY == 0.0
      //On X-axis
      assert(dX < 0.0 && dY == 0.0);
      const double angle = 1.5 * M_PI;
      return angle;
    }
  }
  else
  {
    if (dY > 0.0)
    {
      //dX == 0 && dY > 0.0)
      //On Y-axis, right side of origin
      assert(dX==0.0 && dY > 0.0);
      const double angle = 1.0 * M_PI;
      return angle;
    }
    else if (dY < 0.0)
    {
      //dX == 0 && dY < 0.0)
      //On Y-axis, left side of origin
      assert(dX==0.0 && dY < 0.0);
      const double angle = 0.0 * M_PI;
      return angle;
    }
    else
    {
      //dX == 0.0 && dY == 0.0)
      //On origin
      assert(dX==0.0 && dY == 0.0);
      const double angle = 0.0 * M_PI;
      return angle;
    }
  }
}
//---------------------------------------------------------------------------
double Dial::GetDistance(const double dX, const double dY)
{
  return std::sqrt( (dX * dX) + (dY * dY) );
}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const Dial& dial)
{
  os
    << "<Dial>"
    << "<blue>"
      << static_cast<int>(dial.m_blue)
    << "</blue>"
    << "<green>"
    << static_cast<int>(dial.m_green)
    << "</green>"
    << "<position>"
    << dial.m_position
    << "</position>"
    << "<red>"
    << static_cast<int>(dial.m_red)
    << "</red>"
    << "</Dial>";
  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