//---------------------------------------------------------------------------
/*
DialWidget, class for displaying a 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/CppDialWidget.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <cmath>
#include <iostream>
//---------------------------------------------------------------------------
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include "dial.h"
#include "dialwidget.h"
#include "rectangle.h"
//#include "trace.h"
//---------------------------------------------------------------------------
DialWidget::DialWidget(
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 Dial(position,red,green,blue))
{
this->SetGeometry(Rect(x,y,width,height));
}
//---------------------------------------------------------------------------
void DialWidget::Click(const int x,const int y)
{
if (!IsClicked(x,y)) return;
const int midx = GetGeometry().GetX() + (GetGeometry().GetWidth() / 2);
const int midy = GetGeometry().GetY() + (GetGeometry().GetHeight() / 2);
const double dx = boost::numeric_cast<double>(x - midx);
const double dy = boost::numeric_cast<double>(y - midy);
const double angle = Dial::GetAngle(dx,dy);
const double position = angle / (2.0 * M_PI);
assert(position >= 0.0);
assert(position <= 1.0);
m_dial->SetPosition(position);
}
//---------------------------------------------------------------------------
const std::string DialWidget::GetVersion()
{
return "2.3";
}
//---------------------------------------------------------------------------
const std::vector<std::string> DialWidget::GetVersionHistory()
{
std::vector<std::string> v;
v.push_back("2011-07-03: Version 1.0: initial version");
v.push_back("2011-08-07: Version 2.0: conformized architure for MysteryMachine");
v.push_back("2011-08-20: Version 2.1: added operator<<");
v.push_back("2011-08-31: Version 2.2: allow changing the dial its color");
v.push_back("2011-09-08: Version 2.3: check for clicking on the dial, instead of its rectangle");
return v;
}
//---------------------------------------------------------------------------
//From www.richelbilderbeek.nl/CppGetAngle.htm
double DialWidget::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 DialWidget::GetDistance(const double dX, const double dY)
{
return std::sqrt( (dX * dX) + (dY * dY) );
}
//---------------------------------------------------------------------------
bool DialWidget::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 DialWidget& widget)
{
os
<< "<DialWidget>"
<< *widget.m_dial
<< widget.GetGeometry()
<< "</DialWidget>";
return os;
}
//---------------------------------------------------------------------------
|