Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) WtDialWidget

 

WtQt CreatorLubuntu

 

WtDialWidget is a Wt widget class to display an DialWidget.

 

WtDialWidget is used in the tool TestDial.

Technical facts

 

 

 

 

 

 

./CppWtDialWidget/CppWtDialWidget.pri

 

INCLUDEPATH += \
    ../../Classes/CppWtDialWidget

SOURCES += \
    ../../Classes/CppWtDialWidget/wtdialwidget.cpp

HEADERS  += \
    ../../Classes/CppWtDialWidget/wtdialwidget.h

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

 

 

 

 

 

./CppWtDialWidget/wtdialwidget.h

 

//---------------------------------------------------------------------------
/*
WtDialWidget, Wt widget for displaying the 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/CppWtDialWidget.htm
//---------------------------------------------------------------------------
#ifndef WTDIALWIDGET_H
#define WTDIALWIDGET_H

#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-variable"
#include <boost/signals2.hpp>

#include <Wt/WPaintDevice>
#include <Wt/WPaintedWidget>
#pragma GCC diagnostic pop

namespace Wt { struct WMouseEventEvent; }

namespace ribi {

struct Dial;
struct DialWidget;

///Wt display widget of a Dial
struct WtDialWidget : public Wt::WPaintedWidget
{
  explicit WtDialWidget(
    const double intensity    = 0.0,
    const unsigned char red   = 255,
    const unsigned char green = 255,
    const unsigned char blue  = 255);

  const DialWidget * GetWidget() const { return m_widget.get(); }

  DialWidget * GetWidget() { return m_widget.get(); }

  ///Draw a Dial
  static void DrawDial(
    Wt::WPainter& painter,
    const int left, const int top,
    const int width, const int height,
    const Dial * const dial);

  ///Draw a Dial
  static void DrawDial(
    Wt::WPainter& painter,
    const DialWidget * const widget);

  static std::string GetVersion();
  static std::vector<std::string> GetVersionHistory();

  protected:
  void paintEvent(Wt::WPaintDevice *paintDevice);

  private:
  boost::scoped_ptr<DialWidget> m_widget;

  void DoRepaint();
  void OnClicked(const Wt::WMouseEvent& e);

  ///OnResize is called when the geometry of the widget is changed
  void OnResize();

  ///Wt resize: hide it from sight
  void resize(const Wt::WLength& width, const Wt::WLength& height);
};

} //~namespace ribi

#endif // WTLEDWIDGET_H

 

 

 

 

 

./CppWtDialWidget/wtdialwidget.cpp

 

//---------------------------------------------------------------------------
/*
WtDialWidget, Wt widget for displaying the 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/CppWtDialWidget.htm
//---------------------------------------------------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#include "wtdialwidget.h"

#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 "dialwidget.h"
#include "geometry.h"
#pragma GCC diagnostic pop

ribi::WtDialWidget::WtDialWidget(
  const double intensity,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
  : m_widget(new DialWidget(intensity,red,green,blue))
{
  assert(m_widget);

  m_widget->GetDial()->m_signal_color_changed.connect(
    boost::bind(
      &ribi::WtDialWidget::DoRepaint,
      this));

  m_widget->GetDial()->m_signal_position_changed.connect(
    boost::bind(
      &ribi::WtDialWidget::DoRepaint,
      this));

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

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

  m_widget->SetGeometry(0,0,32,32);
}

void ribi::WtDialWidget::DoRepaint()
{
  this->update();
}

void ribi::WtDialWidget::DrawDial(
  Wt::WPainter& painter,
  const int left, const int top,
  const int width, const int height,
  const Dial * const dial)
{
  const double position = dial->GetPosition();

  //Draw knob
  {
    Wt::WPen pen = painter.pen();
    pen.setWidth(1);
    pen.setColor(Wt::WColor(0,0,0));
    painter.setPen(pen);
  }

  painter.setBrush(Wt::WColor(
    dial->GetRed(),
    dial->GetGreen(),
    dial->GetBlue()));

  painter.drawEllipse(left+1,top+1,width-2,height-2);

  //Draw pointer
  const int midx = width / 2;
  const int midy = height / 2;
  const double ray = static_cast<double>(std::min( midx, midy ));
  const double two_pi = boost::math::constants::two_pi<double>();
  const double angle = position * two_pi;
  const int pointerX
    = static_cast<int>( static_cast<double>(midx) + (std::sin(angle) * ray) );
  const int pointerY
    = static_cast<int>( static_cast<double>(midy) - (std::cos(angle) * ray) );

  {
    Wt::WPen pen = painter.pen();
    pen.setWidth(4);
    painter.setPen(pen);
  }
  painter.drawLine(left+midx,top+midy,left+pointerX,top+pointerY);
}

void ribi::WtDialWidget::DrawDial(
  Wt::WPainter& painter,
  const DialWidget * const widget)
{
  DrawDial(
    painter,
    Geometry().GetLeft(widget->GetGeometry()),
    Geometry().GetTop(widget->GetGeometry()),
    Geometry().GetWidth(widget->GetGeometry()),
    Geometry().GetHeight(widget->GetGeometry()),
    widget->GetDial()
  );
}

std::string ribi::WtDialWidget::GetVersion()
{
  return "3.2";
}

std::vector<std::string> ribi::WtDialWidget::GetVersionHistory()
{
  return {
    "2011-04-10: version 1.0: initial version",
    "2011-04-11: version 1.1: fixed #include guard, fixed initial date, made default dial white",
    "2011-07-03: version 2.0: moved Dial its user interface logic to new class DialWidget",
    "2011-08-07: Version 3.0: conformized architure for MysteryMachine",
    "2011-08-31: Version 3.1: allow changing the dial its color",
    "2014-04-23: Version 3.2: use of Boost.Geometry its rectangle class"
  };
}

void ribi::WtDialWidget::OnClicked(const Wt::WMouseEvent& e)
{
  m_widget->Click(e.widget().x,e.widget().y);
}

void ribi::WtDialWidget::OnResize()
{
  resize(
    Geometry().GetWidth(m_widget->GetGeometry()),
    Geometry().GetHeight(m_widget->GetGeometry())
  );
}

void ribi::WtDialWidget::paintEvent(Wt::WPaintDevice *paintDevice)
{
  Wt::WPainter painter(paintDevice);
  this->DrawDial(painter,m_widget.get());
}

void ribi::WtDialWidget::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