Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QtLedWidget

 

QtQt CreatorLubuntu

 

QtLedWidget is a Qt class to display an Led.

 

QtLedWidget is used in the tool TestLed.

Technical facts

 

 

 

 

 

 

./CppQtLedWidget/CppQtLedWidget.pri

 

INCLUDEPATH += \
    ../../Classes/CppQtLedWidget

SOURCES += \
    ../../Classes/CppQtLedWidget/qtledwidget.cpp \
    ../../Classes/CppQtLedWidget/qtledwidgetdialog.cpp

HEADERS  += \
    ../../Classes/CppQtLedWidget/qtledwidget.h \
    ../../Classes/CppQtLedWidget/qtledwidgetdialog.h

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

FORMS += \
    ../../Classes/CppQtLedWidget/qtledwidgetdialog.ui

 

 

 

 

 

./CppQtLedWidget/qtledwidget.h

 

//---------------------------------------------------------------------------
/*
QtLedWidget, Qt widget for displaying the Led 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/CppQtLedWidget.htm
//---------------------------------------------------------------------------
#ifndef QTLEDWIDGET_H
#define QTLEDWIDGET_H

#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/scoped_ptr.hpp>

#include <QWidget>

#include "led.h"       //Needed by MOC
#include "ledwidget.h" //Needed by MOC
#pragma GCC diagnostic pop

namespace ribi {

class QtLedWidget : public QWidget
{
  Q_OBJECT
public:
  explicit QtLedWidget(
    QWidget *parent = 0,
    const double intensity    = 0.0,
    const unsigned char red   = 255,
    const unsigned char green =   0,
    const unsigned char blue  =   0);
  const LedWidget * GetWidget() const { return m_widget.get(); }
  LedWidget * GetWidget() { return m_widget.get(); }

  ///Draw a Led from a Led
  static void DrawLed(
    QPainter& painter,
    const int left, const int top,
    const int width, const int height,
    const Led * const led);

  ///Draw a Led from a LedWidget
  static void DrawLed(
    QPainter& painter,
    const boost::shared_ptr<const LedWidget> widget);


protected:
  void paintEvent(QPaintEvent *);
  void resizeEvent(QResizeEvent *);

private:
  boost::shared_ptr<LedWidget> m_widget;

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

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

};

} //~namespace ribi

#endif // QTLEDWIDGET_H

 

 

 

 

 

./CppQtLedWidget/qtledwidget.cpp

 

//---------------------------------------------------------------------------
/*
QtLedWidget, Qt widget for displaying the Led 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/CppQtLedWidget.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 "qtledwidget.h"

#include <cassert>
#include <iostream>

#include <boost/numeric/conversion/cast.hpp>

#include <QResizeEvent>
#include <QPainter>

#include "led.h"
//#include "trace.h"
#pragma GCC diagnostic pop

ribi::QtLedWidget::QtLedWidget(
  QWidget *parent,
  const double intensity,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
  : QWidget(parent),
    m_widget(new LedWidget(intensity,red,green,blue))
{
  assert(m_widget);
  m_widget->GetLed()->m_signal_color_changed.connect(
    boost::bind(
      &ribi::QtLedWidget::repaint,
      this));

  m_widget->GetLed()->m_signal_intensity_changed.connect(
    boost::bind(
      &ribi::QtLedWidget::repaint,
      this));

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

  GetWidget()->SetGeometry(0,0,100,100);
  GetWidget()->GetLed()->SetColor(255,124,0);
  GetWidget()->GetLed()->SetIntensity(0.99);
}

///Draw a Led from a Led
void ribi::QtLedWidget::DrawLed(
    QPainter& painter,
    const int left, const int top,
    const int width, const int height,
    const Led * const led)
{
  assert(width  > 0);
  assert(height > 0);
  const int red   = boost::numeric_cast<int>(led->GetRed());
  const int green = boost::numeric_cast<int>(led->GetGreen());
  const int blue  = boost::numeric_cast<int>(led->GetBlue());
  const double intensity = led->GetIntensity();
  //Fraction red/green/blue
  const double fR = static_cast<double>(red  ) / 255.0;
  const double fG = static_cast<double>(green) / 255.0;
  const double fB = static_cast<double>(blue ) / 255.0;

  {
    //Draw circle and major surface
    const double maxBrightness = 0.66 * 255.0;
    const double minBrightness = 0.25 * maxBrightness;
    const int r = static_cast<int>(
      minBrightness + (fR * intensity * (maxBrightness - minBrightness) ) );
    const int g = static_cast<int>(
      minBrightness + (fG * intensity * (maxBrightness - minBrightness) ) );
    const int b = static_cast<int>(
      minBrightness + (fB * intensity * (maxBrightness - minBrightness) ) );
    assert( r >= 0 ); assert( r < 256);
    assert( g >= 0 ); assert( g < 256);
    assert( b >= 0 ); assert( b < 256);
    const int pen_width = 1 + (std::min(width,height) / 25);
    assert(pen_width > 0);
    {
      QPen pen = painter.pen();
      pen.setWidth(pen_width);
      pen.setColor(QColor(0,0,0));
      painter.setPen(pen);
    }
    painter.setBrush(QBrush(QColor(r,g,b)));
    painter.drawEllipse(
      left + pen_width,
      top + pen_width,
      width  - (4 * pen_width),
      height - (4 * pen_width)
    );
  }
  {
    //Draw topleft smaller lighter surface
    const double maxBrightness = 1.00 * 255.0;
    const double minBrightness = 0.25 * maxBrightness;
    const int r = static_cast<int>(
      minBrightness + (fR * intensity * (maxBrightness - minBrightness) ) );
    const int g = static_cast<int>(
      minBrightness + (fG * intensity * (maxBrightness - minBrightness) ) );
    const int b = static_cast<int>(
      minBrightness + (fB * intensity * (maxBrightness - minBrightness) ) );
    assert( r >= 0 ); assert( r < 256);
    assert( g >= 0 ); assert( g < 256);
    assert( b >= 0 ); assert( b < 256);
    const int x = left + (width  / 2) - (0.707 * static_cast<double>(width ) * 0.5);
    const int y = top  + (height / 2) - (0.707 * static_cast<double>(height) * 0.5);
    const int w = (0.707 * static_cast<double>(width ) * 0.5);
    const int h = (0.707 * static_cast<double>(height) * 0.5);
    {
      QPen pen = painter.pen();
      pen.setWidth(1);
      pen.setColor(QColor(r,g,b));
      painter.setPen(pen);
    }
    painter.setBrush(QBrush(QColor(r,g,b)));
    painter.drawEllipse(x,y,w,h);
  }
  {
    //Draw bottomright smaller darker surface
    const double maxBrightness = 0.33 * 255.0;
    const double minBrightness = 0.25 * maxBrightness;
    const int r = static_cast<int>(
      minBrightness + (fR * intensity * (maxBrightness - minBrightness) ) );
    const int g = static_cast<int>(
      minBrightness + (fG * intensity * (maxBrightness - minBrightness) ) );
    const int b = static_cast<int>(
      minBrightness + (fB * intensity * (maxBrightness - minBrightness) ) );
    assert( r >= 0 ); assert( r < 256);
    assert( g >= 0 ); assert( g < 256);
    assert( b >= 0 ); assert( b < 256);
    const int x = left + (width  / 2) - 1;
    const int y = top  + (height / 2) - 1;
    const int w = (0.707 * static_cast<double>(width ) * 0.5);
    const int h = (0.707 * static_cast<double>(height) * 0.5);

    {
      QPen pen = painter.pen();
      pen.setWidth(1);
      pen.setColor(QColor(r,g,b));
      painter.setPen(pen);
    }
    painter.setBrush(QBrush(QColor(r,g,b)));
    painter.drawEllipse(x,y,w,h);
  }
}

void ribi::QtLedWidget::DrawLed(
  QPainter& painter,
  const boost::shared_ptr<const LedWidget> widget)
{
  DrawLed(
    painter,
    widget->GetLeft(),
    widget->GetTop(),
    widget->GetWidth(),
    widget->GetHeight(),
    widget->GetLed()
  );
}

std::string ribi::QtLedWidget::GetVersion() noexcept
{
  return "1.5";
}

std::vector<std::string> ribi::QtLedWidget::GetVersionHistory() noexcept
{
  return {
    "2011-04-10: Version 1.0: initial version",
    "2011-04-11: Version 1.1: added About information",
    "2011-09-08: Version 1.2: removed redundant signals",
    "2012-07-07: Version 1.3: added resizeEvent",
    "2012-08-26: Version 1.4: fixed bug in resizeEvent",
    "2014-03-28: Version 1.5: replaced custom Rect class by Boost.Geometry"
  };
}

void ribi::QtLedWidget::OnResize()
{
  this->setGeometry(
    this->GetWidget()->GetLeft(),
    this->GetWidget()->GetTop(),
    this->GetWidget()->GetWidth(),
    this->GetWidget()->GetHeight()
  );
  this->repaint();
}

void ribi::QtLedWidget::paintEvent(QPaintEvent *)
{
  QPainter p(this);
  DrawLed(p,m_widget);
}

void ribi::QtLedWidget::resizeEvent(QResizeEvent * )
{
  m_widget->SetGeometry(
    x(), //geometry().left(),
    y(), //geometry().top(),
    width(), //geometry().width(),
    height() //geometry().height()
  );

  //  0, //x(), //geometry().left(),
  //  0, //y(), //geometry().top(),
  //  width(), //geometry().width(),
  //  height() //geometry().height()
}

 

 

 

 

 

./CppQtLedWidget/qtledwidgetdialog.h

 

//---------------------------------------------------------------------------
/*
QtLedWidget, Qt widget for displaying the Led 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/CppQtLedWidget.htm
//---------------------------------------------------------------------------
#ifndef QTLEDDIALOG_H
#define QTLEDDIALOG_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-parameter"
#include <boost/shared_ptr.hpp>
#include "qthideandshowdialog.h"
#pragma GCC diagnostic pop

namespace Ui {
  class QtLedWidgetDialog;
}

namespace ribi {

struct LedWidget;

///Displays an LED as a dialog
class QtLedWidgetDialog : public QDialog
{
  Q_OBJECT

public:
  explicit QtLedWidgetDialog(QWidget *parent = 0);
  QtLedWidgetDialog(const QtLedWidgetDialog&) = delete;
  QtLedWidgetDialog& operator=(const QtLedWidgetDialog&) = delete;
  ~QtLedWidgetDialog();

  void SetWidget(const boost::shared_ptr<LedWidget>& widget) noexcept;
  boost::shared_ptr<LedWidget> GetWidget() const noexcept { return m_widget; }


private:
  Ui::QtLedWidgetDialog *ui;

  ///The widget to work on
  boost::shared_ptr<LedWidget> m_widget;
};

} //~namespace ribi

#endif // QTLEDDIALOG_H

 

 

 

 

 

./CppQtLedWidget/qtledwidgetdialog.cpp

 

//---------------------------------------------------------------------------
/*
QtLedWidget, Qt widget for displaying the Led 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/CppQtLedWidget.htm
//---------------------------------------------------------------------------
#include "qtledwidgetdialog.h"

#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 <cassert>

#include "ledwidget.h"
#include "ui_qtledwidgetdialog.h"
#pragma GCC diagnostic pop

ribi::QtLedWidgetDialog::QtLedWidgetDialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::QtLedWidgetDialog),
  m_widget{}
{
  ui->setupUi(this);
}

ribi::QtLedWidgetDialog::~QtLedWidgetDialog()
{
  delete ui;
}

void ribi::QtLedWidgetDialog::SetWidget(const boost::shared_ptr<LedWidget>& /* widget */) noexcept
{
  /*
  const bool verbose{false};

  assert(widget);
  if (m_widget == widget)
  {
    return;
  }
  if (verbose)
  {
    std::stringstream s;
    s << "Setting widget '" << widget->ToStr() << "'\n";
  }

  const auto competency_after  = widget->GetL
  const auto is_complex_after  = widget->GetIsComplex();
  const auto is_concrete_after = widget->GetIsConcrete();
  const auto is_specific_after = widget->GetIsSpecific();
  const auto text_after        = widget->GetText();


  bool competency_changed  = true;
  bool is_complex_changed  = true;
  bool is_concrete_changed = true;
  bool is_specific_changed = true;
  bool text_changed = true;

  if (m_widget)
  {
    const auto competency_before  = m_widget->GetCompetency();
    const auto is_complex_before  = m_widget->GetIsComplex();
    const auto is_concrete_before = m_widget->GetIsConcrete();
    const auto is_specific_before = m_widget->GetIsSpecific();
    const auto text_before        = m_widget->GetText();

    competency_changed  = competency_before != competency_after;
    is_complex_changed  = is_complex_before != is_complex_after;
    is_concrete_changed = is_concrete_before != is_concrete_after;
    is_specific_changed = is_specific_before != is_specific_after;
    text_changed = text_before != text_after;


    if (verbose)
    {
      if (competency_changed)
      {
        std::stringstream s;
        s
          << "Competency will change from "
          << Competencies().ToStr(competency_before)
          << " to "
          << Competencies().ToStr(competency_after)
          << '\n'
        ;
        TRACE(s.str());
      }
      if (is_complex_changed)
      {
        std::stringstream s;
        s << "IsComplex will change from " << is_complex_before
          << " to " << is_complex_after << '\n';
        TRACE(s.str());
      }
      if (is_concrete_changed)
      {
        std::stringstream s;
        s << "IsConcrete will change from " << is_concrete_before
          << " to " << is_concrete_after << '\n';
        TRACE(s.str());
      }
      if (is_specific_changed)
      {
        std::stringstream s;
        s << "IsSpecific will change from " << is_specific_before
          << " to " << is_specific_after << '\n';
        TRACE(s.str());
      }
      if (text_changed)
      {
        std::stringstream s;
        s << "Text will change from '" << text_before
          << "' to '" << text_after << "'\n";
        TRACE(s.str());
      }
    }

    //Disconnect m_widget
    m_widget->m_signal_competency_changed.disconnect(
      boost::bind(&ribi::cmap::QtExampleDialog::OnCompetencyChanged,this,boost::lambda::_1)
    );
    m_widget->m_signal_is_complex_changed.disconnect(
      boost::bind(&ribi::cmap::QtExampleDialog::OnIsComplexChanged,this,boost::lambda::_1)
    );
    m_widget->m_signal_is_concrete_changed.disconnect(
      boost::bind(&ribi::cmap::QtExampleDialog::OnIsConcreteChanged,this,boost::lambda::_1)
    );
    m_widget->m_signal_is_specific_changed.disconnect(
      boost::bind(&ribi::cmap::QtExampleDialog::OnIsSpecificChanged,this,boost::lambda::_1)
    );
    m_widget->m_signal_text_changed.disconnect(
      boost::bind(&ribi::cmap::QtExampleDialog::OnTextChanged,this,boost::lambda::_1)
    );
  }

  //Replace m_widget by the new one
  m_widget = widget;


  assert(m_widget->GetCompetency() == competency_after );
  assert(m_widget->GetIsComplex()  == is_complex_after );
  assert(m_widget->GetIsConcrete() == is_concrete_after);
  assert(m_widget->GetIsSpecific() == is_specific_after);
  assert(m_widget->GetText()       == text_after       );

  m_widget->m_signal_competency_changed.connect(
    boost::bind(&ribi::cmap::QtExampleDialog::OnCompetencyChanged,this,boost::lambda::_1)
  );
  m_widget->m_signal_is_complex_changed.connect(
    boost::bind(&ribi::cmap::QtExampleDialog::OnIsComplexChanged,this,boost::lambda::_1)
  );
  m_widget->m_signal_is_concrete_changed.connect(
    boost::bind(&ribi::cmap::QtExampleDialog::OnIsConcreteChanged,this,boost::lambda::_1)
  );
  m_widget->m_signal_is_specific_changed.connect(
    boost::bind(&ribi::cmap::QtExampleDialog::OnIsSpecificChanged,this,boost::lambda::_1)
  );
  m_widget->m_signal_text_changed.connect(
    boost::bind(&ribi::cmap::QtExampleDialog::OnTextChanged,this,boost::lambda::_1)
  );

  //Emit everything that has changed
  if (competency_changed)
  {
    m_widget->m_signal_competency_changed(m_widget.get());
  }
  if (is_complex_changed)
  {
    m_widget->m_signal_is_complex_changed(m_widget.get());
  }
  if (is_concrete_changed)
  {
    m_widget->m_signal_is_concrete_changed(m_widget.get());
  }
  if (is_specific_changed)
  {
    m_widget->m_signal_is_specific_changed(m_widget.get());
  }
  if (text_changed)
  {
    m_widget->m_signal_text_changed(m_widget.get());
  }
  assert( widget ==  m_widget);
  assert(*widget == *m_widget);
  */
}

 

 

 

 

 

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