Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Dial

 

STLQt CreatorLubuntu

 

Dial is a class for a dial.

 

Dial is displayed by QtDialWidget and WtDialWidget.

 

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

Technical facts

 

 

 

 

 

 

./CppDial/CppDial.pri

 

INCLUDEPATH += \
    ../../Classes/CppDial

SOURCES += \
    ../../Classes/CppDial/dial.cpp

HEADERS  += \
    ../../Classes/CppDial/dial.h

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

 

 

 

 

 

./CppDial/dial.h

 

//---------------------------------------------------------------------------
/*
Dial, dial 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/CppDial.htm
//---------------------------------------------------------------------------
#ifndef DIAL_H
#define DIAL_H

#include <iosfwd>
#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/checked_delete.hpp>
#include <boost/signals2.hpp>
#pragma GCC diagnostic pop

namespace ribi {

///Dial is a class for a dial
struct Dial
{
  explicit 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 noexcept { return m_blue; }

  ///Get the greenness of the Dial its color
  unsigned char GetGreen() const noexcept { 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 noexcept { return m_position; }

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

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

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

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

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

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

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

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

  ///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() noexcept {}
  friend void boost::checked_delete<>(Dial*);
  friend void boost::checked_delete<>(const Dial*);

  unsigned char m_blue;
  unsigned char m_green;
  double m_position;
  unsigned char m_red;

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

  #ifndef NDEBUG
  ///Test this class
  static void Test() noexcept;
  #endif
};

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

} //~namespace ribi

#endif // DIAL_H

 

 

 

 

 

./CppDial/dial.cpp

 

//---------------------------------------------------------------------------
/*
Dial, dial 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/CppDial.htm
//---------------------------------------------------------------------------
#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 "dial.h"

#include <cassert>
#include <boost/lexical_cast.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/numeric/conversion/cast.hpp>

#include "geometry.h"
#include "testtimer.h"
#include "trace.h"

#pragma GCC diagnostic pop

ribi::Dial::Dial(
  const double position,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
  :
    m_signal_color_changed{},
    m_signal_position_changed{},
    m_blue{blue},
    m_green{green},
    m_position{position},
    m_red{red}
{
  #ifndef NDEBUG
  Test();
  #endif
  assert(m_position >= 0.0);
  assert(m_position <= 1.0);
}

std::string ribi::Dial::GetVersion() noexcept
{
  return "3.3";
}

std::vector<std::string> ribi::Dial::GetVersionHistory() noexcept
{
  return {
    "2011-04-11: Version 1.0: initial version",
    "2011-06-03: Version 2.0: moved widget logic to DialWidget class",
    "2011-08-07: Version 3.0: conformized architure for MysteryMachine",
    "2011-08-20: Version 3.1: added operator<<",
    "2011-08-31: Version 3.2: allow changing the dial its color",
    "2013-04-30: Version 3.3: added testing, fixed bug in GetAngle"
  };
}

void ribi::Dial::SetBlue(const int b) noexcept
{
  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 ribi::Dial::SetColor(const int r,const int g,const int b) noexcept
{
  SetRed(r);
  SetGreen(g);
  SetBlue(b);
}

void ribi::Dial::SetGreen(const int g) noexcept
{
  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 ribi::Dial::SetRed(const int r) noexcept
{
  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 ribi::Dial::SetPosition(const double position) noexcept
{
  assert(position >= 0.0);
  assert(position <= 1.0);
  if (m_position != position)
  {
    m_position = position;
    m_signal_position_changed();
  }
}

#ifndef NDEBUG
void ribi::Dial::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
}
#endif

std::ostream& ribi::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